2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @file DOM iterator, which iterates over list items, lines and paragraphs.
\r
10 CKEDITOR.plugins.add( 'domiterator' );
\r
15 * @name CKEDITOR.dom.iterator
\r
17 function iterator( range )
\r
19 if ( arguments.length < 1 )
\r
23 this.forceBrBreak = false;
\r
25 // Whether include <br>s into the enlarged range.(#3730).
\r
26 this.enlargeBr = true;
\r
27 this.enforceRealBlocks = false;
\r
29 this._ || ( this._ = {} );
\r
32 var beginWhitespaceRegex = /^[\r\n\t ]+$/,
\r
33 isBookmark = CKEDITOR.dom.walker.bookmark();
\r
35 iterator.prototype = {
\r
36 getNextParagraph : function( blockTag )
\r
38 // The block element to be returned.
\r
41 // The range object used to identify the paragraph contents.
\r
44 // Indicats that the current element in the loop is the last one.
\r
47 // Instructs to cleanup remaining BRs.
\r
48 var removePreviousBr, removeLastBr;
\r
50 // This is the first iteration. Let's initialize it.
\r
51 if ( !this._.lastNode )
\r
53 range = this.range.clone();
\r
54 range.enlarge( this.forceBrBreak || !this.enlargeBr ?
\r
55 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS : CKEDITOR.ENLARGE_BLOCK_CONTENTS );
\r
57 var walker = new CKEDITOR.dom.walker( range ),
\r
58 ignoreBookmarkTextEvaluator = CKEDITOR.dom.walker.bookmark( true, true );
\r
59 // Avoid anchor inside bookmark inner text.
\r
60 walker.evaluator = ignoreBookmarkTextEvaluator;
\r
61 this._.nextNode = walker.next();
\r
62 // TODO: It's better to have walker.reset() used here.
\r
63 walker = new CKEDITOR.dom.walker( range );
\r
64 walker.evaluator = ignoreBookmarkTextEvaluator;
\r
65 var lastNode = walker.previous();
\r
66 this._.lastNode = lastNode.getNextSourceNode( true );
\r
68 // We may have an empty text node at the end of block due to [3770].
\r
69 // If that node is the lastNode, it would cause our logic to leak to the
\r
70 // next block.(#3887)
\r
71 if ( this._.lastNode &&
\r
72 this._.lastNode.type == CKEDITOR.NODE_TEXT &&
\r
73 !CKEDITOR.tools.trim( this._.lastNode.getText( ) ) &&
\r
74 this._.lastNode.getParent().isBlockBoundary() )
\r
76 var testRange = new CKEDITOR.dom.range( range.document );
\r
77 testRange.moveToPosition( this._.lastNode, CKEDITOR.POSITION_AFTER_END );
\r
78 if ( testRange.checkEndOfBlock() )
\r
80 var path = new CKEDITOR.dom.elementPath( testRange.endContainer );
\r
81 var lastBlock = path.block || path.blockLimit;
\r
82 this._.lastNode = lastBlock.getNextSourceNode( true );
\r
86 // Probably the document end is reached, we need a marker node.
\r
87 if ( !this._.lastNode )
\r
89 this._.lastNode = this._.docEndMarker = range.document.createText( '' );
\r
90 this._.lastNode.insertAfter( lastNode );
\r
93 // Let's reuse this variable.
\r
97 var currentNode = this._.nextNode;
\r
98 lastNode = this._.lastNode;
\r
100 this._.nextNode = null;
\r
101 while ( currentNode )
\r
103 // closeRange indicates that a paragraph boundary has been found,
\r
104 // so the range can be closed.
\r
105 var closeRange = false;
\r
107 // includeNode indicates that the current node is good to be part
\r
108 // of the range. By default, any non-element node is ok for it.
\r
109 var includeNode = ( currentNode.type != CKEDITOR.NODE_ELEMENT ),
\r
110 continueFromSibling = false;
\r
112 // If it is an element node, let's check if it can be part of the
\r
114 if ( !includeNode )
\r
116 var nodeName = currentNode.getName();
\r
118 if ( currentNode.isBlockBoundary( this.forceBrBreak && { br : 1 } ) )
\r
120 // <br> boundaries must be part of the range. It will
\r
121 // happen only if ForceBrBreak.
\r
122 if ( nodeName == 'br' )
\r
123 includeNode = true;
\r
124 else if ( !range && !currentNode.getChildCount() && nodeName != 'hr' )
\r
126 // If we have found an empty block, and haven't started
\r
127 // the range yet, it means we must return this block.
\r
128 block = currentNode;
\r
129 isLast = currentNode.equals( lastNode );
\r
133 // The range must finish right before the boundary,
\r
134 // including possibly skipped empty spaces. (#1603)
\r
137 range.setEndAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
\r
139 // The found boundary must be set as the next one at this
\r
141 if ( nodeName != 'br' )
\r
142 this._.nextNode = currentNode;
\r
149 // If we have child nodes, let's check them.
\r
150 if ( currentNode.getFirst() )
\r
152 // If we don't have a range yet, let's start it.
\r
155 range = new CKEDITOR.dom.range( this.range.document );
\r
156 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
\r
159 currentNode = currentNode.getFirst();
\r
162 includeNode = true;
\r
165 else if ( currentNode.type == CKEDITOR.NODE_TEXT )
\r
167 // Ignore normal whitespaces (i.e. not including or
\r
168 // other unicode whitespaces) before/after a block node.
\r
169 if ( beginWhitespaceRegex.test( currentNode.getText() ) )
\r
170 includeNode = false;
\r
173 // The current node is good to be part of the range and we are
\r
174 // starting a new range, initialize it first.
\r
175 if ( includeNode && !range )
\r
177 range = new CKEDITOR.dom.range( this.range.document );
\r
178 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
\r
181 // The last node has been found.
\r
182 isLast = ( ( !closeRange || includeNode ) && currentNode.equals( lastNode ) );
\r
184 // If we are in an element boundary, let's check if it is time
\r
185 // to close the range, otherwise we include the parent within it.
\r
186 if ( range && !closeRange )
\r
188 while ( !currentNode.getNext() && !isLast )
\r
190 var parentNode = currentNode.getParent();
\r
192 if ( parentNode.isBlockBoundary( this.forceBrBreak && { br : 1 } ) )
\r
195 isLast = isLast || ( parentNode.equals( lastNode) );
\r
199 currentNode = parentNode;
\r
200 includeNode = true;
\r
201 isLast = ( currentNode.equals( lastNode ) );
\r
202 continueFromSibling = true;
\r
206 // Now finally include the node.
\r
208 range.setEndAt( currentNode, CKEDITOR.POSITION_AFTER_END );
\r
210 currentNode = currentNode.getNextSourceNode( continueFromSibling, null, lastNode );
\r
211 isLast = !currentNode;
\r
213 // We have found a block boundary. Let's close the range and move out of the
\r
215 if ( ( closeRange || isLast ) && range )
\r
217 var boundaryNodes = range.getBoundaryNodes(),
\r
218 startPath = new CKEDITOR.dom.elementPath( range.startContainer );
\r
220 // Drop the range if it only contains bookmark nodes, and is
\r
221 // not because of the original collapsed range. (#4087,#4450)
\r
222 if ( boundaryNodes.startNode.getParent().equals( startPath.blockLimit )
\r
223 && isBookmark( boundaryNodes.startNode ) && isBookmark( boundaryNodes.endNode ) )
\r
226 this._.nextNode = null;
\r
237 // Now, based on the processed range, look for (or create) the block to be returned.
\r
240 // If no range has been found, this is the end.
\r
243 this._.docEndMarker && this._.docEndMarker.remove();
\r
244 this._.nextNode = null;
\r
248 startPath = new CKEDITOR.dom.elementPath( range.startContainer );
\r
249 var startBlockLimit = startPath.blockLimit,
\r
250 checkLimits = { div : 1, th : 1, td : 1 };
\r
251 block = startPath.block;
\r
254 && !this.enforceRealBlocks
\r
255 && checkLimits[ startBlockLimit.getName() ]
\r
256 && range.checkStartOfBlock()
\r
257 && range.checkEndOfBlock() )
\r
258 block = startBlockLimit;
\r
259 else if ( !block || ( this.enforceRealBlocks && block.getName() == 'li' ) )
\r
261 // Create the fixed block.
\r
262 block = this.range.document.createElement( blockTag || 'p' );
\r
264 // Move the contents of the temporary range to the fixed block.
\r
265 range.extractContents().appendTo( block );
\r
268 // Insert the fixed block into the DOM.
\r
269 range.insertNode( block );
\r
271 removePreviousBr = removeLastBr = true;
\r
273 else if ( block.getName() != 'li' )
\r
275 // If the range doesn't includes the entire contents of the
\r
276 // block, we must split it, isolating the range in a dedicated
\r
278 if ( !range.checkStartOfBlock() || !range.checkEndOfBlock() )
\r
280 // The resulting block will be a clone of the current one.
\r
281 block = block.clone( false );
\r
283 // Extract the range contents, moving it to the new block.
\r
284 range.extractContents().appendTo( block );
\r
287 // Split the block. At this point, the range will be in the
\r
288 // right position for our intents.
\r
289 var splitInfo = range.splitBlock();
\r
291 removePreviousBr = !splitInfo.wasStartOfBlock;
\r
292 removeLastBr = !splitInfo.wasEndOfBlock;
\r
294 // Insert the new block into the DOM.
\r
295 range.insertNode( block );
\r
298 else if ( !isLast )
\r
300 // LIs are returned as is, with all their children (due to the
\r
301 // nested lists). But, the next node is the node right after
\r
302 // the current range, which could be an <li> child (nested
\r
303 // lists) or the next sibling <li>.
\r
305 this._.nextNode = ( block.equals( lastNode ) ? null :
\r
306 range.getBoundaryNodes().endNode.getNextSourceNode( true, null, lastNode ) );
\r
310 if ( removePreviousBr )
\r
312 var previousSibling = block.getPrevious();
\r
313 if ( previousSibling && previousSibling.type == CKEDITOR.NODE_ELEMENT )
\r
315 if ( previousSibling.getName() == 'br' )
\r
316 previousSibling.remove();
\r
317 else if ( previousSibling.getLast() && previousSibling.getLast().$.nodeName.toLowerCase() == 'br' )
\r
318 previousSibling.getLast().remove();
\r
322 if ( removeLastBr )
\r
324 // Ignore bookmark nodes.(#3783)
\r
325 var bookmarkGuard = CKEDITOR.dom.walker.bookmark( false, true );
\r
327 var lastChild = block.getLast();
\r
328 if ( lastChild && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.getName() == 'br' )
\r
330 // Take care not to remove the block expanding <br> in non-IE browsers.
\r
331 if ( CKEDITOR.env.ie
\r
332 || lastChild.getPrevious( bookmarkGuard )
\r
333 || lastChild.getNext( bookmarkGuard ) )
\r
334 lastChild.remove();
\r
338 // Get a reference for the next element. This is important because the
\r
339 // above block can be removed or changed, so we can rely on it for the
\r
340 // next interation.
\r
341 if ( !this._.nextNode )
\r
343 this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null :
\r
344 block.getNextSourceNode( true, null, lastNode );
\r
351 CKEDITOR.dom.range.prototype.createIterator = function()
\r
353 return new iterator( this );
\r