X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Fdom%2Frangelist.js;h=38788a8325e2e43fd6a301b1e10460175509bdb1;hb=a272c66d841421f8bf933c16535bdcde1c4649fc;hp=9c12e48a24f76518c4db842d1c94f7aefa42956f;hpb=e371ddf8abcb89013e20e6d0dd746adec344d0e5;p=ckeditor.git diff --git a/_source/core/dom/rangelist.js b/_source/core/dom/rangelist.js index 9c12e48..38788a8 100644 --- a/_source/core/dom/rangelist.js +++ b/_source/core/dom/rangelist.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -39,7 +39,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license createIterator : function() { var rangeList = this, - bookmarks = [], + bookmark = CKEDITOR.dom.walker.bookmark(), + guard = function( node ) { return ! ( node.is && node.is( 'tr' ) ); }, + bookmarks = [], current; /** @@ -49,8 +51,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license /** * Retrieves the next range in the list. + * @param {Boolean} mergeConsequent Whether join two adjacent ranges into single, e.g. consequent table cells. */ - getNextRange : function() + getNextRange : function( mergeConsequent ) { current = current == undefined ? 0 : current + 1; @@ -65,11 +68,58 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( !current ) { // Make sure bookmark correctness by reverse processing. - for ( var i = rangeList.length - 1; i > 0; i-- ) + for ( var i = rangeList.length - 1; i >= 0; i-- ) bookmarks.unshift( rangeList[ i ].createBookmark( true ) ); } - else - range.moveToBookmark( bookmarks.shift() ); + + if ( mergeConsequent ) + { + // Figure out how many ranges should be merged. + var mergeCount = 0; + while ( rangeList[ current + mergeCount + 1 ] ) + { + var doc = range.document, + found = 0, + left = doc.getById( bookmarks[ mergeCount ].endNode ), + right = doc.getById( bookmarks[ mergeCount + 1 ].startNode ), + next; + + // Check subsequent range. + while ( 1 ) + { + next = left.getNextSourceNode( false ); + if ( !right.equals( next ) ) + { + // This could be yet another bookmark or + // walking across block boundaries. + if ( bookmark( next ) || ( next.type == CKEDITOR.NODE_ELEMENT && next.isBlockBoundary() ) ) + { + left = next; + continue; + } + } + else + found = 1; + + break; + } + + if ( !found ) + break; + + mergeCount++; + } + } + + range.moveToBookmark( bookmarks.shift() ); + + // Merge ranges finally after moving to bookmarks. + while( mergeCount-- ) + { + next = rangeList[ ++current ]; + next.moveToBookmark( bookmarks.shift() ); + range.setEnd( next.endContainer, next.endOffset ); + } } return range;