JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
ded0a32cd432b20ca4ca709d2ebff543a18688ba
[ckeditor.git] / _source / plugins / domiterator / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @file DOM iterator, which iterates over list items, lines and paragraphs.\r
8  */\r
9 \r
10 CKEDITOR.plugins.add( 'domiterator' );\r
11 \r
12 (function()\r
13 {\r
14 \r
15         function iterator( range )\r
16         {\r
17                 if ( arguments.length < 1 )\r
18                         return;\r
19 \r
20                 this.range = range;\r
21                 this.forceBrBreak = false;\r
22 \r
23                 // Whether include <br>s into the enlarged range.(#3730).\r
24                 this.enlargeBr = true;\r
25                 this.enforceRealBlocks = false;\r
26 \r
27                 this._ || ( this._ = {} );\r
28         }\r
29 \r
30         var beginWhitespaceRegex = /^[\r\n\t ]+$/,\r
31                 isBookmark = CKEDITOR.dom.walker.bookmark();\r
32 \r
33         iterator.prototype = {\r
34                 getNextParagraph : function( blockTag )\r
35                 {\r
36                         // The block element to be returned.\r
37                         var block;\r
38 \r
39                         // The range object used to identify the paragraph contents.\r
40                         var range;\r
41 \r
42                         // Indicats that the current element in the loop is the last one.\r
43                         var isLast;\r
44 \r
45                         // Instructs to cleanup remaining BRs.\r
46                         var removePreviousBr, removeLastBr;\r
47 \r
48                         // This is the first iteration. Let's initialize it.\r
49                         if ( !this._.lastNode )\r
50                         {\r
51                                 range = this.range.clone();\r
52                                 range.enlarge( this.forceBrBreak || !this.enlargeBr ?\r
53                                                            CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS : CKEDITOR.ENLARGE_BLOCK_CONTENTS );\r
54 \r
55                                 var walker = new CKEDITOR.dom.walker( range ),\r
56                                         ignoreBookmarkTextEvaluator = CKEDITOR.dom.walker.bookmark( true, true );\r
57                                 // Avoid anchor inside bookmark inner text.\r
58                                 walker.evaluator = ignoreBookmarkTextEvaluator;\r
59                                 this._.nextNode = walker.next();\r
60                                 // TODO: It's better to have walker.reset() used here.\r
61                                 walker = new CKEDITOR.dom.walker( range );\r
62                                 walker.evaluator = ignoreBookmarkTextEvaluator;\r
63                                 var lastNode = walker.previous();\r
64                                 this._.lastNode = lastNode.getNextSourceNode( true );\r
65 \r
66                                 // We may have an empty text node at the end of block due to [3770].\r
67                                 // If that node is the lastNode, it would cause our logic to leak to the\r
68                                 // next block.(#3887)\r
69                                 if ( this._.lastNode &&\r
70                                                 this._.lastNode.type == CKEDITOR.NODE_TEXT &&\r
71                                                 !CKEDITOR.tools.trim( this._.lastNode.getText( ) ) &&\r
72                                                 this._.lastNode.getParent().isBlockBoundary() )\r
73                                 {\r
74                                         var testRange = new CKEDITOR.dom.range( range.document );\r
75                                         testRange.moveToPosition( this._.lastNode, CKEDITOR.POSITION_AFTER_END );\r
76                                         if ( testRange.checkEndOfBlock() )\r
77                                         {\r
78                                                 var path = new CKEDITOR.dom.elementPath( testRange.endContainer );\r
79                                                 var lastBlock = path.block || path.blockLimit;\r
80                                                 this._.lastNode = lastBlock.getNextSourceNode( true );\r
81                                         }\r
82                                 }\r
83 \r
84                                 // Probably the document end is reached, we need a marker node.\r
85                                 if ( !this._.lastNode )\r
86                                 {\r
87                                         this._.lastNode = this._.docEndMarker = range.document.createText( '' );\r
88                                         this._.lastNode.insertAfter( lastNode );\r
89                                 }\r
90 \r
91                                 // Let's reuse this variable.\r
92                                 range = null;\r
93                         }\r
94 \r
95                         var currentNode = this._.nextNode;\r
96                         lastNode = this._.lastNode;\r
97 \r
98                         this._.nextNode = null;\r
99                         while ( currentNode )\r
100                         {\r
101                                 // closeRange indicates that a paragraph boundary has been found,\r
102                                 // so the range can be closed.\r
103                                 var closeRange = false;\r
104 \r
105                                 // includeNode indicates that the current node is good to be part\r
106                                 // of the range. By default, any non-element node is ok for it.\r
107                                 var includeNode = ( currentNode.type != CKEDITOR.NODE_ELEMENT ),\r
108                                         continueFromSibling = false;\r
109 \r
110                                 // If it is an element node, let's check if it can be part of the\r
111                                 // range.\r
112                                 if ( !includeNode )\r
113                                 {\r
114                                         var nodeName = currentNode.getName();\r
115 \r
116                                         if ( currentNode.isBlockBoundary( this.forceBrBreak && { br : 1 } ) )\r
117                                         {\r
118                                                 // <br> boundaries must be part of the range. It will\r
119                                                 // happen only if ForceBrBreak.\r
120                                                 if ( nodeName == 'br' )\r
121                                                         includeNode = true;\r
122                                                 else if ( !range && !currentNode.getChildCount() && nodeName != 'hr' )\r
123                                                 {\r
124                                                         // If we have found an empty block, and haven't started\r
125                                                         // the range yet, it means we must return this block.\r
126                                                         block = currentNode;\r
127                                                         isLast = currentNode.equals( lastNode );\r
128                                                         break;\r
129                                                 }\r
130 \r
131                                                 // The range must finish right before the boundary,\r
132                                                 // including possibly skipped empty spaces. (#1603)\r
133                                                 if ( range )\r
134                                                 {\r
135                                                         range.setEndAt( currentNode, CKEDITOR.POSITION_BEFORE_START );\r
136 \r
137                                                         // The found boundary must be set as the next one at this\r
138                                                         // point. (#1717)\r
139                                                         if ( nodeName != 'br' )\r
140                                                                 this._.nextNode = currentNode;\r
141                                                 }\r
142 \r
143                                                 closeRange = true;\r
144                                         }\r
145                                         else\r
146                                         {\r
147                                                 // If we have child nodes, let's check them.\r
148                                                 if ( currentNode.getFirst() )\r
149                                                 {\r
150                                                         // If we don't have a range yet, let's start it.\r
151                                                         if ( !range )\r
152                                                         {\r
153                                                                 range = new CKEDITOR.dom.range( this.range.document );\r
154                                                                 range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );\r
155                                                         }\r
156 \r
157                                                         currentNode = currentNode.getFirst();\r
158                                                         continue;\r
159                                                 }\r
160                                                 includeNode = true;\r
161                                         }\r
162                                 }\r
163                                 else if ( currentNode.type == CKEDITOR.NODE_TEXT )\r
164                                 {\r
165                                         // Ignore normal whitespaces (i.e. not including &nbsp; or\r
166                                         // other unicode whitespaces) before/after a block node.\r
167                                         if ( beginWhitespaceRegex.test( currentNode.getText() ) )\r
168                                                 includeNode = false;\r
169                                 }\r
170 \r
171                                 // The current node is good to be part of the range and we are\r
172                                 // starting a new range, initialize it first.\r
173                                 if ( includeNode && !range )\r
174                                 {\r
175                                         range = new CKEDITOR.dom.range( this.range.document );\r
176                                         range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );\r
177                                 }\r
178 \r
179                                 // The last node has been found.\r
180                                 isLast = ( ( !closeRange || includeNode ) && currentNode.equals( lastNode ) );\r
181 \r
182                                 // If we are in an element boundary, let's check if it is time\r
183                                 // to close the range, otherwise we include the parent within it.\r
184                                 if ( range && !closeRange )\r
185                                 {\r
186                                         while ( !currentNode.getNext() && !isLast )\r
187                                         {\r
188                                                 var parentNode = currentNode.getParent();\r
189 \r
190                                                 if ( parentNode.isBlockBoundary( this.forceBrBreak && { br : 1 } ) )\r
191                                                 {\r
192                                                         closeRange = true;\r
193                                                         isLast = isLast || ( parentNode.equals( lastNode) );\r
194                                                         break;\r
195                                                 }\r
196 \r
197                                                 currentNode = parentNode;\r
198                                                 includeNode = true;\r
199                                                 isLast = ( currentNode.equals( lastNode ) );\r
200                                                 continueFromSibling = true;\r
201                                         }\r
202                                 }\r
203 \r
204                                 // Now finally include the node.\r
205                                 if ( includeNode )\r
206                                         range.setEndAt( currentNode, CKEDITOR.POSITION_AFTER_END );\r
207 \r
208                                 currentNode = currentNode.getNextSourceNode( continueFromSibling, null, lastNode );\r
209                                 isLast = !currentNode;\r
210 \r
211                                 // We have found a block boundary. Let's close the range and move out of the\r
212                                 // loop.\r
213                                 if ( ( closeRange || isLast ) && range )\r
214                                 {\r
215                                         var boundaryNodes = range.getBoundaryNodes(),\r
216                                                 startPath = new CKEDITOR.dom.elementPath( range.startContainer );\r
217 \r
218                                         // Drop the range if it only contains bookmark nodes, and is\r
219                                         // not because of the original collapsed range. (#4087,#4450)\r
220                                         if ( boundaryNodes.startNode.getParent().equals( startPath.blockLimit )\r
221                                                  && isBookmark( boundaryNodes.startNode ) && isBookmark( boundaryNodes.endNode ) )\r
222                                         {\r
223                                                 range = null;\r
224                                                 this._.nextNode = null;\r
225                                         }\r
226                                         else\r
227                                                 break;\r
228                                 }\r
229 \r
230                                 if ( isLast )\r
231                                         break;\r
232 \r
233                         }\r
234 \r
235                         // Now, based on the processed range, look for (or create) the block to be returned.\r
236                         if ( !block )\r
237                         {\r
238                                 // If no range has been found, this is the end.\r
239                                 if ( !range )\r
240                                 {\r
241                                         this._.docEndMarker && this._.docEndMarker.remove();\r
242                                         this._.nextNode = null;\r
243                                         return null;\r
244                                 }\r
245 \r
246                                 startPath = new CKEDITOR.dom.elementPath( range.startContainer );\r
247                                 var startBlockLimit = startPath.blockLimit,\r
248                                         checkLimits = { div : 1, th : 1, td : 1 };\r
249                                 block = startPath.block;\r
250 \r
251                                 if ( !block\r
252                                                 && !this.enforceRealBlocks\r
253                                                 && checkLimits[ startBlockLimit.getName() ]\r
254                                                 && range.checkStartOfBlock()\r
255                                                 && range.checkEndOfBlock() )\r
256                                         block = startBlockLimit;\r
257                                 else if ( !block || ( this.enforceRealBlocks && block.getName() == 'li' ) )\r
258                                 {\r
259                                         // Create the fixed block.\r
260                                         block = this.range.document.createElement( blockTag || 'p' );\r
261 \r
262                                         // Move the contents of the temporary range to the fixed block.\r
263                                         range.extractContents().appendTo( block );\r
264                                         block.trim();\r
265 \r
266                                         // Insert the fixed block into the DOM.\r
267                                         range.insertNode( block );\r
268 \r
269                                         removePreviousBr = removeLastBr = true;\r
270                                 }\r
271                                 else if ( block.getName() != 'li' )\r
272                                 {\r
273                                         // If the range doesn't includes the entire contents of the\r
274                                         // block, we must split it, isolating the range in a dedicated\r
275                                         // block.\r
276                                         if ( !range.checkStartOfBlock() || !range.checkEndOfBlock() )\r
277                                         {\r
278                                                 // The resulting block will be a clone of the current one.\r
279                                                 block = block.clone( false );\r
280 \r
281                                                 // Extract the range contents, moving it to the new block.\r
282                                                 range.extractContents().appendTo( block );\r
283                                                 block.trim();\r
284 \r
285                                                 // Split the block. At this point, the range will be in the\r
286                                                 // right position for our intents.\r
287                                                 var splitInfo = range.splitBlock();\r
288 \r
289                                                 removePreviousBr = !splitInfo.wasStartOfBlock;\r
290                                                 removeLastBr = !splitInfo.wasEndOfBlock;\r
291 \r
292                                                 // Insert the new block into the DOM.\r
293                                                 range.insertNode( block );\r
294                                         }\r
295                                 }\r
296                                 else if ( !isLast )\r
297                                 {\r
298                                         // LIs are returned as is, with all their children (due to the\r
299                                         // nested lists). But, the next node is the node right after\r
300                                         // the current range, which could be an <li> child (nested\r
301                                         // lists) or the next sibling <li>.\r
302 \r
303                                         this._.nextNode = ( block.equals( lastNode ) ? null :\r
304                                                 range.getBoundaryNodes().endNode.getNextSourceNode( true, null, lastNode ) );\r
305                                 }\r
306                         }\r
307 \r
308                         if ( removePreviousBr )\r
309                         {\r
310                                 var previousSibling = block.getPrevious();\r
311                                 if ( previousSibling && previousSibling.type == CKEDITOR.NODE_ELEMENT )\r
312                                 {\r
313                                         if ( previousSibling.getName() == 'br' )\r
314                                                 previousSibling.remove();\r
315                                         else if ( previousSibling.getLast() && previousSibling.getLast().$.nodeName.toLowerCase() == 'br' )\r
316                                                 previousSibling.getLast().remove();\r
317                                 }\r
318                         }\r
319 \r
320                         if ( removeLastBr )\r
321                         {\r
322                                 // Ignore bookmark nodes.(#3783)\r
323                                 var bookmarkGuard = CKEDITOR.dom.walker.bookmark( false, true );\r
324 \r
325                                 var lastChild = block.getLast();\r
326                                 if ( lastChild && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.getName() == 'br' )\r
327                                 {\r
328                                         // Take care not to remove the block expanding <br> in non-IE browsers.\r
329                                         if ( CKEDITOR.env.ie\r
330                                                  || lastChild.getPrevious( bookmarkGuard )\r
331                                                  || lastChild.getNext( bookmarkGuard ) )\r
332                                                 lastChild.remove();\r
333                                 }\r
334                         }\r
335 \r
336                         // Get a reference for the next element. This is important because the\r
337                         // above block can be removed or changed, so we can rely on it for the\r
338                         // next interation.\r
339                         if ( !this._.nextNode )\r
340                         {\r
341                                 this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null :\r
342                                         block.getNextSourceNode( true, null, lastNode );\r
343                         }\r
344 \r
345                         return block;\r
346                 }\r
347         };\r
348 \r
349         CKEDITOR.dom.range.prototype.createIterator = function()\r
350         {\r
351                 return new iterator( this );\r
352         };\r
353 })();\r