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