JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / selection / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         // #### checkSelectionChange : START\r
9 \r
10         // The selection change check basically saves the element parent tree of\r
11         // the current node and check it on successive requests. If there is any\r
12         // change on the tree, then the selectionChange event gets fired.\r
13         function checkSelectionChange()\r
14         {\r
15                 try\r
16                 {\r
17                         // In IE, the "selectionchange" event may still get thrown when\r
18                         // releasing the WYSIWYG mode, so we need to check it first.\r
19                         var sel = this.getSelection();\r
20                         if ( !sel || !sel.document.getWindow().$ )\r
21                                 return;\r
22 \r
23                         var firstElement = sel.getStartElement();\r
24                         var currentPath = new CKEDITOR.dom.elementPath( firstElement );\r
25 \r
26                         if ( !currentPath.compare( this._.selectionPreviousPath ) )\r
27                         {\r
28                                 this._.selectionPreviousPath = currentPath;\r
29                                 this.fire( 'selectionChange', { selection : sel, path : currentPath, element : firstElement } );\r
30                         }\r
31                 }\r
32                 catch (e)\r
33                 {}\r
34         }\r
35 \r
36         var checkSelectionChangeTimer,\r
37                 checkSelectionChangeTimeoutPending;\r
38 \r
39         function checkSelectionChangeTimeout()\r
40         {\r
41                 // Firing the "OnSelectionChange" event on every key press started to\r
42                 // be too slow. This function guarantees that there will be at least\r
43                 // 200ms delay between selection checks.\r
44 \r
45                 checkSelectionChangeTimeoutPending = true;\r
46 \r
47                 if ( checkSelectionChangeTimer )\r
48                         return;\r
49 \r
50                 checkSelectionChangeTimeoutExec.call( this );\r
51 \r
52                 checkSelectionChangeTimer = CKEDITOR.tools.setTimeout( checkSelectionChangeTimeoutExec, 200, this );\r
53         }\r
54 \r
55         function checkSelectionChangeTimeoutExec()\r
56         {\r
57                 checkSelectionChangeTimer = null;\r
58 \r
59                 if ( checkSelectionChangeTimeoutPending )\r
60                 {\r
61                         // Call this with a timeout so the browser properly moves the\r
62                         // selection after the mouseup. It happened that the selection was\r
63                         // being moved after the mouseup when clicking inside selected text\r
64                         // with Firefox.\r
65                         CKEDITOR.tools.setTimeout( checkSelectionChange, 0, this );\r
66 \r
67                         checkSelectionChangeTimeoutPending = false;\r
68                 }\r
69         }\r
70 \r
71         // #### checkSelectionChange : END\r
72 \r
73         var selectAllCmd =\r
74         {\r
75                 modes : { wysiwyg : 1, source : 1 },\r
76                 exec : function( editor )\r
77                 {\r
78                         switch ( editor.mode )\r
79                         {\r
80                                 case 'wysiwyg' :\r
81                                         editor.document.$.execCommand( 'SelectAll', false, null );\r
82                                         break;\r
83                                 case 'source' :\r
84                                         // Select the contents of the textarea\r
85                                         var textarea = editor.textarea.$ ;\r
86                                         if ( CKEDITOR.env.ie )\r
87                                         {\r
88                                                 textarea.createTextRange().execCommand( 'SelectAll' ) ;\r
89                                         }\r
90                                         else\r
91                                         {\r
92                                                 textarea.selectionStart = 0 ;\r
93                                                 textarea.selectionEnd = textarea.value.length ;\r
94                                         }\r
95                                         textarea.focus() ;\r
96                         }\r
97                 },\r
98                 canUndo : false\r
99         };\r
100 \r
101         CKEDITOR.plugins.add( 'selection',\r
102         {\r
103                 init : function( editor )\r
104                 {\r
105                         editor.on( 'contentDom', function()\r
106                                 {\r
107                                         var doc = editor.document,\r
108                                                 body = doc.getBody(),\r
109                                                 html = doc.getDocumentElement();\r
110 \r
111                                         if ( CKEDITOR.env.ie )\r
112                                         {\r
113                                                 // Other browsers don't loose the selection if the\r
114                                                 // editor document loose the focus. In IE, we don't\r
115                                                 // have support for it, so we reproduce it here, other\r
116                                                 // than firing the selection change event.\r
117 \r
118                                                 var savedRange,\r
119                                                         saveEnabled,\r
120                                                         restoreEnabled = 1;\r
121 \r
122                                                 // "onfocusin" is fired before "onfocus". It makes it\r
123                                                 // possible to restore the selection before click\r
124                                                 // events get executed.\r
125                                                 body.on( 'focusin', function( evt )\r
126                                                         {\r
127                                                                 // If there are elements with layout they fire this event but\r
128                                                                 // it must be ignored to allow edit its contents #4682\r
129                                                                 if ( evt.data.$.srcElement.nodeName != 'BODY' )\r
130                                                                         return;\r
131 \r
132                                                                 // If we have saved a range, restore it at this\r
133                                                                 // point.\r
134                                                                 if ( savedRange )\r
135                                                                 {\r
136                                                                         // Range restored here might invalidate the DOM structure thus break up\r
137                                                                         // the locked selection, give it up. (#6083)\r
138                                                                         var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
139                                                                         if ( restoreEnabled && !lockedSelection )\r
140                                                                         {\r
141                                                                                 // Well not break because of this.\r
142                                                                                 try\r
143                                                                                 {\r
144                                                                                         savedRange.select();\r
145                                                                                 }\r
146                                                                                 catch (e)\r
147                                                                                 {}\r
148                                                                         }\r
149 \r
150                                                                         savedRange = null;\r
151                                                                 }\r
152                                                         });\r
153 \r
154                                                 body.on( 'focus', function()\r
155                                                         {\r
156                                                                 // Enable selections to be saved.\r
157                                                                 saveEnabled = 1;\r
158 \r
159                                                                 saveSelection();\r
160                                                         });\r
161 \r
162                                                 body.on( 'beforedeactivate', function( evt )\r
163                                                         {\r
164                                                                 // Ignore this event if it's caused by focus switch between\r
165                                                                 // internal editable control type elements, e.g. layouted paragraph. (#4682)\r
166                                                                 if ( evt.data.$.toElement )\r
167                                                                         return;\r
168 \r
169                                                                 // Disable selections from being saved.\r
170                                                                 saveEnabled = 0;\r
171                                                                 restoreEnabled = 1;\r
172                                                         });\r
173 \r
174                                                 // IE before version 8 will leave cursor blinking inside the document after\r
175                                                 // editor blurred unless we clean up the selection. (#4716)\r
176                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
177                                                 {\r
178                                                         editor.on( 'blur', function( evt )\r
179                                                         {\r
180                                                                 // Try/Catch to avoid errors if the editor is hidden. (#6375)\r
181                                                                 try\r
182                                                                 {\r
183                                                                         editor.document && editor.document.$.selection.empty();\r
184                                                                 }\r
185                                                                 catch (e) {}\r
186                                                         });\r
187                                                 }\r
188 \r
189                                                 // Listening on document element ensures that\r
190                                                 // scrollbar is included. (#5280)\r
191                                                 html.on( 'mousedown', function()\r
192                                                 {\r
193                                                         // Lock restore selection now, as we have\r
194                                                         // a followed 'click' event which introduce\r
195                                                         // new selection. (#5735)\r
196                                                         restoreEnabled = 0;\r
197                                                 });\r
198 \r
199                                                 html.on( 'mouseup', function()\r
200                                                 {\r
201                                                         restoreEnabled = 1;\r
202                                                 });\r
203 \r
204                                                 // In IE6/7 the blinking cursor appears, but contents are\r
205                                                 // not editable. (#5634)\r
206                                                 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )\r
207                                                 {\r
208                                                         // The 'click' event is not fired when clicking the\r
209                                                         // scrollbars, so we can use it to check whether\r
210                                                         // the empty space following <body> has been clicked.\r
211                                                         html.on( 'click', function( evt )\r
212                                                         {\r
213                                                                 if ( evt.data.getTarget().getName() == 'html' )\r
214                                                                         editor.getSelection().getRanges()[ 0 ].select();\r
215                                                         });\r
216                                                 }\r
217 \r
218                                                 var scroll;\r
219                                                 // IE fires the "selectionchange" event when clicking\r
220                                                 // inside a selection. We don't want to capture that.\r
221                                                 body.on( 'mousedown', function( evt )\r
222                                                 {\r
223                                                         // IE scrolls document to top on right mousedown\r
224                                                         // when editor has no focus, remember this scroll\r
225                                                         // position and revert it before context menu opens. (#5778)\r
226                                                         if ( evt.data.$.button == 2 )\r
227                                                         {\r
228                                                                 var sel = editor.document.$.selection;\r
229                                                                 if ( sel.type == 'None' )\r
230                                                                         scroll = editor.window.getScrollPosition();\r
231                                                         }\r
232                                                         disableSave();\r
233                                                 });\r
234 \r
235                                                 body.on( 'mouseup',\r
236                                                         function( evt )\r
237                                                         {\r
238                                                                 // Restore recorded scroll position when needed on right mouseup.\r
239                                                                 if ( evt.data.$.button == 2 && scroll )\r
240                                                                 {\r
241                                                                         editor.document.$.documentElement.scrollLeft = scroll.x;\r
242                                                                         editor.document.$.documentElement.scrollTop = scroll.y;\r
243                                                                 }\r
244                                                                 scroll = null;\r
245 \r
246                                                                 saveEnabled = 1;\r
247                                                                 setTimeout( function()\r
248                                                                         {\r
249                                                                                 saveSelection( true );\r
250                                                                         },\r
251                                                                         0 );\r
252                                                         });\r
253 \r
254                                                 body.on( 'keydown', disableSave );\r
255                                                 body.on( 'keyup',\r
256                                                         function()\r
257                                                         {\r
258                                                                 saveEnabled = 1;\r
259                                                                 saveSelection();\r
260                                                         });\r
261 \r
262 \r
263                                                 // IE is the only to provide the "selectionchange"\r
264                                                 // event.\r
265                                                 doc.on( 'selectionchange', saveSelection );\r
266 \r
267                                                 function disableSave()\r
268                                                 {\r
269                                                         saveEnabled = 0;\r
270                                                 }\r
271 \r
272                                                 function saveSelection( testIt )\r
273                                                 {\r
274                                                         if ( saveEnabled )\r
275                                                         {\r
276                                                                 var doc = editor.document,\r
277                                                                         sel = editor.getSelection(),\r
278                                                                         nativeSel = sel && sel.getNative();\r
279 \r
280                                                                 // There is a very specific case, when clicking\r
281                                                                 // inside a text selection. In that case, the\r
282                                                                 // selection collapses at the clicking point,\r
283                                                                 // but the selection object remains in an\r
284                                                                 // unknown state, making createRange return a\r
285                                                                 // range at the very start of the document. In\r
286                                                                 // such situation we have to test the range, to\r
287                                                                 // be sure it's valid.\r
288                                                                 if ( testIt && nativeSel && nativeSel.type == 'None' )\r
289                                                                 {\r
290                                                                         // The "InsertImage" command can be used to\r
291                                                                         // test whether the selection is good or not.\r
292                                                                         // If not, it's enough to give some time to\r
293                                                                         // IE to put things in order for us.\r
294                                                                         if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )\r
295                                                                         {\r
296                                                                                 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );\r
297                                                                                 return;\r
298                                                                         }\r
299                                                                 }\r
300 \r
301                                                                 // Avoid saving selection from within text input. (#5747)\r
302                                                                 var parentTag;\r
303                                                                 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'\r
304                                                                         && ( parentTag = nativeSel.createRange() )\r
305                                                                         && ( parentTag = parentTag.parentElement() )\r
306                                                                         && ( parentTag = parentTag.nodeName )\r
307                                                                         && parentTag.toLowerCase() in { input: 1, textarea : 1 } )\r
308                                                                 {\r
309                                                                         return;\r
310                                                                 }\r
311 \r
312                                                                 savedRange = nativeSel && sel.getRanges()[ 0 ];\r
313 \r
314                                                                 checkSelectionChangeTimeout.call( editor );\r
315                                                         }\r
316                                                 }\r
317                                         }\r
318                                         else\r
319                                         {\r
320                                                 // In other browsers, we make the selection change\r
321                                                 // check based on other events, like clicks or keys\r
322                                                 // press.\r
323 \r
324                                                 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );\r
325                                                 doc.on( 'keyup', checkSelectionChangeTimeout, editor );\r
326                                         }\r
327                                 });\r
328 \r
329                         editor.addCommand( 'selectAll', selectAllCmd );\r
330                         editor.ui.addButton( 'SelectAll',\r
331                                 {\r
332                                         label : editor.lang.selectAll,\r
333                                         command : 'selectAll'\r
334                                 });\r
335 \r
336                         editor.selectionChange = checkSelectionChangeTimeout;\r
337                 }\r
338         });\r
339 \r
340         /**\r
341          * Gets the current selection from the editing area when in WYSIWYG mode.\r
342          * @returns {CKEDITOR.dom.selection} A selection object or null if not on\r
343          *              WYSIWYG mode or no selection is available.\r
344          * @example\r
345          * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;\r
346          * alert( selection.getType() );\r
347          */\r
348         CKEDITOR.editor.prototype.getSelection = function()\r
349         {\r
350                 return this.document && this.document.getSelection();\r
351         };\r
352 \r
353         CKEDITOR.editor.prototype.forceNextSelectionCheck = function()\r
354         {\r
355                 delete this._.selectionPreviousPath;\r
356         };\r
357 \r
358         /**\r
359          * Gets the current selection from the document.\r
360          * @returns {CKEDITOR.dom.selection} A selection object.\r
361          * @example\r
362          * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;\r
363          * alert( selection.getType() );\r
364          */\r
365         CKEDITOR.dom.document.prototype.getSelection = function()\r
366         {\r
367                 var sel = new CKEDITOR.dom.selection( this );\r
368                 return ( !sel || sel.isInvalid ) ? null : sel;\r
369         };\r
370 \r
371         /**\r
372          * No selection.\r
373          * @constant\r
374          * @example\r
375          * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )\r
376          *     alert( 'Nothing is selected' );\r
377          */\r
378         CKEDITOR.SELECTION_NONE         = 1;\r
379 \r
380         /**\r
381          * Text or collapsed selection.\r
382          * @constant\r
383          * @example\r
384          * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )\r
385          *     alert( 'Text is selected' );\r
386          */\r
387         CKEDITOR.SELECTION_TEXT         = 2;\r
388 \r
389         /**\r
390          * Element selection.\r
391          * @constant\r
392          * @example\r
393          * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )\r
394          *     alert( 'An element is selected' );\r
395          */\r
396         CKEDITOR.SELECTION_ELEMENT      = 3;\r
397 \r
398         /**\r
399          * Manipulates the selection in a DOM document.\r
400          * @constructor\r
401          * @example\r
402          */\r
403         CKEDITOR.dom.selection = function( document )\r
404         {\r
405                 var lockedSelection = document.getCustomData( 'cke_locked_selection' );\r
406 \r
407                 if ( lockedSelection )\r
408                         return lockedSelection;\r
409 \r
410                 this.document = document;\r
411                 this.isLocked = 0;\r
412                 this._ =\r
413                 {\r
414                         cache : {}\r
415                 };\r
416 \r
417                 /**\r
418                  * IE BUG: The selection's document may be a different document than the\r
419                  * editor document. Return null if that's the case.\r
420                  */\r
421                 if ( CKEDITOR.env.ie )\r
422                 {\r
423                         var range = this.getNative().createRange();\r
424                         if ( !range\r
425                                 || ( range.item && range.item(0).ownerDocument != this.document.$ )\r
426                                 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )\r
427                         {\r
428                                 this.isInvalid = true;\r
429                         }\r
430                 }\r
431 \r
432                 return this;\r
433         };\r
434 \r
435         var styleObjectElements =\r
436         {\r
437                 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,\r
438                 a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1\r
439         };\r
440 \r
441         CKEDITOR.dom.selection.prototype =\r
442         {\r
443                 /**\r
444                  * Gets the native selection object from the browser.\r
445                  * @function\r
446                  * @returns {Object} The native selection object.\r
447                  * @example\r
448                  * var selection = editor.getSelection().<b>getNative()</b>;\r
449                  */\r
450                 getNative :\r
451                         CKEDITOR.env.ie ?\r
452                                 function()\r
453                                 {\r
454                                         return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );\r
455                                 }\r
456                         :\r
457                                 function()\r
458                                 {\r
459                                         return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );\r
460                                 },\r
461 \r
462                 /**\r
463                  * Gets the type of the current selection. The following values are\r
464                  * available:\r
465                  * <ul>\r
466                  *              <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>\r
467                  *              <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or\r
468                  *                      collapsed selection.</li>\r
469                  *              <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element\r
470                  *                      selection.</li>\r
471                  * </ul>\r
472                  * @function\r
473                  * @returns {Number} One of the following constant values:\r
474                  *              {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or\r
475                  *              {@link CKEDITOR.SELECTION_ELEMENT}.\r
476                  * @example\r
477                  * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )\r
478                  *     alert( 'Text is selected' );\r
479                  */\r
480                 getType :\r
481                         CKEDITOR.env.ie ?\r
482                                 function()\r
483                                 {\r
484                                         var cache = this._.cache;\r
485                                         if ( cache.type )\r
486                                                 return cache.type;\r
487 \r
488                                         var type = CKEDITOR.SELECTION_NONE;\r
489 \r
490                                         try\r
491                                         {\r
492                                                 var sel = this.getNative(),\r
493                                                         ieType = sel.type;\r
494 \r
495                                                 if ( ieType == 'Text' )\r
496                                                         type = CKEDITOR.SELECTION_TEXT;\r
497 \r
498                                                 if ( ieType == 'Control' )\r
499                                                         type = CKEDITOR.SELECTION_ELEMENT;\r
500 \r
501                                                 // It is possible that we can still get a text range\r
502                                                 // object even when type == 'None' is returned by IE.\r
503                                                 // So we'd better check the object returned by\r
504                                                 // createRange() rather than by looking at the type.\r
505                                                 if ( sel.createRange().parentElement )\r
506                                                         type = CKEDITOR.SELECTION_TEXT;\r
507                                         }\r
508                                         catch(e) {}\r
509 \r
510                                         return ( cache.type = type );\r
511                                 }\r
512                         :\r
513                                 function()\r
514                                 {\r
515                                         var cache = this._.cache;\r
516                                         if ( cache.type )\r
517                                                 return cache.type;\r
518 \r
519                                         var type = CKEDITOR.SELECTION_TEXT;\r
520 \r
521                                         var sel = this.getNative();\r
522 \r
523                                         if ( !sel )\r
524                                                 type = CKEDITOR.SELECTION_NONE;\r
525                                         else if ( sel.rangeCount == 1 )\r
526                                         {\r
527                                                 // Check if the actual selection is a control (IMG,\r
528                                                 // TABLE, HR, etc...).\r
529 \r
530                                                 var range = sel.getRangeAt(0),\r
531                                                         startContainer = range.startContainer;\r
532 \r
533                                                 if ( startContainer == range.endContainer\r
534                                                         && startContainer.nodeType == 1\r
535                                                         && ( range.endOffset - range.startOffset ) == 1\r
536                                                         && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )\r
537                                                 {\r
538                                                         type = CKEDITOR.SELECTION_ELEMENT;\r
539                                                 }\r
540                                         }\r
541 \r
542                                         return ( cache.type = type );\r
543                                 },\r
544 \r
545                 /**\r
546                  * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.\r
547                  * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns\r
548                  * one range for each table cell when one or more table row is selected.\r
549                  * @return {Array}\r
550                  * @example\r
551                  * var ranges = selection.getRanges();\r
552                  * alert(ranges.length);\r
553                  */\r
554                 getRanges : (function()\r
555                 {\r
556                         var func = CKEDITOR.env.ie ?\r
557                                 ( function()\r
558                                 {\r
559                                         // Finds the container and offset for a specific boundary\r
560                                         // of an IE range.\r
561                                         var getBoundaryInformation = function( range, start )\r
562                                         {\r
563                                                 // Creates a collapsed range at the requested boundary.\r
564                                                 range = range.duplicate();\r
565                                                 range.collapse( start );\r
566 \r
567                                                 // Gets the element that encloses the range entirely.\r
568                                                 var parent = range.parentElement();\r
569                                                 var siblings = parent.childNodes;\r
570 \r
571                                                 var testRange;\r
572 \r
573                                                 for ( var i = 0 ; i < siblings.length ; i++ )\r
574                                                 {\r
575                                                         var child = siblings[ i ];\r
576                                                         if ( child.nodeType == 1 )\r
577                                                         {\r
578                                                                 testRange = range.duplicate();\r
579 \r
580                                                                 testRange.moveToElementText( child );\r
581 \r
582                                                                 var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),\r
583                                                                         comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );\r
584 \r
585                                                                 testRange.collapse();\r
586 \r
587                                                                 if ( comparisonStart > 0 )\r
588                                                                         break;\r
589                                                                 // When selection stay at the side of certain self-closing elements, e.g. BR,\r
590                                                                 // our comparison will never shows an equality. (#4824)\r
591                                                                 else if ( !comparisonStart\r
592                                                                         || comparisonEnd == 1 && comparisonStart == -1 )\r
593                                                                         return { container : parent, offset : i };\r
594                                                                 else if ( !comparisonEnd )\r
595                                                                         return { container : parent, offset : i + 1 };\r
596 \r
597                                                                 testRange = null;\r
598                                                         }\r
599                                                 }\r
600 \r
601                                                 if ( !testRange )\r
602                                                 {\r
603                                                         testRange = range.duplicate();\r
604                                                         testRange.moveToElementText( parent );\r
605                                                         testRange.collapse( false );\r
606                                                 }\r
607 \r
608                                                 testRange.setEndPoint( 'StartToStart', range );\r
609                                                 // IE report line break as CRLF with range.text but\r
610                                                 // only LF with textnode.nodeValue, normalize them to avoid\r
611                                                 // breaking character counting logic below. (#3949)\r
612                                                 var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
613 \r
614                                                 try\r
615                                                 {\r
616                                                         while ( distance > 0 )\r
617                                                                 distance -= siblings[ --i ].nodeValue.length;\r
618                                                 }\r
619                                                 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
620                                                 catch( e )\r
621                                                 {\r
622                                                         distance = 0;\r
623                                                 }\r
624 \r
625 \r
626                                                 if ( distance === 0 )\r
627                                                 {\r
628                                                         return {\r
629                                                                 container : parent,\r
630                                                                 offset : i\r
631                                                         };\r
632                                                 }\r
633                                                 else\r
634                                                 {\r
635                                                         return {\r
636                                                                 container : siblings[ i ],\r
637                                                                 offset : -distance\r
638                                                         };\r
639                                                 }\r
640                                         };\r
641 \r
642                                         return function()\r
643                                         {\r
644                                                 // IE doesn't have range support (in the W3C way), so we\r
645                                                 // need to do some magic to transform selections into\r
646                                                 // CKEDITOR.dom.range instances.\r
647 \r
648                                                 var sel = this.getNative(),\r
649                                                         nativeRange = sel && sel.createRange(),\r
650                                                         type = this.getType(),\r
651                                                         range;\r
652 \r
653                                                 if ( !sel )\r
654                                                         return [];\r
655 \r
656                                                 if ( type == CKEDITOR.SELECTION_TEXT )\r
657                                                 {\r
658                                                         range = new CKEDITOR.dom.range( this.document );\r
659 \r
660                                                         var boundaryInfo = getBoundaryInformation( nativeRange, true );\r
661                                                         range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );\r
662 \r
663                                                         boundaryInfo = getBoundaryInformation( nativeRange );\r
664                                                         range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );\r
665 \r
666                                                         // Correct an invalid IE range case on empty list item. (#5850)\r
667                                                         if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING\r
668                                                                         && range.endOffset <= range.startContainer.getIndex() )\r
669                                                         {\r
670                                                                 range.collapse();\r
671                                                         }\r
672 \r
673                                                         return [ range ];\r
674                                                 }\r
675                                                 else if ( type == CKEDITOR.SELECTION_ELEMENT )\r
676                                                 {\r
677                                                         var retval = [];\r
678 \r
679                                                         for ( var i = 0 ; i < nativeRange.length ; i++ )\r
680                                                         {\r
681                                                                 var element = nativeRange.item( i ),\r
682                                                                         parentElement = element.parentNode,\r
683                                                                         j = 0;\r
684 \r
685                                                                 range = new CKEDITOR.dom.range( this.document );\r
686 \r
687                                                                 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )\r
688                                                                 { /*jsl:pass*/ }\r
689 \r
690                                                                 range.setStart( new CKEDITOR.dom.node( parentElement ), j );\r
691                                                                 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );\r
692                                                                 retval.push( range );\r
693                                                         }\r
694 \r
695                                                         return retval;\r
696                                                 }\r
697 \r
698                                                 return [];\r
699                                         };\r
700                                 })()\r
701                         :\r
702                                 function()\r
703                                 {\r
704 \r
705                                         // On browsers implementing the W3C range, we simply\r
706                                         // tranform the native ranges in CKEDITOR.dom.range\r
707                                         // instances.\r
708 \r
709                                         var ranges = [],\r
710                                                 range,\r
711                                                 doc = this.document,\r
712                                                 sel = this.getNative();\r
713 \r
714                                         if ( !sel )\r
715                                                 return ranges;\r
716 \r
717                                         // On WebKit, it may happen that we'll have no selection\r
718                                         // available. We normalize it here by replicating the\r
719                                         // behavior of other browsers.\r
720                                         if ( !sel.rangeCount )\r
721                                         {\r
722                                                 range = new CKEDITOR.dom.range( doc );\r
723                                                 range.moveToElementEditStart( doc.getBody() );\r
724                                                 ranges.push( range );\r
725                                         }\r
726 \r
727                                         for ( var i = 0 ; i < sel.rangeCount ; i++ )\r
728                                         {\r
729                                                 var nativeRange = sel.getRangeAt( i );\r
730 \r
731                                                 range = new CKEDITOR.dom.range( doc );\r
732 \r
733                                                 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );\r
734                                                 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );\r
735                                                 ranges.push( range );\r
736                                         }\r
737                                         return ranges;\r
738                                 };\r
739 \r
740                         return function( onlyEditables )\r
741                         {\r
742                                 var cache = this._.cache;\r
743                                 if ( cache.ranges && !onlyEditables )\r
744                                         return cache.ranges;\r
745                                 else if ( !cache.ranges )\r
746                                         cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );\r
747 \r
748                                 // Split range into multiple by read-only nodes.\r
749                                 if ( onlyEditables )\r
750                                 {\r
751                                         var ranges = cache.ranges;\r
752                                         for ( var i = 0; i < ranges.length; i++ )\r
753                                         {\r
754                                                 var range = ranges[ i ];\r
755 \r
756                                                 // Drop range spans inside one ready-only node.\r
757                                                 var parent = range.getCommonAncestor();\r
758                                                 if ( parent.isReadOnly() )\r
759                                                         ranges.splice( i, 1 );\r
760 \r
761                                                 if ( range.collapsed )\r
762                                                         continue;\r
763 \r
764                                                 var startContainer = range.startContainer,\r
765                                                         endContainer = range.endContainer,\r
766                                                         startOffset = range.startOffset,\r
767                                                         endOffset = range.endOffset,\r
768                                                         walkerRange = range.clone();\r
769 \r
770                                                 // Range may start inside a non-editable element, restart range\r
771                                                 // by the end of it.\r
772                                                 var readOnly;\r
773                                                 if ( ( readOnly = startContainer.isReadOnly() ) )\r
774                                                         range.setStartAfter( readOnly );\r
775 \r
776                                                 // Enlarge range start/end with text node to avoid walker\r
777                                                 // being DOM destructive, it doesn't interfere our checking\r
778                                                 // of elements below as well.\r
779                                                 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )\r
780                                                 {\r
781                                                         if ( startOffset >= startContainer.getLength() )\r
782                                                                 walkerRange.setStartAfter( startContainer );\r
783                                                         else\r
784                                                                 walkerRange.setStartBefore( startContainer );\r
785                                                 }\r
786 \r
787                                                 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )\r
788                                                 {\r
789                                                         if ( !endOffset )\r
790                                                                 walkerRange.setEndBefore( endContainer );\r
791                                                         else\r
792                                                                 walkerRange.setEndAfter( endContainer );\r
793                                                 }\r
794 \r
795                                                 // Looking for non-editable element inside the range.\r
796                                                 var walker = new CKEDITOR.dom.walker( walkerRange );\r
797                                                 walker.evaluator = function( node )\r
798                                                 {\r
799                                                         if ( node.type == CKEDITOR.NODE_ELEMENT\r
800                                                                 && node.getAttribute( 'contenteditable' ) == 'false' )\r
801                                                         {\r
802                                                                 var newRange = range.clone();\r
803                                                                 range.setEndBefore( node );\r
804 \r
805                                                                 // Drop collapsed range around read-only elements,\r
806                                                                 // it make sure the range list empty when selecting\r
807                                                                 // only non-editable elements.\r
808                                                                 if ( range.collapsed )\r
809                                                                         ranges.splice( i--, 1 );\r
810 \r
811                                                                 // Avoid creating invalid range.\r
812                                                                 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )\r
813                                                                 {\r
814                                                                         newRange.setStartAfter( node );\r
815                                                                         if ( !newRange.collapsed )\r
816                                                                                 ranges.splice( i + 1, 0, newRange );\r
817                                                                 }\r
818 \r
819                                                                 return true;\r
820                                                         }\r
821 \r
822                                                         return false;\r
823                                                 };\r
824 \r
825                                                 walker.next();\r
826                                         }\r
827                                 }\r
828 \r
829                                 return cache.ranges;\r
830                         };\r
831                 })(),\r
832 \r
833                 /**\r
834                  * Gets the DOM element in which the selection starts.\r
835                  * @returns {CKEDITOR.dom.element} The element at the beginning of the\r
836                  *              selection.\r
837                  * @example\r
838                  * var element = editor.getSelection().<b>getStartElement()</b>;\r
839                  * alert( element.getName() );\r
840                  */\r
841                 getStartElement : function()\r
842                 {\r
843                         var cache = this._.cache;\r
844                         if ( cache.startElement !== undefined )\r
845                                 return cache.startElement;\r
846 \r
847                         var node,\r
848                                 sel = this.getNative();\r
849 \r
850                         switch ( this.getType() )\r
851                         {\r
852                                 case CKEDITOR.SELECTION_ELEMENT :\r
853                                         return this.getSelectedElement();\r
854 \r
855                                 case CKEDITOR.SELECTION_TEXT :\r
856 \r
857                                         var range = this.getRanges()[0];\r
858 \r
859                                         if ( range )\r
860                                         {\r
861                                                 if ( !range.collapsed )\r
862                                                 {\r
863                                                         range.optimize();\r
864 \r
865                                                         // Decrease the range content to exclude particial\r
866                                                         // selected node on the start which doesn't have\r
867                                                         // visual impact. ( #3231 )\r
868                                                         while ( 1 )\r
869                                                         {\r
870                                                                 var startContainer = range.startContainer,\r
871                                                                         startOffset = range.startOffset;\r
872                                                                 // Limit the fix only to non-block elements.(#3950)\r
873                                                                 if ( startOffset == ( startContainer.getChildCount ?\r
874                                                                          startContainer.getChildCount() : startContainer.getLength() )\r
875                                                                          && !startContainer.isBlockBoundary() )\r
876                                                                         range.setStartAfter( startContainer );\r
877                                                                 else break;\r
878                                                         }\r
879 \r
880                                                         node = range.startContainer;\r
881 \r
882                                                         if ( node.type != CKEDITOR.NODE_ELEMENT )\r
883                                                                 return node.getParent();\r
884 \r
885                                                         node = node.getChild( range.startOffset );\r
886 \r
887                                                         if ( !node || node.type != CKEDITOR.NODE_ELEMENT )\r
888                                                                 node = range.startContainer;\r
889                                                         else\r
890                                                         {\r
891                                                                 var child = node.getFirst();\r
892                                                                 while (  child && child.type == CKEDITOR.NODE_ELEMENT )\r
893                                                                 {\r
894                                                                         node = child;\r
895                                                                         child = child.getFirst();\r
896                                                                 }\r
897                                                         }\r
898                                                 }\r
899                                                 else\r
900                                                 {\r
901                                                         node = range.startContainer;\r
902                                                         if ( node.type != CKEDITOR.NODE_ELEMENT )\r
903                                                                 node = node.getParent();\r
904                                                 }\r
905 \r
906                                                 node = node.$;\r
907                                         }\r
908                         }\r
909 \r
910                         return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );\r
911                 },\r
912 \r
913                 /**\r
914                  * Gets the current selected element.\r
915                  * @returns {CKEDITOR.dom.element} The selected element. Null if no\r
916                  *              selection is available or the selection type is not\r
917                  *              {@link CKEDITOR.SELECTION_ELEMENT}.\r
918                  * @example\r
919                  * var element = editor.getSelection().<b>getSelectedElement()</b>;\r
920                  * alert( element.getName() );\r
921                  */\r
922                 getSelectedElement : function()\r
923                 {\r
924                         var cache = this._.cache;\r
925                         if ( cache.selectedElement !== undefined )\r
926                                 return cache.selectedElement;\r
927 \r
928                         var self = this;\r
929 \r
930                         var node = CKEDITOR.tools.tryThese(\r
931                                 // Is it native IE control type selection?\r
932                                 function()\r
933                                 {\r
934                                         return self.getNative().createRange().item( 0 );\r
935                                 },\r
936                                 // Figure it out by checking if there's a single enclosed\r
937                                 // node of the range.\r
938                                 function()\r
939                                 {\r
940                                         var range  = self.getRanges()[ 0 ],\r
941                                                 enclosed,\r
942                                                 selected;\r
943 \r
944                                         // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>\r
945                                         for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )\r
946                                                 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )\r
947                                                 && styleObjectElements[ enclosed.getName() ]\r
948                                                 && ( selected = enclosed ) ); i-- )\r
949                                         {\r
950                                                 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]\r
951                                                 range.shrink( CKEDITOR.SHRINK_ELEMENT );\r
952                                         }\r
953 \r
954                                         return  selected.$;\r
955                                 });\r
956 \r
957                         return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );\r
958                 },\r
959 \r
960                 lock : function()\r
961                 {\r
962                         // Call all cacheable function.\r
963                         this.getRanges();\r
964                         this.getStartElement();\r
965                         this.getSelectedElement();\r
966 \r
967                         // The native selection is not available when locked.\r
968                         this._.cache.nativeSel = {};\r
969 \r
970                         this.isLocked = 1;\r
971 \r
972                         // Save this selection inside the DOM document.\r
973                         this.document.setCustomData( 'cke_locked_selection', this );\r
974                 },\r
975 \r
976                 unlock : function( restore )\r
977                 {\r
978                         var doc = this.document,\r
979                                 lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
980 \r
981                         if ( lockedSelection )\r
982                         {\r
983                                 doc.setCustomData( 'cke_locked_selection', null );\r
984 \r
985                                 if ( restore )\r
986                                 {\r
987                                         var selectedElement = lockedSelection.getSelectedElement(),\r
988                                                 ranges = !selectedElement && lockedSelection.getRanges();\r
989 \r
990                                         this.isLocked = 0;\r
991                                         this.reset();\r
992 \r
993                                         doc.getBody().focus();\r
994 \r
995                                         if ( selectedElement )\r
996                                                 this.selectElement( selectedElement );\r
997                                         else\r
998                                                 this.selectRanges( ranges );\r
999                                 }\r
1000                         }\r
1001 \r
1002                         if  ( !lockedSelection || !restore )\r
1003                         {\r
1004                                 this.isLocked = 0;\r
1005                                 this.reset();\r
1006                         }\r
1007                 },\r
1008 \r
1009                 reset : function()\r
1010                 {\r
1011                         this._.cache = {};\r
1012                 },\r
1013 \r
1014                 /**\r
1015                  *  Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.\r
1016                  * @param element\r
1017                  */\r
1018                 selectElement : function( element )\r
1019                 {\r
1020                         if ( this.isLocked )\r
1021                         {\r
1022                                 var range = new CKEDITOR.dom.range( this.document );\r
1023                                 range.setStartBefore( element );\r
1024                                 range.setEndAfter( element );\r
1025 \r
1026                                 this._.cache.selectedElement = element;\r
1027                                 this._.cache.startElement = element;\r
1028                                 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );\r
1029                                 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;\r
1030 \r
1031                                 return;\r
1032                         }\r
1033 \r
1034                         if ( CKEDITOR.env.ie )\r
1035                         {\r
1036                                 this.getNative().empty();\r
1037 \r
1038                                 try\r
1039                                 {\r
1040                                         // Try to select the node as a control.\r
1041                                         range = this.document.$.body.createControlRange();\r
1042                                         range.addElement( element.$ );\r
1043                                         range.select();\r
1044                                 }\r
1045                                 catch( e )\r
1046                                 {\r
1047                                         // If failed, select it as a text range.\r
1048                                         range = this.document.$.body.createTextRange();\r
1049                                         range.moveToElementText( element.$ );\r
1050                                         range.select();\r
1051                                 }\r
1052                                 finally\r
1053                                 {\r
1054                                         this.document.fire( 'selectionchange' );\r
1055                                 }\r
1056 \r
1057                                 this.reset();\r
1058                         }\r
1059                         else\r
1060                         {\r
1061                                 // Create the range for the element.\r
1062                                 range = this.document.$.createRange();\r
1063                                 range.selectNode( element.$ );\r
1064 \r
1065                                 // Select the range.\r
1066                                 var sel = this.getNative();\r
1067                                 sel.removeAllRanges();\r
1068                                 sel.addRange( range );\r
1069 \r
1070                                 this.reset();\r
1071                         }\r
1072                 },\r
1073 \r
1074                 /**\r
1075                  *  Adding the specified ranges to document selection preceding\r
1076                  * by clearing up the original selection.\r
1077                  * @param {CKEDITOR.dom.range} ranges\r
1078                  */\r
1079                 selectRanges : function( ranges )\r
1080                 {\r
1081                         if ( this.isLocked )\r
1082                         {\r
1083                                 this._.cache.selectedElement = null;\r
1084                                 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();\r
1085                                 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );\r
1086                                 this._.cache.type = CKEDITOR.SELECTION_TEXT;\r
1087 \r
1088                                 return;\r
1089                         }\r
1090 \r
1091                         if ( CKEDITOR.env.ie )\r
1092                         {\r
1093                                 if ( ranges.length > 1 )\r
1094                                 {\r
1095                                         // IE doesn't accept multiple ranges selection, so we join all into one.\r
1096                                         var last = ranges[ ranges.length -1 ] ;\r
1097                                         ranges[ 0 ].setEnd( last.endContainer, last.endOffset );\r
1098                                         ranges.length = 1;\r
1099                                 }\r
1100 \r
1101                                 if ( ranges[ 0 ] )\r
1102                                         ranges[ 0 ].select();\r
1103 \r
1104                                 this.reset();\r
1105                         }\r
1106                         else\r
1107                         {\r
1108                                 var sel = this.getNative();\r
1109 \r
1110                                 if ( ranges.length )\r
1111                                         sel.removeAllRanges();\r
1112 \r
1113                                 for ( var i = 0 ; i < ranges.length ; i++ )\r
1114                                 {\r
1115                                         // Joining sequential ranges introduced by\r
1116                                         // readonly elements protection.\r
1117                                         if ( i < ranges.length -1 )\r
1118                                         {\r
1119                                                 var left = ranges[ i ], right = ranges[ i +1 ],\r
1120                                                                 between = left.clone();\r
1121                                                 between.setStart( left.endContainer, left.endOffset );\r
1122                                                 between.setEnd( right.startContainer, right.startOffset );\r
1123 \r
1124                                                 // Don't confused by Firefox adjancent multi-ranges\r
1125                                                 // introduced by table cells selection.\r
1126                                                 if ( !between.collapsed )\r
1127                                                 {\r
1128                                                         between.shrink( CKEDITOR.NODE_ELEMENT, true );\r
1129                                                         if ( between.getCommonAncestor().isReadOnly())\r
1130                                                         {\r
1131                                                                 right.setStart( left.startContainer, left.startOffset );\r
1132                                                                 ranges.splice( i--, 1 );\r
1133                                                                 continue;\r
1134                                                         }\r
1135                                                 }\r
1136                                         }\r
1137 \r
1138                                         var range = ranges[ i ];\r
1139                                         var nativeRange = this.document.$.createRange();\r
1140                                         var startContainer = range.startContainer;\r
1141 \r
1142                                         // In FF2, if we have a collapsed range, inside an empty\r
1143                                         // element, we must add something to it otherwise the caret\r
1144                                         // will not be visible.\r
1145                                         // In Opera instead, the selection will be moved out of the\r
1146                                         // element. (#4657)\r
1147                                         if ( range.collapsed &&\r
1148                                                 ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&\r
1149                                                 startContainer.type == CKEDITOR.NODE_ELEMENT &&\r
1150                                                 !startContainer.getChildCount() )\r
1151                                         {\r
1152                                                 startContainer.appendText( '' );\r
1153                                         }\r
1154 \r
1155                                         nativeRange.setStart( startContainer.$, range.startOffset );\r
1156                                         nativeRange.setEnd( range.endContainer.$, range.endOffset );\r
1157 \r
1158                                         // Select the range.\r
1159                                         sel.addRange( nativeRange );\r
1160                                 }\r
1161 \r
1162                                 this.reset();\r
1163                         }\r
1164                 },\r
1165 \r
1166                 /**\r
1167                  *  Create bookmark for every single of this selection range (from #getRanges)\r
1168                  * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,\r
1169                  * with extra cares to avoid interferon among those ranges. Same arguments are\r
1170                  * received as with the underlay range method.\r
1171                  */\r
1172                 createBookmarks : function( serializable )\r
1173                 {\r
1174                         return this.getRanges().createBookmarks( serializable );\r
1175                 },\r
1176 \r
1177                 /**\r
1178                  *  Create bookmark for every single of this selection range (from #getRanges)\r
1179                  * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,\r
1180                  * with extra cares to avoid interferon among those ranges. Same arguments are\r
1181                  * received as with the underlay range method.\r
1182                  */\r
1183                 createBookmarks2 : function( normalized )\r
1184                 {\r
1185                         return this.getRanges().createBookmarks2( normalized );\r
1186                 },\r
1187 \r
1188                 /**\r
1189                  * Select the virtual ranges denote by the bookmarks by calling #selectRanges.\r
1190                  * @param bookmarks\r
1191                  */\r
1192                 selectBookmarks : function( bookmarks )\r
1193                 {\r
1194                         var ranges = [];\r
1195                         for ( var i = 0 ; i < bookmarks.length ; i++ )\r
1196                         {\r
1197                                 var range = new CKEDITOR.dom.range( this.document );\r
1198                                 range.moveToBookmark( bookmarks[i] );\r
1199                                 ranges.push( range );\r
1200                         }\r
1201                         this.selectRanges( ranges );\r
1202                         return this;\r
1203                 },\r
1204 \r
1205                 /**\r
1206                  * Retrieve the common ancestor node of the first range and the last range.\r
1207                  */\r
1208                 getCommonAncestor : function()\r
1209                 {\r
1210                         var ranges = this.getRanges(),\r
1211                                 startNode = ranges[ 0 ].startContainer,\r
1212                                 endNode = ranges[ ranges.length - 1 ].endContainer;\r
1213                         return startNode.getCommonAncestor( endNode );\r
1214                 },\r
1215 \r
1216                 /**\r
1217                  * Moving scroll bar to the current selection's start position.\r
1218                  */\r
1219                 scrollIntoView : function()\r
1220                 {\r
1221                         // If we have split the block, adds a temporary span at the\r
1222                         // range position and scroll relatively to it.\r
1223                         var start = this.getStartElement();\r
1224                         start.scrollIntoView();\r
1225                 }\r
1226         };\r
1227 })();\r
1228 \r
1229 ( function()\r
1230 {\r
1231         var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),\r
1232                         fillerTextRegex = /\ufeff|\u00a0/,\r
1233                         nonCells = { table:1,tbody:1,tr:1 };\r
1234 \r
1235         CKEDITOR.dom.range.prototype.select =\r
1236                 CKEDITOR.env.ie ?\r
1237                         // V2\r
1238                         function( forceExpand )\r
1239                         {\r
1240                                 var collapsed = this.collapsed;\r
1241                                 var isStartMarkerAlone;\r
1242                                 var dummySpan;\r
1243 \r
1244                                 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.\r
1245                                 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...\r
1246                                 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells\r
1247                                         || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )\r
1248                                 {\r
1249                                         this.shrink( CKEDITOR.NODE_ELEMENT, true );\r
1250                                 }\r
1251 \r
1252                                 var bookmark = this.createBookmark();\r
1253 \r
1254                                 // Create marker tags for the start and end boundaries.\r
1255                                 var startNode = bookmark.startNode;\r
1256 \r
1257                                 var endNode;\r
1258                                 if ( !collapsed )\r
1259                                         endNode = bookmark.endNode;\r
1260 \r
1261                                 // Create the main range which will be used for the selection.\r
1262                                 var ieRange = this.document.$.body.createTextRange();\r
1263 \r
1264                                 // Position the range at the start boundary.\r
1265                                 ieRange.moveToElementText( startNode.$ );\r
1266                                 ieRange.moveStart( 'character', 1 );\r
1267 \r
1268                                 if ( endNode )\r
1269                                 {\r
1270                                         // Create a tool range for the end.\r
1271                                         var ieRangeEnd = this.document.$.body.createTextRange();\r
1272 \r
1273                                         // Position the tool range at the end.\r
1274                                         ieRangeEnd.moveToElementText( endNode.$ );\r
1275 \r
1276                                         // Move the end boundary of the main range to match the tool range.\r
1277                                         ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );\r
1278                                         ieRange.moveEnd( 'character', -1 );\r
1279                                 }\r
1280                                 else\r
1281                                 {\r
1282                                         // The isStartMarkerAlone logic comes from V2. It guarantees that the lines\r
1283                                         // will expand and that the cursor will be blinking on the right place.\r
1284                                         // Actually, we are using this flag just to avoid using this hack in all\r
1285                                         // situations, but just on those needed.\r
1286                                         var next = startNode.getNext( notWhitespaces );\r
1287                                         isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) )     // already a filler there?\r
1288                                                                                   && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );\r
1289 \r
1290                                         // Append a temporary <span>&#65279;</span> before the selection.\r
1291                                         // This is needed to avoid IE destroying selections inside empty\r
1292                                         // inline elements, like <b></b> (#253).\r
1293                                         // It is also needed when placing the selection right after an inline\r
1294                                         // element to avoid the selection moving inside of it.\r
1295                                         dummySpan = this.document.createElement( 'span' );\r
1296                                         dummySpan.setHtml( '&#65279;' );        // Zero Width No-Break Space (U+FEFF). See #1359.\r
1297                                         dummySpan.insertBefore( startNode );\r
1298 \r
1299                                         if ( isStartMarkerAlone )\r
1300                                         {\r
1301                                                 // To expand empty blocks or line spaces after <br>, we need\r
1302                                                 // instead to have any char, which will be later deleted using the\r
1303                                                 // selection.\r
1304                                                 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)\r
1305                                                 this.document.createText( '\ufeff' ).insertBefore( startNode );\r
1306                                         }\r
1307                                 }\r
1308 \r
1309                                 // Remove the markers (reset the position, because of the changes in the DOM tree).\r
1310                                 this.setStartBefore( startNode );\r
1311                                 startNode.remove();\r
1312 \r
1313                                 if ( collapsed )\r
1314                                 {\r
1315                                         if ( isStartMarkerAlone )\r
1316                                         {\r
1317                                                 // Move the selection start to include the temporary \ufeff.\r
1318                                                 ieRange.moveStart( 'character', -1 );\r
1319 \r
1320                                                 ieRange.select();\r
1321 \r
1322                                                 // Remove our temporary stuff.\r
1323                                                 this.document.$.selection.clear();\r
1324                                         }\r
1325                                         else\r
1326                                                 ieRange.select();\r
1327 \r
1328                                         this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );\r
1329                                         dummySpan.remove();\r
1330                                 }\r
1331                                 else\r
1332                                 {\r
1333                                         this.setEndBefore( endNode );\r
1334                                         endNode.remove();\r
1335                                         ieRange.select();\r
1336                                 }\r
1337 \r
1338                                 this.document.fire( 'selectionchange' );\r
1339                         }\r
1340                 :\r
1341                         function()\r
1342                         {\r
1343                                 var startContainer = this.startContainer;\r
1344 \r
1345                                 // If we have a collapsed range, inside an empty element, we must add\r
1346                                 // something to it, otherwise the caret will not be visible.\r
1347                                 if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )\r
1348                                         startContainer.append( new CKEDITOR.dom.text( '' ) );\r
1349 \r
1350                                 var nativeRange = this.document.$.createRange();\r
1351                                 nativeRange.setStart( startContainer.$, this.startOffset );\r
1352 \r
1353                                 try\r
1354                                 {\r
1355                                         nativeRange.setEnd( this.endContainer.$, this.endOffset );\r
1356                                 }\r
1357                                 catch ( e )\r
1358                                 {\r
1359                                         // There is a bug in Firefox implementation (it would be too easy\r
1360                                         // otherwise). The new start can't be after the end (W3C says it can).\r
1361                                         // So, let's create a new range and collapse it to the desired point.\r
1362                                         if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )\r
1363                                         {\r
1364                                                 this.collapse( true );\r
1365                                                 nativeRange.setEnd( this.endContainer.$, this.endOffset );\r
1366                                         }\r
1367                                         else\r
1368                                                 throw( e );\r
1369                                 }\r
1370 \r
1371                                 var selection = this.document.getSelection().getNative();\r
1372                                 selection.removeAllRanges();\r
1373                                 selection.addRange( nativeRange );\r
1374                         };\r
1375 } )();\r