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
96 body = doc.getBody();
\r
98 if ( CKEDITOR.env.ie )
\r
100 // Other browsers don't loose the selection if the
\r
101 // editor document loose the focus. In IE, we don't
\r
102 // have support for it, so we reproduce it here, other
\r
103 // than firing the selection change event.
\r
108 // "onfocusin" is fired before "onfocus". It makes it
\r
109 // possible to restore the selection before click
\r
110 // events get executed.
\r
111 body.on( 'focusin', function()
\r
113 // If we have saved a range, restore it at this
\r
117 // Well not break because of this.
\r
120 savedRange.select();
\r
129 editor.window.on( 'focus', function()
\r
131 // Enable selections to be saved.
\r
132 saveEnabled = true;
\r
137 body.on( 'beforedeactivate', function()
\r
139 // Disable selections from being saved.
\r
140 saveEnabled = false;
\r
143 // IE fires the "selectionchange" event when clicking
\r
144 // inside a selection. We don't want to capture that.
\r
145 body.on( 'mousedown', disableSave );
\r
146 body.on( 'mouseup',
\r
149 // IE context-menu event in table cells collapse
\r
150 // whatever selection is, avoiding saving this
\r
151 // 'wrong' snapshot.(#3001)
\r
153 if ( evt.$.button == 2 && evt.getTarget().hasAscendant( 'table' ) )
\r
156 saveEnabled = true;
\r
157 setTimeout( function()
\r
159 saveSelection( true );
\r
164 body.on( 'keydown', disableSave );
\r
168 saveEnabled = true;
\r
173 // IE is the only to provide the "selectionchange"
\r
175 doc.on( 'selectionchange', saveSelection );
\r
177 function disableSave()
\r
179 saveEnabled = false;
\r
182 function saveSelection( testIt )
\r
186 var doc = editor.document,
\r
187 sel = doc && doc.$.selection;
\r
189 // There is a very specific case, when clicking
\r
190 // inside a text selection. In that case, the
\r
191 // selection collapses at the clicking point,
\r
192 // but the selection object remains in an
\r
193 // unknown state, making createRange return a
\r
194 // range at the very start of the document. In
\r
195 // such situation we have to test the range, to
\r
196 // be sure it's valid.
\r
197 if ( testIt && sel && sel.type == 'None' )
\r
199 // The "InsertImage" command can be used to
\r
200 // test whether the selection is good or not.
\r
201 // If not, it's enough to give some time to
\r
202 // IE to put things in order for us.
\r
203 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
205 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
210 savedRange = sel && sel.createRange();
\r
212 checkSelectionChangeTimeout.call( editor );
\r
218 // In other browsers, we make the selection change
\r
219 // check based on other events, like clicks or keys
\r
222 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
223 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
227 editor.addCommand( 'selectAll', selectAllCmd );
\r
228 editor.ui.addButton( 'SelectAll',
\r
230 label : editor.lang.selectAll,
\r
231 command : 'selectAll'
\r
234 editor.selectionChange = checkSelectionChangeTimeout;
\r
239 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
240 * @returns {CKEDITOR.dom.selection} A selection object or null if not on
\r
241 * WYSIWYG mode or no selection is available.
\r
243 * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;
\r
244 * alert( selection.getType() );
\r
246 CKEDITOR.editor.prototype.getSelection = function()
\r
248 return this.document && this.document.getSelection();
\r
251 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
253 delete this._.selectionPreviousPath;
\r
257 * Gets the current selection from the document.
\r
258 * @returns {CKEDITOR.dom.selection} A selection object.
\r
260 * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;
\r
261 * alert( selection.getType() );
\r
263 CKEDITOR.dom.document.prototype.getSelection = function()
\r
265 var sel = new CKEDITOR.dom.selection( this );
\r
266 return ( !sel || sel.isInvalid ) ? null : sel;
\r
273 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
274 * alert( 'Nothing is selected' );
\r
276 CKEDITOR.SELECTION_NONE = 1;
\r
279 * Text or collapsed selection.
\r
282 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
283 * alert( 'Text is selected' );
\r
285 CKEDITOR.SELECTION_TEXT = 2;
\r
288 * Element selection.
\r
291 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
292 * alert( 'An element is selected' );
\r
294 CKEDITOR.SELECTION_ELEMENT = 3;
\r
297 * Manipulates the selection in a DOM document.
\r
301 CKEDITOR.dom.selection = function( document )
\r
303 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
305 if ( lockedSelection )
\r
306 return lockedSelection;
\r
308 this.document = document;
\r
309 this.isLocked = false;
\r
316 * IE BUG: The selection's document may be a different document than the
\r
317 * editor document. Return null if that's the case.
\r
319 if ( CKEDITOR.env.ie )
\r
321 var range = this.getNative().createRange();
\r
323 || ( range.item && range.item(0).ownerDocument != this.document.$ )
\r
324 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
326 this.isInvalid = true;
\r
333 var styleObjectElements =
\r
335 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
\r
336 a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1
\r
339 CKEDITOR.dom.selection.prototype =
\r
342 * Gets the native selection object from the browser.
\r
344 * @returns {Object} The native selection object.
\r
346 * var selection = editor.getSelection().<b>getNative()</b>;
\r
352 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
357 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
361 * Gets the type of the current selection. The following values are
\r
364 * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>
\r
365 * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or
\r
366 * collapsed selection.</li>
\r
367 * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element
\r
371 * @returns {Number} One of the following constant values:
\r
372 * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or
\r
373 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
375 * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )
\r
376 * alert( 'Text is selected' );
\r
382 var cache = this._.cache;
\r
386 var type = CKEDITOR.SELECTION_NONE;
\r
390 var sel = this.getNative(),
\r
393 if ( ieType == 'Text' )
\r
394 type = CKEDITOR.SELECTION_TEXT;
\r
396 if ( ieType == 'Control' )
\r
397 type = CKEDITOR.SELECTION_ELEMENT;
\r
399 // It is possible that we can still get a text range
\r
400 // object even when type == 'None' is returned by IE.
\r
401 // So we'd better check the object returned by
\r
402 // createRange() rather than by looking at the type.
\r
403 if ( sel.createRange().parentElement )
\r
404 type = CKEDITOR.SELECTION_TEXT;
\r
408 return ( cache.type = type );
\r
413 var cache = this._.cache;
\r
417 var type = CKEDITOR.SELECTION_TEXT;
\r
419 var sel = this.getNative();
\r
422 type = CKEDITOR.SELECTION_NONE;
\r
423 else if ( sel.rangeCount == 1 )
\r
425 // Check if the actual selection is a control (IMG,
\r
426 // TABLE, HR, etc...).
\r
428 var range = sel.getRangeAt(0),
\r
429 startContainer = range.startContainer;
\r
431 if ( startContainer == range.endContainer
\r
432 && startContainer.nodeType == 1
\r
433 && ( range.endOffset - range.startOffset ) == 1
\r
434 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
436 type = CKEDITOR.SELECTION_ELEMENT;
\r
440 return ( cache.type = type );
\r
447 // Finds the container and offset for a specific boundary
\r
449 var getBoundaryInformation = function( range, start )
\r
451 // Creates a collapsed range at the requested boundary.
\r
452 range = range.duplicate();
\r
453 range.collapse( start );
\r
455 // Gets the element that encloses the range entirely.
\r
456 var parent = range.parentElement();
\r
457 var siblings = parent.childNodes;
\r
461 for ( var i = 0 ; i < siblings.length ; i++ )
\r
463 var child = siblings[ i ];
\r
464 if ( child.nodeType == 1 )
\r
466 testRange = range.duplicate();
\r
468 testRange.moveToElementText( child );
\r
469 testRange.collapse();
\r
471 var comparison = testRange.compareEndPoints( 'StartToStart', range );
\r
473 if ( comparison > 0 )
\r
475 else if ( comparison === 0 )
\r
477 container : parent,
\r
487 testRange = range.duplicate();
\r
488 testRange.moveToElementText( parent );
\r
489 testRange.collapse( false );
\r
492 testRange.setEndPoint( 'StartToStart', range );
\r
493 // IE report line break as CRLF with range.text but
\r
494 // only LF with textnode.nodeValue, normalize them to avoid
\r
495 // breaking character counting logic below. (#3949)
\r
496 var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
498 while ( distance > 0 )
\r
499 distance -= siblings[ --i ].nodeValue.length;
\r
501 if ( distance === 0 )
\r
504 container : parent,
\r
511 container : siblings[ i ],
\r
519 var cache = this._.cache;
\r
520 if ( cache.ranges )
\r
521 return cache.ranges;
\r
523 // IE doesn't have range support (in the W3C way), so we
\r
524 // need to do some magic to transform selections into
\r
525 // CKEDITOR.dom.range instances.
\r
527 var sel = this.getNative(),
\r
528 nativeRange = sel && sel.createRange(),
\r
529 type = this.getType(),
\r
535 if ( type == CKEDITOR.SELECTION_TEXT )
\r
537 range = new CKEDITOR.dom.range( this.document );
\r
539 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
540 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
542 boundaryInfo = getBoundaryInformation( nativeRange );
\r
543 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
545 return ( cache.ranges = [ range ] );
\r
547 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
549 var retval = this._.cache.ranges = [];
\r
551 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
553 var element = nativeRange.item( i ),
\r
554 parentElement = element.parentNode,
\r
557 range = new CKEDITOR.dom.range( this.document );
\r
559 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
562 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
563 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
564 retval.push( range );
\r
570 return ( cache.ranges = [] );
\r
576 var cache = this._.cache;
\r
577 if ( cache.ranges )
\r
578 return cache.ranges;
\r
580 // On browsers implementing the W3C range, we simply
\r
581 // tranform the native ranges in CKEDITOR.dom.range
\r
585 var sel = this.getNative();
\r
590 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
592 var nativeRange = sel.getRangeAt( i );
\r
593 var range = new CKEDITOR.dom.range( this.document );
\r
595 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
596 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
597 ranges.push( range );
\r
600 return ( cache.ranges = ranges );
\r
604 * Gets the DOM element in which the selection starts.
\r
605 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
608 * var element = editor.getSelection().<b>getStartElement()</b>;
\r
609 * alert( element.getName() );
\r
611 getStartElement : function()
\r
613 var cache = this._.cache;
\r
614 if ( cache.startElement !== undefined )
\r
615 return cache.startElement;
\r
618 sel = this.getNative();
\r
620 switch ( this.getType() )
\r
622 case CKEDITOR.SELECTION_ELEMENT :
\r
623 return this.getSelectedElement();
\r
625 case CKEDITOR.SELECTION_TEXT :
\r
627 var range = this.getRanges()[0];
\r
631 if ( !range.collapsed )
\r
635 // Decrease the range content to exclude particial
\r
636 // selected node on the start which doesn't have
\r
637 // visual impact. ( #3231 )
\r
640 var startContainer = range.startContainer,
\r
641 startOffset = range.startOffset;
\r
642 // Limit the fix only to non-block elements.(#3950)
\r
643 if ( startOffset == ( startContainer.getChildCount ?
\r
644 startContainer.getChildCount() : startContainer.getLength() )
\r
645 && !startContainer.isBlockBoundary() )
\r
646 range.setStartAfter( startContainer );
\r
650 node = range.startContainer;
\r
652 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
653 return node.getParent();
\r
655 node = node.getChild( range.startOffset );
\r
657 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
658 return range.startContainer;
\r
660 var child = node.getFirst();
\r
661 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
664 child = child.getFirst();
\r
671 if ( CKEDITOR.env.ie )
\r
673 range = sel.createRange();
\r
674 range.collapse( true );
\r
676 node = range.parentElement();
\r
680 node = sel.anchorNode;
\r
682 if ( node && node.nodeType != 1 )
\r
683 node = node.parentNode;
\r
687 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
691 * Gets the current selected element.
\r
692 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
693 * selection is available or the selection type is not
\r
694 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
696 * var element = editor.getSelection().<b>getSelectedElement()</b>;
\r
697 * alert( element.getName() );
\r
699 getSelectedElement : function()
\r
701 var cache = this._.cache;
\r
702 if ( cache.selectedElement !== undefined )
\r
703 return cache.selectedElement;
\r
707 if ( this.getType() == CKEDITOR.SELECTION_ELEMENT )
\r
709 var sel = this.getNative();
\r
711 if ( CKEDITOR.env.ie )
\r
715 node = sel.createRange().item(0);
\r
721 var range = sel.getRangeAt( 0 );
\r
722 node = range.startContainer.childNodes[ range.startOffset ];
\r
726 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
731 // Call all cacheable function.
\r
733 this.getStartElement();
\r
734 this.getSelectedElement();
\r
736 // The native selection is not available when locked.
\r
737 this._.cache.nativeSel = {};
\r
739 this.isLocked = true;
\r
741 // Save this selection inside the DOM document.
\r
742 this.document.setCustomData( 'cke_locked_selection', this );
\r
745 unlock : function( restore )
\r
747 var doc = this.document,
\r
748 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
750 if ( lockedSelection )
\r
752 doc.setCustomData( 'cke_locked_selection', null );
\r
756 var selectedElement = lockedSelection.getSelectedElement(),
\r
757 ranges = !selectedElement && lockedSelection.getRanges();
\r
759 this.isLocked = false;
\r
762 doc.getBody().focus();
\r
764 if ( selectedElement )
\r
765 this.selectElement( selectedElement );
\r
767 this.selectRanges( ranges );
\r
771 if ( !lockedSelection || !restore )
\r
773 this.isLocked = false;
\r
783 selectElement : function( element )
\r
785 if ( this.isLocked )
\r
787 var range = new CKEDITOR.dom.range( this.document );
\r
788 range.setStartBefore( element );
\r
789 range.setEndAfter( element );
\r
791 this._.cache.selectedElement = element;
\r
792 this._.cache.startElement = element;
\r
793 this._.cache.ranges = [ range ];
\r
794 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
799 if ( CKEDITOR.env.ie )
\r
801 this.getNative().empty();
\r
805 // Try to select the node as a control.
\r
806 range = this.document.$.body.createControlRange();
\r
807 range.addElement( element.$ );
\r
812 // If failed, select it as a text range.
\r
813 range = this.document.$.body.createTextRange();
\r
814 range.moveToElementText( element.$ );
\r
822 // Create the range for the element.
\r
823 range = this.document.$.createRange();
\r
824 range.selectNode( element.$ );
\r
826 // Select the range.
\r
827 var sel = this.getNative();
\r
828 sel.removeAllRanges();
\r
829 sel.addRange( range );
\r
835 selectRanges : function( ranges )
\r
837 if ( this.isLocked )
\r
839 this._.cache.selectedElement = null;
\r
840 this._.cache.startElement = ranges[ 0 ].getTouchedStartNode();
\r
841 this._.cache.ranges = ranges;
\r
842 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
847 if ( CKEDITOR.env.ie )
\r
849 // IE doesn't accept multiple ranges selection, so we just
\r
850 // select the first one.
\r
852 ranges[ 0 ].select();
\r
858 var sel = this.getNative();
\r
859 sel.removeAllRanges();
\r
861 for ( var i = 0 ; i < ranges.length ; i++ )
\r
863 var range = ranges[ i ];
\r
864 var nativeRange = this.document.$.createRange();
\r
865 var startContainer = range.startContainer;
\r
867 // In FF2, if we have a collapsed range, inside an empty
\r
868 // element, we must add something to it otherwise the caret
\r
869 // will not be visible.
\r
870 if ( range.collapsed &&
\r
871 ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) &&
\r
872 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
873 !startContainer.getChildCount() )
\r
875 startContainer.appendText( '' );
\r
878 nativeRange.setStart( startContainer.$, range.startOffset );
\r
879 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
881 // Select the range.
\r
882 sel.addRange( nativeRange );
\r
889 createBookmarks : function( serializable )
\r
892 ranges = this.getRanges(),
\r
893 length = ranges.length,
\r
895 for ( var i = 0; i < length ; i++ )
\r
897 retval.push( bookmark = ranges[ i ].createBookmark( serializable, true ) );
\r
899 serializable = bookmark.serializable;
\r
901 var bookmarkStart = serializable ? this.document.getById( bookmark.startNode ) : bookmark.startNode,
\r
902 bookmarkEnd = serializable ? this.document.getById( bookmark.endNode ) : bookmark.endNode;
\r
904 // Updating the offset values for rest of ranges which have been mangled(#3256).
\r
905 for ( var j = i + 1 ; j < length ; j++ )
\r
907 var dirtyRange = ranges[ j ],
\r
908 rangeStart = dirtyRange.startContainer,
\r
909 rangeEnd = dirtyRange.endContainer;
\r
911 rangeStart.equals( bookmarkStart.getParent() ) && dirtyRange.startOffset++;
\r
912 rangeStart.equals( bookmarkEnd.getParent() ) && dirtyRange.startOffset++;
\r
913 rangeEnd.equals( bookmarkStart.getParent() ) && dirtyRange.endOffset++;
\r
914 rangeEnd.equals( bookmarkEnd.getParent() ) && dirtyRange.endOffset++;
\r
921 createBookmarks2 : function( normalized )
\r
923 var bookmarks = [],
\r
924 ranges = this.getRanges();
\r
926 for ( var i = 0 ; i < ranges.length ; i++ )
\r
927 bookmarks.push( ranges[i].createBookmark2( normalized ) );
\r
932 selectBookmarks : function( bookmarks )
\r
935 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
937 var range = new CKEDITOR.dom.range( this.document );
\r
938 range.moveToBookmark( bookmarks[i] );
\r
939 ranges.push( range );
\r
941 this.selectRanges( ranges );
\r
945 // Moving scroll bar to the current selection's start position.
\r
946 scrollIntoView : function()
\r
948 // If we have split the block, adds a temporary span at the
\r
949 // range position and scroll relatively to it.
\r
950 var start = this.getStartElement();
\r
951 start.scrollIntoView();
\r
957 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true );
\r
958 var fillerTextRegex = /\ufeff|\u00a0/;
\r
960 CKEDITOR.dom.range.prototype.select =
\r
963 function( forceExpand )
\r
965 var collapsed = this.collapsed;
\r
966 var isStartMarkerAlone;
\r
969 var bookmark = this.createBookmark();
\r
971 // Create marker tags for the start and end boundaries.
\r
972 var startNode = bookmark.startNode;
\r
976 endNode = bookmark.endNode;
\r
978 // Create the main range which will be used for the selection.
\r
979 var ieRange = this.document.$.body.createTextRange();
\r
981 // Position the range at the start boundary.
\r
982 ieRange.moveToElementText( startNode.$ );
\r
983 ieRange.moveStart( 'character', 1 );
\r
987 // Create a tool range for the end.
\r
988 var ieRangeEnd = this.document.$.body.createTextRange();
\r
990 // Position the tool range at the end.
\r
991 ieRangeEnd.moveToElementText( endNode.$ );
\r
993 // Move the end boundary of the main range to match the tool range.
\r
994 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
995 ieRange.moveEnd( 'character', -1 );
\r
999 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
1000 // will expand and that the cursor will be blinking on the right place.
\r
1001 // Actually, we are using this flag just to avoid using this hack in all
\r
1002 // situations, but just on those needed.
\r
1003 var next = startNode.getNext( notWhitespaces );
\r
1004 isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?
\r
1005 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );
\r
1007 // Append a temporary <span></span> before the selection.
\r
1008 // This is needed to avoid IE destroying selections inside empty
\r
1009 // inline elements, like <b></b> (#253).
\r
1010 // It is also needed when placing the selection right after an inline
\r
1011 // element to avoid the selection moving inside of it.
\r
1012 dummySpan = this.document.createElement( 'span' );
\r
1013 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1014 dummySpan.insertBefore( startNode );
\r
1016 if ( isStartMarkerAlone )
\r
1018 // To expand empty blocks or line spaces after <br>, we need
\r
1019 // instead to have any char, which will be later deleted using the
\r
1021 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1022 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1026 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1027 this.setStartBefore( startNode );
\r
1028 startNode.remove();
\r
1032 if ( isStartMarkerAlone )
\r
1034 // Move the selection start to include the temporary \ufeff.
\r
1035 ieRange.moveStart( 'character', -1 );
\r
1039 // Remove our temporary stuff.
\r
1040 this.document.$.selection.clear();
\r
1045 dummySpan.remove();
\r
1049 this.setEndBefore( endNode );
\r
1057 var startContainer = this.startContainer;
\r
1059 // If we have a collapsed range, inside an empty element, we must add
\r
1060 // something to it, otherwise the caret will not be visible.
\r
1061 if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )
\r
1062 startContainer.append( new CKEDITOR.dom.text( '' ) );
\r
1064 var nativeRange = this.document.$.createRange();
\r
1065 nativeRange.setStart( startContainer.$, this.startOffset );
\r
1069 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1073 // There is a bug in Firefox implementation (it would be too easy
\r
1074 // otherwise). The new start can't be after the end (W3C says it can).
\r
1075 // So, let's create a new range and collapse it to the desired point.
\r
1076 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )
\r
1078 this.collapse( true );
\r
1079 nativeRange.setEnd( this.endContainer.$, this.endOffset );
\r
1085 var selection = this.document.getSelection().getNative();
\r
1086 selection.removeAllRanges();
\r
1087 selection.addRange( nativeRange );
\r