JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / core / dom / rangelist.js
index 9c12e48..f29cc5f 100644 (file)
@@ -39,7 +39,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        createIterator : function()\r
                        {\r
                                var rangeList = this,\r
-                                       bookmarks = [],\r
+                                       bookmark = CKEDITOR.dom.walker.bookmark(),\r
+                                       guard = function( node ) { return ! ( node.is && node.is( 'tr' ) ); },\r
+                                               bookmarks = [],\r
                                        current;\r
 \r
                                /**\r
@@ -49,8 +51,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                        /**\r
                                         * Retrieves the next range in the list.\r
+                                        * @param {Boolean} mergeConsequent Whether join two adjacent ranges into single, e.g. consequent table cells.\r
                                         */\r
-                                       getNextRange : function()\r
+                                       getNextRange : function( mergeConsequent )\r
                                        {\r
                                                current = current == undefined ? 0 : current + 1;\r
 \r
@@ -65,11 +68,59 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        if ( !current )\r
                                                        {\r
                                                                // Make sure bookmark correctness by reverse processing.\r
-                                                               for ( var i = rangeList.length - 1; i > 0; i-- )\r
+                                                               for ( var i = rangeList.length - 1; i >= 0; i-- )\r
                                                                        bookmarks.unshift( rangeList[ i ].createBookmark( true ) );\r
                                                        }\r
-                                                       else\r
-                                                               range.moveToBookmark( bookmarks.shift() );\r
+\r
+                                                       if ( mergeConsequent )\r
+                                                       {\r
+                                                               // Figure out how many ranges should be merged.\r
+                                                               var mergeCount = 0;\r
+                                                               while ( rangeList[ current + mergeCount + 1 ] )\r
+                                                               {\r
+                                                                       var doc = range.document,\r
+                                                                               found = 0,\r
+                                                                               left =  doc.getById( bookmarks[ mergeCount ].endNode ),\r
+                                                                               right = doc.getById( bookmarks[ mergeCount + 1 ].startNode ),\r
+                                                                               next;\r
+\r
+                                                                       // Check subsequent range.\r
+                                                                       while ( 1 )\r
+                                                                       {\r
+                                                                               next = left.getNextSourceNode( false );\r
+                                                                               if ( !right.equals( next ) )\r
+                                                                               {\r
+                                                                                       // This could be yet another bookmark or\r
+                                                                                       // walking across block boundaries.\r
+                                                                                       if ( bookmark( next ) || ( next.type == CKEDITOR.NODE_ELEMENT && next.isBlockBoundary() ) )\r
+                                                                                       {\r
+                                                                                               left = next;\r
+                                                                                               continue;\r
+                                                                                       }\r
+                                                                               }\r
+                                                                               else\r
+                                                                                       found = 1;\r
+\r
+                                                                               break;\r
+                                                                       }\r
+\r
+                                                                       if ( !found )\r
+                                                                               break;\r
+\r
+                                                                       mergeCount++;\r
+                                                               }\r
+                                                       }\r
+\r
+                                                       range.moveToBookmark( bookmarks.shift() );\r
+\r
+                                                       var next;\r
+                                                       // Merge ranges finally after moving to bookmarks.\r
+                                                       while( mergeCount-- )\r
+                                                       {\r
+                                                               next = rangeList[ ++current ];\r
+                                                               next.moveToBookmark( bookmarks.shift() );\r
+                                                               range.setEnd( next.endContainer, next.endOffset );\r
+                                                       }\r
                                                }\r
 \r
                                                return range;\r