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
75 modes : { wysiwyg : 1, source : 1 },
\r
76 exec : function( editor )
\r
78 switch ( editor.mode )
\r
81 editor.document.$.execCommand( 'SelectAll', false, null );
\r
82 // Force triggering selectionChange (#7008)
\r
83 editor.forceNextSelectionCheck();
\r
84 editor.selectionChange();
\r
87 // Select the contents of the textarea
\r
88 var textarea = editor.textarea.$;
\r
89 if ( CKEDITOR.env.ie )
\r
90 textarea.createTextRange().execCommand( 'SelectAll' );
\r
93 textarea.selectionStart = 0;
\r
94 textarea.selectionEnd = textarea.value.length;
\r
102 CKEDITOR.plugins.add( 'selection',
\r
104 init : function( editor )
\r
106 editor.on( 'contentDom', function()
\r
108 var doc = editor.document,
\r
109 body = doc.getBody(),
\r
110 html = doc.getDocumentElement();
\r
112 if ( CKEDITOR.env.ie )
\r
114 // Other browsers don't loose the selection if the
\r
115 // editor document loose the focus. In IE, we don't
\r
116 // have support for it, so we reproduce it here, other
\r
117 // than firing the selection change event.
\r
121 restoreEnabled = 1;
\r
123 // "onfocusin" is fired before "onfocus". It makes it
\r
124 // possible to restore the selection before click
\r
125 // events get executed.
\r
126 body.on( 'focusin', function( evt )
\r
128 // If there are elements with layout they fire this event but
\r
129 // it must be ignored to allow edit its contents #4682
\r
130 if ( evt.data.$.srcElement.nodeName != 'BODY' )
\r
133 // If we have saved a range, restore it at this
\r
137 // Range restored here might invalidate the DOM structure thus break up
\r
138 // the locked selection, give it up. (#6083)
\r
139 var lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
140 if ( restoreEnabled && !lockedSelection )
\r
142 // Well not break because of this.
\r
145 savedRange.select();
\r
155 body.on( 'focus', function()
\r
157 // Enable selections to be saved.
\r
163 body.on( 'beforedeactivate', function( evt )
\r
165 // Ignore this event if it's caused by focus switch between
\r
166 // internal editable control type elements, e.g. layouted paragraph. (#4682)
\r
167 if ( evt.data.$.toElement )
\r
170 // Disable selections from being saved.
\r
172 restoreEnabled = 1;
\r
175 // IE before version 8 will leave cursor blinking inside the document after
\r
176 // editor blurred unless we clean up the selection. (#4716)
\r
177 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
\r
179 editor.on( 'blur', function( evt )
\r
181 // Try/Catch to avoid errors if the editor is hidden. (#6375)
\r
184 editor.document && editor.document.$.selection.empty();
\r
190 // Listening on document element ensures that
\r
191 // scrollbar is included. (#5280)
\r
192 html.on( 'mousedown', function()
\r
194 // Lock restore selection now, as we have
\r
195 // a followed 'click' event which introduce
\r
196 // new selection. (#5735)
\r
197 restoreEnabled = 0;
\r
200 html.on( 'mouseup', function()
\r
202 restoreEnabled = 1;
\r
205 // In IE6/7 the blinking cursor appears, but contents are
\r
206 // not editable. (#5634)
\r
207 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )
\r
209 // The 'click' event is not fired when clicking the
\r
210 // scrollbars, so we can use it to check whether
\r
211 // the empty space following <body> has been clicked.
\r
212 html.on( 'click', function( evt )
\r
214 if ( evt.data.getTarget().getName() == 'html' )
\r
215 editor.getSelection().getRanges()[ 0 ].select();
\r
220 // IE fires the "selectionchange" event when clicking
\r
221 // inside a selection. We don't want to capture that.
\r
222 body.on( 'mousedown', function( evt )
\r
224 // IE scrolls document to top on right mousedown
\r
225 // when editor has no focus, remember this scroll
\r
226 // position and revert it before context menu opens. (#5778)
\r
227 if ( evt.data.$.button == 2 )
\r
229 var sel = editor.document.$.selection;
\r
230 if ( sel.type == 'None' )
\r
231 scroll = editor.window.getScrollPosition();
\r
236 body.on( 'mouseup',
\r
239 // Restore recorded scroll position when needed on right mouseup.
\r
240 if ( evt.data.$.button == 2 && scroll )
\r
242 editor.document.$.documentElement.scrollLeft = scroll.x;
\r
243 editor.document.$.documentElement.scrollTop = scroll.y;
\r
248 setTimeout( function()
\r
250 saveSelection( true );
\r
255 body.on( 'keydown', disableSave );
\r
264 // IE is the only to provide the "selectionchange"
\r
266 doc.on( 'selectionchange', saveSelection );
\r
268 function disableSave()
\r
273 function saveSelection( testIt )
\r
277 var doc = editor.document,
\r
278 sel = editor.getSelection(),
\r
279 nativeSel = sel && sel.getNative();
\r
281 // There is a very specific case, when clicking
\r
282 // inside a text selection. In that case, the
\r
283 // selection collapses at the clicking point,
\r
284 // but the selection object remains in an
\r
285 // unknown state, making createRange return a
\r
286 // range at the very start of the document. In
\r
287 // such situation we have to test the range, to
\r
288 // be sure it's valid.
\r
289 if ( testIt && nativeSel && nativeSel.type == 'None' )
\r
291 // The "InsertImage" command can be used to
\r
292 // test whether the selection is good or not.
\r
293 // If not, it's enough to give some time to
\r
294 // IE to put things in order for us.
\r
295 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
297 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
302 // Avoid saving selection from within text input. (#5747)
\r
304 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
\r
305 && ( parentTag = nativeSel.createRange() )
\r
306 && ( parentTag = parentTag.parentElement() )
\r
307 && ( parentTag = parentTag.nodeName )
\r
308 && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
\r
313 savedRange = nativeSel && sel.getRanges()[ 0 ];
\r
315 checkSelectionChangeTimeout.call( editor );
\r
321 // In other browsers, we make the selection change
\r
322 // check based on other events, like clicks or keys
\r
325 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
326 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
330 // Clear the cached range path before unload. (#7174)
\r
331 editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );
\r
333 editor.addCommand( 'selectAll', selectAllCmd );
\r
334 editor.ui.addButton( 'SelectAll',
\r
336 label : editor.lang.selectAll,
\r
337 command : 'selectAll'
\r
340 editor.selectionChange = checkSelectionChangeTimeout;
\r
345 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
346 * @returns {CKEDITOR.dom.selection} A selection object or null if not on
\r
347 * WYSIWYG mode or no selection is available.
\r
349 * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;
\r
350 * alert( selection.getType() );
\r
352 CKEDITOR.editor.prototype.getSelection = function()
\r
354 return this.document && this.document.getSelection();
\r
357 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
359 delete this._.selectionPreviousPath;
\r
363 * Gets the current selection from the document.
\r
364 * @returns {CKEDITOR.dom.selection} A selection object.
\r
366 * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;
\r
367 * alert( selection.getType() );
\r
369 CKEDITOR.dom.document.prototype.getSelection = function()
\r
371 var sel = new CKEDITOR.dom.selection( this );
\r
372 return ( !sel || sel.isInvalid ) ? null : sel;
\r
379 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
380 * alert( 'Nothing is selected' );
\r
382 CKEDITOR.SELECTION_NONE = 1;
\r
385 * Text or collapsed selection.
\r
388 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
389 * alert( 'Text is selected' );
\r
391 CKEDITOR.SELECTION_TEXT = 2;
\r
394 * Element selection.
\r
397 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
398 * alert( 'An element is selected' );
\r
400 CKEDITOR.SELECTION_ELEMENT = 3;
\r
403 * Manipulates the selection in a DOM document.
\r
407 CKEDITOR.dom.selection = function( document )
\r
409 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
411 if ( lockedSelection )
\r
412 return lockedSelection;
\r
414 this.document = document;
\r
422 * IE BUG: The selection's document may be a different document than the
\r
423 * editor document. Return null if that's the case.
\r
425 if ( CKEDITOR.env.ie )
\r
427 var range = this.getNative().createRange();
\r
429 || ( range.item && range.item(0).ownerDocument != this.document.$ )
\r
430 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
432 this.isInvalid = true;
\r
439 var styleObjectElements =
\r
441 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
\r
442 a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1
\r
445 CKEDITOR.dom.selection.prototype =
\r
448 * Gets the native selection object from the browser.
\r
450 * @returns {Object} The native selection object.
\r
452 * var selection = editor.getSelection().<b>getNative()</b>;
\r
458 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
463 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
467 * Gets the type of the current selection. The following values are
\r
470 * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>
\r
471 * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or
\r
472 * collapsed selection.</li>
\r
473 * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element
\r
477 * @returns {Number} One of the following constant values:
\r
478 * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or
\r
479 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
481 * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )
\r
482 * alert( 'Text is selected' );
\r
488 var cache = this._.cache;
\r
492 var type = CKEDITOR.SELECTION_NONE;
\r
496 var sel = this.getNative(),
\r
499 if ( ieType == 'Text' )
\r
500 type = CKEDITOR.SELECTION_TEXT;
\r
502 if ( ieType == 'Control' )
\r
503 type = CKEDITOR.SELECTION_ELEMENT;
\r
505 // It is possible that we can still get a text range
\r
506 // object even when type == 'None' is returned by IE.
\r
507 // So we'd better check the object returned by
\r
508 // createRange() rather than by looking at the type.
\r
509 if ( sel.createRange().parentElement )
\r
510 type = CKEDITOR.SELECTION_TEXT;
\r
514 return ( cache.type = type );
\r
519 var cache = this._.cache;
\r
523 var type = CKEDITOR.SELECTION_TEXT;
\r
525 var sel = this.getNative();
\r
528 type = CKEDITOR.SELECTION_NONE;
\r
529 else if ( sel.rangeCount == 1 )
\r
531 // Check if the actual selection is a control (IMG,
\r
532 // TABLE, HR, etc...).
\r
534 var range = sel.getRangeAt(0),
\r
535 startContainer = range.startContainer;
\r
537 if ( startContainer == range.endContainer
\r
538 && startContainer.nodeType == 1
\r
539 && ( range.endOffset - range.startOffset ) == 1
\r
540 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
542 type = CKEDITOR.SELECTION_ELEMENT;
\r
546 return ( cache.type = type );
\r
550 * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.
\r
551 * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns
\r
552 * one range for each table cell when one or more table row is selected.
\r
555 * var ranges = selection.getRanges();
\r
556 * alert(ranges.length);
\r
558 getRanges : (function()
\r
560 var func = CKEDITOR.env.ie ?
\r
563 function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }
\r
565 // Finds the container and offset for a specific boundary
\r
567 var getBoundaryInformation = function( range, start )
\r
569 // Creates a collapsed range at the requested boundary.
\r
570 range = range.duplicate();
\r
571 range.collapse( start );
\r
573 // Gets the element that encloses the range entirely.
\r
574 var parent = range.parentElement();
\r
576 // Empty parent element, e.g. <i>^</i>
\r
577 if ( !parent.hasChildNodes() )
\r
578 return { container : parent, offset : 0 };
\r
580 var siblings = parent.children,
\r
582 testRange = range.duplicate(),
\r
584 endIndex = siblings.length - 1,
\r
589 // Binary search over all element childs to test the range to see whether
\r
590 // range is right on the boundary of one element.
\r
591 while ( startIndex <= endIndex )
\r
593 index = Math.floor( ( startIndex + endIndex ) / 2 );
\r
594 child = siblings[ index ];
\r
595 testRange.moveToElementText( child );
\r
596 position = testRange.compareEndPoints( 'StartToStart', range );
\r
598 if ( position > 0 )
\r
599 endIndex = index - 1;
\r
600 else if ( position < 0 )
\r
601 startIndex = index + 1;
\r
603 return { container : parent, offset : getNodeIndex( child ) };
\r
606 // All childs are text nodes,
\r
607 // or to the right hand of test range are all text nodes. (#6992)
\r
608 if ( index == -1 || index == siblings.length - 1 && position < 0 )
\r
610 // Adapt test range to embrace the entire parent contents.
\r
611 testRange.moveToElementText( parent );
\r
612 testRange.setEndPoint( 'StartToStart', range );
\r
614 // IE report line break as CRLF with range.text but
\r
615 // only LF with textnode.nodeValue, normalize them to avoid
\r
616 // breaking character counting logic below. (#3949)
\r
617 distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
619 siblings = parent.childNodes;
\r
621 // Actual range anchor right beside test range at the boundary of text node.
\r
624 child = siblings[ siblings.length - 1 ];
\r
626 if ( child.nodeType == CKEDITOR.NODE_ELEMENT )
\r
627 return { container : parent, offset : siblings.length };
\r
629 return { container : child, offset : child.nodeValue.length };
\r
632 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
633 var i = siblings.length;
\r
634 while ( distance > 0 )
\r
635 distance -= siblings[ --i ].nodeValue.length;
\r
637 return { container : siblings[ i ], offset : -distance };
\r
639 // Test range was one offset beyond OR behind the anchored text node.
\r
642 // Adapt one side of test range to the actual range
\r
643 // for measuring the offset between them.
\r
644 testRange.collapse( position > 0 ? true : false );
\r
645 testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );
\r
647 // IE report line break as CRLF with range.text but
\r
648 // only LF with textnode.nodeValue, normalize them to avoid
\r
649 // breaking character counting logic below. (#3949)
\r
650 distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
652 // Actual range anchor right beside test range at the inner boundary of text node.
\r
654 return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };
\r
656 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
657 while ( distance > 0 )
\r
659 child = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];
\r
662 distance -= child.nodeValue.length;
\r
664 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)
\r
667 return { container : parent, offset : getNodeIndex( child ) };
\r
671 return { container : child, offset : position > 0 ? -distance : child.nodeValue.length + distance };
\r
677 // IE doesn't have range support (in the W3C way), so we
\r
678 // need to do some magic to transform selections into
\r
679 // CKEDITOR.dom.range instances.
\r
681 var sel = this.getNative(),
\r
682 nativeRange = sel && sel.createRange(),
\r
683 type = this.getType(),
\r
689 if ( type == CKEDITOR.SELECTION_TEXT )
\r
691 range = new CKEDITOR.dom.range( this.document );
\r
693 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
694 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
696 boundaryInfo = getBoundaryInformation( nativeRange );
\r
697 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
699 // Correct an invalid IE range case on empty list item. (#5850)
\r
700 if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING
\r
701 && range.endOffset <= range.startContainer.getIndex() )
\r
708 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
712 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
714 var element = nativeRange.item( i ),
\r
715 parentElement = element.parentNode,
\r
718 range = new CKEDITOR.dom.range( this.document );
\r
720 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
723 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
724 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
725 retval.push( range );
\r
738 // On browsers implementing the W3C range, we simply
\r
739 // tranform the native ranges in CKEDITOR.dom.range
\r
744 doc = this.document,
\r
745 sel = this.getNative();
\r
750 // On WebKit, it may happen that we'll have no selection
\r
751 // available. We normalize it here by replicating the
\r
752 // behavior of other browsers.
\r
753 if ( !sel.rangeCount )
\r
755 range = new CKEDITOR.dom.range( doc );
\r
756 range.moveToElementEditStart( doc.getBody() );
\r
757 ranges.push( range );
\r
760 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
762 var nativeRange = sel.getRangeAt( i );
\r
764 range = new CKEDITOR.dom.range( doc );
\r
766 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
767 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
768 ranges.push( range );
\r
773 return function( onlyEditables )
\r
775 var cache = this._.cache;
\r
776 if ( cache.ranges && !onlyEditables )
\r
777 return cache.ranges;
\r
778 else if ( !cache.ranges )
\r
779 cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );
\r
781 // Split range into multiple by read-only nodes.
\r
782 if ( onlyEditables )
\r
784 var ranges = cache.ranges;
\r
785 for ( var i = 0; i < ranges.length; i++ )
\r
787 var range = ranges[ i ];
\r
789 // Drop range spans inside one ready-only node.
\r
790 var parent = range.getCommonAncestor();
\r
791 if ( parent.isReadOnly() )
\r
792 ranges.splice( i, 1 );
\r
794 if ( range.collapsed )
\r
797 var startContainer = range.startContainer,
\r
798 endContainer = range.endContainer,
\r
799 startOffset = range.startOffset,
\r
800 endOffset = range.endOffset,
\r
801 walkerRange = range.clone();
\r
803 // Range may start inside a non-editable element, restart range
\r
804 // by the end of it.
\r
806 if ( ( readOnly = startContainer.isReadOnly() ) )
\r
807 range.setStartAfter( readOnly );
\r
809 // Enlarge range start/end with text node to avoid walker
\r
810 // being DOM destructive, it doesn't interfere our checking
\r
811 // of elements below as well.
\r
812 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
814 if ( startOffset >= startContainer.getLength() )
\r
815 walkerRange.setStartAfter( startContainer );
\r
817 walkerRange.setStartBefore( startContainer );
\r
820 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
823 walkerRange.setEndBefore( endContainer );
\r
825 walkerRange.setEndAfter( endContainer );
\r
828 // Looking for non-editable element inside the range.
\r
829 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
830 walker.evaluator = function( node )
\r
832 if ( node.type == CKEDITOR.NODE_ELEMENT
\r
833 && node.isReadOnly() )
\r
835 var newRange = range.clone();
\r
836 range.setEndBefore( node );
\r
838 // Drop collapsed range around read-only elements,
\r
839 // it make sure the range list empty when selecting
\r
840 // only non-editable elements.
\r
841 if ( range.collapsed )
\r
842 ranges.splice( i--, 1 );
\r
844 // Avoid creating invalid range.
\r
845 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )
\r
847 newRange.setStartAfter( node );
\r
848 if ( !newRange.collapsed )
\r
849 ranges.splice( i + 1, 0, newRange );
\r
862 return cache.ranges;
\r
867 * Gets the DOM element in which the selection starts.
\r
868 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
871 * var element = editor.getSelection().<b>getStartElement()</b>;
\r
872 * alert( element.getName() );
\r
874 getStartElement : function()
\r
876 var cache = this._.cache;
\r
877 if ( cache.startElement !== undefined )
\r
878 return cache.startElement;
\r
881 sel = this.getNative();
\r
883 switch ( this.getType() )
\r
885 case CKEDITOR.SELECTION_ELEMENT :
\r
886 return this.getSelectedElement();
\r
888 case CKEDITOR.SELECTION_TEXT :
\r
890 var range = this.getRanges()[0];
\r
894 if ( !range.collapsed )
\r
898 // Decrease the range content to exclude particial
\r
899 // selected node on the start which doesn't have
\r
900 // visual impact. ( #3231 )
\r
903 var startContainer = range.startContainer,
\r
904 startOffset = range.startOffset;
\r
905 // Limit the fix only to non-block elements.(#3950)
\r
906 if ( startOffset == ( startContainer.getChildCount ?
\r
907 startContainer.getChildCount() : startContainer.getLength() )
\r
908 && !startContainer.isBlockBoundary() )
\r
909 range.setStartAfter( startContainer );
\r
913 node = range.startContainer;
\r
915 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
916 return node.getParent();
\r
918 node = node.getChild( range.startOffset );
\r
920 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
921 node = range.startContainer;
\r
924 var child = node.getFirst();
\r
925 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
928 child = child.getFirst();
\r
934 node = range.startContainer;
\r
935 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
936 node = node.getParent();
\r
943 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
947 * Gets the current selected element.
\r
948 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
949 * selection is available or the selection type is not
\r
950 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
952 * var element = editor.getSelection().<b>getSelectedElement()</b>;
\r
953 * alert( element.getName() );
\r
955 getSelectedElement : function()
\r
957 var cache = this._.cache;
\r
958 if ( cache.selectedElement !== undefined )
\r
959 return cache.selectedElement;
\r
963 var node = CKEDITOR.tools.tryThese(
\r
964 // Is it native IE control type selection?
\r
967 return self.getNative().createRange().item( 0 );
\r
969 // Figure it out by checking if there's a single enclosed
\r
970 // node of the range.
\r
973 var range = self.getRanges()[ 0 ],
\r
977 // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>
\r
978 for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )
\r
979 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )
\r
980 && styleObjectElements[ enclosed.getName() ]
\r
981 && ( selected = enclosed ) ); i-- )
\r
983 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]
\r
984 range.shrink( CKEDITOR.SHRINK_ELEMENT );
\r
990 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
995 // Call all cacheable function.
\r
997 this.getStartElement();
\r
998 this.getSelectedElement();
\r
1000 // The native selection is not available when locked.
\r
1001 this._.cache.nativeSel = {};
\r
1003 this.isLocked = 1;
\r
1005 // Save this selection inside the DOM document.
\r
1006 this.document.setCustomData( 'cke_locked_selection', this );
\r
1009 unlock : function( restore )
\r
1011 var doc = this.document,
\r
1012 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
1014 if ( lockedSelection )
\r
1016 doc.setCustomData( 'cke_locked_selection', null );
\r
1020 var selectedElement = lockedSelection.getSelectedElement(),
\r
1021 ranges = !selectedElement && lockedSelection.getRanges();
\r
1023 this.isLocked = 0;
\r
1026 doc.getBody().focus();
\r
1028 if ( selectedElement )
\r
1029 this.selectElement( selectedElement );
\r
1031 this.selectRanges( ranges );
\r
1035 if ( !lockedSelection || !restore )
\r
1037 this.isLocked = 0;
\r
1042 reset : function()
\r
1044 this._.cache = {};
\r
1048 * Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.
\r
1051 selectElement : function( element )
\r
1053 if ( this.isLocked )
\r
1055 var range = new CKEDITOR.dom.range( this.document );
\r
1056 range.setStartBefore( element );
\r
1057 range.setEndAfter( element );
\r
1059 this._.cache.selectedElement = element;
\r
1060 this._.cache.startElement = element;
\r
1061 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );
\r
1062 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
1067 if ( CKEDITOR.env.ie )
\r
1069 this.getNative().empty();
\r
1073 // Try to select the node as a control.
\r
1074 range = this.document.$.body.createControlRange();
\r
1075 range.addElement( element.$ );
\r
1080 // If failed, select it as a text range.
\r
1081 range = this.document.$.body.createTextRange();
\r
1082 range.moveToElementText( element.$ );
\r
1087 this.document.fire( 'selectionchange' );
\r
1094 // Create the range for the element.
\r
1095 range = this.document.$.createRange();
\r
1096 range.selectNode( element.$ );
\r
1098 // Select the range.
\r
1099 var sel = this.getNative();
\r
1100 sel.removeAllRanges();
\r
1101 sel.addRange( range );
\r
1108 * Adding the specified ranges to document selection preceding
\r
1109 * by clearing up the original selection.
\r
1110 * @param {CKEDITOR.dom.range} ranges
\r
1112 selectRanges : function( ranges )
\r
1114 if ( this.isLocked )
\r
1116 this._.cache.selectedElement = null;
\r
1117 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();
\r
1118 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );
\r
1119 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
1124 if ( CKEDITOR.env.ie )
\r
1126 if ( ranges.length > 1 )
\r
1128 // IE doesn't accept multiple ranges selection, so we join all into one.
\r
1129 var last = ranges[ ranges.length -1 ] ;
\r
1130 ranges[ 0 ].setEnd( last.endContainer, last.endOffset );
\r
1131 ranges.length = 1;
\r
1134 if ( ranges[ 0 ] )
\r
1135 ranges[ 0 ].select();
\r
1141 var sel = this.getNative();
\r
1143 if ( ranges.length )
\r
1144 sel.removeAllRanges();
\r
1146 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1148 // Joining sequential ranges introduced by
\r
1149 // readonly elements protection.
\r
1150 if ( i < ranges.length -1 )
\r
1152 var left = ranges[ i ], right = ranges[ i +1 ],
\r
1153 between = left.clone();
\r
1154 between.setStart( left.endContainer, left.endOffset );
\r
1155 between.setEnd( right.startContainer, right.startOffset );
\r
1157 // Don't confused by Firefox adjancent multi-ranges
\r
1158 // introduced by table cells selection.
\r
1159 if ( !between.collapsed )
\r
1161 between.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1162 var ancestor = between.getCommonAncestor(),
\r
1163 enclosed = between.getEnclosedNode();
\r
1165 // The following cases has to be considered:
\r
1166 // 1. <span contenteditable="false">[placeholder]</span>
\r
1167 // 2. <input contenteditable="false" type="radio"/> (#6621)
\r
1168 if ( ancestor.isReadOnly() || enclosed && enclosed.isReadOnly() )
\r
1170 right.setStart( left.startContainer, left.startOffset );
\r
1171 ranges.splice( i--, 1 );
\r
1177 var range = ranges[ i ];
\r
1178 var nativeRange = this.document.$.createRange();
\r
1179 var startContainer = range.startContainer;
\r
1181 // In FF2, if we have a collapsed range, inside an empty
\r
1182 // element, we must add something to it otherwise the caret
\r
1183 // will not be visible.
\r
1184 // In Opera instead, the selection will be moved out of the
\r
1185 // element. (#4657)
\r
1186 if ( range.collapsed &&
\r
1187 ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&
\r
1188 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
1189 !startContainer.getChildCount() )
\r
1191 startContainer.appendText( '' );
\r
1194 nativeRange.setStart( startContainer.$, range.startOffset );
\r
1195 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1197 // Select the range.
\r
1198 sel.addRange( nativeRange );
\r
1206 * Create bookmark for every single of this selection range (from #getRanges)
\r
1207 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,
\r
1208 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1209 * received as with the underlay range method.
\r
1211 createBookmarks : function( serializable )
\r
1213 return this.getRanges().createBookmarks( serializable );
\r
1217 * Create bookmark for every single of this selection range (from #getRanges)
\r
1218 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,
\r
1219 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1220 * received as with the underlay range method.
\r
1222 createBookmarks2 : function( normalized )
\r
1224 return this.getRanges().createBookmarks2( normalized );
\r
1228 * Select the virtual ranges denote by the bookmarks by calling #selectRanges.
\r
1229 * @param bookmarks
\r
1231 selectBookmarks : function( bookmarks )
\r
1234 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
1236 var range = new CKEDITOR.dom.range( this.document );
\r
1237 range.moveToBookmark( bookmarks[i] );
\r
1238 ranges.push( range );
\r
1240 this.selectRanges( ranges );
\r
1245 * Retrieve the common ancestor node of the first range and the last range.
\r
1247 getCommonAncestor : function()
\r
1249 var ranges = this.getRanges(),
\r
1250 startNode = ranges[ 0 ].startContainer,
\r
1251 endNode = ranges[ ranges.length - 1 ].endContainer;
\r
1252 return startNode.getCommonAncestor( endNode );
\r
1256 * Moving scroll bar to the current selection's start position.
\r
1258 scrollIntoView : function()
\r
1260 // If we have split the block, adds a temporary span at the
\r
1261 // range position and scroll relatively to it.
\r
1262 var start = this.getStartElement();
\r
1263 start.scrollIntoView();
\r
1270 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1271 fillerTextRegex = /\ufeff|\u00a0/,
\r
1272 nonCells = { table:1,tbody:1,tr:1 };
\r
1274 CKEDITOR.dom.range.prototype.select =
\r
1277 function( forceExpand )
\r
1279 var collapsed = this.collapsed;
\r
1280 var isStartMarkerAlone;
\r
1283 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.
\r
1284 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...
\r
1285 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells
\r
1286 || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )
\r
1288 this.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1291 var bookmark = this.createBookmark();
\r
1293 // Create marker tags for the start and end boundaries.
\r
1294 var startNode = bookmark.startNode;
\r
1298 endNode = bookmark.endNode;
\r
1300 // Create the main range which will be used for the selection.
\r
1301 var ieRange = this.document.$.body.createTextRange();
\r
1303 // Position the range at the start boundary.
\r
1304 ieRange.moveToElementText( startNode.$ );
\r
1305 ieRange.moveStart( 'character', 1 );
\r
1309 // Create a tool range for the end.
\r
1310 var ieRangeEnd = this.document.$.body.createTextRange();
\r
1312 // Position the tool range at the end.
\r
1313 ieRangeEnd.moveToElementText( endNode.$ );
\r
1315 // Move the end boundary of the main range to match the tool range.
\r
1316 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
1317 ieRange.moveEnd( 'character', -1 );
\r
1321 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
1322 // will expand and that the cursor will be blinking on the right place.
\r
1323 // Actually, we are using this flag just to avoid using this hack in all
\r
1324 // situations, but just on those needed.
\r
1325 var next = startNode.getNext( notWhitespaces );
\r
1326 isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?
\r
1327 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );
\r
1329 // Append a temporary <span></span> before the selection.
\r
1330 // This is needed to avoid IE destroying selections inside empty
\r
1331 // inline elements, like <b></b> (#253).
\r
1332 // It is also needed when placing the selection right after an inline
\r
1333 // element to avoid the selection moving inside of it.
\r
1334 dummySpan = this.document.createElement( 'span' );
\r
1335 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1336 dummySpan.insertBefore( startNode );
\r
1338 if ( isStartMarkerAlone )
\r
1340 // To expand empty blocks or line spaces after <br>, we need
\r
1341 // instead to have any char, which will be later deleted using the
\r
1343 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1344 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1348 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1349 this.setStartBefore( startNode );
\r
1350 startNode.remove();
\r
1354 if ( isStartMarkerAlone )
\r
1356 // Move the selection start to include the temporary \ufeff.
\r
1357 ieRange.moveStart( 'character', -1 );
\r
1361 // Remove our temporary stuff.
\r
1362 this.document.$.selection.clear();
\r
1367 this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );
\r
1368 dummySpan.remove();
\r
1372 this.setEndBefore( endNode );
\r
1377 this.document.fire( 'selectionchange' );
\r
1382 var startContainer = this.startContainer;
\r
1384 // If we have a collapsed range, inside an empty element, we must add
\r
1385 // something to it, otherwise the caret will not be visible.
\r
1386 if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )
\r
1387 startContainer.append( new CKEDITOR.dom.text( '' ) );
\r
1389 var nativeRange = this.document.$.createRange();
\r
1390 nativeRange.setStart( startContainer.$, this.startOffset );
\r
1394 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1398 // There is a bug in Firefox implementation (it would be too easy
\r
1399 // otherwise). The new start can't be after the end (W3C says it can).
\r
1400 // So, let's create a new range and collapse it to the desired point.
\r
1401 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )
\r
1403 this.collapse( true );
\r
1404 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1410 var selection = this.document.getSelection().getNative();
\r
1411 // getSelection() returns null in case when iframe is "display:none" in FF. (#6577)
\r
1414 selection.removeAllRanges();
\r
1415 selection.addRange( nativeRange );
\r