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