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