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