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
210 // IE fires the "selectionchange" event when clicking
\r
211 // inside a selection. We don't want to capture that.
\r
212 body.on( 'mousedown', function ()
\r
217 body.on( 'mouseup',
\r
220 saveEnabled = true;
\r
221 setTimeout( function()
\r
223 saveSelection( true );
\r
228 body.on( 'keydown', disableSave );
\r
232 saveEnabled = true;
\r
237 // IE is the only to provide the "selectionchange"
\r
239 doc.on( 'selectionchange', saveSelection );
\r
241 function disableSave()
\r
243 saveEnabled = false;
\r
246 function saveSelection( testIt )
\r
250 var doc = editor.document,
\r
251 sel = editor.getSelection(),
\r
252 nativeSel = sel && sel.getNative();
\r
254 // There is a very specific case, when clicking
\r
255 // inside a text selection. In that case, the
\r
256 // selection collapses at the clicking point,
\r
257 // but the selection object remains in an
\r
258 // unknown state, making createRange return a
\r
259 // range at the very start of the document. In
\r
260 // such situation we have to test the range, to
\r
261 // be sure it's valid.
\r
262 if ( testIt && nativeSel && nativeSel.type == 'None' )
\r
264 // The "InsertImage" command can be used to
\r
265 // test whether the selection is good or not.
\r
266 // If not, it's enough to give some time to
\r
267 // IE to put things in order for us.
\r
268 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
270 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
275 // Avoid saving selection from within text input. (#5747)
\r
277 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
\r
278 && ( parentTag = nativeSel.createRange() )
\r
279 && ( parentTag = parentTag.parentElement() )
\r
280 && ( parentTag = parentTag.nodeName )
\r
281 && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
\r
286 savedRange = nativeSel && sel.getRanges()[ 0 ];
\r
288 checkSelectionChangeTimeout.call( editor );
\r
294 // In other browsers, we make the selection change
\r
295 // check based on other events, like clicks or keys
\r
298 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
299 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
303 editor.addCommand( 'selectAll', selectAllCmd );
\r
304 editor.ui.addButton( 'SelectAll',
\r
306 label : editor.lang.selectAll,
\r
307 command : 'selectAll'
\r
310 editor.selectionChange = checkSelectionChangeTimeout;
\r
315 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
316 * @returns {CKEDITOR.dom.selection} A selection object or null if not on
\r
317 * WYSIWYG mode or no selection is available.
\r
319 * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;
\r
320 * alert( selection.getType() );
\r
322 CKEDITOR.editor.prototype.getSelection = function()
\r
324 return this.document && this.document.getSelection();
\r
327 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
329 delete this._.selectionPreviousPath;
\r
333 * Gets the current selection from the document.
\r
334 * @returns {CKEDITOR.dom.selection} A selection object.
\r
336 * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;
\r
337 * alert( selection.getType() );
\r
339 CKEDITOR.dom.document.prototype.getSelection = function()
\r
341 var sel = new CKEDITOR.dom.selection( this );
\r
342 return ( !sel || sel.isInvalid ) ? null : sel;
\r
349 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
350 * alert( 'Nothing is selected' );
\r
352 CKEDITOR.SELECTION_NONE = 1;
\r
355 * Text or collapsed selection.
\r
358 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
359 * alert( 'Text is selected' );
\r
361 CKEDITOR.SELECTION_TEXT = 2;
\r
364 * Element selection.
\r
367 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
368 * alert( 'An element is selected' );
\r
370 CKEDITOR.SELECTION_ELEMENT = 3;
\r
373 * Manipulates the selection in a DOM document.
\r
377 CKEDITOR.dom.selection = function( document )
\r
379 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
381 if ( lockedSelection )
\r
382 return lockedSelection;
\r
384 this.document = document;
\r
385 this.isLocked = false;
\r
392 * IE BUG: The selection's document may be a different document than the
\r
393 * editor document. Return null if that's the case.
\r
395 if ( CKEDITOR.env.ie )
\r
397 var range = this.getNative().createRange();
\r
399 || ( range.item && range.item(0).ownerDocument != this.document.$ )
\r
400 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
402 this.isInvalid = true;
\r
409 var styleObjectElements =
\r
411 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
\r
412 a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1
\r
415 CKEDITOR.dom.selection.prototype =
\r
418 * Gets the native selection object from the browser.
\r
420 * @returns {Object} The native selection object.
\r
422 * var selection = editor.getSelection().<b>getNative()</b>;
\r
428 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
433 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
437 * Gets the type of the current selection. The following values are
\r
440 * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>
\r
441 * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or
\r
442 * collapsed selection.</li>
\r
443 * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element
\r
447 * @returns {Number} One of the following constant values:
\r
448 * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or
\r
449 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
451 * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )
\r
452 * alert( 'Text is selected' );
\r
458 var cache = this._.cache;
\r
462 var type = CKEDITOR.SELECTION_NONE;
\r
466 var sel = this.getNative(),
\r
469 if ( ieType == 'Text' )
\r
470 type = CKEDITOR.SELECTION_TEXT;
\r
472 if ( ieType == 'Control' )
\r
473 type = CKEDITOR.SELECTION_ELEMENT;
\r
475 // It is possible that we can still get a text range
\r
476 // object even when type == 'None' is returned by IE.
\r
477 // So we'd better check the object returned by
\r
478 // createRange() rather than by looking at the type.
\r
479 if ( sel.createRange().parentElement )
\r
480 type = CKEDITOR.SELECTION_TEXT;
\r
484 return ( cache.type = type );
\r
489 var cache = this._.cache;
\r
493 var type = CKEDITOR.SELECTION_TEXT;
\r
495 var sel = this.getNative();
\r
498 type = CKEDITOR.SELECTION_NONE;
\r
499 else if ( sel.rangeCount == 1 )
\r
501 // Check if the actual selection is a control (IMG,
\r
502 // TABLE, HR, etc...).
\r
504 var range = sel.getRangeAt(0),
\r
505 startContainer = range.startContainer;
\r
507 if ( startContainer == range.endContainer
\r
508 && startContainer.nodeType == 1
\r
509 && ( range.endOffset - range.startOffset ) == 1
\r
510 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
512 type = CKEDITOR.SELECTION_ELEMENT;
\r
516 return ( cache.type = type );
\r
520 * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.
\r
521 * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns
\r
522 * one range for each table cell when one or more table row is selected.
\r
525 * var ranges = selection.getRanges();
\r
526 * alert(ranges.length);
\r
528 getRanges : (function ()
\r
530 var func = CKEDITOR.env.ie ?
\r
533 // Finds the container and offset for a specific boundary
\r
535 var getBoundaryInformation = function( range, start )
\r
537 // Creates a collapsed range at the requested boundary.
\r
538 range = range.duplicate();
\r
539 range.collapse( start );
\r
541 // Gets the element that encloses the range entirely.
\r
542 var parent = range.parentElement();
\r
543 var siblings = parent.childNodes;
\r
547 for ( var i = 0 ; i < siblings.length ; i++ )
\r
549 var child = siblings[ i ];
\r
550 if ( child.nodeType == 1 )
\r
552 testRange = range.duplicate();
\r
554 testRange.moveToElementText( child );
\r
556 var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),
\r
557 comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );
\r
559 testRange.collapse();
\r
561 if ( comparisonStart > 0 )
\r
563 // When selection stay at the side of certain self-closing elements, e.g. BR,
\r
564 // our comparison will never shows an equality. (#4824)
\r
565 else if ( !comparisonStart
\r
566 || comparisonEnd == 1 && comparisonStart == -1 )
\r
567 return { container : parent, offset : i };
\r
568 else if ( !comparisonEnd )
\r
569 return { container : parent, offset : i + 1 };
\r
577 testRange = range.duplicate();
\r
578 testRange.moveToElementText( parent );
\r
579 testRange.collapse( false );
\r
582 testRange.setEndPoint( 'StartToStart', range );
\r
583 // IE report line break as CRLF with range.text but
\r
584 // only LF with textnode.nodeValue, normalize them to avoid
\r
585 // breaking character counting logic below. (#3949)
\r
586 var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
590 while ( distance > 0 )
\r
591 distance -= siblings[ --i ].nodeValue.length;
\r
593 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)
\r
600 if ( distance === 0 )
\r
603 container : parent,
\r
610 container : siblings[ i ],
\r
618 // IE doesn't have range support (in the W3C way), so we
\r
619 // need to do some magic to transform selections into
\r
620 // CKEDITOR.dom.range instances.
\r
622 var sel = this.getNative(),
\r
623 nativeRange = sel && sel.createRange(),
\r
624 type = this.getType(),
\r
630 if ( type == CKEDITOR.SELECTION_TEXT )
\r
632 range = new CKEDITOR.dom.range( this.document );
\r
634 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
635 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
637 boundaryInfo = getBoundaryInformation( nativeRange );
\r
638 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
642 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
646 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
648 var element = nativeRange.item( i ),
\r
649 parentElement = element.parentNode,
\r
652 range = new CKEDITOR.dom.range( this.document );
\r
654 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
657 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
658 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
659 retval.push( range );
\r
672 // On browsers implementing the W3C range, we simply
\r
673 // tranform the native ranges in CKEDITOR.dom.range
\r
677 var sel = this.getNative();
\r
682 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
684 var nativeRange = sel.getRangeAt( i );
\r
685 var range = new CKEDITOR.dom.range( this.document );
\r
687 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
688 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
689 ranges.push( range );
\r
694 return function( onlyEditables )
\r
696 var cache = this._.cache;
\r
697 if ( cache.ranges && !onlyEditables )
\r
698 return cache.ranges;
\r
699 else if ( !cache.ranges )
\r
700 cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );
\r
702 // Split range into multiple by read-only nodes.
\r
703 if ( onlyEditables )
\r
705 var ranges = cache.ranges;
\r
706 for ( var i = 0; i < ranges.length; i++ )
\r
708 var range = ranges[ i ];
\r
710 // Drop range spans inside one ready-only node.
\r
711 var parent = range.getCommonAncestor();
\r
712 if ( parent.isReadOnly())
\r
713 ranges.splice( i, 1 );
\r
715 if ( range.collapsed )
\r
718 var startContainer = range.startContainer,
\r
719 endContainer = range.endContainer,
\r
720 startOffset = range.startOffset,
\r
721 endOffset = range.endOffset,
\r
722 walkerRange = range.clone();
\r
724 // Range may start inside a non-editable element, restart range
\r
725 // by the end of it.
\r
727 if ( ( readOnly = startContainer.isReadOnly() ) )
\r
728 range.setStartAfter( readOnly );
\r
730 // Enlarge range start/end with text node to avoid walker
\r
731 // being DOM destructive, it doesn't interfere our checking
\r
732 // of elements below as well.
\r
733 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
735 if ( startOffset >= startContainer.getLength() )
\r
736 walkerRange.setStartAfter( startContainer );
\r
738 walkerRange.setStartBefore( startContainer );
\r
741 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
744 walkerRange.setEndBefore( endContainer );
\r
746 walkerRange.setEndAfter( endContainer );
\r
749 // Looking for non-editable element inside the range.
\r
750 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
751 walker.evaluator = function( node )
\r
753 if ( node.type == CKEDITOR.NODE_ELEMENT
\r
754 && node.getAttribute( 'contenteditable' ) == 'false' )
\r
756 var newRange = range.clone();
\r
757 range.setEndBefore( node );
\r
759 // Drop collapsed range around read-only elements,
\r
760 // it make sure the range list empty when selecting
\r
761 // only non-editable elements.
\r
762 if ( range.collapsed )
\r
763 ranges.splice( i--, 1 );
\r
765 // Avoid creating invalid range.
\r
766 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )
\r
768 newRange.setStartAfter( node );
\r
769 if ( !newRange.collapsed )
\r
770 ranges.splice( i + 1, 0, newRange );
\r
783 return cache.ranges;
\r
788 * Gets the DOM element in which the selection starts.
\r
789 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
792 * var element = editor.getSelection().<b>getStartElement()</b>;
\r
793 * alert( element.getName() );
\r
795 getStartElement : function()
\r
797 var cache = this._.cache;
\r
798 if ( cache.startElement !== undefined )
\r
799 return cache.startElement;
\r
802 sel = this.getNative();
\r
804 switch ( this.getType() )
\r
806 case CKEDITOR.SELECTION_ELEMENT :
\r
807 return this.getSelectedElement();
\r
809 case CKEDITOR.SELECTION_TEXT :
\r
811 var range = this.getRanges()[0];
\r
815 if ( !range.collapsed )
\r
819 // Decrease the range content to exclude particial
\r
820 // selected node on the start which doesn't have
\r
821 // visual impact. ( #3231 )
\r
824 var startContainer = range.startContainer,
\r
825 startOffset = range.startOffset;
\r
826 // Limit the fix only to non-block elements.(#3950)
\r
827 if ( startOffset == ( startContainer.getChildCount ?
\r
828 startContainer.getChildCount() : startContainer.getLength() )
\r
829 && !startContainer.isBlockBoundary() )
\r
830 range.setStartAfter( startContainer );
\r
834 node = range.startContainer;
\r
836 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
837 return node.getParent();
\r
839 node = node.getChild( range.startOffset );
\r
841 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
842 return range.startContainer;
\r
844 var child = node.getFirst();
\r
845 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
848 child = child.getFirst();
\r
855 if ( CKEDITOR.env.ie )
\r
857 range = sel.createRange();
\r
858 range.collapse( true );
\r
860 node = range.parentElement();
\r
864 node = sel.anchorNode;
\r
866 if ( node && node.nodeType != 1 )
\r
867 node = node.parentNode;
\r
871 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
875 * Gets the current selected element.
\r
876 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
877 * selection is available or the selection type is not
\r
878 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
880 * var element = editor.getSelection().<b>getSelectedElement()</b>;
\r
881 * alert( element.getName() );
\r
883 getSelectedElement : function()
\r
885 var cache = this._.cache;
\r
886 if ( cache.selectedElement !== undefined )
\r
887 return cache.selectedElement;
\r
891 var node = CKEDITOR.tools.tryThese(
\r
892 // Is it native IE control type selection?
\r
895 return self.getNative().createRange().item( 0 );
\r
897 // Figure it out by checking if there's a single enclosed
\r
898 // node of the range.
\r
901 var range = self.getRanges()[ 0 ],
\r
905 // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>
\r
906 for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )
\r
907 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )
\r
908 && styleObjectElements[ enclosed.getName() ]
\r
909 && ( selected = enclosed ) ); i-- )
\r
911 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]
\r
912 range.shrink( CKEDITOR.SHRINK_ELEMENT );
\r
918 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
923 // Call all cacheable function.
\r
925 this.getStartElement();
\r
926 this.getSelectedElement();
\r
928 // The native selection is not available when locked.
\r
929 this._.cache.nativeSel = {};
\r
931 this.isLocked = true;
\r
933 // Save this selection inside the DOM document.
\r
934 this.document.setCustomData( 'cke_locked_selection', this );
\r
937 unlock : function( restore )
\r
939 var doc = this.document,
\r
940 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
942 if ( lockedSelection )
\r
944 doc.setCustomData( 'cke_locked_selection', null );
\r
948 var selectedElement = lockedSelection.getSelectedElement(),
\r
949 ranges = !selectedElement && lockedSelection.getRanges();
\r
951 this.isLocked = false;
\r
954 doc.getBody().focus();
\r
956 if ( selectedElement )
\r
957 this.selectElement( selectedElement );
\r
959 this.selectRanges( ranges );
\r
963 if ( !lockedSelection || !restore )
\r
965 this.isLocked = false;
\r
976 * Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.
\r
979 selectElement : function( element )
\r
981 if ( this.isLocked )
\r
983 var range = new CKEDITOR.dom.range( this.document );
\r
984 range.setStartBefore( element );
\r
985 range.setEndAfter( element );
\r
987 this._.cache.selectedElement = element;
\r
988 this._.cache.startElement = element;
\r
989 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );
\r
990 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
995 if ( CKEDITOR.env.ie )
\r
997 this.getNative().empty();
\r
1001 // Try to select the node as a control.
\r
1002 range = this.document.$.body.createControlRange();
\r
1003 range.addElement( element.$ );
\r
1008 // If failed, select it as a text range.
\r
1009 range = this.document.$.body.createTextRange();
\r
1010 range.moveToElementText( element.$ );
\r
1015 this.document.fire( 'selectionchange' );
\r
1022 // Create the range for the element.
\r
1023 range = this.document.$.createRange();
\r
1024 range.selectNode( element.$ );
\r
1026 // Select the range.
\r
1027 var sel = this.getNative();
\r
1028 sel.removeAllRanges();
\r
1029 sel.addRange( range );
\r
1036 * Adding the specified ranges to document selection preceding
\r
1037 * by clearing up the original selection.
\r
1038 * @param {CKEDITOR.dom.range} ranges
\r
1040 selectRanges : function( ranges )
\r
1042 if ( this.isLocked )
\r
1044 this._.cache.selectedElement = null;
\r
1045 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();
\r
1046 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );
\r
1047 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
1052 if ( CKEDITOR.env.ie )
\r
1054 if ( ranges.length > 1 )
\r
1056 // IE doesn't accept multiple ranges selection, so we join all into one.
\r
1057 var last = ranges[ ranges.length -1 ] ;
\r
1058 ranges[ 0 ].setEnd( last.endContainer, last.endOffset );
\r
1059 ranges.length = 1;
\r
1062 if ( ranges[ 0 ] )
\r
1063 ranges[ 0 ].select();
\r
1069 var sel = this.getNative();
\r
1071 if ( ranges.length )
\r
1072 sel.removeAllRanges();
\r
1074 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1076 // Joining sequential ranges introduced by
\r
1077 // readonly elements protection.
\r
1078 if ( i < ranges.length -1 )
\r
1080 var left = ranges[ i ], right = ranges[ i +1 ],
\r
1081 between = left.clone();
\r
1082 between.setStart( left.endContainer, left.endOffset );
\r
1083 between.setEnd( right.startContainer, right.startOffset );
\r
1085 // Don't confused by Firefox adjancent multi-ranges
\r
1086 // introduced by table cells selection.
\r
1087 if ( !between.collapsed )
\r
1089 between.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1090 if ( between.getCommonAncestor().isReadOnly())
\r
1092 right.setStart( left.startContainer, left.startOffset );
\r
1093 ranges.splice( i--, 1 );
\r
1099 var range = ranges[ i ];
\r
1100 var nativeRange = this.document.$.createRange();
\r
1101 var startContainer = range.startContainer;
\r
1103 // In FF2, if we have a collapsed range, inside an empty
\r
1104 // element, we must add something to it otherwise the caret
\r
1105 // will not be visible.
\r
1106 if ( range.collapsed &&
\r
1107 ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) &&
\r
1108 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
1109 !startContainer.getChildCount() )
\r
1111 startContainer.appendText( '' );
\r
1114 nativeRange.setStart( startContainer.$, range.startOffset );
\r
1115 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1117 // Select the range.
\r
1118 sel.addRange( nativeRange );
\r
1126 * Create bookmark for every single of this selection range (from #getRanges)
\r
1127 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,
\r
1128 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1129 * received as with the underlay range method.
\r
1131 createBookmarks : function( serializable )
\r
1133 return this.getRanges().createBookmarks( serializable );
\r
1137 * Create bookmark for every single of this selection range (from #getRanges)
\r
1138 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,
\r
1139 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1140 * received as with the underlay range method.
\r
1142 createBookmarks2 : function( normalized )
\r
1144 return this.getRanges().createBookmarks2( normalized );
\r
1148 * Select the virtual ranges denote by the bookmarks by calling #selectRanges.
\r
1149 * @param bookmarks
\r
1151 selectBookmarks : function( bookmarks )
\r
1154 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
1156 var range = new CKEDITOR.dom.range( this.document );
\r
1157 range.moveToBookmark( bookmarks[i] );
\r
1158 ranges.push( range );
\r
1160 this.selectRanges( ranges );
\r
1165 * Retrieve the common ancestor node of the first range and the last range.
\r
1167 getCommonAncestor : function()
\r
1169 var ranges = this.getRanges(),
\r
1170 startNode = ranges[ 0 ].startContainer,
\r
1171 endNode = ranges[ ranges.length - 1 ].endContainer;
\r
1172 return startNode.getCommonAncestor( endNode );
\r
1176 * Moving scroll bar to the current selection's start position.
\r
1178 scrollIntoView : function()
\r
1180 // If we have split the block, adds a temporary span at the
\r
1181 // range position and scroll relatively to it.
\r
1182 var start = this.getStartElement();
\r
1183 start.scrollIntoView();
\r
1190 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1191 fillerTextRegex = /\ufeff|\u00a0/,
\r
1192 nonCells = { table:1,tbody:1,tr:1 };
\r
1194 CKEDITOR.dom.range.prototype.select =
\r
1197 function( forceExpand )
\r
1199 var collapsed = this.collapsed;
\r
1200 var isStartMarkerAlone;
\r
1203 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.
\r
1204 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...
\r
1205 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells
\r
1206 || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )
\r
1208 this.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1211 var bookmark = this.createBookmark();
\r
1213 // Create marker tags for the start and end boundaries.
\r
1214 var startNode = bookmark.startNode;
\r
1218 endNode = bookmark.endNode;
\r
1220 // Create the main range which will be used for the selection.
\r
1221 var ieRange = this.document.$.body.createTextRange();
\r
1223 // Position the range at the start boundary.
\r
1224 ieRange.moveToElementText( startNode.$ );
\r
1225 ieRange.moveStart( 'character', 1 );
\r
1229 // Create a tool range for the end.
\r
1230 var ieRangeEnd = this.document.$.body.createTextRange();
\r
1232 // Position the tool range at the end.
\r
1233 ieRangeEnd.moveToElementText( endNode.$ );
\r
1235 // Move the end boundary of the main range to match the tool range.
\r
1236 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
1237 ieRange.moveEnd( 'character', -1 );
\r
1241 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
1242 // will expand and that the cursor will be blinking on the right place.
\r
1243 // Actually, we are using this flag just to avoid using this hack in all
\r
1244 // situations, but just on those needed.
\r
1245 var next = startNode.getNext( notWhitespaces );
\r
1246 isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?
\r
1247 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );
\r
1249 // Append a temporary <span></span> before the selection.
\r
1250 // This is needed to avoid IE destroying selections inside empty
\r
1251 // inline elements, like <b></b> (#253).
\r
1252 // It is also needed when placing the selection right after an inline
\r
1253 // element to avoid the selection moving inside of it.
\r
1254 dummySpan = this.document.createElement( 'span' );
\r
1255 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1256 dummySpan.insertBefore( startNode );
\r
1258 if ( isStartMarkerAlone )
\r
1260 // To expand empty blocks or line spaces after <br>, we need
\r
1261 // instead to have any char, which will be later deleted using the
\r
1263 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1264 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1268 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1269 this.setStartBefore( startNode );
\r
1270 startNode.remove();
\r
1274 if ( isStartMarkerAlone )
\r
1276 // Move the selection start to include the temporary \ufeff.
\r
1277 ieRange.moveStart( 'character', -1 );
\r
1281 // Remove our temporary stuff.
\r
1282 this.document.$.selection.clear();
\r
1287 this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );
\r
1288 dummySpan.remove();
\r
1292 this.setEndBefore( endNode );
\r
1297 this.document.fire( 'selectionchange' );
\r
1302 var startContainer = this.startContainer;
\r
1304 // If we have a collapsed range, inside an empty element, we must add
\r
1305 // something to it, otherwise the caret will not be visible.
\r
1306 if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )
\r
1307 startContainer.append( new CKEDITOR.dom.text( '' ) );
\r
1309 var nativeRange = this.document.$.createRange();
\r
1310 nativeRange.setStart( startContainer.$, this.startOffset );
\r
1314 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1318 // There is a bug in Firefox implementation (it would be too easy
\r
1319 // otherwise). The new start can't be after the end (W3C says it can).
\r
1320 // So, let's create a new range and collapse it to the desired point.
\r
1321 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )
\r
1323 this.collapse( true );
\r
1324 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1330 var selection = this.document.getSelection().getNative();
\r
1331 selection.removeAllRanges();
\r
1332 selection.addRange( nativeRange );
\r