JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / domiterator / plugin.js
index f0b1835..5c092bb 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -20,17 +20,29 @@ CKEDITOR.plugins.add( 'domiterator' );
                        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
 \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
@@ -80,7 +92,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
@@ -112,13 +124,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
-                                               parentPre = currentNode.hasAscendant( 'pre' );\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
@@ -132,7 +144,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                                                // <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
@@ -154,7 +166,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                                                                this._.nextNode = currentNode;\r
                                                }\r
 \r
-                                               closeRange = true;\r
+                                               closeRange = 1;\r
                                        }\r
                                        else\r
                                        {\r
@@ -171,7 +183,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
@@ -179,7 +191,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
@@ -197,22 +209,25 @@ 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
                                                if ( parentNode.isBlockBoundary( this.forceBrBreak\r
                                                                && !parentPre && { br : 1 } ) )\r
                                                {\r
-                                                       closeRange = true;\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
                                                currentNode = parentNode;\r
-                                               includeNode = true;\r
+                                               includeNode = 1;\r
                                                isLast = ( currentNode.equals( lastNode ) );\r
-                                               continueFromSibling = true;\r
+                                               continueFromSibling = 1;\r
                                        }\r
                                }\r
 \r
@@ -220,7 +235,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,15 +271,15 @@ 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
-                               }\r
+                                               removePreviousBr = removeLastBr = true;\r
+                                       }\r
                                else if ( block.getName() != 'li' )\r
                                {\r
                                        // If the range doesn't includes the entire contents of the\r
@@ -297,8 +312,7 @@ 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
@@ -316,9 +330,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
@@ -336,7 +347,7 @@ CKEDITOR.plugins.add( 'domiterator' );
                        if ( !this._.nextNode )\r
                        {\r
                                this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null :\r
-                                       block.getNextSourceNode( true, null, lastNode );\r
+                                       getNextSourceNode( block, 1, lastNode );\r
                        }\r
 \r
                        return block;\r