On prépare de quoi se concentrer sur les images incorporées.
[ckeditor.git] / skins / ckeditor / _source / plugins / domiterator / plugin.js
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
5
6 /**
7 * @file DOM iterator, which iterates over list items, lines and paragraphs.
8 */
9
10 CKEDITOR.plugins.add( 'domiterator' );
11
12 (function()
13 {
14 /**
15 * @name CKEDITOR.dom.iterator
16 */
17 function iterator( range )
18 {
19 if ( arguments.length < 1 )
20 return;
21
22 this.range = range;
23 this.forceBrBreak = 0;
24
25 // Whether include <br>s into the enlarged range.(#3730).
26 this.enlargeBr = 1;
27 this.enforceRealBlocks = 0;
28
29 this._ || ( this._ = {} );
30 }
31
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 ); };
37
38 // Get a reference for the next element, bookmark nodes are skipped.
39 function getNextSourceNode( node, startFromSibling, lastNode )
40 {
41 var next = node.getNextSourceNode( startFromSibling, null, lastNode );
42 while ( !bookmarkGuard( next ) )
43 next = next.getNextSourceNode( startFromSibling, null, lastNode );
44 return next;
45 }
46
47 iterator.prototype = {
48 getNextParagraph : function( blockTag )
49 {
50 // The block element to be returned.
51 var block;
52
53 // The range object used to identify the paragraph contents.
54 var range;
55
56 // Indicats that the current element in the loop is the last one.
57 var isLast;
58
59 // Indicate at least one of the range boundaries is inside a preformat block.
60 var touchPre;
61
62 // Instructs to cleanup remaining BRs.
63 var removePreviousBr, removeLastBr;
64
65 // This is the first iteration. Let's initialize it.
66 if ( !this._.lastNode )
67 {
68 range = this.range.clone();
69
70 // Shrink the range to exclude harmful "noises" (#4087, #4450, #5435).
71 range.shrink( CKEDITOR.NODE_ELEMENT, true );
72
73 touchPre = range.endContainer.hasAscendant( 'pre', true )
74 || range.startContainer.hasAscendant( 'pre', true );
75
76 range.enlarge( this.forceBrBreak && !touchPre || !this.enlargeBr ?
77 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS : CKEDITOR.ENLARGE_BLOCK_CONTENTS );
78
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 );
89
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
92 // next block.(#3887)
93 if ( this._.lastNode &&
94 this._.lastNode.type == CKEDITOR.NODE_TEXT &&
95 !CKEDITOR.tools.trim( this._.lastNode.getText() ) &&
96 this._.lastNode.getParent().isBlockBoundary() )
97 {
98 var testRange = new CKEDITOR.dom.range( range.document );
99 testRange.moveToPosition( this._.lastNode, CKEDITOR.POSITION_AFTER_END );
100 if ( testRange.checkEndOfBlock() )
101 {
102 var path = new CKEDITOR.dom.elementPath( testRange.endContainer );
103 var lastBlock = path.block || path.blockLimit;
104 this._.lastNode = lastBlock.getNextSourceNode( true );
105 }
106 }
107
108 // Probably the document end is reached, we need a marker node.
109 if ( !this._.lastNode )
110 {
111 this._.lastNode = this._.docEndMarker = range.document.createText( '' );
112 this._.lastNode.insertAfter( lastNode );
113 }
114
115 // Let's reuse this variable.
116 range = null;
117 }
118
119 var currentNode = this._.nextNode;
120 lastNode = this._.lastNode;
121
122 this._.nextNode = null;
123 while ( currentNode )
124 {
125 // closeRange indicates that a paragraph boundary has been found,
126 // so the range can be closed.
127 var closeRange = 0,
128 parentPre = currentNode.hasAscendant( 'pre' );
129
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;
134
135 // If it is an element node, let's check if it can be part of the
136 // range.
137 if ( !includeNode )
138 {
139 var nodeName = currentNode.getName();
140
141 if ( currentNode.isBlockBoundary( this.forceBrBreak &&
142 !parentPre && { br : 1 } ) )
143 {
144 // <br> boundaries must be part of the range. It will
145 // happen only if ForceBrBreak.
146 if ( nodeName == 'br' )
147 includeNode = 1;
148 else if ( !range && !currentNode.getChildCount() && nodeName != 'hr' )
149 {
150 // If we have found an empty block, and haven't started
151 // the range yet, it means we must return this block.
152 block = currentNode;
153 isLast = currentNode.equals( lastNode );
154 break;
155 }
156
157 // The range must finish right before the boundary,
158 // including possibly skipped empty spaces. (#1603)
159 if ( range )
160 {
161 range.setEndAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
162
163 // The found boundary must be set as the next one at this
164 // point. (#1717)
165 if ( nodeName != 'br' )
166 this._.nextNode = currentNode;
167 }
168
169 closeRange = 1;
170 }
171 else
172 {
173 // If we have child nodes, let's check them.
174 if ( currentNode.getFirst() )
175 {
176 // If we don't have a range yet, let's start it.
177 if ( !range )
178 {
179 range = new CKEDITOR.dom.range( this.range.document );
180 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
181 }
182
183 currentNode = currentNode.getFirst();
184 continue;
185 }
186 includeNode = 1;
187 }
188 }
189 else if ( currentNode.type == CKEDITOR.NODE_TEXT )
190 {
191 // Ignore normal whitespaces (i.e. not including &nbsp; or
192 // other unicode whitespaces) before/after a block node.
193 if ( beginWhitespaceRegex.test( currentNode.getText() ) )
194 includeNode = 0;
195 }
196
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 )
200 {
201 range = new CKEDITOR.dom.range( this.range.document );
202 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );
203 }
204
205 // The last node has been found.
206 isLast = ( ( !closeRange || includeNode ) && currentNode.equals( lastNode ) );
207
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 )
211 {
212 while ( !currentNode.getNext( skipGuard ) && !isLast )
213 {
214 var parentNode = currentNode.getParent();
215
216 if ( parentNode.isBlockBoundary( this.forceBrBreak
217 && !parentPre && { br : 1 } ) )
218 {
219 closeRange = 1;
220 includeNode = 0;
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 );
224 break;
225 }
226
227 currentNode = parentNode;
228 includeNode = 1;
229 isLast = ( currentNode.equals( lastNode ) );
230 continueFromSibling = 1;
231 }
232 }
233
234 // Now finally include the node.
235 if ( includeNode )
236 range.setEndAt( currentNode, CKEDITOR.POSITION_AFTER_END );
237
238 currentNode = getNextSourceNode ( currentNode, continueFromSibling, lastNode );
239 isLast = !currentNode;
240
241 // We have found a block boundary. Let's close the range and move out of the
242 // loop.
243 if ( isLast || ( closeRange && range ) )
244 break;
245 }
246
247 // Now, based on the processed range, look for (or create) the block to be returned.
248 if ( !block )
249 {
250 // If no range has been found, this is the end.
251 if ( !range )
252 {
253 this._.docEndMarker && this._.docEndMarker.remove();
254 this._.nextNode = null;
255 return null;
256 }
257
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;
262
263 if ( !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' ) )
270 {
271 // Create the fixed block.
272 block = this.range.document.createElement( blockTag || 'p' );
273
274 // Move the contents of the temporary range to the fixed block.
275 range.extractContents().appendTo( block );
276 block.trim();
277
278 // Insert the fixed block into the DOM.
279 range.insertNode( block );
280
281 removePreviousBr = removeLastBr = true;
282 }
283 else if ( block.getName() != 'li' )
284 {
285 // If the range doesn't includes the entire contents of the
286 // block, we must split it, isolating the range in a dedicated
287 // block.
288 if ( !range.checkStartOfBlock() || !range.checkEndOfBlock() )
289 {
290 // The resulting block will be a clone of the current one.
291 block = block.clone( false );
292
293 // Extract the range contents, moving it to the new block.
294 range.extractContents().appendTo( block );
295 block.trim();
296
297 // Split the block. At this point, the range will be in the
298 // right position for our intents.
299 var splitInfo = range.splitBlock();
300
301 removePreviousBr = !splitInfo.wasStartOfBlock;
302 removeLastBr = !splitInfo.wasEndOfBlock;
303
304 // Insert the new block into the DOM.
305 range.insertNode( block );
306 }
307 }
308 else if ( !isLast )
309 {
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>.
314
315 this._.nextNode = ( block.equals( lastNode ) ? null : getNextSourceNode( range.getBoundaryNodes().endNode, 1, lastNode ) );
316 }
317 }
318
319 if ( removePreviousBr )
320 {
321 var previousSibling = block.getPrevious();
322 if ( previousSibling && previousSibling.type == CKEDITOR.NODE_ELEMENT )
323 {
324 if ( previousSibling.getName() == 'br' )
325 previousSibling.remove();
326 else if ( previousSibling.getLast() && previousSibling.getLast().$.nodeName.toLowerCase() == 'br' )
327 previousSibling.getLast().remove();
328 }
329 }
330
331 if ( removeLastBr )
332 {
333 var lastChild = block.getLast();
334 if ( lastChild && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.getName() == 'br' )
335 {
336 // Take care not to remove the block expanding <br> in non-IE browsers.
337 if ( CKEDITOR.env.ie
338 || lastChild.getPrevious( bookmarkGuard )
339 || lastChild.getNext( bookmarkGuard ) )
340 lastChild.remove();
341 }
342 }
343
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
346 // next interation.
347 if ( !this._.nextNode )
348 {
349 this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null :
350 getNextSourceNode( block, 1, lastNode );
351 }
352
353 return block;
354 }
355 };
356
357 CKEDITOR.dom.range.prototype.createIterator = function()
358 {
359 return new iterator( this );
360 };
361 })();