2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 // #### checkSelectionChange : START
\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
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
23 var firstElement = sel.getStartElement();
\r
24 var currentPath = new CKEDITOR.dom.elementPath( firstElement );
\r
26 if ( !currentPath.compare( this._.selectionPreviousPath ) )
\r
28 this._.selectionPreviousPath = currentPath;
\r
29 this.fire( 'selectionChange', { selection : sel, path : currentPath, element : firstElement } );
\r
36 var checkSelectionChangeTimer,
\r
37 checkSelectionChangeTimeoutPending;
\r
39 function checkSelectionChangeTimeout()
\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
45 checkSelectionChangeTimeoutPending = true;
\r
47 if ( checkSelectionChangeTimer )
\r
50 checkSelectionChangeTimeoutExec.call( this );
\r
52 checkSelectionChangeTimer = CKEDITOR.tools.setTimeout( checkSelectionChangeTimeoutExec, 200, this );
\r
55 function checkSelectionChangeTimeoutExec()
\r
57 checkSelectionChangeTimer = null;
\r
59 if ( checkSelectionChangeTimeoutPending )
\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
65 CKEDITOR.tools.setTimeout( checkSelectionChange, 0, this );
\r
67 checkSelectionChangeTimeoutPending = false;
\r
71 // #### checkSelectionChange : END
\r
73 function rangeRequiresFix( range )
\r
75 function isInlineCt( node )
\r
77 return node && node.type == CKEDITOR.NODE_ELEMENT
\r
78 && node.getName() in CKEDITOR.dtd.$removeEmpty;
\r
81 function singletonBlock( node )
\r
83 var body = range.document.getBody();
\r
84 return !node.is( 'body' ) && body.getChildCount() == 1;
\r
87 var start = range.startContainer,
\r
88 offset = range.startOffset;
\r
90 if ( start.type == CKEDITOR.NODE_TEXT )
\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
102 modes : { wysiwyg : 1, source : 1 },
\r
103 readOnly : CKEDITOR.env.ie || CKEDITOR.env.webkit,
\r
104 exec : function( editor )
\r
106 switch ( editor.mode )
\r
109 editor.document.$.execCommand( 'SelectAll', false, null );
\r
110 // Force triggering selectionChange (#7008)
\r
111 editor.forceNextSelectionCheck();
\r
112 editor.selectionChange();
\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
121 textarea.selectionStart = 0;
\r
122 textarea.selectionEnd = textarea.value.length;
\r
130 function createFillingChar( doc )
\r
132 removeFillingChar( doc );
\r
134 var fillingChar = doc.createText( '\u200B' );
\r
135 doc.setCustomData( 'cke-fillingChar', fillingChar );
\r
137 return fillingChar;
\r
140 function getFillingChar( doc )
\r
142 return doc && doc.getCustomData( 'cke-fillingChar' );
\r
145 // Checks if a filling char has been used, eventualy removing it (#1272).
\r
146 function checkFillingChar( doc )
\r
148 var fillingChar = doc && getFillingChar( doc );
\r
151 // Use this flag to avoid removing the filling char right after
\r
153 if ( fillingChar.getCustomData( 'ready' ) )
\r
154 removeFillingChar( doc );
\r
156 fillingChar.setCustomData( 'ready', 1 );
\r
160 function removeFillingChar( doc )
\r
162 var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' );
\r
166 sel = doc.getSelection().getNative(),
\r
168 range = sel && sel.type != 'None' && sel.getRangeAt( 0 );
\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
175 bm = [ sel.anchorOffset, sel.focusOffset ];
\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
183 // Revert the bookmark order on reverse selection.
\r
184 isReversedSelection( sel ) && bm.unshift( bm.pop() );
\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
192 // Restore the bookmark.
\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
204 function isReversedSelection( sel )
\r
206 if ( !sel.isCollapsed )
\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
216 CKEDITOR.plugins.add( 'selection',
\r
218 init : function( editor )
\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
224 editor.on( 'selectionChange', function() { checkFillingChar( editor.document ); } );
\r
225 editor.on( 'beforeSetMode', function() { removeFillingChar( editor.document ); } );
\r
227 var fillingCharBefore,
\r
230 function beforeData()
\r
232 var doc = editor.document,
\r
233 fillingChar = getFillingChar( doc );
\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
243 fillingCharBefore = fillingChar.getText();
\r
244 fillingChar.setText( fillingCharBefore.replace( /\u200B/g, '' ) );
\r
247 function afterData()
\r
249 var doc = editor.document,
\r
250 fillingChar = getFillingChar( doc );
\r
254 fillingChar.setText( fillingCharBefore );
\r
256 if ( resetSelection )
\r
258 doc.$.defaultView.getSelection().setPosition( fillingChar.$,fillingChar.getLength() );
\r
259 resetSelection = 0;
\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
269 editor.on( 'contentDom', function()
\r
271 var doc = editor.document,
\r
272 body = doc.getBody(),
\r
273 html = doc.getDocumentElement();
\r
275 if ( CKEDITOR.env.ie )
\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
284 restoreEnabled = 1;
\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
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
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
300 var lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
301 if ( lockedSelection )
\r
303 lockedSelection.unlock( 1 );
\r
304 lockedSelection.lock();
\r
306 // Then check ff we have saved a range, restore it at this
\r
308 else if ( savedRange && restoreEnabled )
\r
310 // Well not break because of this.
\r
311 try { savedRange.select(); } catch (e) {}
\r
316 body.on( 'focus', function()
\r
318 // Enable selections to be saved.
\r
324 body.on( 'beforedeactivate', function( evt )
\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
331 // Disable selections from being saved.
\r
333 restoreEnabled = 1;
\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
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
348 // Error proof when the editor is not visible. (#6375)
\r
349 try{ doc.$.selection.empty(); } catch ( er){}
\r
352 // Listening on document element ensures that
\r
353 // scrollbar is included. (#5280)
\r
354 html.on( 'mousedown', function()
\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
362 html.on( 'mouseup', function()
\r
364 restoreEnabled = 1;
\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
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
377 var sel = editor.document.$.selection;
\r
378 if ( sel.type == 'None' )
\r
379 scroll = editor.window.getScrollPosition();
\r
384 body.on( 'mouseup',
\r
387 // Restore recorded scroll position when needed on right mouseup.
\r
388 if ( evt.data.$.button == 2 && scroll )
\r
390 editor.document.$.documentElement.scrollLeft = scroll.x;
\r
391 editor.document.$.documentElement.scrollTop = scroll.y;
\r
396 setTimeout( function()
\r
398 saveSelection( true );
\r
403 body.on( 'keydown', disableSave );
\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
417 function moveRangeToPoint( range, x, y )
\r
419 // Error prune in IE7. (#9034, #9110)
\r
420 try { range.moveToPoint( x, y ); } catch ( e ) {}
\r
423 html.on( 'mousedown', function( evt )
\r
425 // Expand the text range along with mouse move.
\r
426 function onHover( evt )
\r
431 // Read the current cursor.
\r
432 var rngEnd = body.$.createTextRange();
\r
434 moveRangeToPoint( rngEnd, evt.x, evt.y );
\r
436 // Handle drag directions.
\r
437 textRng.setEndPoint(
\r
438 textRng.compareEndPoints( 'StartToStart', rngEnd ) < 0 ?
\r
443 // Update selection with new range.
\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
456 // Start to build the text range.
\r
457 var textRng = body.$.createTextRange();
\r
458 moveRangeToPoint( textRng, evt.x, evt.y );
\r
460 html.on( 'mousemove', onHover );
\r
462 html.on( 'mouseup', function( evt )
\r
464 html.removeListener( 'mousemove', onHover );
\r
465 evt.removeListener();
\r
467 // Make it in effect on mouse up. (#9022)
\r
474 // It's much simpler for IE8, we just need to reselect the reported range.
\r
475 if ( CKEDITOR.env.ie8 )
\r
477 html.on( 'mouseup', function( evt )
\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
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
493 // IE is the only to provide the "selectionchange"
\r
495 doc.on( 'selectionchange', saveSelection );
\r
497 function disableSave()
\r
502 function saveSelection( testIt )
\r
506 var doc = editor.document,
\r
507 sel = editor.getSelection(),
\r
508 nativeSel = sel && sel.getNative();
\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
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
526 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
531 // Avoid saving selection from within text input. (#5747)
\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
542 savedRange = nativeSel && sel.getRanges()[ 0 ];
\r
544 checkSelectionChangeTimeout.call( editor );
\r
550 // In other browsers, we make the selection change
\r
551 // check based on other events, like clicks or keys
\r
554 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
555 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
556 doc.on( 'selectionchange', checkSelectionChangeTimeout, editor );
\r
559 if ( CKEDITOR.env.webkit )
\r
561 doc.on( 'keydown', function( evt )
\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
569 case 33 : // PAGEUP
\r
570 case 34 : // PAGEDOWN
\r
573 case 37 : // LEFT-ARROW
\r
574 case 39 : // RIGHT-ARROW
\r
575 case 8 : // BACKSPACE
\r
578 removeFillingChar( editor.document );
\r
581 }, null, null, 10 );
\r
585 // Clear the cached range path before unload. (#7174)
\r
586 editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );
\r
588 editor.addCommand( 'selectAll', selectAllCmd );
\r
589 editor.ui.addButton( 'SelectAll',
\r
591 label : editor.lang.selectAll,
\r
592 command : 'selectAll'
\r
596 * Check if to fire the {@link CKEDITOR.editor#selectionChange} event
\r
597 * for the current editor instance.
\r
599 * @param {Boolean} checkNow Check immediately without any delay.
\r
601 editor.selectionChange = function( checkNow )
\r
603 ( checkNow ? checkSelectionChange : checkSelectionChangeTimeout ).call( this );
\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
609 var sel = editor.getSelection();
\r
610 sel && sel.getNative().clear();
\r
611 }, null, null, 9 );
\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
620 * var selection = CKEDITOR.instances.editor1.<strong>getSelection()</strong>;
\r
621 * alert( selection.getType() );
\r
623 CKEDITOR.editor.prototype.getSelection = function()
\r
625 return this.document && this.document.getSelection();
\r
628 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
630 delete this._.selectionPreviousPath;
\r
634 * Gets the current selection from the document.
\r
635 * @returns {CKEDITOR.dom.selection} A selection object.
\r
637 * var selection = CKEDITOR.instances.editor1.document.<strong>getSelection()</strong>;
\r
638 * alert( selection.getType() );
\r
640 CKEDITOR.dom.document.prototype.getSelection = function()
\r
642 var sel = new CKEDITOR.dom.selection( this );
\r
643 return ( !sel || sel.isInvalid ) ? null : sel;
\r
650 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
651 * alert( 'Nothing is selected' );
\r
653 CKEDITOR.SELECTION_NONE = 1;
\r
656 * A text or a collapsed selection.
\r
659 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
660 * alert( 'A text is selected' );
\r
662 CKEDITOR.SELECTION_TEXT = 2;
\r
665 * Element selection.
\r
668 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
669 * alert( 'An element is selected' );
\r
671 CKEDITOR.SELECTION_ELEMENT = 3;
\r
674 * Manipulates the selection in a DOM document.
\r
676 * @param {CKEDITOR.dom.document} document The DOM document that contains the selection.
\r
678 * var sel = new <strong>CKEDITOR.dom.selection( CKEDITOR.document )</strong>;
\r
680 CKEDITOR.dom.selection = function( document )
\r
682 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
684 if ( lockedSelection )
\r
685 return lockedSelection;
\r
687 this.document = document;
\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
698 if ( CKEDITOR.env.ie )
\r
700 // Avoid breaking because of it. (#8836)
\r
703 var range = this.getNative().createRange();
\r
705 ( range.item && range.item( 0 ).ownerDocument != this.document.$ ) ||
\r
706 ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
713 this.isInvalid = true;
\r
720 var styleObjectElements =
\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
726 CKEDITOR.dom.selection.prototype =
\r
729 * Gets the native selection object from the browser.
\r
731 * @returns {Object} The native browser selection object.
\r
733 * var selection = editor.getSelection().<strong>getNative()</strong>;
\r
739 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
744 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
748 * Gets the type of the current selection. The following values are
\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
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
762 * if ( editor.getSelection().<strong>getType()</strong> == CKEDITOR.SELECTION_TEXT )
\r
763 * alert( 'A text is selected' );
\r
769 var cache = this._.cache;
\r
773 var type = CKEDITOR.SELECTION_NONE;
\r
777 var sel = this.getNative(),
\r
780 if ( ieType == 'Text' )
\r
781 type = CKEDITOR.SELECTION_TEXT;
\r
783 if ( ieType == 'Control' )
\r
784 type = CKEDITOR.SELECTION_ELEMENT;
\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
795 return ( cache.type = type );
\r
800 var cache = this._.cache;
\r
804 var type = CKEDITOR.SELECTION_TEXT;
\r
806 var sel = this.getNative();
\r
809 type = CKEDITOR.SELECTION_NONE;
\r
810 else if ( sel.rangeCount == 1 )
\r
812 // Check if the actual selection is a control (IMG,
\r
813 // TABLE, HR, etc...).
\r
815 var range = sel.getRangeAt(0),
\r
816 startContainer = range.startContainer;
\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
823 type = CKEDITOR.SELECTION_ELEMENT;
\r
827 return ( cache.type = type );
\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
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
838 * var ranges = selection.<strong>getRanges()</strong>;
\r
839 * alert( ranges.length );
\r
841 getRanges : (function()
\r
843 var func = CKEDITOR.env.ie ?
\r
846 function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }
\r
848 // Finds the container and offset for a specific boundary
\r
850 var getBoundaryInformation = function( range, start )
\r
852 // Creates a collapsed range at the requested boundary.
\r
853 range = range.duplicate();
\r
854 range.collapse( start );
\r
856 // Gets the element that encloses the range entirely.
\r
857 var parent = range.parentElement(),
\r
858 doc = parent.ownerDocument;
\r
860 // Empty parent element, e.g. <i>^</i>
\r
861 if ( !parent.hasChildNodes() )
\r
862 return { container : parent, offset : 0 };
\r
864 var siblings = parent.children,
\r
867 testRange = range.duplicate(),
\r
869 endIndex = siblings.length - 1,
\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
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
884 if ( position > 0 )
\r
885 endIndex = index - 1;
\r
886 else if ( position < 0 )
\r
887 startIndex = index + 1;
\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
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
900 return { container : parent, offset : getNodeIndex( child ) };
\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
908 // Adapt test range to embrace the entire parent contents.
\r
909 testRange.moveToElementText( parent );
\r
910 testRange.setEndPoint( 'StartToStart', range );
\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
917 siblings = parent.childNodes;
\r
919 // Actual range anchor right beside test range at the boundary of text node.
\r
922 child = siblings[ siblings.length - 1 ];
\r
924 if ( child.nodeType != CKEDITOR.NODE_TEXT )
\r
925 return { container : parent, offset : siblings.length };
\r
927 return { container : child, offset : child.nodeValue.length };
\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
934 sibling = siblings[ --i ];
\r
935 if ( sibling.nodeType == CKEDITOR.NODE_TEXT )
\r
937 container = sibling;
\r
938 distance -= sibling.nodeValue.length;
\r
942 return { container : container, offset : -distance };
\r
944 // Test range was one offset beyond OR behind the anchored text node.
\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
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
957 // Actual range anchor right beside test range at the inner boundary of text node.
\r
959 return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };
\r
961 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
962 while ( distance > 0 )
\r
966 sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];
\r
967 if ( sibling.nodeType == CKEDITOR.NODE_TEXT )
\r
969 distance -= sibling.nodeValue.length;
\r
970 container = sibling;
\r
974 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)
\r
977 return { container : parent, offset : getNodeIndex( child ) };
\r
981 return { container : container, offset : position > 0 ? -distance : container.nodeValue.length + distance };
\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
991 var sel = this.getNative(),
\r
992 nativeRange = sel && sel.createRange(),
\r
993 type = this.getType(),
\r
999 if ( type == CKEDITOR.SELECTION_TEXT )
\r
1001 range = new CKEDITOR.dom.range( this.document );
\r
1003 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
1004 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
1006 boundaryInfo = getBoundaryInformation( nativeRange );
\r
1007 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\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
1018 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
1022 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
1024 var element = nativeRange.item( i ),
\r
1025 parentElement = element.parentNode,
\r
1028 range = new CKEDITOR.dom.range( this.document );
\r
1030 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\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
1048 // On browsers implementing the W3C range, we simply
\r
1049 // tranform the native ranges in CKEDITOR.dom.range
\r
1054 doc = this.document,
\r
1055 sel = this.getNative();
\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
1065 range = new CKEDITOR.dom.range( doc );
\r
1066 range.moveToElementEditStart( doc.getBody() );
\r
1067 ranges.push( range );
\r
1070 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
1072 var nativeRange = sel.getRangeAt( i );
\r
1074 range = new CKEDITOR.dom.range( doc );
\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
1083 return function( onlyEditables )
\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
1091 // Split range into multiple by read-only nodes.
\r
1092 if ( onlyEditables )
\r
1094 var ranges = cache.ranges;
\r
1095 for ( var i = 0; i < ranges.length; i++ )
\r
1097 var range = ranges[ i ];
\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
1104 if ( range.collapsed )
\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
1111 var current = range.startContainer;
\r
1114 if ( current.is( 'body' ) || !current.isReadOnly() )
\r
1117 if ( current.type == CKEDITOR.NODE_ELEMENT
\r
1118 && current.getAttribute( 'contentEditable' ) == 'false' )
\r
1119 range.setStartAfter( current );
\r
1121 current = current.getParent();
\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
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
1136 if ( startOffset >= startContainer.getLength() )
\r
1137 walkerRange.setStartAfter( startContainer );
\r
1139 walkerRange.setStartBefore( startContainer );
\r
1142 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
1145 walkerRange.setEndBefore( endContainer );
\r
1147 walkerRange.setEndAfter( endContainer );
\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
1154 if ( node.type == CKEDITOR.NODE_ELEMENT
\r
1155 && node.isReadOnly() )
\r
1157 var newRange = range.clone();
\r
1158 range.setEndBefore( node );
\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
1166 // Avoid creating invalid range.
\r
1167 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )
\r
1169 newRange.setStartAfter( node );
\r
1170 if ( !newRange.collapsed )
\r
1171 ranges.splice( i + 1, 0, newRange );
\r
1184 return cache.ranges;
\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
1193 * var element = editor.getSelection().<strong>getStartElement()</strong>;
\r
1194 * alert( element.getName() );
\r
1196 getStartElement : function()
\r
1198 var cache = this._.cache;
\r
1199 if ( cache.startElement !== undefined )
\r
1200 return cache.startElement;
\r
1203 sel = this.getNative();
\r
1205 switch ( this.getType() )
\r
1207 case CKEDITOR.SELECTION_ELEMENT :
\r
1208 return this.getSelectedElement();
\r
1210 case CKEDITOR.SELECTION_TEXT :
\r
1212 var range = this.getRanges()[0];
\r
1216 if ( !range.collapsed )
\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
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
1235 node = range.startContainer;
\r
1237 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
1238 return node.getParent();
\r
1240 node = node.getChild( range.startOffset );
\r
1242 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
1243 node = range.startContainer;
\r
1246 var child = node.getFirst();
\r
1247 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
1250 child = child.getFirst();
\r
1256 node = range.startContainer;
\r
1257 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
1258 node = node.getParent();
\r
1265 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\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
1274 * var element = editor.getSelection().<strong>getSelectedElement()</strong>;
\r
1275 * alert( element.getName() );
\r
1277 getSelectedElement : function()
\r
1279 var cache = this._.cache;
\r
1280 if ( cache.selectedElement !== undefined )
\r
1281 return cache.selectedElement;
\r
1285 var node = CKEDITOR.tools.tryThese(
\r
1286 // Is it native IE control type selection?
\r
1289 return self.getNative().createRange().item( 0 );
\r
1291 // If a table or list is fully selected.
\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
1300 for ( var t in tags )
\r
1302 if ( ( root = ancestor.getAscendant( t, 1 ) ) )
\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
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
1318 return function( node, isWalkOut )
\r
1320 if ( node.type == CKEDITOR.NODE_TEXT && ( !CKEDITOR.tools.trim( node.getText() ) || node.getParent().data( 'cke-bookmark' ) ) )
\r
1324 if ( node.type == CKEDITOR.NODE_ELEMENT )
\r
1326 tag = node.getName();
\r
1328 // Bypass bogus br at the end of block.
\r
1329 if ( tag == 'br' && isEnd && node.equals( node.getParent().getBogus() ) )
\r
1332 if ( isWalkOut && tag in enlargeables || tag in CKEDITOR.dtd.$removeEmpty )
\r
1336 walker.halted = 1;
\r
1341 walker.guard = guard( walker );
\r
1343 if ( walker.checkBackward() && !walker.halted )
\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
1359 // Figure it out by checking if there's a single enclosed
\r
1360 // node of the range.
\r
1363 var range = self.getRanges()[ 0 ],
\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
1373 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]
\r
1374 range.shrink( CKEDITOR.SHRINK_ELEMENT );
\r
1377 return selected.$;
\r
1380 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\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
1388 * var text = editor.getSelection().<strong>getSelectedText()</strong>;
\r
1391 getSelectedText : function()
\r
1393 var cache = this._.cache;
\r
1394 if ( cache.selectedText !== undefined )
\r
1395 return cache.selectedText;
\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
1402 return ( cache.selectedText = text );
\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
1411 * editor.getSelection().<strong>lock()</strong>;
\r
1415 // Call all cacheable function.
\r
1417 this.getStartElement();
\r
1418 this.getSelectedElement();
\r
1419 this.getSelectedText();
\r
1421 // The native selection is not available when locked.
\r
1422 this._.cache.nativeSel = {};
\r
1424 this.isLocked = 1;
\r
1426 // Save this selection inside the DOM document.
\r
1427 this.document.setCustomData( 'cke_locked_selection', this );
\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
1435 * editor.getSelection().<strong>unlock()</strong>;
\r
1437 unlock : function( restore )
\r
1439 var doc = this.document,
\r
1440 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
1442 if ( lockedSelection )
\r
1444 doc.setCustomData( 'cke_locked_selection', null );
\r
1448 var selectedElement = lockedSelection.getSelectedElement(),
\r
1449 ranges = !selectedElement && lockedSelection.getRanges();
\r
1451 this.isLocked = 0;
\r
1454 if ( selectedElement )
\r
1455 this.selectElement( selectedElement );
\r
1457 this.selectRanges( ranges );
\r
1461 if ( !lockedSelection || !restore )
\r
1463 this.isLocked = 0;
\r
1469 * Clears the selection cache.
\r
1471 * editor.getSelection().<strong>reset()</strong>;
\r
1473 reset : function()
\r
1475 this._.cache = {};
\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
1482 * var element = editor.document.getById( 'sampleElement' );
\r
1483 * editor.getSelection.<strong>selectElement( element )</strong>;
\r
1485 selectElement : function( element )
\r
1487 if ( this.isLocked )
\r
1489 var range = new CKEDITOR.dom.range( this.document );
\r
1490 range.setStartBefore( element );
\r
1491 range.setEndAfter( element );
\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
1501 range = new CKEDITOR.dom.range( element.getDocument() );
\r
1502 range.setStartBefore( element );
\r
1503 range.setEndAfter( element );
\r
1506 this.document.fire( 'selectionchange' );
\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
1516 * var ranges = new CKEDITOR.dom.range( editor.document );
\r
1517 * editor.getSelection().<strong>selectRanges( [ ranges ] )</strong>;
\r
1519 selectRanges : function( ranges )
\r
1521 if ( this.isLocked )
\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
1531 if ( CKEDITOR.env.ie )
\r
1533 if ( ranges.length > 1 )
\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
1541 if ( ranges[ 0 ] )
\r
1542 ranges[ 0 ].select();
\r
1548 var sel = this.getNative();
\r
1550 // getNative() returns null if iframe is "display:none" in FF. (#6577)
\r
1554 if ( ranges.length )
\r
1556 sel.removeAllRanges();
\r
1557 // Remove any existing filling char first.
\r
1558 CKEDITOR.env.webkit && removeFillingChar( this.document );
\r
1561 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1563 // Joining sequential ranges introduced by
\r
1564 // readonly elements protection.
\r
1565 if ( i < ranges.length -1 )
\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
1572 // Don't confused by Firefox adjancent multi-ranges
\r
1573 // introduced by table cells selection.
\r
1574 if ( !between.collapsed )
\r
1576 between.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1577 var ancestor = between.getCommonAncestor(),
\r
1578 enclosed = between.getEnclosedNode();
\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
1585 right.setStart( left.startContainer, left.startOffset );
\r
1586 ranges.splice( i--, 1 );
\r
1592 var range = ranges[ i ];
\r
1593 var nativeRange = this.document.$.createRange();
\r
1594 var startContainer = range.startContainer;
\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
1606 startContainer.appendText( '' );
\r
1609 if ( range.collapsed
\r
1610 && CKEDITOR.env.webkit
\r
1611 && rangeRequiresFix( range ) )
\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
1618 var next = fillingChar.getNext();
\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
1625 removeFillingChar( this.document );
\r
1626 range.moveToPosition( next, CKEDITOR.POSITION_BEFORE_START );
\r
1629 range.moveToPosition( fillingChar, CKEDITOR.POSITION_AFTER_END );
\r
1632 nativeRange.setStart( range.startContainer.$, range.startOffset );
\r
1636 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\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
1645 range.collapse( 1 );
\r
1646 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1652 // Select the range.
\r
1653 sel.addRange( nativeRange );
\r
1656 // Don't miss selection change event for non-IEs.
\r
1657 this.document.fire( 'selectionchange' );
\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
1669 * var bookmarks = editor.getSelection().<strong>createBookmarks()</strong>;
\r
1671 createBookmarks : function( serializable )
\r
1673 return this.getRanges().createBookmarks( serializable );
\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
1683 * var bookmarks = editor.getSelection().<strong>createBookmarks2()</strong>;
\r
1685 createBookmarks2 : function( normalized )
\r
1687 return this.getRanges().createBookmarks2( normalized );
\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
1695 * var bookmarks = editor.getSelection().createBookmarks();
\r
1696 * editor.getSelection().<strong>selectBookmarks( bookmarks )</strong>;
\r
1698 selectBookmarks : function( bookmarks )
\r
1701 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
1703 var range = new CKEDITOR.dom.range( this.document );
\r
1704 range.moveToBookmark( bookmarks[i] );
\r
1705 ranges.push( range );
\r
1707 this.selectRanges( ranges );
\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
1715 * var ancestor = editor.getSelection().<strong>getCommonAncestor()</strong>;
\r
1717 getCommonAncestor : function()
\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
1726 * Moves the scrollbar to the starting position of the current selection.
\r
1728 * editor.getSelection().<strong>scrollIntoView()</strong>;
\r
1730 scrollIntoView : function()
\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
1742 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1743 fillerTextRegex = /\ufeff|\u00a0/,
\r
1744 nonCells = { table:1,tbody:1,tr:1 };
\r
1746 CKEDITOR.dom.range.prototype.select =
\r
1749 function( forceExpand )
\r
1751 var collapsed = this.collapsed,
\r
1752 isStartMarkerAlone, dummySpan, ieRange;
\r
1754 // Try to make a object selection.
\r
1755 var selected = this.getEnclosedNode();
\r
1760 ieRange = this.document.$.body.createControlRange();
\r
1761 ieRange.addElement( selected.$ );
\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
1773 this.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1776 var bookmark = this.createBookmark();
\r
1778 // Create marker tags for the start and end boundaries.
\r
1779 var startNode = bookmark.startNode;
\r
1783 endNode = bookmark.endNode;
\r
1785 // Create the main range which will be used for the selection.
\r
1786 ieRange = this.document.$.body.createTextRange();
\r
1788 // Position the range at the start boundary.
\r
1789 ieRange.moveToElementText( startNode.$ );
\r
1790 ieRange.moveStart( 'character', 1 );
\r
1794 // Create a tool range for the end.
\r
1795 var ieRangeEnd = this.document.$.body.createTextRange();
\r
1797 // Position the tool range at the end.
\r
1798 ieRangeEnd.moveToElementText( endNode.$ );
\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
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
1814 // Append a temporary <span></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( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1821 dummySpan.insertBefore( startNode );
\r
1823 if ( isStartMarkerAlone )
\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
1828 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1829 this.document.createText( '\ufeff' ).insertBefore( startNode );
\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
1839 if ( isStartMarkerAlone )
\r
1841 // Move the selection start to include the temporary \ufeff.
\r
1842 ieRange.moveStart( 'character', -1 );
\r
1846 // Remove our temporary stuff.
\r
1847 this.document.$.selection.clear();
\r
1852 this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );
\r
1853 dummySpan.remove();
\r
1857 this.setEndBefore( endNode );
\r
1862 this.document.fire( 'selectionchange' );
\r
1867 this.document.getSelection().selectRanges( [ this ] );
\r