2 Copyright (c) 2003-2010, 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
84 // Select the contents of the textarea
\r
85 var textarea = editor.textarea.$ ;
\r
86 if ( CKEDITOR.env.ie )
\r
88 textarea.createTextRange().execCommand( 'SelectAll' ) ;
\r
92 textarea.selectionStart = 0 ;
\r
93 textarea.selectionEnd = textarea.value.length ;
\r
101 CKEDITOR.plugins.add( 'selection',
\r
103 init : function( editor )
\r
105 editor.on( 'contentDom', function()
\r
107 var doc = editor.document,
\r
108 body = doc.getBody(),
\r
109 html = doc.getDocumentElement();
\r
111 if ( CKEDITOR.env.ie )
\r
113 // Other browsers don't loose the selection if the
\r
114 // editor document loose the focus. In IE, we don't
\r
115 // have support for it, so we reproduce it here, other
\r
116 // than firing the selection change event.
\r
120 restoreEnabled = 1;
\r
122 // "onfocusin" is fired before "onfocus". It makes it
\r
123 // possible to restore the selection before click
\r
124 // events get executed.
\r
125 body.on( 'focusin', function( evt )
\r
127 // If there are elements with layout they fire this event but
\r
128 // it must be ignored to allow edit its contents #4682
\r
129 if ( evt.data.$.srcElement.nodeName != 'BODY' )
\r
132 // If we have saved a range, restore it at this
\r
136 // Range restored here might invalidate the DOM structure thus break up
\r
137 // the locked selection, give it up. (#6083)
\r
138 var lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
139 if ( restoreEnabled && !lockedSelection )
\r
141 // Well not break because of this.
\r
144 savedRange.select();
\r
154 body.on( 'focus', function()
\r
156 // Enable selections to be saved.
\r
162 body.on( 'beforedeactivate', function( evt )
\r
164 // Ignore this event if it's caused by focus switch between
\r
165 // internal editable control type elements, e.g. layouted paragraph. (#4682)
\r
166 if ( evt.data.$.toElement )
\r
169 // Disable selections from being saved.
\r
171 restoreEnabled = 1;
\r
174 // IE before version 8 will leave cursor blinking inside the document after
\r
175 // editor blurred unless we clean up the selection. (#4716)
\r
176 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
\r
178 editor.on( 'blur', function( evt )
\r
180 // Try/Catch to avoid errors if the editor is hidden. (#6375)
\r
183 editor.document && editor.document.$.selection.empty();
\r
189 // Listening on document element ensures that
\r
190 // scrollbar is included. (#5280)
\r
191 html.on( 'mousedown', function()
\r
193 // Lock restore selection now, as we have
\r
194 // a followed 'click' event which introduce
\r
195 // new selection. (#5735)
\r
196 restoreEnabled = 0;
\r
199 html.on( 'mouseup', function()
\r
201 restoreEnabled = 1;
\r
204 // In IE6/7 the blinking cursor appears, but contents are
\r
205 // not editable. (#5634)
\r
206 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )
\r
208 // The 'click' event is not fired when clicking the
\r
209 // scrollbars, so we can use it to check whether
\r
210 // the empty space following <body> has been clicked.
\r
211 html.on( 'click', function( evt )
\r
213 if ( evt.data.getTarget().getName() == 'html' )
\r
214 editor.getSelection().getRanges()[ 0 ].select();
\r
219 // IE fires the "selectionchange" event when clicking
\r
220 // inside a selection. We don't want to capture that.
\r
221 body.on( 'mousedown', function( evt )
\r
223 // IE scrolls document to top on right mousedown
\r
224 // when editor has no focus, remember this scroll
\r
225 // position and revert it before context menu opens. (#5778)
\r
226 if ( evt.data.$.button == 2 )
\r
228 var sel = editor.document.$.selection;
\r
229 if ( sel.type == 'None' )
\r
230 scroll = editor.window.getScrollPosition();
\r
235 body.on( 'mouseup',
\r
238 // Restore recorded scroll position when needed on right mouseup.
\r
239 if ( evt.data.$.button == 2 && scroll )
\r
241 editor.document.$.documentElement.scrollLeft = scroll.x;
\r
242 editor.document.$.documentElement.scrollTop = scroll.y;
\r
247 setTimeout( function()
\r
249 saveSelection( true );
\r
254 body.on( 'keydown', disableSave );
\r
263 // IE is the only to provide the "selectionchange"
\r
265 doc.on( 'selectionchange', saveSelection );
\r
267 function disableSave()
\r
272 function saveSelection( testIt )
\r
276 var doc = editor.document,
\r
277 sel = editor.getSelection(),
\r
278 nativeSel = sel && sel.getNative();
\r
280 // There is a very specific case, when clicking
\r
281 // inside a text selection. In that case, the
\r
282 // selection collapses at the clicking point,
\r
283 // but the selection object remains in an
\r
284 // unknown state, making createRange return a
\r
285 // range at the very start of the document. In
\r
286 // such situation we have to test the range, to
\r
287 // be sure it's valid.
\r
288 if ( testIt && nativeSel && nativeSel.type == 'None' )
\r
290 // The "InsertImage" command can be used to
\r
291 // test whether the selection is good or not.
\r
292 // If not, it's enough to give some time to
\r
293 // IE to put things in order for us.
\r
294 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
296 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
301 // Avoid saving selection from within text input. (#5747)
\r
303 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
\r
304 && ( parentTag = nativeSel.createRange() )
\r
305 && ( parentTag = parentTag.parentElement() )
\r
306 && ( parentTag = parentTag.nodeName )
\r
307 && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
\r
312 savedRange = nativeSel && sel.getRanges()[ 0 ];
\r
314 checkSelectionChangeTimeout.call( editor );
\r
320 // In other browsers, we make the selection change
\r
321 // check based on other events, like clicks or keys
\r
324 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
325 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
329 editor.addCommand( 'selectAll', selectAllCmd );
\r
330 editor.ui.addButton( 'SelectAll',
\r
332 label : editor.lang.selectAll,
\r
333 command : 'selectAll'
\r
336 editor.selectionChange = checkSelectionChangeTimeout;
\r
341 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
342 * @returns {CKEDITOR.dom.selection} A selection object or null if not on
\r
343 * WYSIWYG mode or no selection is available.
\r
345 * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;
\r
346 * alert( selection.getType() );
\r
348 CKEDITOR.editor.prototype.getSelection = function()
\r
350 return this.document && this.document.getSelection();
\r
353 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
355 delete this._.selectionPreviousPath;
\r
359 * Gets the current selection from the document.
\r
360 * @returns {CKEDITOR.dom.selection} A selection object.
\r
362 * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;
\r
363 * alert( selection.getType() );
\r
365 CKEDITOR.dom.document.prototype.getSelection = function()
\r
367 var sel = new CKEDITOR.dom.selection( this );
\r
368 return ( !sel || sel.isInvalid ) ? null : sel;
\r
375 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
376 * alert( 'Nothing is selected' );
\r
378 CKEDITOR.SELECTION_NONE = 1;
\r
381 * Text or collapsed selection.
\r
384 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
385 * alert( 'Text is selected' );
\r
387 CKEDITOR.SELECTION_TEXT = 2;
\r
390 * Element selection.
\r
393 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
394 * alert( 'An element is selected' );
\r
396 CKEDITOR.SELECTION_ELEMENT = 3;
\r
399 * Manipulates the selection in a DOM document.
\r
403 CKEDITOR.dom.selection = function( document )
\r
405 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
407 if ( lockedSelection )
\r
408 return lockedSelection;
\r
410 this.document = document;
\r
418 * IE BUG: The selection's document may be a different document than the
\r
419 * editor document. Return null if that's the case.
\r
421 if ( CKEDITOR.env.ie )
\r
423 var range = this.getNative().createRange();
\r
425 || ( range.item && range.item(0).ownerDocument != this.document.$ )
\r
426 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
428 this.isInvalid = true;
\r
435 var styleObjectElements =
\r
437 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
\r
438 a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1
\r
441 CKEDITOR.dom.selection.prototype =
\r
444 * Gets the native selection object from the browser.
\r
446 * @returns {Object} The native selection object.
\r
448 * var selection = editor.getSelection().<b>getNative()</b>;
\r
454 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
459 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
463 * Gets the type of the current selection. The following values are
\r
466 * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>
\r
467 * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or
\r
468 * collapsed selection.</li>
\r
469 * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element
\r
473 * @returns {Number} One of the following constant values:
\r
474 * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or
\r
475 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
477 * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )
\r
478 * alert( 'Text is selected' );
\r
484 var cache = this._.cache;
\r
488 var type = CKEDITOR.SELECTION_NONE;
\r
492 var sel = this.getNative(),
\r
495 if ( ieType == 'Text' )
\r
496 type = CKEDITOR.SELECTION_TEXT;
\r
498 if ( ieType == 'Control' )
\r
499 type = CKEDITOR.SELECTION_ELEMENT;
\r
501 // It is possible that we can still get a text range
\r
502 // object even when type == 'None' is returned by IE.
\r
503 // So we'd better check the object returned by
\r
504 // createRange() rather than by looking at the type.
\r
505 if ( sel.createRange().parentElement )
\r
506 type = CKEDITOR.SELECTION_TEXT;
\r
510 return ( cache.type = type );
\r
515 var cache = this._.cache;
\r
519 var type = CKEDITOR.SELECTION_TEXT;
\r
521 var sel = this.getNative();
\r
524 type = CKEDITOR.SELECTION_NONE;
\r
525 else if ( sel.rangeCount == 1 )
\r
527 // Check if the actual selection is a control (IMG,
\r
528 // TABLE, HR, etc...).
\r
530 var range = sel.getRangeAt(0),
\r
531 startContainer = range.startContainer;
\r
533 if ( startContainer == range.endContainer
\r
534 && startContainer.nodeType == 1
\r
535 && ( range.endOffset - range.startOffset ) == 1
\r
536 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
538 type = CKEDITOR.SELECTION_ELEMENT;
\r
542 return ( cache.type = type );
\r
546 * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.
\r
547 * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns
\r
548 * one range for each table cell when one or more table row is selected.
\r
551 * var ranges = selection.getRanges();
\r
552 * alert(ranges.length);
\r
554 getRanges : (function()
\r
556 var func = CKEDITOR.env.ie ?
\r
559 // Finds the container and offset for a specific boundary
\r
561 var getBoundaryInformation = function( range, start )
\r
563 // Creates a collapsed range at the requested boundary.
\r
564 range = range.duplicate();
\r
565 range.collapse( start );
\r
567 // Gets the element that encloses the range entirely.
\r
568 var parent = range.parentElement();
\r
569 var siblings = parent.childNodes;
\r
573 for ( var i = 0 ; i < siblings.length ; i++ )
\r
575 var child = siblings[ i ];
\r
576 if ( child.nodeType == 1 )
\r
578 testRange = range.duplicate();
\r
580 testRange.moveToElementText( child );
\r
582 var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),
\r
583 comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );
\r
585 testRange.collapse();
\r
587 if ( comparisonStart > 0 )
\r
589 // When selection stay at the side of certain self-closing elements, e.g. BR,
\r
590 // our comparison will never shows an equality. (#4824)
\r
591 else if ( !comparisonStart
\r
592 || comparisonEnd == 1 && comparisonStart == -1 )
\r
593 return { container : parent, offset : i };
\r
594 else if ( !comparisonEnd )
\r
595 return { container : parent, offset : i + 1 };
\r
603 testRange = range.duplicate();
\r
604 testRange.moveToElementText( parent );
\r
605 testRange.collapse( false );
\r
608 testRange.setEndPoint( 'StartToStart', range );
\r
609 // IE report line break as CRLF with range.text but
\r
610 // only LF with textnode.nodeValue, normalize them to avoid
\r
611 // breaking character counting logic below. (#3949)
\r
612 var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
616 while ( distance > 0 )
\r
617 distance -= siblings[ --i ].nodeValue.length;
\r
619 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)
\r
626 if ( distance === 0 )
\r
629 container : parent,
\r
636 container : siblings[ i ],
\r
644 // IE doesn't have range support (in the W3C way), so we
\r
645 // need to do some magic to transform selections into
\r
646 // CKEDITOR.dom.range instances.
\r
648 var sel = this.getNative(),
\r
649 nativeRange = sel && sel.createRange(),
\r
650 type = this.getType(),
\r
656 if ( type == CKEDITOR.SELECTION_TEXT )
\r
658 range = new CKEDITOR.dom.range( this.document );
\r
660 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
661 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
663 boundaryInfo = getBoundaryInformation( nativeRange );
\r
664 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
666 // Correct an invalid IE range case on empty list item. (#5850)
\r
667 if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING
\r
668 && range.endOffset <= range.startContainer.getIndex() )
\r
675 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
679 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
681 var element = nativeRange.item( i ),
\r
682 parentElement = element.parentNode,
\r
685 range = new CKEDITOR.dom.range( this.document );
\r
687 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
690 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
691 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
692 retval.push( range );
\r
705 // On browsers implementing the W3C range, we simply
\r
706 // tranform the native ranges in CKEDITOR.dom.range
\r
711 doc = this.document,
\r
712 sel = this.getNative();
\r
717 // On WebKit, it may happen that we'll have no selection
\r
718 // available. We normalize it here by replicating the
\r
719 // behavior of other browsers.
\r
720 if ( !sel.rangeCount )
\r
722 range = new CKEDITOR.dom.range( doc );
\r
723 range.moveToElementEditStart( doc.getBody() );
\r
724 ranges.push( range );
\r
727 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
729 var nativeRange = sel.getRangeAt( i );
\r
731 range = new CKEDITOR.dom.range( doc );
\r
733 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
734 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
735 ranges.push( range );
\r
740 return function( onlyEditables )
\r
742 var cache = this._.cache;
\r
743 if ( cache.ranges && !onlyEditables )
\r
744 return cache.ranges;
\r
745 else if ( !cache.ranges )
\r
746 cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );
\r
748 // Split range into multiple by read-only nodes.
\r
749 if ( onlyEditables )
\r
751 var ranges = cache.ranges;
\r
752 for ( var i = 0; i < ranges.length; i++ )
\r
754 var range = ranges[ i ];
\r
756 // Drop range spans inside one ready-only node.
\r
757 var parent = range.getCommonAncestor();
\r
758 if ( parent.isReadOnly() )
\r
759 ranges.splice( i, 1 );
\r
761 if ( range.collapsed )
\r
764 var startContainer = range.startContainer,
\r
765 endContainer = range.endContainer,
\r
766 startOffset = range.startOffset,
\r
767 endOffset = range.endOffset,
\r
768 walkerRange = range.clone();
\r
770 // Range may start inside a non-editable element, restart range
\r
771 // by the end of it.
\r
773 if ( ( readOnly = startContainer.isReadOnly() ) )
\r
774 range.setStartAfter( readOnly );
\r
776 // Enlarge range start/end with text node to avoid walker
\r
777 // being DOM destructive, it doesn't interfere our checking
\r
778 // of elements below as well.
\r
779 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
781 if ( startOffset >= startContainer.getLength() )
\r
782 walkerRange.setStartAfter( startContainer );
\r
784 walkerRange.setStartBefore( startContainer );
\r
787 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
790 walkerRange.setEndBefore( endContainer );
\r
792 walkerRange.setEndAfter( endContainer );
\r
795 // Looking for non-editable element inside the range.
\r
796 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
797 walker.evaluator = function( node )
\r
799 if ( node.type == CKEDITOR.NODE_ELEMENT
\r
800 && node.getAttribute( 'contenteditable' ) == 'false' )
\r
802 var newRange = range.clone();
\r
803 range.setEndBefore( node );
\r
805 // Drop collapsed range around read-only elements,
\r
806 // it make sure the range list empty when selecting
\r
807 // only non-editable elements.
\r
808 if ( range.collapsed )
\r
809 ranges.splice( i--, 1 );
\r
811 // Avoid creating invalid range.
\r
812 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )
\r
814 newRange.setStartAfter( node );
\r
815 if ( !newRange.collapsed )
\r
816 ranges.splice( i + 1, 0, newRange );
\r
829 return cache.ranges;
\r
834 * Gets the DOM element in which the selection starts.
\r
835 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
838 * var element = editor.getSelection().<b>getStartElement()</b>;
\r
839 * alert( element.getName() );
\r
841 getStartElement : function()
\r
843 var cache = this._.cache;
\r
844 if ( cache.startElement !== undefined )
\r
845 return cache.startElement;
\r
848 sel = this.getNative();
\r
850 switch ( this.getType() )
\r
852 case CKEDITOR.SELECTION_ELEMENT :
\r
853 return this.getSelectedElement();
\r
855 case CKEDITOR.SELECTION_TEXT :
\r
857 var range = this.getRanges()[0];
\r
861 if ( !range.collapsed )
\r
865 // Decrease the range content to exclude particial
\r
866 // selected node on the start which doesn't have
\r
867 // visual impact. ( #3231 )
\r
870 var startContainer = range.startContainer,
\r
871 startOffset = range.startOffset;
\r
872 // Limit the fix only to non-block elements.(#3950)
\r
873 if ( startOffset == ( startContainer.getChildCount ?
\r
874 startContainer.getChildCount() : startContainer.getLength() )
\r
875 && !startContainer.isBlockBoundary() )
\r
876 range.setStartAfter( startContainer );
\r
880 node = range.startContainer;
\r
882 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
883 return node.getParent();
\r
885 node = node.getChild( range.startOffset );
\r
887 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
888 node = range.startContainer;
\r
891 var child = node.getFirst();
\r
892 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
895 child = child.getFirst();
\r
901 node = range.startContainer;
\r
902 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
903 node = node.getParent();
\r
910 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
914 * Gets the current selected element.
\r
915 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
916 * selection is available or the selection type is not
\r
917 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
919 * var element = editor.getSelection().<b>getSelectedElement()</b>;
\r
920 * alert( element.getName() );
\r
922 getSelectedElement : function()
\r
924 var cache = this._.cache;
\r
925 if ( cache.selectedElement !== undefined )
\r
926 return cache.selectedElement;
\r
930 var node = CKEDITOR.tools.tryThese(
\r
931 // Is it native IE control type selection?
\r
934 return self.getNative().createRange().item( 0 );
\r
936 // Figure it out by checking if there's a single enclosed
\r
937 // node of the range.
\r
940 var range = self.getRanges()[ 0 ],
\r
944 // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>
\r
945 for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )
\r
946 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )
\r
947 && styleObjectElements[ enclosed.getName() ]
\r
948 && ( selected = enclosed ) ); i-- )
\r
950 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]
\r
951 range.shrink( CKEDITOR.SHRINK_ELEMENT );
\r
957 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
962 // Call all cacheable function.
\r
964 this.getStartElement();
\r
965 this.getSelectedElement();
\r
967 // The native selection is not available when locked.
\r
968 this._.cache.nativeSel = {};
\r
972 // Save this selection inside the DOM document.
\r
973 this.document.setCustomData( 'cke_locked_selection', this );
\r
976 unlock : function( restore )
\r
978 var doc = this.document,
\r
979 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
981 if ( lockedSelection )
\r
983 doc.setCustomData( 'cke_locked_selection', null );
\r
987 var selectedElement = lockedSelection.getSelectedElement(),
\r
988 ranges = !selectedElement && lockedSelection.getRanges();
\r
993 doc.getBody().focus();
\r
995 if ( selectedElement )
\r
996 this.selectElement( selectedElement );
\r
998 this.selectRanges( ranges );
\r
1002 if ( !lockedSelection || !restore )
\r
1004 this.isLocked = 0;
\r
1009 reset : function()
\r
1011 this._.cache = {};
\r
1015 * Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.
\r
1018 selectElement : function( element )
\r
1020 if ( this.isLocked )
\r
1022 var range = new CKEDITOR.dom.range( this.document );
\r
1023 range.setStartBefore( element );
\r
1024 range.setEndAfter( element );
\r
1026 this._.cache.selectedElement = element;
\r
1027 this._.cache.startElement = element;
\r
1028 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );
\r
1029 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
1034 if ( CKEDITOR.env.ie )
\r
1036 this.getNative().empty();
\r
1040 // Try to select the node as a control.
\r
1041 range = this.document.$.body.createControlRange();
\r
1042 range.addElement( element.$ );
\r
1047 // If failed, select it as a text range.
\r
1048 range = this.document.$.body.createTextRange();
\r
1049 range.moveToElementText( element.$ );
\r
1054 this.document.fire( 'selectionchange' );
\r
1061 // Create the range for the element.
\r
1062 range = this.document.$.createRange();
\r
1063 range.selectNode( element.$ );
\r
1065 // Select the range.
\r
1066 var sel = this.getNative();
\r
1067 sel.removeAllRanges();
\r
1068 sel.addRange( range );
\r
1075 * Adding the specified ranges to document selection preceding
\r
1076 * by clearing up the original selection.
\r
1077 * @param {CKEDITOR.dom.range} ranges
\r
1079 selectRanges : function( ranges )
\r
1081 if ( this.isLocked )
\r
1083 this._.cache.selectedElement = null;
\r
1084 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();
\r
1085 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );
\r
1086 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
1091 if ( CKEDITOR.env.ie )
\r
1093 if ( ranges.length > 1 )
\r
1095 // IE doesn't accept multiple ranges selection, so we join all into one.
\r
1096 var last = ranges[ ranges.length -1 ] ;
\r
1097 ranges[ 0 ].setEnd( last.endContainer, last.endOffset );
\r
1098 ranges.length = 1;
\r
1101 if ( ranges[ 0 ] )
\r
1102 ranges[ 0 ].select();
\r
1108 var sel = this.getNative();
\r
1110 if ( ranges.length )
\r
1111 sel.removeAllRanges();
\r
1113 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1115 // Joining sequential ranges introduced by
\r
1116 // readonly elements protection.
\r
1117 if ( i < ranges.length -1 )
\r
1119 var left = ranges[ i ], right = ranges[ i +1 ],
\r
1120 between = left.clone();
\r
1121 between.setStart( left.endContainer, left.endOffset );
\r
1122 between.setEnd( right.startContainer, right.startOffset );
\r
1124 // Don't confused by Firefox adjancent multi-ranges
\r
1125 // introduced by table cells selection.
\r
1126 if ( !between.collapsed )
\r
1128 between.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1129 if ( between.getCommonAncestor().isReadOnly())
\r
1131 right.setStart( left.startContainer, left.startOffset );
\r
1132 ranges.splice( i--, 1 );
\r
1138 var range = ranges[ i ];
\r
1139 var nativeRange = this.document.$.createRange();
\r
1140 var startContainer = range.startContainer;
\r
1142 // In FF2, if we have a collapsed range, inside an empty
\r
1143 // element, we must add something to it otherwise the caret
\r
1144 // will not be visible.
\r
1145 // In Opera instead, the selection will be moved out of the
\r
1146 // element. (#4657)
\r
1147 if ( range.collapsed &&
\r
1148 ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&
\r
1149 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
1150 !startContainer.getChildCount() )
\r
1152 startContainer.appendText( '' );
\r
1155 nativeRange.setStart( startContainer.$, range.startOffset );
\r
1156 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1158 // Select the range.
\r
1159 sel.addRange( nativeRange );
\r
1167 * Create bookmark for every single of this selection range (from #getRanges)
\r
1168 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,
\r
1169 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1170 * received as with the underlay range method.
\r
1172 createBookmarks : function( serializable )
\r
1174 return this.getRanges().createBookmarks( serializable );
\r
1178 * Create bookmark for every single of this selection range (from #getRanges)
\r
1179 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,
\r
1180 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1181 * received as with the underlay range method.
\r
1183 createBookmarks2 : function( normalized )
\r
1185 return this.getRanges().createBookmarks2( normalized );
\r
1189 * Select the virtual ranges denote by the bookmarks by calling #selectRanges.
\r
1190 * @param bookmarks
\r
1192 selectBookmarks : function( bookmarks )
\r
1195 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
1197 var range = new CKEDITOR.dom.range( this.document );
\r
1198 range.moveToBookmark( bookmarks[i] );
\r
1199 ranges.push( range );
\r
1201 this.selectRanges( ranges );
\r
1206 * Retrieve the common ancestor node of the first range and the last range.
\r
1208 getCommonAncestor : function()
\r
1210 var ranges = this.getRanges(),
\r
1211 startNode = ranges[ 0 ].startContainer,
\r
1212 endNode = ranges[ ranges.length - 1 ].endContainer;
\r
1213 return startNode.getCommonAncestor( endNode );
\r
1217 * Moving scroll bar to the current selection's start position.
\r
1219 scrollIntoView : function()
\r
1221 // If we have split the block, adds a temporary span at the
\r
1222 // range position and scroll relatively to it.
\r
1223 var start = this.getStartElement();
\r
1224 start.scrollIntoView();
\r
1231 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1232 fillerTextRegex = /\ufeff|\u00a0/,
\r
1233 nonCells = { table:1,tbody:1,tr:1 };
\r
1235 CKEDITOR.dom.range.prototype.select =
\r
1238 function( forceExpand )
\r
1240 var collapsed = this.collapsed;
\r
1241 var isStartMarkerAlone;
\r
1244 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.
\r
1245 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...
\r
1246 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells
\r
1247 || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )
\r
1249 this.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1252 var bookmark = this.createBookmark();
\r
1254 // Create marker tags for the start and end boundaries.
\r
1255 var startNode = bookmark.startNode;
\r
1259 endNode = bookmark.endNode;
\r
1261 // Create the main range which will be used for the selection.
\r
1262 var ieRange = this.document.$.body.createTextRange();
\r
1264 // Position the range at the start boundary.
\r
1265 ieRange.moveToElementText( startNode.$ );
\r
1266 ieRange.moveStart( 'character', 1 );
\r
1270 // Create a tool range for the end.
\r
1271 var ieRangeEnd = this.document.$.body.createTextRange();
\r
1273 // Position the tool range at the end.
\r
1274 ieRangeEnd.moveToElementText( endNode.$ );
\r
1276 // Move the end boundary of the main range to match the tool range.
\r
1277 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
1278 ieRange.moveEnd( 'character', -1 );
\r
1282 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
1283 // will expand and that the cursor will be blinking on the right place.
\r
1284 // Actually, we are using this flag just to avoid using this hack in all
\r
1285 // situations, but just on those needed.
\r
1286 var next = startNode.getNext( notWhitespaces );
\r
1287 isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?
\r
1288 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );
\r
1290 // Append a temporary <span></span> before the selection.
\r
1291 // This is needed to avoid IE destroying selections inside empty
\r
1292 // inline elements, like <b></b> (#253).
\r
1293 // It is also needed when placing the selection right after an inline
\r
1294 // element to avoid the selection moving inside of it.
\r
1295 dummySpan = this.document.createElement( 'span' );
\r
1296 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1297 dummySpan.insertBefore( startNode );
\r
1299 if ( isStartMarkerAlone )
\r
1301 // To expand empty blocks or line spaces after <br>, we need
\r
1302 // instead to have any char, which will be later deleted using the
\r
1304 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1305 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1309 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1310 this.setStartBefore( startNode );
\r
1311 startNode.remove();
\r
1315 if ( isStartMarkerAlone )
\r
1317 // Move the selection start to include the temporary \ufeff.
\r
1318 ieRange.moveStart( 'character', -1 );
\r
1322 // Remove our temporary stuff.
\r
1323 this.document.$.selection.clear();
\r
1328 this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );
\r
1329 dummySpan.remove();
\r
1333 this.setEndBefore( endNode );
\r
1338 this.document.fire( 'selectionchange' );
\r
1343 var startContainer = this.startContainer;
\r
1345 // If we have a collapsed range, inside an empty element, we must add
\r
1346 // something to it, otherwise the caret will not be visible.
\r
1347 if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )
\r
1348 startContainer.append( new CKEDITOR.dom.text( '' ) );
\r
1350 var nativeRange = this.document.$.createRange();
\r
1351 nativeRange.setStart( startContainer.$, this.startOffset );
\r
1355 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1359 // There is a bug in Firefox implementation (it would be too easy
\r
1360 // otherwise). The new start can't be after the end (W3C says it can).
\r
1361 // So, let's create a new range and collapse it to the desired point.
\r
1362 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )
\r
1364 this.collapse( true );
\r
1365 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1371 var selection = this.document.getSelection().getNative();
\r
1372 selection.removeAllRanges();
\r
1373 selection.addRange( nativeRange );
\r