2 Copyright (c) 2003-2011, 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 var start = range.startContainer,
\r
82 offset = range.startOffset;
\r
84 if ( start.type == CKEDITOR.NODE_TEXT )
\r
87 // 1. Empty inline element. <span>^</span>
\r
88 // 2. Adjoin to inline element. <p><strong>text</strong>^</p>
\r
89 return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) );
\r
94 modes : { wysiwyg : 1, source : 1 },
\r
95 readOnly : CKEDITOR.env.ie || CKEDITOR.env.webkit,
\r
96 exec : function( editor )
\r
98 switch ( editor.mode )
\r
101 editor.document.$.execCommand( 'SelectAll', false, null );
\r
102 // Force triggering selectionChange (#7008)
\r
103 editor.forceNextSelectionCheck();
\r
104 editor.selectionChange();
\r
107 // Select the contents of the textarea
\r
108 var textarea = editor.textarea.$;
\r
109 if ( CKEDITOR.env.ie )
\r
110 textarea.createTextRange().execCommand( 'SelectAll' );
\r
113 textarea.selectionStart = 0;
\r
114 textarea.selectionEnd = textarea.value.length;
\r
122 function createFillingChar( doc )
\r
124 removeFillingChar( doc );
\r
126 var fillingChar = doc.createText( '\u200B' );
\r
127 doc.setCustomData( 'cke-fillingChar', fillingChar );
\r
129 return fillingChar;
\r
132 function getFillingChar( doc )
\r
134 return doc && doc.getCustomData( 'cke-fillingChar' );
\r
137 // Checks if a filling char has been used, eventualy removing it (#1272).
\r
138 function checkFillingChar( doc )
\r
140 var fillingChar = doc && getFillingChar( doc );
\r
143 // Use this flag to avoid removing the filling char right after
\r
145 if ( fillingChar.getCustomData( 'ready' ) )
\r
146 removeFillingChar( doc );
\r
148 fillingChar.setCustomData( 'ready', 1 );
\r
152 function removeFillingChar( doc )
\r
154 var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' );
\r
157 // We can't simply remove the filling node because the user
\r
158 // will actually enlarge it when typing, so we just remove the
\r
159 // invisible char from it.
\r
160 fillingChar.setText( fillingChar.getText().replace( /\u200B/g, '' ) );
\r
165 CKEDITOR.plugins.add( 'selection',
\r
167 init : function( editor )
\r
169 // On WebKit only, we need a special "filling" char on some situations
\r
170 // (#1272). Here we set the events that should invalidate that char.
\r
171 if ( CKEDITOR.env.webkit )
\r
173 editor.on( 'selectionChange', function() { checkFillingChar( editor.document ); } );
\r
174 editor.on( 'beforeSetMode', function() { removeFillingChar( editor.document ); } );
\r
175 editor.on( 'key', function( e )
\r
177 // Remove the filling char before some keys get
\r
178 // executed, so they'll not get blocked by it.
\r
179 switch ( e.data.keyCode )
\r
182 case CKEDITOR.SHIFT + 13 : // SHIFT-ENTER
\r
183 case 37 : // LEFT-ARROW
\r
184 case 39 : // RIGHT-ARROW
\r
185 case 8 : // BACKSPACE
\r
186 removeFillingChar( editor.document );
\r
188 }, null, null, 10 );
\r
190 var fillingCharBefore,
\r
193 function beforeData()
\r
195 var doc = editor.document,
\r
196 fillingChar = getFillingChar( doc );
\r
200 // If cursor is right blinking by side of the filler node, save it for restoring,
\r
201 // as the following text substitution will blind it. (#7437)
\r
202 var sel = doc.$.defaultView.getSelection();
\r
203 if ( sel.type == 'Caret' && sel.anchorNode == fillingChar.$ )
\r
204 resetSelection = 1;
\r
206 fillingCharBefore = fillingChar.getText();
\r
207 fillingChar.setText( fillingCharBefore.replace( /\u200B/g, '' ) );
\r
210 function afterData()
\r
212 var doc = editor.document,
\r
213 fillingChar = getFillingChar( doc );
\r
217 fillingChar.setText( fillingCharBefore );
\r
219 if ( resetSelection )
\r
221 doc.$.defaultView.getSelection().setPosition( fillingChar.$,fillingChar.getLength() );
\r
222 resetSelection = 0;
\r
226 editor.on( 'beforeUndoImage', beforeData );
\r
227 editor.on( 'afterUndoImage', afterData );
\r
228 editor.on( 'beforeGetData', beforeData, null, null, 0 );
\r
229 editor.on( 'getData', afterData );
\r
232 editor.on( 'contentDom', function()
\r
234 var doc = editor.document,
\r
235 body = doc.getBody(),
\r
236 html = doc.getDocumentElement();
\r
238 if ( CKEDITOR.env.ie )
\r
240 // Other browsers don't loose the selection if the
\r
241 // editor document loose the focus. In IE, we don't
\r
242 // have support for it, so we reproduce it here, other
\r
243 // than firing the selection change event.
\r
247 restoreEnabled = 1;
\r
249 // "onfocusin" is fired before "onfocus". It makes it
\r
250 // possible to restore the selection before click
\r
251 // events get executed.
\r
252 body.on( 'focusin', function( evt )
\r
254 // If there are elements with layout they fire this event but
\r
255 // it must be ignored to allow edit its contents #4682
\r
256 if ( evt.data.$.srcElement.nodeName != 'BODY' )
\r
259 // If we have saved a range, restore it at this
\r
263 if ( restoreEnabled )
\r
265 // Well not break because of this.
\r
268 savedRange.select();
\r
273 // Update locked selection because of the normalized text nodes. (#6083, #6987)
\r
274 var lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
275 if ( lockedSelection )
\r
277 lockedSelection.unlock();
\r
278 lockedSelection.lock();
\r
286 body.on( 'focus', function()
\r
288 // Enable selections to be saved.
\r
294 body.on( 'beforedeactivate', function( evt )
\r
296 // Ignore this event if it's caused by focus switch between
\r
297 // internal editable control type elements, e.g. layouted paragraph. (#4682)
\r
298 if ( evt.data.$.toElement )
\r
301 // Disable selections from being saved.
\r
303 restoreEnabled = 1;
\r
306 // IE before version 8 will leave cursor blinking inside the document after
\r
307 // editor blurred unless we clean up the selection. (#4716)
\r
308 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
\r
310 editor.on( 'blur', function( evt )
\r
312 // Try/Catch to avoid errors if the editor is hidden. (#6375)
\r
315 editor.document && editor.document.$.selection.empty();
\r
321 // Listening on document element ensures that
\r
322 // scrollbar is included. (#5280)
\r
323 html.on( 'mousedown', function()
\r
325 // Lock restore selection now, as we have
\r
326 // a followed 'click' event which introduce
\r
327 // new selection. (#5735)
\r
328 restoreEnabled = 0;
\r
331 html.on( 'mouseup', function()
\r
333 restoreEnabled = 1;
\r
336 // In IE6/7 the blinking cursor appears, but contents are
\r
337 // not editable. (#5634)
\r
338 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )
\r
340 // The 'click' event is not fired when clicking the
\r
341 // scrollbars, so we can use it to check whether
\r
342 // the empty space following <body> has been clicked.
\r
343 html.on( 'click', function( evt )
\r
345 if ( evt.data.getTarget().getName() == 'html' )
\r
346 editor.getSelection().getRanges()[ 0 ].select();
\r
351 // IE fires the "selectionchange" event when clicking
\r
352 // inside a selection. We don't want to capture that.
\r
353 body.on( 'mousedown', function( evt )
\r
355 // IE scrolls document to top on right mousedown
\r
356 // when editor has no focus, remember this scroll
\r
357 // position and revert it before context menu opens. (#5778)
\r
358 if ( evt.data.$.button == 2 )
\r
360 var sel = editor.document.$.selection;
\r
361 if ( sel.type == 'None' )
\r
362 scroll = editor.window.getScrollPosition();
\r
367 body.on( 'mouseup',
\r
370 // Restore recorded scroll position when needed on right mouseup.
\r
371 if ( evt.data.$.button == 2 && scroll )
\r
373 editor.document.$.documentElement.scrollLeft = scroll.x;
\r
374 editor.document.$.documentElement.scrollTop = scroll.y;
\r
379 setTimeout( function()
\r
381 saveSelection( true );
\r
386 body.on( 'keydown', disableSave );
\r
395 // IE is the only to provide the "selectionchange"
\r
397 doc.on( 'selectionchange', saveSelection );
\r
399 function disableSave()
\r
404 function saveSelection( testIt )
\r
408 var doc = editor.document,
\r
409 sel = editor.getSelection(),
\r
410 nativeSel = sel && sel.getNative();
\r
412 // There is a very specific case, when clicking
\r
413 // inside a text selection. In that case, the
\r
414 // selection collapses at the clicking point,
\r
415 // but the selection object remains in an
\r
416 // unknown state, making createRange return a
\r
417 // range at the very start of the document. In
\r
418 // such situation we have to test the range, to
\r
419 // be sure it's valid.
\r
420 if ( testIt && nativeSel && nativeSel.type == 'None' )
\r
422 // The "InsertImage" command can be used to
\r
423 // test whether the selection is good or not.
\r
424 // If not, it's enough to give some time to
\r
425 // IE to put things in order for us.
\r
426 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
428 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
433 // Avoid saving selection from within text input. (#5747)
\r
435 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
\r
436 && ( parentTag = nativeSel.createRange() )
\r
437 && ( parentTag = parentTag.parentElement() )
\r
438 && ( parentTag = parentTag.nodeName )
\r
439 && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
\r
444 savedRange = nativeSel && sel.getRanges()[ 0 ];
\r
446 checkSelectionChangeTimeout.call( editor );
\r
452 // In other browsers, we make the selection change
\r
453 // check based on other events, like clicks or keys
\r
456 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
457 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
461 // Clear the cached range path before unload. (#7174)
\r
462 editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );
\r
464 editor.addCommand( 'selectAll', selectAllCmd );
\r
465 editor.ui.addButton( 'SelectAll',
\r
467 label : editor.lang.selectAll,
\r
468 command : 'selectAll'
\r
471 editor.selectionChange = checkSelectionChangeTimeout;
\r
476 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
477 * @returns {CKEDITOR.dom.selection} A selection object or null if not on
\r
478 * WYSIWYG mode or no selection is available.
\r
480 * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;
\r
481 * alert( selection.getType() );
\r
483 CKEDITOR.editor.prototype.getSelection = function()
\r
485 return this.document && this.document.getSelection();
\r
488 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
490 delete this._.selectionPreviousPath;
\r
494 * Gets the current selection from the document.
\r
495 * @returns {CKEDITOR.dom.selection} A selection object.
\r
497 * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;
\r
498 * alert( selection.getType() );
\r
500 CKEDITOR.dom.document.prototype.getSelection = function()
\r
502 var sel = new CKEDITOR.dom.selection( this );
\r
503 return ( !sel || sel.isInvalid ) ? null : sel;
\r
510 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
511 * alert( 'Nothing is selected' );
\r
513 CKEDITOR.SELECTION_NONE = 1;
\r
516 * Text or collapsed selection.
\r
519 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
520 * alert( 'Text is selected' );
\r
522 CKEDITOR.SELECTION_TEXT = 2;
\r
525 * Element selection.
\r
528 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
529 * alert( 'An element is selected' );
\r
531 CKEDITOR.SELECTION_ELEMENT = 3;
\r
534 * Manipulates the selection in a DOM document.
\r
538 CKEDITOR.dom.selection = function( document )
\r
540 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
542 if ( lockedSelection )
\r
543 return lockedSelection;
\r
545 this.document = document;
\r
553 * IE BUG: The selection's document may be a different document than the
\r
554 * editor document. Return null if that's the case.
\r
556 if ( CKEDITOR.env.ie )
\r
558 var range = this.getNative().createRange();
\r
560 || ( range.item && range.item(0).ownerDocument != this.document.$ )
\r
561 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
563 this.isInvalid = true;
\r
570 var styleObjectElements =
\r
572 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
\r
573 a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1
\r
576 CKEDITOR.dom.selection.prototype =
\r
579 * Gets the native selection object from the browser.
\r
581 * @returns {Object} The native selection object.
\r
583 * var selection = editor.getSelection().<b>getNative()</b>;
\r
589 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
594 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
598 * Gets the type of the current selection. The following values are
\r
601 * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>
\r
602 * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or
\r
603 * collapsed selection.</li>
\r
604 * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element
\r
608 * @returns {Number} One of the following constant values:
\r
609 * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or
\r
610 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
612 * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )
\r
613 * alert( 'Text is selected' );
\r
619 var cache = this._.cache;
\r
623 var type = CKEDITOR.SELECTION_NONE;
\r
627 var sel = this.getNative(),
\r
630 if ( ieType == 'Text' )
\r
631 type = CKEDITOR.SELECTION_TEXT;
\r
633 if ( ieType == 'Control' )
\r
634 type = CKEDITOR.SELECTION_ELEMENT;
\r
636 // It is possible that we can still get a text range
\r
637 // object even when type == 'None' is returned by IE.
\r
638 // So we'd better check the object returned by
\r
639 // createRange() rather than by looking at the type.
\r
640 if ( sel.createRange().parentElement )
\r
641 type = CKEDITOR.SELECTION_TEXT;
\r
645 return ( cache.type = type );
\r
650 var cache = this._.cache;
\r
654 var type = CKEDITOR.SELECTION_TEXT;
\r
656 var sel = this.getNative();
\r
659 type = CKEDITOR.SELECTION_NONE;
\r
660 else if ( sel.rangeCount == 1 )
\r
662 // Check if the actual selection is a control (IMG,
\r
663 // TABLE, HR, etc...).
\r
665 var range = sel.getRangeAt(0),
\r
666 startContainer = range.startContainer;
\r
668 if ( startContainer == range.endContainer
\r
669 && startContainer.nodeType == 1
\r
670 && ( range.endOffset - range.startOffset ) == 1
\r
671 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
673 type = CKEDITOR.SELECTION_ELEMENT;
\r
677 return ( cache.type = type );
\r
681 * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.
\r
682 * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns
\r
683 * one range for each table cell when one or more table row is selected.
\r
686 * var ranges = selection.getRanges();
\r
687 * alert(ranges.length);
\r
689 getRanges : (function()
\r
691 var func = CKEDITOR.env.ie ?
\r
694 function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }
\r
696 // Finds the container and offset for a specific boundary
\r
698 var getBoundaryInformation = function( range, start )
\r
700 // Creates a collapsed range at the requested boundary.
\r
701 range = range.duplicate();
\r
702 range.collapse( start );
\r
704 // Gets the element that encloses the range entirely.
\r
705 var parent = range.parentElement(),
\r
706 doc = parent.ownerDocument;
\r
708 // Empty parent element, e.g. <i>^</i>
\r
709 if ( !parent.hasChildNodes() )
\r
710 return { container : parent, offset : 0 };
\r
712 var siblings = parent.children,
\r
715 testRange = range.duplicate(),
\r
717 endIndex = siblings.length - 1,
\r
722 // Binary search over all element childs to test the range to see whether
\r
723 // range is right on the boundary of one element.
\r
724 while ( startIndex <= endIndex )
\r
726 index = Math.floor( ( startIndex + endIndex ) / 2 );
\r
727 child = siblings[ index ];
\r
728 testRange.moveToElementText( child );
\r
729 position = testRange.compareEndPoints( 'StartToStart', range );
\r
731 if ( position > 0 )
\r
732 endIndex = index - 1;
\r
733 else if ( position < 0 )
\r
734 startIndex = index + 1;
\r
737 // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs.
\r
738 // e.g. <p>text<br />^<br /></p> (#7433)
\r
739 if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' )
\r
741 var bmId = 'cke_range_marker';
\r
742 range.execCommand( 'CreateBookmark', false, bmId );
\r
743 child = doc.getElementsByName( bmId )[ 0 ];
\r
744 var offset = getNodeIndex( child );
\r
745 parent.removeChild( child );
\r
746 return { container : parent, offset : offset };
\r
749 return { container : parent, offset : getNodeIndex( child ) };
\r
753 // All childs are text nodes,
\r
754 // or to the right hand of test range are all text nodes. (#6992)
\r
755 if ( index == -1 || index == siblings.length - 1 && position < 0 )
\r
757 // Adapt test range to embrace the entire parent contents.
\r
758 testRange.moveToElementText( parent );
\r
759 testRange.setEndPoint( 'StartToStart', range );
\r
761 // IE report line break as CRLF with range.text but
\r
762 // only LF with textnode.nodeValue, normalize them to avoid
\r
763 // breaking character counting logic below. (#3949)
\r
764 distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
766 siblings = parent.childNodes;
\r
768 // Actual range anchor right beside test range at the boundary of text node.
\r
771 child = siblings[ siblings.length - 1 ];
\r
773 if ( child.nodeType == CKEDITOR.NODE_ELEMENT )
\r
774 return { container : parent, offset : siblings.length };
\r
776 return { container : child, offset : child.nodeValue.length };
\r
779 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
780 var i = siblings.length;
\r
781 while ( distance > 0 )
\r
782 distance -= siblings[ --i ].nodeValue.length;
\r
784 return { container : siblings[ i ], offset : -distance };
\r
786 // Test range was one offset beyond OR behind the anchored text node.
\r
789 // Adapt one side of test range to the actual range
\r
790 // for measuring the offset between them.
\r
791 testRange.collapse( position > 0 ? true : false );
\r
792 testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );
\r
794 // IE report line break as CRLF with range.text but
\r
795 // only LF with textnode.nodeValue, normalize them to avoid
\r
796 // breaking character counting logic below. (#3949)
\r
797 distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
799 // Actual range anchor right beside test range at the inner boundary of text node.
\r
801 return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };
\r
803 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
804 while ( distance > 0 )
\r
808 sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];
\r
809 distance -= sibling.nodeValue.length;
\r
812 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)
\r
815 return { container : parent, offset : getNodeIndex( child ) };
\r
819 return { container : child, offset : position > 0 ? -distance : child.nodeValue.length + distance };
\r
825 // IE doesn't have range support (in the W3C way), so we
\r
826 // need to do some magic to transform selections into
\r
827 // CKEDITOR.dom.range instances.
\r
829 var sel = this.getNative(),
\r
830 nativeRange = sel && sel.createRange(),
\r
831 type = this.getType(),
\r
837 if ( type == CKEDITOR.SELECTION_TEXT )
\r
839 range = new CKEDITOR.dom.range( this.document );
\r
841 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
842 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
844 boundaryInfo = getBoundaryInformation( nativeRange );
\r
845 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
847 // Correct an invalid IE range case on empty list item. (#5850)
\r
848 if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING
\r
849 && range.endOffset <= range.startContainer.getIndex() )
\r
856 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
860 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
862 var element = nativeRange.item( i ),
\r
863 parentElement = element.parentNode,
\r
866 range = new CKEDITOR.dom.range( this.document );
\r
868 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
871 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
872 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
873 retval.push( range );
\r
886 // On browsers implementing the W3C range, we simply
\r
887 // tranform the native ranges in CKEDITOR.dom.range
\r
892 doc = this.document,
\r
893 sel = this.getNative();
\r
898 // On WebKit, it may happen that we'll have no selection
\r
899 // available. We normalize it here by replicating the
\r
900 // behavior of other browsers.
\r
901 if ( !sel.rangeCount )
\r
903 range = new CKEDITOR.dom.range( doc );
\r
904 range.moveToElementEditStart( doc.getBody() );
\r
905 ranges.push( range );
\r
908 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
910 var nativeRange = sel.getRangeAt( i );
\r
912 range = new CKEDITOR.dom.range( doc );
\r
914 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
915 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
916 ranges.push( range );
\r
921 return function( onlyEditables )
\r
923 var cache = this._.cache;
\r
924 if ( cache.ranges && !onlyEditables )
\r
925 return cache.ranges;
\r
926 else if ( !cache.ranges )
\r
927 cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );
\r
929 // Split range into multiple by read-only nodes.
\r
930 if ( onlyEditables )
\r
932 var ranges = cache.ranges;
\r
933 for ( var i = 0; i < ranges.length; i++ )
\r
935 var range = ranges[ i ];
\r
937 // Drop range spans inside one ready-only node.
\r
938 var parent = range.getCommonAncestor();
\r
939 if ( parent.isReadOnly() )
\r
940 ranges.splice( i, 1 );
\r
942 if ( range.collapsed )
\r
945 var startContainer = range.startContainer,
\r
946 endContainer = range.endContainer,
\r
947 startOffset = range.startOffset,
\r
948 endOffset = range.endOffset,
\r
949 walkerRange = range.clone();
\r
951 // Range may start inside a non-editable element, restart range
\r
952 // by the end of it.
\r
954 if ( ( readOnly = startContainer.isReadOnly() ) )
\r
955 range.setStartAfter( readOnly );
\r
957 // Enlarge range start/end with text node to avoid walker
\r
958 // being DOM destructive, it doesn't interfere our checking
\r
959 // of elements below as well.
\r
960 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
962 if ( startOffset >= startContainer.getLength() )
\r
963 walkerRange.setStartAfter( startContainer );
\r
965 walkerRange.setStartBefore( startContainer );
\r
968 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
971 walkerRange.setEndBefore( endContainer );
\r
973 walkerRange.setEndAfter( endContainer );
\r
976 // Looking for non-editable element inside the range.
\r
977 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
978 walker.evaluator = function( node )
\r
980 if ( node.type == CKEDITOR.NODE_ELEMENT
\r
981 && node.isReadOnly() )
\r
983 var newRange = range.clone();
\r
984 range.setEndBefore( node );
\r
986 // Drop collapsed range around read-only elements,
\r
987 // it make sure the range list empty when selecting
\r
988 // only non-editable elements.
\r
989 if ( range.collapsed )
\r
990 ranges.splice( i--, 1 );
\r
992 // Avoid creating invalid range.
\r
993 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )
\r
995 newRange.setStartAfter( node );
\r
996 if ( !newRange.collapsed )
\r
997 ranges.splice( i + 1, 0, newRange );
\r
1010 return cache.ranges;
\r
1015 * Gets the DOM element in which the selection starts.
\r
1016 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
1019 * var element = editor.getSelection().<b>getStartElement()</b>;
\r
1020 * alert( element.getName() );
\r
1022 getStartElement : function()
\r
1024 var cache = this._.cache;
\r
1025 if ( cache.startElement !== undefined )
\r
1026 return cache.startElement;
\r
1029 sel = this.getNative();
\r
1031 switch ( this.getType() )
\r
1033 case CKEDITOR.SELECTION_ELEMENT :
\r
1034 return this.getSelectedElement();
\r
1036 case CKEDITOR.SELECTION_TEXT :
\r
1038 var range = this.getRanges()[0];
\r
1042 if ( !range.collapsed )
\r
1046 // Decrease the range content to exclude particial
\r
1047 // selected node on the start which doesn't have
\r
1048 // visual impact. ( #3231 )
\r
1051 var startContainer = range.startContainer,
\r
1052 startOffset = range.startOffset;
\r
1053 // Limit the fix only to non-block elements.(#3950)
\r
1054 if ( startOffset == ( startContainer.getChildCount ?
\r
1055 startContainer.getChildCount() : startContainer.getLength() )
\r
1056 && !startContainer.isBlockBoundary() )
\r
1057 range.setStartAfter( startContainer );
\r
1061 node = range.startContainer;
\r
1063 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
1064 return node.getParent();
\r
1066 node = node.getChild( range.startOffset );
\r
1068 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
1069 node = range.startContainer;
\r
1072 var child = node.getFirst();
\r
1073 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
1076 child = child.getFirst();
\r
1082 node = range.startContainer;
\r
1083 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
1084 node = node.getParent();
\r
1091 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
1095 * Gets the current selected element.
\r
1096 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
1097 * selection is available or the selection type is not
\r
1098 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
1100 * var element = editor.getSelection().<b>getSelectedElement()</b>;
\r
1101 * alert( element.getName() );
\r
1103 getSelectedElement : function()
\r
1105 var cache = this._.cache;
\r
1106 if ( cache.selectedElement !== undefined )
\r
1107 return cache.selectedElement;
\r
1111 var node = CKEDITOR.tools.tryThese(
\r
1112 // Is it native IE control type selection?
\r
1115 return self.getNative().createRange().item( 0 );
\r
1117 // Figure it out by checking if there's a single enclosed
\r
1118 // node of the range.
\r
1121 var range = self.getRanges()[ 0 ],
\r
1125 // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>
\r
1126 for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )
\r
1127 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )
\r
1128 && styleObjectElements[ enclosed.getName() ]
\r
1129 && ( selected = enclosed ) ); i-- )
\r
1131 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]
\r
1132 range.shrink( CKEDITOR.SHRINK_ELEMENT );
\r
1135 return selected.$;
\r
1138 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
1143 // Call all cacheable function.
\r
1145 this.getStartElement();
\r
1146 this.getSelectedElement();
\r
1148 // The native selection is not available when locked.
\r
1149 this._.cache.nativeSel = {};
\r
1151 this.isLocked = 1;
\r
1153 // Save this selection inside the DOM document.
\r
1154 this.document.setCustomData( 'cke_locked_selection', this );
\r
1157 unlock : function( restore )
\r
1159 var doc = this.document,
\r
1160 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
1162 if ( lockedSelection )
\r
1164 doc.setCustomData( 'cke_locked_selection', null );
\r
1168 var selectedElement = lockedSelection.getSelectedElement(),
\r
1169 ranges = !selectedElement && lockedSelection.getRanges();
\r
1171 this.isLocked = 0;
\r
1174 doc.getBody().focus();
\r
1176 if ( selectedElement )
\r
1177 this.selectElement( selectedElement );
\r
1179 this.selectRanges( ranges );
\r
1183 if ( !lockedSelection || !restore )
\r
1185 this.isLocked = 0;
\r
1190 reset : function()
\r
1192 this._.cache = {};
\r
1196 * Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.
\r
1199 selectElement : function( element )
\r
1201 if ( this.isLocked )
\r
1203 var range = new CKEDITOR.dom.range( this.document );
\r
1204 range.setStartBefore( element );
\r
1205 range.setEndAfter( element );
\r
1207 this._.cache.selectedElement = element;
\r
1208 this._.cache.startElement = element;
\r
1209 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );
\r
1210 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
1215 range = new CKEDITOR.dom.range( element.getDocument() );
\r
1216 range.setStartBefore( element );
\r
1217 range.setEndAfter( element );
\r
1220 this.document.fire( 'selectionchange' );
\r
1226 * Adding the specified ranges to document selection preceding
\r
1227 * by clearing up the original selection.
\r
1228 * @param {CKEDITOR.dom.range} ranges
\r
1230 selectRanges : function( ranges )
\r
1232 if ( this.isLocked )
\r
1234 this._.cache.selectedElement = null;
\r
1235 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();
\r
1236 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );
\r
1237 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
1242 if ( CKEDITOR.env.ie )
\r
1244 if ( ranges.length > 1 )
\r
1246 // IE doesn't accept multiple ranges selection, so we join all into one.
\r
1247 var last = ranges[ ranges.length -1 ] ;
\r
1248 ranges[ 0 ].setEnd( last.endContainer, last.endOffset );
\r
1249 ranges.length = 1;
\r
1252 if ( ranges[ 0 ] )
\r
1253 ranges[ 0 ].select();
\r
1259 var sel = this.getNative();
\r
1261 // getNative() returns null if iframe is "display:none" in FF. (#6577)
\r
1265 if ( ranges.length )
\r
1267 sel.removeAllRanges();
\r
1268 // Remove any existing filling char first.
\r
1269 CKEDITOR.env.webkit && removeFillingChar( this.document );
\r
1272 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1274 // Joining sequential ranges introduced by
\r
1275 // readonly elements protection.
\r
1276 if ( i < ranges.length -1 )
\r
1278 var left = ranges[ i ], right = ranges[ i +1 ],
\r
1279 between = left.clone();
\r
1280 between.setStart( left.endContainer, left.endOffset );
\r
1281 between.setEnd( right.startContainer, right.startOffset );
\r
1283 // Don't confused by Firefox adjancent multi-ranges
\r
1284 // introduced by table cells selection.
\r
1285 if ( !between.collapsed )
\r
1287 between.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1288 var ancestor = between.getCommonAncestor(),
\r
1289 enclosed = between.getEnclosedNode();
\r
1291 // The following cases has to be considered:
\r
1292 // 1. <span contenteditable="false">[placeholder]</span>
\r
1293 // 2. <input contenteditable="false" type="radio"/> (#6621)
\r
1294 if ( ancestor.isReadOnly() || enclosed && enclosed.isReadOnly() )
\r
1296 right.setStart( left.startContainer, left.startOffset );
\r
1297 ranges.splice( i--, 1 );
\r
1303 var range = ranges[ i ];
\r
1304 var nativeRange = this.document.$.createRange();
\r
1305 var startContainer = range.startContainer;
\r
1307 // In FF2, if we have a collapsed range, inside an empty
\r
1308 // element, we must add something to it otherwise the caret
\r
1309 // will not be visible.
\r
1310 // In Opera instead, the selection will be moved out of the
\r
1311 // element. (#4657)
\r
1312 if ( range.collapsed &&
\r
1313 ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&
\r
1314 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
1315 !startContainer.getChildCount() )
\r
1317 startContainer.appendText( '' );
\r
1320 if ( range.collapsed
\r
1321 && CKEDITOR.env.webkit
\r
1322 && rangeRequiresFix( range ) )
\r
1324 // Append a zero-width space so WebKit will not try to
\r
1325 // move the selection by itself (#1272).
\r
1326 var fillingChar = createFillingChar( this.document );
\r
1327 range.insertNode( fillingChar ) ;
\r
1329 var next = fillingChar.getNext();
\r
1331 // If the filling char is followed by a <br>, whithout
\r
1332 // having something before it, it'll not blink.
\r
1333 // Let's remove it in this case.
\r
1334 if ( next && !fillingChar.getPrevious() && next.type == CKEDITOR.NODE_ELEMENT && next.getName() == 'br' )
\r
1336 removeFillingChar( this.document );
\r
1337 range.moveToPosition( next, CKEDITOR.POSITION_BEFORE_START );
\r
1340 range.moveToPosition( fillingChar, CKEDITOR.POSITION_AFTER_END );
\r
1343 nativeRange.setStart( range.startContainer.$, range.startOffset );
\r
1347 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1351 // There is a bug in Firefox implementation (it would be too easy
\r
1352 // otherwise). The new start can't be after the end (W3C says it can).
\r
1353 // So, let's create a new range and collapse it to the desired point.
\r
1354 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )
\r
1356 range.collapse( 1 );
\r
1357 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1363 // Select the range.
\r
1364 sel.addRange( nativeRange );
\r
1372 * Create bookmark for every single of this selection range (from #getRanges)
\r
1373 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,
\r
1374 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1375 * received as with the underlay range method.
\r
1377 createBookmarks : function( serializable )
\r
1379 return this.getRanges().createBookmarks( serializable );
\r
1383 * Create bookmark for every single of this selection range (from #getRanges)
\r
1384 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,
\r
1385 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1386 * received as with the underlay range method.
\r
1388 createBookmarks2 : function( normalized )
\r
1390 return this.getRanges().createBookmarks2( normalized );
\r
1394 * Select the virtual ranges denote by the bookmarks by calling #selectRanges.
\r
1395 * @param bookmarks
\r
1397 selectBookmarks : function( bookmarks )
\r
1400 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
1402 var range = new CKEDITOR.dom.range( this.document );
\r
1403 range.moveToBookmark( bookmarks[i] );
\r
1404 ranges.push( range );
\r
1406 this.selectRanges( ranges );
\r
1411 * Retrieve the common ancestor node of the first range and the last range.
\r
1413 getCommonAncestor : function()
\r
1415 var ranges = this.getRanges(),
\r
1416 startNode = ranges[ 0 ].startContainer,
\r
1417 endNode = ranges[ ranges.length - 1 ].endContainer;
\r
1418 return startNode.getCommonAncestor( endNode );
\r
1422 * Moving scroll bar to the current selection's start position.
\r
1424 scrollIntoView : function()
\r
1426 // If we have split the block, adds a temporary span at the
\r
1427 // range position and scroll relatively to it.
\r
1428 var start = this.getStartElement();
\r
1429 start.scrollIntoView();
\r
1436 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1437 fillerTextRegex = /\ufeff|\u00a0/,
\r
1438 nonCells = { table:1,tbody:1,tr:1 };
\r
1440 CKEDITOR.dom.range.prototype.select =
\r
1443 function( forceExpand )
\r
1445 var collapsed = this.collapsed,
\r
1446 isStartMarkerAlone, dummySpan, ieRange;
\r
1448 // Try to make a object selection.
\r
1449 var selected = this.getEnclosedNode();
\r
1454 ieRange = this.document.$.body.createControlRange();
\r
1455 ieRange.addElement( selected.$ );
\r
1462 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.
\r
1463 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...
\r
1464 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells
\r
1465 || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )
\r
1467 this.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1470 var bookmark = this.createBookmark();
\r
1472 // Create marker tags for the start and end boundaries.
\r
1473 var startNode = bookmark.startNode;
\r
1477 endNode = bookmark.endNode;
\r
1479 // Create the main range which will be used for the selection.
\r
1480 ieRange = this.document.$.body.createTextRange();
\r
1482 // Position the range at the start boundary.
\r
1483 ieRange.moveToElementText( startNode.$ );
\r
1484 ieRange.moveStart( 'character', 1 );
\r
1488 // Create a tool range for the end.
\r
1489 var ieRangeEnd = this.document.$.body.createTextRange();
\r
1491 // Position the tool range at the end.
\r
1492 ieRangeEnd.moveToElementText( endNode.$ );
\r
1494 // Move the end boundary of the main range to match the tool range.
\r
1495 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
1496 ieRange.moveEnd( 'character', -1 );
\r
1500 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
1501 // will expand and that the cursor will be blinking on the right place.
\r
1502 // Actually, we are using this flag just to avoid using this hack in all
\r
1503 // situations, but just on those needed.
\r
1504 var next = startNode.getNext( notWhitespaces );
\r
1505 isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?
\r
1506 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );
\r
1508 // Append a temporary <span></span> before the selection.
\r
1509 // This is needed to avoid IE destroying selections inside empty
\r
1510 // inline elements, like <b></b> (#253).
\r
1511 // It is also needed when placing the selection right after an inline
\r
1512 // element to avoid the selection moving inside of it.
\r
1513 dummySpan = this.document.createElement( 'span' );
\r
1514 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1515 dummySpan.insertBefore( startNode );
\r
1517 if ( isStartMarkerAlone )
\r
1519 // To expand empty blocks or line spaces after <br>, we need
\r
1520 // instead to have any char, which will be later deleted using the
\r
1522 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1523 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1527 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1528 this.setStartBefore( startNode );
\r
1529 startNode.remove();
\r
1533 if ( isStartMarkerAlone )
\r
1535 // Move the selection start to include the temporary \ufeff.
\r
1536 ieRange.moveStart( 'character', -1 );
\r
1540 // Remove our temporary stuff.
\r
1541 this.document.$.selection.clear();
\r
1546 this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );
\r
1547 dummySpan.remove();
\r
1551 this.setEndBefore( endNode );
\r
1556 this.document.fire( 'selectionchange' );
\r
1561 this.document.getSelection().selectRanges( [ this ] );
\r