X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fselection%2Fplugin.js;h=e192e5e1cf9cf1a16defdf4f8ebd528be43d7cba;hb=3fe9cac293e090ea459a3ee10d78cbe9e1dd0e03;hp=9a85a84da5502185fdd87382aa8f6ac183c71547;hpb=e7789c1ad838194d45eeee6ac2eb6e55f5cf35a1;p=ckeditor.git diff --git a/_source/plugins/selection/plugin.js b/_source/plugins/selection/plugin.js index 9a85a84..e192e5e 100644 --- a/_source/plugins/selection/plugin.js +++ b/_source/plugins/selection/plugin.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -17,7 +17,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // In IE, the "selectionchange" event may still get thrown when // releasing the WYSIWYG mode, so we need to check it first. var sel = this.getSelection(); - if ( !sel ) + if ( !sel || !sel.document.getWindow().$ ) return; var firstElement = sel.getStartElement(); @@ -70,42 +70,211 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // #### checkSelectionChange : END + function rangeRequiresFix( range ) + { + function isTextCt( node, isAtEnd ) + { + if ( !node || node.type == CKEDITOR.NODE_TEXT ) + return false; + + var testRng = range.clone(); + return testRng[ 'moveToElementEdit' + ( isAtEnd ? 'End' : 'Start' ) ]( node ); + } + + var ct = range.startContainer; + + var previous = range.getPreviousNode( isVisible, null, ct ), + next = range.getNextNode( isVisible, null, ct ); + + // Any adjacent text container may absorb the cursor, e.g. + //

text^foo

+ //

foo^text

+ //
^

foo

+ if ( isTextCt( previous ) || isTextCt( next, 1 ) ) + return true; + + // Empty block/inline element is also affected. ^,

^

