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