X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fdomiterator%2Fplugin.js;h=f68f7c09d3533b72bba37a4f69a5364872a160c4;hb=2f22c0c38f17e75be5541089076885442aaa2377;hp=9fc5fd5400ea537d853c3a93ce1243066a1edbb7;hpb=039a051ccf3901311661022a30afd60fc38130c9;p=ckeditor.git diff --git a/_source/plugins/domiterator/plugin.js b/_source/plugins/domiterator/plugin.js index 9fc5fd5..f68f7c0 100644 --- a/_source/plugins/domiterator/plugin.js +++ b/_source/plugins/domiterator/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, 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 */ @@ -30,7 +30,19 @@ CKEDITOR.plugins.add( 'domiterator' ); } var beginWhitespaceRegex = /^[\r\n\t ]+$/, - isBookmark = CKEDITOR.dom.walker.bookmark(); + // Ignore bookmark nodes.(#3783) + bookmarkGuard = CKEDITOR.dom.walker.bookmark( false, true ), + whitespacesGuard = CKEDITOR.dom.walker.whitespaces( true ), + skipGuard = function( node ) { return bookmarkGuard( node ) && whitespacesGuard( node ); }; + + // Get a reference for the next element, bookmark nodes are skipped. + function getNextSourceNode( node, startFromSibling, lastNode ) + { + var next = node.getNextSourceNode( startFromSibling, null, lastNode ); + while ( !bookmarkGuard( next ) ) + next = next.getNextSourceNode( startFromSibling, null, lastNode ); + return next; + } iterator.prototype = { getNextParagraph : function( blockTag ) @@ -51,7 +63,7 @@ CKEDITOR.plugins.add( 'domiterator' ); var removePreviousBr, removeLastBr; // This is the first iteration. Let's initialize it. - if ( !this._.lastNode ) + if ( !this._.started ) { range = this.range.clone(); @@ -64,44 +76,49 @@ CKEDITOR.plugins.add( 'domiterator' ); range.enlarge( this.forceBrBreak && !touchPre || !this.enlargeBr ? CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS : CKEDITOR.ENLARGE_BLOCK_CONTENTS ); - var walker = new CKEDITOR.dom.walker( range ), - ignoreBookmarkTextEvaluator = CKEDITOR.dom.walker.bookmark( true, true ); - // Avoid anchor inside bookmark inner text. - walker.evaluator = ignoreBookmarkTextEvaluator; - this._.nextNode = walker.next(); - // TODO: It's better to have walker.reset() used here. - walker = new CKEDITOR.dom.walker( range ); - walker.evaluator = ignoreBookmarkTextEvaluator; - var lastNode = walker.previous(); - this._.lastNode = lastNode.getNextSourceNode( true ); - - // We may have an empty text node at the end of block due to [3770]. - // If that node is the lastNode, it would cause our logic to leak to the - // next block.(#3887) - if ( this._.lastNode && - this._.lastNode.type == CKEDITOR.NODE_TEXT && - !CKEDITOR.tools.trim( this._.lastNode.getText() ) && - this._.lastNode.getParent().isBlockBoundary() ) + if ( !range.collapsed ) { - var testRange = new CKEDITOR.dom.range( range.document ); - testRange.moveToPosition( this._.lastNode, CKEDITOR.POSITION_AFTER_END ); - if ( testRange.checkEndOfBlock() ) + var walker = new CKEDITOR.dom.walker( range.clone() ), + ignoreBookmarkTextEvaluator = CKEDITOR.dom.walker.bookmark( true, true ); + // Avoid anchor inside bookmark inner text. + walker.evaluator = ignoreBookmarkTextEvaluator; + this._.nextNode = walker.next(); + // TODO: It's better to have walker.reset() used here. + walker = new CKEDITOR.dom.walker( range.clone() ); + walker.evaluator = ignoreBookmarkTextEvaluator; + var lastNode = walker.previous(); + this._.lastNode = lastNode.getNextSourceNode( true ); + + // We may have an empty text node at the end of block due to [3770]. + // If that node is the lastNode, it would cause our logic to leak to the + // next block.(#3887) + if ( this._.lastNode && + this._.lastNode.type == CKEDITOR.NODE_TEXT && + !CKEDITOR.tools.trim( this._.lastNode.getText() ) && + this._.lastNode.getParent().isBlockBoundary() ) { - var path = new CKEDITOR.dom.elementPath( testRange.endContainer ); - var lastBlock = path.block || path.blockLimit; - this._.lastNode = lastBlock.getNextSourceNode( true ); + var testRange = new CKEDITOR.dom.range( range.document ); + testRange.moveToPosition( this._.lastNode, CKEDITOR.POSITION_AFTER_END ); + if ( testRange.checkEndOfBlock() ) + { + var path = new CKEDITOR.dom.elementPath( testRange.endContainer ); + var lastBlock = path.block || path.blockLimit; + this._.lastNode = lastBlock.getNextSourceNode( true ); + } } - } - // Probably the document end is reached, we need a marker node. - if ( !this._.lastNode ) - { - this._.lastNode = this._.docEndMarker = range.document.createText( '' ); - this._.lastNode.insertAfter( lastNode ); + // Probably the document end is reached, we need a marker node. + if ( !this._.lastNode ) + { + this._.lastNode = this._.docEndMarker = range.document.createText( '' ); + this._.lastNode.insertAfter( lastNode ); + } + + // Let's reuse this variable. + range = null; } - // Let's reuse this variable. - range = null; + this._.started = 1; } var currentNode = this._.nextNode; @@ -197,7 +214,7 @@ CKEDITOR.plugins.add( 'domiterator' ); // to close the range, otherwise we include the parent within it. if ( range && !closeRange ) { - while ( !currentNode.getNext() && !isLast ) + while ( !currentNode.getNext( skipGuard ) && !isLast ) { var parentNode = currentNode.getParent(); @@ -205,7 +222,10 @@ CKEDITOR.plugins.add( 'domiterator' ); && !parentPre && { br : 1 } ) ) { closeRange = 1; + includeNode = 0; isLast = isLast || ( parentNode.equals( lastNode) ); + // Make sure range includes bookmarks at the end of the block. (#7359) + range.setEndAt( parentNode, CKEDITOR.POSITION_BEFORE_END ); break; } @@ -220,7 +240,7 @@ CKEDITOR.plugins.add( 'domiterator' ); if ( includeNode ) range.setEndAt( currentNode, CKEDITOR.POSITION_AFTER_END ); - currentNode = currentNode.getNextSourceNode( continueFromSibling, null, lastNode ); + currentNode = getNextSourceNode ( currentNode, continueFromSibling, lastNode ); isLast = !currentNode; // We have found a block boundary. Let's close the range and move out of the @@ -256,14 +276,14 @@ CKEDITOR.plugins.add( 'domiterator' ); // Create the fixed block. block = this.range.document.createElement( blockTag || 'p' ); - // Move the contents of the temporary range to the fixed block. - range.extractContents().appendTo( block ); - block.trim(); + // Move the contents of the temporary range to the fixed block. + range.extractContents().appendTo( block ); + block.trim(); - // Insert the fixed block into the DOM. - range.insertNode( block ); + // Insert the fixed block into the DOM. + range.insertNode( block ); - removePreviousBr = removeLastBr = true; + removePreviousBr = removeLastBr = true; } else if ( block.getName() != 'li' ) { @@ -297,14 +317,10 @@ CKEDITOR.plugins.add( 'domiterator' ); // the current range, which could be an
  • child (nested // lists) or the next sibling
  • . - this._.nextNode = ( block.equals( lastNode ) ? null : - range.getBoundaryNodes().endNode.getNextSourceNode( true, null, lastNode ) ); + this._.nextNode = ( block.equals( lastNode ) ? null : getNextSourceNode( range.getBoundaryNodes().endNode, 1, lastNode ) ); } } - // Ignore bookmark nodes.(#3783) - var bookmarkGuard = CKEDITOR.dom.walker.bookmark( false, true ); - if ( removePreviousBr ) { var previousSibling = block.getPrevious(); @@ -335,14 +351,8 @@ CKEDITOR.plugins.add( 'domiterator' ); // next interation. if ( !this._.nextNode ) { - this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null : - block.getNextSourceNode( true, null, lastNode ); - } - - if ( !bookmarkGuard( this._.nextNode ) ) - { - this._.nextNode = this._.nextNode.getNextSourceNode( true, null, function( node ) - { return !node.equals( lastNode ) && bookmarkGuard( node ); } ); + this._.nextNode = ( isLast || block.equals( lastNode ) || !lastNode ) ? null : + getNextSourceNode( block, 1, lastNode ); } return block;