JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / core / dom / range.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 CKEDITOR.dom.range = function( document )\r
7 {\r
8         this.startContainer     = null;\r
9         this.startOffset        = null;\r
10         this.endContainer       = null;\r
11         this.endOffset          = null;\r
12         this.collapsed          = true;\r
13 \r
14         this.document = document;\r
15 };\r
16 \r
17 (function()\r
18 {\r
19         // Updates the "collapsed" property for the given range object.\r
20         var updateCollapsed = function( range )\r
21         {\r
22                 range.collapsed = (\r
23                         range.startContainer &&\r
24                         range.endContainer &&\r
25                         range.startContainer.equals( range.endContainer ) &&\r
26                         range.startOffset == range.endOffset );\r
27         };\r
28 \r
29         // This is a shared function used to delete, extract and clone the range\r
30         // contents.\r
31         // V2\r
32         var execContentsAction = function( range, action, docFrag )\r
33         {\r
34                 range.optimizeBookmark();\r
35 \r
36                 var startNode   = range.startContainer;\r
37                 var endNode             = range.endContainer;\r
38 \r
39                 var startOffset = range.startOffset;\r
40                 var endOffset   = range.endOffset;\r
41 \r
42                 var removeStartNode;\r
43                 var removeEndNode;\r
44 \r
45                 // For text containers, we must simply split the node and point to the\r
46                 // second part. The removal will be handled by the rest of the code .\r
47                 if ( endNode.type == CKEDITOR.NODE_TEXT )\r
48                         endNode = endNode.split( endOffset );\r
49                 else\r
50                 {\r
51                         // If the end container has children and the offset is pointing\r
52                         // to a child, then we should start from it.\r
53                         if ( endNode.getChildCount() > 0 )\r
54                         {\r
55                                 // If the offset points after the last node.\r
56                                 if ( endOffset >= endNode.getChildCount() )\r
57                                 {\r
58                                         // Let's create a temporary node and mark it for removal.\r
59                                         endNode = endNode.append( range.document.createText( '' ) );\r
60                                         removeEndNode = true;\r
61                                 }\r
62                                 else\r
63                                         endNode = endNode.getChild( endOffset );\r
64                         }\r
65                 }\r
66 \r
67                 // For text containers, we must simply split the node. The removal will\r
68                 // be handled by the rest of the code .\r
69                 if ( startNode.type == CKEDITOR.NODE_TEXT )\r
70                 {\r
71                         startNode.split( startOffset );\r
72 \r
73                         // In cases the end node is the same as the start node, the above\r
74                         // splitting will also split the end, so me must move the end to\r
75                         // the second part of the split.\r
76                         if ( startNode.equals( endNode ) )\r
77                                 endNode = startNode.getNext();\r
78                 }\r
79                 else\r
80                 {\r
81                         // If the start container has children and the offset is pointing\r
82                         // to a child, then we should start from its previous sibling.\r
83 \r
84                         // If the offset points to the first node, we don't have a\r
85                         // sibling, so let's use the first one, but mark it for removal.\r
86                         if ( !startOffset )\r
87                         {\r
88                                 // Let's create a temporary node and mark it for removal.\r
89                                 startNode = startNode.getFirst().insertBeforeMe( range.document.createText( '' ) );\r
90                                 removeStartNode = true;\r
91                         }\r
92                         else if ( startOffset >= startNode.getChildCount() )\r
93                         {\r
94                                 // Let's create a temporary node and mark it for removal.\r
95                                 startNode = startNode.append( range.document.createText( '' ) );\r
96                                 removeStartNode = true;\r
97                         }\r
98                         else\r
99                                 startNode = startNode.getChild( startOffset ).getPrevious();\r
100                 }\r
101 \r
102                 // Get the parent nodes tree for the start and end boundaries.\r
103                 var startParents        = startNode.getParents();\r
104                 var endParents          = endNode.getParents();\r
105 \r
106                 // Compare them, to find the top most siblings.\r
107                 var i, topStart, topEnd;\r
108 \r
109                 for ( i = 0 ; i < startParents.length ; i++ )\r
110                 {\r
111                         topStart = startParents[ i ];\r
112                         topEnd = endParents[ i ];\r
113 \r
114                         // The compared nodes will match until we find the top most\r
115                         // siblings (different nodes that have the same parent).\r
116                         // "i" will hold the index in the parents array for the top\r
117                         // most element.\r
118                         if ( !topStart.equals( topEnd ) )\r
119                                 break;\r
120                 }\r
121 \r
122                 var clone = docFrag, levelStartNode, levelClone, currentNode, currentSibling;\r
123 \r
124                 // Remove all successive sibling nodes for every node in the\r
125                 // startParents tree.\r
126                 for ( var j = i ; j < startParents.length ; j++ )\r
127                 {\r
128                         levelStartNode = startParents[j];\r
129 \r
130                         // For Extract and Clone, we must clone this level.\r
131                         if ( clone && !levelStartNode.equals( startNode ) )             // action = 0 = Delete\r
132                                 levelClone = clone.append( levelStartNode.clone() );\r
133 \r
134                         currentNode = levelStartNode.getNext();\r
135 \r
136                         while( currentNode )\r
137                         {\r
138                                 // Stop processing when the current node matches a node in the\r
139                                 // endParents tree or if it is the endNode.\r
140                                 if ( currentNode.equals( endParents[ j ] ) || currentNode.equals( endNode ) )\r
141                                         break;\r
142 \r
143                                 // Cache the next sibling.\r
144                                 currentSibling = currentNode.getNext();\r
145 \r
146                                 // If cloning, just clone it.\r
147                                 if ( action == 2 )      // 2 = Clone\r
148                                         clone.append( currentNode.clone( true ) );\r
149                                 else\r
150                                 {\r
151                                         // Both Delete and Extract will remove the node.\r
152                                         currentNode.remove();\r
153 \r
154                                         // When Extracting, move the removed node to the docFrag.\r
155                                         if ( action == 1 )      // 1 = Extract\r
156                                                 clone.append( currentNode );\r
157                                 }\r
158 \r
159                                 currentNode = currentSibling;\r
160                         }\r
161 \r
162                         if ( clone )\r
163                                 clone = levelClone;\r
164                 }\r
165 \r
166                 clone = docFrag;\r
167 \r
168                 // Remove all previous sibling nodes for every node in the\r
169                 // endParents tree.\r
170                 for ( var k = i ; k < endParents.length ; k++ )\r
171                 {\r
172                         levelStartNode = endParents[ k ];\r
173 \r
174                         // For Extract and Clone, we must clone this level.\r
175                         if ( action > 0 && !levelStartNode.equals( endNode ) )          // action = 0 = Delete\r
176                                 levelClone = clone.append( levelStartNode.clone() );\r
177 \r
178                         // The processing of siblings may have already been done by the parent.\r
179                         if ( !startParents[ k ] || levelStartNode.$.parentNode != startParents[ k ].$.parentNode )\r
180                         {\r
181                                 currentNode = levelStartNode.getPrevious();\r
182 \r
183                                 while( currentNode )\r
184                                 {\r
185                                         // Stop processing when the current node matches a node in the\r
186                                         // startParents tree or if it is the startNode.\r
187                                         if ( currentNode.equals( startParents[ k ] ) || currentNode.equals( startNode ) )\r
188                                                 break;\r
189 \r
190                                         // Cache the next sibling.\r
191                                         currentSibling = currentNode.getPrevious();\r
192 \r
193                                         // If cloning, just clone it.\r
194                                         if ( action == 2 )      // 2 = Clone\r
195                                                 clone.$.insertBefore( currentNode.$.cloneNode( true ), clone.$.firstChild ) ;\r
196                                         else\r
197                                         {\r
198                                                 // Both Delete and Extract will remove the node.\r
199                                                 currentNode.remove();\r
200 \r
201                                                 // When Extracting, mode the removed node to the docFrag.\r
202                                                 if ( action == 1 )      // 1 = Extract\r
203                                                         clone.$.insertBefore( currentNode.$, clone.$.firstChild );\r
204                                         }\r
205 \r
206                                         currentNode = currentSibling;\r
207                                 }\r
208                         }\r
209 \r
210                         if ( clone )\r
211                                 clone = levelClone;\r
212                 }\r
213 \r
214                 if ( action == 2 )              // 2 = Clone.\r
215                 {\r
216                         // No changes in the DOM should be done, so fix the split text (if any).\r
217 \r
218                         var startTextNode = range.startContainer;\r
219                         if ( startTextNode.type == CKEDITOR.NODE_TEXT )\r
220                         {\r
221                                 startTextNode.$.data += startTextNode.$.nextSibling.data;\r
222                                 startTextNode.$.parentNode.removeChild( startTextNode.$.nextSibling );\r
223                         }\r
224 \r
225                         var endTextNode = range.endContainer;\r
226                         if ( endTextNode.type == CKEDITOR.NODE_TEXT && endTextNode.$.nextSibling )\r
227                         {\r
228                                 endTextNode.$.data += endTextNode.$.nextSibling.data;\r
229                                 endTextNode.$.parentNode.removeChild( endTextNode.$.nextSibling );\r
230                         }\r
231                 }\r
232                 else\r
233                 {\r
234                         // Collapse the range.\r
235 \r
236                         // If a node has been partially selected, collapse the range between\r
237                         // topStart and topEnd. Otherwise, simply collapse it to the start. (W3C specs).\r
238                         if ( topStart && topEnd && ( startNode.$.parentNode != topStart.$.parentNode || endNode.$.parentNode != topEnd.$.parentNode ) )\r
239                         {\r
240                                 var endIndex = topEnd.getIndex();\r
241 \r
242                                 // If the start node is to be removed, we must correct the\r
243                                 // index to reflect the removal.\r
244                                 if ( removeStartNode && topEnd.$.parentNode == startNode.$.parentNode )\r
245                                         endIndex--;\r
246 \r
247                                 range.setStart( topEnd.getParent(), endIndex );\r
248                         }\r
249 \r
250                         // Collapse it to the start.\r
251                         range.collapse( true );\r
252                 }\r
253 \r
254                 // Cleanup any marked node.\r
255                 if( removeStartNode )\r
256                         startNode.remove();\r
257 \r
258                 if( removeEndNode && endNode.$.parentNode )\r
259                         endNode.remove();\r
260         };\r
261 \r
262         var inlineChildReqElements = { abbr:1,acronym:1,b:1,bdo:1,big:1,cite:1,code:1,del:1,dfn:1,em:1,font:1,i:1,ins:1,label:1,kbd:1,q:1,samp:1,small:1,span:1,strike:1,strong:1,sub:1,sup:1,tt:1,u:1,'var':1 };\r
263 \r
264         // Creates the appropriate node evaluator for the dom walker used inside\r
265         // check(Start|End)OfBlock.\r
266         function getCheckStartEndBlockEvalFunction( isStart )\r
267         {\r
268                 var hadBr = false, bookmarkEvaluator = CKEDITOR.dom.walker.bookmark( true );\r
269                 return function( node )\r
270                 {\r
271                         // First ignore bookmark nodes.\r
272                         if ( bookmarkEvaluator( node ) )\r
273                                 return true;\r
274 \r
275                         if ( node.type == CKEDITOR.NODE_TEXT )\r
276                         {\r
277                                 // If there's any visible text, then we're not at the start.\r
278                                 if ( CKEDITOR.tools.trim( node.getText() ).length )\r
279                                         return false;\r
280                                 }\r
281                         else if( node.type == CKEDITOR.NODE_ELEMENT )\r
282                         {\r
283                                 // If there are non-empty inline elements (e.g. <img />), then we're not\r
284                                 // at the start.\r
285                                 if ( !inlineChildReqElements[ node.getName() ] )\r
286                                 {\r
287                                         // If we're working at the end-of-block, forgive the first <br /> in non-IE\r
288                                         // browsers.\r
289                                         if ( !isStart && !CKEDITOR.env.ie && node.getName() == 'br' && !hadBr )\r
290                                                 hadBr = true;\r
291                                         else\r
292                                                 return false;\r
293                                 }\r
294                         }\r
295                         return true;\r
296                 };\r
297         }\r
298 \r
299         // Evaluator for CKEDITOR.dom.element::checkBoundaryOfElement, reject any\r
300         // text node and non-empty elements unless it's being bookmark text.\r
301         function elementBoundaryEval( node )\r
302         {\r
303                 // Reject any text node unless it's being bookmark\r
304                 // OR it's spaces. (#3883)\r
305                 return node.type != CKEDITOR.NODE_TEXT\r
306                             && node.getName() in CKEDITOR.dtd.$removeEmpty\r
307                             || !CKEDITOR.tools.trim( node.getText() )\r
308                             || node.getParent().hasAttribute( '_fck_bookmark' );\r
309         }\r
310 \r
311         var whitespaceEval = new CKEDITOR.dom.walker.whitespaces(),\r
312                 bookmarkEval = new CKEDITOR.dom.walker.bookmark();\r
313 \r
314         function nonWhitespaceOrBookmarkEval( node )\r
315         {\r
316                 // Whitespaces and bookmark nodes are to be ignored.\r
317                 return !whitespaceEval( node ) && !bookmarkEval( node );\r
318         }\r
319 \r
320         CKEDITOR.dom.range.prototype =\r
321         {\r
322                 clone : function()\r
323                 {\r
324                         var clone = new CKEDITOR.dom.range( this.document );\r
325 \r
326                         clone.startContainer = this.startContainer;\r
327                         clone.startOffset = this.startOffset;\r
328                         clone.endContainer = this.endContainer;\r
329                         clone.endOffset = this.endOffset;\r
330                         clone.collapsed = this.collapsed;\r
331 \r
332                         return clone;\r
333                 },\r
334 \r
335                 collapse : function( toStart )\r
336                 {\r
337                         if ( toStart )\r
338                         {\r
339                                 this.endContainer       = this.startContainer;\r
340                                 this.endOffset          = this.startOffset;\r
341                         }\r
342                         else\r
343                         {\r
344                                 this.startContainer     = this.endContainer;\r
345                                 this.startOffset        = this.endOffset;\r
346                         }\r
347 \r
348                         this.collapsed = true;\r
349                 },\r
350 \r
351                 // The selection may be lost when cloning (due to the splitText() call).\r
352                 cloneContents : function()\r
353                 {\r
354                         var docFrag = new CKEDITOR.dom.documentFragment( this.document );\r
355 \r
356                         if ( !this.collapsed )\r
357                                 execContentsAction( this, 2, docFrag );\r
358 \r
359                         return docFrag;\r
360                 },\r
361 \r
362                 deleteContents : function()\r
363                 {\r
364                         if ( this.collapsed )\r
365                                 return;\r
366 \r
367                         execContentsAction( this, 0 );\r
368                 },\r
369 \r
370                 extractContents : function()\r
371                 {\r
372                         var docFrag = new CKEDITOR.dom.documentFragment( this.document );\r
373 \r
374                         if ( !this.collapsed )\r
375                                 execContentsAction( this, 1, docFrag );\r
376 \r
377                         return docFrag;\r
378                 },\r
379 \r
380                 /**\r
381                  * Creates a bookmark object, which can be later used to restore the\r
382                  * range by using the moveToBookmark function.\r
383                  * This is an "intrusive" way to create a bookmark. It includes <span> tags\r
384                  * in the range boundaries. The advantage of it is that it is possible to\r
385                  * handle DOM mutations when moving back to the bookmark.\r
386                  * Attention: the inclusion of nodes in the DOM is a design choice and\r
387                  * should not be changed as there are other points in the code that may be\r
388                  * using those nodes to perform operations. See GetBookmarkNode.\r
389                  * @param {Boolean} [serializable] Indicates that the bookmark nodes\r
390                  *              must contain ids, which can be used to restore the range even\r
391                  *              when these nodes suffer mutations (like a clonation or innerHTML\r
392                  *              change).\r
393                  * @returns {Object} And object representing a bookmark.\r
394                  */\r
395                 createBookmark : function( serializable )\r
396                 {\r
397                         var startNode, endNode;\r
398                         var baseId;\r
399                         var clone;\r
400 \r
401                         startNode = this.document.createElement( 'span' );\r
402                         startNode.setAttribute( '_fck_bookmark', 1 );\r
403                         startNode.setStyle( 'display', 'none' );\r
404 \r
405                         // For IE, it must have something inside, otherwise it may be\r
406                         // removed during DOM operations.\r
407                         startNode.setHtml( '&nbsp;' );\r
408 \r
409                         if ( serializable )\r
410                         {\r
411                                 baseId = 'cke_bm_' + CKEDITOR.tools.getNextNumber();\r
412                                 startNode.setAttribute( 'id', baseId + 'S' );\r
413                         }\r
414 \r
415                         // If collapsed, the endNode will not be created.\r
416                         if ( !this.collapsed )\r
417                         {\r
418                                 endNode = startNode.clone();\r
419                                 endNode.setHtml( '&nbsp;' );\r
420 \r
421                                 if ( serializable )\r
422                                         endNode.setAttribute( 'id', baseId + 'E' );\r
423 \r
424                                 clone = this.clone();\r
425                                 clone.collapse();\r
426                                 clone.insertNode( endNode );\r
427                         }\r
428 \r
429                         clone = this.clone();\r
430                         clone.collapse( true );\r
431                         clone.insertNode( startNode );\r
432 \r
433                         // Update the range position.\r
434                         if ( endNode )\r
435                         {\r
436                                 this.setStartAfter( startNode );\r
437                                 this.setEndBefore( endNode );\r
438                         }\r
439                         else\r
440                                 this.moveToPosition( startNode, CKEDITOR.POSITION_AFTER_END );\r
441 \r
442                         return {\r
443                                 startNode : serializable ? baseId + 'S' : startNode,\r
444                                 endNode : serializable ? baseId + 'E' : endNode,\r
445                                 serializable : serializable\r
446                         };\r
447                 },\r
448 \r
449                 /**\r
450                  * Creates a "non intrusive" and "mutation sensible" bookmark. This\r
451                  * kind of bookmark should be used only when the DOM is supposed to\r
452                  * remain stable after its creation.\r
453                  * @param {Boolean} [normalized] Indicates that the bookmark must\r
454                  *              normalized. When normalized, the successive text nodes are\r
455                  *              considered a single node. To sucessful load a normalized\r
456                  *              bookmark, the DOM tree must be also normalized before calling\r
457                  *              moveToBookmark.\r
458                  * @returns {Object} An object representing the bookmark.\r
459                  */\r
460                 createBookmark2 : function( normalized )\r
461                 {\r
462                         var startContainer      = this.startContainer,\r
463                                 endContainer    = this.endContainer;\r
464 \r
465                         var startOffset = this.startOffset,\r
466                                 endOffset       = this.endOffset;\r
467 \r
468                         var child, previous;\r
469 \r
470                         // If there is no range then get out of here.\r
471                         // It happens on initial load in Safari #962 and if the editor it's\r
472                         // hidden also in Firefox\r
473                         if ( !startContainer || !endContainer )\r
474                                 return { start : 0, end : 0 };\r
475 \r
476                         if ( normalized )\r
477                         {\r
478                                 // Find out if the start is pointing to a text node that will\r
479                                 // be normalized.\r
480                                 if ( startContainer.type == CKEDITOR.NODE_ELEMENT )\r
481                                 {\r
482                                         child = startContainer.getChild( startOffset );\r
483 \r
484                                         // In this case, move the start information to that text\r
485                                         // node.\r
486                                         if ( child && child.type == CKEDITOR.NODE_TEXT\r
487                                                         && startOffset > 0 && child.getPrevious().type == CKEDITOR.NODE_TEXT )\r
488                                         {\r
489                                                 startContainer = child;\r
490                                                 startOffset = 0;\r
491                                         }\r
492                                 }\r
493 \r
494                                 // Normalize the start.\r
495                                 while ( startContainer.type == CKEDITOR.NODE_TEXT\r
496                                                 && ( previous = startContainer.getPrevious() )\r
497                                                 && previous.type == CKEDITOR.NODE_TEXT )\r
498                                 {\r
499                                         startContainer = previous;\r
500                                         startOffset += previous.getLength();\r
501                                 }\r
502 \r
503                                 // Process the end only if not normalized.\r
504                                 if ( !this.isCollapsed )\r
505                                 {\r
506                                         // Find out if the start is pointing to a text node that\r
507                                         // will be normalized.\r
508                                         if ( endContainer.type == CKEDITOR.NODE_ELEMENT )\r
509                                         {\r
510                                                 child = endContainer.getChild( endOffset );\r
511 \r
512                                                 // In this case, move the start information to that\r
513                                                 // text node.\r
514                                                 if ( child && child.type == CKEDITOR.NODE_TEXT\r
515                                                                 && endOffset > 0 && child.getPrevious().type == CKEDITOR.NODE_TEXT )\r
516                                                 {\r
517                                                         endContainer = child;\r
518                                                         endOffset = 0;\r
519                                                 }\r
520                                         }\r
521 \r
522                                         // Normalize the end.\r
523                                         while ( endContainer.type == CKEDITOR.NODE_TEXT\r
524                                                         && ( previous = endContainer.getPrevious() )\r
525                                                         && previous.type == CKEDITOR.NODE_TEXT )\r
526                                         {\r
527                                                 endContainer = previous;\r
528                                                 endOffset += previous.getLength();\r
529                                         }\r
530                                 }\r
531                         }\r
532 \r
533                         return {\r
534                                 start           : startContainer.getAddress( normalized ),\r
535                                 end                     : this.isCollapsed ? null : endContainer.getAddress( normalized ),\r
536                                 startOffset     : startOffset,\r
537                                 endOffset       : endOffset,\r
538                                 normalized      : normalized,\r
539                                 is2                     : true          // It's a createBookmark2 bookmark.\r
540                         };\r
541                 },\r
542 \r
543                 moveToBookmark : function( bookmark )\r
544                 {\r
545                         if ( bookmark.is2 )             // Created with createBookmark2().\r
546                         {\r
547                                 // Get the start information.\r
548                                 var startContainer      = this.document.getByAddress( bookmark.start, bookmark.normalized ),\r
549                                         startOffset     = bookmark.startOffset;\r
550 \r
551                                 // Get the end information.\r
552                                 var endContainer        = bookmark.end && this.document.getByAddress( bookmark.end, bookmark.normalized ),\r
553                                         endOffset       = bookmark.endOffset;\r
554 \r
555                                 // Set the start boundary.\r
556                                 this.setStart( startContainer, startOffset );\r
557 \r
558                                 // Set the end boundary. If not available, collapse it.\r
559                                 if ( endContainer )\r
560                                         this.setEnd( endContainer, endOffset );\r
561                                 else\r
562                                         this.collapse( true );\r
563                         }\r
564                         else                                    // Created with createBookmark().\r
565                         {\r
566                                 var serializable = bookmark.serializable,\r
567                                         startNode       = serializable ? this.document.getById( bookmark.startNode ) : bookmark.startNode,\r
568                                         endNode         = serializable ? this.document.getById( bookmark.endNode ) : bookmark.endNode;\r
569 \r
570                                 // Set the range start at the bookmark start node position.\r
571                                 this.setStartBefore( startNode );\r
572 \r
573                                 // Remove it, because it may interfere in the setEndBefore call.\r
574                                 startNode.remove();\r
575 \r
576                                 // Set the range end at the bookmark end node position, or simply\r
577                                 // collapse it if it is not available.\r
578                                 if ( endNode )\r
579                                 {\r
580                                         this.setEndBefore( endNode );\r
581                                         endNode.remove();\r
582                                 }\r
583                                 else\r
584                                         this.collapse( true );\r
585                         }\r
586                 },\r
587 \r
588                 getBoundaryNodes : function()\r
589                 {\r
590                         var startNode = this.startContainer,\r
591                                 endNode = this.endContainer,\r
592                                 startOffset = this.startOffset,\r
593                                 endOffset = this.endOffset,\r
594                                 childCount;\r
595 \r
596                         if ( startNode.type == CKEDITOR.NODE_ELEMENT )\r
597                         {\r
598                                 childCount = startNode.getChildCount();\r
599                                 if ( childCount > startOffset )\r
600                                         startNode = startNode.getChild( startOffset );\r
601                                 else if ( childCount < 1 )\r
602                                         startNode = startNode.getPreviousSourceNode();\r
603                                 else            // startOffset > childCount but childCount is not 0\r
604                                 {\r
605                                         // Try to take the node just after the current position.\r
606                                         startNode = startNode.$;\r
607                                         while ( startNode.lastChild )\r
608                                                 startNode = startNode.lastChild;\r
609                                         startNode = new CKEDITOR.dom.node( startNode );\r
610 \r
611                                         // Normally we should take the next node in DFS order. But it\r
612                                         // is also possible that we've already reached the end of\r
613                                         // document.\r
614                                         startNode = startNode.getNextSourceNode() || startNode;\r
615                                 }\r
616                         }\r
617                         if ( endNode.type == CKEDITOR.NODE_ELEMENT )\r
618                         {\r
619                                 childCount = endNode.getChildCount();\r
620                                 if ( childCount > endOffset )\r
621                                         endNode = endNode.getChild( endOffset ).getPreviousSourceNode( true );\r
622                                 else if ( childCount < 1 )\r
623                                         endNode = endNode.getPreviousSourceNode();\r
624                                 else            // endOffset > childCount but childCount is not 0\r
625                                 {\r
626                                         // Try to take the node just before the current position.\r
627                                         endNode = endNode.$;\r
628                                         while ( endNode.lastChild )\r
629                                                 endNode = endNode.lastChild;\r
630                                         endNode = new CKEDITOR.dom.node( endNode );\r
631                                 }\r
632                         }\r
633 \r
634                         // Sometimes the endNode will come right before startNode for collapsed\r
635                         // ranges. Fix it. (#3780)\r
636                         if ( startNode.getPosition( endNode ) & CKEDITOR.POSITION_FOLLOWING )\r
637                                 startNode = endNode;\r
638 \r
639                         return { startNode : startNode, endNode : endNode };\r
640                 },\r
641 \r
642                 /**\r
643                  * Find the node which fully contains the range.\r
644                  * @param includeSelf\r
645                  * @param {Boolean} ignoreTextNode Whether ignore CKEDITOR.NODE_TEXT type.\r
646                  */\r
647                 getCommonAncestor : function( includeSelf , ignoreTextNode )\r
648                 {\r
649                         var start = this.startContainer,\r
650                                 end = this.endContainer,\r
651                                 ancestor;\r
652 \r
653                         if ( start.equals( end ) )\r
654                         {\r
655                                 if ( includeSelf\r
656                                                 && start.type == CKEDITOR.NODE_ELEMENT\r
657                                                 && this.startOffset == this.endOffset - 1 )\r
658                                         ancestor = start.getChild( this.startOffset );\r
659                                 else\r
660                                         ancestor = start;\r
661                         }\r
662                         else\r
663                                 ancestor = start.getCommonAncestor( end );\r
664 \r
665                         return ignoreTextNode && !ancestor.is ? ancestor.getParent() : ancestor;\r
666                 },\r
667 \r
668                 /**\r
669                  * Transforms the startContainer and endContainer properties from text\r
670                  * nodes to element nodes, whenever possible. This is actually possible\r
671                  * if either of the boundary containers point to a text node, and its\r
672                  * offset is set to zero, or after the last char in the node.\r
673                  */\r
674                 optimize : function()\r
675                 {\r
676                         var container = this.startContainer;\r
677                         var offset = this.startOffset;\r
678 \r
679                         if ( container.type != CKEDITOR.NODE_ELEMENT )\r
680                         {\r
681                                 if ( !offset )\r
682                                         this.setStartBefore( container );\r
683                                 else if ( offset >= container.getLength() )\r
684                                         this.setStartAfter( container );\r
685                         }\r
686 \r
687                         container = this.endContainer;\r
688                         offset = this.endOffset;\r
689 \r
690                         if ( container.type != CKEDITOR.NODE_ELEMENT )\r
691                         {\r
692                                 if ( !offset )\r
693                                         this.setEndBefore( container );\r
694                                 else if ( offset >= container.getLength() )\r
695                                         this.setEndAfter( container );\r
696                         }\r
697                 },\r
698 \r
699                 /**\r
700                  * Move the range out of bookmark nodes if they're been the container.\r
701                  */\r
702                 optimizeBookmark: function()\r
703                 {\r
704                         var startNode = this.startContainer,\r
705                                 endNode = this.endContainer;\r
706 \r
707                         if ( startNode.is && startNode.is( 'span' )\r
708                                 && startNode.hasAttribute( '_fck_bookmark' ) )\r
709                                 this.setStartAt( startNode, CKEDITOR.POSITION_BEFORE_START );\r
710                         if ( endNode && endNode.is && endNode.is( 'span' )\r
711                                 && endNode.hasAttribute( '_fck_bookmark' ) )\r
712                                 this.setEndAt( endNode,  CKEDITOR.POSITION_AFTER_END );\r
713                 },\r
714 \r
715                 trim : function( ignoreStart, ignoreEnd )\r
716                 {\r
717                         var startContainer = this.startContainer,\r
718                                 startOffset = this.startOffset,\r
719                                 collapsed = this.collapsed;\r
720                         if ( ( !ignoreStart || collapsed )\r
721                                  && startContainer && startContainer.type == CKEDITOR.NODE_TEXT )\r
722                         {\r
723                                 // If the offset is zero, we just insert the new node before\r
724                                 // the start.\r
725                                 if ( !startOffset )\r
726                                 {\r
727                                         startOffset = startContainer.getIndex();\r
728                                         startContainer = startContainer.getParent();\r
729                                 }\r
730                                 // If the offset is at the end, we'll insert it after the text\r
731                                 // node.\r
732                                 else if ( startOffset >= startContainer.getLength() )\r
733                                 {\r
734                                         startOffset = startContainer.getIndex() + 1;\r
735                                         startContainer = startContainer.getParent();\r
736                                 }\r
737                                 // In other case, we split the text node and insert the new\r
738                                 // node at the split point.\r
739                                 else\r
740                                 {\r
741                                         var nextText = startContainer.split( startOffset );\r
742 \r
743                                         startOffset = startContainer.getIndex() + 1;\r
744                                         startContainer = startContainer.getParent();\r
745                                         // Check if it is necessary to update the end boundary.\r
746                                         if ( !collapsed && this.startContainer.equals( this.endContainer ) )\r
747                                                 this.setEnd( nextText, this.endOffset - this.startOffset );\r
748                                 }\r
749 \r
750                                 this.setStart( startContainer, startOffset );\r
751 \r
752                                 if ( collapsed )\r
753                                         this.collapse( true );\r
754                         }\r
755 \r
756                         var endContainer = this.endContainer;\r
757                         var endOffset = this.endOffset;\r
758 \r
759                         if ( !( ignoreEnd || collapsed )\r
760                                  && endContainer && endContainer.type == CKEDITOR.NODE_TEXT )\r
761                         {\r
762                                 // If the offset is zero, we just insert the new node before\r
763                                 // the start.\r
764                                 if ( !endOffset )\r
765                                 {\r
766                                         endOffset = endContainer.getIndex();\r
767                                         endContainer = endContainer.getParent();\r
768                                 }\r
769                                 // If the offset is at the end, we'll insert it after the text\r
770                                 // node.\r
771                                 else if ( endOffset >= endContainer.getLength() )\r
772                                 {\r
773                                         endOffset = endContainer.getIndex() + 1;\r
774                                         endContainer = endContainer.getParent();\r
775                                 }\r
776                                 // In other case, we split the text node and insert the new\r
777                                 // node at the split point.\r
778                                 else\r
779                                 {\r
780                                         endContainer.split( endOffset );\r
781 \r
782                                         endOffset = endContainer.getIndex() + 1;\r
783                                         endContainer = endContainer.getParent();\r
784                                 }\r
785 \r
786                                 this.setEnd( endContainer, endOffset );\r
787                         }\r
788                 },\r
789 \r
790                 enlarge : function( unit )\r
791                 {\r
792                         switch ( unit )\r
793                         {\r
794                                 case CKEDITOR.ENLARGE_ELEMENT :\r
795 \r
796                                         if ( this.collapsed )\r
797                                                 return;\r
798 \r
799                                         // Get the common ancestor.\r
800                                         var commonAncestor = this.getCommonAncestor();\r
801 \r
802                                         var body = this.document.getBody();\r
803 \r
804                                         // For each boundary\r
805                                         //              a. Depending on its position, find out the first node to be checked (a sibling) or, if not available, to be enlarge.\r
806                                         //              b. Go ahead checking siblings and enlarging the boundary as much as possible until the common ancestor is not reached. After reaching the common ancestor, just save the enlargeable node to be used later.\r
807 \r
808                                         var startTop, endTop;\r
809 \r
810                                         var enlargeable, sibling, commonReached;\r
811 \r
812                                         // Indicates that the node can be added only if whitespace\r
813                                         // is available before it.\r
814                                         var needsWhiteSpace = false;\r
815                                         var isWhiteSpace;\r
816                                         var siblingText;\r
817 \r
818                                         // Process the start boundary.\r
819 \r
820                                         var container = this.startContainer;\r
821                                         var offset = this.startOffset;\r
822 \r
823                                         if ( container.type == CKEDITOR.NODE_TEXT )\r
824                                         {\r
825                                                 if ( offset )\r
826                                                 {\r
827                                                         // Check if there is any non-space text before the\r
828                                                         // offset. Otherwise, container is null.\r
829                                                         container = !CKEDITOR.tools.trim( container.substring( 0, offset ) ).length && container;\r
830 \r
831                                                         // If we found only whitespace in the node, it\r
832                                                         // means that we'll need more whitespace to be able\r
833                                                         // to expand. For example, <i> can be expanded in\r
834                                                         // "A <i> [B]</i>", but not in "A<i> [B]</i>".\r
835                                                         needsWhiteSpace = !!container;\r
836                                                 }\r
837 \r
838                                                 if ( container )\r
839                                                 {\r
840                                                         if ( !( sibling = container.getPrevious() ) )\r
841                                                                 enlargeable = container.getParent();\r
842                                                 }\r
843                                         }\r
844                                         else\r
845                                         {\r
846                                                 // If we have offset, get the node preceeding it as the\r
847                                                 // first sibling to be checked.\r
848                                                 if ( offset )\r
849                                                         sibling = container.getChild( offset - 1 ) || container.getLast();\r
850 \r
851                                                 // If there is no sibling, mark the container to be\r
852                                                 // enlarged.\r
853                                                 if ( !sibling )\r
854                                                         enlargeable = container;\r
855                                         }\r
856 \r
857                                         while ( enlargeable || sibling )\r
858                                         {\r
859                                                 if ( enlargeable && !sibling )\r
860                                                 {\r
861                                                         // If we reached the common ancestor, mark the flag\r
862                                                         // for it.\r
863                                                         if ( !commonReached && enlargeable.equals( commonAncestor ) )\r
864                                                                 commonReached = true;\r
865 \r
866                                                         if ( !body.contains( enlargeable ) )\r
867                                                                 break;\r
868 \r
869                                                         // If we don't need space or this element breaks\r
870                                                         // the line, then enlarge it.\r
871                                                         if ( !needsWhiteSpace || enlargeable.getComputedStyle( 'display' ) != 'inline' )\r
872                                                         {\r
873                                                                 needsWhiteSpace = false;\r
874 \r
875                                                                 // If the common ancestor has been reached,\r
876                                                                 // we'll not enlarge it immediately, but just\r
877                                                                 // mark it to be enlarged later if the end\r
878                                                                 // boundary also enlarges it.\r
879                                                                 if ( commonReached )\r
880                                                                         startTop = enlargeable;\r
881                                                                 else\r
882                                                                         this.setStartBefore( enlargeable );\r
883                                                         }\r
884 \r
885                                                         sibling = enlargeable.getPrevious();\r
886                                                 }\r
887 \r
888                                                 // Check all sibling nodes preceeding the enlargeable\r
889                                                 // node. The node wil lbe enlarged only if none of them\r
890                                                 // blocks it.\r
891                                                 while ( sibling )\r
892                                                 {\r
893                                                         // This flag indicates that this node has\r
894                                                         // whitespaces at the end.\r
895                                                         isWhiteSpace = false;\r
896 \r
897                                                         if ( sibling.type == CKEDITOR.NODE_TEXT )\r
898                                                         {\r
899                                                                 siblingText = sibling.getText();\r
900 \r
901                                                                 if ( /[^\s\ufeff]/.test( siblingText ) )\r
902                                                                         sibling = null;\r
903 \r
904                                                                 isWhiteSpace = /[\s\ufeff]$/.test( siblingText );\r
905                                                         }\r
906                                                         else\r
907                                                         {\r
908                                                                 // If this is a visible element.\r
909                                                                 // We need to check for the bookmark attribute because IE insists on\r
910                                                                 // rendering the display:none nodes we use for bookmarks. (#3363)\r
911                                                                 if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_fck_bookmark' ) )\r
912                                                                 {\r
913                                                                         // We'll accept it only if we need\r
914                                                                         // whitespace, and this is an inline\r
915                                                                         // element with whitespace only.\r
916                                                                         if ( needsWhiteSpace && CKEDITOR.dtd.$removeEmpty[ sibling.getName() ] )\r
917                                                                         {\r
918                                                                                 // It must contains spaces and inline elements only.\r
919 \r
920                                                                                 siblingText = sibling.getText();\r
921 \r
922                                                                                 if ( !(/[^\s\ufeff]/).test( siblingText ) )     // Spaces + Zero Width No-Break Space (U+FEFF)\r
923                                                                                         sibling = null;\r
924                                                                                 else\r
925                                                                                 {\r
926                                                                                         var allChildren = sibling.$.all || sibling.$.getElementsByTagName( '*' );\r
927                                                                                         for ( var i = 0, child ; child = allChildren[ i++ ] ; )\r
928                                                                                         {\r
929                                                                                                 if ( !CKEDITOR.dtd.$removeEmpty[ child.nodeName.toLowerCase() ] )\r
930                                                                                                 {\r
931                                                                                                         sibling = null;\r
932                                                                                                         break;\r
933                                                                                                 }\r
934                                                                                         }\r
935                                                                                 }\r
936 \r
937                                                                                 if ( sibling )\r
938                                                                                         isWhiteSpace = !!siblingText.length;\r
939                                                                         }\r
940                                                                         else\r
941                                                                                 sibling = null;\r
942                                                                 }\r
943                                                         }\r
944 \r
945                                                         // A node with whitespaces has been found.\r
946                                                         if ( isWhiteSpace )\r
947                                                         {\r
948                                                                 // Enlarge the last enlargeable node, if we\r
949                                                                 // were waiting for spaces.\r
950                                                                 if ( needsWhiteSpace )\r
951                                                                 {\r
952                                                                         if ( commonReached )\r
953                                                                                 startTop = enlargeable;\r
954                                                                         else if ( enlargeable )\r
955                                                                                 this.setStartBefore( enlargeable );\r
956                                                                 }\r
957                                                                 else\r
958                                                                         needsWhiteSpace = true;\r
959                                                         }\r
960 \r
961                                                         if ( sibling )\r
962                                                         {\r
963                                                                 var next = sibling.getPrevious();\r
964 \r
965                                                                 if ( !enlargeable && !next )\r
966                                                                 {\r
967                                                                         // Set the sibling as enlargeable, so it's\r
968                                                                         // parent will be get later outside this while.\r
969                                                                         enlargeable = sibling;\r
970                                                                         sibling = null;\r
971                                                                         break;\r
972                                                                 }\r
973 \r
974                                                                 sibling = next;\r
975                                                         }\r
976                                                         else\r
977                                                         {\r
978                                                                 // If sibling has been set to null, then we\r
979                                                                 // need to stop enlarging.\r
980                                                                 enlargeable = null;\r
981                                                         }\r
982                                                 }\r
983 \r
984                                                 if ( enlargeable )\r
985                                                         enlargeable = enlargeable.getParent();\r
986                                         }\r
987 \r
988                                         // Process the end boundary. This is basically the same\r
989                                         // code used for the start boundary, with small changes to\r
990                                         // make it work in the oposite side (to the right). This\r
991                                         // makes it difficult to reuse the code here. So, fixes to\r
992                                         // the above code are likely to be replicated here.\r
993 \r
994                                         container = this.endContainer;\r
995                                         offset = this.endOffset;\r
996 \r
997                                         // Reset the common variables.\r
998                                         enlargeable = sibling = null;\r
999                                         commonReached = needsWhiteSpace = false;\r
1000 \r
1001                                         if ( container.type == CKEDITOR.NODE_TEXT )\r
1002                                         {\r
1003                                                 // Check if there is any non-space text after the\r
1004                                                 // offset. Otherwise, container is null.\r
1005                                                 container = !CKEDITOR.tools.trim( container.substring( offset ) ).length && container;\r
1006 \r
1007                                                 // If we found only whitespace in the node, it\r
1008                                                 // means that we'll need more whitespace to be able\r
1009                                                 // to expand. For example, <i> can be expanded in\r
1010                                                 // "A <i> [B]</i>", but not in "A<i> [B]</i>".\r
1011                                                 needsWhiteSpace = !( container && container.getLength() );\r
1012 \r
1013                                                 if ( container )\r
1014                                                 {\r
1015                                                         if ( !( sibling = container.getNext() ) )\r
1016                                                                 enlargeable = container.getParent();\r
1017                                                 }\r
1018                                         }\r
1019                                         else\r
1020                                         {\r
1021                                                 // Get the node right after the boudary to be checked\r
1022                                                 // first.\r
1023                                                 sibling = container.getChild( offset );\r
1024 \r
1025                                                 if ( !sibling )\r
1026                                                         enlargeable = container;\r
1027                                         }\r
1028 \r
1029                                         while ( enlargeable || sibling )\r
1030                                         {\r
1031                                                 if ( enlargeable && !sibling )\r
1032                                                 {\r
1033                                                         if ( !commonReached && enlargeable.equals( commonAncestor ) )\r
1034                                                                 commonReached = true;\r
1035 \r
1036                                                         if ( !body.contains( enlargeable ) )\r
1037                                                                 break;\r
1038 \r
1039                                                         if ( !needsWhiteSpace || enlargeable.getComputedStyle( 'display' ) != 'inline' )\r
1040                                                         {\r
1041                                                                 needsWhiteSpace = false;\r
1042 \r
1043                                                                 if ( commonReached )\r
1044                                                                         endTop = enlargeable;\r
1045                                                                 else if ( enlargeable )\r
1046                                                                         this.setEndAfter( enlargeable );\r
1047                                                         }\r
1048 \r
1049                                                         sibling = enlargeable.getNext();\r
1050                                                 }\r
1051 \r
1052                                                 while ( sibling )\r
1053                                                 {\r
1054                                                         isWhiteSpace = false;\r
1055 \r
1056                                                         if ( sibling.type == CKEDITOR.NODE_TEXT )\r
1057                                                         {\r
1058                                                                 siblingText = sibling.getText();\r
1059 \r
1060                                                                 if ( /[^\s\ufeff]/.test( siblingText ) )\r
1061                                                                         sibling = null;\r
1062 \r
1063                                                                 isWhiteSpace = /^[\s\ufeff]/.test( siblingText );\r
1064                                                         }\r
1065                                                         else\r
1066                                                         {\r
1067                                                                 // If this is a visible element.\r
1068                                                                 // We need to check for the bookmark attribute because IE insists on\r
1069                                                                 // rendering the display:none nodes we use for bookmarks. (#3363)\r
1070                                                                 if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_fck_bookmark' ) )\r
1071                                                                 {\r
1072                                                                         // We'll accept it only if we need\r
1073                                                                         // whitespace, and this is an inline\r
1074                                                                         // element with whitespace only.\r
1075                                                                         if ( needsWhiteSpace && CKEDITOR.dtd.$removeEmpty[ sibling.getName() ] )\r
1076                                                                         {\r
1077                                                                                 // It must contains spaces and inline elements only.\r
1078 \r
1079                                                                                 siblingText = sibling.getText();\r
1080 \r
1081                                                                                 if ( !(/[^\s\ufeff]/).test( siblingText ) )\r
1082                                                                                         sibling = null;\r
1083                                                                                 else\r
1084                                                                                 {\r
1085                                                                                         allChildren = sibling.$.all || sibling.$.getElementsByTagName( '*' );\r
1086                                                                                         for ( i = 0 ; child = allChildren[ i++ ] ; )\r
1087                                                                                         {\r
1088                                                                                                 if ( !CKEDITOR.dtd.$removeEmpty[ child.nodeName.toLowerCase() ] )\r
1089                                                                                                 {\r
1090                                                                                                         sibling = null;\r
1091                                                                                                         break;\r
1092                                                                                                 }\r
1093                                                                                         }\r
1094                                                                                 }\r
1095 \r
1096                                                                                 if ( sibling )\r
1097                                                                                         isWhiteSpace = !!siblingText.length;\r
1098                                                                         }\r
1099                                                                         else\r
1100                                                                                 sibling = null;\r
1101                                                                 }\r
1102                                                         }\r
1103 \r
1104                                                         if ( isWhiteSpace )\r
1105                                                         {\r
1106                                                                 if ( needsWhiteSpace )\r
1107                                                                 {\r
1108                                                                         if ( commonReached )\r
1109                                                                                 endTop = enlargeable;\r
1110                                                                         else\r
1111                                                                                 this.setEndAfter( enlargeable );\r
1112                                                                 }\r
1113                                                         }\r
1114 \r
1115                                                         if ( sibling )\r
1116                                                         {\r
1117                                                                 next = sibling.getNext();\r
1118 \r
1119                                                                 if ( !enlargeable && !next )\r
1120                                                                 {\r
1121                                                                         enlargeable = sibling;\r
1122                                                                         sibling = null;\r
1123                                                                         break;\r
1124                                                                 }\r
1125 \r
1126                                                                 sibling = next;\r
1127                                                         }\r
1128                                                         else\r
1129                                                         {\r
1130                                                                 // If sibling has been set to null, then we\r
1131                                                                 // need to stop enlarging.\r
1132                                                                 enlargeable = null;\r
1133                                                         }\r
1134                                                 }\r
1135 \r
1136                                                 if ( enlargeable )\r
1137                                                         enlargeable = enlargeable.getParent();\r
1138                                         }\r
1139 \r
1140                                         // If the common ancestor can be enlarged by both boundaries, then include it also.\r
1141                                         if ( startTop && endTop )\r
1142                                         {\r
1143                                                 commonAncestor = startTop.contains( endTop ) ? endTop : startTop;\r
1144 \r
1145                                                 this.setStartBefore( commonAncestor );\r
1146                                                 this.setEndAfter( commonAncestor );\r
1147                                         }\r
1148                                         break;\r
1149 \r
1150                                 case CKEDITOR.ENLARGE_BLOCK_CONTENTS:\r
1151                                 case CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS:\r
1152 \r
1153                                         // Enlarging the start boundary.\r
1154                                         var walkerRange = new CKEDITOR.dom.range( this.document );\r
1155 \r
1156                                         body = this.document.getBody();\r
1157 \r
1158                                         walkerRange.setStartAt( body, CKEDITOR.POSITION_AFTER_START );\r
1159                                         walkerRange.setEnd( this.startContainer, this.startOffset );\r
1160 \r
1161                                         var walker = new CKEDITOR.dom.walker( walkerRange ),\r
1162                                             blockBoundary,  // The node on which the enlarging should stop.\r
1163                                                 tailBr, //\r
1164                                             defaultGuard = CKEDITOR.dom.walker.blockBoundary(\r
1165                                                                 ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ? { br : 1 } : null ),\r
1166                                                 // Record the encountered 'blockBoundary' for later use.\r
1167                                                 boundaryGuard = function( node )\r
1168                                                 {\r
1169                                                         var retval = defaultGuard( node );\r
1170                                                         if ( !retval )\r
1171                                                                 blockBoundary = node;\r
1172                                                         return retval;\r
1173                                                 },\r
1174                                                 // Record the encounted 'tailBr' for later use.\r
1175                                                 tailBrGuard = function( node )\r
1176                                                 {\r
1177                                                         var retval = boundaryGuard( node );\r
1178                                                         if ( !retval && node.is && node.is( 'br' ) )\r
1179                                                                 tailBr = node;\r
1180                                                         return retval;\r
1181                                                 };\r
1182 \r
1183                                         walker.guard = boundaryGuard;\r
1184 \r
1185                                         enlargeable = walker.lastBackward();\r
1186 \r
1187                                         // It's the body which stop the enlarging if no block boundary found.\r
1188                                         blockBoundary = blockBoundary || body;\r
1189 \r
1190                                         // Start the range at different position by comparing\r
1191                                         // the document position of it with 'enlargeable' node.\r
1192                                         this.setStartAt(\r
1193                                                         blockBoundary,\r
1194                                                         !blockBoundary.is( 'br' ) &&\r
1195                                                         ( !enlargeable && this.checkStartOfBlock()\r
1196                                                           || enlargeable && blockBoundary.contains( enlargeable ) ) ?\r
1197                                                                 CKEDITOR.POSITION_AFTER_START :\r
1198                                                                 CKEDITOR.POSITION_AFTER_END );\r
1199 \r
1200                                         // Enlarging the end boundary.\r
1201                                         walkerRange = this.clone();\r
1202                                         walkerRange.collapse();\r
1203                                         walkerRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );\r
1204                                         walker = new CKEDITOR.dom.walker( walkerRange );\r
1205 \r
1206                                         // tailBrGuard only used for on range end.\r
1207                                         walker.guard = ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ?\r
1208                                                 tailBrGuard : boundaryGuard;\r
1209                                         blockBoundary = null;\r
1210                                         // End the range right before the block boundary node.\r
1211 \r
1212                                         enlargeable = walker.lastForward();\r
1213 \r
1214                                         // It's the body which stop the enlarging if no block boundary found.\r
1215                                         blockBoundary = blockBoundary || body;\r
1216 \r
1217                                         // Start the range at different position by comparing\r
1218                                         // the document position of it with 'enlargeable' node.\r
1219                                         this.setEndAt(\r
1220                                                         blockBoundary,\r
1221                                                         ( !enlargeable && this.checkEndOfBlock()\r
1222                                                           || enlargeable && blockBoundary.contains( enlargeable ) ) ?\r
1223                                                                 CKEDITOR.POSITION_BEFORE_END :\r
1224                                                                 CKEDITOR.POSITION_BEFORE_START );\r
1225                                         // We must include the <br> at the end of range if there's\r
1226                                         // one and we're expanding list item contents\r
1227                                         if ( tailBr )\r
1228                                                 this.setEndAfter( tailBr );\r
1229                         }\r
1230                 },\r
1231 \r
1232                 /**\r
1233                  * Inserts a node at the start of the range. The range will be expanded\r
1234                  * the contain the node.\r
1235                  */\r
1236                 insertNode : function( node )\r
1237                 {\r
1238                         this.optimizeBookmark();\r
1239                         this.trim( false, true );\r
1240 \r
1241                         var startContainer = this.startContainer;\r
1242                         var startOffset = this.startOffset;\r
1243 \r
1244                         var nextNode = startContainer.getChild( startOffset );\r
1245 \r
1246                         if ( nextNode )\r
1247                                 node.insertBefore( nextNode );\r
1248                         else\r
1249                                 startContainer.append( node );\r
1250 \r
1251                         // Check if we need to update the end boundary.\r
1252                         if ( node.getParent().equals( this.endContainer ) )\r
1253                                 this.endOffset++;\r
1254 \r
1255                         // Expand the range to embrace the new node.\r
1256                         this.setStartBefore( node );\r
1257                 },\r
1258 \r
1259                 moveToPosition : function( node, position )\r
1260                 {\r
1261                         this.setStartAt( node, position );\r
1262                         this.collapse( true );\r
1263                 },\r
1264 \r
1265                 selectNodeContents : function( node )\r
1266                 {\r
1267                         this.setStart( node, 0 );\r
1268                         this.setEnd( node, node.type == CKEDITOR.NODE_TEXT ? node.getLength() : node.getChildCount() );\r
1269                 },\r
1270 \r
1271                 /**\r
1272                  * Sets the start position of a Range.\r
1273                  * @param {CKEDITOR.dom.node} startNode The node to start the range.\r
1274                  * @param {Number} startOffset An integer greater than or equal to zero\r
1275                  *              representing the offset for the start of the range from the start\r
1276                  *              of startNode.\r
1277                  */\r
1278                 setStart : function( startNode, startOffset )\r
1279                 {\r
1280                         // W3C requires a check for the new position. If it is after the end\r
1281                         // boundary, the range should be collapsed to the new start. It seams\r
1282                         // we will not need this check for our use of this class so we can\r
1283                         // ignore it for now.\r
1284 \r
1285                         this.startContainer     = startNode;\r
1286                         this.startOffset        = startOffset;\r
1287 \r
1288                         if ( !this.endContainer )\r
1289                         {\r
1290                                 this.endContainer       = startNode;\r
1291                                 this.endOffset          = startOffset;\r
1292                         }\r
1293 \r
1294                         updateCollapsed( this );\r
1295                 },\r
1296 \r
1297                 /**\r
1298                  * Sets the end position of a Range.\r
1299                  * @param {CKEDITOR.dom.node} endNode The node to end the range.\r
1300                  * @param {Number} endOffset An integer greater than or equal to zero\r
1301                  *              representing the offset for the end of the range from the start\r
1302                  *              of endNode.\r
1303                  */\r
1304                 setEnd : function( endNode, endOffset )\r
1305                 {\r
1306                         // W3C requires a check for the new position. If it is before the start\r
1307                         // boundary, the range should be collapsed to the new end. It seams we\r
1308                         // will not need this check for our use of this class so we can ignore\r
1309                         // it for now.\r
1310 \r
1311                         this.endContainer       = endNode;\r
1312                         this.endOffset          = endOffset;\r
1313 \r
1314                         if ( !this.startContainer )\r
1315                         {\r
1316                                 this.startContainer     = endNode;\r
1317                                 this.startOffset        = endOffset;\r
1318                         }\r
1319 \r
1320                         updateCollapsed( this );\r
1321                 },\r
1322 \r
1323                 setStartAfter : function( node )\r
1324                 {\r
1325                         this.setStart( node.getParent(), node.getIndex() + 1 );\r
1326                 },\r
1327 \r
1328                 setStartBefore : function( node )\r
1329                 {\r
1330                         this.setStart( node.getParent(), node.getIndex() );\r
1331                 },\r
1332 \r
1333                 setEndAfter : function( node )\r
1334                 {\r
1335                         this.setEnd( node.getParent(), node.getIndex() + 1 );\r
1336                 },\r
1337 \r
1338                 setEndBefore : function( node )\r
1339                 {\r
1340                         this.setEnd( node.getParent(), node.getIndex() );\r
1341                 },\r
1342 \r
1343                 setStartAt : function( node, position )\r
1344                 {\r
1345                         switch( position )\r
1346                         {\r
1347                                 case CKEDITOR.POSITION_AFTER_START :\r
1348                                         this.setStart( node, 0 );\r
1349                                         break;\r
1350 \r
1351                                 case CKEDITOR.POSITION_BEFORE_END :\r
1352                                         if ( node.type == CKEDITOR.NODE_TEXT )\r
1353                                                 this.setStart( node, node.getLength() );\r
1354                                         else\r
1355                                                 this.setStart( node, node.getChildCount() );\r
1356                                         break;\r
1357 \r
1358                                 case CKEDITOR.POSITION_BEFORE_START :\r
1359                                         this.setStartBefore( node );\r
1360                                         break;\r
1361 \r
1362                                 case CKEDITOR.POSITION_AFTER_END :\r
1363                                         this.setStartAfter( node );\r
1364                         }\r
1365 \r
1366                         updateCollapsed( this );\r
1367                 },\r
1368 \r
1369                 setEndAt : function( node, position )\r
1370                 {\r
1371                         switch( position )\r
1372                         {\r
1373                                 case CKEDITOR.POSITION_AFTER_START :\r
1374                                         this.setEnd( node, 0 );\r
1375                                         break;\r
1376 \r
1377                                 case CKEDITOR.POSITION_BEFORE_END :\r
1378                                         if ( node.type == CKEDITOR.NODE_TEXT )\r
1379                                                 this.setEnd( node, node.getLength() );\r
1380                                         else\r
1381                                                 this.setEnd( node, node.getChildCount() );\r
1382                                         break;\r
1383 \r
1384                                 case CKEDITOR.POSITION_BEFORE_START :\r
1385                                         this.setEndBefore( node );\r
1386                                         break;\r
1387 \r
1388                                 case CKEDITOR.POSITION_AFTER_END :\r
1389                                         this.setEndAfter( node );\r
1390                         }\r
1391 \r
1392                         updateCollapsed( this );\r
1393                 },\r
1394 \r
1395                 fixBlock : function( isStart, blockTag )\r
1396                 {\r
1397                         var bookmark = this.createBookmark(),\r
1398                                 fixedBlock = this.document.createElement( blockTag );\r
1399 \r
1400                         this.collapse( isStart );\r
1401 \r
1402                         this.enlarge( CKEDITOR.ENLARGE_BLOCK_CONTENTS );\r
1403 \r
1404                         this.extractContents().appendTo( fixedBlock );\r
1405                         fixedBlock.trim();\r
1406 \r
1407                         if ( !CKEDITOR.env.ie )\r
1408                                 fixedBlock.appendBogus();\r
1409 \r
1410                         this.insertNode( fixedBlock );\r
1411 \r
1412                         this.moveToBookmark( bookmark );\r
1413 \r
1414                         return fixedBlock;\r
1415                 },\r
1416 \r
1417                 splitBlock : function( blockTag )\r
1418                 {\r
1419                         var startPath   = new CKEDITOR.dom.elementPath( this.startContainer ),\r
1420                                 endPath         = new CKEDITOR.dom.elementPath( this.endContainer );\r
1421 \r
1422                         var startBlockLimit     = startPath.blockLimit,\r
1423                                 endBlockLimit   = endPath.blockLimit;\r
1424 \r
1425                         var startBlock  = startPath.block,\r
1426                                 endBlock        = endPath.block;\r
1427 \r
1428                         var elementPath = null;\r
1429                         // Do nothing if the boundaries are in different block limits.\r
1430                         if ( !startBlockLimit.equals( endBlockLimit ) )\r
1431                                 return null;\r
1432 \r
1433                         // Get or fix current blocks.\r
1434                         if ( blockTag != 'br' )\r
1435                         {\r
1436                                 if ( !startBlock )\r
1437                                 {\r
1438                                         startBlock = this.fixBlock( true, blockTag );\r
1439                                         endBlock = new CKEDITOR.dom.elementPath( this.endContainer ).block;\r
1440                                 }\r
1441 \r
1442                                 if ( !endBlock )\r
1443                                         endBlock = this.fixBlock( false, blockTag );\r
1444                         }\r
1445 \r
1446                         // Get the range position.\r
1447                         var isStartOfBlock = startBlock && this.checkStartOfBlock(),\r
1448                                 isEndOfBlock = endBlock && this.checkEndOfBlock();\r
1449 \r
1450                         // Delete the current contents.\r
1451                         // TODO: Why is 2.x doing CheckIsEmpty()?\r
1452                         this.deleteContents();\r
1453 \r
1454                         if ( startBlock && startBlock.equals( endBlock ) )\r
1455                         {\r
1456                                 if ( isEndOfBlock )\r
1457                                 {\r
1458                                         elementPath = new CKEDITOR.dom.elementPath( this.startContainer );\r
1459                                         this.moveToPosition( endBlock, CKEDITOR.POSITION_AFTER_END );\r
1460                                         endBlock = null;\r
1461                                 }\r
1462                                 else if ( isStartOfBlock )\r
1463                                 {\r
1464                                         elementPath = new CKEDITOR.dom.elementPath( this.startContainer );\r
1465                                         this.moveToPosition( startBlock, CKEDITOR.POSITION_BEFORE_START );\r
1466                                         startBlock = null;\r
1467                                 }\r
1468                                 else\r
1469                                 {\r
1470                                         endBlock = this.splitElement( startBlock );\r
1471 \r
1472                                         // In Gecko, the last child node must be a bogus <br>.\r
1473                                         // Note: bogus <br> added under <ul> or <ol> would cause\r
1474                                         // lists to be incorrectly rendered.\r
1475                                         if ( !CKEDITOR.env.ie && !startBlock.is( 'ul', 'ol') )\r
1476                                                 startBlock.appendBogus() ;\r
1477                                 }\r
1478                         }\r
1479 \r
1480                         return {\r
1481                                 previousBlock : startBlock,\r
1482                                 nextBlock : endBlock,\r
1483                                 wasStartOfBlock : isStartOfBlock,\r
1484                                 wasEndOfBlock : isEndOfBlock,\r
1485                                 elementPath : elementPath\r
1486                         };\r
1487                 },\r
1488 \r
1489                 /**\r
1490                  * Branch the specified element from the collapsed range position and\r
1491                  * place the caret between the two result branches.\r
1492                  * Note: The range must be collapsed and been enclosed by this element.\r
1493                  * @param {CKEDITOR.dom.element} element\r
1494                  * @return {CKEDITOR.dom.element} Root element of the new branch after the split.\r
1495                  */\r
1496                 splitElement : function( toSplit )\r
1497                 {\r
1498                         if ( !this.collapsed )\r
1499                                 return null;\r
1500 \r
1501                         // Extract the contents of the block from the selection point to the end\r
1502                         // of its contents.\r
1503                         this.setEndAt( toSplit, CKEDITOR.POSITION_BEFORE_END );\r
1504                         var documentFragment = this.extractContents();\r
1505 \r
1506                         // Duplicate the element after it.\r
1507                         var clone = toSplit.clone( false );\r
1508 \r
1509                         // Place the extracted contents into the duplicated element.\r
1510                         documentFragment.appendTo( clone );\r
1511                         clone.insertAfter( toSplit );\r
1512                         this.moveToPosition( toSplit, CKEDITOR.POSITION_AFTER_END );\r
1513                         return clone;\r
1514                 },\r
1515 \r
1516                 /**\r
1517                  * Check whether current range is on the inner edge of the specified element.\r
1518                  * @param {Number} checkType ( CKEDITOR.START | CKEDITOR.END ) The checking side.\r
1519                  * @param {CKEDITOR.dom.element} element The target element to check.\r
1520                  */\r
1521                 checkBoundaryOfElement : function( element, checkType )\r
1522                 {\r
1523                         var walkerRange = this.clone();\r
1524                         // Expand the range to element boundary.\r
1525                         walkerRange[ checkType == CKEDITOR.START ?\r
1526                          'setStartAt' : 'setEndAt' ]\r
1527                          ( element, checkType == CKEDITOR.START ?\r
1528                            CKEDITOR.POSITION_AFTER_START\r
1529                            : CKEDITOR.POSITION_BEFORE_END );\r
1530 \r
1531                         var walker = new CKEDITOR.dom.walker( walkerRange ),\r
1532                          retval = false;\r
1533                         walker.evaluator = elementBoundaryEval;\r
1534                         return walker[ checkType == CKEDITOR.START ?\r
1535                                 'checkBackward' : 'checkForward' ]();\r
1536                 },\r
1537                 // Calls to this function may produce changes to the DOM. The range may\r
1538                 // be updated to reflect such changes.\r
1539                 checkStartOfBlock : function()\r
1540                 {\r
1541                         var startContainer = this.startContainer,\r
1542                                 startOffset = this.startOffset;\r
1543 \r
1544                         // If the starting node is a text node, and non-empty before the offset,\r
1545                         // then we're surely not at the start of block.\r
1546                         if ( startOffset && startContainer.type == CKEDITOR.NODE_TEXT )\r
1547                         {\r
1548                                 var textBefore = CKEDITOR.tools.ltrim( startContainer.substring( 0, startOffset ) );\r
1549                                 if ( textBefore.length )\r
1550                                         return false;\r
1551                         }\r
1552 \r
1553                         // Antecipate the trim() call here, so the walker will not make\r
1554                         // changes to the DOM, which would not get reflected into this\r
1555                         // range otherwise.\r
1556                         this.trim();\r
1557 \r
1558                         // We need to grab the block element holding the start boundary, so\r
1559                         // let's use an element path for it.\r
1560                         var path = new CKEDITOR.dom.elementPath( this.startContainer );\r
1561 \r
1562                         // Creates a range starting at the block start until the range start.\r
1563                         var walkerRange = this.clone();\r
1564                         walkerRange.collapse( true );\r
1565                         walkerRange.setStartAt( path.block || path.blockLimit, CKEDITOR.POSITION_AFTER_START );\r
1566 \r
1567                         var walker = new CKEDITOR.dom.walker( walkerRange );\r
1568                         walker.evaluator = getCheckStartEndBlockEvalFunction( true );\r
1569 \r
1570                         return walker.checkBackward();\r
1571                 },\r
1572 \r
1573                 checkEndOfBlock : function()\r
1574                 {\r
1575                         var endContainer = this.endContainer,\r
1576                                 endOffset = this.endOffset;\r
1577 \r
1578                         // If the ending node is a text node, and non-empty after the offset,\r
1579                         // then we're surely not at the end of block.\r
1580                         if ( endContainer.type == CKEDITOR.NODE_TEXT )\r
1581                         {\r
1582                                 var textAfter = CKEDITOR.tools.rtrim( endContainer.substring( endOffset ) );\r
1583                                 if ( textAfter.length )\r
1584                                         return false;\r
1585                         }\r
1586 \r
1587                         // Antecipate the trim() call here, so the walker will not make\r
1588                         // changes to the DOM, which would not get reflected into this\r
1589                         // range otherwise.\r
1590                         this.trim();\r
1591 \r
1592                         // We need to grab the block element holding the start boundary, so\r
1593                         // let's use an element path for it.\r
1594                         var path = new CKEDITOR.dom.elementPath( this.endContainer );\r
1595 \r
1596                         // Creates a range starting at the block start until the range start.\r
1597                         var walkerRange = this.clone();\r
1598                         walkerRange.collapse( false );\r
1599                         walkerRange.setEndAt( path.block || path.blockLimit, CKEDITOR.POSITION_BEFORE_END );\r
1600 \r
1601                         var walker = new CKEDITOR.dom.walker( walkerRange );\r
1602                         walker.evaluator = getCheckStartEndBlockEvalFunction( false );\r
1603 \r
1604                         return walker.checkForward();\r
1605                 },\r
1606 \r
1607                 /**\r
1608                  * Moves the range boundaries to the first/end editing point inside an\r
1609                  * element. For example, in an element tree like\r
1610                  * "&lt;p&gt;&lt;b&gt;&lt;i&gt;&lt;/i&gt;&lt;/b&gt; Text&lt;/p&gt;", the start editing point is\r
1611                  * "&lt;p&gt;&lt;b&gt;&lt;i&gt;^&lt;/i&gt;&lt;/b&gt; Text&lt;/p&gt;" (inside &lt;i&gt;).\r
1612                  * @param {CKEDITOR.dom.element} el The element into which look for the\r
1613                  *              editing spot.\r
1614                  * @param {Boolean} isMoveToEnd Whether move to the end editable position.\r
1615                  */\r
1616                 moveToElementEditablePosition : function( el, isMoveToEnd )\r
1617                 {\r
1618                         var isEditable;\r
1619 \r
1620                         while ( el && el.type == CKEDITOR.NODE_ELEMENT )\r
1621                         {\r
1622                                 isEditable = el.isEditable();\r
1623 \r
1624                                 // If an editable element is found, move inside it.\r
1625                                 if ( isEditable )\r
1626                                         this.moveToPosition( el, isMoveToEnd ?\r
1627                                                                  CKEDITOR.POSITION_BEFORE_END :\r
1628                                                                  CKEDITOR.POSITION_AFTER_START );\r
1629                                 // Stop immediately if we've found a non editable inline element (e.g <img>).\r
1630                                 else if ( CKEDITOR.dtd.$inline[ el.getName() ] )\r
1631                                 {\r
1632                                         this.moveToPosition( el, isMoveToEnd ?\r
1633                                                                  CKEDITOR.POSITION_AFTER_END :\r
1634                                                                  CKEDITOR.POSITION_BEFORE_START );\r
1635                                         return true;\r
1636                                 }\r
1637 \r
1638                                 // Non-editable non-inline elements are to be bypassed, getting the next one.\r
1639                                 if ( CKEDITOR.dtd.$empty[ el.getName() ] )\r
1640                                         el = el[ isMoveToEnd ? 'getPrevious' : 'getNext' ]( nonWhitespaceOrBookmarkEval );\r
1641                                 else\r
1642                                         el = el[ isMoveToEnd ? 'getLast' : 'getFirst' ]( nonWhitespaceOrBookmarkEval );\r
1643 \r
1644                                 // Stop immediately if we've found a text node.\r
1645                                 if ( el && el.type == CKEDITOR.NODE_TEXT )\r
1646                                 {\r
1647                                         this.moveToPosition( el, isMoveToEnd ?\r
1648                                                                  CKEDITOR.POSITION_AFTER_END :\r
1649                                                                  CKEDITOR.POSITION_BEFORE_START );\r
1650                                         return true;\r
1651                                 }\r
1652                         }\r
1653 \r
1654                         return isEditable;\r
1655                 },\r
1656 \r
1657                 /**\r
1658                  *@see {CKEDITOR.dom.range.moveToElementEditablePosition}\r
1659                  */\r
1660                 moveToElementEditStart : function( target )\r
1661                 {\r
1662                         return this.moveToElementEditablePosition( target );\r
1663                 },\r
1664 \r
1665                 /**\r
1666                  *@see {CKEDITOR.dom.range.moveToElementEditablePosition}\r
1667                  */\r
1668                 moveToElementEditEnd : function( target )\r
1669                 {\r
1670                         return this.moveToElementEditablePosition( target, true );\r
1671                 },\r
1672 \r
1673                 /**\r
1674                  * Get the single node enclosed within the range if there's one.\r
1675                  */\r
1676                 getEnclosedNode : function()\r
1677                 {\r
1678                         var walkerRange = this.clone(),\r
1679                                 walker = new CKEDITOR.dom.walker( walkerRange ),\r
1680                                 isNotBookmarks = CKEDITOR.dom.walker.bookmark( true ),\r
1681                                 isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),\r
1682                                 evaluator = function( node )\r
1683                                 {\r
1684                                         return isNotWhitespaces( node ) && isNotBookmarks( node );\r
1685                                 };\r
1686                         walkerRange.evaluator = evaluator;\r
1687                         var node = walker.next();\r
1688                         walker.reset();\r
1689                         return node && node.equals( walker.previous() ) ? node : null;\r
1690                 },\r
1691 \r
1692                 getTouchedStartNode : function()\r
1693                 {\r
1694                         var container = this.startContainer ;\r
1695 \r
1696                         if ( this.collapsed || container.type != CKEDITOR.NODE_ELEMENT )\r
1697                                 return container ;\r
1698 \r
1699                         return container.getChild( this.startOffset ) || container ;\r
1700                 },\r
1701 \r
1702                 getTouchedEndNode : function()\r
1703                 {\r
1704                         var container = this.endContainer ;\r
1705 \r
1706                         if ( this.collapsed || container.type != CKEDITOR.NODE_ELEMENT )\r
1707                                 return container ;\r
1708 \r
1709                         return container.getChild( this.endOffset - 1 ) || container ;\r
1710                 }\r
1711         };\r
1712 })();\r
1713 \r
1714 CKEDITOR.POSITION_AFTER_START   = 1;    // <element>^contents</element>         "^text"\r
1715 CKEDITOR.POSITION_BEFORE_END    = 2;    // <element>contents^</element>         "text^"\r
1716 CKEDITOR.POSITION_BEFORE_START  = 3;    // ^<element>contents</element>         ^"text"\r
1717 CKEDITOR.POSITION_AFTER_END             = 4;    // <element>contents</element>^         "text"\r
1718 \r
1719 CKEDITOR.ENLARGE_ELEMENT = 1;\r
1720 CKEDITOR.ENLARGE_BLOCK_CONTENTS = 2;\r
1721 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS = 3;\r
1722 \r
1723 /**\r
1724  * Check boundary types.\r
1725  * @see CKEDITOR.dom.range::checkBoundaryOfElement\r
1726  */\r
1727 CKEDITOR.START = 1;\r
1728 CKEDITOR.END = 2;\r
1729 CKEDITOR.STARTEND = 3;\r