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 if ( restoreEnabled )
\r
138 // Well not break because of this.
\r
141 savedRange.select();
\r
151 body.on( 'focus', function()
\r
153 // Enable selections to be saved.
\r
154 saveEnabled = true;
\r
159 body.on( 'beforedeactivate', function( evt )
\r
161 // Ignore this event if it's caused by focus switch between
\r
162 // internal editable control type elements, e.g. layouted paragraph. (#4682)
\r
163 if ( evt.data.$.toElement )
\r
166 // Disable selections from being saved.
\r
167 saveEnabled = false;
\r
168 restoreEnabled = 1;
\r
171 // IE before version 8 will leave cursor blinking inside the document after
\r
172 // editor blurred unless we clean up the selection. (#4716)
\r
173 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
\r
175 editor.on( 'blur', function( evt )
\r
177 editor.document && editor.document.$.selection.empty();
\r
181 // Listening on document element ensures that
\r
182 // scrollbar is included. (#5280)
\r
183 html.on( 'mousedown', function ()
\r
185 // Lock restore selection now, as we have
\r
186 // a followed 'click' event which introduce
\r
187 // new selection. (#5735)
\r
188 restoreEnabled = 0;
\r
191 html.on( 'mouseup', function ()
\r
193 restoreEnabled = 1;
\r
196 // In IE6/7 the blinking cursor appears, but contents are
\r
197 // not editable. (#5634)
\r
198 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )
\r
200 // The 'click' event is not fired when clicking the
\r
201 // scrollbars, so we can use it to check whether
\r
202 // the empty space following <body> has been clicked.
\r
203 html.on( 'click', function( evt )
\r
205 if ( evt.data.getTarget().getName() == 'html' )
\r
206 editor.getSelection().getRanges()[ 0 ].select();
\r
211 // IE fires the "selectionchange" event when clicking
\r
212 // inside a selection. We don't want to capture that.
\r
213 body.on( 'mousedown', function( evt )
\r
215 // IE scrolls document to top on right mousedown
\r
216 // when editor has no focus, remember this scroll
\r
217 // position and revert it before context menu opens. (#5778)
\r
218 if ( evt.data.$.button == 2 )
\r
220 var sel = editor.document.$.selection;
\r
221 if ( sel.type == 'None' )
\r
222 scroll = editor.window.getScrollPosition();
\r
227 body.on( 'mouseup',
\r
230 // Restore recorded scroll position when needed on right mouseup.
\r
231 if ( evt.data.$.button == 2 && scroll )
\r
233 editor.document.$.documentElement.scrollLeft = scroll.x;
\r
234 editor.document.$.documentElement.scrollTop = scroll.y;
\r
238 saveEnabled = true;
\r
239 setTimeout( function()
\r
241 saveSelection( true );
\r
246 body.on( 'keydown', disableSave );
\r
250 saveEnabled = true;
\r
255 // IE is the only to provide the "selectionchange"
\r
257 doc.on( 'selectionchange', saveSelection );
\r
259 function disableSave()
\r
261 saveEnabled = false;
\r
264 function saveSelection( testIt )
\r
268 var doc = editor.document,
\r
269 sel = editor.getSelection(),
\r
270 nativeSel = sel && sel.getNative();
\r
272 // There is a very specific case, when clicking
\r
273 // inside a text selection. In that case, the
\r
274 // selection collapses at the clicking point,
\r
275 // but the selection object remains in an
\r
276 // unknown state, making createRange return a
\r
277 // range at the very start of the document. In
\r
278 // such situation we have to test the range, to
\r
279 // be sure it's valid.
\r
280 if ( testIt && nativeSel && nativeSel.type == 'None' )
\r
282 // The "InsertImage" command can be used to
\r
283 // test whether the selection is good or not.
\r
284 // If not, it's enough to give some time to
\r
285 // IE to put things in order for us.
\r
286 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
288 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
293 // Avoid saving selection from within text input. (#5747)
\r
295 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
\r
296 && ( parentTag = nativeSel.createRange() )
\r
297 && ( parentTag = parentTag.parentElement() )
\r
298 && ( parentTag = parentTag.nodeName )
\r
299 && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
\r
304 savedRange = nativeSel && sel.getRanges()[ 0 ];
\r
306 checkSelectionChangeTimeout.call( editor );
\r
312 // In other browsers, we make the selection change
\r
313 // check based on other events, like clicks or keys
\r
316 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
317 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
321 editor.addCommand( 'selectAll', selectAllCmd );
\r
322 editor.ui.addButton( 'SelectAll',
\r
324 label : editor.lang.selectAll,
\r
325 command : 'selectAll'
\r
328 editor.selectionChange = checkSelectionChangeTimeout;
\r
333 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
334 * @returns {CKEDITOR.dom.selection} A selection object or null if not on
\r
335 * WYSIWYG mode or no selection is available.
\r
337 * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;
\r
338 * alert( selection.getType() );
\r
340 CKEDITOR.editor.prototype.getSelection = function()
\r
342 return this.document && this.document.getSelection();
\r
345 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
347 delete this._.selectionPreviousPath;
\r
351 * Gets the current selection from the document.
\r
352 * @returns {CKEDITOR.dom.selection} A selection object.
\r
354 * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;
\r
355 * alert( selection.getType() );
\r
357 CKEDITOR.dom.document.prototype.getSelection = function()
\r
359 var sel = new CKEDITOR.dom.selection( this );
\r
360 return ( !sel || sel.isInvalid ) ? null : sel;
\r
367 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
368 * alert( 'Nothing is selected' );
\r
370 CKEDITOR.SELECTION_NONE = 1;
\r
373 * Text or collapsed selection.
\r
376 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
377 * alert( 'Text is selected' );
\r
379 CKEDITOR.SELECTION_TEXT = 2;
\r
382 * Element selection.
\r
385 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
386 * alert( 'An element is selected' );
\r
388 CKEDITOR.SELECTION_ELEMENT = 3;
\r
391 * Manipulates the selection in a DOM document.
\r
395 CKEDITOR.dom.selection = function( document )
\r
397 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
399 if ( lockedSelection )
\r
400 return lockedSelection;
\r
402 this.document = document;
\r
403 this.isLocked = false;
\r
410 * IE BUG: The selection's document may be a different document than the
\r
411 * editor document. Return null if that's the case.
\r
413 if ( CKEDITOR.env.ie )
\r
415 var range = this.getNative().createRange();
\r
417 || ( range.item && range.item(0).ownerDocument != this.document.$ )
\r
418 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
420 this.isInvalid = true;
\r
427 var styleObjectElements =
\r
429 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
\r
430 a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1
\r
433 CKEDITOR.dom.selection.prototype =
\r
436 * Gets the native selection object from the browser.
\r
438 * @returns {Object} The native selection object.
\r
440 * var selection = editor.getSelection().<b>getNative()</b>;
\r
446 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
451 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
455 * Gets the type of the current selection. The following values are
\r
458 * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>
\r
459 * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or
\r
460 * collapsed selection.</li>
\r
461 * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element
\r
465 * @returns {Number} One of the following constant values:
\r
466 * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or
\r
467 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
469 * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )
\r
470 * alert( 'Text is selected' );
\r
476 var cache = this._.cache;
\r
480 var type = CKEDITOR.SELECTION_NONE;
\r
484 var sel = this.getNative(),
\r
487 if ( ieType == 'Text' )
\r
488 type = CKEDITOR.SELECTION_TEXT;
\r
490 if ( ieType == 'Control' )
\r
491 type = CKEDITOR.SELECTION_ELEMENT;
\r
493 // It is possible that we can still get a text range
\r
494 // object even when type == 'None' is returned by IE.
\r
495 // So we'd better check the object returned by
\r
496 // createRange() rather than by looking at the type.
\r
497 if ( sel.createRange().parentElement )
\r
498 type = CKEDITOR.SELECTION_TEXT;
\r
502 return ( cache.type = type );
\r
507 var cache = this._.cache;
\r
511 var type = CKEDITOR.SELECTION_TEXT;
\r
513 var sel = this.getNative();
\r
516 type = CKEDITOR.SELECTION_NONE;
\r
517 else if ( sel.rangeCount == 1 )
\r
519 // Check if the actual selection is a control (IMG,
\r
520 // TABLE, HR, etc...).
\r
522 var range = sel.getRangeAt(0),
\r
523 startContainer = range.startContainer;
\r
525 if ( startContainer == range.endContainer
\r
526 && startContainer.nodeType == 1
\r
527 && ( range.endOffset - range.startOffset ) == 1
\r
528 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
530 type = CKEDITOR.SELECTION_ELEMENT;
\r
534 return ( cache.type = type );
\r
538 * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.
\r
539 * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns
\r
540 * one range for each table cell when one or more table row is selected.
\r
543 * var ranges = selection.getRanges();
\r
544 * alert(ranges.length);
\r
546 getRanges : (function ()
\r
548 var func = CKEDITOR.env.ie ?
\r
551 // Finds the container and offset for a specific boundary
\r
553 var getBoundaryInformation = function( range, start )
\r
555 // Creates a collapsed range at the requested boundary.
\r
556 range = range.duplicate();
\r
557 range.collapse( start );
\r
559 // Gets the element that encloses the range entirely.
\r
560 var parent = range.parentElement();
\r
561 var siblings = parent.childNodes;
\r
565 for ( var i = 0 ; i < siblings.length ; i++ )
\r
567 var child = siblings[ i ];
\r
568 if ( child.nodeType == 1 )
\r
570 testRange = range.duplicate();
\r
572 testRange.moveToElementText( child );
\r
574 var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),
\r
575 comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );
\r
577 testRange.collapse();
\r
579 if ( comparisonStart > 0 )
\r
581 // When selection stay at the side of certain self-closing elements, e.g. BR,
\r
582 // our comparison will never shows an equality. (#4824)
\r
583 else if ( !comparisonStart
\r
584 || comparisonEnd == 1 && comparisonStart == -1 )
\r
585 return { container : parent, offset : i };
\r
586 else if ( !comparisonEnd )
\r
587 return { container : parent, offset : i + 1 };
\r
595 testRange = range.duplicate();
\r
596 testRange.moveToElementText( parent );
\r
597 testRange.collapse( false );
\r
600 testRange.setEndPoint( 'StartToStart', range );
\r
601 // IE report line break as CRLF with range.text but
\r
602 // only LF with textnode.nodeValue, normalize them to avoid
\r
603 // breaking character counting logic below. (#3949)
\r
604 var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
608 while ( distance > 0 )
\r
609 distance -= siblings[ --i ].nodeValue.length;
\r
611 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)
\r
618 if ( distance === 0 )
\r
621 container : parent,
\r
628 container : siblings[ i ],
\r
636 // IE doesn't have range support (in the W3C way), so we
\r
637 // need to do some magic to transform selections into
\r
638 // CKEDITOR.dom.range instances.
\r
640 var sel = this.getNative(),
\r
641 nativeRange = sel && sel.createRange(),
\r
642 type = this.getType(),
\r
648 if ( type == CKEDITOR.SELECTION_TEXT )
\r
650 range = new CKEDITOR.dom.range( this.document );
\r
652 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
653 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
655 boundaryInfo = getBoundaryInformation( nativeRange );
\r
656 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
660 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
664 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
666 var element = nativeRange.item( i ),
\r
667 parentElement = element.parentNode,
\r
670 range = new CKEDITOR.dom.range( this.document );
\r
672 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
675 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
676 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
677 retval.push( range );
\r
690 // On browsers implementing the W3C range, we simply
\r
691 // tranform the native ranges in CKEDITOR.dom.range
\r
696 doc = this.document,
\r
697 sel = this.getNative();
\r
702 // On WebKit, it may happen that we'll have no selection
\r
703 // available. We normalize it here by replicating the
\r
704 // behavior of other browsers.
\r
705 if ( !sel.rangeCount )
\r
707 range = new CKEDITOR.dom.range( doc );
\r
708 range.moveToElementEditStart( doc.getBody() );
\r
709 ranges.push( range );
\r
712 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
714 var nativeRange = sel.getRangeAt( i );
\r
716 range = new CKEDITOR.dom.range( doc );
\r
718 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
719 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
720 ranges.push( range );
\r
725 return function( onlyEditables )
\r
727 var cache = this._.cache;
\r
728 if ( cache.ranges && !onlyEditables )
\r
729 return cache.ranges;
\r
730 else if ( !cache.ranges )
\r
731 cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );
\r
733 // Split range into multiple by read-only nodes.
\r
734 if ( onlyEditables )
\r
736 var ranges = cache.ranges;
\r
737 for ( var i = 0; i < ranges.length; i++ )
\r
739 var range = ranges[ i ];
\r
741 // Drop range spans inside one ready-only node.
\r
742 var parent = range.getCommonAncestor();
\r
743 if ( parent.isReadOnly())
\r
744 ranges.splice( i, 1 );
\r
746 if ( range.collapsed )
\r
749 var startContainer = range.startContainer,
\r
750 endContainer = range.endContainer,
\r
751 startOffset = range.startOffset,
\r
752 endOffset = range.endOffset,
\r
753 walkerRange = range.clone();
\r
755 // Range may start inside a non-editable element, restart range
\r
756 // by the end of it.
\r
758 if ( ( readOnly = startContainer.isReadOnly() ) )
\r
759 range.setStartAfter( readOnly );
\r
761 // Enlarge range start/end with text node to avoid walker
\r
762 // being DOM destructive, it doesn't interfere our checking
\r
763 // of elements below as well.
\r
764 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
766 if ( startOffset >= startContainer.getLength() )
\r
767 walkerRange.setStartAfter( startContainer );
\r
769 walkerRange.setStartBefore( startContainer );
\r
772 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
775 walkerRange.setEndBefore( endContainer );
\r
777 walkerRange.setEndAfter( endContainer );
\r
780 // Looking for non-editable element inside the range.
\r
781 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
782 walker.evaluator = function( node )
\r
784 if ( node.type == CKEDITOR.NODE_ELEMENT
\r
785 && node.getAttribute( 'contenteditable' ) == 'false' )
\r
787 var newRange = range.clone();
\r
788 range.setEndBefore( node );
\r
790 // Drop collapsed range around read-only elements,
\r
791 // it make sure the range list empty when selecting
\r
792 // only non-editable elements.
\r
793 if ( range.collapsed )
\r
794 ranges.splice( i--, 1 );
\r
796 // Avoid creating invalid range.
\r
797 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )
\r
799 newRange.setStartAfter( node );
\r
800 if ( !newRange.collapsed )
\r
801 ranges.splice( i + 1, 0, newRange );
\r
814 return cache.ranges;
\r
819 * Gets the DOM element in which the selection starts.
\r
820 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
823 * var element = editor.getSelection().<b>getStartElement()</b>;
\r
824 * alert( element.getName() );
\r
826 getStartElement : function()
\r
828 var cache = this._.cache;
\r
829 if ( cache.startElement !== undefined )
\r
830 return cache.startElement;
\r
833 sel = this.getNative();
\r
835 switch ( this.getType() )
\r
837 case CKEDITOR.SELECTION_ELEMENT :
\r
838 return this.getSelectedElement();
\r
840 case CKEDITOR.SELECTION_TEXT :
\r
842 var range = this.getRanges()[0];
\r
846 if ( !range.collapsed )
\r
850 // Decrease the range content to exclude particial
\r
851 // selected node on the start which doesn't have
\r
852 // visual impact. ( #3231 )
\r
855 var startContainer = range.startContainer,
\r
856 startOffset = range.startOffset;
\r
857 // Limit the fix only to non-block elements.(#3950)
\r
858 if ( startOffset == ( startContainer.getChildCount ?
\r
859 startContainer.getChildCount() : startContainer.getLength() )
\r
860 && !startContainer.isBlockBoundary() )
\r
861 range.setStartAfter( startContainer );
\r
865 node = range.startContainer;
\r
867 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
868 return node.getParent();
\r
870 node = node.getChild( range.startOffset );
\r
872 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
873 return range.startContainer;
\r
875 var child = node.getFirst();
\r
876 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
879 child = child.getFirst();
\r
886 if ( CKEDITOR.env.ie )
\r
888 range = sel.createRange();
\r
889 range.collapse( true );
\r
891 node = range.parentElement();
\r
895 node = sel.anchorNode;
\r
897 if ( node && node.nodeType != 1 )
\r
898 node = node.parentNode;
\r
902 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
906 * Gets the current selected element.
\r
907 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
908 * selection is available or the selection type is not
\r
909 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
911 * var element = editor.getSelection().<b>getSelectedElement()</b>;
\r
912 * alert( element.getName() );
\r
914 getSelectedElement : function()
\r
916 var cache = this._.cache;
\r
917 if ( cache.selectedElement !== undefined )
\r
918 return cache.selectedElement;
\r
922 var node = CKEDITOR.tools.tryThese(
\r
923 // Is it native IE control type selection?
\r
926 return self.getNative().createRange().item( 0 );
\r
928 // Figure it out by checking if there's a single enclosed
\r
929 // node of the range.
\r
932 var range = self.getRanges()[ 0 ],
\r
936 // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>
\r
937 for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )
\r
938 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )
\r
939 && styleObjectElements[ enclosed.getName() ]
\r
940 && ( selected = enclosed ) ); i-- )
\r
942 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]
\r
943 range.shrink( CKEDITOR.SHRINK_ELEMENT );
\r
949 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
954 // Call all cacheable function.
\r
956 this.getStartElement();
\r
957 this.getSelectedElement();
\r
959 // The native selection is not available when locked.
\r
960 this._.cache.nativeSel = {};
\r
962 this.isLocked = true;
\r
964 // Save this selection inside the DOM document.
\r
965 this.document.setCustomData( 'cke_locked_selection', this );
\r
968 unlock : function( restore )
\r
970 var doc = this.document,
\r
971 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
973 if ( lockedSelection )
\r
975 doc.setCustomData( 'cke_locked_selection', null );
\r
979 var selectedElement = lockedSelection.getSelectedElement(),
\r
980 ranges = !selectedElement && lockedSelection.getRanges();
\r
982 this.isLocked = false;
\r
985 doc.getBody().focus();
\r
987 if ( selectedElement )
\r
988 this.selectElement( selectedElement );
\r
990 this.selectRanges( ranges );
\r
994 if ( !lockedSelection || !restore )
\r
996 this.isLocked = false;
\r
1001 reset : function()
\r
1003 this._.cache = {};
\r
1007 * Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.
\r
1010 selectElement : function( element )
\r
1012 if ( this.isLocked )
\r
1014 var range = new CKEDITOR.dom.range( this.document );
\r
1015 range.setStartBefore( element );
\r
1016 range.setEndAfter( element );
\r
1018 this._.cache.selectedElement = element;
\r
1019 this._.cache.startElement = element;
\r
1020 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );
\r
1021 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
1026 if ( CKEDITOR.env.ie )
\r
1028 this.getNative().empty();
\r
1032 // Try to select the node as a control.
\r
1033 range = this.document.$.body.createControlRange();
\r
1034 range.addElement( element.$ );
\r
1039 // If failed, select it as a text range.
\r
1040 range = this.document.$.body.createTextRange();
\r
1041 range.moveToElementText( element.$ );
\r
1046 this.document.fire( 'selectionchange' );
\r
1053 // Create the range for the element.
\r
1054 range = this.document.$.createRange();
\r
1055 range.selectNode( element.$ );
\r
1057 // Select the range.
\r
1058 var sel = this.getNative();
\r
1059 sel.removeAllRanges();
\r
1060 sel.addRange( range );
\r
1067 * Adding the specified ranges to document selection preceding
\r
1068 * by clearing up the original selection.
\r
1069 * @param {CKEDITOR.dom.range} ranges
\r
1071 selectRanges : function( ranges )
\r
1073 if ( this.isLocked )
\r
1075 this._.cache.selectedElement = null;
\r
1076 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();
\r
1077 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );
\r
1078 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
1083 if ( CKEDITOR.env.ie )
\r
1085 if ( ranges.length > 1 )
\r
1087 // IE doesn't accept multiple ranges selection, so we join all into one.
\r
1088 var last = ranges[ ranges.length -1 ] ;
\r
1089 ranges[ 0 ].setEnd( last.endContainer, last.endOffset );
\r
1090 ranges.length = 1;
\r
1093 if ( ranges[ 0 ] )
\r
1094 ranges[ 0 ].select();
\r
1100 var sel = this.getNative();
\r
1102 if ( ranges.length )
\r
1103 sel.removeAllRanges();
\r
1105 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1107 // Joining sequential ranges introduced by
\r
1108 // readonly elements protection.
\r
1109 if ( i < ranges.length -1 )
\r
1111 var left = ranges[ i ], right = ranges[ i +1 ],
\r
1112 between = left.clone();
\r
1113 between.setStart( left.endContainer, left.endOffset );
\r
1114 between.setEnd( right.startContainer, right.startOffset );
\r
1116 // Don't confused by Firefox adjancent multi-ranges
\r
1117 // introduced by table cells selection.
\r
1118 if ( !between.collapsed )
\r
1120 between.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1121 if ( between.getCommonAncestor().isReadOnly())
\r
1123 right.setStart( left.startContainer, left.startOffset );
\r
1124 ranges.splice( i--, 1 );
\r
1130 var range = ranges[ i ];
\r
1131 var nativeRange = this.document.$.createRange();
\r
1132 var startContainer = range.startContainer;
\r
1134 // In FF2, if we have a collapsed range, inside an empty
\r
1135 // element, we must add something to it otherwise the caret
\r
1136 // will not be visible.
\r
1137 // In Opera instead, the selection will be moved out of the
\r
1138 // element. (#4657)
\r
1139 if ( range.collapsed &&
\r
1140 ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&
\r
1141 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
1142 !startContainer.getChildCount() )
\r
1144 startContainer.appendText( '' );
\r
1147 nativeRange.setStart( startContainer.$, range.startOffset );
\r
1148 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1150 // Select the range.
\r
1151 sel.addRange( nativeRange );
\r
1159 * Create bookmark for every single of this selection range (from #getRanges)
\r
1160 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,
\r
1161 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1162 * received as with the underlay range method.
\r
1164 createBookmarks : function( serializable )
\r
1166 return this.getRanges().createBookmarks( serializable );
\r
1170 * Create bookmark for every single of this selection range (from #getRanges)
\r
1171 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,
\r
1172 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1173 * received as with the underlay range method.
\r
1175 createBookmarks2 : function( normalized )
\r
1177 return this.getRanges().createBookmarks2( normalized );
\r
1181 * Select the virtual ranges denote by the bookmarks by calling #selectRanges.
\r
1182 * @param bookmarks
\r
1184 selectBookmarks : function( bookmarks )
\r
1187 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
1189 var range = new CKEDITOR.dom.range( this.document );
\r
1190 range.moveToBookmark( bookmarks[i] );
\r
1191 ranges.push( range );
\r
1193 this.selectRanges( ranges );
\r
1198 * Retrieve the common ancestor node of the first range and the last range.
\r
1200 getCommonAncestor : function()
\r
1202 var ranges = this.getRanges(),
\r
1203 startNode = ranges[ 0 ].startContainer,
\r
1204 endNode = ranges[ ranges.length - 1 ].endContainer;
\r
1205 return startNode.getCommonAncestor( endNode );
\r
1209 * Moving scroll bar to the current selection's start position.
\r
1211 scrollIntoView : function()
\r
1213 // If we have split the block, adds a temporary span at the
\r
1214 // range position and scroll relatively to it.
\r
1215 var start = this.getStartElement();
\r
1216 start.scrollIntoView();
\r
1223 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1224 fillerTextRegex = /\ufeff|\u00a0/,
\r
1225 nonCells = { table:1,tbody:1,tr:1 };
\r
1227 CKEDITOR.dom.range.prototype.select =
\r
1230 function( forceExpand )
\r
1232 var collapsed = this.collapsed;
\r
1233 var isStartMarkerAlone;
\r
1236 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.
\r
1237 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...
\r
1238 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells
\r
1239 || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )
\r
1241 this.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1244 var bookmark = this.createBookmark();
\r
1246 // Create marker tags for the start and end boundaries.
\r
1247 var startNode = bookmark.startNode;
\r
1251 endNode = bookmark.endNode;
\r
1253 // Create the main range which will be used for the selection.
\r
1254 var ieRange = this.document.$.body.createTextRange();
\r
1256 // Position the range at the start boundary.
\r
1257 ieRange.moveToElementText( startNode.$ );
\r
1258 ieRange.moveStart( 'character', 1 );
\r
1262 // Create a tool range for the end.
\r
1263 var ieRangeEnd = this.document.$.body.createTextRange();
\r
1265 // Position the tool range at the end.
\r
1266 ieRangeEnd.moveToElementText( endNode.$ );
\r
1268 // Move the end boundary of the main range to match the tool range.
\r
1269 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
1270 ieRange.moveEnd( 'character', -1 );
\r
1274 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
1275 // will expand and that the cursor will be blinking on the right place.
\r
1276 // Actually, we are using this flag just to avoid using this hack in all
\r
1277 // situations, but just on those needed.
\r
1278 var next = startNode.getNext( notWhitespaces );
\r
1279 isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?
\r
1280 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );
\r
1282 // Append a temporary <span></span> before the selection.
\r
1283 // This is needed to avoid IE destroying selections inside empty
\r
1284 // inline elements, like <b></b> (#253).
\r
1285 // It is also needed when placing the selection right after an inline
\r
1286 // element to avoid the selection moving inside of it.
\r
1287 dummySpan = this.document.createElement( 'span' );
\r
1288 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1289 dummySpan.insertBefore( startNode );
\r
1291 if ( isStartMarkerAlone )
\r
1293 // To expand empty blocks or line spaces after <br>, we need
\r
1294 // instead to have any char, which will be later deleted using the
\r
1296 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1297 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1301 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1302 this.setStartBefore( startNode );
\r
1303 startNode.remove();
\r
1307 if ( isStartMarkerAlone )
\r
1309 // Move the selection start to include the temporary \ufeff.
\r
1310 ieRange.moveStart( 'character', -1 );
\r
1314 // Remove our temporary stuff.
\r
1315 this.document.$.selection.clear();
\r
1320 this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );
\r
1321 dummySpan.remove();
\r
1325 this.setEndBefore( endNode );
\r
1330 this.document.fire( 'selectionchange' );
\r
1335 var startContainer = this.startContainer;
\r
1337 // If we have a collapsed range, inside an empty element, we must add
\r
1338 // something to it, otherwise the caret will not be visible.
\r
1339 if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )
\r
1340 startContainer.append( new CKEDITOR.dom.text( '' ) );
\r
1342 var nativeRange = this.document.$.createRange();
\r
1343 nativeRange.setStart( startContainer.$, this.startOffset );
\r
1347 nativeRange.setEnd( this.endContainer.$, this.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 this.collapse( true );
\r
1357 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1363 var selection = this.document.getSelection().getNative();
\r
1364 selection.removeAllRanges();
\r
1365 selection.addRange( nativeRange );
\r