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