2 Copyright (c) 2003-2009, 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
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 exec : function( editor )
\r
77 switch ( editor.mode )
\r
80 editor.document.$.execCommand( 'SelectAll', false, null );
\r
89 CKEDITOR.plugins.add( 'selection',
\r
91 init : function( editor )
\r
93 editor.on( 'contentDom', function()
\r
95 var doc = editor.document;
\r
97 if ( CKEDITOR.env.ie )
\r
99 // Other browsers don't loose the selection if the
\r
100 // editor document loose the focus. In IE, we don't
\r
101 // have support for it, so we reproduce it here, other
\r
102 // than firing the selection change event.
\r
107 // "onfocusin" is fired before "onfocus". It makes it
\r
108 // possible to restore the selection before click
\r
109 // events get executed.
\r
110 doc.on( 'focusin', function()
\r
112 // If we have saved a range, restore it at this
\r
116 // Well not break because of this.
\r
119 savedRange.select();
\r
128 editor.window.on( 'focus', function()
\r
130 // Enable selections to be saved.
\r
131 saveEnabled = true;
\r
136 // Check document selection before 'blur' fired, this
\r
137 // will prevent us from breaking text selection somewhere
\r
138 // else on the host page.(#3909)
\r
139 editor.document.on( 'beforedeactivate', function()
\r
141 // Disable selections from being saved.
\r
142 saveEnabled = false;
\r
144 // IE may leave the selection still inside the
\r
145 // document. Let's force it to be removed.
\r
146 // TODO: The following has effect for
\r
147 // collapsed selections.
\r
148 editor.document.$.execCommand( 'Unselect' );
\r
151 // IE fires the "selectionchange" event when clicking
\r
152 // inside a selection. We don't want to capture that.
\r
153 doc.on( 'mousedown', disableSave );
\r
157 saveEnabled = true;
\r
158 setTimeout( function()
\r
160 saveSelection( true );
\r
165 doc.on( 'keydown', disableSave );
\r
169 saveEnabled = true;
\r
174 // IE is the only to provide the "selectionchange"
\r
176 doc.on( 'selectionchange', saveSelection );
\r
178 function disableSave()
\r
180 saveEnabled = false;
\r
183 function saveSelection( testIt )
\r
187 var doc = editor.document,
\r
188 sel = doc && doc.$.selection;
\r
190 // There is a very specific case, when clicking
\r
191 // inside a text selection. In that case, the
\r
192 // selection collapses at the clicking point,
\r
193 // but the selection object remains in an
\r
194 // unknown state, making createRange return a
\r
195 // range at the very start of the document. In
\r
196 // such situation we have to test the range, to
\r
197 // be sure it's valid.
\r
198 if ( testIt && sel && sel.type == 'None' )
\r
200 // The "InsertImage" command can be used to
\r
201 // test whether the selection is good or not.
\r
202 // If not, it's enough to give some time to
\r
203 // IE to put things in order for us.
\r
204 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
206 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
211 savedRange = sel && sel.createRange();
\r
213 checkSelectionChangeTimeout.call( editor );
\r
219 // In other browsers, we make the selection change
\r
220 // check based on other events, like clicks or keys
\r
223 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
224 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
228 editor.addCommand( 'selectAll', selectAllCmd );
\r
229 editor.ui.addButton( 'SelectAll',
\r
231 label : editor.lang.selectAll,
\r
232 command : 'selectAll'
\r
235 editor.selectionChange = checkSelectionChangeTimeout;
\r
240 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
241 * @returns {CKEDITOR.dom.selection} A selection object or null if not on
\r
242 * WYSIWYG mode or no selection is available.
\r
244 * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;
\r
245 * alert( selection.getType() );
\r
247 CKEDITOR.editor.prototype.getSelection = function()
\r
249 return this.document && this.document.getSelection();
\r
252 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
254 delete this._.selectionPreviousPath;
\r
258 * Gets the current selection from the document.
\r
259 * @returns {CKEDITOR.dom.selection} A selection object.
\r
261 * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;
\r
262 * alert( selection.getType() );
\r
264 CKEDITOR.dom.document.prototype.getSelection = function()
\r
266 var sel = new CKEDITOR.dom.selection( this );
\r
267 return ( !sel || sel.isInvalid ) ? null : sel;
\r
274 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
275 * alert( 'Nothing is selected' );
\r
277 CKEDITOR.SELECTION_NONE = 1;
\r
280 * Text or collapsed selection.
\r
283 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
284 * alert( 'Text is selected' );
\r
286 CKEDITOR.SELECTION_TEXT = 2;
\r
289 * Element selection.
\r
292 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
293 * alert( 'An element is selected' );
\r
295 CKEDITOR.SELECTION_ELEMENT = 3;
\r
298 * Manipulates the selection in a DOM document.
\r
302 CKEDITOR.dom.selection = function( document )
\r
304 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
306 if ( lockedSelection )
\r
307 return lockedSelection;
\r
309 this.document = document;
\r
310 this.isLocked = false;
\r
317 * IE BUG: The selection's document may be a different document than the
\r
318 * editor document. Return null if that's the case.
\r
320 if ( CKEDITOR.env.ie )
\r
322 var range = this.getNative().createRange();
\r
324 || ( range.item && range.item(0).ownerDocument != this.document.$ )
\r
325 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
327 this.isInvalid = true;
\r
334 var styleObjectElements =
\r
336 img:1,hr:1,li:1,table:1,tr:1,td:1,embed:1,object:1,ol:1,ul:1,
\r
337 a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1
\r
340 CKEDITOR.dom.selection.prototype =
\r
343 * Gets the native selection object from the browser.
\r
345 * @returns {Object} The native selection object.
\r
347 * var selection = editor.getSelection().<b>getNative()</b>;
\r
353 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
358 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
362 * Gets the type of the current selection. The following values are
\r
365 * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>
\r
366 * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or
\r
367 * collapsed selection.</li>
\r
368 * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element
\r
372 * @returns {Number} One of the following constant values:
\r
373 * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or
\r
374 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
376 * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )
\r
377 * alert( 'Text is selected' );
\r
383 var cache = this._.cache;
\r
387 var type = CKEDITOR.SELECTION_NONE;
\r
391 var sel = this.getNative(),
\r
394 if ( ieType == 'Text' )
\r
395 type = CKEDITOR.SELECTION_TEXT;
\r
397 if ( ieType == 'Control' )
\r
398 type = CKEDITOR.SELECTION_ELEMENT;
\r
400 // It is possible that we can still get a text range
\r
401 // object even when type == 'None' is returned by IE.
\r
402 // So we'd better check the object returned by
\r
403 // createRange() rather than by looking at the type.
\r
404 if ( sel.createRange().parentElement )
\r
405 type = CKEDITOR.SELECTION_TEXT;
\r
409 return ( cache.type = type );
\r
414 var cache = this._.cache;
\r
418 var type = CKEDITOR.SELECTION_TEXT;
\r
420 var sel = this.getNative();
\r
423 type = CKEDITOR.SELECTION_NONE;
\r
424 else if ( sel.rangeCount == 1 )
\r
426 // Check if the actual selection is a control (IMG,
\r
427 // TABLE, HR, etc...).
\r
429 var range = sel.getRangeAt(0),
\r
430 startContainer = range.startContainer;
\r
432 if ( startContainer == range.endContainer
\r
433 && startContainer.nodeType == 1
\r
434 && ( range.endOffset - range.startOffset ) == 1
\r
435 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
437 type = CKEDITOR.SELECTION_ELEMENT;
\r
441 return ( cache.type = type );
\r
448 // Finds the container and offset for a specific boundary
\r
450 var getBoundaryInformation = function( range, start )
\r
452 // Creates a collapsed range at the requested boundary.
\r
453 range = range.duplicate();
\r
454 range.collapse( start );
\r
456 // Gets the element that encloses the range entirely.
\r
457 var parent = range.parentElement();
\r
458 var siblings = parent.childNodes;
\r
462 for ( var i = 0 ; i < siblings.length ; i++ )
\r
464 var child = siblings[ i ];
\r
465 if ( child.nodeType == 1 )
\r
467 testRange = range.duplicate();
\r
469 testRange.moveToElementText( child );
\r
470 testRange.collapse();
\r
472 var comparison = testRange.compareEndPoints( 'StartToStart', range );
\r
474 if ( comparison > 0 )
\r
476 else if ( comparison === 0 )
\r
478 container : parent,
\r
488 testRange = range.duplicate();
\r
489 testRange.moveToElementText( parent );
\r
490 testRange.collapse( false );
\r
493 testRange.setEndPoint( 'StartToStart', range );
\r
494 // IE report line break as CRLF with range.text but
\r
495 // only LF with textnode.nodeValue, normalize them to avoid
\r
496 // breaking character counting logic below. (#3949)
\r
497 var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
499 while ( distance > 0 )
\r
500 distance -= siblings[ --i ].nodeValue.length;
\r
502 if ( distance === 0 )
\r
505 container : parent,
\r
512 container : siblings[ i ],
\r
520 var cache = this._.cache;
\r
521 if ( cache.ranges )
\r
522 return cache.ranges;
\r
524 // IE doesn't have range support (in the W3C way), so we
\r
525 // need to do some magic to transform selections into
\r
526 // CKEDITOR.dom.range instances.
\r
528 var sel = this.getNative(),
\r
529 nativeRange = sel && sel.createRange(),
\r
530 type = this.getType(),
\r
536 if ( type == CKEDITOR.SELECTION_TEXT )
\r
538 range = new CKEDITOR.dom.range( this.document );
\r
540 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
541 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
543 boundaryInfo = getBoundaryInformation( nativeRange );
\r
544 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
546 return ( cache.ranges = [ range ] );
\r
548 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
550 var retval = this._.cache.ranges = [];
\r
552 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
554 var element = nativeRange.item( i ),
\r
555 parentElement = element.parentNode,
\r
558 range = new CKEDITOR.dom.range( this.document );
\r
560 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
563 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
564 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
565 retval.push( range );
\r
571 return ( cache.ranges = [] );
\r
577 var cache = this._.cache;
\r
578 if ( cache.ranges )
\r
579 return cache.ranges;
\r
581 // On browsers implementing the W3C range, we simply
\r
582 // tranform the native ranges in CKEDITOR.dom.range
\r
586 var sel = this.getNative();
\r
591 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
593 var nativeRange = sel.getRangeAt( i );
\r
594 var range = new CKEDITOR.dom.range( this.document );
\r
596 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
597 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
598 ranges.push( range );
\r
601 return ( cache.ranges = ranges );
\r
605 * Gets the DOM element in which the selection starts.
\r
606 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
609 * var element = editor.getSelection().<b>getStartElement()</b>;
\r
610 * alert( element.getName() );
\r
612 getStartElement : function()
\r
614 var cache = this._.cache;
\r
615 if ( cache.startElement !== undefined )
\r
616 return cache.startElement;
\r
619 sel = this.getNative();
\r
621 switch ( this.getType() )
\r
623 case CKEDITOR.SELECTION_ELEMENT :
\r
624 return this.getSelectedElement();
\r
626 case CKEDITOR.SELECTION_TEXT :
\r
628 var range = this.getRanges()[0];
\r
632 if ( !range.collapsed )
\r
636 // Decrease the range content to exclude particial
\r
637 // selected node on the start which doesn't have
\r
638 // visual impact. ( #3231 )
\r
641 var startContainer = range.startContainer,
\r
642 startOffset = range.startOffset;
\r
643 if ( startOffset == ( startContainer.getChildCount ?
\r
644 startContainer.getChildCount() : startContainer.getLength() ) )
\r
645 range.setStartAfter( startContainer );
\r
649 node = range.startContainer;
\r
651 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
652 return node.getParent();
\r
654 node = node.getChild( range.startOffset );
\r
656 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
657 return range.startContainer;
\r
659 var child = node.getFirst();
\r
660 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
663 child = child.getFirst();
\r
670 if ( CKEDITOR.env.ie )
\r
672 range = sel.createRange();
\r
673 range.collapse( true );
\r
675 node = range.parentElement();
\r
679 node = sel.anchorNode;
\r
681 if ( node && node.nodeType != 1 )
\r
682 node = node.parentNode;
\r
686 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
690 * Gets the current selected element.
\r
691 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
692 * selection is available or the selection type is not
\r
693 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
695 * var element = editor.getSelection().<b>getSelectedElement()</b>;
\r
696 * alert( element.getName() );
\r
698 getSelectedElement : function()
\r
700 var cache = this._.cache;
\r
701 if ( cache.selectedElement !== undefined )
\r
702 return cache.selectedElement;
\r
706 if ( this.getType() == CKEDITOR.SELECTION_ELEMENT )
\r
708 var sel = this.getNative();
\r
710 if ( CKEDITOR.env.ie )
\r
714 node = sel.createRange().item(0);
\r
720 var range = sel.getRangeAt( 0 );
\r
721 node = range.startContainer.childNodes[ range.startOffset ];
\r
725 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
730 // Call all cacheable function.
\r
732 this.getStartElement();
\r
733 this.getSelectedElement();
\r
735 // The native selection is not available when locked.
\r
736 this._.cache.nativeSel = {};
\r
738 this.isLocked = true;
\r
740 // Save this selection inside the DOM document.
\r
741 this.document.setCustomData( 'cke_locked_selection', this );
\r
744 unlock : function( restore )
\r
746 var doc = this.document,
\r
747 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
749 if ( lockedSelection )
\r
751 doc.setCustomData( 'cke_locked_selection', null );
\r
755 var selectedElement = lockedSelection.getSelectedElement(),
\r
756 ranges = !selectedElement && lockedSelection.getRanges();
\r
758 this.isLocked = false;
\r
761 doc.getBody().focus();
\r
763 if ( selectedElement )
\r
764 this.selectElement( selectedElement );
\r
766 this.selectRanges( ranges );
\r
770 if ( !lockedSelection || !restore )
\r
772 this.isLocked = false;
\r
782 selectElement : function( element )
\r
784 if ( this.isLocked )
\r
786 var range = new CKEDITOR.dom.range( this.document );
\r
787 range.setStartBefore( element );
\r
788 range.setEndAfter( element );
\r
790 this._.cache.selectedElement = element;
\r
791 this._.cache.startElement = element;
\r
792 this._.cache.ranges = [ range ];
\r
793 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
798 if ( CKEDITOR.env.ie )
\r
800 this.getNative().empty();
\r
804 // Try to select the node as a control.
\r
805 range = this.document.$.body.createControlRange();
\r
806 range.addElement( element.$ );
\r
811 // If failed, select it as a text range.
\r
812 range = this.document.$.body.createTextRange();
\r
813 range.moveToElementText( element.$ );
\r
821 // Create the range for the element.
\r
822 range = this.document.$.createRange();
\r
823 range.selectNode( element.$ );
\r
825 // Select the range.
\r
826 var sel = this.getNative();
\r
827 sel.removeAllRanges();
\r
828 sel.addRange( range );
\r
834 selectRanges : function( ranges )
\r
836 if ( this.isLocked )
\r
838 this._.cache.selectedElement = null;
\r
839 this._.cache.startElement = ranges[ 0 ].getTouchedStartNode();
\r
840 this._.cache.ranges = ranges;
\r
841 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
846 if ( CKEDITOR.env.ie )
\r
848 // IE doesn't accept multiple ranges selection, so we just
\r
849 // select the first one.
\r
851 ranges[ 0 ].select();
\r
857 var sel = this.getNative();
\r
858 sel.removeAllRanges();
\r
860 for ( var i = 0 ; i < ranges.length ; i++ )
\r
862 var range = ranges[ i ];
\r
863 var nativeRange = this.document.$.createRange();
\r
864 var startContainer = range.startContainer;
\r
866 // In FF2, if we have a collapsed range, inside an empty
\r
867 // element, we must add something to it otherwise the caret
\r
868 // will not be visible.
\r
869 if ( range.collapsed &&
\r
870 ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) &&
\r
871 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
872 !startContainer.getChildCount() )
\r
874 startContainer.appendText( '' );
\r
877 nativeRange.setStart( startContainer.$, range.startOffset );
\r
878 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
880 // Select the range.
\r
881 sel.addRange( nativeRange );
\r
888 createBookmarks : function( serializable )
\r
891 ranges = this.getRanges(),
\r
892 length = ranges.length,
\r
894 for ( var i = 0; i < length ; i++ )
\r
896 retval.push( bookmark = ranges[ i ].createBookmark( serializable, true ) );
\r
898 serializable = bookmark.serializable;
\r
900 var bookmarkStart = serializable ? this.document.getById( bookmark.startNode ) : bookmark.startNode,
\r
901 bookmarkEnd = serializable ? this.document.getById( bookmark.endNode ) : bookmark.endNode;
\r
903 // Updating the offset values for rest of ranges which have been mangled(#3256).
\r
904 for ( var j = i + 1 ; j < length ; j++ )
\r
906 var dirtyRange = ranges[ j ],
\r
907 rangeStart = dirtyRange.startContainer,
\r
908 rangeEnd = dirtyRange.endContainer;
\r
910 rangeStart.equals( bookmarkStart.getParent() ) && dirtyRange.startOffset++;
\r
911 rangeStart.equals( bookmarkEnd.getParent() ) && dirtyRange.startOffset++;
\r
912 rangeEnd.equals( bookmarkStart.getParent() ) && dirtyRange.endOffset++;
\r
913 rangeEnd.equals( bookmarkEnd.getParent() ) && dirtyRange.endOffset++;
\r
920 createBookmarks2 : function( normalized )
\r
922 var bookmarks = [],
\r
923 ranges = this.getRanges();
\r
925 for ( var i = 0 ; i < ranges.length ; i++ )
\r
926 bookmarks.push( ranges[i].createBookmark2( normalized ) );
\r
931 selectBookmarks : function( bookmarks )
\r
934 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
936 var range = new CKEDITOR.dom.range( this.document );
\r
937 range.moveToBookmark( bookmarks[i] );
\r
938 ranges.push( range );
\r
940 this.selectRanges( ranges );
\r
946 CKEDITOR.dom.range.prototype.select =
\r
949 function( forceExpand )
\r
951 var collapsed = this.collapsed;
\r
952 var isStartMarkerAlone;
\r
955 var bookmark = this.createBookmark();
\r
957 // Create marker tags for the start and end boundaries.
\r
958 var startNode = bookmark.startNode;
\r
962 endNode = bookmark.endNode;
\r
964 // Create the main range which will be used for the selection.
\r
965 var ieRange = this.document.$.body.createTextRange();
\r
967 // Position the range at the start boundary.
\r
968 ieRange.moveToElementText( startNode.$ );
\r
969 ieRange.moveStart( 'character', 1 );
\r
973 // Create a tool range for the end.
\r
974 var ieRangeEnd = this.document.$.body.createTextRange();
\r
976 // Position the tool range at the end.
\r
977 ieRangeEnd.moveToElementText( endNode.$ );
\r
979 // Move the end boundary of the main range to match the tool range.
\r
980 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
981 ieRange.moveEnd( 'character', -1 );
\r
985 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
986 // will expand and that the cursor will be blinking on the right place.
\r
987 // Actually, we are using this flag just to avoid using this hack in all
\r
988 // situations, but just on those needed.
\r
989 isStartMarkerAlone = forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) );
\r
991 // Append a temporary <span></span> before the selection.
\r
992 // This is needed to avoid IE destroying selections inside empty
\r
993 // inline elements, like <b></b> (#253).
\r
994 // It is also needed when placing the selection right after an inline
\r
995 // element to avoid the selection moving inside of it.
\r
996 dummySpan = this.document.createElement( 'span' );
\r
997 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
998 dummySpan.insertBefore( startNode );
\r
1000 if ( isStartMarkerAlone )
\r
1002 // To expand empty blocks or line spaces after <br>, we need
\r
1003 // instead to have any char, which will be later deleted using the
\r
1005 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1006 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1010 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1011 this.setStartBefore( startNode );
\r
1012 startNode.remove();
\r
1016 if ( isStartMarkerAlone )
\r
1018 // Move the selection start to include the temporary \ufeff.
\r
1019 ieRange.moveStart( 'character', -1 );
\r
1023 // Remove our temporary stuff.
\r
1024 this.document.$.selection.clear();
\r
1029 dummySpan.remove();
\r
1033 this.setEndBefore( endNode );
\r
1041 var startContainer = this.startContainer;
\r
1043 // If we have a collapsed range, inside an empty element, we must add
\r
1044 // something to it, otherwise the caret will not be visible.
\r
1045 if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )
\r
1046 startContainer.append( new CKEDITOR.dom.text( '' ) );
\r
1048 var nativeRange = this.document.$.createRange();
\r
1049 nativeRange.setStart( startContainer.$, this.startOffset );
\r
1053 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1057 // There is a bug in Firefox implementation (it would be too easy
\r
1058 // otherwise). The new start can't be after the end (W3C says it can).
\r
1059 // So, let's create a new range and collapse it to the desired point.
\r
1060 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )
\r
1062 this.collapse( true );
\r
1063 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1069 var selection = this.document.getSelection().getNative();
\r
1070 selection.removeAllRanges();
\r
1071 selection.addRange( nativeRange );
\r