X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Fdom%2Fwalker.js;h=3d1942596e51d4ce96278d36bdd155fdf396fe30;hb=refs%2Ftags%2Fv3.6.3;hp=7f430b8ad026c1a4ddf66ef0bbcdcf3e7401190f;hpb=e73319a12b56100b29ef456fd74114fe5519e01c;p=ckeditor.git diff --git a/_source/core/dom/walker.js b/_source/core/dom/walker.js index 7f430b8..3d19425 100644 --- a/_source/core/dom/walker.js +++ b/_source/core/dom/walker.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -8,40 +8,50 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // This function is to be called under a "walker" instance scope. function iterate( rtl, breakOnFalse ) { + var range = this.range; + // Return null if we have reached the end. if ( this._.end ) return null; - var node, - range = this.range, - guard, - userGuard = this.guard, - type = this.type, - getSourceNodeFn = ( rtl ? 'getPreviousSourceNode' : 'getNextSourceNode' ); - // This is the first call. Initialize it. if ( !this._.start ) { this._.start = 1; - // Trim text nodes and optmize the range boundaries. DOM changes - // may happen at this point. - range.trim(); - // A collapsed range must return null at first call. if ( range.collapsed ) { this.end(); return null; } + + // Move outside of text node edges. + range.optimize(); } + var node, + startCt = range.startContainer, + endCt = range.endContainer, + startOffset = range.startOffset, + endOffset = range.endOffset, + guard, + userGuard = this.guard, + type = this.type, + getSourceNodeFn = ( rtl ? 'getPreviousSourceNode' : 'getNextSourceNode' ); + // Create the LTR guard function, if necessary. if ( !rtl && !this._.guardLTR ) { - // Gets the node that stops the walker when going LTR. - var limitLTR = range.endContainer, - blockerLTR = limitLTR.getChild( range.endOffset ); + // The node that stops walker from moving up. + var limitLTR = endCt.type == CKEDITOR.NODE_ELEMENT ? + endCt : + endCt.getParent(); + + // The node that stops the walker from going to next. + var blockerLTR = endCt.type == CKEDITOR.NODE_ELEMENT ? + endCt.getChild( endOffset ) : + endCt.getNext(); this._.guardLTR = function( node, movingOut ) { @@ -54,9 +64,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Create the RTL guard function, if necessary. if ( rtl && !this._.guardRTL ) { - // Gets the node that stops the walker when going LTR. - var limitRTL = range.startContainer, - blockerRTL = ( range.startOffset > 0 ) && limitRTL.getChild( range.startOffset - 1 ); + // The node that stops walker from moving up. + var limitRTL = startCt.type == CKEDITOR.NODE_ELEMENT ? + startCt : + startCt.getParent(); + + // The node that stops the walker from going to next. + var blockerRTL = startCt.type == CKEDITOR.NODE_ELEMENT ? + startOffset ? + startCt.getChild( startOffset - 1 ) : null : + startCt.getPrevious(); this._.guardRTL = function( node, movingOut ) { @@ -89,35 +106,33 @@ For licensing, see LICENSE.html or http://ckeditor.com/license else { // Get the first node to be returned. - if ( rtl ) { - node = range.endContainer; + node = endCt; - if ( range.endOffset > 0 ) + if ( node.type == CKEDITOR.NODE_ELEMENT ) { - node = node.getChild( range.endOffset - 1 ); - if ( guard( node ) === false ) - node = null; + if ( endOffset > 0 ) + node = node.getChild( endOffset - 1 ); + else + node = ( guard ( node, true ) === false ) ? + null : node.getPreviousSourceNode( true, type, guard ); } - else - node = ( guard ( node, true ) === false ) ? - null : node.getPreviousSourceNode( true, type, guard ); } else { - node = range.startContainer; - node = node.getChild( range.startOffset ); + node = startCt; - if ( node ) + if ( node.type == CKEDITOR.NODE_ELEMENT ) { - if ( guard( node ) === false ) - node = null; + if ( ! ( node = node.getChild( startOffset ) ) ) + node = ( guard ( startCt, true ) === false ) ? + null : startCt.getNextSourceNode( true, type, guard ) ; } - else - node = ( guard ( range.startContainer, true ) === false ) ? - null : range.startContainer.getNextSourceNode( true, type, guard ) ; } + + if ( node && guard( node ) === false ) + node = null; } while ( node && !this._.end ) @@ -431,6 +446,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }; }; + CKEDITOR.dom.walker.bogus = function( type, isReject ) + { + function nonEmpty( node ) + { + return !isWhitespaces( node ) && !isBookmark( node ); + } + + return function( node ) + { + var parent = node.getParent(), + isBogus = !CKEDITOR.env.ie ? node.is && node.is( 'br' ) : + node.getText && tailNbspRegex.test( node.getText() ); + + isBogus = isBogus && parent.isBlockBoundary() && !!parent.getLast( nonEmpty ); + + return !! ( isReject ^ isBogus ); + }; + }; + var tailNbspRegex = /^[\t\r\n ]*(?: |\xa0)$/, isWhitespaces = CKEDITOR.dom.walker.whitespaces(), isBookmark = CKEDITOR.dom.walker.bookmark(),