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