X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fselection%2Fplugin.js;h=3a0a181d2954b6a2d96b8c64f326da8480b884fd;hb=6e682412d5cc0dfaedb376482e585bf2989c6863;hp=f0d2c4456ffa10d94a892dc55aea9f01df9d69ac;hpb=c9fdde67e6384bd5a66adc2b3bba5c4ce9db56c7;p=ckeditor.git diff --git a/_source/plugins/selection/plugin.js b/_source/plugins/selection/plugin.js index f0d2c44..3a0a181 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-2012, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -70,38 +70,202 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // #### checkSelectionChange : END + function rangeRequiresFix( range ) + { + function isInlineCt( node ) + { + return node && node.type == CKEDITOR.NODE_ELEMENT + && node.getName() in CKEDITOR.dtd.$removeEmpty; + } + + function singletonBlock( node ) + { + var body = range.document.getBody(); + return !node.is( 'body' ) && body.getChildCount() == 1; + } + + var start = range.startContainer, + offset = range.startOffset; + + if ( start.type == CKEDITOR.NODE_TEXT ) + return false; + + // 1. Empty inline element. ^ + // 2. Adjoin to inline element.

text^

+ // 3. The only empty block in document.

^

(#7222) + return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) || singletonBlock( start ) + : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) ); + } + 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, @@ -129,21 +293,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 ) { - if ( restoreEnabled ) - { - // Well not break because of this. - try - { - savedRange.select(); - } - catch (e) - {} - } - + // Well not break because of this. + try { savedRange.select(); } catch (e) {} savedRange = null; } }); @@ -151,7 +316,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(); }); @@ -164,23 +329,29 @@ 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() { - editor.on( 'blur', function( evt ) - { - editor.document && 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 () + html.on( 'mousedown', function() { // Lock restore selection now, as we have // a followed 'click' event which introduce @@ -188,25 +359,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license restoreEnabled = 0; }); - html.on( 'mouseup', function () + html.on( 'mouseup', function() { restoreEnabled = 1; }); - // In IE6/7 the blinking cursor appears, but contents are - // not editable. (#5634) - if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) ) - { - // The 'click' event is not fired when clicking the - // scrollbars, so we can use it to check whether - // the empty space following has been clicked. - html.on( 'click', function( evt ) - { - if ( evt.data.getTarget().getName() == 'html' ) - editor.getSelection().getRanges()[ 0 ].select(); - }); - } - var scroll; // IE fires the "selectionchange" event when clicking // inside a selection. We don't want to capture that. @@ -235,7 +392,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } scroll = null; - saveEnabled = true; + saveEnabled = 1; setTimeout( function() { saveSelection( true ); @@ -247,10 +404,91 @@ 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 focus the editor when + // clicking at the region below body (on html element) content, we emulate + // the normal behavior on old IEs. (#1659, #7932) + if ( ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat ) + && doc.$.compatMode != 'BackCompat' ) + { + 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( + textRng.compareEndPoints( 'StartToStart', rngEnd ) < 0 ? + 'EndToEnd' : + 'StartToStart', + rngEnd ); + + // Update selection with new range. + textRng.select(); + } + } + + evt = evt.data.$; + + // We're sure that the click happens at the region + // below body, but not on scrollbar. + if ( evt.y < html.$.clientHeight + && evt.y > body.$.offsetTop + body.$.clientHeight + && evt.x < html.$.clientWidth ) + { + // Start to build the text range. + var textRng = body.$.createTextRange(); + moveRangeToPoint( textRng, evt.x, evt.y ); + + html.on( 'mousemove', onHover ); + + html.on( 'mouseup', function( evt ) + { + html.removeListener( 'mousemove', onHover ); + evt.removeListener(); + + // Make it in effect on mouse up. (#9022) + textRng.select(); + } ); + } + }); + } + + // It's much simpler for IE8, we just need to reselect the reported range. + if ( CKEDITOR.env.ie8 ) + { + html.on( 'mouseup', function( evt ) + { + // 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. + if ( evt.data.getTarget().getName() == 'html' ) + { + 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. @@ -258,7 +496,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license function disableSave() { - saveEnabled = false; + saveEnabled = 0; } function saveSelection( testIt ) @@ -315,9 +553,38 @@ 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 ) + { + 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, 10 ); } }); + // Clear the cached range path before unload. (#7174) + editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor ); + editor.addCommand( 'selectAll', selectAllCmd ); editor.ui.addButton( 'SelectAll', { @@ -325,16 +592,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() @@ -351,7 +634,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() @@ -370,11 +653,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; @@ -390,7 +673,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 ) { @@ -400,7 +685,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return lockedSelection; this.document = document; - this.isLocked = false; + this.isLocked = 0; this._ = { cache : {} @@ -408,14 +693,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; } @@ -425,19 +718,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 ? @@ -455,19 +748,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 ? @@ -535,19 +828,23 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** - * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection. - * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns - * one range for each table cell when one or more table row is selected. - * @return {Array} + * 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); + * var ranges = selection.getRanges(); + * alert( ranges.length ); */ - getRanges : (function () + 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 ) @@ -557,77 +854,131 @@ 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 }; } }; @@ -655,6 +1006,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license boundaryInfo = getBoundaryInformation( nativeRange ); range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset ); + // 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 ) @@ -740,24 +1098,36 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Drop range spans inside one ready-only node. var parent = range.getCommonAncestor(); - if ( parent.isReadOnly()) + 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(); - // Range may start inside a non-editable element, restart range - // by the end of it. - var readOnly; - if ( ( readOnly = startContainer.isReadOnly() ) ) - range.setStartAfter( readOnly ); - // Enlarge range start/end with text node to avoid walker // being DOM destructive, it doesn't interfere our checking // of elements below as well. @@ -782,7 +1152,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license walker.evaluator = function( node ) { if ( node.type == CKEDITOR.NODE_ELEMENT - && node.getAttribute( 'contenteditable' ) == 'false' ) + && node.isReadOnly() ) { var newRange = range.clone(); range.setEndBefore( node ); @@ -820,7 +1190,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license * @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() @@ -850,7 +1220,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; @@ -870,32 +1240,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.$; } } @@ -903,12 +1266,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() @@ -925,6 +1288,74 @@ 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() @@ -949,22 +1380,60 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 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, @@ -979,11 +1448,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 @@ -993,19 +1460,27 @@ 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 = {}; }, /** - * Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element. - * @param element + * 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 ) { @@ -1023,50 +1498,23 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 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.$ ); + this.document.fire( 'selectionchange' ); + this.reset(); - // Select the range. - var sel = this.getNative(); - sel.removeAllRanges(); - sel.addRange( range ); - - this.reset(); - } }, /** - * Adding the specified ranges to document selection preceding - * by clearing up the original selection. - * @param {CKEDITOR.dom.range} ranges + * 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 ) { @@ -1099,8 +1547,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { var sel = this.getNative(); + // 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++ ) { @@ -1118,7 +1574,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license if ( !between.collapsed ) { between.shrink( CKEDITOR.NODE_ELEMENT, true ); - if ( between.getCommonAncestor().isReadOnly()) + 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 ); @@ -1144,22 +1606,67 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 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(); } }, /** - * Create bookmark for every single of this selection range (from #getRanges) - * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method, - * with extra cares to avoid interferon among those ranges. Same arguments are - * received as with the underlay range method. + * 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 ) { @@ -1167,10 +1674,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** - * Create bookmark for every single of this selection range (from #getRanges) - * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method, - * with extra cares to avoid interferon among those ranges. Same arguments are - * received as with the underlay range method. + * 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 ) { @@ -1178,8 +1688,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** - * Select the virtual ranges denote by the bookmarks by calling #selectRanges. - * @param bookmarks + * 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 ) { @@ -1195,7 +1709,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** - * Retrieve the common ancestor node of the first range and the last range. + * 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() { @@ -1206,7 +1723,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** - * 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() { @@ -1229,9 +1748,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // V2 function( forceExpand ) { - var collapsed = this.collapsed; - var isStartMarkerAlone; - var dummySpan; + var collapsed = this.collapsed, + isStartMarkerAlone, dummySpan, ieRange; + + // 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 ) {} + } // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g. // [... =>
cell
... @@ -1251,7 +1783,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license endNode = bookmark.endNode; // Create the main range which will be used for the selection. - var ieRange = this.document.$.body.createTextRange(); + ieRange = this.document.$.body.createTextRange(); // Position the range at the start boundary. ieRange.moveToElementText( startNode.$ ); @@ -1332,36 +1864,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license : function() { - var startContainer = this.startContainer; - - // 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( '' ) ); - - var nativeRange = this.document.$.createRange(); - nativeRange.setStart( startContainer.$, this.startOffset ); - - 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 ); - } - else - throw( e ); - } - - var selection = this.document.getSelection().getNative(); - selection.removeAllRanges(); - selection.addRange( nativeRange ); + this.document.getSelection().selectRanges( [ this ] ); }; } )();
[cell