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