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 html.on( 'mousedown', function( evt )
\r
421 // Expand the text range along with mouse move.
\r
422 function onHover( evt )
\r
427 // Read the current cursor.
\r
428 var rngEnd = body.$.createTextRange();
\r
429 rngEnd.moveToPoint( evt.x, evt.y );
\r
431 // Handle drag directions.
\r
432 textRng.setEndPoint(
\r
433 textRng.compareEndPoints( 'StartToStart', rngEnd ) < 0 ?
\r
438 // Update selection with new range.
\r
443 // We're sure that the click happens at the region
\r
444 // below body, but not on scrollbar.
\r
445 if ( evt.y < html.$.clientHeight
\r
446 && evt.y > body.$.offsetTop + body.$.clientHeight
\r
447 && evt.x < html.$.clientWidth )
\r
449 // Start to build the text range.
\r
450 var textRng = body.$.createTextRange();
\r
451 textRng.moveToPoint( evt.x, evt.y );
\r
454 html.on( 'mousemove', onHover );
\r
456 html.on( 'mouseup', function( evt )
\r
458 html.removeListener( 'mousemove', onHover );
\r
459 evt.removeListener();
\r
467 // It's much simpler for IE8, we just need to reselect the reported range.
\r
468 if ( CKEDITOR.env.ie8 )
\r
470 html.on( 'mouseup', function( evt )
\r
472 // The event is not fired when clicking on the scrollbars,
\r
473 // so we can safely check the following to understand
\r
474 // whether the empty space following <body> has been clicked.
\r
475 if ( evt.data.getTarget().getName() == 'html' )
\r
477 var sel = CKEDITOR.document.$.selection,
\r
478 range = sel.createRange();
\r
479 // The selection range is reported on host, but actually it should applies to the content doc.
\r
480 if ( sel.type != 'None' && range.parentElement().ownerDocument == doc.$ )
\r
486 // IE is the only to provide the "selectionchange"
\r
488 doc.on( 'selectionchange', saveSelection );
\r
490 function disableSave()
\r
495 function saveSelection( testIt )
\r
499 var doc = editor.document,
\r
500 sel = editor.getSelection(),
\r
501 nativeSel = sel && sel.getNative();
\r
503 // There is a very specific case, when clicking
\r
504 // inside a text selection. In that case, the
\r
505 // selection collapses at the clicking point,
\r
506 // but the selection object remains in an
\r
507 // unknown state, making createRange return a
\r
508 // range at the very start of the document. In
\r
509 // such situation we have to test the range, to
\r
510 // be sure it's valid.
\r
511 if ( testIt && nativeSel && nativeSel.type == 'None' )
\r
513 // The "InsertImage" command can be used to
\r
514 // test whether the selection is good or not.
\r
515 // If not, it's enough to give some time to
\r
516 // IE to put things in order for us.
\r
517 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
519 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
524 // Avoid saving selection from within text input. (#5747)
\r
526 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
\r
527 && ( parentTag = nativeSel.createRange() )
\r
528 && ( parentTag = parentTag.parentElement() )
\r
529 && ( parentTag = parentTag.nodeName )
\r
530 && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
\r
535 savedRange = nativeSel && sel.getRanges()[ 0 ];
\r
537 checkSelectionChangeTimeout.call( editor );
\r
543 // In other browsers, we make the selection change
\r
544 // check based on other events, like clicks or keys
\r
547 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
548 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
549 doc.on( 'selectionchange', checkSelectionChangeTimeout, editor );
\r
552 if ( CKEDITOR.env.webkit )
\r
554 doc.on( 'keydown', function( evt )
\r
556 var key = evt.data.getKey();
\r
557 // Remove the filling char before some keys get
\r
558 // executed, so they'll not get blocked by it.
\r
562 case 33 : // PAGEUP
\r
563 case 34 : // PAGEDOWN
\r
566 case 37 : // LEFT-ARROW
\r
567 case 39 : // RIGHT-ARROW
\r
568 case 8 : // BACKSPACE
\r
571 removeFillingChar( editor.document );
\r
574 }, null, null, 10 );
\r
578 // Clear the cached range path before unload. (#7174)
\r
579 editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );
\r
581 editor.addCommand( 'selectAll', selectAllCmd );
\r
582 editor.ui.addButton( 'SelectAll',
\r
584 label : editor.lang.selectAll,
\r
585 command : 'selectAll'
\r
588 editor.selectionChange = checkSelectionChangeTimeout;
\r
590 // IE9 might cease to work if there's an object selection inside the iframe (#7639).
\r
591 CKEDITOR.env.ie9Compat && editor.on( 'destroy', function()
\r
593 var sel = editor.getSelection();
\r
594 sel && sel.getNative().clear();
\r
595 }, null, null, 9 );
\r
600 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
601 * @returns {CKEDITOR.dom.selection} A selection object or null if not in
\r
602 * WYSIWYG mode or no selection is available.
\r
604 * var selection = CKEDITOR.instances.editor1.<strong>getSelection()</strong>;
\r
605 * alert( selection.getType() );
\r
607 CKEDITOR.editor.prototype.getSelection = function()
\r
609 return this.document && this.document.getSelection();
\r
612 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
614 delete this._.selectionPreviousPath;
\r
618 * Gets the current selection from the document.
\r
619 * @returns {CKEDITOR.dom.selection} A selection object.
\r
621 * var selection = CKEDITOR.instances.editor1.document.<strong>getSelection()</strong>;
\r
622 * alert( selection.getType() );
\r
624 CKEDITOR.dom.document.prototype.getSelection = function()
\r
626 var sel = new CKEDITOR.dom.selection( this );
\r
627 return ( !sel || sel.isInvalid ) ? null : sel;
\r
634 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
635 * alert( 'Nothing is selected' );
\r
637 CKEDITOR.SELECTION_NONE = 1;
\r
640 * A text or a collapsed selection.
\r
643 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
644 * alert( 'A text is selected' );
\r
646 CKEDITOR.SELECTION_TEXT = 2;
\r
649 * Element selection.
\r
652 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
653 * alert( 'An element is selected' );
\r
655 CKEDITOR.SELECTION_ELEMENT = 3;
\r
658 * Manipulates the selection in a DOM document.
\r
660 * @param {CKEDITOR.dom.document} document The DOM document that contains the selection.
\r
662 * var sel = new <strong>CKEDITOR.dom.selection( CKEDITOR.document )</strong>;
\r
664 CKEDITOR.dom.selection = function( document )
\r
666 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
668 if ( lockedSelection )
\r
669 return lockedSelection;
\r
671 this.document = document;
\r
679 * IE BUG: The selection's document may be a different document than the
\r
680 * editor document. Return null if that is the case.
\r
682 if ( CKEDITOR.env.ie )
\r
684 // Avoid breaking because of it. (#8836)
\r
687 var range = this.getNative().createRange();
\r
689 ( range.item && range.item( 0 ).ownerDocument != this.document.$ ) ||
\r
690 ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
697 this.isInvalid = true;
\r
704 var styleObjectElements =
\r
706 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
\r
707 a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1
\r
710 CKEDITOR.dom.selection.prototype =
\r
713 * Gets the native selection object from the browser.
\r
715 * @returns {Object} The native browser selection object.
\r
717 * var selection = editor.getSelection().<strong>getNative()</strong>;
\r
723 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
728 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
732 * Gets the type of the current selection. The following values are
\r
735 * <li><code>{@link CKEDITOR.SELECTION_NONE}</code> (1): No selection.</li>
\r
736 * <li><code>{@link CKEDITOR.SELECTION_TEXT}</code> (2): A text or a collapsed
\r
737 * selection is selected.</li>
\r
738 * <li><code>{@link CKEDITOR.SELECTION_ELEMENT}</code> (3): An element is
\r
742 * @returns {Number} One of the following constant values:
\r
743 * <code>{@link CKEDITOR.SELECTION_NONE}</code>, <code>{@link CKEDITOR.SELECTION_TEXT}</code>, or
\r
744 * <code>{@link CKEDITOR.SELECTION_ELEMENT}</code>.
\r
746 * if ( editor.getSelection().<strong>getType()</strong> == CKEDITOR.SELECTION_TEXT )
\r
747 * alert( 'A text is selected' );
\r
753 var cache = this._.cache;
\r
757 var type = CKEDITOR.SELECTION_NONE;
\r
761 var sel = this.getNative(),
\r
764 if ( ieType == 'Text' )
\r
765 type = CKEDITOR.SELECTION_TEXT;
\r
767 if ( ieType == 'Control' )
\r
768 type = CKEDITOR.SELECTION_ELEMENT;
\r
770 // It is possible that we can still get a text range
\r
771 // object even when type == 'None' is returned by IE.
\r
772 // So we'd better check the object returned by
\r
773 // createRange() rather than by looking at the type.
\r
774 if ( sel.createRange().parentElement )
\r
775 type = CKEDITOR.SELECTION_TEXT;
\r
779 return ( cache.type = type );
\r
784 var cache = this._.cache;
\r
788 var type = CKEDITOR.SELECTION_TEXT;
\r
790 var sel = this.getNative();
\r
793 type = CKEDITOR.SELECTION_NONE;
\r
794 else if ( sel.rangeCount == 1 )
\r
796 // Check if the actual selection is a control (IMG,
\r
797 // TABLE, HR, etc...).
\r
799 var range = sel.getRangeAt(0),
\r
800 startContainer = range.startContainer;
\r
802 if ( startContainer == range.endContainer
\r
803 && startContainer.nodeType == 1
\r
804 && ( range.endOffset - range.startOffset ) == 1
\r
805 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
807 type = CKEDITOR.SELECTION_ELEMENT;
\r
811 return ( cache.type = type );
\r
815 * Retrieves the <code>{@link CKEDITOR.dom.range}</code> instances that represent the current selection.
\r
816 * Note: Some browsers return multiple ranges even for a continuous selection. Firefox, for example, returns
\r
817 * one range for each table cell when one or more table rows are selected.
\r
819 * @param {Boolean} [onlyEditables] If set to <code>true</code>, this function retrives editable ranges only.
\r
820 * @return {Array} Range instances that represent the current selection.
\r
822 * var ranges = selection.<strong>getRanges()</strong>;
\r
823 * alert( ranges.length );
\r
825 getRanges : (function()
\r
827 var func = CKEDITOR.env.ie ?
\r
830 function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }
\r
832 // Finds the container and offset for a specific boundary
\r
834 var getBoundaryInformation = function( range, start )
\r
836 // Creates a collapsed range at the requested boundary.
\r
837 range = range.duplicate();
\r
838 range.collapse( start );
\r
840 // Gets the element that encloses the range entirely.
\r
841 var parent = range.parentElement(),
\r
842 doc = parent.ownerDocument;
\r
844 // Empty parent element, e.g. <i>^</i>
\r
845 if ( !parent.hasChildNodes() )
\r
846 return { container : parent, offset : 0 };
\r
848 var siblings = parent.children,
\r
851 testRange = range.duplicate(),
\r
853 endIndex = siblings.length - 1,
\r
859 // Binary search over all element childs to test the range to see whether
\r
860 // range is right on the boundary of one element.
\r
861 while ( startIndex <= endIndex )
\r
863 index = Math.floor( ( startIndex + endIndex ) / 2 );
\r
864 child = siblings[ index ];
\r
865 testRange.moveToElementText( child );
\r
866 position = testRange.compareEndPoints( 'StartToStart', range );
\r
868 if ( position > 0 )
\r
869 endIndex = index - 1;
\r
870 else if ( position < 0 )
\r
871 startIndex = index + 1;
\r
874 // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs.
\r
875 // e.g. <p>text<br />^<br /></p> (#7433)
\r
876 if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' )
\r
878 // "Fall back" to w3c selection.
\r
879 var sel = doc.defaultView.getSelection();
\r
880 return { container : sel[ start ? 'anchorNode' : 'focusNode' ],
\r
881 offset : sel[ start ? 'anchorOffset' : 'focusOffset' ] };
\r
884 return { container : parent, offset : getNodeIndex( child ) };
\r
888 // All childs are text nodes,
\r
889 // or to the right hand of test range are all text nodes. (#6992)
\r
890 if ( index == -1 || index == siblings.length - 1 && position < 0 )
\r
892 // Adapt test range to embrace the entire parent contents.
\r
893 testRange.moveToElementText( parent );
\r
894 testRange.setEndPoint( 'StartToStart', range );
\r
896 // IE report line break as CRLF with range.text but
\r
897 // only LF with textnode.nodeValue, normalize them to avoid
\r
898 // breaking character counting logic below. (#3949)
\r
899 distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
901 siblings = parent.childNodes;
\r
903 // Actual range anchor right beside test range at the boundary of text node.
\r
906 child = siblings[ siblings.length - 1 ];
\r
908 if ( child.nodeType != CKEDITOR.NODE_TEXT )
\r
909 return { container : parent, offset : siblings.length };
\r
911 return { container : child, offset : child.nodeValue.length };
\r
914 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
915 var i = siblings.length;
\r
916 while ( distance > 0 && i > 0 )
\r
918 sibling = siblings[ --i ];
\r
919 if ( sibling.nodeType == CKEDITOR.NODE_TEXT )
\r
921 container = sibling;
\r
922 distance -= sibling.nodeValue.length;
\r
926 return { container : container, offset : -distance };
\r
928 // Test range was one offset beyond OR behind the anchored text node.
\r
931 // Adapt one side of test range to the actual range
\r
932 // for measuring the offset between them.
\r
933 testRange.collapse( position > 0 ? true : false );
\r
934 testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );
\r
936 // IE report line break as CRLF with range.text but
\r
937 // only LF with textnode.nodeValue, normalize them to avoid
\r
938 // breaking character counting logic below. (#3949)
\r
939 distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
941 // Actual range anchor right beside test range at the inner boundary of text node.
\r
943 return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };
\r
945 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
946 while ( distance > 0 )
\r
950 sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];
\r
951 if ( sibling.nodeType == CKEDITOR.NODE_TEXT )
\r
953 distance -= sibling.nodeValue.length;
\r
954 container = sibling;
\r
958 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)
\r
961 return { container : parent, offset : getNodeIndex( child ) };
\r
965 return { container : container, offset : position > 0 ? -distance : container.nodeValue.length + distance };
\r
971 // IE doesn't have range support (in the W3C way), so we
\r
972 // need to do some magic to transform selections into
\r
973 // CKEDITOR.dom.range instances.
\r
975 var sel = this.getNative(),
\r
976 nativeRange = sel && sel.createRange(),
\r
977 type = this.getType(),
\r
983 if ( type == CKEDITOR.SELECTION_TEXT )
\r
985 range = new CKEDITOR.dom.range( this.document );
\r
987 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
988 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
990 boundaryInfo = getBoundaryInformation( nativeRange );
\r
991 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
993 // Correct an invalid IE range case on empty list item. (#5850)
\r
994 if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING
\r
995 && range.endOffset <= range.startContainer.getIndex() )
\r
1002 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
1006 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
1008 var element = nativeRange.item( i ),
\r
1009 parentElement = element.parentNode,
\r
1012 range = new CKEDITOR.dom.range( this.document );
\r
1014 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
1017 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
1018 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
1019 retval.push( range );
\r
1032 // On browsers implementing the W3C range, we simply
\r
1033 // tranform the native ranges in CKEDITOR.dom.range
\r
1038 doc = this.document,
\r
1039 sel = this.getNative();
\r
1044 // On WebKit, it may happen that we'll have no selection
\r
1045 // available. We normalize it here by replicating the
\r
1046 // behavior of other browsers.
\r
1047 if ( !sel.rangeCount )
\r
1049 range = new CKEDITOR.dom.range( doc );
\r
1050 range.moveToElementEditStart( doc.getBody() );
\r
1051 ranges.push( range );
\r
1054 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
1056 var nativeRange = sel.getRangeAt( i );
\r
1058 range = new CKEDITOR.dom.range( doc );
\r
1060 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
1061 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
1062 ranges.push( range );
\r
1067 return function( onlyEditables )
\r
1069 var cache = this._.cache;
\r
1070 if ( cache.ranges && !onlyEditables )
\r
1071 return cache.ranges;
\r
1072 else if ( !cache.ranges )
\r
1073 cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );
\r
1075 // Split range into multiple by read-only nodes.
\r
1076 if ( onlyEditables )
\r
1078 var ranges = cache.ranges;
\r
1079 for ( var i = 0; i < ranges.length; i++ )
\r
1081 var range = ranges[ i ];
\r
1083 // Drop range spans inside one ready-only node.
\r
1084 var parent = range.getCommonAncestor();
\r
1085 if ( parent.isReadOnly() )
\r
1086 ranges.splice( i, 1 );
\r
1088 if ( range.collapsed )
\r
1091 // Range may start inside a non-editable element,
\r
1092 // replace the range start after it.
\r
1093 if ( range.startContainer.isReadOnly() )
\r
1095 var current = range.startContainer;
\r
1098 if ( current.is( 'body' ) || !current.isReadOnly() )
\r
1101 if ( current.type == CKEDITOR.NODE_ELEMENT
\r
1102 && current.getAttribute( 'contentEditable' ) == 'false' )
\r
1103 range.setStartAfter( current );
\r
1105 current = current.getParent();
\r
1109 var startContainer = range.startContainer,
\r
1110 endContainer = range.endContainer,
\r
1111 startOffset = range.startOffset,
\r
1112 endOffset = range.endOffset,
\r
1113 walkerRange = range.clone();
\r
1115 // Enlarge range start/end with text node to avoid walker
\r
1116 // being DOM destructive, it doesn't interfere our checking
\r
1117 // of elements below as well.
\r
1118 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
1120 if ( startOffset >= startContainer.getLength() )
\r
1121 walkerRange.setStartAfter( startContainer );
\r
1123 walkerRange.setStartBefore( startContainer );
\r
1126 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
1129 walkerRange.setEndBefore( endContainer );
\r
1131 walkerRange.setEndAfter( endContainer );
\r
1134 // Looking for non-editable element inside the range.
\r
1135 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
1136 walker.evaluator = function( node )
\r
1138 if ( node.type == CKEDITOR.NODE_ELEMENT
\r
1139 && node.isReadOnly() )
\r
1141 var newRange = range.clone();
\r
1142 range.setEndBefore( node );
\r
1144 // Drop collapsed range around read-only elements,
\r
1145 // it make sure the range list empty when selecting
\r
1146 // only non-editable elements.
\r
1147 if ( range.collapsed )
\r
1148 ranges.splice( i--, 1 );
\r
1150 // Avoid creating invalid range.
\r
1151 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )
\r
1153 newRange.setStartAfter( node );
\r
1154 if ( !newRange.collapsed )
\r
1155 ranges.splice( i + 1, 0, newRange );
\r
1168 return cache.ranges;
\r
1173 * Gets the DOM element in which the selection starts.
\r
1174 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
1177 * var element = editor.getSelection().<strong>getStartElement()</strong>;
\r
1178 * alert( element.getName() );
\r
1180 getStartElement : function()
\r
1182 var cache = this._.cache;
\r
1183 if ( cache.startElement !== undefined )
\r
1184 return cache.startElement;
\r
1187 sel = this.getNative();
\r
1189 switch ( this.getType() )
\r
1191 case CKEDITOR.SELECTION_ELEMENT :
\r
1192 return this.getSelectedElement();
\r
1194 case CKEDITOR.SELECTION_TEXT :
\r
1196 var range = this.getRanges()[0];
\r
1200 if ( !range.collapsed )
\r
1204 // Decrease the range content to exclude particial
\r
1205 // selected node on the start which doesn't have
\r
1206 // visual impact. ( #3231 )
\r
1209 var startContainer = range.startContainer,
\r
1210 startOffset = range.startOffset;
\r
1211 // Limit the fix only to non-block elements.(#3950)
\r
1212 if ( startOffset == ( startContainer.getChildCount ?
\r
1213 startContainer.getChildCount() : startContainer.getLength() )
\r
1214 && !startContainer.isBlockBoundary() )
\r
1215 range.setStartAfter( startContainer );
\r
1219 node = range.startContainer;
\r
1221 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
1222 return node.getParent();
\r
1224 node = node.getChild( range.startOffset );
\r
1226 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
1227 node = range.startContainer;
\r
1230 var child = node.getFirst();
\r
1231 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
1234 child = child.getFirst();
\r
1240 node = range.startContainer;
\r
1241 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
1242 node = node.getParent();
\r
1249 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
1253 * Gets the currently selected element.
\r
1254 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
1255 * selection is available or the selection type is not
\r
1256 * <code>{@link CKEDITOR.SELECTION_ELEMENT}</code>.
\r
1258 * var element = editor.getSelection().<strong>getSelectedElement()</strong>;
\r
1259 * alert( element.getName() );
\r
1261 getSelectedElement : function()
\r
1263 var cache = this._.cache;
\r
1264 if ( cache.selectedElement !== undefined )
\r
1265 return cache.selectedElement;
\r
1269 var node = CKEDITOR.tools.tryThese(
\r
1270 // Is it native IE control type selection?
\r
1273 return self.getNative().createRange().item( 0 );
\r
1275 // If a table or list is fully selected.
\r
1280 range = self.getRanges()[ 0 ],
\r
1281 ancestor = range.getCommonAncestor( 1, 1 ),
\r
1282 tags = { table:1,ul:1,ol:1,dl:1 };
\r
1284 for ( var t in tags )
\r
1286 if ( ( root = ancestor.getAscendant( t, 1 ) ) )
\r
1292 // Enlarging the start boundary.
\r
1293 var testRange = new CKEDITOR.dom.range( this.document );
\r
1294 testRange.setStartAt( root, CKEDITOR.POSITION_AFTER_START );
\r
1295 testRange.setEnd( range.startContainer, range.startOffset );
\r
1297 var enlargeables = CKEDITOR.tools.extend( tags, CKEDITOR.dtd.$listItem, CKEDITOR.dtd.$tableContent ),
\r
1298 walker = new CKEDITOR.dom.walker( testRange ),
\r
1299 // Check the range is at the inner boundary of the structural element.
\r
1300 guard = function( walker, isEnd )
\r
1302 return function( node, isWalkOut )
\r
1304 if ( node.type == CKEDITOR.NODE_TEXT && ( !CKEDITOR.tools.trim( node.getText() ) || node.getParent().data( 'cke-bookmark' ) ) )
\r
1308 if ( node.type == CKEDITOR.NODE_ELEMENT )
\r
1310 tag = node.getName();
\r
1312 // Bypass bogus br at the end of block.
\r
1313 if ( tag == 'br' && isEnd && node.equals( node.getParent().getBogus() ) )
\r
1316 if ( isWalkOut && tag in enlargeables || tag in CKEDITOR.dtd.$removeEmpty )
\r
1320 walker.halted = 1;
\r
1325 walker.guard = guard( walker );
\r
1327 if ( walker.checkBackward() && !walker.halted )
\r
1329 walker = new CKEDITOR.dom.walker( testRange );
\r
1330 testRange.setStart( range.endContainer, range.endOffset );
\r
1331 testRange.setEndAt( root, CKEDITOR.POSITION_BEFORE_END );
\r
1332 walker.guard = guard( walker, 1 );
\r
1333 if ( walker.checkForward() && !walker.halted )
\r
1343 // Figure it out by checking if there's a single enclosed
\r
1344 // node of the range.
\r
1347 var range = self.getRanges()[ 0 ],
\r
1351 // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>
\r
1352 for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )
\r
1353 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )
\r
1354 && styleObjectElements[ enclosed.getName() ]
\r
1355 && ( selected = enclosed ) ); i-- )
\r
1357 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]
\r
1358 range.shrink( CKEDITOR.SHRINK_ELEMENT );
\r
1361 return selected.$;
\r
1364 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
1368 * Retrieves the text contained within the range. An empty string is returned for non-text selection.
\r
1369 * @returns {String} A string of text within the current selection.
\r
1372 * var text = editor.getSelection().<strong>getSelectedText()</strong>;
\r
1375 getSelectedText : function()
\r
1377 var cache = this._.cache;
\r
1378 if ( cache.selectedText !== undefined )
\r
1379 return cache.selectedText;
\r
1382 nativeSel = this.getNative();
\r
1383 if ( this.getType() == CKEDITOR.SELECTION_TEXT )
\r
1384 text = CKEDITOR.env.ie ? nativeSel.createRange().text : nativeSel.toString();
\r
1386 return ( cache.selectedText = text );
\r
1390 * Locks the selection made in the editor in order to make it possible to
\r
1391 * manipulate it without browser interference. A locked selection is
\r
1392 * cached and remains unchanged until it is released with the <code>#unlock</code>
\r
1395 * editor.getSelection().<strong>lock()</strong>;
\r
1399 // Call all cacheable function.
\r
1401 this.getStartElement();
\r
1402 this.getSelectedElement();
\r
1403 this.getSelectedText();
\r
1405 // The native selection is not available when locked.
\r
1406 this._.cache.nativeSel = {};
\r
1408 this.isLocked = 1;
\r
1410 // Save this selection inside the DOM document.
\r
1411 this.document.setCustomData( 'cke_locked_selection', this );
\r
1415 * Unlocks the selection made in the editor and locked with the <code>#lock</code> method.
\r
1416 * An unlocked selection is no longer cached and can be changed.
\r
1417 * @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
1419 * editor.getSelection().<strong>unlock()</strong>;
\r
1421 unlock : function( restore )
\r
1423 var doc = this.document,
\r
1424 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
1426 if ( lockedSelection )
\r
1428 doc.setCustomData( 'cke_locked_selection', null );
\r
1432 var selectedElement = lockedSelection.getSelectedElement(),
\r
1433 ranges = !selectedElement && lockedSelection.getRanges();
\r
1435 this.isLocked = 0;
\r
1438 doc.getBody().focus();
\r
1440 if ( selectedElement )
\r
1441 this.selectElement( selectedElement );
\r
1443 this.selectRanges( ranges );
\r
1447 if ( !lockedSelection || !restore )
\r
1449 this.isLocked = 0;
\r
1455 * Clears the selection cache.
\r
1457 * editor.getSelection().<strong>reset()</strong>;
\r
1459 reset : function()
\r
1461 this._.cache = {};
\r
1465 * Makes the current selection of type <code>{@link CKEDITOR.SELECTION_ELEMENT}</code> by enclosing the specified element.
\r
1466 * @param {CKEDITOR.dom.element} element The element to enclose in the selection.
\r
1468 * var element = editor.document.getById( 'sampleElement' );
\r
1469 * editor.getSelection.<strong>selectElement( element )</strong>;
\r
1471 selectElement : function( element )
\r
1473 if ( this.isLocked )
\r
1475 var range = new CKEDITOR.dom.range( this.document );
\r
1476 range.setStartBefore( element );
\r
1477 range.setEndAfter( element );
\r
1479 this._.cache.selectedElement = element;
\r
1480 this._.cache.startElement = element;
\r
1481 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );
\r
1482 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
1487 range = new CKEDITOR.dom.range( element.getDocument() );
\r
1488 range.setStartBefore( element );
\r
1489 range.setEndAfter( element );
\r
1492 this.document.fire( 'selectionchange' );
\r
1498 * Clears the original selection and adds the specified ranges
\r
1499 * to the document selection.
\r
1500 * @param {Array} ranges An array of <code>{@link CKEDITOR.dom.range}</code> instances representing ranges to be added to the document.
\r
1502 * var ranges = new CKEDITOR.dom.range( editor.document );
\r
1503 * editor.getSelection().<strong>selectRanges( [ ranges ] )</strong>;
\r
1505 selectRanges : function( ranges )
\r
1507 if ( this.isLocked )
\r
1509 this._.cache.selectedElement = null;
\r
1510 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();
\r
1511 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );
\r
1512 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
1517 if ( CKEDITOR.env.ie )
\r
1519 if ( ranges.length > 1 )
\r
1521 // IE doesn't accept multiple ranges selection, so we join all into one.
\r
1522 var last = ranges[ ranges.length -1 ] ;
\r
1523 ranges[ 0 ].setEnd( last.endContainer, last.endOffset );
\r
1524 ranges.length = 1;
\r
1527 if ( ranges[ 0 ] )
\r
1528 ranges[ 0 ].select();
\r
1534 var sel = this.getNative();
\r
1536 // getNative() returns null if iframe is "display:none" in FF. (#6577)
\r
1540 if ( ranges.length )
\r
1542 sel.removeAllRanges();
\r
1543 // Remove any existing filling char first.
\r
1544 CKEDITOR.env.webkit && removeFillingChar( this.document );
\r
1547 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1549 // Joining sequential ranges introduced by
\r
1550 // readonly elements protection.
\r
1551 if ( i < ranges.length -1 )
\r
1553 var left = ranges[ i ], right = ranges[ i +1 ],
\r
1554 between = left.clone();
\r
1555 between.setStart( left.endContainer, left.endOffset );
\r
1556 between.setEnd( right.startContainer, right.startOffset );
\r
1558 // Don't confused by Firefox adjancent multi-ranges
\r
1559 // introduced by table cells selection.
\r
1560 if ( !between.collapsed )
\r
1562 between.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1563 var ancestor = between.getCommonAncestor(),
\r
1564 enclosed = between.getEnclosedNode();
\r
1566 // The following cases has to be considered:
\r
1567 // 1. <span contenteditable="false">[placeholder]</span>
\r
1568 // 2. <input contenteditable="false" type="radio"/> (#6621)
\r
1569 if ( ancestor.isReadOnly() || enclosed && enclosed.isReadOnly() )
\r
1571 right.setStart( left.startContainer, left.startOffset );
\r
1572 ranges.splice( i--, 1 );
\r
1578 var range = ranges[ i ];
\r
1579 var nativeRange = this.document.$.createRange();
\r
1580 var startContainer = range.startContainer;
\r
1582 // In FF2, if we have a collapsed range, inside an empty
\r
1583 // element, we must add something to it otherwise the caret
\r
1584 // will not be visible.
\r
1585 // In Opera instead, the selection will be moved out of the
\r
1586 // element. (#4657)
\r
1587 if ( range.collapsed &&
\r
1588 ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&
\r
1589 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
1590 !startContainer.getChildCount() )
\r
1592 startContainer.appendText( '' );
\r
1595 if ( range.collapsed
\r
1596 && CKEDITOR.env.webkit
\r
1597 && rangeRequiresFix( range ) )
\r
1599 // Append a zero-width space so WebKit will not try to
\r
1600 // move the selection by itself (#1272).
\r
1601 var fillingChar = createFillingChar( this.document );
\r
1602 range.insertNode( fillingChar ) ;
\r
1604 var next = fillingChar.getNext();
\r
1606 // If the filling char is followed by a <br>, whithout
\r
1607 // having something before it, it'll not blink.
\r
1608 // Let's remove it in this case.
\r
1609 if ( next && !fillingChar.getPrevious() && next.type == CKEDITOR.NODE_ELEMENT && next.getName() == 'br' )
\r
1611 removeFillingChar( this.document );
\r
1612 range.moveToPosition( next, CKEDITOR.POSITION_BEFORE_START );
\r
1615 range.moveToPosition( fillingChar, CKEDITOR.POSITION_AFTER_END );
\r
1618 nativeRange.setStart( range.startContainer.$, range.startOffset );
\r
1622 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1626 // There is a bug in Firefox implementation (it would be too easy
\r
1627 // otherwise). The new start can't be after the end (W3C says it can).
\r
1628 // So, let's create a new range and collapse it to the desired point.
\r
1629 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )
\r
1631 range.collapse( 1 );
\r
1632 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1638 // Select the range.
\r
1639 sel.addRange( nativeRange );
\r
1642 // Don't miss selection change event for non-IEs.
\r
1643 this.document.fire( 'selectionchange' );
\r
1649 * Creates a bookmark for each range of this selection (from <code>#getRanges</code>)
\r
1650 * by calling the <code>{@link CKEDITOR.dom.range.prototype.createBookmark}</code> method,
\r
1651 * with extra care taken to avoid interference among those ranges. The arguments
\r
1652 * received are the same as with the underlying range method.
\r
1653 * @returns {Array} Array of bookmarks for each range.
\r
1655 * var bookmarks = editor.getSelection().<strong>createBookmarks()</strong>;
\r
1657 createBookmarks : function( serializable )
\r
1659 return this.getRanges().createBookmarks( serializable );
\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.createBookmark2}</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>createBookmarks2()</strong>;
\r
1671 createBookmarks2 : function( normalized )
\r
1673 return this.getRanges().createBookmarks2( normalized );
\r
1677 * Selects the virtual ranges denoted by the bookmarks by calling <code>#selectRanges</code>.
\r
1678 * @param {Array} bookmarks The bookmarks representing ranges to be selected.
\r
1679 * @returns {CKEDITOR.dom.selection} This selection object, after the ranges were selected.
\r
1681 * var bookmarks = editor.getSelection().createBookmarks();
\r
1682 * editor.getSelection().<strong>selectBookmarks( bookmarks )</strong>;
\r
1684 selectBookmarks : function( bookmarks )
\r
1687 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
1689 var range = new CKEDITOR.dom.range( this.document );
\r
1690 range.moveToBookmark( bookmarks[i] );
\r
1691 ranges.push( range );
\r
1693 this.selectRanges( ranges );
\r
1698 * Retrieves the common ancestor node of the first range and the last range.
\r
1699 * @returns {CKEDITOR.dom.element} The common ancestor of the selection.
\r
1701 * var ancestor = editor.getSelection().<strong>getCommonAncestor()</strong>;
\r
1703 getCommonAncestor : function()
\r
1705 var ranges = this.getRanges(),
\r
1706 startNode = ranges[ 0 ].startContainer,
\r
1707 endNode = ranges[ ranges.length - 1 ].endContainer;
\r
1708 return startNode.getCommonAncestor( endNode );
\r
1712 * Moves the scrollbar to the starting position of the current selection.
\r
1714 * editor.getSelection().<strong>scrollIntoView()</strong>;
\r
1716 scrollIntoView : function()
\r
1718 // If we have split the block, adds a temporary span at the
\r
1719 // range position and scroll relatively to it.
\r
1720 var start = this.getStartElement();
\r
1721 start.scrollIntoView();
\r
1728 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1729 fillerTextRegex = /\ufeff|\u00a0/,
\r
1730 nonCells = { table:1,tbody:1,tr:1 };
\r
1732 CKEDITOR.dom.range.prototype.select =
\r
1735 function( forceExpand )
\r
1737 var collapsed = this.collapsed,
\r
1738 isStartMarkerAlone, dummySpan, ieRange;
\r
1740 // Try to make a object selection.
\r
1741 var selected = this.getEnclosedNode();
\r
1746 ieRange = this.document.$.body.createControlRange();
\r
1747 ieRange.addElement( selected.$ );
\r
1754 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.
\r
1755 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...
\r
1756 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells
\r
1757 || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )
\r
1759 this.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1762 var bookmark = this.createBookmark();
\r
1764 // Create marker tags for the start and end boundaries.
\r
1765 var startNode = bookmark.startNode;
\r
1769 endNode = bookmark.endNode;
\r
1771 // Create the main range which will be used for the selection.
\r
1772 ieRange = this.document.$.body.createTextRange();
\r
1774 // Position the range at the start boundary.
\r
1775 ieRange.moveToElementText( startNode.$ );
\r
1776 ieRange.moveStart( 'character', 1 );
\r
1780 // Create a tool range for the end.
\r
1781 var ieRangeEnd = this.document.$.body.createTextRange();
\r
1783 // Position the tool range at the end.
\r
1784 ieRangeEnd.moveToElementText( endNode.$ );
\r
1786 // Move the end boundary of the main range to match the tool range.
\r
1787 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
1788 ieRange.moveEnd( 'character', -1 );
\r
1792 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
1793 // will expand and that the cursor will be blinking on the right place.
\r
1794 // Actually, we are using this flag just to avoid using this hack in all
\r
1795 // situations, but just on those needed.
\r
1796 var next = startNode.getNext( notWhitespaces );
\r
1797 isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?
\r
1798 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );
\r
1800 // Append a temporary <span></span> before the selection.
\r
1801 // This is needed to avoid IE destroying selections inside empty
\r
1802 // inline elements, like <b></b> (#253).
\r
1803 // It is also needed when placing the selection right after an inline
\r
1804 // element to avoid the selection moving inside of it.
\r
1805 dummySpan = this.document.createElement( 'span' );
\r
1806 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1807 dummySpan.insertBefore( startNode );
\r
1809 if ( isStartMarkerAlone )
\r
1811 // To expand empty blocks or line spaces after <br>, we need
\r
1812 // instead to have any char, which will be later deleted using the
\r
1814 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1815 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1819 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1820 this.setStartBefore( startNode );
\r
1821 startNode.remove();
\r
1825 if ( isStartMarkerAlone )
\r
1827 // Move the selection start to include the temporary \ufeff.
\r
1828 ieRange.moveStart( 'character', -1 );
\r
1832 // Remove our temporary stuff.
\r
1833 this.document.$.selection.clear();
\r
1838 this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );
\r
1839 dummySpan.remove();
\r
1843 this.setEndBefore( endNode );
\r
1848 this.document.fire( 'selectionchange' );
\r
1853 this.document.getSelection().selectRanges( [ this ] );
\r