2 Copyright (c) 2003-2011, 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
73 function rangeRequiresFix( range )
\r
75 function isInlineCt( node )
\r
77 return node && node.type == CKEDITOR.NODE_ELEMENT
\r
78 && node.getName() in CKEDITOR.dtd.$removeEmpty;
\r
81 var start = range.startContainer,
\r
82 offset = range.startOffset;
\r
84 if ( start.type == CKEDITOR.NODE_TEXT )
\r
87 // 1. Empty inline element. <span>^</span>
\r
88 // 2. Adjoin to inline element. <p><strong>text</strong>^</p>
\r
89 return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) );
\r
94 modes : { wysiwyg : 1, source : 1 },
\r
95 exec : function( editor )
\r
97 switch ( editor.mode )
\r
100 editor.document.$.execCommand( 'SelectAll', false, null );
\r
101 // Force triggering selectionChange (#7008)
\r
102 editor.forceNextSelectionCheck();
\r
103 editor.selectionChange();
\r
106 // Select the contents of the textarea
\r
107 var textarea = editor.textarea.$;
\r
108 if ( CKEDITOR.env.ie )
\r
109 textarea.createTextRange().execCommand( 'SelectAll' );
\r
112 textarea.selectionStart = 0;
\r
113 textarea.selectionEnd = textarea.value.length;
\r
121 function createFillingChar( doc )
\r
123 removeFillingChar( doc );
\r
125 var fillingChar = doc.createText( '\u200B' );
\r
126 doc.setCustomData( 'cke-fillingChar', fillingChar );
\r
128 return fillingChar;
\r
131 function getFillingChar( doc )
\r
133 return doc && doc.getCustomData( 'cke-fillingChar' );
\r
136 // Checks if a filling char has been used, eventualy removing it (#1272).
\r
137 function checkFillingChar( doc )
\r
139 var fillingChar = doc && getFillingChar( doc );
\r
142 // Use this flag to avoid removing the filling char right after
\r
144 if ( fillingChar.getCustomData( 'ready' ) )
\r
145 removeFillingChar( doc );
\r
147 fillingChar.setCustomData( 'ready', 1 );
\r
151 function removeFillingChar( doc )
\r
153 var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' );
\r
156 // We can't simply remove the filling node because the user
\r
157 // will actually enlarge it when typing, so we just remove the
\r
158 // invisible char from it.
\r
159 fillingChar.setText( fillingChar.getText().replace( /\u200B/g, '' ) );
\r
164 CKEDITOR.plugins.add( 'selection',
\r
166 init : function( editor )
\r
168 // On WebKit only, we need a special "filling" char on some situations
\r
169 // (#1272). Here we set the events that should invalidate that char.
\r
170 if ( CKEDITOR.env.webkit )
\r
172 editor.on( 'selectionChange', function() { checkFillingChar( editor.document ); } );
\r
173 editor.on( 'beforeSetMode', function() { removeFillingChar( editor.document ); } );
\r
174 editor.on( 'key', function( e )
\r
176 // Remove the filling char before some keys get
\r
177 // executed, so they'll not get blocked by it.
\r
178 switch ( e.data.keyCode )
\r
181 case CKEDITOR.SHIFT + 13 : // SHIFT-ENTER
\r
182 case 37 : // LEFT-ARROW
\r
183 case 39 : // RIGHT-ARROW
\r
184 case 8 : // BACKSPACE
\r
185 removeFillingChar( editor.document );
\r
187 }, null, null, 10 );
\r
189 var fillingCharBefore,
\r
192 function beforeData()
\r
194 var doc = editor.document,
\r
195 fillingChar = getFillingChar( doc );
\r
199 // If cursor is right blinking by side of the filler node, save it for restoring,
\r
200 // as the following text substitution will blind it. (#7437)
\r
201 var sel = doc.$.defaultView.getSelection();
\r
202 if ( sel.type == 'Caret' && sel.anchorNode == fillingChar.$ )
\r
203 resetSelection = 1;
\r
205 fillingCharBefore = fillingChar.getText();
\r
206 fillingChar.setText( fillingCharBefore.replace( /\u200B/g, '' ) );
\r
209 function afterData()
\r
211 var doc = editor.document,
\r
212 fillingChar = getFillingChar( doc );
\r
216 fillingChar.setText( fillingCharBefore );
\r
218 if ( resetSelection )
\r
220 doc.$.defaultView.getSelection().setPosition( fillingChar.$,fillingChar.getLength() );
\r
221 resetSelection = 0;
\r
225 editor.on( 'beforeUndoImage', beforeData );
\r
226 editor.on( 'afterUndoImage', afterData );
\r
227 editor.on( 'beforeGetData', beforeData, null, null, 0 );
\r
228 editor.on( 'getData', afterData );
\r
231 editor.on( 'contentDom', function()
\r
233 var doc = editor.document,
\r
234 body = doc.getBody(),
\r
235 html = doc.getDocumentElement();
\r
237 if ( CKEDITOR.env.ie )
\r
239 // Other browsers don't loose the selection if the
\r
240 // editor document loose the focus. In IE, we don't
\r
241 // have support for it, so we reproduce it here, other
\r
242 // than firing the selection change event.
\r
246 restoreEnabled = 1;
\r
248 // "onfocusin" is fired before "onfocus". It makes it
\r
249 // possible to restore the selection before click
\r
250 // events get executed.
\r
251 body.on( 'focusin', function( evt )
\r
253 // If there are elements with layout they fire this event but
\r
254 // it must be ignored to allow edit its contents #4682
\r
255 if ( evt.data.$.srcElement.nodeName != 'BODY' )
\r
258 // If we have saved a range, restore it at this
\r
262 if ( restoreEnabled )
\r
264 // Well not break because of this.
\r
267 savedRange.select();
\r
272 // Update locked selection because of the normalized text nodes. (#6083, #6987)
\r
273 var lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
274 if ( lockedSelection )
\r
276 lockedSelection.unlock();
\r
277 lockedSelection.lock();
\r
285 body.on( 'focus', function()
\r
287 // Enable selections to be saved.
\r
293 body.on( 'beforedeactivate', function( evt )
\r
295 // Ignore this event if it's caused by focus switch between
\r
296 // internal editable control type elements, e.g. layouted paragraph. (#4682)
\r
297 if ( evt.data.$.toElement )
\r
300 // Disable selections from being saved.
\r
302 restoreEnabled = 1;
\r
305 // IE before version 8 will leave cursor blinking inside the document after
\r
306 // editor blurred unless we clean up the selection. (#4716)
\r
307 if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
\r
309 editor.on( 'blur', function( evt )
\r
311 // Try/Catch to avoid errors if the editor is hidden. (#6375)
\r
314 editor.document && editor.document.$.selection.empty();
\r
320 // Listening on document element ensures that
\r
321 // scrollbar is included. (#5280)
\r
322 html.on( 'mousedown', function()
\r
324 // Lock restore selection now, as we have
\r
325 // a followed 'click' event which introduce
\r
326 // new selection. (#5735)
\r
327 restoreEnabled = 0;
\r
330 html.on( 'mouseup', function()
\r
332 restoreEnabled = 1;
\r
335 // In IE6/7 the blinking cursor appears, but contents are
\r
336 // not editable. (#5634)
\r
337 if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )
\r
339 // The 'click' event is not fired when clicking the
\r
340 // scrollbars, so we can use it to check whether
\r
341 // the empty space following <body> has been clicked.
\r
342 html.on( 'click', function( evt )
\r
344 if ( evt.data.getTarget().getName() == 'html' )
\r
345 editor.getSelection().getRanges()[ 0 ].select();
\r
350 // IE fires the "selectionchange" event when clicking
\r
351 // inside a selection. We don't want to capture that.
\r
352 body.on( 'mousedown', function( evt )
\r
354 // IE scrolls document to top on right mousedown
\r
355 // when editor has no focus, remember this scroll
\r
356 // position and revert it before context menu opens. (#5778)
\r
357 if ( evt.data.$.button == 2 )
\r
359 var sel = editor.document.$.selection;
\r
360 if ( sel.type == 'None' )
\r
361 scroll = editor.window.getScrollPosition();
\r
366 body.on( 'mouseup',
\r
369 // Restore recorded scroll position when needed on right mouseup.
\r
370 if ( evt.data.$.button == 2 && scroll )
\r
372 editor.document.$.documentElement.scrollLeft = scroll.x;
\r
373 editor.document.$.documentElement.scrollTop = scroll.y;
\r
378 setTimeout( function()
\r
380 saveSelection( true );
\r
385 body.on( 'keydown', disableSave );
\r
394 // IE is the only to provide the "selectionchange"
\r
396 doc.on( 'selectionchange', saveSelection );
\r
398 function disableSave()
\r
403 function saveSelection( testIt )
\r
407 var doc = editor.document,
\r
408 sel = editor.getSelection(),
\r
409 nativeSel = sel && sel.getNative();
\r
411 // There is a very specific case, when clicking
\r
412 // inside a text selection. In that case, the
\r
413 // selection collapses at the clicking point,
\r
414 // but the selection object remains in an
\r
415 // unknown state, making createRange return a
\r
416 // range at the very start of the document. In
\r
417 // such situation we have to test the range, to
\r
418 // be sure it's valid.
\r
419 if ( testIt && nativeSel && nativeSel.type == 'None' )
\r
421 // The "InsertImage" command can be used to
\r
422 // test whether the selection is good or not.
\r
423 // If not, it's enough to give some time to
\r
424 // IE to put things in order for us.
\r
425 if ( !doc.$.queryCommandEnabled( 'InsertImage' ) )
\r
427 CKEDITOR.tools.setTimeout( saveSelection, 50, this, true );
\r
432 // Avoid saving selection from within text input. (#5747)
\r
434 if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'
\r
435 && ( parentTag = nativeSel.createRange() )
\r
436 && ( parentTag = parentTag.parentElement() )
\r
437 && ( parentTag = parentTag.nodeName )
\r
438 && parentTag.toLowerCase() in { input: 1, textarea : 1 } )
\r
443 savedRange = nativeSel && sel.getRanges()[ 0 ];
\r
445 checkSelectionChangeTimeout.call( editor );
\r
451 // In other browsers, we make the selection change
\r
452 // check based on other events, like clicks or keys
\r
455 doc.on( 'mouseup', checkSelectionChangeTimeout, editor );
\r
456 doc.on( 'keyup', checkSelectionChangeTimeout, editor );
\r
460 // Clear the cached range path before unload. (#7174)
\r
461 editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );
\r
463 editor.addCommand( 'selectAll', selectAllCmd );
\r
464 editor.ui.addButton( 'SelectAll',
\r
466 label : editor.lang.selectAll,
\r
467 command : 'selectAll'
\r
470 editor.selectionChange = checkSelectionChangeTimeout;
\r
475 * Gets the current selection from the editing area when in WYSIWYG mode.
\r
476 * @returns {CKEDITOR.dom.selection} A selection object or null if not on
\r
477 * WYSIWYG mode or no selection is available.
\r
479 * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;
\r
480 * alert( selection.getType() );
\r
482 CKEDITOR.editor.prototype.getSelection = function()
\r
484 return this.document && this.document.getSelection();
\r
487 CKEDITOR.editor.prototype.forceNextSelectionCheck = function()
\r
489 delete this._.selectionPreviousPath;
\r
493 * Gets the current selection from the document.
\r
494 * @returns {CKEDITOR.dom.selection} A selection object.
\r
496 * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;
\r
497 * alert( selection.getType() );
\r
499 CKEDITOR.dom.document.prototype.getSelection = function()
\r
501 var sel = new CKEDITOR.dom.selection( this );
\r
502 return ( !sel || sel.isInvalid ) ? null : sel;
\r
509 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_NONE )
\r
510 * alert( 'Nothing is selected' );
\r
512 CKEDITOR.SELECTION_NONE = 1;
\r
515 * Text or collapsed selection.
\r
518 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )
\r
519 * alert( 'Text is selected' );
\r
521 CKEDITOR.SELECTION_TEXT = 2;
\r
524 * Element selection.
\r
527 * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_ELEMENT )
\r
528 * alert( 'An element is selected' );
\r
530 CKEDITOR.SELECTION_ELEMENT = 3;
\r
533 * Manipulates the selection in a DOM document.
\r
537 CKEDITOR.dom.selection = function( document )
\r
539 var lockedSelection = document.getCustomData( 'cke_locked_selection' );
\r
541 if ( lockedSelection )
\r
542 return lockedSelection;
\r
544 this.document = document;
\r
552 * IE BUG: The selection's document may be a different document than the
\r
553 * editor document. Return null if that's the case.
\r
555 if ( CKEDITOR.env.ie )
\r
557 var range = this.getNative().createRange();
\r
559 || ( range.item && range.item(0).ownerDocument != this.document.$ )
\r
560 || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )
\r
562 this.isInvalid = true;
\r
569 var styleObjectElements =
\r
571 img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,
\r
572 a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1
\r
575 CKEDITOR.dom.selection.prototype =
\r
578 * Gets the native selection object from the browser.
\r
580 * @returns {Object} The native selection object.
\r
582 * var selection = editor.getSelection().<b>getNative()</b>;
\r
588 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.$.selection );
\r
593 return this._.cache.nativeSel || ( this._.cache.nativeSel = this.document.getWindow().$.getSelection() );
\r
597 * Gets the type of the current selection. The following values are
\r
600 * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>
\r
601 * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or
\r
602 * collapsed selection.</li>
\r
603 * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element
\r
607 * @returns {Number} One of the following constant values:
\r
608 * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or
\r
609 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
611 * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )
\r
612 * alert( 'Text is selected' );
\r
618 var cache = this._.cache;
\r
622 var type = CKEDITOR.SELECTION_NONE;
\r
626 var sel = this.getNative(),
\r
629 if ( ieType == 'Text' )
\r
630 type = CKEDITOR.SELECTION_TEXT;
\r
632 if ( ieType == 'Control' )
\r
633 type = CKEDITOR.SELECTION_ELEMENT;
\r
635 // It is possible that we can still get a text range
\r
636 // object even when type == 'None' is returned by IE.
\r
637 // So we'd better check the object returned by
\r
638 // createRange() rather than by looking at the type.
\r
639 if ( sel.createRange().parentElement )
\r
640 type = CKEDITOR.SELECTION_TEXT;
\r
644 return ( cache.type = type );
\r
649 var cache = this._.cache;
\r
653 var type = CKEDITOR.SELECTION_TEXT;
\r
655 var sel = this.getNative();
\r
658 type = CKEDITOR.SELECTION_NONE;
\r
659 else if ( sel.rangeCount == 1 )
\r
661 // Check if the actual selection is a control (IMG,
\r
662 // TABLE, HR, etc...).
\r
664 var range = sel.getRangeAt(0),
\r
665 startContainer = range.startContainer;
\r
667 if ( startContainer == range.endContainer
\r
668 && startContainer.nodeType == 1
\r
669 && ( range.endOffset - range.startOffset ) == 1
\r
670 && styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
\r
672 type = CKEDITOR.SELECTION_ELEMENT;
\r
676 return ( cache.type = type );
\r
680 * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.
\r
681 * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns
\r
682 * one range for each table cell when one or more table row is selected.
\r
685 * var ranges = selection.getRanges();
\r
686 * alert(ranges.length);
\r
688 getRanges : (function()
\r
690 var func = CKEDITOR.env.ie ?
\r
693 function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }
\r
695 // Finds the container and offset for a specific boundary
\r
697 var getBoundaryInformation = function( range, start )
\r
699 // Creates a collapsed range at the requested boundary.
\r
700 range = range.duplicate();
\r
701 range.collapse( start );
\r
703 // Gets the element that encloses the range entirely.
\r
704 var parent = range.parentElement(),
\r
705 doc = parent.ownerDocument;
\r
707 // Empty parent element, e.g. <i>^</i>
\r
708 if ( !parent.hasChildNodes() )
\r
709 return { container : parent, offset : 0 };
\r
711 var siblings = parent.children,
\r
714 testRange = range.duplicate(),
\r
716 endIndex = siblings.length - 1,
\r
721 // Binary search over all element childs to test the range to see whether
\r
722 // range is right on the boundary of one element.
\r
723 while ( startIndex <= endIndex )
\r
725 index = Math.floor( ( startIndex + endIndex ) / 2 );
\r
726 child = siblings[ index ];
\r
727 testRange.moveToElementText( child );
\r
728 position = testRange.compareEndPoints( 'StartToStart', range );
\r
730 if ( position > 0 )
\r
731 endIndex = index - 1;
\r
732 else if ( position < 0 )
\r
733 startIndex = index + 1;
\r
736 // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs.
\r
737 // e.g. <p>text<br />^<br /></p> (#7433)
\r
738 if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' )
\r
740 var bmId = 'cke_range_marker';
\r
741 range.execCommand( 'CreateBookmark', false, bmId );
\r
742 child = doc.getElementsByName( bmId )[ 0 ];
\r
743 var offset = getNodeIndex( child );
\r
744 parent.removeChild( child );
\r
745 return { container : parent, offset : offset };
\r
748 return { container : parent, offset : getNodeIndex( child ) };
\r
752 // All childs are text nodes,
\r
753 // or to the right hand of test range are all text nodes. (#6992)
\r
754 if ( index == -1 || index == siblings.length - 1 && position < 0 )
\r
756 // Adapt test range to embrace the entire parent contents.
\r
757 testRange.moveToElementText( parent );
\r
758 testRange.setEndPoint( 'StartToStart', range );
\r
760 // IE report line break as CRLF with range.text but
\r
761 // only LF with textnode.nodeValue, normalize them to avoid
\r
762 // breaking character counting logic below. (#3949)
\r
763 distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
765 siblings = parent.childNodes;
\r
767 // Actual range anchor right beside test range at the boundary of text node.
\r
770 child = siblings[ siblings.length - 1 ];
\r
772 if ( child.nodeType == CKEDITOR.NODE_ELEMENT )
\r
773 return { container : parent, offset : siblings.length };
\r
775 return { container : child, offset : child.nodeValue.length };
\r
778 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
779 var i = siblings.length;
\r
780 while ( distance > 0 )
\r
781 distance -= siblings[ --i ].nodeValue.length;
\r
783 return { container : siblings[ i ], offset : -distance };
\r
785 // Test range was one offset beyond OR behind the anchored text node.
\r
788 // Adapt one side of test range to the actual range
\r
789 // for measuring the offset between them.
\r
790 testRange.collapse( position > 0 ? true : false );
\r
791 testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );
\r
793 // IE report line break as CRLF with range.text but
\r
794 // only LF with textnode.nodeValue, normalize them to avoid
\r
795 // breaking character counting logic below. (#3949)
\r
796 distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;
\r
798 // Actual range anchor right beside test range at the inner boundary of text node.
\r
800 return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };
\r
802 // Start the measuring until distance overflows, meanwhile count the text nodes.
\r
803 while ( distance > 0 )
\r
807 sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];
\r
808 distance -= sibling.nodeValue.length;
\r
811 // Measurement in IE could be somtimes wrong because of <select> element. (#4611)
\r
814 return { container : parent, offset : getNodeIndex( child ) };
\r
818 return { container : child, offset : position > 0 ? -distance : child.nodeValue.length + distance };
\r
824 // IE doesn't have range support (in the W3C way), so we
\r
825 // need to do some magic to transform selections into
\r
826 // CKEDITOR.dom.range instances.
\r
828 var sel = this.getNative(),
\r
829 nativeRange = sel && sel.createRange(),
\r
830 type = this.getType(),
\r
836 if ( type == CKEDITOR.SELECTION_TEXT )
\r
838 range = new CKEDITOR.dom.range( this.document );
\r
840 var boundaryInfo = getBoundaryInformation( nativeRange, true );
\r
841 range.setStart( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
843 boundaryInfo = getBoundaryInformation( nativeRange );
\r
844 range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );
\r
846 // Correct an invalid IE range case on empty list item. (#5850)
\r
847 if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING
\r
848 && range.endOffset <= range.startContainer.getIndex() )
\r
855 else if ( type == CKEDITOR.SELECTION_ELEMENT )
\r
859 for ( var i = 0 ; i < nativeRange.length ; i++ )
\r
861 var element = nativeRange.item( i ),
\r
862 parentElement = element.parentNode,
\r
865 range = new CKEDITOR.dom.range( this.document );
\r
867 for (; j < parentElement.childNodes.length && parentElement.childNodes[j] != element ; j++ )
\r
870 range.setStart( new CKEDITOR.dom.node( parentElement ), j );
\r
871 range.setEnd( new CKEDITOR.dom.node( parentElement ), j + 1 );
\r
872 retval.push( range );
\r
885 // On browsers implementing the W3C range, we simply
\r
886 // tranform the native ranges in CKEDITOR.dom.range
\r
891 doc = this.document,
\r
892 sel = this.getNative();
\r
897 // On WebKit, it may happen that we'll have no selection
\r
898 // available. We normalize it here by replicating the
\r
899 // behavior of other browsers.
\r
900 if ( !sel.rangeCount )
\r
902 range = new CKEDITOR.dom.range( doc );
\r
903 range.moveToElementEditStart( doc.getBody() );
\r
904 ranges.push( range );
\r
907 for ( var i = 0 ; i < sel.rangeCount ; i++ )
\r
909 var nativeRange = sel.getRangeAt( i );
\r
911 range = new CKEDITOR.dom.range( doc );
\r
913 range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );
\r
914 range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );
\r
915 ranges.push( range );
\r
920 return function( onlyEditables )
\r
922 var cache = this._.cache;
\r
923 if ( cache.ranges && !onlyEditables )
\r
924 return cache.ranges;
\r
925 else if ( !cache.ranges )
\r
926 cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );
\r
928 // Split range into multiple by read-only nodes.
\r
929 if ( onlyEditables )
\r
931 var ranges = cache.ranges;
\r
932 for ( var i = 0; i < ranges.length; i++ )
\r
934 var range = ranges[ i ];
\r
936 // Drop range spans inside one ready-only node.
\r
937 var parent = range.getCommonAncestor();
\r
938 if ( parent.isReadOnly() )
\r
939 ranges.splice( i, 1 );
\r
941 if ( range.collapsed )
\r
944 var startContainer = range.startContainer,
\r
945 endContainer = range.endContainer,
\r
946 startOffset = range.startOffset,
\r
947 endOffset = range.endOffset,
\r
948 walkerRange = range.clone();
\r
950 // Range may start inside a non-editable element, restart range
\r
951 // by the end of it.
\r
953 if ( ( readOnly = startContainer.isReadOnly() ) )
\r
954 range.setStartAfter( readOnly );
\r
956 // Enlarge range start/end with text node to avoid walker
\r
957 // being DOM destructive, it doesn't interfere our checking
\r
958 // of elements below as well.
\r
959 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
961 if ( startOffset >= startContainer.getLength() )
\r
962 walkerRange.setStartAfter( startContainer );
\r
964 walkerRange.setStartBefore( startContainer );
\r
967 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
970 walkerRange.setEndBefore( endContainer );
\r
972 walkerRange.setEndAfter( endContainer );
\r
975 // Looking for non-editable element inside the range.
\r
976 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
977 walker.evaluator = function( node )
\r
979 if ( node.type == CKEDITOR.NODE_ELEMENT
\r
980 && node.isReadOnly() )
\r
982 var newRange = range.clone();
\r
983 range.setEndBefore( node );
\r
985 // Drop collapsed range around read-only elements,
\r
986 // it make sure the range list empty when selecting
\r
987 // only non-editable elements.
\r
988 if ( range.collapsed )
\r
989 ranges.splice( i--, 1 );
\r
991 // Avoid creating invalid range.
\r
992 if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )
\r
994 newRange.setStartAfter( node );
\r
995 if ( !newRange.collapsed )
\r
996 ranges.splice( i + 1, 0, newRange );
\r
1009 return cache.ranges;
\r
1014 * Gets the DOM element in which the selection starts.
\r
1015 * @returns {CKEDITOR.dom.element} The element at the beginning of the
\r
1018 * var element = editor.getSelection().<b>getStartElement()</b>;
\r
1019 * alert( element.getName() );
\r
1021 getStartElement : function()
\r
1023 var cache = this._.cache;
\r
1024 if ( cache.startElement !== undefined )
\r
1025 return cache.startElement;
\r
1028 sel = this.getNative();
\r
1030 switch ( this.getType() )
\r
1032 case CKEDITOR.SELECTION_ELEMENT :
\r
1033 return this.getSelectedElement();
\r
1035 case CKEDITOR.SELECTION_TEXT :
\r
1037 var range = this.getRanges()[0];
\r
1041 if ( !range.collapsed )
\r
1045 // Decrease the range content to exclude particial
\r
1046 // selected node on the start which doesn't have
\r
1047 // visual impact. ( #3231 )
\r
1050 var startContainer = range.startContainer,
\r
1051 startOffset = range.startOffset;
\r
1052 // Limit the fix only to non-block elements.(#3950)
\r
1053 if ( startOffset == ( startContainer.getChildCount ?
\r
1054 startContainer.getChildCount() : startContainer.getLength() )
\r
1055 && !startContainer.isBlockBoundary() )
\r
1056 range.setStartAfter( startContainer );
\r
1060 node = range.startContainer;
\r
1062 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
1063 return node.getParent();
\r
1065 node = node.getChild( range.startOffset );
\r
1067 if ( !node || node.type != CKEDITOR.NODE_ELEMENT )
\r
1068 node = range.startContainer;
\r
1071 var child = node.getFirst();
\r
1072 while ( child && child.type == CKEDITOR.NODE_ELEMENT )
\r
1075 child = child.getFirst();
\r
1081 node = range.startContainer;
\r
1082 if ( node.type != CKEDITOR.NODE_ELEMENT )
\r
1083 node = node.getParent();
\r
1090 return cache.startElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
1094 * Gets the current selected element.
\r
1095 * @returns {CKEDITOR.dom.element} The selected element. Null if no
\r
1096 * selection is available or the selection type is not
\r
1097 * {@link CKEDITOR.SELECTION_ELEMENT}.
\r
1099 * var element = editor.getSelection().<b>getSelectedElement()</b>;
\r
1100 * alert( element.getName() );
\r
1102 getSelectedElement : function()
\r
1104 var cache = this._.cache;
\r
1105 if ( cache.selectedElement !== undefined )
\r
1106 return cache.selectedElement;
\r
1110 var node = CKEDITOR.tools.tryThese(
\r
1111 // Is it native IE control type selection?
\r
1114 return self.getNative().createRange().item( 0 );
\r
1116 // Figure it out by checking if there's a single enclosed
\r
1117 // node of the range.
\r
1120 var range = self.getRanges()[ 0 ],
\r
1124 // Check first any enclosed element, e.g. <ul>[<li><a href="#">item</a></li>]</ul>
\r
1125 for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() )
\r
1126 && ( enclosed.type == CKEDITOR.NODE_ELEMENT )
\r
1127 && styleObjectElements[ enclosed.getName() ]
\r
1128 && ( selected = enclosed ) ); i-- )
\r
1130 // Then check any deep wrapped element, e.g. [<b><i><img /></i></b>]
\r
1131 range.shrink( CKEDITOR.SHRINK_ELEMENT );
\r
1134 return selected.$;
\r
1137 return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );
\r
1142 // Call all cacheable function.
\r
1144 this.getStartElement();
\r
1145 this.getSelectedElement();
\r
1147 // The native selection is not available when locked.
\r
1148 this._.cache.nativeSel = {};
\r
1150 this.isLocked = 1;
\r
1152 // Save this selection inside the DOM document.
\r
1153 this.document.setCustomData( 'cke_locked_selection', this );
\r
1156 unlock : function( restore )
\r
1158 var doc = this.document,
\r
1159 lockedSelection = doc.getCustomData( 'cke_locked_selection' );
\r
1161 if ( lockedSelection )
\r
1163 doc.setCustomData( 'cke_locked_selection', null );
\r
1167 var selectedElement = lockedSelection.getSelectedElement(),
\r
1168 ranges = !selectedElement && lockedSelection.getRanges();
\r
1170 this.isLocked = 0;
\r
1173 doc.getBody().focus();
\r
1175 if ( selectedElement )
\r
1176 this.selectElement( selectedElement );
\r
1178 this.selectRanges( ranges );
\r
1182 if ( !lockedSelection || !restore )
\r
1184 this.isLocked = 0;
\r
1189 reset : function()
\r
1191 this._.cache = {};
\r
1195 * Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.
\r
1198 selectElement : function( element )
\r
1200 if ( this.isLocked )
\r
1202 var range = new CKEDITOR.dom.range( this.document );
\r
1203 range.setStartBefore( element );
\r
1204 range.setEndAfter( element );
\r
1206 this._.cache.selectedElement = element;
\r
1207 this._.cache.startElement = element;
\r
1208 this._.cache.ranges = new CKEDITOR.dom.rangeList( range );
\r
1209 this._.cache.type = CKEDITOR.SELECTION_ELEMENT;
\r
1214 range = new CKEDITOR.dom.range( element.getDocument() );
\r
1215 range.setStartBefore( element );
\r
1216 range.setEndAfter( element );
\r
1219 this.document.fire( 'selectionchange' );
\r
1225 * Adding the specified ranges to document selection preceding
\r
1226 * by clearing up the original selection.
\r
1227 * @param {CKEDITOR.dom.range} ranges
\r
1229 selectRanges : function( ranges )
\r
1231 if ( this.isLocked )
\r
1233 this._.cache.selectedElement = null;
\r
1234 this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();
\r
1235 this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );
\r
1236 this._.cache.type = CKEDITOR.SELECTION_TEXT;
\r
1241 if ( CKEDITOR.env.ie )
\r
1243 if ( ranges.length > 1 )
\r
1245 // IE doesn't accept multiple ranges selection, so we join all into one.
\r
1246 var last = ranges[ ranges.length -1 ] ;
\r
1247 ranges[ 0 ].setEnd( last.endContainer, last.endOffset );
\r
1248 ranges.length = 1;
\r
1251 if ( ranges[ 0 ] )
\r
1252 ranges[ 0 ].select();
\r
1258 var sel = this.getNative();
\r
1260 // getNative() returns null if iframe is "display:none" in FF. (#6577)
\r
1264 if ( ranges.length )
\r
1266 sel.removeAllRanges();
\r
1267 // Remove any existing filling char first.
\r
1268 CKEDITOR.env.webkit && removeFillingChar( this.document );
\r
1271 for ( var i = 0 ; i < ranges.length ; i++ )
\r
1273 // Joining sequential ranges introduced by
\r
1274 // readonly elements protection.
\r
1275 if ( i < ranges.length -1 )
\r
1277 var left = ranges[ i ], right = ranges[ i +1 ],
\r
1278 between = left.clone();
\r
1279 between.setStart( left.endContainer, left.endOffset );
\r
1280 between.setEnd( right.startContainer, right.startOffset );
\r
1282 // Don't confused by Firefox adjancent multi-ranges
\r
1283 // introduced by table cells selection.
\r
1284 if ( !between.collapsed )
\r
1286 between.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1287 var ancestor = between.getCommonAncestor(),
\r
1288 enclosed = between.getEnclosedNode();
\r
1290 // The following cases has to be considered:
\r
1291 // 1. <span contenteditable="false">[placeholder]</span>
\r
1292 // 2. <input contenteditable="false" type="radio"/> (#6621)
\r
1293 if ( ancestor.isReadOnly() || enclosed && enclosed.isReadOnly() )
\r
1295 right.setStart( left.startContainer, left.startOffset );
\r
1296 ranges.splice( i--, 1 );
\r
1302 var range = ranges[ i ];
\r
1303 var nativeRange = this.document.$.createRange();
\r
1304 var startContainer = range.startContainer;
\r
1306 // In FF2, if we have a collapsed range, inside an empty
\r
1307 // element, we must add something to it otherwise the caret
\r
1308 // will not be visible.
\r
1309 // In Opera instead, the selection will be moved out of the
\r
1310 // element. (#4657)
\r
1311 if ( range.collapsed &&
\r
1312 ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&
\r
1313 startContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
1314 !startContainer.getChildCount() )
\r
1316 startContainer.appendText( '' );
\r
1319 if ( range.collapsed
\r
1320 && CKEDITOR.env.webkit
\r
1321 && rangeRequiresFix( range ) )
\r
1323 // Append a zero-width space so WebKit will not try to
\r
1324 // move the selection by itself (#1272).
\r
1325 var fillingChar = createFillingChar( this.document );
\r
1326 range.insertNode( fillingChar ) ;
\r
1328 var next = fillingChar.getNext();
\r
1330 // If the filling char is followed by a <br>, whithout
\r
1331 // having something before it, it'll not blink.
\r
1332 // Let's remove it in this case.
\r
1333 if ( next && !fillingChar.getPrevious() && next.type == CKEDITOR.NODE_ELEMENT && next.getName() == 'br' )
\r
1335 removeFillingChar( this.document );
\r
1336 range.moveToPosition( next, CKEDITOR.POSITION_BEFORE_START );
\r
1339 range.moveToPosition( fillingChar, CKEDITOR.POSITION_AFTER_END );
\r
1342 nativeRange.setStart( range.startContainer.$, range.startOffset );
\r
1346 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1350 // There is a bug in Firefox implementation (it would be too easy
\r
1351 // otherwise). The new start can't be after the end (W3C says it can).
\r
1352 // So, let's create a new range and collapse it to the desired point.
\r
1353 if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )
\r
1355 range.collapse( 1 );
\r
1356 nativeRange.setEnd( range.endContainer.$, range.endOffset );
\r
1362 // Select the range.
\r
1363 sel.addRange( nativeRange );
\r
1371 * Create bookmark for every single of this selection range (from #getRanges)
\r
1372 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,
\r
1373 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1374 * received as with the underlay range method.
\r
1376 createBookmarks : function( serializable )
\r
1378 return this.getRanges().createBookmarks( serializable );
\r
1382 * Create bookmark for every single of this selection range (from #getRanges)
\r
1383 * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,
\r
1384 * with extra cares to avoid interferon among those ranges. Same arguments are
\r
1385 * received as with the underlay range method.
\r
1387 createBookmarks2 : function( normalized )
\r
1389 return this.getRanges().createBookmarks2( normalized );
\r
1393 * Select the virtual ranges denote by the bookmarks by calling #selectRanges.
\r
1394 * @param bookmarks
\r
1396 selectBookmarks : function( bookmarks )
\r
1399 for ( var i = 0 ; i < bookmarks.length ; i++ )
\r
1401 var range = new CKEDITOR.dom.range( this.document );
\r
1402 range.moveToBookmark( bookmarks[i] );
\r
1403 ranges.push( range );
\r
1405 this.selectRanges( ranges );
\r
1410 * Retrieve the common ancestor node of the first range and the last range.
\r
1412 getCommonAncestor : function()
\r
1414 var ranges = this.getRanges(),
\r
1415 startNode = ranges[ 0 ].startContainer,
\r
1416 endNode = ranges[ ranges.length - 1 ].endContainer;
\r
1417 return startNode.getCommonAncestor( endNode );
\r
1421 * Moving scroll bar to the current selection's start position.
\r
1423 scrollIntoView : function()
\r
1425 // If we have split the block, adds a temporary span at the
\r
1426 // range position and scroll relatively to it.
\r
1427 var start = this.getStartElement();
\r
1428 start.scrollIntoView();
\r
1435 var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1436 fillerTextRegex = /\ufeff|\u00a0/,
\r
1437 nonCells = { table:1,tbody:1,tr:1 };
\r
1439 CKEDITOR.dom.range.prototype.select =
\r
1442 function( forceExpand )
\r
1444 var collapsed = this.collapsed,
\r
1445 isStartMarkerAlone, dummySpan, ieRange;
\r
1447 // Try to make a object selection.
\r
1448 var selected = this.getEnclosedNode();
\r
1453 ieRange = this.document.$.body.createControlRange();
\r
1454 ieRange.addElement( selected.$ );
\r
1461 // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.
\r
1462 // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...
\r
1463 if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells
\r
1464 || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )
\r
1466 this.shrink( CKEDITOR.NODE_ELEMENT, true );
\r
1469 var bookmark = this.createBookmark();
\r
1471 // Create marker tags for the start and end boundaries.
\r
1472 var startNode = bookmark.startNode;
\r
1476 endNode = bookmark.endNode;
\r
1478 // Create the main range which will be used for the selection.
\r
1479 ieRange = this.document.$.body.createTextRange();
\r
1481 // Position the range at the start boundary.
\r
1482 ieRange.moveToElementText( startNode.$ );
\r
1483 ieRange.moveStart( 'character', 1 );
\r
1487 // Create a tool range for the end.
\r
1488 var ieRangeEnd = this.document.$.body.createTextRange();
\r
1490 // Position the tool range at the end.
\r
1491 ieRangeEnd.moveToElementText( endNode.$ );
\r
1493 // Move the end boundary of the main range to match the tool range.
\r
1494 ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );
\r
1495 ieRange.moveEnd( 'character', -1 );
\r
1499 // The isStartMarkerAlone logic comes from V2. It guarantees that the lines
\r
1500 // will expand and that the cursor will be blinking on the right place.
\r
1501 // Actually, we are using this flag just to avoid using this hack in all
\r
1502 // situations, but just on those needed.
\r
1503 var next = startNode.getNext( notWhitespaces );
\r
1504 isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?
\r
1505 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );
\r
1507 // Append a temporary <span></span> before the selection.
\r
1508 // This is needed to avoid IE destroying selections inside empty
\r
1509 // inline elements, like <b></b> (#253).
\r
1510 // It is also needed when placing the selection right after an inline
\r
1511 // element to avoid the selection moving inside of it.
\r
1512 dummySpan = this.document.createElement( 'span' );
\r
1513 dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.
\r
1514 dummySpan.insertBefore( startNode );
\r
1516 if ( isStartMarkerAlone )
\r
1518 // To expand empty blocks or line spaces after <br>, we need
\r
1519 // instead to have any char, which will be later deleted using the
\r
1521 // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)
\r
1522 this.document.createText( '\ufeff' ).insertBefore( startNode );
\r
1526 // Remove the markers (reset the position, because of the changes in the DOM tree).
\r
1527 this.setStartBefore( startNode );
\r
1528 startNode.remove();
\r
1532 if ( isStartMarkerAlone )
\r
1534 // Move the selection start to include the temporary \ufeff.
\r
1535 ieRange.moveStart( 'character', -1 );
\r
1539 // Remove our temporary stuff.
\r
1540 this.document.$.selection.clear();
\r
1545 this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );
\r
1546 dummySpan.remove();
\r
1550 this.setEndBefore( endNode );
\r
1555 this.document.fire( 'selectionchange' );
\r
1560 this.document.getSelection().selectRanges( [ this ] );
\r