0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -05:00

Trim meaningless white space from text nodes in cleanTree fn.

If a text node at the beinning of a block began with white-space, it would mean
some situations where we should be doing all the transformations for
enter/delete/backspace were being left to the browser.
This commit is contained in:
Neil Jenkins 2014-04-25 10:59:30 +10:00
parent 0d5398868f
commit 996a0dd672
3 changed files with 60 additions and 14 deletions

View file

@ -2371,7 +2371,8 @@ var removeEmptyInlines = function ( root ) {
*/ */
var cleanTree = function ( node, allowStyles ) { var cleanTree = function ( node, allowStyles ) {
var children = node.childNodes, var children = node.childNodes,
i, l, child, nodeName, nodeType, rewriter, childLength; i, l, child, nodeName, nodeType, rewriter, childLength,
data, j, ll;
for ( i = 0, l = children.length; i < l; i += 1 ) { for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i]; child = children[i];
nodeName = child.nodeName; nodeName = child.nodeName;
@ -2393,11 +2394,33 @@ var cleanTree = function ( node, allowStyles ) {
if ( childLength ) { if ( childLength ) {
cleanTree( child, allowStyles ); cleanTree( child, allowStyles );
} }
} else if ( nodeType !== TEXT_NODE || ( } else {
!( notWS.test( child.data ) ) && if ( nodeType === TEXT_NODE ) {
!( i > 0 && isInline( children[ i - 1 ] ) ) && data = child.data;
!( i + 1 < l && isInline( children[ i + 1 ] ) ) if ( notWS.test( data ) ) {
) ) { j = 0;
ll = data.length;
if ( !i || !isInline( children[ i - 1 ] ) ) {
while ( j < ll && !notWS.test( data.charAt( j ) ) ) {
j += 1;
}
if ( j ) {
child.data = data = data.slice( j );
ll -= j;
}
}
if ( i + 1 === l || !isInline( children[ i + 1 ] ) ) {
j = ll - 1;
while ( j >= 0 && !notWS.test( data.charAt( j ) ) ) {
j -= 1;
}
if ( j < ll - 1 ) {
child.data = data.slice( 0, j );
}
}
continue;
}
}
node.removeChild( child ); node.removeChild( child );
i -= 1; i -= 1;
l -= 1; l -= 1;

File diff suppressed because one or more lines are too long

View file

@ -1309,7 +1309,8 @@ var removeEmptyInlines = function ( root ) {
*/ */
var cleanTree = function ( node, allowStyles ) { var cleanTree = function ( node, allowStyles ) {
var children = node.childNodes, var children = node.childNodes,
i, l, child, nodeName, nodeType, rewriter, childLength; i, l, child, nodeName, nodeType, rewriter, childLength,
data, j, ll;
for ( i = 0, l = children.length; i < l; i += 1 ) { for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i]; child = children[i];
nodeName = child.nodeName; nodeName = child.nodeName;
@ -1331,11 +1332,33 @@ var cleanTree = function ( node, allowStyles ) {
if ( childLength ) { if ( childLength ) {
cleanTree( child, allowStyles ); cleanTree( child, allowStyles );
} }
} else if ( nodeType !== TEXT_NODE || ( } else {
!( notWS.test( child.data ) ) && if ( nodeType === TEXT_NODE ) {
!( i > 0 && isInline( children[ i - 1 ] ) ) && data = child.data;
!( i + 1 < l && isInline( children[ i + 1 ] ) ) if ( notWS.test( data ) ) {
) ) { j = 0;
ll = data.length;
if ( !i || !isInline( children[ i - 1 ] ) ) {
while ( j < ll && !notWS.test( data.charAt( j ) ) ) {
j += 1;
}
if ( j ) {
child.data = data = data.slice( j );
ll -= j;
}
}
if ( i + 1 === l || !isInline( children[ i + 1 ] ) ) {
j = ll - 1;
while ( j >= 0 && !notWS.test( data.charAt( j ) ) ) {
j -= 1;
}
if ( j < ll - 1 ) {
child.data = data.slice( 0, j );
}
}
continue;
}
}
node.removeChild( child ); node.removeChild( child );
i -= 1; i -= 1;
l -= 1; l -= 1;