JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.4
[ckeditor.git] / _source / plugins / selection / plugin.js
1 /*\r
2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         // #### checkSelectionChange : START\r
9 \r
10         // The selection change check basically saves the element parent tree of\r
11         // the current node and check it on successive requests. If there is any\r
12         // change on the tree, then the selectionChange event gets fired.\r
13         function checkSelectionChange()\r
14         {\r
15                 try\r
16                 {\r
17                         // In IE, the "selectionchange" event may still get thrown when\r
18                         // releasing the WYSIWYG mode, so we need to check it first.\r
19                         var sel = this.getSelection();\r
20                         if ( !sel || !sel.document.getWindow().$ )\r
21                                 return;\r
22 \r
23                         var firstElement = sel.getStartElement();\r
24                         var currentPath = new CKEDITOR.dom.elementPath( firstElement );\r
25 \r
26                         if ( !currentPath.compare( this._.selectionPreviousPath ) )\r
27                         {\r
28                                 this._.selectionPreviousPath = currentPath;\r
29                                 this.fire( 'selectionChange', { selection : sel, path : currentPath, element : firstElement } );\r
30                         }\r
31                 }\r
32                 catch (e)\r
33                 {}\r
34         }\r
35 \r
36         var checkSelectionChangeTimer,\r
37                 checkSelectionChangeTimeoutPending;\r
38 \r
39         function checkSelectionChangeTimeout()\r
40         {\r
41                 // Firing the "OnSelectionChange" event on every key press started to\r
42                 // be too slow. This function guarantees that there will be at least\r
43                 // 200ms delay between selection checks.\r
44 \r
45                 checkSelectionChangeTimeoutPending = true;\r
46 \r
47                 if ( checkSelectionChangeTimer )\r
48                         return;\r
49 \r
50                 checkSelectionChangeTimeoutExec.call( this );\r
51 \r
52                 checkSelectionChangeTimer = CKEDITOR.tools.setTimeout( checkSelectionChangeTimeoutExec, 200, this );\r
53         }\r
54 \r
55         function checkSelectionChangeTimeoutExec()\r
56         {\r
57                 checkSelectionChangeTimer = null;\r
58 \r
59                 if ( checkSelectionChangeTimeoutPending )\r
60                 {\r
61                         // Call this with a timeout so the browser properly moves the\r
62                         // selection after the mouseup. It happened that the selection was\r
63                         // being moved after the mouseup when clicking inside selected text\r
64                         // with Firefox.\r
65                         CKEDITOR.tools.setTimeout( checkSelectionChange, 0, this );\r
66 \r
67                         checkSelectionChangeTimeoutPending = false;\r
68                 }\r
69         }\r
70 \r
71         // #### checkSelectionChange : END\r
72 \r
73         function rangeRequiresFix( range )\r
74         {\r
75                 function isInlineCt( node )\r
76                 {\r
77                         return node && node.type == CKEDITOR.NODE_ELEMENT\r
78                                         && node.getName() in CKEDITOR.dtd.$removeEmpty;\r
79                 }\r
80 \r
81                 function singletonBlock( node )\r
82                 {\r
83                         var body = range.document.getBody();\r
84                         return !node.is( 'body' ) && body.getChildCount() == 1;\r
85                 }\r
86 \r
87                 var start = range.startContainer,\r
88                         offset = range.startOffset;\r
89 \r
90                 if ( start.type == CKEDITOR.NODE_TEXT )\r
91                         return false;\r
92 \r
93                 // 1. Empty inline element. <span>^</span>\r
94                 // 2. Adjoin to inline element. <p><strong>text</strong>^</p>\r
95                 // 3. The only empty block in document. <body><p>^</p></body> (#7222)\r
96                 return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) || singletonBlock( start )\r
97                                 : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) );\r
98         }\r
99 \r
100         var selectAllCmd =\r
101         {\r
102                 modes : { wysiwyg : 1, source : 1 },\r
103                 readOnly : CKEDITOR.env.ie || CKEDITOR.env.webkit,\r
104                 exec : function( editor )\r
105                 {\r
106                         switch ( editor.mode )\r
107                         {\r
108                                 case 'wysiwyg' :\r
109                                         editor.document.$.execCommand( 'SelectAll', false, null );\r
110                                         // Force triggering selectionChange (#7008)\r
111                                         editor.forceNextSelectionCheck();\r
112                                         editor.selectionChange();\r
113                                         break;\r
114                                 case 'source' :\r
115                                         // Select the contents of the textarea\r
116                                         var textarea = editor.textarea.$;\r
117                                         if ( CKEDITOR.env.ie )\r
118                                                 textarea.createTextRange().execCommand( 'SelectAll' );\r
119                                         else\r
120                                         {\r
121                                                 textarea.selectionStart = 0;\r
122                                                 textarea.selectionEnd = textarea.value.length;\r
123                                         }\r
124                                         textarea.focus();\r
125                         }\r
126                 },\r
127                 canUndo : false\r
128         };\r
129 \r
130         function createFillingChar( doc )\r
131         {\r
132                 removeFillingChar( doc );\r
133 \r
134                 var fillingChar = doc.createText( '\u200B' );\r
135                 doc.setCustomData( 'cke-fillingChar', fillingChar );\r
136 \r
137                 return fillingChar;\r
138         }\r
139 \r
140         function getFillingChar( doc )\r
141         {\r
142                 return doc && doc.getCustomData( 'cke-fillingChar' );\r
143         }\r
144 \r
145         // Checks if a filling char has been used, eventualy removing it (#1272).\r
146         function checkFillingChar( doc )\r
147         {\r
148                 var fillingChar = doc && getFillingChar( doc );\r
149                 if ( fillingChar )\r
150                 {\r
151                         // Use this flag to avoid removing the filling char right after\r
152                         // creating it.\r
153                         if ( fillingChar.getCustomData( 'ready' ) )\r
154                                 removeFillingChar( doc );\r
155                         else\r
156                                 fillingChar.setCustomData( 'ready', 1 );\r
157                 }\r
158         }\r
159 \r
160         function removeFillingChar( doc )\r
161         {\r
162                 var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' );\r
163                 if ( fillingChar )\r
164                 {\r
165                         var bm,\r
166                         sel = doc.getSelection().getNative(),\r
167                         // Be error proof.\r
168                         range = sel && sel.type != 'None' && sel.getRangeAt( 0 );\r
169 \r
170                         // Text selection position might get mangled by\r
171                         // subsequent dom modification, save it now for restoring. (#8617)\r
172                         if ( fillingChar.getLength() > 1\r
173                                  && range && range.intersectsNode( fillingChar.$ ) )\r
174                         {\r
175                                 bm = [ sel.anchorOffset, sel.focusOffset ];\r
176 \r
177                                 // Anticipate the offset change brought by the removed char.\r
178                                 var startAffected = sel.anchorNode == fillingChar.$ && sel.anchorOffset > 0,\r
179                                         endAffected = sel.focusNode == fillingChar.$ && sel.focusOffset > 0;\r
180                                 startAffected && bm[ 0 ]--;\r
181                                 endAffected && bm[ 1 ]--;\r
182 \r
183                                 // Revert the bookmark order on reverse selection.\r
184                                 isReversedSelection( sel ) && bm.unshift( bm.pop() );\r
185                         }\r
186 \r
187                         // We can't simply remove the filling node because the user\r
188                         // will actually enlarge it when typing, so we just remove the\r
189                         // invisible char from it.\r
190                         fillingChar.setText( fillingChar.getText().replace( /\u200B/g, '' ) );\r
191 \r
192                         // Restore the bookmark.\r
193                         if ( bm )\r
194                         {\r
195                                 var rng = sel.getRangeAt( 0 );\r
196                                 rng.setStart( rng.startContainer, bm[ 0 ] );\r
197                                 rng.setEnd( rng.startContainer, bm[ 1 ] );\r
198                                 sel.removeAllRanges();\r
199                                 sel.addRange( rng );\r
200                         }\r
201                 }\r
202         }\r
203 \r
204         function isReversedSelection( sel )\r
205         {\r
206                 if ( !sel.isCollapsed )\r
207                 {\r
208                         var range = sel.getRangeAt( 0 );\r
209                         // Potentially alter an reversed selection range.\r
210                         range.setStart( sel.anchorNode, sel.anchorOffset );\r
211                         range.setEnd( sel.focusNode, sel.focusOffset );\r
212                         return range.collapsed;\r
213                 }\r
214         }\r
215 \r
216         CKEDITOR.plugins.add( 'selection',\r
217         {\r
218                 init : function( editor )\r
219                 {\r
220                         // On WebKit only, we need a special "filling" char on some situations\r
221                         // (#1272). Here we set the events that should invalidate that char.\r
222                         if ( CKEDITOR.env.webkit )\r
223                         {\r
224                                 editor.on( 'selectionChange', function() { checkFillingChar( editor.document ); } );\r
225                                 editor.on( 'beforeSetMode', function() { removeFillingChar( editor.document ); } );\r
226 \r
227                                 var fillingCharBefore,\r
228                                         resetSelection;\r
229 \r
230                                 function beforeData()\r
231                                 {\r
232                                         var doc = editor.document,\r
233                                                 fillingChar = getFillingChar( doc );\r
234 \r
235                                         if ( fillingChar )\r
236                                         {\r
237                                                 // If cursor is right blinking by side of the filler node, save it for restoring,\r
238                                                 // as the following text substitution will blind it. (#7437)\r
239                                                 var sel = doc.$.defaultView.getSelection();\r
240                                                 if ( sel.type == 'Caret' && sel.anchorNode == fillingChar.$ )\r
241                                                         resetSelection = 1;\r
242 \r
243                                                 fillingCharBefore = fillingChar.getText();\r
244                                                 fillingChar.setText( fillingCharBefore.replace( /\u200B/g, '' ) );\r
245                                         }\r
246                                 }\r
247                                 function afterData()\r
248                                 {\r
249                                         var doc = editor.document,\r
250                                                 fillingChar = getFillingChar( doc );\r
251 \r
252                                         if ( fillingChar )\r
253                                         {\r
254                                                 fillingChar.setText( fillingCharBefore );\r
255 \r
256                                                 if ( resetSelection )\r
257                                                 {\r
258                                                         doc.$.defaultView.getSelection().setPosition( fillingChar.$,fillingChar.getLength() );\r
259                                                         resetSelection = 0;\r
260                                                 }\r
261                                         }\r
262                                 }\r
263                                 editor.on( 'beforeUndoImage', beforeData );\r
264                                 editor.on( 'afterUndoImage', afterData );\r
265                                 editor.on( 'beforeGetData', beforeData, null, null, 0 );\r
266                                 editor.on( 'getData', afterData );\r
267                         }\r
268 \r
269                         editor.on( 'contentDom', function()\r
270                                 {\r
271                                         var doc = editor.document,\r
272                                                 body = doc.getBody(),\r
273                                                 html = doc.getDocumentElement();\r
274 \r
275                                         if ( CKEDITOR.env.ie )\r
276                                         {\r
277                                                 // Other browsers don't loose the selection if the\r
278                                                 // editor document loose the focus. In IE, we don't\r
279                                                 // have support for it, so we reproduce it here, other\r
280                                                 // than firing the selection change event.\r
281 \r
282                                                 var savedRange,\r
283                                                         saveEnabled,\r
284                                                         restoreEnabled = 1;\r
285 \r
286                                                 // "onfocusin" is fired before "onfocus". It makes it\r
287                                                 // possible to restore the selection before click\r
288                                                 // events get executed.\r
289                                                 body.on( 'focusin', function( evt )\r
290                                                         {\r
291                                                                 // If there are elements with layout they fire this event but\r
292                                                                 // it must be ignored to allow edit its contents #4682\r
293                                                                 if ( evt.data.$.srcElement.nodeName != 'BODY' )\r
294                                                                         return;\r
295 \r
296                                                                 // Give the priority to locked selection since it probably\r
297                                                                 // reflects the actual situation, besides locked selection\r
298                                                                 // could be interfered because of text nodes normalizing.\r
299                                                                 // (#6083, #6987)\r
300                                                                 var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
301                                                                 if ( lockedSelection )\r
302                                                                 {\r
303                                                                         lockedSelection.unlock( 1 );\r
304                                                                         lockedSelection.lock();\r
305                                                                 }\r
306                                                                 // Then check ff we have saved a range, restore it at this\r
307                                                                 // point.\r
308                                                                 else if ( savedRange && restoreEnabled )\r
309                                                                 {\r
310                                                                         // Well not break because of this.\r
311                                                                         try { savedRange.select(); } catch (e) {}\r
312                                                                         savedRange = null;\r
313                                                                 }\r
314                                                         });\r
315 \r
316                                                 body.on( 'focus', function()\r
317                                                         {\r
318                                                                 // Enable selections to be saved.\r
319                                                                 saveEnabled = 1;\r
320 \r
321                                                                 saveSelection();\r
322                                                         });\r
323 \r
324                                                 body.on( 'beforedeactivate', function( evt )\r
325                                                         {\r
326                                                                 // Ignore this event if it's caused by focus switch between\r
327                                                                 // internal editable control type elements, e.g. layouted paragraph. (#4682)\r
328                                                                 if ( evt.data.$.toElement )\r
329                                                                         return;\r
330 \r
331                                                                 // Disable selections from being saved.\r
332                                                                 saveEnabled = 0;\r
333                                                                 restoreEnabled = 1;\r
334                                                         });\r
335 \r
336                                                 // [IE] Iframe will still keep the selection when blurred, if\r
337                                                 // focus is moved onto a non-editing host, e.g. link or button, but\r
338                                                 // it becomes a problem for the object type selection, since the resizer\r
339                                                 // handler attached on it will mark other part of the UI, especially\r
340                                                 // for the dialog. (#8157)\r
341                                                 // [IE<8] Even worse For old IEs, the cursor will not vanish even if\r
342                                                 // the selection has been moved to another text input in some cases. (#4716)\r
343                                                 //\r
344                                                 // Now the range restore is disabled, so we simply force IE to clean\r
345                                                 // up the selection before blur.\r
346                                                 CKEDITOR.env.ie && editor.on( 'blur', function()\r
347                                                 {\r
348                                                         // Error proof when the editor is not visible. (#6375)\r
349                                                         try{ doc.$.selection.empty(); } catch ( er){}\r
350                                                 });\r
351 \r
352                                                 // Listening on document element ensures that\r
353                                                 // scrollbar is included. (#5280)\r
354                                                 html.on( 'mousedown', function()\r
355                                                 {\r
356                                                         // Lock restore selection now, as we have\r
357                                                         // a followed 'click' event which introduce\r
358                                                         // new selection. (#5735)\r
359                                                         restoreEnabled = 0;\r
360                                                 });\r
361 \r
362                                                 html.on( 'mouseup', function()\r
363                                                 {\r
364                                                         restoreEnabled = 1;\r
365                                                 });\r
366 \r
367                                                 var scroll;\r
368                                                 // IE fires the "selectionchange" event when clicking\r
369                                                 // inside a selection. We don't want to capture that.\r
370                                                 body.on( 'mousedown', function( evt )\r
371                                                 {\r
372                                                         // IE scrolls document to top on right mousedown\r
373                                                         // when editor has no focus, remember this scroll\r
374                                                         // position and revert it before context menu opens. (#5778)\r
375                                                         if ( evt.data.$.button == 2 )\r
376                                                         {\r
377                                                                 var sel = editor.document.$.selection;\r
378                                                                 if ( sel.type == 'None' )\r
379                                                                         scroll = editor.window.getScrollPosition();\r
380                                                         }\r
381                                                         disableSave();\r
382                                                 });\r
383 \r
384                                                 body.on( 'mouseup',\r
385                                                         function( evt )\r
386                                                         {\r
387                                                                 // Restore recorded scroll position when needed on right mouseup.\r
388                                                                 if ( evt.data.$.button == 2 && scroll )\r
389                                                                 {\r
390                                                                         editor.document.$.documentElement.scrollLeft = scroll.x;\r
391                                                                         editor.document.$.documentElement.scrollTop = scroll.y;\r
392                                                                 }\r
393                                                                 scroll = null;\r
394 \r
395                                                                 saveEnabled = 1;\r
396                                                                 setTimeout( function()\r
397                                                                         {\r
398                                                                                 saveSelection( true );\r
399                                                                         },\r
400                                                                         0 );\r
401                                                         });\r
402 \r
403                                                 body.on( 'keydown', disableSave );\r
404                                                 body.on( 'keyup',\r
405                                                         function()\r
406                                                         {\r
407                                                                 saveEnabled = 1;\r
408                                                                 saveSelection();\r
409                                                         });\r
410 \r
411                                                 // When content doc is in standards mode, IE doesn't focus the editor when\r
412                                                 // clicking at the region below body (on html element) content, we emulate\r
413                                                 // the normal behavior on old IEs. (#1659, #7932)\r
414                                                 if ( ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat )\r
415                                                          && doc.$.compatMode != 'BackCompat' )\r
416                                                 {\r
417                                                         function moveRangeToPoint( range, x, y )\r
418                                                         {\r
419                                                                 // Error prune in IE7. (#9034, #9110)\r
420                                                                 try { range.moveToPoint( x, y ); } catch ( e ) {}\r
421                                                         }\r
422 \r
423                                                         html.on( 'mousedown', function( evt )\r
424                                                         {\r
425                                                                 // Expand the text range along with mouse move.\r
426                                                                 function onHover( evt )\r
427                                                                 {\r
428                                                                         evt = evt.data.$;\r
429                                                                         if ( textRng )\r
430                                                                         {\r
431                                                                                 // Read the current cursor.\r
432                                                                                 var rngEnd = body.$.createTextRange();\r
433 \r
434                                                                                 moveRangeToPoint( rngEnd, evt.x, evt.y );\r
435 \r
436                                                                                 // Handle drag directions.\r
437                                                                                 textRng.setEndPoint(\r
438                                                                                         textRng.compareEndPoints( 'StartToStart', rngEnd ) < 0 ?\r
439                                                                                         'EndToEnd' :\r
440                                                                                         'StartToStart',\r
441                                                                                         rngEnd );\r
442 \r
443                                                                                 // Update selection with new range.\r
444                                                                                 textRng.select();\r
445                                                                         }\r
446                                                                 }\r
447 \r
448                                                                 evt = evt.data.$;\r
449 \r
450                                                                 // We're sure that the click happens at the region\r
451                                                                 // below body, but not on scrollbar.\r
452                                                                 if ( evt.y < html.$.clientHeight\r
453                                                                          && evt.y > body.$.offsetTop + body.$.clientHeight\r
454                                                                          && evt.x < html.$.clientWidth )\r
455                                                                 {\r
456                                                                         // Start to build the text range.\r
457                                                                         var textRng = body.$.createTextRange();\r
458                                                                         moveRangeToPoint( textRng, evt.x, evt.y );\r
459 \r
460                                                                         html.on( 'mousemove', onHover );\r
461 \r
462                                                                         html.on( 'mouseup', function( evt )\r
463                                                                         {\r
464                                                                                 html.removeListener( 'mousemove', onHover );\r
465                                                                                 evt.removeListener();\r
466 \r
467                                                                                 // Make it in effect on mouse up. (#9022)\r
468                                                                                 textRng.select();\r
469                                                                         } );\r
470                                                                 }\r
471                                                         });\r
472                                                 }\r
473 \r
474                                                 // It's much simpler for IE8, we just need to reselect the reported range.\r
475                                                 if ( CKEDITOR.env.ie8 )\r
476                                                 {\r
477                                                         html.on( 'mouseup', function( evt )\r
478                                                         {\r
479                                                                 // The event is not fired when clicking on the scrollbars,\r
480                                                                 // so we can safely check the following to understand\r
481                                                                 // whether the empty space following <body> has been clicked.\r
482                                                                 if ( evt.data.getTarget().getName() == 'html' )\r
483                                                                 {\r
484                                                                         var sel = CKEDITOR.document.$.selection,\r
485                                                                                 range = sel.createRange();\r
486                                                                         // The selection range is reported on host, but actually it should applies to the content doc.\r
487                                                                         if ( sel.type != 'None' && range.parentElement().ownerDocument == doc.$ )\r
488                                                                                 range.select();\r
489                                                                 }\r
490                                                         } );\r
491                                                 }\r
492 \r
493                                                 // IE is the only to provide the "selectionchange"\r
494                                                 // event.\r
495                                                 doc.on( 'selectionchange', saveSelection );\r
496 \r
497                                                 function disableSave()\r
498                                                 {\r
499                                                         saveEnabled = 0;\r
500                                                 }\r
501 \r
502                                                 function saveSelection( testIt )\r
503                                                 {\r
504                                                         if ( saveEnabled )\r
505                                                         {\r
506                                                                 var doc = editor.document,\r
507                                                                         sel = editor.getSelection(),\r
508                                                                         nativeSel = sel && sel.getNative();\r
509 \r
510                                                                 // There is a very specific case, when clicking\r
511                                                                 // inside a text selection. In that case, the\r
512                                                                 // selection collapses at the clicking point,\r
513                                                                 // but the selection object remains in an\r
514                                                                 // unknown state, making createRange return a\r
515                                                                 // range at the very start of the document. In\r
516                                                                 // such situation we have to test the range, to\r
517                                                                 // be sure it's valid.\r
518                                                                 if ( testIt && nativeSel && nativeSel.type == 'None' )\r
519                                                                 {\r
520                                                                         // The "InsertImage" command can be used to\r
521                                                                         // test whether the selection is good or not.\r
522                                                                         // If not, it's enough to give some time to\r
523                                                                         // IE to put things in order for us.\r
524                                                                         if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )\r
525                                                                         {\r
526                                                                                 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );\r
527                                                                                 return;\r
528                                                                         }\r
529                                                                 }\r
530 \r
531                                                                 // Avoid saving selection from within text input. (#5747)\r
532                                                                 var parentTag;\r
533                                                                 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'\r
534                                                                         && ( parentTag = nativeSel.createRange() )\r
535                                                                         && ( parentTag = parentTag.parentElement() )\r
536                                                                         && ( parentTag = parentTag.nodeName )\r
537                                                                         && parentTag.toLowerCase() in { input: 1, textarea : 1 } )\r
538                                                                 {\r
539                                                                         return;\r
540                                                                 }\r
541 \r
542                                                                 savedRange = nativeSel && sel.getRanges()[ 0 ];\r
543 \r
544                                                                 checkSelectionChangeTimeout.call( editor );\r
545                                                         }\r
546                                                 }\r
547                                         }\r
548                                         else\r
549                                         {\r
550                                                 // In other browsers, we make the selection change\r
551                                                 // check based on other events, like clicks or keys\r
552                                                 // press.\r
553 \r
554                                                 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );\r
555                                                 doc.on( 'keyup', checkSelectionChangeTimeout, editor );\r
556                                                 doc.on( 'selectionchange', checkSelectionChangeTimeout, editor );\r
557                                         }\r
558 \r
559                                         if ( CKEDITOR.env.webkit )\r
560                                         {\r
561                                                 doc.on( 'keydown', function( evt )\r
562                                                 {\r
563                                                         var key = evt.data.getKey();\r
564                                                         // Remove the filling char before some keys get\r
565                                                         // executed, so they'll not get blocked by it.\r
566                                                         switch ( key )\r
567                                                         {\r
568                                                                 case 13 :       // ENTER\r
569                                                                 case 33 :       // PAGEUP\r
570                                                                 case 34 :       // PAGEDOWN\r
571                                                                 case 35 :       // HOME\r
572                                                                 case 36 :       // END\r
573                                                                 case 37 :       // LEFT-ARROW\r
574                                                                 case 39 :       // RIGHT-ARROW\r
575                                                                 case 8 :        // BACKSPACE\r
576                                                                 case 45 :       // INS\r
577                                                                 case 46 :       // DEl\r
578                                                                         removeFillingChar( editor.document );\r
579                                                         }\r
580 \r
581                                                 }, null, null, 10 );\r
582                                         }\r
583                                 });\r
584 \r
585                         // Clear the cached range path before unload. (#7174)\r
586                         editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );\r
587 \r
588                         editor.addCommand( 'selectAll', selectAllCmd );\r
589                         editor.ui.addButton( 'SelectAll',\r
590                                 {\r
591                                         label : editor.lang.selectAll,\r
592                                         command : 'selectAll'\r
593                                 });\r
594 \r
595                         /**\r
596                          * Check if to fire the {@link CKEDITOR.editor#selectionChange} event\r
597                          * for the current editor instance.\r
598                          *\r
599                          * @param {Boolean} checkNow Check immediately without any delay.\r
600                          */\r
601                         editor.selectionChange = function( checkNow )\r
602                         {\r
603                                 ( checkNow ? checkSelectionChange : checkSelectionChangeTimeout ).call( this );\r
604                         };\r
605 \r
606                         // IE9 might cease to work if there's an object selection inside the iframe (#7639).\r
607                         CKEDITOR.env.ie9Compat && editor.on( 'destroy', function()\r
608                         {\r
609                                 var sel = editor.getSelection();\r
610                                 sel && sel.getNative().clear();\r
611                         }, null, null, 9 );\r
612                 }\r
613         });\r
614 \r
615         /**\r
616          * Gets the current selection from the editing area when in WYSIWYG mode.\r
617          * @returns {CKEDITOR.dom.selection} A selection object or null if not in\r
618          *              WYSIWYG mode or no selection is available.\r
619          * @example\r
620          * var selection = CKEDITOR.instances.editor1.<strong>getSelection()</strong>;\r
621          * alert( selection.getType() );\r
622          */\r
623         CKEDITOR.editor.prototype.getSelection = function()\r
624         {\r
625                 return this.document && this.document.getSelection();\r
626         };\r
627 \r
628         CKEDITOR.editor.prototype.forceNextSelectionCheck = function()\r
629         {\r
630                 delete this._.selectionPreviousPath;\r
631         };\r
632 \r
633         /**\r
634          * Gets the current selection from the document.\r
635          * @returns {CKEDITOR.dom.selection} A selection object.\r
636          * @example\r
637          * var selection = CKEDITOR.instances.editor1.document.<strong>getSelection()</strong>;\r
638          * alert( selection.getType() );\r
639          */\r
640         CKEDITOR.dom.document.prototype.getSelection = function()\r
641         {\r
642                 var sel = new CKEDITOR.dom.selection( this );\r
643                 return ( !sel || sel.isInvalid ) ? null : sel;\r
644         };\r
645 \r
646         /**\r
647          * No selection.\r
648          * @constant\r
649          * @example\r
650          * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )\r
651          *     alert( 'Nothing is selected' );\r
652          */\r
653         CKEDITOR.SELECTION_NONE         = 1;\r
654 \r
655         /**\r
656          * A text or a collapsed selection.\r
657          * @constant\r
658          * @example\r
659          * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )\r
660          *     alert( 'A text is selected' );\r
661          */\r
662         CKEDITOR.SELECTION_TEXT         = 2;\r
663 \r
664         /**\r
665          * Element selection.\r
666          * @constant\r
667          * @example\r
668          * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )\r
669          *     alert( 'An element is selected' );\r
670          */\r
671         CKEDITOR.SELECTION_ELEMENT      = 3;\r
672 \r
673         /**\r
674          * Manipulates the selection in a DOM document.\r
675          * @constructor\r
676          * @param {CKEDITOR.dom.document} document The DOM document that contains the selection.\r
677          * @example\r
678          * var sel = new <strong>CKEDITOR.dom.selection( CKEDITOR.document )</strong>;\r
679          */\r
680         CKEDITOR.dom.selection = function( document )\r
681         {\r
682                 var lockedSelection = document.getCustomData( 'cke_locked_selection' );\r
683 \r
684                 if ( lockedSelection )\r
685                         return lockedSelection;\r
686 \r
687                 this.document = document;\r
688                 this.isLocked = 0;\r
689                 this._ =\r
690                 {\r
691                         cache : {}\r
692                 };\r
693 \r
694                 /**\r
695                  * IE BUG: The selection's document may be a different document than the\r
696                  * editor document. Return null if that is the case.\r
697                  */\r
698                 if ( CKEDITOR.env.ie )\r
699                 {\r
700                         // Avoid breaking because of it. (#8836)\r
701                         try\r
702                         {\r
703                                 var range = this.getNative().createRange();\r
704                                 if ( !range ||\r
705                                          ( range.item && range.item( 0 ).ownerDocument != this.document.$ ) ||\r
706                                          ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )\r
707                                 {\r
708                                         throw 0;\r
709                                 }\r
710                         }\r
711                         catch ( e )\r
712                         {\r
713                                 this.isInvalid = true;\r
714                         }\r
715                 }\r
716 \r
717                 return this;\r
718         };\r
719 \r
720         var styleObjectElements =\r
721                 {\r
722                         img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,\r
723                         a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1\r
724                 };\r
725 \r
726         CKEDITOR.dom.selection.prototype =\r
727         {\r
728                 /**\r
729                  * Gets the native selection object from the browser.\r
730                  * @function\r
731                  * @returns {Object} The native browser selection object.\r
732                  * @example\r
733                  * var selection = editor.getSelection().<strong>getNative()</strong>;\r
734                  */\r
735                 getNative :\r
736                         CKEDITOR.env.ie ?\r
737                                 function()\r
738                                 {\r
739                                         return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );\r
740                                 }\r
741                         :\r
742                                 function()\r
743                                 {\r
744                                         return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );\r
745                                 },\r
746 \r
747                 /**\r
748                  * Gets the type of the current selection. The following values are\r
749                  * available:\r
750                  * <ul>\r
751                  *              <li><code>{@link CKEDITOR.SELECTION_NONE}</code> (1): No selection.</li>\r
752                  *              <li><code>{@link CKEDITOR.SELECTION_TEXT}</code> (2): A text or a collapsed\r
753                  *                      selection is selected.</li>\r
754                  *              <li><code>{@link CKEDITOR.SELECTION_ELEMENT}</code> (3): An element is\r
755                  *                      selected.</li>\r
756                  * </ul>\r
757                  * @function\r
758                  * @returns {Number} One of the following constant values:\r
759                  *              <code>{@link CKEDITOR.SELECTION_NONE}</code>, <code>{@link CKEDITOR.SELECTION_TEXT}</code>, or\r
760                  *              <code>{@link CKEDITOR.SELECTION_ELEMENT}</code>.\r
761                  * @example\r
762                  * if ( editor.getSelection().<strong>getType()</strong> == CKEDITOR.SELECTION_TEXT )\r
763                  *     alert( 'A text is selected' );\r
764                  */\r
765                 getType :\r
766                         CKEDITOR.env.ie ?\r
767                                 function()\r
768                                 {\r
769                                         var cache = this._.cache;\r
770                                         if ( cache.type )\r
771                                                 return cache.type;\r
772 \r
773                                         var type = CKEDITOR.SELECTION_NONE;\r
774 \r
775                                         try\r
776                                         {\r
777                                                 var sel = this.getNative(),\r
778                                                         ieType = sel.type;\r
779 \r
780                                                 if ( ieType == 'Text' )\r
781                                                         type = CKEDITOR.SELECTION_TEXT;\r
782 \r
783                                                 if ( ieType == 'Control' )\r
784                                                         type = CKEDITOR.SELECTION_ELEMENT;\r
785 \r
786                                                 // It is possible that we can still get a text range\r
787                                                 // object even when type == 'None' is returned by IE.\r
788                                                 // So we'd better check the object returned by\r
789                                                 // createRange() rather than by looking at the type.\r
790                                                 if ( sel.createRange().parentElement )\r
791                                                         type = CKEDITOR.SELECTION_TEXT;\r
792                                         }\r
793                                         catch(e) {}\r
794 \r
795                                         return ( cache.type = type );\r
796                                 }\r
797                         :\r
798                                 function()\r
799                                 {\r
800                                         var cache = this._.cache;\r
801                                         if ( cache.type )\r
802                                                 return cache.type;\r
803 \r
804                                         var type = CKEDITOR.SELECTION_TEXT;\r
805 \r
806                                         var sel = this.getNative();\r
807 \r
808                                         if ( !sel )\r
809                                                 type = CKEDITOR.SELECTION_NONE;\r
810                                         else if ( sel.rangeCount == 1 )\r
811                                         {\r
812                                                 // Check if the actual selection is a control (IMG,\r
813                                                 // TABLE, HR, etc...).\r
814 \r
815                                                 var range = sel.getRangeAt(0),\r
816                                                         startContainer = range.startContainer;\r
817 \r
818                                                 if ( startContainer == range.endContainer\r
819                                                         && startContainer.nodeType == 1\r
820                                                         && ( range.endOffset - range.startOffset ) == 1\r
821                                                         && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )\r
822                                                 {\r
823                                                         type = CKEDITOR.SELECTION_ELEMENT;\r
824                                                 }\r
825                                         }\r
826 \r
827                                         return ( cache.type = type );\r
828                                 },\r
829 \r
830                 /**\r
831                  * Retrieves the <code>{@link CKEDITOR.dom.range}</code> instances that represent the current selection.\r
832                  * Note: Some browsers return multiple ranges even for a continuous selection. Firefox, for example, returns\r
833                  * one range for each table cell when one or more table rows are selected.\r
834                  * @function\r
835                  * @param {Boolean} [onlyEditables] If set to <code>true</code>, this function retrives editable ranges only.\r
836                  * @return {Array} Range instances that represent the current selection.\r
837                  * @example\r
838                  * var ranges = selection.<strong>getRanges()</strong>;\r
839                  * alert( ranges.length );\r
840                  */\r
841                 getRanges : (function()\r
842                 {\r
843                         var func = CKEDITOR.env.ie ?\r
844                                 ( function()\r
845                                 {\r
846                                         function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }\r
847 \r
848                                         // Finds the container and offset for a specific boundary\r
849                                         // of an IE range.\r
850                                         var getBoundaryInformation = function( range, start )\r
851                                         {\r
852                                                 // Creates a collapsed range at the requested boundary.\r
853                                                 range = range.duplicate();\r
854                                                 range.collapse( start );\r
855 \r
856                                                 // Gets the element that encloses the range entirely.\r
857                                                 var parent = range.parentElement(),\r
858                                                         doc = parent.ownerDocument;\r
859 \r
860                                                 // Empty parent element, e.g. <i>^</i>\r
861                                                 if ( !parent.hasChildNodes() )\r
862                                                         return  { container : parent, offset : 0 };\r
863 \r
864                                                 var siblings = parent.children,\r
865                                                         child,\r
866                                                         sibling,\r
867                                                         testRange = range.duplicate(),\r
868                                                         startIndex = 0,\r
869                                                         endIndex = siblings.length - 1,\r
870                                                         index = -1,\r
871                                                         position,\r
872                                                         distance,\r
873                                                         container;\r
874 \r
875                                                 // Binary search over all element childs to test the range to see whether\r
876                                                 // range is right on the boundary of one element.\r
877                                                 while ( startIndex <= endIndex )\r
878                                                 {\r
879                                                         index = Math.floor( ( startIndex + endIndex ) / 2 );\r
880                                                         child = siblings[ index ];\r
881                                                         testRange.moveToElementText( child );\r
882                                                         position = testRange.compareEndPoints( 'StartToStart', range );\r
883 \r
884                                                         if ( position > 0 )\r
885                                                                 endIndex = index - 1;\r
886                                                         else if ( position < 0 )\r
887                                                                 startIndex = index + 1;\r
888                                                         else\r
889                                                         {\r
890                                                                 // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs.\r
891                                                                 // e.g. <p>text<br />^<br /></p> (#7433)\r
892                                                                 if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' )\r
893                                                                 {\r
894                                                                         // "Fall back" to w3c selection.\r
895                                                                         var sel = doc.defaultView.getSelection();\r
896                                                                         return { container : sel[ start ? 'anchorNode' : 'focusNode' ],\r
897                                                                                 offset : sel[ start ? 'anchorOffset' : 'focusOffset' ] };\r
898                                                                 }\r
899                                                                 else\r
900                                                                         return { container : parent, offset : getNodeIndex( child ) };\r
901                                                         }\r
902                                                 }\r
903 \r
904                                                 // All childs are text nodes,\r
905                                                 // or to the right hand of test range are all text nodes. (#6992)\r
906                                                 if ( index == -1 || index == siblings.length - 1 && position < 0 )\r
907                                                 {\r
908                                                         // Adapt test range to embrace the entire parent contents.\r
909                                                         testRange.moveToElementText( parent );\r
910                                                         testRange.setEndPoint( 'StartToStart', range );\r
911 \r
912                                                         // IE report line break as CRLF with range.text but\r
913                                                         // only LF with textnode.nodeValue, normalize them to avoid\r
914                                                         // breaking character counting logic below. (#3949)\r
915                                                         distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
916 \r
917                                                         siblings = parent.childNodes;\r
918 \r
919                                                         // Actual range anchor right beside test range at the boundary of text node.\r
920                                                         if ( !distance )\r
921                                                         {\r
922                                                                 child = siblings[ siblings.length - 1 ];\r
923 \r
924                                                                 if ( child.nodeType != CKEDITOR.NODE_TEXT )\r
925                                                                         return { container : parent, offset : siblings.length };\r
926                                                                 else\r
927                                                                         return { container : child, offset : child.nodeValue.length };\r
928                                                         }\r
929 \r
930                                                         // Start the measuring until distance overflows, meanwhile count the text nodes.\r
931                                                         var i = siblings.length;\r
932                                                         while ( distance > 0 && i > 0 )\r
933                                                         {\r
934                                                                 sibling = siblings[ --i ];\r
935                                                                 if ( sibling.nodeType == CKEDITOR.NODE_TEXT )\r
936                                                                 {\r
937                                                                         container = sibling;\r
938                                                                         distance -= sibling.nodeValue.length;\r
939                                                                 }\r
940                                                         }\r
941 \r
942                                                         return  { container : container, offset : -distance };\r
943                                                 }\r
944                                                 // Test range was one offset beyond OR behind the anchored text node.\r
945                                                 else\r
946                                                 {\r
947                                                         // Adapt one side of test range to the actual range\r
948                                                         // for measuring the offset between them.\r
949                                                         testRange.collapse( position > 0 ? true : false );\r
950                                                         testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );\r
951 \r
952                                                         // IE report line break as CRLF with range.text but\r
953                                                         // only LF with textnode.nodeValue, normalize them to avoid\r
954                                                         // breaking character counting logic below. (#3949)\r
955                                                         distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
956 \r
957                                                         // Actual range anchor right beside test range at the inner boundary of text node.\r
958                                                         if ( !distance )\r
959                                                                 return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };\r
960 \r
961                                                         // Start the measuring until distance overflows, meanwhile count the text nodes.\r
962                                                         while ( distance > 0 )\r
963                                                         {\r
964                                                                 try\r
965                                                                 {\r
966                                                                         sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
967                                                                         if ( sibling.nodeType == CKEDITOR.NODE_TEXT )\r
968                                                                         {\r
969                                                                                 distance -= sibling.nodeValue.length;\r
970                                                                                 container = sibling;\r
971                                                                         }\r
972                                                                         child = sibling;\r
973                                                                 }\r
974                                                                 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
975                                                                 catch( e )\r
976                                                                 {\r
977                                                                         return { container : parent, offset : getNodeIndex( child ) };\r
978                                                                 }\r
979                                                         }\r
980 \r
981                                                         return { container : container, offset : position > 0 ? -distance : container.nodeValue.length + distance };\r
982                                                 }\r
983                                         };\r
984 \r
985                                         return function()\r
986                                         {\r
987                                                 // IE doesn't have range support (in the W3C way), so we\r
988                                                 // need to do some magic to transform selections into\r
989                                                 // CKEDITOR.dom.range instances.\r
990 \r
991                                                 var sel = this.getNative(),\r
992                                                         nativeRange = sel && sel.createRange(),\r
993                                                         type = this.getType(),\r
994                                                         range;\r
995 \r
996                                                 if ( !sel )\r
997                                                         return [];\r
998 \r
999                                                 if ( type == CKEDITOR.SELECTION_TEXT )\r
1000                                                 {\r
1001                                                         range = new CKEDITOR.dom.range( this.document );\r
1002 \r
1003                                                         var boundaryInfo = getBoundaryInformation( nativeRange, true );\r
1004                                                         range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );\r
1005 \r
1006                                                         boundaryInfo = getBoundaryInformation( nativeRange );\r
1007                                                         range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );\r
1008 \r
1009                                                         // Correct an invalid IE range case on empty list item. (#5850)\r
1010                                                         if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING\r
1011                                                                         && range.endOffset <= range.startContainer.getIndex() )\r
1012                                                         {\r
1013                                                                 range.collapse();\r
1014                                                         }\r
1015 \r
1016                                                         return [ range ];\r
1017                                                 }\r
1018                                                 else if ( type == CKEDITOR.SELECTION_ELEMENT )\r
1019                                                 {\r
1020                                                         var retval = [];\r
1021 \r
1022                                                         for ( var i = 0 ; i < nativeRange.length ; i++ )\r
1023                                                         {\r
1024                                                                 var element = nativeRange.item( i ),\r
1025                                                                         parentElement = element.parentNode,\r
1026                                                                         j = 0;\r
1027 \r
1028                                                                 range = new CKEDITOR.dom.range( this.document );\r
1029 \r
1030                                                                 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )\r
1031                                                                 { /*jsl:pass*/ }\r
1032 \r
1033                                                                 range.setStart( new CKEDITOR.dom.node( parentElement ), j );\r
1034                                                                 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );\r
1035                                                                 retval.push( range );\r
1036                                                         }\r
1037 \r
1038                                                         return retval;\r
1039                                                 }\r
1040 \r
1041                                                 return [];\r
1042                                         };\r
1043                                 })()\r
1044                         :\r
1045                                 function()\r
1046                                 {\r
1047 \r
1048                                         // On browsers implementing the W3C range, we simply\r
1049                                         // tranform the native ranges in CKEDITOR.dom.range\r
1050                                         // instances.\r
1051 \r
1052                                         var ranges = [],\r
1053                                                 range,\r
1054                                                 doc = this.document,\r
1055                                                 sel = this.getNative();\r
1056 \r
1057                                         if ( !sel )\r
1058                                                 return ranges;\r
1059 \r
1060                                         // On WebKit, it may happen that we'll have no selection\r
1061                                         // available. We normalize it here by replicating the\r
1062                                         // behavior of other browsers.\r
1063                                         if ( !sel.rangeCount )\r
1064                                         {\r
1065                                                 range = new CKEDITOR.dom.range( doc );\r
1066                                                 range.moveToElementEditStart( doc.getBody() );\r
1067                                                 ranges.push( range );\r
1068                                         }\r
1069 \r
1070                                         for ( var i = 0 ; i < sel.rangeCount ; i++ )\r
1071                                         {\r
1072                                                 var nativeRange = sel.getRangeAt( i );\r
1073 \r
1074                                                 range = new CKEDITOR.dom.range( doc );\r
1075 \r
1076                                                 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );\r
1077                                                 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );\r
1078                                                 ranges.push( range );\r
1079                                         }\r
1080                                         return ranges;\r
1081                                 };\r
1082 \r
1083                         return function( onlyEditables )\r
1084                         {\r
1085                                 var cache = this._.cache;\r
1086                                 if ( cache.ranges && !onlyEditables )\r
1087                                         return cache.ranges;\r
1088                                 else if ( !cache.ranges )\r
1089                                         cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );\r
1090 \r
1091                                 // Split range into multiple by read-only nodes.\r
1092                                 if ( onlyEditables )\r
1093                                 {\r
1094                                         var ranges = cache.ranges;\r
1095                                         for ( var i = 0; i < ranges.length; i++ )\r
1096                                         {\r
1097                                                 var range = ranges[ i ];\r
1098 \r
1099                                                 // Drop range spans inside one ready-only node.\r
1100                                                 var parent = range.getCommonAncestor();\r
1101                                                 if ( parent.isReadOnly() )\r
1102                                                         ranges.splice( i, 1 );\r
1103 \r
1104                                                 if ( range.collapsed )\r
1105                                                         continue;\r
1106 \r
1107                                                 // Range may start inside a non-editable element,\r
1108                                                 // replace the range start after it.\r
1109                                                 if ( range.startContainer.isReadOnly() )\r
1110                                                 {\r
1111                                                         var current = range.startContainer;\r
1112                                                         while( current )\r
1113                                                         {\r
1114                                                                 if ( current.is( 'body' ) || !current.isReadOnly() )\r
1115                                                                         break;\r
1116 \r
1117                                                                 if ( current.type == CKEDITOR.NODE_ELEMENT\r
1118                                                                                 && current.getAttribute( 'contentEditable' ) == 'false' )\r
1119                                                                         range.setStartAfter( current );\r
1120 \r
1121                                                                 current = current.getParent();\r
1122                                                         }\r
1123                                                 }\r
1124 \r
1125                                                 var startContainer = range.startContainer,\r
1126                                                         endContainer = range.endContainer,\r
1127                                                         startOffset = range.startOffset,\r
1128                                                         endOffset = range.endOffset,\r
1129                                                         walkerRange = range.clone();\r
1130 \r
1131                                                 // Enlarge range start/end with text node to avoid walker\r
1132                                                 // being DOM destructive, it doesn't interfere our checking\r
1133                                                 // of elements below as well.\r
1134                                                 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )\r
1135                                                 {\r
1136                                                         if ( startOffset >= startContainer.getLength() )\r
1137                                                                 walkerRange.setStartAfter( startContainer );\r
1138                                                         else\r
1139                                                                 walkerRange.setStartBefore( startContainer );\r
1140                                                 }\r
1141 \r
1142                                                 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )\r
1143                                                 {\r
1144                                                         if ( !endOffset )\r
1145                                                                 walkerRange.setEndBefore( endContainer );\r
1146                                                         else\r
1147                                                                 walkerRange.setEndAfter( endContainer );\r
1148                                                 }\r
1149 \r
1150                                                 // Looking for non-editable element inside the range.\r
1151                                                 var walker = new CKEDITOR.dom.walker( walkerRange );\r
1152                                                 walker.evaluator = function( node )\r
1153                                                 {\r
1154                                                         if ( node.type == CKEDITOR.NODE_ELEMENT\r
1155                                                                 && node.isReadOnly() )\r
1156                                                         {\r
1157                                                                 var newRange = range.clone();\r
1158                                                                 range.setEndBefore( node );\r
1159 \r
1160                                                                 // Drop collapsed range around read-only elements,\r
1161                                                                 // it make sure the range list empty when selecting\r
1162                                                                 // only non-editable elements.\r
1163                                                                 if ( range.collapsed )\r
1164                                                                         ranges.splice( i--, 1 );\r
1165 \r
1166                                                                 // Avoid creating invalid range.\r
1167                                                                 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )\r
1168                                                                 {\r
1169                                                                         newRange.setStartAfter( node );\r
1170                                                                         if ( !newRange.collapsed )\r
1171                                                                                 ranges.splice( i + 1, 0, newRange );\r
1172                                                                 }\r
1173 \r
1174                                                                 return true;\r
1175                                                         }\r
1176 \r
1177                                                         return false;\r
1178                                                 };\r
1179 \r
1180                                                 walker.next();\r
1181                                         }\r
1182                                 }\r
1183 \r
1184                                 return cache.ranges;\r
1185                         };\r
1186                 })(),\r
1187 \r
1188                 /**\r
1189                  * Gets the DOM element in which the selection starts.\r
1190                  * @returns {CKEDITOR.dom.element} The element at the beginning of the\r
1191                  *              selection.\r
1192                  * @example\r
1193                  * var element = editor.getSelection().<strong>getStartElement()</strong>;\r
1194                  * alert( element.getName() );\r
1195                  */\r
1196                 getStartElement : function()\r
1197                 {\r
1198                         var cache = this._.cache;\r
1199                         if ( cache.startElement !== undefined )\r
1200                                 return cache.startElement;\r
1201 \r
1202                         var node,\r
1203                                 sel = this.getNative();\r
1204 \r
1205                         switch ( this.getType() )\r
1206                         {\r
1207                                 case CKEDITOR.SELECTION_ELEMENT :\r
1208                                         return this.getSelectedElement();\r
1209 \r
1210                                 case CKEDITOR.SELECTION_TEXT :\r
1211 \r
1212                                         var range = this.getRanges()[0];\r
1213 \r
1214                                         if ( range )\r
1215                                         {\r
1216                                                 if ( !range.collapsed )\r
1217                                                 {\r
1218                                                         range.optimize();\r
1219 \r
1220                                                         // Decrease the range content to exclude particial\r
1221                                                         // selected node on the start which doesn't have\r
1222                                                         // visual impact. ( #3231 )\r
1223                                                         while ( 1 )\r
1224                                                         {\r
1225                                                                 var startContainer = range.startContainer,\r
1226                                                                         startOffset = range.startOffset;\r
1227                                                                 // Limit the fix only to non-block elements.(#3950)\r
1228                                                                 if ( startOffset == ( startContainer.getChildCount ?\r
1229                                                                          startContainer.getChildCount() : startContainer.getLength() )\r
1230                                                                          && !startContainer.isBlockBoundary() )\r
1231                                                                         range.setStartAfter( startContainer );\r
1232                                                                 else break;\r
1233                                                         }\r
1234 \r
1235                                                         node = range.startContainer;\r
1236 \r
1237                                                         if ( node.type != CKEDITOR.NODE_ELEMENT )\r
1238                                                                 return node.getParent();\r
1239 \r
1240                                                         node = node.getChild( range.startOffset );\r
1241 \r
1242                                                         if ( !node || node.type != CKEDITOR.NODE_ELEMENT )\r
1243                                                                 node = range.startContainer;\r
1244                                                         else\r
1245                                                         {\r
1246                                                                 var child = node.getFirst();\r
1247                                                                 while (  child && child.type == CKEDITOR.NODE_ELEMENT )\r
1248                                                                 {\r
1249                                                                         node = child;\r
1250                                                                         child = child.getFirst();\r
1251                                                                 }\r
1252                                                         }\r
1253                                                 }\r
1254                                                 else\r
1255                                                 {\r
1256                                                         node = range.startContainer;\r
1257                                                         if ( node.type != CKEDITOR.NODE_ELEMENT )\r
1258                                                                 node = node.getParent();\r
1259                                                 }\r
1260 \r
1261                                                 node = node.$;\r
1262                                         }\r
1263                         }\r
1264 \r
1265                         return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );\r
1266                 },\r
1267 \r
1268                 /**\r
1269                  * Gets the currently selected element.\r
1270                  * @returns {CKEDITOR.dom.element} The selected element. Null if no\r
1271                  *              selection is available or the selection type is not\r
1272                  *              <code>{@link CKEDITOR.SELECTION_ELEMENT}</code>.\r
1273                  * @example\r
1274                  * var element = editor.getSelection().<strong>getSelectedElement()</strong>;\r
1275                  * alert( element.getName() );\r
1276                  */\r
1277                 getSelectedElement : function()\r
1278                 {\r
1279                         var cache = this._.cache;\r
1280                         if ( cache.selectedElement !== undefined )\r
1281                                 return cache.selectedElement;\r
1282 \r
1283                         var self = this;\r
1284 \r
1285                         var node = CKEDITOR.tools.tryThese(\r
1286                                 // Is it native IE control type selection?\r
1287                                 function()\r
1288                                 {\r
1289                                         return self.getNative().createRange().item( 0 );\r
1290                                 },\r
1291                                 // If a table or list is fully selected.\r
1292                                 function()\r
1293                                 {\r
1294                                         var root,\r
1295                                                 retval,\r
1296                                                 range  = self.getRanges()[ 0 ],\r
1297                                                 ancestor = range.getCommonAncestor( 1, 1 ),\r
1298                                                 tags = { table:1,ul:1,ol:1,dl:1 };\r
1299 \r
1300                                         for ( var t in tags )\r
1301                                         {\r
1302                                                 if ( ( root = ancestor.getAscendant( t, 1 ) ) )\r
1303                                                         break;\r
1304                                         }\r
1305 \r
1306                                         if ( root )\r
1307                                         {\r
1308                                                 // Enlarging the start boundary.\r
1309                                                 var testRange = new CKEDITOR.dom.range( this.document );\r
1310                                                 testRange.setStartAt( root, CKEDITOR.POSITION_AFTER_START );\r
1311                                                 testRange.setEnd( range.startContainer, range.startOffset );\r
1312 \r
1313                                                 var enlargeables = CKEDITOR.tools.extend( tags, CKEDITOR.dtd.$listItem, CKEDITOR.dtd.$tableContent ),\r
1314                                                         walker = new CKEDITOR.dom.walker( testRange ),\r
1315                                                         // Check the range is at the inner boundary of the structural element.\r
1316                                                         guard = function( walker, isEnd )\r
1317                                                         {\r
1318                                                                 return function( node, isWalkOut )\r
1319                                                                 {\r
1320                                                                         if ( node.type == CKEDITOR.NODE_TEXT && ( !CKEDITOR.tools.trim( node.getText() ) || node.getParent().data( 'cke-bookmark' ) ) )\r
1321                                                                                 return true;\r
1322 \r
1323                                                                         var tag;\r
1324                                                                         if ( node.type == CKEDITOR.NODE_ELEMENT )\r
1325                                                                         {\r
1326                                                                                 tag = node.getName();\r
1327 \r
1328                                                                                 // Bypass bogus br at the end of block.\r
1329                                                                                 if ( tag == 'br' && isEnd && node.equals( node.getParent().getBogus() ) )\r
1330                                                                                         return true;\r
1331 \r
1332                                                                                 if ( isWalkOut && tag in enlargeables || tag in CKEDITOR.dtd.$removeEmpty )\r
1333                                                                                         return true;\r
1334                                                                         }\r
1335 \r
1336                                                                         walker.halted = 1;\r
1337                                                                         return false;\r
1338                                                                 };\r
1339                                                         };\r
1340 \r
1341                                                 walker.guard = guard( walker );\r
1342 \r
1343                                                 if ( walker.checkBackward() && !walker.halted )\r
1344                                                 {\r
1345                                                         walker = new CKEDITOR.dom.walker( testRange );\r
1346                                                         testRange.setStart( range.endContainer, range.endOffset );\r
1347                                                         testRange.setEndAt( root, CKEDITOR.POSITION_BEFORE_END );\r
1348                                                         walker.guard = guard( walker, 1 );\r
1349                                                         if ( walker.checkForward() && !walker.halted )\r
1350                                                                 retval = root.$;\r
1351                                                 }\r
1352                                         }\r
1353 \r
1354                                         if ( !retval )\r
1355                                                 throw 0;\r
1356 \r
1357                                         return retval;\r
1358                                 },\r
1359                                 // Figure it out by checking if there's a single enclosed\r
1360                                 // node of the range.\r
1361                                 function()\r
1362                                 {\r
1363                                         var range  = self.getRanges()[ 0 ],\r
1364                                                 enclosed,\r
1365                                                 selected;\r
1366 \r
1367                                         // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>\r
1368                                         for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )\r
1369                                                 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )\r
1370                                                 && styleObjectElements[ enclosed.getName() ]\r
1371                                                 && ( selected = enclosed ) ); i-- )\r
1372                                         {\r
1373                                                 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]\r
1374                                                 range.shrink( CKEDITOR.SHRINK_ELEMENT );\r
1375                                         }\r
1376 \r
1377                                         return  selected.$;\r
1378                                 });\r
1379 \r
1380                         return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );\r
1381                 },\r
1382 \r
1383                 /**\r
1384                  * Retrieves the text contained within the range. An empty string is returned for non-text selection.\r
1385                  * @returns {String} A string of text within the current selection.\r
1386                  * @since 3.6.1\r
1387                  * @example\r
1388                  * var text = editor.getSelection().<strong>getSelectedText()</strong>;\r
1389                  * alert( text );\r
1390                  */\r
1391                 getSelectedText : function()\r
1392                 {\r
1393                         var cache = this._.cache;\r
1394                         if ( cache.selectedText !== undefined )\r
1395                                 return cache.selectedText;\r
1396 \r
1397                         var text = '',\r
1398                                 nativeSel = this.getNative();\r
1399                         if ( this.getType() == CKEDITOR.SELECTION_TEXT )\r
1400                                 text = CKEDITOR.env.ie ? nativeSel.createRange().text : nativeSel.toString();\r
1401 \r
1402                         return ( cache.selectedText = text );\r
1403                 },\r
1404 \r
1405                 /**\r
1406                  * Locks the selection made in the editor in order to make it possible to\r
1407                  * manipulate it without browser interference. A locked selection is\r
1408                  * cached and remains unchanged until it is released with the <code>#unlock</code>\r
1409                  * method.\r
1410                  * @example\r
1411                  * editor.getSelection().<strong>lock()</strong>;\r
1412                  */\r
1413                 lock : function()\r
1414                 {\r
1415                         // Call all cacheable function.\r
1416                         this.getRanges();\r
1417                         this.getStartElement();\r
1418                         this.getSelectedElement();\r
1419                         this.getSelectedText();\r
1420 \r
1421                         // The native selection is not available when locked.\r
1422                         this._.cache.nativeSel = {};\r
1423 \r
1424                         this.isLocked = 1;\r
1425 \r
1426                         // Save this selection inside the DOM document.\r
1427                         this.document.setCustomData( 'cke_locked_selection', this );\r
1428                 },\r
1429 \r
1430                 /**\r
1431                  * Unlocks the selection made in the editor and locked with the <code>#lock</code> method.\r
1432                  * An unlocked selection is no longer cached and can be changed.\r
1433                  * @param {Boolean} [restore] If set to <code>true</code>, the selection is restored back to the selection saved earlier by using the <code>#lock</code> method.\r
1434                  * @example\r
1435                  * editor.getSelection().<strong>unlock()</strong>;\r
1436                  */\r
1437                 unlock : function( restore )\r
1438                 {\r
1439                         var doc = this.document,\r
1440                                 lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
1441 \r
1442                         if ( lockedSelection )\r
1443                         {\r
1444                                 doc.setCustomData( 'cke_locked_selection', null );\r
1445 \r
1446                                 if ( restore )\r
1447                                 {\r
1448                                         var selectedElement = lockedSelection.getSelectedElement(),\r
1449                                                 ranges = !selectedElement && lockedSelection.getRanges();\r
1450 \r
1451                                         this.isLocked = 0;\r
1452                                         this.reset();\r
1453 \r
1454                                         if ( selectedElement )\r
1455                                                 this.selectElement( selectedElement );\r
1456                                         else\r
1457                                                 this.selectRanges( ranges );\r
1458                                 }\r
1459                         }\r
1460 \r
1461                         if  ( !lockedSelection || !restore )\r
1462                         {\r
1463                                 this.isLocked = 0;\r
1464                                 this.reset();\r
1465                         }\r
1466                 },\r
1467 \r
1468                 /**\r
1469                  * Clears the selection cache.\r
1470                  * @example\r
1471                  * editor.getSelection().<strong>reset()</strong>;\r
1472                  */\r
1473                 reset : function()\r
1474                 {\r
1475                         this._.cache = {};\r
1476                 },\r
1477 \r
1478                 /**\r
1479                  * Makes the current selection of type <code>{@link CKEDITOR.SELECTION_ELEMENT}</code> by enclosing the specified element.\r
1480                  * @param {CKEDITOR.dom.element} element The element to enclose in the selection.\r
1481                  * @example\r
1482                  * var element = editor.document.getById( 'sampleElement' );\r
1483                  * editor.getSelection.<strong>selectElement( element )</strong>;\r
1484                  */\r
1485                 selectElement : function( element )\r
1486                 {\r
1487                         if ( this.isLocked )\r
1488                         {\r
1489                                 var range = new CKEDITOR.dom.range( this.document );\r
1490                                 range.setStartBefore( element );\r
1491                                 range.setEndAfter( element );\r
1492 \r
1493                                 this._.cache.selectedElement = element;\r
1494                                 this._.cache.startElement = element;\r
1495                                 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );\r
1496                                 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;\r
1497 \r
1498                                 return;\r
1499                         }\r
1500 \r
1501                         range = new CKEDITOR.dom.range( element.getDocument() );\r
1502                         range.setStartBefore( element );\r
1503                         range.setEndAfter( element );\r
1504                         range.select();\r
1505 \r
1506                         this.document.fire( 'selectionchange' );\r
1507                         this.reset();\r
1508 \r
1509                 },\r
1510 \r
1511                 /**\r
1512                  *  Clears the original selection and adds the specified ranges\r
1513                  * to the document selection.\r
1514                  * @param {Array} ranges An array of <code>{@link CKEDITOR.dom.range}</code> instances representing ranges to be added to the document.\r
1515                  * @example\r
1516                  * var ranges = new CKEDITOR.dom.range( editor.document );\r
1517                  * editor.getSelection().<strong>selectRanges( [ ranges ] )</strong>;\r
1518                  */\r
1519                 selectRanges : function( ranges )\r
1520                 {\r
1521                         if ( this.isLocked )\r
1522                         {\r
1523                                 this._.cache.selectedElement = null;\r
1524                                 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();\r
1525                                 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );\r
1526                                 this._.cache.type = CKEDITOR.SELECTION_TEXT;\r
1527 \r
1528                                 return;\r
1529                         }\r
1530 \r
1531                         if ( CKEDITOR.env.ie )\r
1532                         {\r
1533                                 if ( ranges.length > 1 )\r
1534                                 {\r
1535                                         // IE doesn't accept multiple ranges selection, so we join all into one.\r
1536                                         var last = ranges[ ranges.length -1 ] ;\r
1537                                         ranges[ 0 ].setEnd( last.endContainer, last.endOffset );\r
1538                                         ranges.length = 1;\r
1539                                 }\r
1540 \r
1541                                 if ( ranges[ 0 ] )\r
1542                                         ranges[ 0 ].select();\r
1543 \r
1544                                 this.reset();\r
1545                         }\r
1546                         else\r
1547                         {\r
1548                                 var sel = this.getNative();\r
1549 \r
1550                                 // getNative() returns null if iframe is "display:none" in FF. (#6577)\r
1551                                 if ( !sel )\r
1552                                         return;\r
1553 \r
1554                                 if ( ranges.length )\r
1555                                 {\r
1556                                         sel.removeAllRanges();\r
1557                                         // Remove any existing filling char first.\r
1558                                         CKEDITOR.env.webkit && removeFillingChar( this.document );\r
1559                                 }\r
1560 \r
1561                                 for ( var i = 0 ; i < ranges.length ; i++ )\r
1562                                 {\r
1563                                         // Joining sequential ranges introduced by\r
1564                                         // readonly elements protection.\r
1565                                         if ( i < ranges.length -1 )\r
1566                                         {\r
1567                                                 var left = ranges[ i ], right = ranges[ i +1 ],\r
1568                                                                 between = left.clone();\r
1569                                                 between.setStart( left.endContainer, left.endOffset );\r
1570                                                 between.setEnd( right.startContainer, right.startOffset );\r
1571 \r
1572                                                 // Don't confused by Firefox adjancent multi-ranges\r
1573                                                 // introduced by table cells selection.\r
1574                                                 if ( !between.collapsed )\r
1575                                                 {\r
1576                                                         between.shrink( CKEDITOR.NODE_ELEMENT, true );\r
1577                                                         var ancestor = between.getCommonAncestor(),\r
1578                                                                 enclosed = between.getEnclosedNode();\r
1579 \r
1580                                                         // The following cases has to be considered:\r
1581                                                         // 1. <span contenteditable="false">[placeholder]</span>\r
1582                                                         // 2. <input contenteditable="false"  type="radio"/> (#6621)\r
1583                                                         if ( ancestor.isReadOnly() || enclosed && enclosed.isReadOnly() )\r
1584                                                         {\r
1585                                                                 right.setStart( left.startContainer, left.startOffset );\r
1586                                                                 ranges.splice( i--, 1 );\r
1587                                                                 continue;\r
1588                                                         }\r
1589                                                 }\r
1590                                         }\r
1591 \r
1592                                         var range = ranges[ i ];\r
1593                                         var nativeRange = this.document.$.createRange();\r
1594                                         var startContainer = range.startContainer;\r
1595 \r
1596                                         // In FF2, if we have a collapsed range, inside an empty\r
1597                                         // element, we must add something to it otherwise the caret\r
1598                                         // will not be visible.\r
1599                                         // In Opera instead, the selection will be moved out of the\r
1600                                         // element. (#4657)\r
1601                                         if ( range.collapsed &&\r
1602                                                 ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&\r
1603                                                 startContainer.type == CKEDITOR.NODE_ELEMENT &&\r
1604                                                 !startContainer.getChildCount() )\r
1605                                         {\r
1606                                                 startContainer.appendText( '' );\r
1607                                         }\r
1608 \r
1609                                         if ( range.collapsed\r
1610                                                         && CKEDITOR.env.webkit\r
1611                                                         && rangeRequiresFix( range ) )\r
1612                                         {\r
1613                                                 // Append a zero-width space so WebKit will not try to\r
1614                                                 // move the selection by itself (#1272).\r
1615                                                 var fillingChar = createFillingChar( this.document );\r
1616                                                 range.insertNode( fillingChar ) ;\r
1617 \r
1618                                                 var next = fillingChar.getNext();\r
1619 \r
1620                                                 // If the filling char is followed by a <br>, whithout\r
1621                                                 // having something before it, it'll not blink.\r
1622                                                 // Let's remove it in this case.\r
1623                                                 if ( next && !fillingChar.getPrevious() && next.type == CKEDITOR.NODE_ELEMENT && next.getName() == 'br' )\r
1624                                                 {\r
1625                                                         removeFillingChar( this.document );\r
1626                                                         range.moveToPosition( next, CKEDITOR.POSITION_BEFORE_START );\r
1627                                                 }\r
1628                                                 else\r
1629                                                         range.moveToPosition( fillingChar, CKEDITOR.POSITION_AFTER_END );\r
1630                                         }\r
1631 \r
1632                                         nativeRange.setStart( range.startContainer.$, range.startOffset );\r
1633 \r
1634                                         try\r
1635                                         {\r
1636                                                 nativeRange.setEnd( range.endContainer.$, range.endOffset );\r
1637                                         }\r
1638                                         catch ( e )\r
1639                                         {\r
1640                                                 // There is a bug in Firefox implementation (it would be too easy\r
1641                                                 // otherwise). The new start can't be after the end (W3C says it can).\r
1642                                                 // So, let's create a new range and collapse it to the desired point.\r
1643                                                 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )\r
1644                                                 {\r
1645                                                         range.collapse( 1 );\r
1646                                                         nativeRange.setEnd( range.endContainer.$, range.endOffset );\r
1647                                                 }\r
1648                                                 else\r
1649                                                         throw e;\r
1650                                         }\r
1651 \r
1652                                         // Select the range.\r
1653                                         sel.addRange( nativeRange );\r
1654                                 }\r
1655 \r
1656                                 // Don't miss selection change event for non-IEs.\r
1657                                 this.document.fire( 'selectionchange' );\r
1658                                 this.reset();\r
1659                         }\r
1660                 },\r
1661 \r
1662                 /**\r
1663                  *  Creates a bookmark for each range of this selection (from <code>#getRanges</code>)\r
1664                  * by calling the <code>{@link CKEDITOR.dom.range.prototype.createBookmark}</code> method,\r
1665                  * with extra care taken to avoid interference among those ranges. The arguments\r
1666                  * received are the same as with the underlying range method.\r
1667                  * @returns {Array} Array of bookmarks for each range.\r
1668                  * @example\r
1669                  * var bookmarks = editor.getSelection().<strong>createBookmarks()</strong>;\r
1670                  */\r
1671                 createBookmarks : function( serializable )\r
1672                 {\r
1673                         return this.getRanges().createBookmarks( serializable );\r
1674                 },\r
1675 \r
1676                 /**\r
1677                  *  Creates a bookmark for each range of this selection (from <code>#getRanges</code>)\r
1678                  * by calling the <code>{@link CKEDITOR.dom.range.prototype.createBookmark2}</code> method,\r
1679                  * with extra care taken to avoid interference among those ranges. The arguments\r
1680                  * received are the same as with the underlying range method.\r
1681                  * @returns {Array} Array of bookmarks for each range.\r
1682                  * @example\r
1683                  * var bookmarks = editor.getSelection().<strong>createBookmarks2()</strong>;\r
1684                  */\r
1685                 createBookmarks2 : function( normalized )\r
1686                 {\r
1687                         return this.getRanges().createBookmarks2( normalized );\r
1688                 },\r
1689 \r
1690                 /**\r
1691                  * Selects the virtual ranges denoted by the bookmarks by calling <code>#selectRanges</code>.\r
1692                  * @param {Array} bookmarks The bookmarks representing ranges to be selected.\r
1693                  * @returns {CKEDITOR.dom.selection} This selection object, after the ranges were selected.\r
1694                  * @example\r
1695                  * var bookmarks = editor.getSelection().createBookmarks();\r
1696                  * editor.getSelection().<strong>selectBookmarks( bookmarks )</strong>;\r
1697                  */\r
1698                 selectBookmarks : function( bookmarks )\r
1699                 {\r
1700                         var ranges = [];\r
1701                         for ( var i = 0 ; i < bookmarks.length ; i++ )\r
1702                         {\r
1703                                 var range = new CKEDITOR.dom.range( this.document );\r
1704                                 range.moveToBookmark( bookmarks[i] );\r
1705                                 ranges.push( range );\r
1706                         }\r
1707                         this.selectRanges( ranges );\r
1708                         return this;\r
1709                 },\r
1710 \r
1711                 /**\r
1712                  * Retrieves the common ancestor node of the first range and the last range.\r
1713                  * @returns {CKEDITOR.dom.element} The common ancestor of the selection.\r
1714                  * @example\r
1715                  * var ancestor = editor.getSelection().<strong>getCommonAncestor()</strong>;\r
1716                  */\r
1717                 getCommonAncestor : function()\r
1718                 {\r
1719                         var ranges = this.getRanges(),\r
1720                                 startNode = ranges[ 0 ].startContainer,\r
1721                                 endNode = ranges[ ranges.length - 1 ].endContainer;\r
1722                         return startNode.getCommonAncestor( endNode );\r
1723                 },\r
1724 \r
1725                 /**\r
1726                  * Moves the scrollbar to the starting position of the current selection.\r
1727                  * @example\r
1728                  * editor.getSelection().<strong>scrollIntoView()</strong>;\r
1729                  */\r
1730                 scrollIntoView : function()\r
1731                 {\r
1732                         // If we have split the block, adds a temporary span at the\r
1733                         // range position and scroll relatively to it.\r
1734                         var start = this.getStartElement();\r
1735                         start.scrollIntoView();\r
1736                 }\r
1737         };\r
1738 })();\r
1739 \r
1740 ( function()\r
1741 {\r
1742         var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),\r
1743                         fillerTextRegex = /\ufeff|\u00a0/,\r
1744                         nonCells = { table:1,tbody:1,tr:1 };\r
1745 \r
1746         CKEDITOR.dom.range.prototype.select =\r
1747                 CKEDITOR.env.ie ?\r
1748                         // V2\r
1749                         function( forceExpand )\r
1750                         {\r
1751                                 var collapsed = this.collapsed,\r
1752                                         isStartMarkerAlone, dummySpan, ieRange;\r
1753 \r
1754                                 // Try to make a object selection.\r
1755                                 var selected = this.getEnclosedNode();\r
1756                                 if ( selected )\r
1757                                 {\r
1758                                         try\r
1759                                         {\r
1760                                                 ieRange = this.document.$.body.createControlRange();\r
1761                                                 ieRange.addElement( selected.$ );\r
1762                                                 ieRange.select();\r
1763                                                 return;\r
1764                                         }\r
1765                                         catch( er ) {}\r
1766                                 }\r
1767 \r
1768                                 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.\r
1769                                 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...\r
1770                                 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells\r
1771                                         || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )\r
1772                                 {\r
1773                                         this.shrink( CKEDITOR.NODE_ELEMENT, true );\r
1774                                 }\r
1775 \r
1776                                 var bookmark = this.createBookmark();\r
1777 \r
1778                                 // Create marker tags for the start and end boundaries.\r
1779                                 var startNode = bookmark.startNode;\r
1780 \r
1781                                 var endNode;\r
1782                                 if ( !collapsed )\r
1783                                         endNode = bookmark.endNode;\r
1784 \r
1785                                 // Create the main range which will be used for the selection.\r
1786                                 ieRange = this.document.$.body.createTextRange();\r
1787 \r
1788                                 // Position the range at the start boundary.\r
1789                                 ieRange.moveToElementText( startNode.$ );\r
1790                                 ieRange.moveStart( 'character', 1 );\r
1791 \r
1792                                 if ( endNode )\r
1793                                 {\r
1794                                         // Create a tool range for the end.\r
1795                                         var ieRangeEnd = this.document.$.body.createTextRange();\r
1796 \r
1797                                         // Position the tool range at the end.\r
1798                                         ieRangeEnd.moveToElementText( endNode.$ );\r
1799 \r
1800                                         // Move the end boundary of the main range to match the tool range.\r
1801                                         ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );\r
1802                                         ieRange.moveEnd( 'character', -1 );\r
1803                                 }\r
1804                                 else\r
1805                                 {\r
1806                                         // The isStartMarkerAlone logic comes from V2. It guarantees that the lines\r
1807                                         // will expand and that the cursor will be blinking on the right place.\r
1808                                         // Actually, we are using this flag just to avoid using this hack in all\r
1809                                         // situations, but just on those needed.\r
1810                                         var next = startNode.getNext( notWhitespaces );\r
1811                                         isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) )     // already a filler there?\r
1812                                                                                   && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );\r
1813 \r
1814                                         // Append a temporary <span>&#65279;</span> before the selection.\r
1815                                         // This is needed to avoid IE destroying selections inside empty\r
1816                                         // inline elements, like <b></b> (#253).\r
1817                                         // It is also needed when placing the selection right after an inline\r
1818                                         // element to avoid the selection moving inside of it.\r
1819                                         dummySpan = this.document.createElement( 'span' );\r
1820                                         dummySpan.setHtml( '&#65279;' );        // Zero Width No-Break Space (U+FEFF). See #1359.\r
1821                                         dummySpan.insertBefore( startNode );\r
1822 \r
1823                                         if ( isStartMarkerAlone )\r
1824                                         {\r
1825                                                 // To expand empty blocks or line spaces after <br>, we need\r
1826                                                 // instead to have any char, which will be later deleted using the\r
1827                                                 // selection.\r
1828                                                 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)\r
1829                                                 this.document.createText( '\ufeff' ).insertBefore( startNode );\r
1830                                         }\r
1831                                 }\r
1832 \r
1833                                 // Remove the markers (reset the position, because of the changes in the DOM tree).\r
1834                                 this.setStartBefore( startNode );\r
1835                                 startNode.remove();\r
1836 \r
1837                                 if ( collapsed )\r
1838                                 {\r
1839                                         if ( isStartMarkerAlone )\r
1840                                         {\r
1841                                                 // Move the selection start to include the temporary \ufeff.\r
1842                                                 ieRange.moveStart( 'character', -1 );\r
1843 \r
1844                                                 ieRange.select();\r
1845 \r
1846                                                 // Remove our temporary stuff.\r
1847                                                 this.document.$.selection.clear();\r
1848                                         }\r
1849                                         else\r
1850                                                 ieRange.select();\r
1851 \r
1852                                         this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );\r
1853                                         dummySpan.remove();\r
1854                                 }\r
1855                                 else\r
1856                                 {\r
1857                                         this.setEndBefore( endNode );\r
1858                                         endNode.remove();\r
1859                                         ieRange.select();\r
1860                                 }\r
1861 \r
1862                                 this.document.fire( 'selectionchange' );\r
1863                         }\r
1864                 :\r
1865                         function()\r
1866                         {\r
1867                                 this.document.getSelection().selectRanges( [ this ] );\r
1868                         };\r
1869 } )();\r