JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / domiterator / plugin.js
index ded0a32..9fc5fd5 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -11,18 +11,20 @@ CKEDITOR.plugins.add( 'domiterator' );
 \r
 (function()\r
 {\r
-\r
+       /**\r
+        * @name CKEDITOR.dom.iterator\r
+        */\r
        function iterator( range )\r
        {\r
                if ( arguments.length < 1 )\r
                        return;\r
 \r
                this.range = range;\r
-               this.forceBrBreak = false;\r
+               this.forceBrBreak = 0;\r
 \r
                // Whether include <br>s into the enlarged range.(#3730).\r
-               this.enlargeBr = true;\r
-               this.enforceRealBlocks = false;\r
+               this.enlargeBr = 1;\r
+               this.enforceRealBlocks = 0;\r
 \r
                this._ || ( this._ = {} );\r
        }\r
@@ -42,6 +44,9 @@ CKEDITOR.plugins.add( 'domiterator' );
                        // Indicats that the current element in the loop is the last one.\r
                        var isLast;\r
 \r
+                       // Indicate at least one of the range boundaries is inside a preformat block.\r
+                       var touchPre;\r
+\r
                        // Instructs to cleanup remaining BRs.\r
                        var removePreviousBr, removeLastBr;\r
 \r
@@ -49,7 +54,14 @@ CKEDITOR.plugins.add( 'domiterator' );
                        if ( !this._.lastNode )\r
                        {\r
                                range = this.range.clone();\r
-                               range.enlarge( this.forceBrBreak || !this.enlargeBr ?\r
+\r
+                               // Shrink the range to exclude harmful "noises" (#4087, #4450, #5435).\r
+                               range.shrink( CKEDITOR.NODE_ELEMENT, true );\r
+\r
+                               touchPre = range.endContainer.hasAscendant( 'pre', true )\r
+                                       || range.startContainer.hasAscendant( 'pre', true );\r
+\r
+                               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
@@ -68,7 +80,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                                // next block.(#3887)\r
                                if ( this._.lastNode &&\r
                                                this._.lastNode.type == CKEDITOR.NODE_TEXT &&\r
-                                               !CKEDITOR.tools.trim( this._.lastNode.getText( ) ) &&\r
+                                               !CKEDITOR.tools.trim( this._.lastNode.getText() ) &&\r
                                                this._.lastNode.getParent().isBlockBoundary() )\r
                                {\r
                                        var testRange = new CKEDITOR.dom.range( range.document );\r
@@ -100,12 +112,13 @@ CKEDITOR.plugins.add( 'domiterator' );
                        {\r
                                // closeRange indicates that a paragraph boundary has been found,\r
                                // so the range can be closed.\r
-                               var closeRange = false;\r
+                               var closeRange = 0,\r
+                                       parentPre = currentNode.hasAscendant( 'pre' );\r
 \r
                                // includeNode indicates that the current node is good to be part\r
                                // of the range. By default, any non-element node is ok for it.\r
                                var includeNode = ( currentNode.type != CKEDITOR.NODE_ELEMENT ),\r
-                                       continueFromSibling = false;\r
+                                       continueFromSibling = 0;\r
 \r
                                // If it is an element node, let's check if it can be part of the\r
                                // range.\r
@@ -113,12 +126,13 @@ CKEDITOR.plugins.add( 'domiterator' );
                                {\r
                                        var nodeName = currentNode.getName();\r
 \r
-                                       if ( currentNode.isBlockBoundary( this.forceBrBreak && { br : 1 } ) )\r
+                                       if ( currentNode.isBlockBoundary( this.forceBrBreak &&\r
+                                                       !parentPre && { br : 1 } ) )\r
                                        {\r
                                                // <br> boundaries must be part of the range. It will\r
                                                // happen only if ForceBrBreak.\r
                                                if ( nodeName == 'br' )\r
-                                                       includeNode = true;\r
+                                                       includeNode = 1;\r
                                                else if ( !range && !currentNode.getChildCount() && nodeName != 'hr' )\r
                                                {\r
                                                        // If we have found an empty block, and haven't started\r
@@ -140,7 +154,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                                                                this._.nextNode = currentNode;\r
                                                }\r
 \r
-                                               closeRange = true;\r
+                                               closeRange = 1;\r
                                        }\r
                                        else\r
                                        {\r
@@ -157,7 +171,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                                                        currentNode = currentNode.getFirst();\r
                                                        continue;\r
                                                }\r
-                                               includeNode = true;\r
+                                               includeNode = 1;\r
                                        }\r
                                }\r
                                else if ( currentNode.type == CKEDITOR.NODE_TEXT )\r
@@ -165,7 +179,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                                        // Ignore normal whitespaces (i.e. not including &nbsp; or\r
                                        // other unicode whitespaces) before/after a block node.\r
                                        if ( beginWhitespaceRegex.test( currentNode.getText() ) )\r
-                                               includeNode = false;\r
+                                               includeNode = 0;\r
                                }\r
 \r
                                // The current node is good to be part of the range and we are\r
@@ -187,17 +201,18 @@ CKEDITOR.plugins.add( 'domiterator' );
                                        {\r
                                                var parentNode = currentNode.getParent();\r
 \r
-                                               if ( parentNode.isBlockBoundary( this.forceBrBreak && { br : 1 } ) )\r
+                                               if ( parentNode.isBlockBoundary( this.forceBrBreak\r
+                                                               && !parentPre && { br : 1 } ) )\r
                                                {\r
-                                                       closeRange = true;\r
+                                                       closeRange = 1;\r
                                                        isLast = isLast || ( parentNode.equals( lastNode) );\r
                                                        break;\r
                                                }\r
 \r
                                                currentNode = parentNode;\r
-                                               includeNode = true;\r
+                                               includeNode = 1;\r
                                                isLast = ( currentNode.equals( lastNode ) );\r
-                                               continueFromSibling = true;\r
+                                               continueFromSibling = 1;\r
                                        }\r
                                }\r
 \r
@@ -210,26 +225,8 @@ CKEDITOR.plugins.add( 'domiterator' );
 \r
                                // We have found a block boundary. Let's close the range and move out of the\r
                                // loop.\r
-                               if ( ( closeRange || isLast ) && range )\r
-                               {\r
-                                       var boundaryNodes = range.getBoundaryNodes(),\r
-                                               startPath = new CKEDITOR.dom.elementPath( range.startContainer );\r
-\r
-                                       // Drop the range if it only contains bookmark nodes, and is\r
-                                       // not because of the original collapsed range. (#4087,#4450)\r
-                                       if ( boundaryNodes.startNode.getParent().equals( startPath.blockLimit )\r
-                                                && isBookmark( boundaryNodes.startNode ) && isBookmark( boundaryNodes.endNode ) )\r
-                                       {\r
-                                               range = null;\r
-                                               this._.nextNode = null;\r
-                                       }\r
-                                       else\r
+                               if ( isLast || ( closeRange && range ) )\r
                                                break;\r
-                               }\r
-\r
-                               if ( isLast )\r
-                                       break;\r
-\r
                        }\r
 \r
                        // Now, based on the processed range, look for (or create) the block to be returned.\r
@@ -243,7 +240,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                                        return null;\r
                                }\r
 \r
-                               startPath = new CKEDITOR.dom.elementPath( range.startContainer );\r
+                               var startPath = new CKEDITOR.dom.elementPath( range.startContainer );\r
                                var startBlockLimit = startPath.blockLimit,\r
                                        checkLimits = { div : 1, th : 1, td : 1 };\r
                                block = startPath.block;\r
@@ -305,6 +302,9 @@ CKEDITOR.plugins.add( 'domiterator' );
                                }\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
@@ -319,9 +319,6 @@ CKEDITOR.plugins.add( 'domiterator' );
 \r
                        if ( removeLastBr )\r
                        {\r
-                               // Ignore bookmark nodes.(#3783)\r
-                               var bookmarkGuard = CKEDITOR.dom.walker.bookmark( false, true );\r
-\r
                                var lastChild = block.getLast();\r
                                if ( lastChild && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.getName() == 'br' )\r
                                {\r
@@ -342,6 +339,12 @@ CKEDITOR.plugins.add( 'domiterator' );
                                        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
+                       }\r
+\r
                        return block;\r
                }\r
        };\r