From 8bd0d385eb178d69d045606853ad309763117445 Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Wed, 2 May 2012 12:54:42 -0700 Subject: [PATCH] Added test for whitespacePattern, getDomText and getDomHash in ce * Also fixed some naming typos in the units under test Change-Id: I17f753a122f94dd16f7783f60b5c0eb1c6d3e480 --- modules/ve2/ce/ve.ce.js | 34 ++++++++++++++++++---------------- tests/ve2/ce/ve.ce.test.js | 24 ++++++++++++++++++++++++ tests/ve2/index.html | 1 + 3 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 tests/ve2/ce/ve.ce.test.js diff --git a/modules/ve2/ce/ve.ce.js b/modules/ve2/ce/ve.ce.js index d3b64faad0..7a4374fde3 100644 --- a/modules/ve2/ce/ve.ce.js +++ b/modules/ve2/ce/ve.ce.js @@ -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 += ''; } diff --git a/tests/ve2/ce/ve.ce.test.js b/tests/ve2/ce/ve.ce.test.js new file mode 100644 index 0000000000..e356341d4e --- /dev/null +++ b/tests/ve2/ce/ve.ce.test.js @@ -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( + $( 'abcd' )[0] ), + 'abcd' + ); +} ); + +test( 'getDOMHash', 1, function() { + equal( + ve.ce.getDomHash( $( 'abcd' )[0] ), + '####' + ); +} ); diff --git a/tests/ve2/index.html b/tests/ve2/index.html index e507916b7b..645869fd83 100644 --- a/tests/ve2/index.html +++ b/tests/ve2/index.html @@ -91,6 +91,7 @@ +