X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Fselection%2Fplugin.js;h=d8888c19dae88b213abeec2e1aca5c14eecf7f17;hb=f0610347140239143439a511ee2bd48cb784f470;hp=3a4eb8071798121b9da958da1d06602752ed9f51;hpb=48b1db88210b4160dce439c6e3e32e14af8c106b;p=ckeditor.git diff --git a/_source/plugins/selection/plugin.js b/_source/plugins/selection/plugin.js index 3a4eb80..d8888c1 100644 --- a/_source/plugins/selection/plugin.js +++ b/_source/plugins/selection/plugin.js @@ -70,9 +70,37 @@ 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 ) @@ -99,10 +127,116 @@ For licensing, see LICENSE.html or http://ckeditor.com/license 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 ) + { + // 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, '' ) ); + fillingChar = 0; + } + } + 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 ); } ); + editor.on( 'key', function( e ) + { + // Remove the filling char before some keys get + // executed, so they'll not get blocked by it. + switch ( e.data.keyCode ) + { + case 13 : // ENTER + case CKEDITOR.SHIFT + 13 : // SHIFT-ENTER + case 37 : // LEFT-ARROW + case 39 : // RIGHT-ARROW + case 8 : // BACKSPACE + removeFillingChar( editor.document ); + } + }, null, null, 10 ); + + 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, @@ -134,10 +268,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // point. if ( savedRange ) { - // Range restored here might invalidate the DOM structure thus break up - // the locked selection, give it up. (#6083) - var lockedSelection = doc.getCustomData( 'cke_locked_selection' ); - if ( restoreEnabled && !lockedSelection ) + if ( restoreEnabled ) { // Well not break because of this. try @@ -146,6 +277,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } catch (e) {} + + // Update locked selection because of the normalized text nodes. (#6083, #6987) + var lockedSelection = doc.getCustomData( 'cke_locked_selection' ); + if ( lockedSelection ) + { + lockedSelection.unlock(); + lockedSelection.lock(); + } } savedRange = null; @@ -327,6 +466,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } }); + // Clear the cached range path before unload. (#7174) + editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor ); + editor.addCommand( 'selectAll', selectAllCmd ); editor.ui.addButton( 'SelectAll', { @@ -335,6 +477,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }); editor.selectionChange = checkSelectionChangeTimeout; + + // 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 ); } }); @@ -434,10 +583,10 @@ 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 = { @@ -568,7 +717,8 @@ 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 parent = range.parentElement(), + doc = parent.ownerDocument; // Empty parent element, e.g. ^ if ( !parent.hasChildNodes() ) @@ -576,6 +726,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var siblings = parent.children, child, + sibling, testRange = range.duplicate(), startIndex = 0, endIndex = siblings.length - 1, @@ -597,7 +748,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license else if ( position < 0 ) startIndex = index + 1; else - return { container : parent, offset : getNodeIndex( child ) }; + { + // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs. + // e.g.

text
^

(#7433) + if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' ) + { + var bmId = 'cke_range_marker'; + range.execCommand( 'CreateBookmark', false, bmId ); + child = doc.getElementsByName( bmId )[ 0 ]; + var offset = getNodeIndex( child ); + parent.removeChild( child ); + return { container : parent, offset : offset }; + } + else + return { container : parent, offset : getNodeIndex( child ) }; + } } // All childs are text nodes, @@ -653,10 +818,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Start the measuring until distance overflows, meanwhile count the text nodes. while ( distance > 0 ) { - child = child[ position > 0 ? 'previousSibling' : 'nextSibling' ]; try { - distance -= child.nodeValue.length; + sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ]; + distance -= sibling.nodeValue.length; + child = sibling; } // Measurement in IE could be somtimes wrong because of