2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @file DOM iterator, which iterates over list items, lines and paragraphs.
10 CKEDITOR.plugins.add( 'domiterator' );
15 * @name CKEDITOR.dom.iterator
17 function iterator( range )
19 if ( arguments.length < 1 )
23 this.forceBrBreak = 0;
25 // Whether include <br>s into the enlarged range.(#3730).
27 this.enforceRealBlocks = 0;
29 this._ || ( this._ = {} );
32 var beginWhitespaceRegex = /^[\r\n\t ]+$/,
33 // Ignore bookmark nodes.(#3783)
34 bookmarkGuard = CKEDITOR.dom.walker.bookmark( false, true ),
35 whitespacesGuard = CKEDITOR.dom.walker.whitespaces( true ),
36 skipGuard = function( node ) { return bookmarkGuard( node ) && whitespacesGuard( node ); };
38 // Get a reference for the next element, bookmark nodes are skipped.
39 function getNextSourceNode( node, startFromSibling, lastNode )
41 var next = node.getNextSourceNode( startFromSibling, null, lastNode );
42 while ( !bookmarkGuard( next ) )
43 next = next.getNextSourceNode( startFromSibling, null, lastNode );
47 iterator.prototype = {
48 getNextParagraph : function( blockTag )
50 // The block element to be returned.
53 // The range object used to identify the paragraph contents.
56 // Indicats that the current element in the loop is the last one.
59 // Indicate at least one of the range boundaries is inside a preformat block.
62 // Instructs to cleanup remaining BRs.
63 var removePreviousBr, removeLastBr;
65 // This is the first iteration. Let's initialize it.
66 if ( !this._.lastNode )
68 range = this.range.clone();
70 // Shrink the range to exclude harmful "noises" (#4087, #4450, #5435).
71 range.shrink( CKEDITOR.NODE_ELEMENT, true );
73 touchPre = range.endContainer.hasAscendant( 'pre', true )
74 || range.startContainer.hasAscendant( 'pre', true );
76 range.enlarge( this.forceBrBreak && !touchPre || !this.enlargeBr ?
77 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS : CKEDITOR.ENLARGE_BLOCK_CONTENTS );
79 var walker = new CKEDITOR.dom.walker( range ),
80 ignoreBookmarkTextEvaluator = CKEDITOR.dom.walker.bookmark( true, true );
81 // Avoid anchor inside bookmark inner text.
82 walker.evaluator = ignoreBookmarkTextEvaluator;
83 this._.nextNode = walker.next();
84 // TODO: It's better to have walker.reset() used here.
85 walker = new CKEDITOR.dom.walker( range );
86 walker.evaluator = ignoreBookmarkTextEvaluator;
87 var lastNode = walker.previous();
88 this._.lastNode = lastNode.getNextSourceNode( true );
90 // We may have an empty text node at the end of block due to [3770].
91 // If that node is the lastNode, it would cause our logic to leak to the
93 if ( this._.lastNode &&
94 this._.lastNode.type == CKEDITOR.NODE_TEXT &&
95 !CKEDITOR.tools.trim( this._.lastNode.getText() ) &&
96 this._.lastNode.getParent().isBlockBoundary() )
98 var testRange = new CKEDITOR.dom.range( range.document );
99 testRange.moveToPosition( this._.lastNode, CKEDITOR.POSITION_AFTER_END );
100 if ( testRange.checkEndOfBlock() )
102 var path = new CKEDITOR.dom.elementPath( testRange.endContainer );
103 var lastBlock = path.block || path.blockLimit;
104 this._.lastNode = lastBlock.getNextSourceNode( true );
108 // Probably the document end is reached, we need a marker node.
109 if ( !this._.lastNode )
111 this._.lastNode = this._.docEndMarker = range.document.createText( '' );
112 this._.lastNode.insertAfter( lastNode );
115 // Let's reuse this variable.
119 var currentNode = this._.nextNode;
120 lastNode = this._.lastNode;
122 this._.nextNode = null;
123 while ( currentNode )
125 // closeRange indicates that a paragraph boundary has been found,
126 // so the range can be closed.
128 parentPre = currentNode.hasAscendant( 'pre' );
130 // includeNode indicates that the current node is good to be part
131 // of the range. By default, any non-element node is ok for it.
132 var includeNode = ( currentNode.type != CKEDITOR.NODE_ELEMENT ),
133 continueFromSibling = 0;
135 // If it is an element node, let's check if it can be part of the
139 var nodeName = currentNode.getName();
141 if ( currentNode.isBlockBoundary( this.forceBrBreak &&
142 !parentPre && { br : 1 } ) )
144 // <br> boundaries must be part of the range. It will
145 // happen only if ForceBrBreak.
146 if ( nodeName == 'br' )
148 else if ( !range && !currentNode.getChildCount() && nodeName != 'hr' )
150 // If we have found an empty block, and haven't started
151 // the range yet, it means we must return this block.
153 isLast = currentNode.equals( lastNode );
157 // The range must finish right before the boundary,
158 // including possibly skipped empty spaces. (#1603)
161 range.setEndAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
163 // The found boundary must be set as the next one at this
165 if ( nodeName != 'br' )
166 this._.nextNode = currentNode;
173 // If we have child nodes, let's check them.
174 if ( currentNode.getFirst() )
176 // If we don't have a range yet, let's start it.
179 range = new CKEDITOR.dom.range( this.range.document );
180 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
183 currentNode = currentNode.getFirst();
189 else if ( currentNode.type == CKEDITOR.NODE_TEXT )
191 // Ignore normal whitespaces (i.e. not including or
192 // other unicode whitespaces) before/after a block node.
193 if ( beginWhitespaceRegex.test( currentNode.getText() ) )
197 // The current node is good to be part of the range and we are
198 // starting a new range, initialize it first.
199 if ( includeNode && !range )
201 range = new CKEDITOR.dom.range( this.range.document );
202 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
205 // The last node has been found.
206 isLast = ( ( !closeRange || includeNode ) && currentNode.equals( lastNode ) );
208 // If we are in an element boundary, let's check if it is time
209 // to close the range, otherwise we include the parent within it.
210 if ( range && !closeRange )
212 while ( !currentNode.getNext( skipGuard ) && !isLast )
214 var parentNode = currentNode.getParent();
216 if ( parentNode.isBlockBoundary( this.forceBrBreak
217 && !parentPre && { br : 1 } ) )
221 isLast = isLast || ( parentNode.equals( lastNode) );
222 // Make sure range includes bookmarks at the end of the block. (#7359)
223 range.setEndAt( parentNode, CKEDITOR.POSITION_BEFORE_END );
227 currentNode = parentNode;
229 isLast = ( currentNode.equals( lastNode ) );
230 continueFromSibling = 1;
234 // Now finally include the node.
236 range.setEndAt( currentNode, CKEDITOR.POSITION_AFTER_END );
238 currentNode = getNextSourceNode ( currentNode, continueFromSibling, lastNode );
239 isLast = !currentNode;
241 // We have found a block boundary. Let's close the range and move out of the
243 if ( isLast || ( closeRange && range ) )
247 // Now, based on the processed range, look for (or create) the block to be returned.
250 // If no range has been found, this is the end.
253 this._.docEndMarker && this._.docEndMarker.remove();
254 this._.nextNode = null;
258 var startPath = new CKEDITOR.dom.elementPath( range.startContainer );
259 var startBlockLimit = startPath.blockLimit,
260 checkLimits = { div : 1, th : 1, td : 1 };
261 block = startPath.block;
264 && !this.enforceRealBlocks
265 && checkLimits[ startBlockLimit.getName() ]
266 && range.checkStartOfBlock()
267 && range.checkEndOfBlock() )
268 block = startBlockLimit;
269 else if ( !block || ( this.enforceRealBlocks && block.getName() == 'li' ) )
271 // Create the fixed block.
272 block = this.range.document.createElement( blockTag || 'p' );
274 // Move the contents of the temporary range to the fixed block.
275 range.extractContents().appendTo( block );
278 // Insert the fixed block into the DOM.
279 range.insertNode( block );
281 removePreviousBr = removeLastBr = true;
283 else if ( block.getName() != 'li' )
285 // If the range doesn't includes the entire contents of the
286 // block, we must split it, isolating the range in a dedicated
288 if ( !range.checkStartOfBlock() || !range.checkEndOfBlock() )
290 // The resulting block will be a clone of the current one.
291 block = block.clone( false );
293 // Extract the range contents, moving it to the new block.
294 range.extractContents().appendTo( block );
297 // Split the block. At this point, the range will be in the
298 // right position for our intents.
299 var splitInfo = range.splitBlock();
301 removePreviousBr = !splitInfo.wasStartOfBlock;
302 removeLastBr = !splitInfo.wasEndOfBlock;
304 // Insert the new block into the DOM.
305 range.insertNode( block );
310 // LIs are returned as is, with all their children (due to the
311 // nested lists). But, the next node is the node right after
312 // the current range, which could be an <li> child (nested
313 // lists) or the next sibling <li>.
315 this._.nextNode = ( block.equals( lastNode ) ? null : getNextSourceNode( range.getBoundaryNodes().endNode, 1, lastNode ) );
319 if ( removePreviousBr )
321 var previousSibling = block.getPrevious();
322 if ( previousSibling && previousSibling.type == CKEDITOR.NODE_ELEMENT )
324 if ( previousSibling.getName() == 'br' )
325 previousSibling.remove();
326 else if ( previousSibling.getLast() && previousSibling.getLast().$.nodeName.toLowerCase() == 'br' )
327 previousSibling.getLast().remove();
333 var lastChild = block.getLast();
334 if ( lastChild && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.getName() == 'br' )
336 // Take care not to remove the block expanding <br> in non-IE browsers.
338 || lastChild.getPrevious( bookmarkGuard )
339 || lastChild.getNext( bookmarkGuard ) )
344 // Get a reference for the next element. This is important because the
345 // above block can be removed or changed, so we can rely on it for the
347 if ( !this._.nextNode )
349 this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null :
350 getNextSourceNode( block, 1, lastNode );
357 CKEDITOR.dom.range.prototype.createIterator = function()
359 return new iterator( this );