JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / domiterator / plugin.js
index 5c092bb..f68f7c0 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2011, 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
@@ -63,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
@@ -76,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
@@ -279,7 +284,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                                                range.insertNode( block );\r
 \r
                                                removePreviousBr = removeLastBr = true;\r
-                                       }\r
+                               }\r
                                else if ( block.getName() != 'li' )\r
                                {\r
                                        // If the range doesn't includes the entire contents of the\r
@@ -346,7 +351,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                        // next interation.\r
                        if ( !this._.nextNode )\r
                        {\r
-                               this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null :\r
+                               this._.nextNode = ( isLast || block.equals( lastNode ) || !lastNode ) ? null :\r
                                        getNextSourceNode( block, 1, lastNode );\r
                        }\r
 \r