(#7222) + if ( !( previous || next ) && !( ct.type == CKEDITOR.NODE_ELEMENT && ct.isBlockBoundary() && ct.getBogus() ) ) + return true; + + return false; + } + var selectAllCmd = { modes : { wysiwyg : 1, source : 1 }, + readOnly : CKEDITOR.env.ie || CKEDITOR.env.webkit, exec : function( editor ) { switch ( editor.mode ) { case 'wysiwyg' : editor.document.$.execCommand( 'SelectAll', false, null ); + // Force triggering selectionChange (#7008) + editor.forceNextSelectionCheck(); + editor.selectionChange(); break; case 'source' : // Select the contents of the textarea - var textarea = editor.textarea.$ ; + var textarea = editor.textarea.$; if ( CKEDITOR.env.ie ) - { - textarea.createTextRange().execCommand( 'SelectAll' ) ; - } + textarea.createTextRange().execCommand( 'SelectAll' ); else { - textarea.selectionStart = 0 ; - textarea.selectionEnd = textarea.value.length ; + textarea.selectionStart = 0; + textarea.selectionEnd = textarea.value.length; } - textarea.focus() ; + textarea.focus(); } }, canUndo : false }; + function createFillingChar( doc ) + { + removeFillingChar( doc ); + + var fillingChar = doc.createText( '\u200B' ); + doc.setCustomData( 'cke-fillingChar', fillingChar ); + + return fillingChar; + } + + function getFillingChar( doc ) + { + return doc && doc.getCustomData( 'cke-fillingChar' ); + } + + // Checks if a filling char has been used, eventualy removing it (#1272). + function checkFillingChar( doc ) + { + var fillingChar = doc && getFillingChar( doc ); + if ( fillingChar ) + { + // Use this flag to avoid removing the filling char right after + // creating it. + if ( fillingChar.getCustomData( 'ready' ) ) + removeFillingChar( doc ); + else + fillingChar.setCustomData( 'ready', 1 ); + } + } + + function removeFillingChar( doc ) + { + var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' ); + if ( fillingChar ) + { + var bm, + sel = doc.getSelection().getNative(), + // Be error proof. + range = sel && sel.type != 'None' && sel.getRangeAt( 0 ); + + // Text selection position might get mangled by + // subsequent dom modification, save it now for restoring. (#8617) + if ( fillingChar.getLength() > 1 + && range && range.intersectsNode( fillingChar.$ ) ) + { + bm = [ sel.anchorOffset, sel.focusOffset ]; + + // Anticipate the offset change brought by the removed char. + var startAffected = sel.anchorNode == fillingChar.$ && sel.anchorOffset > 0, + endAffected = sel.focusNode == fillingChar.$ && sel.focusOffset > 0; + startAffected && bm[ 0 ]--; + endAffected && bm[ 1 ]--; + + // Revert the bookmark order on reverse selection. + isReversedSelection( sel ) && bm.unshift( bm.pop() ); + } + + // We can't simply remove the filling node because the user + // will actually enlarge it when typing, so we just remove the + // invisible char from it. + fillingChar.setText( fillingChar.getText().replace( /\u200B/g, '' ) ); + + // Restore the bookmark. + if ( bm ) + { + var rng = sel.getRangeAt( 0 ); + rng.setStart( rng.startContainer, bm[ 0 ] ); + rng.setEnd( rng.startContainer, bm[ 1 ] ); + sel.removeAllRanges(); + sel.addRange( rng ); + } + } + } + + function isReversedSelection( sel ) + { + if ( !sel.isCollapsed ) + { + var range = sel.getRangeAt( 0 ); + // Potentially alter an reversed selection range. + range.setStart( sel.anchorNode, sel.anchorOffset ); + range.setEnd( sel.focusNode, sel.focusOffset ); + return range.collapsed; + } + } + CKEDITOR.plugins.add( 'selection', { init : function( editor ) { + // On WebKit only, we need a special "filling" char on some situations + // (#1272). Here we set the events that should invalidate that char. + if ( CKEDITOR.env.webkit ) + { + editor.on( 'selectionChange', function() { checkFillingChar( editor.document ); } ); + editor.on( 'beforeSetMode', function() { removeFillingChar( editor.document ); } ); + + var fillingCharBefore, + resetSelection; + + function beforeData() + { + var doc = editor.document, + fillingChar = getFillingChar( doc ); + + if ( fillingChar ) + { + // If cursor is right blinking by side of the filler node, save it for restoring, + // as the following text substitution will blind it. (#7437) + var sel = doc.$.defaultView.getSelection(); + if ( sel.type == 'Caret' && sel.anchorNode == fillingChar.$ ) + resetSelection = 1; + + fillingCharBefore = fillingChar.getText(); + fillingChar.setText( fillingCharBefore.replace( /\u200B/g, '' ) ); + } + } + function afterData() + { + var doc = editor.document, + fillingChar = getFillingChar( doc ); + + if ( fillingChar ) + { + fillingChar.setText( fillingCharBefore ); + + if ( resetSelection ) + { + doc.$.defaultView.getSelection().setPosition( fillingChar.$,fillingChar.getLength() ); + resetSelection = 0; + } + } + } + editor.on( 'beforeUndoImage', beforeData ); + editor.on( 'afterUndoImage', afterData ); + editor.on( 'beforeGetData', beforeData, null, null, 0 ); + editor.on( 'getData', afterData ); + } + editor.on( 'contentDom', function() { var doc = editor.document, - body = doc.getBody(); + outerDoc = CKEDITOR.document, + body = doc.getBody(), + html = doc.getDocumentElement(); if ( CKEDITOR.env.ie ) { @@ -115,7 +284,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // than firing the selection change event. var savedRange, - saveEnabled; + saveEnabled, + restoreEnabled = 1; // "onfocusin" is fired before "onfocus". It makes it // possible to restore the selection before click @@ -127,18 +297,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( evt.data.$.srcElement.nodeName != 'BODY' ) return; - // If we have saved a range, restore it at this + // Give the priority to locked selection since it probably + // reflects the actual situation, besides locked selection + // could be interfered because of text nodes normalizing. + // (#6083, #6987) + var lockedSelection = doc.getCustomData( 'cke_locked_selection' ); + if ( lockedSelection ) + { + lockedSelection.unlock( 1 ); + lockedSelection.lock(); + } + // Then check ff we have saved a range, restore it at this // point. - if ( savedRange ) + else if ( savedRange && restoreEnabled ) { // Well not break because of this. - try - { - savedRange.select(); - } - catch (e) - {} - + try { savedRange.select(); } catch (e) {} savedRange = null; } }); @@ -146,7 +320,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license body.on( 'focus', function() { // Enable selections to be saved. - saveEnabled = true; + saveEnabled = 1; saveSelection(); }); @@ -159,26 +333,70 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return; // Disable selections from being saved. - saveEnabled = false; + saveEnabled = 0; + restoreEnabled = 1; }); - // IE before version 8 will leave cursor blinking inside the document after - // editor blurred unless we clean up the selection. (#4716) - if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 ) + // [IE] Iframe will still keep the selection when blurred, if + // focus is moved onto a non-editing host, e.g. link or button, but + // it becomes a problem for the object type selection, since the resizer + // handler attached on it will mark other part of the UI, especially + // for the dialog. (#8157) + // [IE<8] Even worse For old IEs, the cursor will not vanish even if + // the selection has been moved to another text input in some cases. (#4716) + // + // Now the range restore is disabled, so we simply force IE to clean + // up the selection before blur. + CKEDITOR.env.ie && editor.on( 'blur', function() { - doc.getWindow().on( 'blur', function( evt ) - { - editor.document.$.selection.empty(); - }); - } + // Error proof when the editor is not visible. (#6375) + try{ doc.$.selection.empty(); } catch ( er){} + }); + // Listening on document element ensures that + // scrollbar is included. (#5280) + html.on( 'mousedown', function() + { + // Lock restore selection now, as we have + // a followed 'click' event which introduce + // new selection. (#5735) + restoreEnabled = 0; + }); + + html.on( 'mouseup', function() + { + restoreEnabled = 1; + }); + + var scroll; // IE fires the "selectionchange" event when clicking // inside a selection. We don't want to capture that. - body.on( 'mousedown', disableSave ); + body.on( 'mousedown', function( evt ) + { + // IE scrolls document to top on right mousedown + // when editor has no focus, remember this scroll + // position and revert it before context menu opens. (#5778) + if ( evt.data.$.button == 2 ) + { + var sel = editor.document.$.selection; + if ( sel.type == 'None' ) + scroll = editor.window.getScrollPosition(); + } + disableSave(); + }); + body.on( 'mouseup', - function() + function( evt ) { - saveEnabled = true; + // Restore recorded scroll position when needed on right mouseup. + if ( evt.data.$.button == 2 && scroll ) + { + editor.document.$.documentElement.scrollLeft = scroll.x; + editor.document.$.documentElement.scrollTop = scroll.y; + } + scroll = null; + + saveEnabled = 1; setTimeout( function() { saveSelection( true ); @@ -190,18 +408,128 @@ For licensing, see LICENSE.html or http://ckeditor.com/license body.on( 'keyup', function() { - saveEnabled = true; + saveEnabled = 1; saveSelection(); }); + // When content doc is in standards mode, IE doesn't produce text selection + // when click on the region outside of body, we emulate + // the correct behavior here. (#1659, #7932, # 9097) + if ( doc.$.compatMode != 'BackCompat' ) + { + if ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) + { + function moveRangeToPoint( range, x, y ) + { + // Error prune in IE7. (#9034, #9110) + try { range.moveToPoint( x, y ); } catch ( e ) {} + } + + html.on( 'mousedown', function( evt ) + { + // Expand the text range along with mouse move. + function onHover( evt ) + { + evt = evt.data.$; + if ( textRng ) + { + // Read the current cursor. + var rngEnd = body.$.createTextRange(); + + moveRangeToPoint( rngEnd, evt.x, evt.y ); + + // Handle drag directions. + textRng.setEndPoint( + startRng.compareEndPoints( 'StartToStart', rngEnd ) < 0 ? + 'EndToEnd' : + 'StartToStart', + rngEnd ); + + // Update selection with new range. + textRng.select(); + } + } + + function removeListeners() + { + outerDoc.removeListener( 'mouseup', onSelectEnd ); + html.removeListener( 'mouseup', onSelectEnd ); + } + + function onSelectEnd() + { + + html.removeListener( 'mousemove', onHover ); + removeListeners(); + + // Make it in effect on mouse up. (#9022) + textRng.select(); + } + + evt = evt.data; + + // We're sure that the click happens at the region + // outside body, but not on scrollbar. + if ( evt.getTarget().is( 'html' ) && + evt.$.x < html.$.clientWidth && + evt.$.y < html.$.clientHeight ) + { + // Start to build the text range. + var textRng = body.$.createTextRange(); + moveRangeToPoint( textRng, evt.$.x, evt.$.y ); + // Records the dragging start of the above text range. + var startRng = textRng.duplicate(); + + html.on( 'mousemove', onHover ); + outerDoc.on( 'mouseup', onSelectEnd ); + html.on( 'mouseup', onSelectEnd ); + } + }); + } + + // It's much simpler for IE > 8, we just need to reselect the reported range. + if ( CKEDITOR.env.ie8 ) + { + html.on( 'mousedown', function( evt ) + { + if ( evt.data.getTarget().is( 'html' ) ) + { + // Limit the text selection mouse move inside of editable. (#9715) + outerDoc.on( 'mouseup', onSelectEnd ); + html.on( 'mouseup', onSelectEnd ); + } + + }); + + function removeListeners() + { + outerDoc.removeListener( 'mouseup', onSelectEnd ); + html.removeListener( 'mouseup', onSelectEnd ); + } + function onSelectEnd() + { + removeListeners(); + + // The event is not fired when clicking on the scrollbars, + // so we can safely check the following to understand + // whether the empty space following has been clicked. + var sel = CKEDITOR.document.$.selection, + range = sel.createRange(); + // The selection range is reported on host, but actually it should applies to the content doc. + if ( sel.type != 'None' && range.parentElement().ownerDocument == doc.$ ) + range.select(); + } + } + + } // IE is the only to provide the "selectionchange" // event. doc.on( 'selectionchange', saveSelection ); function disableSave() { - saveEnabled = false; + saveEnabled = 0; } function saveSelection( testIt ) @@ -209,7 +537,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( saveEnabled ) { var doc = editor.document, - sel = doc && doc.$.selection; + sel = editor.getSelection(), + nativeSel = sel && sel.getNative(); // There is a very specific case, when clicking // inside a text selection. In that case, the @@ -219,7 +548,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // range at the very start of the document. In // such situation we have to test the range, to // be sure it's valid. - if ( testIt && sel && sel.type == 'None' ) + if ( testIt && nativeSel && nativeSel.type == 'None' ) { // The "InsertImage" command can be used to // test whether the selection is good or not. @@ -232,7 +561,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } } - savedRange = sel && sel.createRange(); + // Avoid saving selection from within text input. (#5747) + var parentTag; + if ( nativeSel && nativeSel.type && nativeSel.type != 'Control' + && ( parentTag = nativeSel.createRange() ) + && ( parentTag = parentTag.parentElement() ) + && ( parentTag = parentTag.nodeName ) + && parentTag.toLowerCase() in { input: 1, textarea : 1 } ) + { + return; + } + + // Not break because of this. (#9132) + try{ savedRange = nativeSel && sel.getRanges()[ 0 ]; } catch( er ) {} checkSelectionChangeTimeout.call( editor ); } @@ -246,9 +587,39 @@ For licensing, see LICENSE.html or http://ckeditor.com/license doc.on( 'mouseup', checkSelectionChangeTimeout, editor ); doc.on( 'keyup', checkSelectionChangeTimeout, editor ); + doc.on( 'selectionchange', checkSelectionChangeTimeout, editor ); + } + + if ( CKEDITOR.env.webkit ) + { + // Before keystroke is handled by editor, check to remove the filling char. + doc.on( 'keydown', function( evt ) + { + var key = evt.data.getKey(); + // Remove the filling char before some keys get + // executed, so they'll not get blocked by it. + switch ( key ) + { + case 13 : // ENTER + case 33 : // PAGEUP + case 34 : // PAGEDOWN + case 35 : // HOME + case 36 : // END + case 37 : // LEFT-ARROW + case 39 : // RIGHT-ARROW + case 8 : // BACKSPACE + case 45 : // INS + case 46 : // DEl + removeFillingChar( editor.document ); + } + + }, null, null, -1 ); } }); + // Clear the cached range path before unload. (#7174) + editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor ); + editor.addCommand( 'selectAll', selectAllCmd ); editor.ui.addButton( 'SelectAll', { @@ -256,16 +627,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license command : 'selectAll' }); - editor.selectionChange = checkSelectionChangeTimeout; + /** + * Check if to fire the {@link CKEDITOR.editor#selectionChange} event + * for the current editor instance. + * + * @param {Boolean} checkNow Check immediately without any delay. + */ + editor.selectionChange = function( checkNow ) + { + ( checkNow ? checkSelectionChange : checkSelectionChangeTimeout ).call( this ); + }; + + // IE9 might cease to work if there's an object selection inside the iframe (#7639). + CKEDITOR.env.ie9Compat && editor.on( 'destroy', function() + { + var sel = editor.getSelection(); + sel && sel.getNative().clear(); + }, null, null, 9 ); } }); /** * Gets the current selection from the editing area when in WYSIWYG mode. - * @returns {CKEDITOR.dom.selection} A selection object or null if not on + * @returns {CKEDITOR.dom.selection} A selection object or null if not in * WYSIWYG mode or no selection is available. * @example - * var selection = CKEDITOR.instances.editor1.getSelection(); + * var selection = CKEDITOR.instances.editor1.getSelection(); * alert( selection.getType() ); */ CKEDITOR.editor.prototype.getSelection = function() @@ -282,7 +669,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license * Gets the current selection from the document. * @returns {CKEDITOR.dom.selection} A selection object. * @example - * var selection = CKEDITOR.instances.editor1.document.getSelection(); + * var selection = CKEDITOR.instances.editor1.document.getSelection(); * alert( selection.getType() ); */ CKEDITOR.dom.document.prototype.getSelection = function() @@ -301,11 +688,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license CKEDITOR.SELECTION_NONE = 1; /** - * Text or collapsed selection. + * A text or a collapsed selection. * @constant * @example * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT ) - * alert( 'Text is selected' ); + * alert( 'A text is selected' ); */ CKEDITOR.SELECTION_TEXT = 2; @@ -321,7 +708,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license /** * Manipulates the selection in a DOM document. * @constructor + * @param {CKEDITOR.dom.document} document The DOM document that contains the selection. * @example + * var sel = new CKEDITOR.dom.selection( CKEDITOR.document ); */ CKEDITOR.dom.selection = function( document ) { @@ -331,7 +720,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return lockedSelection; this.document = document; - this.isLocked = false; + this.isLocked = 0; this._ = { cache : {} @@ -339,14 +728,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license /** * IE BUG: The selection's document may be a different document than the - * editor document. Return null if that's the case. + * editor document. Return null if that is the case. */ if ( CKEDITOR.env.ie ) { - var range = this.getNative().createRange(); - if ( !range - || ( range.item && range.item(0).ownerDocument != this.document.$ ) - || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) ) + // Avoid breaking because of it. (#8836) + try + { + var range = this.getNative().createRange(); + if ( !range || + ( range.item && range.item( 0 ).ownerDocument != this.document.$ ) || + ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) ) + { + throw 0; + } + } + catch ( e ) { this.isInvalid = true; } @@ -356,19 +753,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }; var styleObjectElements = - { - img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1, - a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1 - }; + { + img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1, + a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1 + }; CKEDITOR.dom.selection.prototype = { /** * Gets the native selection object from the browser. * @function - * @returns {Object} The native selection object. + * @returns {Object} The native browser selection object. * @example - * var selection = editor.getSelection().getNative(); + * var selection = editor.getSelection().getNative(); */ getNative : CKEDITOR.env.ie ? @@ -386,19 +783,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license * Gets the type of the current selection. The following values are * available: * * @function * @returns {Number} One of the following constant values: - * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or - * {@link CKEDITOR.SELECTION_ELEMENT}. + * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT}, or + * {@link CKEDITOR.SELECTION_ELEMENT}. * @example - * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT ) - * alert( 'Text is selected' ); + * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT ) + * alert( 'A text is selected' ); */ getType : CKEDITOR.env.ie ? @@ -465,10 +862,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return ( cache.type = type ); }, - getRanges : - CKEDITOR.env.ie ? + /** + * Retrieves the {@link CKEDITOR.dom.range} instances that represent the current selection. + * Note: Some browsers return multiple ranges even for a continuous selection. Firefox, for example, returns + * one range for each table cell when one or more table rows are selected. + * @function + * @param {Boolean} [onlyEditables] If set to true, this function retrives editable ranges only. + * @return {Array} Range instances that represent the current selection. + * @example + * var ranges = selection.getRanges(); + * alert( ranges.length ); + */ + getRanges : (function() + { + var func = CKEDITOR.env.ie ? ( function() { + function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); } + // Finds the container and offset for a specific boundary // of an IE range. var getBoundaryInformation = function( range, start ) @@ -478,86 +889,136 @@ For licensing, see LICENSE.html or http://ckeditor.com/license range.collapse( start ); // Gets the element that encloses the range entirely. - var parent = range.parentElement(); - var siblings = parent.childNodes; - - var testRange; - - for ( var i = 0 ; i < siblings.length ; i++ ) + var parent = range.parentElement(), + doc = parent.ownerDocument; + + // Empty parent element, e.g. ^ + if ( !parent.hasChildNodes() ) + return { container : parent, offset : 0 }; + + var siblings = parent.children, + child, + sibling, + testRange = range.duplicate(), + startIndex = 0, + endIndex = siblings.length - 1, + index = -1, + position, + distance, + container; + + // Binary search over all element childs to test the range to see whether + // range is right on the boundary of one element. + while ( startIndex <= endIndex ) { - var child = siblings[ i ]; - if ( child.nodeType == 1 ) + index = Math.floor( ( startIndex + endIndex ) / 2 ); + child = siblings[ index ]; + testRange.moveToElementText( child ); + position = testRange.compareEndPoints( 'StartToStart', range ); + + if ( position > 0 ) + endIndex = index - 1; + else if ( position < 0 ) + startIndex = index + 1; + else { - testRange = range.duplicate(); - - testRange.moveToElementText( child ); - - var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ), - comparisonEnd = testRange.compareEndPoints( 'EndToStart', range ); - - testRange.collapse(); - - if ( comparisonStart > 0 ) - break; - // When selection stay at the side of certain self-closing elements, e.g. BR, - // our comparison will never shows an equality. (#4824) - else if ( !comparisonStart - || comparisonEnd == 1 && comparisonStart == -1 ) - return { container : parent, offset : i }; - else if ( !comparisonEnd ) - return { container : parent, offset : i + 1 }; - - testRange = null; + // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs. + // e.g.

text
^

(#7433) + if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' ) + { + // "Fall back" to w3c selection. + var sel = doc.defaultView.getSelection(); + return { container : sel[ start ? 'anchorNode' : 'focusNode' ], + offset : sel[ start ? 'anchorOffset' : 'focusOffset' ] }; + } + else + return { container : parent, offset : getNodeIndex( child ) }; } } - if ( !testRange ) + // All childs are text nodes, + // or to the right hand of test range are all text nodes. (#6992) + if ( index == -1 || index == siblings.length - 1 && position < 0 ) { - testRange = range.duplicate(); + // Adapt test range to embrace the entire parent contents. testRange.moveToElementText( parent ); - testRange.collapse( false ); - } + testRange.setEndPoint( 'StartToStart', range ); - testRange.setEndPoint( 'StartToStart', range ); - // IE report line break as CRLF with range.text but - // only LF with textnode.nodeValue, normalize them to avoid - // breaking character counting logic below. (#3949) - var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length; + // IE report line break as CRLF with range.text but + // only LF with textnode.nodeValue, normalize them to avoid + // breaking character counting logic below. (#3949) + distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length; - try - { - while ( distance > 0 ) - distance -= siblings[ --i ].nodeValue.length; - } - // Measurement in IE could be somtimes wrong because of element. (#4611) + catch( e ) + { + return { container : parent, offset : getNodeIndex( child ) }; + } + } + + return { container : container, offset : position > 0 ? -distance : container.nodeValue.length + distance }; } }; return function() { - var cache = this._.cache; - if ( cache.ranges ) - return cache.ranges; - // IE doesn't have range support (in the W3C way), so we // need to do some magic to transform selections into // CKEDITOR.dom.range instances. @@ -580,11 +1041,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license boundaryInfo = getBoundaryInformation( nativeRange ); range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset ); - return ( cache.ranges = [ range ] ); + // Correct an invalid IE range case on empty list item. (#5850) + if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING + && range.endOffset <= range.startContainer.getIndex() ) + { + range.collapse(); + } + + return [ range ]; } else if ( type == CKEDITOR.SELECTION_ELEMENT ) { - var retval = this._.cache.ranges = []; + var retval = []; for ( var i = 0 ; i < nativeRange.length ; i++ ) { @@ -605,45 +1073,159 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return retval; } - return ( cache.ranges = [] ); + return []; }; })() : function() { - var cache = this._.cache; - if ( cache.ranges ) - return cache.ranges; // On browsers implementing the W3C range, we simply // tranform the native ranges in CKEDITOR.dom.range // instances. - var ranges = []; - var sel = this.getNative(); + var ranges = [], + range, + doc = this.document, + sel = this.getNative(); if ( !sel ) - return []; + return ranges; + + // On WebKit, it may happen that we'll have no selection + // available. We normalize it here by replicating the + // behavior of other browsers. + if ( !sel.rangeCount ) + { + range = new CKEDITOR.dom.range( doc ); + range.moveToElementEditStart( doc.getBody() ); + ranges.push( range ); + } for ( var i = 0 ; i < sel.rangeCount ; i++ ) { var nativeRange = sel.getRangeAt( i ); - var range = new CKEDITOR.dom.range( this.document ); + + range = new CKEDITOR.dom.range( doc ); range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset ); range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset ); ranges.push( range ); } + return ranges; + }; - return ( cache.ranges = ranges ); - }, + return function( onlyEditables ) + { + var cache = this._.cache; + if ( cache.ranges && !onlyEditables ) + return cache.ranges; + else if ( !cache.ranges ) + cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) ); + + // Split range into multiple by read-only nodes. + if ( onlyEditables ) + { + var ranges = cache.ranges; + for ( var i = 0; i < ranges.length; i++ ) + { + var range = ranges[ i ]; + + // Drop range spans inside one ready-only node. + var parent = range.getCommonAncestor(); + if ( parent.isReadOnly() ) + ranges.splice( i, 1 ); + + if ( range.collapsed ) + continue; + + // Range may start inside a non-editable element, + // replace the range start after it. + if ( range.startContainer.isReadOnly() ) + { + var current = range.startContainer; + while( current ) + { + if ( current.is( 'body' ) || !current.isReadOnly() ) + break; + + if ( current.type == CKEDITOR.NODE_ELEMENT + && current.getAttribute( 'contentEditable' ) == 'false' ) + range.setStartAfter( current ); + + current = current.getParent(); + } + } + + var startContainer = range.startContainer, + endContainer = range.endContainer, + startOffset = range.startOffset, + endOffset = range.endOffset, + walkerRange = range.clone(); + + // Enlarge range start/end with text node to avoid walker + // being DOM destructive, it doesn't interfere our checking + // of elements below as well. + if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT ) + { + if ( startOffset >= startContainer.getLength() ) + walkerRange.setStartAfter( startContainer ); + else + walkerRange.setStartBefore( startContainer ); + } + + if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT ) + { + if ( !endOffset ) + walkerRange.setEndBefore( endContainer ); + else + walkerRange.setEndAfter( endContainer ); + } + + // Looking for non-editable element inside the range. + var walker = new CKEDITOR.dom.walker( walkerRange ); + walker.evaluator = function( node ) + { + if ( node.type == CKEDITOR.NODE_ELEMENT + && node.isReadOnly() ) + { + var newRange = range.clone(); + range.setEndBefore( node ); + + // Drop collapsed range around read-only elements, + // it make sure the range list empty when selecting + // only non-editable elements. + if ( range.collapsed ) + ranges.splice( i--, 1 ); + + // Avoid creating invalid range. + if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) ) + { + newRange.setStartAfter( node ); + if ( !newRange.collapsed ) + ranges.splice( i + 1, 0, newRange ); + } + + return true; + } + + return false; + }; + + walker.next(); + } + } + + return cache.ranges; + }; + })(), /** * Gets the DOM element in which the selection starts. * @returns {CKEDITOR.dom.element} The element at the beginning of the * selection. * @example - * var element = editor.getSelection().getStartElement(); + * var element = editor.getSelection().getStartElement(); * alert( element.getName() ); */ getStartElement : function() @@ -673,7 +1255,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Decrease the range content to exclude particial // selected node on the start which doesn't have // visual impact. ( #3231 ) - while ( true ) + while ( 1 ) { var startContainer = range.startContainer, startOffset = range.startOffset; @@ -693,32 +1275,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license node = node.getChild( range.startOffset ); if ( !node || node.type != CKEDITOR.NODE_ELEMENT ) - return range.startContainer; - - var child = node.getFirst(); - while ( child && child.type == CKEDITOR.NODE_ELEMENT ) + node = range.startContainer; + else { - node = child; - child = child.getFirst(); + var child = node.getFirst(); + while ( child && child.type == CKEDITOR.NODE_ELEMENT ) + { + node = child; + child = child.getFirst(); + } } - - return node; } - } - - if ( CKEDITOR.env.ie ) - { - range = sel.createRange(); - range.collapse( true ); - - node = range.parentElement(); - } - else - { - node = sel.anchorNode; + else + { + node = range.startContainer; + if ( node.type != CKEDITOR.NODE_ELEMENT ) + node = node.getParent(); + } - if ( node && node.nodeType != 1 ) - node = node.parentNode; + node = node.$; } } @@ -726,12 +1301,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** - * Gets the current selected element. + * Gets the currently selected element. * @returns {CKEDITOR.dom.element} The selected element. Null if no * selection is available or the selection type is not - * {@link CKEDITOR.SELECTION_ELEMENT}. + * {@link CKEDITOR.SELECTION_ELEMENT}. * @example - * var element = editor.getSelection().getSelectedElement(); + * var element = editor.getSelection().getSelectedElement(); * alert( element.getName() ); */ getSelectedElement : function() @@ -748,41 +1323,152 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { return self.getNative().createRange().item( 0 ); }, + // If a table or list is fully selected. + function() + { + var root, + retval, + range = self.getRanges()[ 0 ], + ancestor = range.getCommonAncestor( 1, 1 ), + tags = { table:1,ul:1,ol:1,dl:1 }; + + for ( var t in tags ) + { + if ( ( root = ancestor.getAscendant( t, 1 ) ) ) + break; + } + + if ( root ) + { + // Enlarging the start boundary. + var testRange = new CKEDITOR.dom.range( this.document ); + testRange.setStartAt( root, CKEDITOR.POSITION_AFTER_START ); + testRange.setEnd( range.startContainer, range.startOffset ); + + var enlargeables = CKEDITOR.tools.extend( tags, CKEDITOR.dtd.$listItem, CKEDITOR.dtd.$tableContent ), + walker = new CKEDITOR.dom.walker( testRange ), + // Check the range is at the inner boundary of the structural element. + guard = function( walker, isEnd ) + { + return function( node, isWalkOut ) + { + if ( node.type == CKEDITOR.NODE_TEXT && ( !CKEDITOR.tools.trim( node.getText() ) || node.getParent().data( 'cke-bookmark' ) ) ) + return true; + + var tag; + if ( node.type == CKEDITOR.NODE_ELEMENT ) + { + tag = node.getName(); + + // Bypass bogus br at the end of block. + if ( tag == 'br' && isEnd && node.equals( node.getParent().getBogus() ) ) + return true; + + if ( isWalkOut && tag in enlargeables || tag in CKEDITOR.dtd.$removeEmpty ) + return true; + } + + walker.halted = 1; + return false; + }; + }; + + walker.guard = guard( walker ); + + if ( walker.checkBackward() && !walker.halted ) + { + walker = new CKEDITOR.dom.walker( testRange ); + testRange.setStart( range.endContainer, range.endOffset ); + testRange.setEndAt( root, CKEDITOR.POSITION_BEFORE_END ); + walker.guard = guard( walker, 1 ); + if ( walker.checkForward() && !walker.halted ) + retval = root.$; + } + } + + if ( !retval ) + throw 0; + + return retval; + }, // Figure it out by checking if there's a single enclosed // node of the range. function() { - var range = self.getRanges()[ 0 ]; - range.shrink( CKEDITOR.SHRINK_ELEMENT ); - - var enclosed; - if ( range.startContainer.equals( range.endContainer ) - && ( range.endOffset - range.startOffset ) == 1 - && styleObjectElements[ ( enclosed = range.startContainer.getChild( range.startOffset ) ).getName() ] ) + var range = self.getRanges()[ 0 ], + enclosed, + selected; + + // Check first any enclosed element, e.g. + for ( var i = 2; i && !( ( enclosed = range.getEnclosedNode() ) + && ( enclosed.type == CKEDITOR.NODE_ELEMENT ) + && styleObjectElements[ enclosed.getName() ] + && ( selected = enclosed ) ); i-- ) { - return enclosed.$; + // Then check any deep wrapped element, e.g. [] + range.shrink( CKEDITOR.SHRINK_ELEMENT ); } + + return selected.$; }); return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null ); }, + /** + * Retrieves the text contained within the range. An empty string is returned for non-text selection. + * @returns {String} A string of text within the current selection. + * @since 3.6.1 + * @example + * var text = editor.getSelection().getSelectedText(); + * alert( text ); + */ + getSelectedText : function() + { + var cache = this._.cache; + if ( cache.selectedText !== undefined ) + return cache.selectedText; + + var text = '', + nativeSel = this.getNative(); + if ( this.getType() == CKEDITOR.SELECTION_TEXT ) + text = CKEDITOR.env.ie ? nativeSel.createRange().text : nativeSel.toString(); + + return ( cache.selectedText = text ); + }, + + /** + * Locks the selection made in the editor in order to make it possible to + * manipulate it without browser interference. A locked selection is + * cached and remains unchanged until it is released with the #unlock + * method. + * @example + * editor.getSelection().lock(); + */ lock : function() { // Call all cacheable function. this.getRanges(); this.getStartElement(); this.getSelectedElement(); + this.getSelectedText(); // The native selection is not available when locked. this._.cache.nativeSel = {}; - this.isLocked = true; + this.isLocked = 1; // Save this selection inside the DOM document. this.document.setCustomData( 'cke_locked_selection', this ); }, + /** + * Unlocks the selection made in the editor and locked with the #lock method. + * An unlocked selection is no longer cached and can be changed. + * @param {Boolean} [restore] If set to true, the selection is restored back to the selection saved earlier by using the #lock method. + * @example + * editor.getSelection().unlock(); + */ unlock : function( restore ) { var doc = this.document, @@ -797,11 +1483,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var selectedElement = lockedSelection.getSelectedElement(), ranges = !selectedElement && lockedSelection.getRanges(); - this.isLocked = false; + this.isLocked = 0; this.reset(); - doc.getBody().focus(); - if ( selectedElement ) this.selectElement( selectedElement ); else @@ -811,16 +1495,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( !lockedSelection || !restore ) { - this.isLocked = false; + this.isLocked = 0; this.reset(); } }, + /** + * Clears the selection cache. + * @example + * editor.getSelection().reset(); + */ reset : function() { this._.cache = {}; }, + /** + * Makes the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element. + * @param {CKEDITOR.dom.element} element The element to enclose in the selection. + * @example + * var element = editor.document.getById( 'sampleElement' ); + * editor.getSelection.selectElement( element ); + */ selectElement : function( element ) { if ( this.isLocked ) @@ -831,59 +1527,37 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this._.cache.selectedElement = element; this._.cache.startElement = element; - this._.cache.ranges = [ range ]; + this._.cache.ranges = new CKEDITOR.dom.rangeList( range ); this._.cache.type = CKEDITOR.SELECTION_ELEMENT; return; } - if ( CKEDITOR.env.ie ) - { - this.getNative().empty(); + range = new CKEDITOR.dom.range( element.getDocument() ); + range.setStartBefore( element ); + range.setEndAfter( element ); + range.select(); - try - { - // Try to select the node as a control. - range = this.document.$.body.createControlRange(); - range.addElement( element.$ ); - range.select(); - } - catch(e) - { - // If failed, select it as a text range. - range = this.document.$.body.createTextRange(); - range.moveToElementText( element.$ ); - range.select(); - } - finally - { - this.document.fire( 'selectionchange' ); - } - - this.reset(); - } - else - { - // Create the range for the element. - range = this.document.$.createRange(); - range.selectNode( element.$ ); - - // Select the range. - var sel = this.getNative(); - sel.removeAllRanges(); - sel.addRange( range ); + this.document.fire( 'selectionchange' ); + this.reset(); - this.reset(); - } }, + /** + * Clears the original selection and adds the specified ranges + * to the document selection. + * @param {Array} ranges An array of {@link CKEDITOR.dom.range} instances representing ranges to be added to the document. + * @example + * var ranges = new CKEDITOR.dom.range( editor.document ); + * editor.getSelection().selectRanges( [ ranges ] ); + */ selectRanges : function( ranges ) { if ( this.isLocked ) { this._.cache.selectedElement = null; - this._.cache.startElement = ranges[ 0 ].getTouchedStartNode(); - this._.cache.ranges = ranges; + this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode(); + this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges ); this._.cache.type = CKEDITOR.SELECTION_TEXT; return; @@ -891,8 +1565,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( CKEDITOR.env.ie ) { - // IE doesn't accept multiple ranges selection, so we just - // select the first one. + if ( ranges.length > 1 ) + { + // IE doesn't accept multiple ranges selection, so we join all into one. + var last = ranges[ ranges.length -1 ] ; + ranges[ 0 ].setEnd( last.endContainer, last.endOffset ); + ranges.length = 1; + } + if ( ranges[ 0 ] ) ranges[ 0 ].select(); @@ -901,10 +1581,49 @@ For licensing, see LICENSE.html or http://ckeditor.com/license else { var sel = this.getNative(); - sel.removeAllRanges(); + + // getNative() returns null if iframe is "display:none" in FF. (#6577) + if ( !sel ) + return; + + if ( ranges.length ) + { + sel.removeAllRanges(); + // Remove any existing filling char first. + CKEDITOR.env.webkit && removeFillingChar( this.document ); + } for ( var i = 0 ; i < ranges.length ; i++ ) { + // Joining sequential ranges introduced by + // readonly elements protection. + if ( i < ranges.length -1 ) + { + var left = ranges[ i ], right = ranges[ i +1 ], + between = left.clone(); + between.setStart( left.endContainer, left.endOffset ); + between.setEnd( right.startContainer, right.startOffset ); + + // Don't confused by Firefox adjancent multi-ranges + // introduced by table cells selection. + if ( !between.collapsed ) + { + between.shrink( CKEDITOR.NODE_ELEMENT, true ); + var ancestor = between.getCommonAncestor(), + enclosed = between.getEnclosedNode(); + + // The following cases has to be considered: + // 1. [placeholder] + // 2. (#6621) + if ( ancestor.isReadOnly() || enclosed && enclosed.isReadOnly() ) + { + right.setStart( left.startContainer, left.startOffset ); + ranges.splice( i--, 1 ); + continue; + } + } + } + var range = ranges[ i ]; var nativeRange = this.document.$.createRange(); var startContainer = range.startContainer; @@ -912,68 +1631,105 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // In FF2, if we have a collapsed range, inside an empty // element, we must add something to it otherwise the caret // will not be visible. + // In Opera instead, the selection will be moved out of the + // element. (#4657) if ( range.collapsed && - ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) && + ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() ) { startContainer.appendText( '' ); } - nativeRange.setStart( startContainer.$, range.startOffset ); - nativeRange.setEnd( range.endContainer.$, range.endOffset ); + if ( range.collapsed + && CKEDITOR.env.webkit + && rangeRequiresFix( range ) ) + { + // Append a zero-width space so WebKit will not try to + // move the selection by itself (#1272). + var fillingChar = createFillingChar( this.document ); + range.insertNode( fillingChar ) ; + + var next = fillingChar.getNext(); + + // If the filling char is followed by a
, whithout + // having something before it, it'll not blink. + // Let's remove it in this case. + if ( next && !fillingChar.getPrevious() && next.type == CKEDITOR.NODE_ELEMENT && next.getName() == 'br' ) + { + removeFillingChar( this.document ); + range.moveToPosition( next, CKEDITOR.POSITION_BEFORE_START ); + } + else + range.moveToPosition( fillingChar, CKEDITOR.POSITION_AFTER_END ); + } + + nativeRange.setStart( range.startContainer.$, range.startOffset ); + + try + { + nativeRange.setEnd( range.endContainer.$, range.endOffset ); + } + catch ( e ) + { + // There is a bug in Firefox implementation (it would be too easy + // otherwise). The new start can't be after the end (W3C says it can). + // So, let's create a new range and collapse it to the desired point. + if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 ) + { + range.collapse( 1 ); + nativeRange.setEnd( range.endContainer.$, range.endOffset ); + } + else + throw e; + } // Select the range. sel.addRange( nativeRange ); } + // Don't miss selection change event for non-IEs. + this.document.fire( 'selectionchange' ); this.reset(); } }, + /** + * Creates a bookmark for each range of this selection (from #getRanges) + * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method, + * with extra care taken to avoid interference among those ranges. The arguments + * received are the same as with the underlying range method. + * @returns {Array} Array of bookmarks for each range. + * @example + * var bookmarks = editor.getSelection().createBookmarks(); + */ createBookmarks : function( serializable ) { - var retval = [], - ranges = this.getRanges(), - length = ranges.length, - bookmark; - for ( var i = 0; i < length ; i++ ) - { - retval.push( bookmark = ranges[ i ].createBookmark( serializable, true ) ); - - serializable = bookmark.serializable; - - var bookmarkStart = serializable ? this.document.getById( bookmark.startNode ) : bookmark.startNode, - bookmarkEnd = serializable ? this.document.getById( bookmark.endNode ) : bookmark.endNode; - - // Updating the offset values for rest of ranges which have been mangled(#3256). - for ( var j = i + 1 ; j < length ; j++ ) - { - var dirtyRange = ranges[ j ], - rangeStart = dirtyRange.startContainer, - rangeEnd = dirtyRange.endContainer; - - rangeStart.equals( bookmarkStart.getParent() ) && dirtyRange.startOffset++; - rangeStart.equals( bookmarkEnd.getParent() ) && dirtyRange.startOffset++; - rangeEnd.equals( bookmarkStart.getParent() ) && dirtyRange.endOffset++; - rangeEnd.equals( bookmarkEnd.getParent() ) && dirtyRange.endOffset++; - } - } - - return retval; + return this.getRanges().createBookmarks( serializable ); }, + /** + * Creates a bookmark for each range of this selection (from #getRanges) + * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method, + * with extra care taken to avoid interference among those ranges. The arguments + * received are the same as with the underlying range method. + * @returns {Array} Array of bookmarks for each range. + * @example + * var bookmarks = editor.getSelection().createBookmarks2(); + */ createBookmarks2 : function( normalized ) { - var bookmarks = [], - ranges = this.getRanges(); - - for ( var i = 0 ; i < ranges.length ; i++ ) - bookmarks.push( ranges[i].createBookmark2( normalized ) ); - - return bookmarks; + return this.getRanges().createBookmarks2( normalized ); }, + /** + * Selects the virtual ranges denoted by the bookmarks by calling #selectRanges. + * @param {Array} bookmarks The bookmarks representing ranges to be selected. + * @returns {CKEDITOR.dom.selection} This selection object, after the ranges were selected. + * @example + * var bookmarks = editor.getSelection().createBookmarks(); + * editor.getSelection().selectBookmarks( bookmarks ); + */ selectBookmarks : function( bookmarks ) { var ranges = []; @@ -987,6 +1743,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return this; }, + /** + * Retrieves the common ancestor node of the first range and the last range. + * @returns {CKEDITOR.dom.element} The common ancestor of the selection. + * @example + * var ancestor = editor.getSelection().getCommonAncestor(); + */ getCommonAncestor : function() { var ranges = this.getRanges(), @@ -995,7 +1757,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return startNode.getCommonAncestor( endNode ); }, - // Moving scroll bar to the current selection's start position. + /** + * Moves the scrollbar to the starting position of the current selection. + * @example + * editor.getSelection().scrollIntoView(); + */ scrollIntoView : function() { // If we have split the block, adds a temporary span at the @@ -1004,142 +1770,133 @@ For licensing, see LICENSE.html or http://ckeditor.com/license start.scrollIntoView(); } }; -})(); -( function() -{ -var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ); -var fillerTextRegex = /\ufeff|\u00a0/; - -CKEDITOR.dom.range.prototype.select = - CKEDITOR.env.ie ? - // V2 - function( forceExpand ) - { - var collapsed = this.collapsed; - var isStartMarkerAlone; - var dummySpan; - var bookmark = this.createBookmark(); + var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ), + isVisible = CKEDITOR.dom.walker.invisible( 1 ), + fillerTextRegex = /\ufeff|\u00a0/, + nonCells = { table:1,tbody:1,tr:1 }; - // Create marker tags for the start and end boundaries. - var startNode = bookmark.startNode; + CKEDITOR.dom.range.prototype.select = + CKEDITOR.env.ie ? + // V2 + function( forceExpand ) + { + var collapsed = this.collapsed, + isStartMarkerAlone, dummySpan, ieRange; - var endNode; - if ( !collapsed ) - endNode = bookmark.endNode; + // Try to make a object selection. + var selected = this.getEnclosedNode(); + if ( selected ) + { + try + { + ieRange = this.document.$.body.createControlRange(); + ieRange.addElement( selected.$ ); + ieRange.select(); + return; + } + catch( er ) {} + } - // Create the main range which will be used for the selection. - var ieRange = this.document.$.body.createTextRange(); + // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g. + // [... =>
cell
... + if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells + || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells ) + { + this.shrink( CKEDITOR.NODE_ELEMENT, true ); + } - // Position the range at the start boundary. - ieRange.moveToElementText( startNode.$ ); - ieRange.moveStart( 'character', 1 ); + var bookmark = this.createBookmark(); - if ( endNode ) - { - // Create a tool range for the end. - var ieRangeEnd = this.document.$.body.createTextRange(); + // Create marker tags for the start and end boundaries. + var startNode = bookmark.startNode; - // Position the tool range at the end. - ieRangeEnd.moveToElementText( endNode.$ ); + var endNode; + if ( !collapsed ) + endNode = bookmark.endNode; - // Move the end boundary of the main range to match the tool range. - ieRange.setEndPoint( 'EndToEnd', ieRangeEnd ); - ieRange.moveEnd( 'character', -1 ); - } - else - { - // The isStartMarkerAlone logic comes from V2. It guarantees that the lines - // will expand and that the cursor will be blinking on the right place. - // Actually, we are using this flag just to avoid using this hack in all - // situations, but just on those needed. - var next = startNode.getNext( notWhitespaces ); - isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there? - && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) ); - - // Append a temporary  before the selection. - // This is needed to avoid IE destroying selections inside empty - // inline elements, like (#253). - // It is also needed when placing the selection right after an inline - // element to avoid the selection moving inside of it. - dummySpan = this.document.createElement( 'span' ); - dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359. - dummySpan.insertBefore( startNode ); - - if ( isStartMarkerAlone ) - { - // To expand empty blocks or line spaces after
, we need - // instead to have any char, which will be later deleted using the - // selection. - // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359) - this.document.createText( '\ufeff' ).insertBefore( startNode ); - } - } + // Create the main range which will be used for the selection. + ieRange = this.document.$.body.createTextRange(); - // Remove the markers (reset the position, because of the changes in the DOM tree). - this.setStartBefore( startNode ); - startNode.remove(); + // Position the range at the start boundary. + ieRange.moveToElementText( startNode.$ ); + ieRange.moveStart( 'character', 1 ); - if ( collapsed ) - { - if ( isStartMarkerAlone ) + if ( endNode ) { - // Move the selection start to include the temporary \ufeff. - ieRange.moveStart( 'character', -1 ); + // Create a tool range for the end. + var ieRangeEnd = this.document.$.body.createTextRange(); - ieRange.select(); + // Position the tool range at the end. + ieRangeEnd.moveToElementText( endNode.$ ); - // Remove our temporary stuff. - this.document.$.selection.clear(); + // Move the end boundary of the main range to match the tool range. + ieRange.setEndPoint( 'EndToEnd', ieRangeEnd ); + ieRange.moveEnd( 'character', -1 ); } else - ieRange.select(); + { + // The isStartMarkerAlone logic comes from V2. It guarantees that the lines + // will expand and that the cursor will be blinking on the right place. + // Actually, we are using this flag just to avoid using this hack in all + // situations, but just on those needed. + var next = startNode.getNext( notWhitespaces ); + isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there? + && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) ); + + // Append a temporary  before the selection. + // This is needed to avoid IE destroying selections inside empty + // inline elements, like (#253). + // It is also needed when placing the selection right after an inline + // element to avoid the selection moving inside of it. + dummySpan = this.document.createElement( 'span' ); + dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359. + dummySpan.insertBefore( startNode ); + + if ( isStartMarkerAlone ) + { + // To expand empty blocks or line spaces after
, we need + // instead to have any char, which will be later deleted using the + // selection. + // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359) + this.document.createText( '\ufeff' ).insertBefore( startNode ); + } + } - this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START ); - dummySpan.remove(); - } - else - { - this.setEndBefore( endNode ); - endNode.remove(); - ieRange.select(); - } + // Remove the markers (reset the position, because of the changes in the DOM tree). + this.setStartBefore( startNode ); + startNode.remove(); - this.document.fire( 'selectionchange' ); - } - : - function() - { - var startContainer = this.startContainer; + if ( collapsed ) + { + if ( isStartMarkerAlone ) + { + // Move the selection start to include the temporary \ufeff. + ieRange.moveStart( 'character', -1 ); - // If we have a collapsed range, inside an empty element, we must add - // something to it, otherwise the caret will not be visible. - if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() ) - startContainer.append( new CKEDITOR.dom.text( '' ) ); + ieRange.select(); - var nativeRange = this.document.$.createRange(); - nativeRange.setStart( startContainer.$, this.startOffset ); + // Remove our temporary stuff. + this.document.$.selection.clear(); + } + else + ieRange.select(); - try - { - nativeRange.setEnd( this.endContainer.$, this.endOffset ); - } - catch ( e ) - { - // There is a bug in Firefox implementation (it would be too easy - // otherwise). The new start can't be after the end (W3C says it can). - // So, let's create a new range and collapse it to the desired point. - if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 ) - { - this.collapse( true ); - nativeRange.setEnd( this.endContainer.$, this.endOffset ); + this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START ); + dummySpan.remove(); } else - throw( e ); - } + { + this.setEndBefore( endNode ); + endNode.remove(); + ieRange.select(); + } - var selection = this.document.getSelection().getNative(); - selection.removeAllRanges(); - selection.addRange( nativeRange ); - }; + this.document.fire( 'selectionchange' ); + } + : + function() + { + this.document.getSelection().selectRanges( [ this ] ); + }; } )();
[cell