\r
// #### checkSelectionChange : END\r
\r
+ function rangeRequiresFix( range )\r
+ {\r
+ function isInlineCt( node )\r
+ {\r
+ return node && node.type == CKEDITOR.NODE_ELEMENT\r
+ && node.getName() in CKEDITOR.dtd.$removeEmpty;\r
+ }\r
+\r
+ var start = range.startContainer,\r
+ offset = range.startOffset;\r
+\r
+ if ( start.type == CKEDITOR.NODE_TEXT )\r
+ return false;\r
+\r
+ // 1. Empty inline element. <span>^</span>\r
+ // 2. Adjoin to inline element. <p><strong>text</strong>^</p>\r
+ return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) );\r
+ }\r
+\r
var selectAllCmd =\r
{\r
modes : { wysiwyg : 1, source : 1 },\r
canUndo : false\r
};\r
\r
+ function createFillingChar( doc )\r
+ {\r
+ removeFillingChar( doc );\r
+\r
+ var fillingChar = doc.createText( '\u200B' );\r
+ doc.setCustomData( 'cke-fillingChar', fillingChar );\r
+\r
+ return fillingChar;\r
+ }\r
+\r
+ function getFillingChar( doc )\r
+ {\r
+ return doc && doc.getCustomData( 'cke-fillingChar' );\r
+ }\r
+\r
+ // Checks if a filling char has been used, eventualy removing it (#1272).\r
+ function checkFillingChar( doc )\r
+ {\r
+ var fillingChar = doc && getFillingChar( doc );\r
+ if ( fillingChar )\r
+ {\r
+ // Use this flag to avoid removing the filling char right after\r
+ // creating it.\r
+ if ( fillingChar.getCustomData( 'ready' ) )\r
+ removeFillingChar( doc );\r
+ else\r
+ fillingChar.setCustomData( 'ready', 1 );\r
+ }\r
+ }\r
+\r
+ function removeFillingChar( doc )\r
+ {\r
+ var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' );\r
+ if ( fillingChar )\r
+ {\r
+ // We can't simply remove the filling node because the user\r
+ // will actually enlarge it when typing, so we just remove the\r
+ // invisible char from it.\r
+ fillingChar.setText( fillingChar.getText().replace( /\u200B/g, '' ) );\r
+ fillingChar = 0;\r
+ }\r
+ }\r
+\r
CKEDITOR.plugins.add( 'selection',\r
{\r
init : function( editor )\r
{\r
+ // On WebKit only, we need a special "filling" char on some situations\r
+ // (#1272). Here we set the events that should invalidate that char.\r
+ if ( CKEDITOR.env.webkit )\r
+ {\r
+ editor.on( 'selectionChange', function() { checkFillingChar( editor.document ); } );\r
+ editor.on( 'beforeSetMode', function() { removeFillingChar( editor.document ); } );\r
+ editor.on( 'key', function( e )\r
+ {\r
+ // Remove the filling char before some keys get\r
+ // executed, so they'll not get blocked by it.\r
+ switch ( e.data.keyCode )\r
+ {\r
+ case 13 : // ENTER\r
+ case CKEDITOR.SHIFT + 13 : // SHIFT-ENTER\r
+ case 37 : // LEFT-ARROW\r
+ case 39 : // RIGHT-ARROW\r
+ case 8 : // BACKSPACE\r
+ removeFillingChar( editor.document );\r
+ }\r
+ }, null, null, 10 );\r
+\r
+ var fillingCharBefore,\r
+ resetSelection;\r
+\r
+ function beforeData()\r
+ {\r
+ var doc = editor.document,\r
+ fillingChar = getFillingChar( doc );\r
+\r
+ if ( fillingChar )\r
+ {\r
+ // If cursor is right blinking by side of the filler node, save it for restoring,\r
+ // as the following text substitution will blind it. (#7437)\r
+ var sel = doc.$.defaultView.getSelection();\r
+ if ( sel.type == 'Caret' && sel.anchorNode == fillingChar.$ )\r
+ resetSelection = 1;\r
+\r
+ fillingCharBefore = fillingChar.getText();\r
+ fillingChar.setText( fillingCharBefore.replace( /\u200B/g, '' ) );\r
+ }\r
+ }\r
+ function afterData()\r
+ {\r
+ var doc = editor.document,\r
+ fillingChar = getFillingChar( doc );\r
+\r
+ if ( fillingChar )\r
+ {\r
+ fillingChar.setText( fillingCharBefore );\r
+\r
+ if ( resetSelection )\r
+ {\r
+ doc.$.defaultView.getSelection().setPosition( fillingChar.$,fillingChar.getLength() );\r
+ resetSelection = 0;\r
+ }\r
+ }\r
+ }\r
+ editor.on( 'beforeUndoImage', beforeData );\r
+ editor.on( 'afterUndoImage', afterData );\r
+ editor.on( 'beforeGetData', beforeData, null, null, 0 );\r
+ editor.on( 'getData', afterData );\r
+ }\r
+\r
editor.on( 'contentDom', function()\r
{\r
var doc = editor.document,\r
// point.\r
if ( savedRange )\r
{\r
- // Range restored here might invalidate the DOM structure thus break up\r
- // the locked selection, give it up. (#6083)\r
- var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
- if ( restoreEnabled && !lockedSelection )\r
+ if ( restoreEnabled )\r
{\r
// Well not break because of this.\r
try\r
}\r
catch (e)\r
{}\r
+\r
+ // Update locked selection because of the normalized text nodes. (#6083, #6987)\r
+ var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
+ if ( lockedSelection )\r
+ {\r
+ lockedSelection.unlock();\r
+ lockedSelection.lock();\r
+ }\r
}\r
\r
savedRange = null;\r
range.collapse( start );\r
\r
// Gets the element that encloses the range entirely.\r
- var parent = range.parentElement();\r
+ var parent = range.parentElement(),\r
+ doc = parent.ownerDocument;\r
\r
// Empty parent element, e.g. <i>^</i>\r
if ( !parent.hasChildNodes() )\r
\r
var siblings = parent.children,\r
child,\r
+ sibling,\r
testRange = range.duplicate(),\r
startIndex = 0,\r
endIndex = siblings.length - 1,\r
else if ( position < 0 )\r
startIndex = index + 1;\r
else\r
- return { container : parent, offset : getNodeIndex( child ) };\r
+ {\r
+ // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs.\r
+ // e.g. <p>text<br />^<br /></p> (#7433)\r
+ if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' )\r
+ {\r
+ var bmId = 'cke_range_marker';\r
+ range.execCommand( 'CreateBookmark', false, bmId );\r
+ child = doc.getElementsByName( bmId )[ 0 ];\r
+ var offset = getNodeIndex( child );\r
+ parent.removeChild( child );\r
+ return { container : parent, offset : offset };\r
+ }\r
+ else\r
+ return { container : parent, offset : getNodeIndex( child ) };\r
+ }\r
}\r
\r
// All childs are text nodes,\r
// Start the measuring until distance overflows, meanwhile count the text nodes.\r
while ( distance > 0 )\r
{\r
- child = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
try\r
{\r
- distance -= child.nodeValue.length;\r
+ sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
+ distance -= sibling.nodeValue.length;\r
+ child = sibling;\r
}\r
// Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
catch( e )\r
return;\r
}\r
\r
- if ( CKEDITOR.env.ie )\r
- {\r
- this.getNative().empty();\r
+ range = new CKEDITOR.dom.range( element.getDocument() );\r
+ range.setStartBefore( element );\r
+ range.setEndAfter( element );\r
+ range.select();\r
\r
- try\r
- {\r
- // Try to select the node as a control.\r
- range = this.document.$.body.createControlRange();\r
- range.addElement( element.$ );\r
- range.select();\r
- }\r
- catch( e )\r
- {\r
- // If failed, select it as a text range.\r
- range = this.document.$.body.createTextRange();\r
- range.moveToElementText( element.$ );\r
- range.select();\r
- }\r
- finally\r
- {\r
- this.document.fire( 'selectionchange' );\r
- }\r
+ this.document.fire( 'selectionchange' );\r
+ this.reset();\r
\r
- this.reset();\r
- }\r
- else\r
- {\r
- // Create the range for the element.\r
- range = this.document.$.createRange();\r
- range.selectNode( element.$ );\r
-\r
- // Select the range.\r
- var sel = this.getNative();\r
- sel.removeAllRanges();\r
- sel.addRange( range );\r
-\r
- this.reset();\r
- }\r
},\r
\r
/**\r
{\r
var sel = this.getNative();\r
\r
+ // getNative() returns null if iframe is "display:none" in FF. (#6577)\r
+ if ( !sel )\r
+ return;\r
+\r
if ( ranges.length )\r
+ {\r
sel.removeAllRanges();\r
+ // Remove any existing filling char first.\r
+ CKEDITOR.env.webkit && removeFillingChar( this.document );\r
+ }\r
\r
for ( var i = 0 ; i < ranges.length ; i++ )\r
{\r
startContainer.appendText( '' );\r
}\r
\r
- nativeRange.setStart( startContainer.$, range.startOffset );\r
- nativeRange.setEnd( range.endContainer.$, range.endOffset );\r
+ if ( range.collapsed\r
+ && CKEDITOR.env.webkit\r
+ && rangeRequiresFix( range ) )\r
+ {\r
+ // Append a zero-width space so WebKit will not try to\r
+ // move the selection by itself (#1272).\r
+ var fillingChar = createFillingChar( this.document );\r
+ range.insertNode( fillingChar ) ;\r
+\r
+ var next = fillingChar.getNext();\r
+\r
+ // If the filling char is followed by a <br>, whithout\r
+ // having something before it, it'll not blink.\r
+ // Let's remove it in this case.\r
+ if ( next && !fillingChar.getPrevious() && next.type == CKEDITOR.NODE_ELEMENT && next.getName() == 'br' )\r
+ {\r
+ removeFillingChar( this.document );\r
+ range.moveToPosition( next, CKEDITOR.POSITION_BEFORE_START );\r
+ }\r
+ else\r
+ range.moveToPosition( fillingChar, CKEDITOR.POSITION_AFTER_END );\r
+ }\r
+\r
+ nativeRange.setStart( range.startContainer.$, range.startOffset );\r
+\r
+ try\r
+ {\r
+ nativeRange.setEnd( range.endContainer.$, range.endOffset );\r
+ }\r
+ catch ( e )\r
+ {\r
+ // There is a bug in Firefox implementation (it would be too easy\r
+ // otherwise). The new start can't be after the end (W3C says it can).\r
+ // So, let's create a new range and collapse it to the desired point.\r
+ if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )\r
+ {\r
+ range.collapse( 1 );\r
+ nativeRange.setEnd( range.endContainer.$, range.endOffset );\r
+ }\r
+ else\r
+ throw e;\r
+ }\r
\r
// Select the range.\r
sel.addRange( nativeRange );\r
// V2\r
function( forceExpand )\r
{\r
- var collapsed = this.collapsed;\r
- var isStartMarkerAlone;\r
- var dummySpan;\r
+ var collapsed = this.collapsed,\r
+ isStartMarkerAlone, dummySpan, ieRange;\r
+\r
+ // Try to make a object selection.\r
+ var selected = this.getEnclosedNode();\r
+ if ( selected )\r
+ {\r
+ try\r
+ {\r
+ ieRange = this.document.$.body.createControlRange();\r
+ ieRange.addElement( selected.$ );\r
+ ieRange.select();\r
+ return;\r
+ }\r
+ catch( er ) {}\r
+ }\r
\r
// IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.\r
// <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...\r
endNode = bookmark.endNode;\r
\r
// Create the main range which will be used for the selection.\r
- var ieRange = this.document.$.body.createTextRange();\r
+ ieRange = this.document.$.body.createTextRange();\r
\r
// Position the range at the start boundary.\r
ieRange.moveToElementText( startNode.$ );\r
:\r
function()\r
{\r
- var startContainer = this.startContainer;\r
-\r
- // If we have a collapsed range, inside an empty element, we must add\r
- // something to it, otherwise the caret will not be visible.\r
- if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )\r
- startContainer.append( new CKEDITOR.dom.text( '' ) );\r
-\r
- var nativeRange = this.document.$.createRange();\r
- nativeRange.setStart( startContainer.$, this.startOffset );\r
-\r
- try\r
- {\r
- nativeRange.setEnd( this.endContainer.$, this.endOffset );\r
- }\r
- catch ( e )\r
- {\r
- // There is a bug in Firefox implementation (it would be too easy\r
- // otherwise). The new start can't be after the end (W3C says it can).\r
- // So, let's create a new range and collapse it to the desired point.\r
- if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )\r
- {\r
- this.collapse( true );\r
- nativeRange.setEnd( this.endContainer.$, this.endOffset );\r
- }\r
- else\r
- throw( e );\r
- }\r
-\r
- var selection = this.document.getSelection().getNative();\r
- // getSelection() returns null in case when iframe is "display:none" in FF. (#6577)\r
- if ( selection )\r
- {\r
- selection.removeAllRanges();\r
- selection.addRange( nativeRange );\r
- }\r
+ this.document.getSelection().selectRanges( [ this ] );\r
};\r
} )();\r