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