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