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