Added test for whitespacePattern, getDomText and getDomHash in ce

* Also fixed some naming typos in the units under test

Change-Id: I17f753a122f94dd16f7783f60b5c0eb1c6d3e480
This commit is contained in:
Trevor Parscal 2012-05-02 12:54:42 -07:00
parent fee375cf4e
commit 8bd0d385eb
3 changed files with 43 additions and 16 deletions

View file

@ -28,26 +28,27 @@ ve.ce.whitespacePattern = /[\u0020\u00A0]/g;
*
* @static
* @member
* @param {String} Plain text of DOM element
* @param {DOMElement} element DOM element to get text of
* @returns {String} Plain text of DOM element
*/
ve.ce.getDOMText = function( elem ) {
var nodeType = elem.nodeType,
ve.ce.getDomText = function( element ) {
var nodeType = element.nodeType,
text = '';
if ( nodeType === 1 || nodeType === 9 ) {
// Use textContent || innerText for elements
if ( typeof elem.textContent === 'string' ) {
return elem.textContent;
} else if ( typeof elem.innerText === 'string' ) {
if ( typeof element.textContent === 'string' ) {
return element.textContent;
} else if ( typeof element.innerText === 'string' ) {
// Replace IE's carriage returns
return elem.innerText.replace( /\r\n/g, '' );
return element.innerText.replace( /\r\n/g, '' );
} else {
// Traverse it's children
for ( elem = elem.firstChild; elem; elem = elem.nextSibling) {
text += ve.ce.Surface.getDOMText( elem );
for ( element = element.firstChild; element; element = element.nextSibling) {
text += ve.ce.getDomText( element );
}
}
} else if ( nodeType === 3 || nodeType === 4 ) {
return elem.nodeValue;
return element.nodeValue;
}
// Return the text, replacing spaces and non-breaking spaces with spaces?
// TODO: Why are we replacing spaces (\u0020) with spaces (' ')
@ -64,11 +65,12 @@ ve.ce.getDOMText = function( elem ) {
*
* @static
* @member
* @param {String} Hash of DOM element
* @param {DOMElement} element DOM element to get hash of
* @returns {String} Hash of DOM element
*/
ve.ce.getDOMHash = function( elem ) {
var nodeType = elem.nodeType,
nodeName = elem.nodeName,
ve.ce.getDomHash = function( element ) {
var nodeType = element.nodeType,
nodeName = element.nodeName,
hash = '';
if ( nodeType === 3 || nodeType === 4 ) {
@ -76,8 +78,8 @@ ve.ce.getDOMHash = function( elem ) {
} else if ( nodeType === 1 || nodeType === 9 ) {
hash += '<' + nodeName + '>';
// Traverse it's children
for ( elem = elem.firstChild; elem; elem = elem.nextSibling) {
hash += ve.ce.Surface.getDOMHash( elem );
for ( element = element.firstChild; element; element = element.nextSibling) {
hash += ve.ce.getDomHash( element );
}
hash += '</' + nodeName + '>';
}

View file

@ -0,0 +1,24 @@
module( 've.ce' );
/* Tests */
test( 'whitespacePattern', 4, function() {
equal( 'a b'.match( ve.ce.whitespacePattern ), ' ', 'matches spaces' );
equal( 'a\u00A0b'.match( ve.ce.whitespacePattern ), '\u00A0', 'matches non-breaking spaces' );
equal( 'a\tb'.match( ve.ce.whitespacePattern ), null, 'does not match tab' );
equal( 'ab'.match( ve.ce.whitespacePattern ), null, 'does not match non-whitespace' );
} );
test( 'getDOMText', 1, function() {
equal( ve.ce.getDomText(
$( '<span>a<b><a href="#">b</a></b><span></span><i>c</i>d</span>' )[0] ),
'abcd'
);
} );
test( 'getDOMHash', 1, function() {
equal(
ve.ce.getDomHash( $( '<span>a<b><a href="#">b</a></b><span></span><i>c</i>d</span>' )[0] ),
'<SPAN>#<B><A>#</A></B><SPAN></SPAN><I>#</I>#</SPAN>'
);
} );

View file

@ -91,6 +91,7 @@
<script src="dm/ve.dm.DocumentFragment.test.js"></script>
<script src="dm/ve.dm.Transaction.test.js"></script>
<!-- VisualEditor ContentEditable Tests -->
<script src="ce/ve.ce.test.js"></script>
<script src="ce/ve.ce.NodeFactory.test.js"></script>
<script src="ce/ve.ce.Node.test.js"></script>
<script src="ce/ve.ce.BranchNode.test.js"></script>