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