2 Copyright (c) 2003-2009, 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 var iterator = function( range )
\r
17 if ( arguments.length < 1 )
\r
21 this.forceBrBreak = false;
\r
23 // Whether include <br>s into the enlarged range.(#3730).
\r
24 this.enlargeBr = true;
\r
25 this.enforceRealBlocks = false;
\r
27 this._ || ( this._ = {} );
\r
29 beginWhitespaceRegex = /^[\r\n\t ]+$/;
\r
32 iterator.prototype = {
\r
33 getNextParagraph : function( blockTag )
\r
35 // The block element to be returned.
\r
38 // The range object used to identify the paragraph contents.
\r
41 // Indicats that the current element in the loop is the last one.
\r
44 // Instructs to cleanup remaining BRs.
\r
45 var removePreviousBr, removeLastBr;
\r
47 // This is the first iteration. Let's initialize it.
\r
48 if ( !this._.lastNode )
\r
50 range = this.range.clone();
\r
51 range.enlarge( this.forceBrBreak || !this.enlargeBr ?
\r
52 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS : CKEDITOR.ENLARGE_BLOCK_CONTENTS );
\r
54 var walker = new CKEDITOR.dom.walker( range ),
\r
55 ignoreBookmarkTextEvaluator = CKEDITOR.dom.walker.bookmark( true, true );
\r
56 // Avoid anchor inside bookmark inner text.
\r
57 walker.evaluator = ignoreBookmarkTextEvaluator;
\r
58 this._.nextNode = walker.next();
\r
59 // TODO: It's better to have walker.reset() used here.
\r
60 walker = new CKEDITOR.dom.walker( range );
\r
61 walker.evaluator = ignoreBookmarkTextEvaluator;
\r
62 var lastNode = walker.previous();
\r
63 this._.lastNode = lastNode.getNextSourceNode( true );
\r
65 // We may have an empty text node at the end of block due to [3770].
\r
66 // If that node is the lastNode, it would cause our logic to leak to the
\r
67 // next block.(#3887)
\r
68 if ( this._.lastNode &&
\r
69 this._.lastNode.type == CKEDITOR.NODE_TEXT &&
\r
70 !CKEDITOR.tools.trim( this._.lastNode.getText( ) ) &&
\r
71 this._.lastNode.getParent().isBlockBoundary() )
\r
73 var testRange = new CKEDITOR.dom.range( range.document );
\r
74 testRange.moveToPosition( this._.lastNode, CKEDITOR.POSITION_AFTER_END );
\r
75 if ( testRange.checkEndOfBlock() )
\r
77 var path = new CKEDITOR.dom.elementPath( testRange.endContainer );
\r
78 var lastBlock = path.block || path.blockLimit;
\r
79 this._.lastNode = lastBlock.getNextSourceNode( true );
\r
83 // Probably the document end is reached, we need a marker node.
\r
84 if ( !this._.lastNode )
\r
86 this._.lastNode = this._.docEndMarker = range.document.createText( '' );
\r
87 this._.lastNode.insertAfter( lastNode );
\r
90 // Let's reuse this variable.
\r
94 var currentNode = this._.nextNode;
\r
95 lastNode = this._.lastNode;
\r
97 this._.nextNode = null;
\r
98 while ( currentNode )
\r
100 // closeRange indicates that a paragraph boundary has been found,
\r
101 // so the range can be closed.
\r
102 var closeRange = false;
\r
104 // includeNode indicates that the current node is good to be part
\r
105 // of the range. By default, any non-element node is ok for it.
\r
106 var includeNode = ( currentNode.type != CKEDITOR.NODE_ELEMENT ),
\r
107 continueFromSibling = false;
\r
109 // If it is an element node, let's check if it can be part of the
\r
111 if ( !includeNode )
\r
113 var nodeName = currentNode.getName();
\r
115 if ( currentNode.isBlockBoundary( this.forceBrBreak && { br : 1 } ) )
\r
117 // <br> boundaries must be part of the range. It will
\r
118 // happen only if ForceBrBreak.
\r
119 if ( nodeName == 'br' )
\r
120 includeNode = true;
\r
121 else if ( !range && !currentNode.getChildCount() && nodeName != 'hr' )
\r
123 // If we have found an empty block, and haven't started
\r
124 // the range yet, it means we must return this block.
\r
125 block = currentNode;
\r
126 isLast = currentNode.equals( lastNode );
\r
130 // The range must finish right before the boundary,
\r
131 // including possibly skipped empty spaces. (#1603)
\r
134 range.setEndAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
\r
136 // The found boundary must be set as the next one at this
\r
138 if ( nodeName != 'br' )
\r
139 this._.nextNode = currentNode;
\r
146 // If we have child nodes, let's check them.
\r
147 if ( currentNode.getFirst() )
\r
149 // If we don't have a range yet, let's start it.
\r
152 range = new CKEDITOR.dom.range( this.range.document );
\r
153 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
\r
156 currentNode = currentNode.getFirst();
\r
159 includeNode = true;
\r
162 else if ( currentNode.type == CKEDITOR.NODE_TEXT )
\r
164 // Ignore normal whitespaces (i.e. not including or
\r
165 // other unicode whitespaces) before/after a block node.
\r
166 if ( beginWhitespaceRegex.test( currentNode.getText() ) )
\r
167 includeNode = false;
\r
170 // The current node is good to be part of the range and we are
\r
171 // starting a new range, initialize it first.
\r
172 if ( includeNode && !range )
\r
174 range = new CKEDITOR.dom.range( this.range.document );
\r
175 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
\r
178 // The last node has been found.
\r
179 isLast = ( ( !closeRange || includeNode ) && currentNode.equals( lastNode ) );
\r
181 // If we are in an element boundary, let's check if it is time
\r
182 // to close the range, otherwise we include the parent within it.
\r
183 if ( range && !closeRange )
\r
185 while ( !currentNode.getNext() && !isLast )
\r
187 var parentNode = currentNode.getParent();
\r
189 if ( parentNode.isBlockBoundary( this.forceBrBreak && { br : 1 } ) )
\r
192 isLast = isLast || ( parentNode.equals( lastNode) );
\r
196 currentNode = parentNode;
\r
197 includeNode = true;
\r
198 isLast = ( currentNode.equals( lastNode ) );
\r
199 continueFromSibling = true;
\r
203 // Now finally include the node.
\r
205 range.setEndAt( currentNode, CKEDITOR.POSITION_AFTER_END );
\r
207 currentNode = currentNode.getNextSourceNode( continueFromSibling, null, lastNode );
\r
208 isLast = !currentNode;
\r
210 // We have found a block boundary. Let's close the range and move out of the
\r
212 if ( ( closeRange || isLast ) && range )
\r
214 var boundaryNodes = range.getBoundaryNodes(),
\r
215 startPath = new CKEDITOR.dom.elementPath( range.startContainer ),
\r
216 endPath = new CKEDITOR.dom.elementPath( range.endContainer );
\r
218 // Drop the range if it only contains bookmark nodes.(#4087)
\r
219 if ( boundaryNodes.startNode.equals( boundaryNodes.endNode )
\r
220 && boundaryNodes.startNode.getParent().equals( startPath.blockLimit )
\r
221 && boundaryNodes.startNode.type == CKEDITOR.NODE_ELEMENT
\r
222 && boundaryNodes.startNode.getAttribute( '_fck_bookmark' ) )
\r
225 this._.nextNode = null;
\r
236 // Now, based on the processed range, look for (or create) the block to be returned.
\r
239 // If no range has been found, this is the end.
\r
242 this._.docEndMarker && this._.docEndMarker.remove();
\r
243 this._.nextNode = null;
\r
247 startPath = new CKEDITOR.dom.elementPath( range.startContainer );
\r
248 var startBlockLimit = startPath.blockLimit,
\r
249 checkLimits = { div : 1, th : 1, td : 1 };
\r
250 block = startPath.block;
\r
253 && !this.enforceRealBlocks
\r
254 && checkLimits[ startBlockLimit.getName() ]
\r
255 && range.checkStartOfBlock()
\r
256 && range.checkEndOfBlock() )
\r
257 block = startBlockLimit;
\r
258 else if ( !block || ( this.enforceRealBlocks && block.getName() == 'li' ) )
\r
260 // Create the fixed block.
\r
261 block = this.range.document.createElement( blockTag || 'p' );
\r
263 // Move the contents of the temporary range to the fixed block.
\r
264 range.extractContents().appendTo( block );
\r
267 // Insert the fixed block into the DOM.
\r
268 range.insertNode( block );
\r
270 removePreviousBr = removeLastBr = true;
\r
272 else if ( block.getName() != 'li' )
\r
274 // If the range doesn't includes the entire contents of the
\r
275 // block, we must split it, isolating the range in a dedicated
\r
277 if ( !range.checkStartOfBlock() || !range.checkEndOfBlock() )
\r
279 // The resulting block will be a clone of the current one.
\r
280 block = block.clone( false );
\r
282 // Extract the range contents, moving it to the new block.
\r
283 range.extractContents().appendTo( block );
\r
286 // Split the block. At this point, the range will be in the
\r
287 // right position for our intents.
\r
288 var splitInfo = range.splitBlock();
\r
290 removePreviousBr = !splitInfo.wasStartOfBlock;
\r
291 removeLastBr = !splitInfo.wasEndOfBlock;
\r
293 // Insert the new block into the DOM.
\r
294 range.insertNode( block );
\r
297 else if ( !isLast )
\r
299 // LIs are returned as is, with all their children (due to the
\r
300 // nested lists). But, the next node is the node right after
\r
301 // the current range, which could be an <li> child (nested
\r
302 // lists) or the next sibling <li>.
\r
304 this._.nextNode = ( block.equals( lastNode ) ? null :
\r
305 range.getBoundaryNodes().endNode.getNextSourceNode( true, null, lastNode ) );
\r
309 if ( removePreviousBr )
\r
311 var previousSibling = block.getPrevious();
\r
312 if ( previousSibling && previousSibling.type == CKEDITOR.NODE_ELEMENT )
\r
314 if ( previousSibling.getName() == 'br' )
\r
315 previousSibling.remove();
\r
316 else if ( previousSibling.getLast() && previousSibling.getLast().$.nodeName.toLowerCase() == 'br' )
\r
317 previousSibling.getLast().remove();
\r
321 if ( removeLastBr )
\r
323 // Ignore bookmark nodes.(#3783)
\r
324 var bookmarkGuard = CKEDITOR.dom.walker.bookmark( false, true );
\r
326 var lastChild = block.getLast();
\r
327 if ( lastChild && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.getName() == 'br' )
\r
329 // Take care not to remove the block expanding <br> in non-IE browsers.
\r
330 if ( CKEDITOR.env.ie
\r
331 || lastChild.getPrevious( bookmarkGuard )
\r
332 || lastChild.getNext( bookmarkGuard ) )
\r
333 lastChild.remove();
\r
337 // Get a reference for the next element. This is important because the
\r
338 // above block can be removed or changed, so we can rely on it for the
\r
339 // next interation.
\r
340 if ( !this._.nextNode )
\r
342 this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null :
\r
343 block.getNextSourceNode( true, null, lastNode );
\r
350 CKEDITOR.dom.range.prototype.createIterator = function()
\r
352 return new iterator( this );
\r