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