/*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
For licensing, see LICENSE.html or http://ckeditor.com/license\r
*/\r
\r
// In IE, the "selectionchange" event may still get thrown when\r
// releasing the WYSIWYG mode, so we need to check it first.\r
var sel = this.getSelection();\r
- if ( !sel )\r
+ if ( !sel || !sel.document.getWindow().$ )\r
return;\r
\r
var firstElement = sel.getStartElement();\r
\r
// #### checkSelectionChange : END\r
\r
+ function rangeRequiresFix( range )\r
+ {\r
+ function isTextCt( node, isAtEnd )\r
+ {\r
+ if ( !node || node.type == CKEDITOR.NODE_TEXT )\r
+ return false;\r
+\r
+ var testRng = range.clone();\r
+ return testRng[ 'moveToElementEdit' + ( isAtEnd ? 'End' : 'Start' ) ]( node );\r
+ }\r
+\r
+ var ct = range.startContainer;\r
+\r
+ var previous = range.getPreviousNode( isVisible, null, ct ),\r
+ next = range.getNextNode( isVisible, null, ct );\r
+\r
+ // Any adjacent text container may absorb the cursor, e.g.\r
+ // <p><strong>text</strong>^foo</p>\r
+ // <p>foo^<strong>text</strong></p>\r
+ // <div>^<p>foo</p></div>\r
+ if ( isTextCt( previous ) || isTextCt( next, 1 ) )\r
+ return true;\r
+\r
+ // Empty block/inline element is also affected. <span>^</span>, <p>^</p> (#7222)\r
+ if ( !( previous || next ) && !( ct.type == CKEDITOR.NODE_ELEMENT && ct.isBlockBoundary() && ct.getBogus() ) )\r
+ return true;\r
+\r
+ return false;\r
+ }\r
+\r
var selectAllCmd =\r
{\r
modes : { wysiwyg : 1, source : 1 },\r
+ readOnly : CKEDITOR.env.ie || CKEDITOR.env.webkit,\r
exec : function( editor )\r
{\r
switch ( editor.mode )\r
{\r
case 'wysiwyg' :\r
editor.document.$.execCommand( 'SelectAll', false, null );\r
+ // Force triggering selectionChange (#7008)\r
+ editor.forceNextSelectionCheck();\r
+ editor.selectionChange();\r
break;\r
case 'source' :\r
// Select the contents of the textarea\r
- var textarea = editor.textarea.$ ;\r
+ var textarea = editor.textarea.$;\r
if ( CKEDITOR.env.ie )\r
- {\r
- textarea.createTextRange().execCommand( 'SelectAll' ) ;\r
- }\r
+ textarea.createTextRange().execCommand( 'SelectAll' );\r
else\r
{\r
- textarea.selectionStart = 0 ;\r
- textarea.selectionEnd = textarea.value.length ;\r
+ textarea.selectionStart = 0;\r
+ textarea.selectionEnd = textarea.value.length;\r
}\r
- textarea.focus() ;\r
+ textarea.focus();\r
}\r
},\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
+ var bm,\r
+ sel = doc.getSelection().getNative(),\r
+ // Be error proof.\r
+ range = sel && sel.type != 'None' && sel.getRangeAt( 0 );\r
+\r
+ // Text selection position might get mangled by\r
+ // subsequent dom modification, save it now for restoring. (#8617)\r
+ if ( fillingChar.getLength() > 1\r
+ && range && range.intersectsNode( fillingChar.$ ) )\r
+ {\r
+ bm = [ sel.anchorOffset, sel.focusOffset ];\r
+\r
+ // Anticipate the offset change brought by the removed char.\r
+ var startAffected = sel.anchorNode == fillingChar.$ && sel.anchorOffset > 0,\r
+ endAffected = sel.focusNode == fillingChar.$ && sel.focusOffset > 0;\r
+ startAffected && bm[ 0 ]--;\r
+ endAffected && bm[ 1 ]--;\r
+\r
+ // Revert the bookmark order on reverse selection.\r
+ isReversedSelection( sel ) && bm.unshift( bm.pop() );\r
+ }\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
+\r
+ // Restore the bookmark.\r
+ if ( bm )\r
+ {\r
+ var rng = sel.getRangeAt( 0 );\r
+ rng.setStart( rng.startContainer, bm[ 0 ] );\r
+ rng.setEnd( rng.startContainer, bm[ 1 ] );\r
+ sel.removeAllRanges();\r
+ sel.addRange( rng );\r
+ }\r
+ }\r
+ }\r
+\r
+ function isReversedSelection( sel )\r
+ {\r
+ if ( !sel.isCollapsed )\r
+ {\r
+ var range = sel.getRangeAt( 0 );\r
+ // Potentially alter an reversed selection range.\r
+ range.setStart( sel.anchorNode, sel.anchorOffset );\r
+ range.setEnd( sel.focusNode, sel.focusOffset );\r
+ return range.collapsed;\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
+\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
- body = doc.getBody();\r
+ outerDoc = CKEDITOR.document,\r
+ body = doc.getBody(),\r
+ html = doc.getDocumentElement();\r
\r
if ( CKEDITOR.env.ie )\r
{\r
// than firing the selection change event.\r
\r
var savedRange,\r
- saveEnabled;\r
+ saveEnabled,\r
+ restoreEnabled = 1;\r
\r
// "onfocusin" is fired before "onfocus". It makes it\r
// possible to restore the selection before click\r
if ( evt.data.$.srcElement.nodeName != 'BODY' )\r
return;\r
\r
- // If we have saved a range, restore it at this\r
+ // Give the priority to locked selection since it probably\r
+ // reflects the actual situation, besides locked selection\r
+ // could be interfered because of text nodes normalizing.\r
+ // (#6083, #6987)\r
+ var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
+ if ( lockedSelection )\r
+ {\r
+ lockedSelection.unlock( 1 );\r
+ lockedSelection.lock();\r
+ }\r
+ // Then check ff we have saved a range, restore it at this\r
// point.\r
- if ( savedRange )\r
+ else if ( savedRange && restoreEnabled )\r
{\r
// Well not break because of this.\r
- try\r
- {\r
- savedRange.select();\r
- }\r
- catch (e)\r
- {}\r
-\r
+ try { savedRange.select(); } catch (e) {}\r
savedRange = null;\r
}\r
});\r
body.on( 'focus', function()\r
{\r
// Enable selections to be saved.\r
- saveEnabled = true;\r
+ saveEnabled = 1;\r
\r
saveSelection();\r
});\r
return;\r
\r
// Disable selections from being saved.\r
- saveEnabled = false;\r
+ saveEnabled = 0;\r
+ restoreEnabled = 1;\r
});\r
\r
- // IE before version 8 will leave cursor blinking inside the document after\r
- // editor blurred unless we clean up the selection. (#4716)\r
- if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
+ // [IE] Iframe will still keep the selection when blurred, if\r
+ // focus is moved onto a non-editing host, e.g. link or button, but\r
+ // it becomes a problem for the object type selection, since the resizer\r
+ // handler attached on it will mark other part of the UI, especially\r
+ // for the dialog. (#8157)\r
+ // [IE<8] Even worse For old IEs, the cursor will not vanish even if\r
+ // the selection has been moved to another text input in some cases. (#4716)\r
+ //\r
+ // Now the range restore is disabled, so we simply force IE to clean\r
+ // up the selection before blur.\r
+ CKEDITOR.env.ie && editor.on( 'blur', function()\r
{\r
- doc.getWindow().on( 'blur', function( evt )\r
- {\r
- editor.document.$.selection.empty();\r
- });\r
- }\r
+ // Error proof when the editor is not visible. (#6375)\r
+ try{ doc.$.selection.empty(); } catch ( er){}\r
+ });\r
\r
+ // Listening on document element ensures that\r
+ // scrollbar is included. (#5280)\r
+ html.on( 'mousedown', function()\r
+ {\r
+ // Lock restore selection now, as we have\r
+ // a followed 'click' event which introduce\r
+ // new selection. (#5735)\r
+ restoreEnabled = 0;\r
+ });\r
+\r
+ html.on( 'mouseup', function()\r
+ {\r
+ restoreEnabled = 1;\r
+ });\r
+\r
+ var scroll;\r
// IE fires the "selectionchange" event when clicking\r
// inside a selection. We don't want to capture that.\r
- body.on( 'mousedown', disableSave );\r
+ body.on( 'mousedown', function( evt )\r
+ {\r
+ // IE scrolls document to top on right mousedown\r
+ // when editor has no focus, remember this scroll\r
+ // position and revert it before context menu opens. (#5778)\r
+ if ( evt.data.$.button == 2 )\r
+ {\r
+ var sel = editor.document.$.selection;\r
+ if ( sel.type == 'None' )\r
+ scroll = editor.window.getScrollPosition();\r
+ }\r
+ disableSave();\r
+ });\r
+\r
body.on( 'mouseup',\r
- function()\r
+ function( evt )\r
{\r
- saveEnabled = true;\r
+ // Restore recorded scroll position when needed on right mouseup.\r
+ if ( evt.data.$.button == 2 && scroll )\r
+ {\r
+ editor.document.$.documentElement.scrollLeft = scroll.x;\r
+ editor.document.$.documentElement.scrollTop = scroll.y;\r
+ }\r
+ scroll = null;\r
+\r
+ saveEnabled = 1;\r
setTimeout( function()\r
{\r
saveSelection( true );\r
body.on( 'keyup',\r
function()\r
{\r
- saveEnabled = true;\r
+ saveEnabled = 1;\r
saveSelection();\r
});\r
\r
+ // When content doc is in standards mode, IE doesn't produce text selection\r
+ // when click on the region outside of body, we emulate\r
+ // the correct behavior here. (#1659, #7932, # 9097)\r
+ if ( doc.$.compatMode != 'BackCompat' )\r
+ {\r
+ if ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat )\r
+ {\r
+ function moveRangeToPoint( range, x, y )\r
+ {\r
+ // Error prune in IE7. (#9034, #9110)\r
+ try { range.moveToPoint( x, y ); } catch ( e ) {}\r
+ }\r
+\r
+ html.on( 'mousedown', function( evt )\r
+ {\r
+ // Expand the text range along with mouse move.\r
+ function onHover( evt )\r
+ {\r
+ evt = evt.data.$;\r
+ if ( textRng )\r
+ {\r
+ // Read the current cursor.\r
+ var rngEnd = body.$.createTextRange();\r
+\r
+ moveRangeToPoint( rngEnd, evt.x, evt.y );\r
+\r
+ // Handle drag directions.\r
+ textRng.setEndPoint(\r
+ startRng.compareEndPoints( 'StartToStart', rngEnd ) < 0 ?\r
+ 'EndToEnd' :\r
+ 'StartToStart',\r
+ rngEnd );\r
+\r
+ // Update selection with new range.\r
+ textRng.select();\r
+ }\r
+ }\r
+\r
+ function removeListeners()\r
+ {\r
+ outerDoc.removeListener( 'mouseup', onSelectEnd );\r
+ html.removeListener( 'mouseup', onSelectEnd );\r
+ }\r
+\r
+ function onSelectEnd()\r
+ {\r
+\r
+ html.removeListener( 'mousemove', onHover );\r
+ removeListeners();\r
+\r
+ // Make it in effect on mouse up. (#9022)\r
+ textRng.select();\r
+ }\r
+\r
+ evt = evt.data;\r
+\r
+ // We're sure that the click happens at the region\r
+ // outside body, but not on scrollbar.\r
+ if ( evt.getTarget().is( 'html' ) &&\r
+ evt.$.x < html.$.clientWidth &&\r
+ evt.$.y < html.$.clientHeight )\r
+ {\r
+ // Start to build the text range.\r
+ var textRng = body.$.createTextRange();\r
+ moveRangeToPoint( textRng, evt.$.x, evt.$.y );\r
+ // Records the dragging start of the above text range.\r
+ var startRng = textRng.duplicate();\r
+\r
+ html.on( 'mousemove', onHover );\r
+ outerDoc.on( 'mouseup', onSelectEnd );\r
+ html.on( 'mouseup', onSelectEnd );\r
+ }\r
+ });\r
+ }\r
+\r
+ // It's much simpler for IE > 8, we just need to reselect the reported range.\r
+ if ( CKEDITOR.env.ie8 )\r
+ {\r
+ html.on( 'mousedown', function( evt )\r
+ {\r
+ if ( evt.data.getTarget().is( 'html' ) )\r
+ {\r
+ // Limit the text selection mouse move inside of editable. (#9715)\r
+ outerDoc.on( 'mouseup', onSelectEnd );\r
+ html.on( 'mouseup', onSelectEnd );\r
+ }\r
+\r
+ });\r
+\r
+ function removeListeners()\r
+ {\r
+ outerDoc.removeListener( 'mouseup', onSelectEnd );\r
+ html.removeListener( 'mouseup', onSelectEnd );\r
+ }\r
\r
+ function onSelectEnd()\r
+ {\r
+ removeListeners();\r
+\r
+ // The event is not fired when clicking on the scrollbars,\r
+ // so we can safely check the following to understand\r
+ // whether the empty space following <body> has been clicked.\r
+ var sel = CKEDITOR.document.$.selection,\r
+ range = sel.createRange();\r
+ // The selection range is reported on host, but actually it should applies to the content doc.\r
+ if ( sel.type != 'None' && range.parentElement().ownerDocument == doc.$ )\r
+ range.select();\r
+ }\r
+ }\r
+\r
+ }\r
// IE is the only to provide the "selectionchange"\r
// event.\r
doc.on( 'selectionchange', saveSelection );\r
\r
function disableSave()\r
{\r
- saveEnabled = false;\r
+ saveEnabled = 0;\r
}\r
\r
function saveSelection( testIt )\r
\r
// Avoid saving selection from within text input. (#5747)\r
var parentTag;\r
- if ( nativeSel && nativeSel.type == 'Text'\r
- && ( parentTag = nativeSel.createRange().parentElement().nodeName.toLowerCase() )\r
- && parentTag in { input: 1, textarea : 1 } )\r
+ if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'\r
+ && ( parentTag = nativeSel.createRange() )\r
+ && ( parentTag = parentTag.parentElement() )\r
+ && ( parentTag = parentTag.nodeName )\r
+ && parentTag.toLowerCase() in { input: 1, textarea : 1 } )\r
{\r
return;\r
}\r
\r
- savedRange = nativeSel && sel.getRanges()[ 0 ];\r
+ // Not break because of this. (#9132)\r
+ try{ savedRange = nativeSel && sel.getRanges()[ 0 ]; } catch( er ) {}\r
\r
checkSelectionChangeTimeout.call( editor );\r
}\r
\r
doc.on( 'mouseup', checkSelectionChangeTimeout, editor );\r
doc.on( 'keyup', checkSelectionChangeTimeout, editor );\r
+ doc.on( 'selectionchange', checkSelectionChangeTimeout, editor );\r
+ }\r
+\r
+ if ( CKEDITOR.env.webkit )\r
+ {\r
+ // Before keystroke is handled by editor, check to remove the filling char.\r
+ doc.on( 'keydown', function( evt )\r
+ {\r
+ var key = evt.data.getKey();\r
+ // Remove the filling char before some keys get\r
+ // executed, so they'll not get blocked by it.\r
+ switch ( key )\r
+ {\r
+ case 13 : // ENTER\r
+ case 33 : // PAGEUP\r
+ case 34 : // PAGEDOWN\r
+ case 35 : // HOME\r
+ case 36 : // END\r
+ case 37 : // LEFT-ARROW\r
+ case 39 : // RIGHT-ARROW\r
+ case 8 : // BACKSPACE\r
+ case 45 : // INS\r
+ case 46 : // DEl\r
+ removeFillingChar( editor.document );\r
+ }\r
+\r
+ }, null, null, -1 );\r
}\r
});\r
\r
+ // Clear the cached range path before unload. (#7174)\r
+ editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );\r
+\r
editor.addCommand( 'selectAll', selectAllCmd );\r
editor.ui.addButton( 'SelectAll',\r
{\r
command : 'selectAll'\r
});\r
\r
- editor.selectionChange = checkSelectionChangeTimeout;\r
+ /**\r
+ * Check if to fire the {@link CKEDITOR.editor#selectionChange} event\r
+ * for the current editor instance.\r
+ *\r
+ * @param {Boolean} checkNow Check immediately without any delay.\r
+ */\r
+ editor.selectionChange = function( checkNow )\r
+ {\r
+ ( checkNow ? checkSelectionChange : checkSelectionChangeTimeout ).call( this );\r
+ };\r
+\r
+ // IE9 might cease to work if there's an object selection inside the iframe (#7639).\r
+ CKEDITOR.env.ie9Compat && editor.on( 'destroy', function()\r
+ {\r
+ var sel = editor.getSelection();\r
+ sel && sel.getNative().clear();\r
+ }, null, null, 9 );\r
}\r
});\r
\r
/**\r
* Gets the current selection from the editing area when in WYSIWYG mode.\r
- * @returns {CKEDITOR.dom.selection} A selection object or null if not on\r
+ * @returns {CKEDITOR.dom.selection} A selection object or null if not in\r
* WYSIWYG mode or no selection is available.\r
* @example\r
- * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;\r
+ * var selection = CKEDITOR.instances.editor1.<strong>getSelection()</strong>;\r
* alert( selection.getType() );\r
*/\r
CKEDITOR.editor.prototype.getSelection = function()\r
* Gets the current selection from the document.\r
* @returns {CKEDITOR.dom.selection} A selection object.\r
* @example\r
- * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;\r
+ * var selection = CKEDITOR.instances.editor1.document.<strong>getSelection()</strong>;\r
* alert( selection.getType() );\r
*/\r
CKEDITOR.dom.document.prototype.getSelection = function()\r
CKEDITOR.SELECTION_NONE = 1;\r
\r
/**\r
- * Text or collapsed selection.\r
+ * A text or a collapsed selection.\r
* @constant\r
* @example\r
* if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )\r
- * alert( 'Text is selected' );\r
+ * alert( 'A text is selected' );\r
*/\r
CKEDITOR.SELECTION_TEXT = 2;\r
\r
/**\r
* Manipulates the selection in a DOM document.\r
* @constructor\r
+ * @param {CKEDITOR.dom.document} document The DOM document that contains the selection.\r
* @example\r
+ * var sel = new <strong>CKEDITOR.dom.selection( CKEDITOR.document )</strong>;\r
*/\r
CKEDITOR.dom.selection = function( document )\r
{\r
return lockedSelection;\r
\r
this.document = document;\r
- this.isLocked = false;\r
+ this.isLocked = 0;\r
this._ =\r
{\r
cache : {}\r
\r
/**\r
* IE BUG: The selection's document may be a different document than the\r
- * editor document. Return null if that's the case.\r
+ * editor document. Return null if that is the case.\r
*/\r
if ( CKEDITOR.env.ie )\r
{\r
- var range = this.getNative().createRange();\r
- if ( !range\r
- || ( range.item && range.item(0).ownerDocument != this.document.$ )\r
- || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )\r
+ // Avoid breaking because of it. (#8836)\r
+ try\r
+ {\r
+ var range = this.getNative().createRange();\r
+ if ( !range ||\r
+ ( range.item && range.item( 0 ).ownerDocument != this.document.$ ) ||\r
+ ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )\r
+ {\r
+ throw 0;\r
+ }\r
+ }\r
+ catch ( e )\r
{\r
this.isInvalid = true;\r
}\r
};\r
\r
var styleObjectElements =\r
- {\r
- img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,\r
- a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1\r
- };\r
+ {\r
+ img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,\r
+ a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1\r
+ };\r
\r
CKEDITOR.dom.selection.prototype =\r
{\r
/**\r
* Gets the native selection object from the browser.\r
* @function\r
- * @returns {Object} The native selection object.\r
+ * @returns {Object} The native browser selection object.\r
* @example\r
- * var selection = editor.getSelection().<b>getNative()</b>;\r
+ * var selection = editor.getSelection().<strong>getNative()</strong>;\r
*/\r
getNative :\r
CKEDITOR.env.ie ?\r
* Gets the type of the current selection. The following values are\r
* available:\r
* <ul>\r
- * <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>\r
- * <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or\r
- * collapsed selection.</li>\r
- * <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element\r
- * selection.</li>\r
+ * <li><code>{@link CKEDITOR.SELECTION_NONE}</code> (1): No selection.</li>\r
+ * <li><code>{@link CKEDITOR.SELECTION_TEXT}</code> (2): A text or a collapsed\r
+ * selection is selected.</li>\r
+ * <li><code>{@link CKEDITOR.SELECTION_ELEMENT}</code> (3): An element is\r
+ * selected.</li>\r
* </ul>\r
* @function\r
* @returns {Number} One of the following constant values:\r
- * {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or\r
- * {@link CKEDITOR.SELECTION_ELEMENT}.\r
+ * <code>{@link CKEDITOR.SELECTION_NONE}</code>, <code>{@link CKEDITOR.SELECTION_TEXT}</code>, or\r
+ * <code>{@link CKEDITOR.SELECTION_ELEMENT}</code>.\r
* @example\r
- * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )\r
- * alert( 'Text is selected' );\r
+ * if ( editor.getSelection().<strong>getType()</strong> == CKEDITOR.SELECTION_TEXT )\r
+ * alert( 'A text is selected' );\r
*/\r
getType :\r
CKEDITOR.env.ie ?\r
return ( cache.type = type );\r
},\r
\r
- getRanges :\r
- CKEDITOR.env.ie ?\r
+ /**\r
+ * Retrieves the <code>{@link CKEDITOR.dom.range}</code> instances that represent the current selection.\r
+ * Note: Some browsers return multiple ranges even for a continuous selection. Firefox, for example, returns\r
+ * one range for each table cell when one or more table rows are selected.\r
+ * @function\r
+ * @param {Boolean} [onlyEditables] If set to <code>true</code>, this function retrives editable ranges only.\r
+ * @return {Array} Range instances that represent the current selection.\r
+ * @example\r
+ * var ranges = selection.<strong>getRanges()</strong>;\r
+ * alert( ranges.length );\r
+ */\r
+ getRanges : (function()\r
+ {\r
+ var func = CKEDITOR.env.ie ?\r
( function()\r
{\r
+ function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }\r
+\r
// Finds the container and offset for a specific boundary\r
// of an IE range.\r
var getBoundaryInformation = function( range, start )\r
range.collapse( start );\r
\r
// Gets the element that encloses the range entirely.\r
- var parent = range.parentElement();\r
- var siblings = parent.childNodes;\r
-\r
- var testRange;\r
-\r
- for ( var i = 0 ; i < siblings.length ; i++ )\r
+ var parent = range.parentElement(),\r
+ doc = parent.ownerDocument;\r
+\r
+ // Empty parent element, e.g. <i>^</i>\r
+ if ( !parent.hasChildNodes() )\r
+ return { container : parent, offset : 0 };\r
+\r
+ var siblings = parent.children,\r
+ child,\r
+ sibling,\r
+ testRange = range.duplicate(),\r
+ startIndex = 0,\r
+ endIndex = siblings.length - 1,\r
+ index = -1,\r
+ position,\r
+ distance,\r
+ container;\r
+\r
+ // Binary search over all element childs to test the range to see whether\r
+ // range is right on the boundary of one element.\r
+ while ( startIndex <= endIndex )\r
{\r
- var child = siblings[ i ];\r
- if ( child.nodeType == 1 )\r
+ index = Math.floor( ( startIndex + endIndex ) / 2 );\r
+ child = siblings[ index ];\r
+ testRange.moveToElementText( child );\r
+ position = testRange.compareEndPoints( 'StartToStart', range );\r
+\r
+ if ( position > 0 )\r
+ endIndex = index - 1;\r
+ else if ( position < 0 )\r
+ startIndex = index + 1;\r
+ else\r
{\r
- testRange = range.duplicate();\r
-\r
- testRange.moveToElementText( child );\r
-\r
- var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),\r
- comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );\r
-\r
- testRange.collapse();\r
-\r
- if ( comparisonStart > 0 )\r
- break;\r
- // When selection stay at the side of certain self-closing elements, e.g. BR,\r
- // our comparison will never shows an equality. (#4824)\r
- else if ( !comparisonStart\r
- || comparisonEnd == 1 && comparisonStart == -1 )\r
- return { container : parent, offset : i };\r
- else if ( !comparisonEnd )\r
- return { container : parent, offset : i + 1 };\r
-\r
- testRange = null;\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
+ // "Fall back" to w3c selection.\r
+ var sel = doc.defaultView.getSelection();\r
+ return { container : sel[ start ? 'anchorNode' : 'focusNode' ],\r
+ offset : sel[ start ? 'anchorOffset' : 'focusOffset' ] };\r
+ }\r
+ else\r
+ return { container : parent, offset : getNodeIndex( child ) };\r
}\r
}\r
\r
- if ( !testRange )\r
+ // All childs are text nodes,\r
+ // or to the right hand of test range are all text nodes. (#6992)\r
+ if ( index == -1 || index == siblings.length - 1 && position < 0 )\r
{\r
- testRange = range.duplicate();\r
+ // Adapt test range to embrace the entire parent contents.\r
testRange.moveToElementText( parent );\r
- testRange.collapse( false );\r
- }\r
+ testRange.setEndPoint( 'StartToStart', range );\r
\r
- testRange.setEndPoint( 'StartToStart', range );\r
- // IE report line break as CRLF with range.text but\r
- // only LF with textnode.nodeValue, normalize them to avoid\r
- // breaking character counting logic below. (#3949)\r
- var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
+ // IE report line break as CRLF with range.text but\r
+ // only LF with textnode.nodeValue, normalize them to avoid\r
+ // breaking character counting logic below. (#3949)\r
+ distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
\r
- try\r
- {\r
- while ( distance > 0 )\r
- distance -= siblings[ --i ].nodeValue.length;\r
- }\r
- // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
- catch( e )\r
- {\r
- distance = 0;\r
- }\r
+ siblings = parent.childNodes;\r
\r
+ // Actual range anchor right beside test range at the boundary of text node.\r
+ if ( !distance )\r
+ {\r
+ child = siblings[ siblings.length - 1 ];\r
\r
- if ( distance === 0 )\r
- {\r
- return {\r
- container : parent,\r
- offset : i\r
- };\r
+ if ( child.nodeType != CKEDITOR.NODE_TEXT )\r
+ return { container : parent, offset : siblings.length };\r
+ else\r
+ return { container : child, offset : child.nodeValue.length };\r
+ }\r
+\r
+ // Start the measuring until distance overflows, meanwhile count the text nodes.\r
+ var i = siblings.length;\r
+ while ( distance > 0 && i > 0 )\r
+ {\r
+ sibling = siblings[ --i ];\r
+ if ( sibling.nodeType == CKEDITOR.NODE_TEXT )\r
+ {\r
+ container = sibling;\r
+ distance -= sibling.nodeValue.length;\r
+ }\r
+ }\r
+\r
+ return { container : container, offset : -distance };\r
}\r
+ // Test range was one offset beyond OR behind the anchored text node.\r
else\r
{\r
- return {\r
- container : siblings[ i ],\r
- offset : -distance\r
- };\r
+ // Adapt one side of test range to the actual range\r
+ // for measuring the offset between them.\r
+ testRange.collapse( position > 0 ? true : false );\r
+ testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );\r
+\r
+ // IE report line break as CRLF with range.text but\r
+ // only LF with textnode.nodeValue, normalize them to avoid\r
+ // breaking character counting logic below. (#3949)\r
+ distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
+\r
+ // Actual range anchor right beside test range at the inner boundary of text node.\r
+ if ( !distance )\r
+ return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };\r
+\r
+ // Start the measuring until distance overflows, meanwhile count the text nodes.\r
+ while ( distance > 0 )\r
+ {\r
+ try\r
+ {\r
+ sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
+ if ( sibling.nodeType == CKEDITOR.NODE_TEXT )\r
+ {\r
+ distance -= sibling.nodeValue.length;\r
+ container = sibling;\r
+ }\r
+ child = sibling;\r
+ }\r
+ // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
+ catch( e )\r
+ {\r
+ return { container : parent, offset : getNodeIndex( child ) };\r
+ }\r
+ }\r
+\r
+ return { container : container, offset : position > 0 ? -distance : container.nodeValue.length + distance };\r
}\r
};\r
\r
return function()\r
{\r
- var cache = this._.cache;\r
- if ( cache.ranges )\r
- return cache.ranges;\r
-\r
// IE doesn't have range support (in the W3C way), so we\r
// need to do some magic to transform selections into\r
// CKEDITOR.dom.range instances.\r
boundaryInfo = getBoundaryInformation( nativeRange );\r
range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );\r
\r
- return ( cache.ranges = [ range ] );\r
+ // Correct an invalid IE range case on empty list item. (#5850)\r
+ if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING\r
+ && range.endOffset <= range.startContainer.getIndex() )\r
+ {\r
+ range.collapse();\r
+ }\r
+\r
+ return [ range ];\r
}\r
else if ( type == CKEDITOR.SELECTION_ELEMENT )\r
{\r
- var retval = this._.cache.ranges = [];\r
+ var retval = [];\r
\r
for ( var i = 0 ; i < nativeRange.length ; i++ )\r
{\r
return retval;\r
}\r
\r
- return ( cache.ranges = [] );\r
+ return [];\r
};\r
})()\r
:\r
function()\r
{\r
- var cache = this._.cache;\r
- if ( cache.ranges )\r
- return cache.ranges;\r
\r
// On browsers implementing the W3C range, we simply\r
// tranform the native ranges in CKEDITOR.dom.range\r
// instances.\r
\r
- var ranges = [];\r
- var sel = this.getNative();\r
+ var ranges = [],\r
+ range,\r
+ doc = this.document,\r
+ sel = this.getNative();\r
\r
if ( !sel )\r
- return [];\r
+ return ranges;\r
+\r
+ // On WebKit, it may happen that we'll have no selection\r
+ // available. We normalize it here by replicating the\r
+ // behavior of other browsers.\r
+ if ( !sel.rangeCount )\r
+ {\r
+ range = new CKEDITOR.dom.range( doc );\r
+ range.moveToElementEditStart( doc.getBody() );\r
+ ranges.push( range );\r
+ }\r
\r
for ( var i = 0 ; i < sel.rangeCount ; i++ )\r
{\r
var nativeRange = sel.getRangeAt( i );\r
- var range = new CKEDITOR.dom.range( this.document );\r
+\r
+ range = new CKEDITOR.dom.range( doc );\r
\r
range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );\r
range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );\r
ranges.push( range );\r
}\r
+ return ranges;\r
+ };\r
\r
- return ( cache.ranges = ranges );\r
- },\r
+ return function( onlyEditables )\r
+ {\r
+ var cache = this._.cache;\r
+ if ( cache.ranges && !onlyEditables )\r
+ return cache.ranges;\r
+ else if ( !cache.ranges )\r
+ cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );\r
+\r
+ // Split range into multiple by read-only nodes.\r
+ if ( onlyEditables )\r
+ {\r
+ var ranges = cache.ranges;\r
+ for ( var i = 0; i < ranges.length; i++ )\r
+ {\r
+ var range = ranges[ i ];\r
+\r
+ // Drop range spans inside one ready-only node.\r
+ var parent = range.getCommonAncestor();\r
+ if ( parent.isReadOnly() )\r
+ ranges.splice( i, 1 );\r
+\r
+ if ( range.collapsed )\r
+ continue;\r
+\r
+ // Range may start inside a non-editable element,\r
+ // replace the range start after it.\r
+ if ( range.startContainer.isReadOnly() )\r
+ {\r
+ var current = range.startContainer;\r
+ while( current )\r
+ {\r
+ if ( current.is( 'body' ) || !current.isReadOnly() )\r
+ break;\r
+\r
+ if ( current.type == CKEDITOR.NODE_ELEMENT\r
+ && current.getAttribute( 'contentEditable' ) == 'false' )\r
+ range.setStartAfter( current );\r
+\r
+ current = current.getParent();\r
+ }\r
+ }\r
+\r
+ var startContainer = range.startContainer,\r
+ endContainer = range.endContainer,\r
+ startOffset = range.startOffset,\r
+ endOffset = range.endOffset,\r
+ walkerRange = range.clone();\r
+\r
+ // Enlarge range start/end with text node to avoid walker\r
+ // being DOM destructive, it doesn't interfere our checking\r
+ // of elements below as well.\r
+ if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )\r
+ {\r
+ if ( startOffset >= startContainer.getLength() )\r
+ walkerRange.setStartAfter( startContainer );\r
+ else\r
+ walkerRange.setStartBefore( startContainer );\r
+ }\r
+\r
+ if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )\r
+ {\r
+ if ( !endOffset )\r
+ walkerRange.setEndBefore( endContainer );\r
+ else\r
+ walkerRange.setEndAfter( endContainer );\r
+ }\r
+\r
+ // Looking for non-editable element inside the range.\r
+ var walker = new CKEDITOR.dom.walker( walkerRange );\r
+ walker.evaluator = function( node )\r
+ {\r
+ if ( node.type == CKEDITOR.NODE_ELEMENT\r
+ && node.isReadOnly() )\r
+ {\r
+ var newRange = range.clone();\r
+ range.setEndBefore( node );\r
+\r
+ // Drop collapsed range around read-only elements,\r
+ // it make sure the range list empty when selecting\r
+ // only non-editable elements.\r
+ if ( range.collapsed )\r
+ ranges.splice( i--, 1 );\r
+\r
+ // Avoid creating invalid range.\r
+ if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )\r
+ {\r
+ newRange.setStartAfter( node );\r
+ if ( !newRange.collapsed )\r
+ ranges.splice( i + 1, 0, newRange );\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+ };\r
+\r
+ walker.next();\r
+ }\r
+ }\r
+\r
+ return cache.ranges;\r
+ };\r
+ })(),\r
\r
/**\r
* Gets the DOM element in which the selection starts.\r
* @returns {CKEDITOR.dom.element} The element at the beginning of the\r
* selection.\r
* @example\r
- * var element = editor.getSelection().<b>getStartElement()</b>;\r
+ * var element = editor.getSelection().<strong>getStartElement()</strong>;\r
* alert( element.getName() );\r
*/\r
getStartElement : function()\r
// Decrease the range content to exclude particial\r
// selected node on the start which doesn't have\r
// visual impact. ( #3231 )\r
- while ( true )\r
+ while ( 1 )\r
{\r
var startContainer = range.startContainer,\r
startOffset = range.startOffset;\r
node = node.getChild( range.startOffset );\r
\r
if ( !node || node.type != CKEDITOR.NODE_ELEMENT )\r
- return range.startContainer;\r
-\r
- var child = node.getFirst();\r
- while ( child && child.type == CKEDITOR.NODE_ELEMENT )\r
+ node = range.startContainer;\r
+ else\r
{\r
- node = child;\r
- child = child.getFirst();\r
+ var child = node.getFirst();\r
+ while ( child && child.type == CKEDITOR.NODE_ELEMENT )\r
+ {\r
+ node = child;\r
+ child = child.getFirst();\r
+ }\r
}\r
-\r
- return node;\r
}\r
- }\r
-\r
- if ( CKEDITOR.env.ie )\r
- {\r
- range = sel.createRange();\r
- range.collapse( true );\r
-\r
- node = range.parentElement();\r
- }\r
- else\r
- {\r
- node = sel.anchorNode;\r
+ else\r
+ {\r
+ node = range.startContainer;\r
+ if ( node.type != CKEDITOR.NODE_ELEMENT )\r
+ node = node.getParent();\r
+ }\r
\r
- if ( node && node.nodeType != 1 )\r
- node = node.parentNode;\r
+ node = node.$;\r
}\r
}\r
\r
},\r
\r
/**\r
- * Gets the current selected element.\r
+ * Gets the currently selected element.\r
* @returns {CKEDITOR.dom.element} The selected element. Null if no\r
* selection is available or the selection type is not\r
- * {@link CKEDITOR.SELECTION_ELEMENT}.\r
+ * <code>{@link CKEDITOR.SELECTION_ELEMENT}</code>.\r
* @example\r
- * var element = editor.getSelection().<b>getSelectedElement()</b>;\r
+ * var element = editor.getSelection().<strong>getSelectedElement()</strong>;\r
* alert( element.getName() );\r
*/\r
getSelectedElement : function()\r
{\r
return self.getNative().createRange().item( 0 );\r
},\r
+ // If a table or list is fully selected.\r
+ function()\r
+ {\r
+ var root,\r
+ retval,\r
+ range = self.getRanges()[ 0 ],\r
+ ancestor = range.getCommonAncestor( 1, 1 ),\r
+ tags = { table:1,ul:1,ol:1,dl:1 };\r
+\r
+ for ( var t in tags )\r
+ {\r
+ if ( ( root = ancestor.getAscendant( t, 1 ) ) )\r
+ break;\r
+ }\r
+\r
+ if ( root )\r
+ {\r
+ // Enlarging the start boundary.\r
+ var testRange = new CKEDITOR.dom.range( this.document );\r
+ testRange.setStartAt( root, CKEDITOR.POSITION_AFTER_START );\r
+ testRange.setEnd( range.startContainer, range.startOffset );\r
+\r
+ var enlargeables = CKEDITOR.tools.extend( tags, CKEDITOR.dtd.$listItem, CKEDITOR.dtd.$tableContent ),\r
+ walker = new CKEDITOR.dom.walker( testRange ),\r
+ // Check the range is at the inner boundary of the structural element.\r
+ guard = function( walker, isEnd )\r
+ {\r
+ return function( node, isWalkOut )\r
+ {\r
+ if ( node.type == CKEDITOR.NODE_TEXT && ( !CKEDITOR.tools.trim( node.getText() ) || node.getParent().data( 'cke-bookmark' ) ) )\r
+ return true;\r
+\r
+ var tag;\r
+ if ( node.type == CKEDITOR.NODE_ELEMENT )\r
+ {\r
+ tag = node.getName();\r
+\r
+ // Bypass bogus br at the end of block.\r
+ if ( tag == 'br' && isEnd && node.equals( node.getParent().getBogus() ) )\r
+ return true;\r
+\r
+ if ( isWalkOut && tag in enlargeables || tag in CKEDITOR.dtd.$removeEmpty )\r
+ return true;\r
+ }\r
+\r
+ walker.halted = 1;\r
+ return false;\r
+ };\r
+ };\r
+\r
+ walker.guard = guard( walker );\r
+\r
+ if ( walker.checkBackward() && !walker.halted )\r
+ {\r
+ walker = new CKEDITOR.dom.walker( testRange );\r
+ testRange.setStart( range.endContainer, range.endOffset );\r
+ testRange.setEndAt( root, CKEDITOR.POSITION_BEFORE_END );\r
+ walker.guard = guard( walker, 1 );\r
+ if ( walker.checkForward() && !walker.halted )\r
+ retval = root.$;\r
+ }\r
+ }\r
+\r
+ if ( !retval )\r
+ throw 0;\r
+\r
+ return retval;\r
+ },\r
// Figure it out by checking if there's a single enclosed\r
// node of the range.\r
function()\r
return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );\r
},\r
\r
+ /**\r
+ * Retrieves the text contained within the range. An empty string is returned for non-text selection.\r
+ * @returns {String} A string of text within the current selection.\r
+ * @since 3.6.1\r
+ * @example\r
+ * var text = editor.getSelection().<strong>getSelectedText()</strong>;\r
+ * alert( text );\r
+ */\r
+ getSelectedText : function()\r
+ {\r
+ var cache = this._.cache;\r
+ if ( cache.selectedText !== undefined )\r
+ return cache.selectedText;\r
+\r
+ var text = '',\r
+ nativeSel = this.getNative();\r
+ if ( this.getType() == CKEDITOR.SELECTION_TEXT )\r
+ text = CKEDITOR.env.ie ? nativeSel.createRange().text : nativeSel.toString();\r
+\r
+ return ( cache.selectedText = text );\r
+ },\r
+\r
+ /**\r
+ * Locks the selection made in the editor in order to make it possible to\r
+ * manipulate it without browser interference. A locked selection is\r
+ * cached and remains unchanged until it is released with the <code>#unlock</code>\r
+ * method.\r
+ * @example\r
+ * editor.getSelection().<strong>lock()</strong>;\r
+ */\r
lock : function()\r
{\r
// Call all cacheable function.\r
this.getRanges();\r
this.getStartElement();\r
this.getSelectedElement();\r
+ this.getSelectedText();\r
\r
// The native selection is not available when locked.\r
this._.cache.nativeSel = {};\r
\r
- this.isLocked = true;\r
+ this.isLocked = 1;\r
\r
// Save this selection inside the DOM document.\r
this.document.setCustomData( 'cke_locked_selection', this );\r
},\r
\r
+ /**\r
+ * Unlocks the selection made in the editor and locked with the <code>#lock</code> method.\r
+ * An unlocked selection is no longer cached and can be changed.\r
+ * @param {Boolean} [restore] If set to <code>true</code>, the selection is restored back to the selection saved earlier by using the <code>#lock</code> method.\r
+ * @example\r
+ * editor.getSelection().<strong>unlock()</strong>;\r
+ */\r
unlock : function( restore )\r
{\r
var doc = this.document,\r
var selectedElement = lockedSelection.getSelectedElement(),\r
ranges = !selectedElement && lockedSelection.getRanges();\r
\r
- this.isLocked = false;\r
+ this.isLocked = 0;\r
this.reset();\r
\r
- doc.getBody().focus();\r
-\r
if ( selectedElement )\r
this.selectElement( selectedElement );\r
else\r
\r
if ( !lockedSelection || !restore )\r
{\r
- this.isLocked = false;\r
+ this.isLocked = 0;\r
this.reset();\r
}\r
},\r
\r
+ /**\r
+ * Clears the selection cache.\r
+ * @example\r
+ * editor.getSelection().<strong>reset()</strong>;\r
+ */\r
reset : function()\r
{\r
this._.cache = {};\r
},\r
\r
+ /**\r
+ * Makes the current selection of type <code>{@link CKEDITOR.SELECTION_ELEMENT}</code> by enclosing the specified element.\r
+ * @param {CKEDITOR.dom.element} element The element to enclose in the selection.\r
+ * @example\r
+ * var element = editor.document.getById( 'sampleElement' );\r
+ * editor.getSelection.<strong>selectElement( element )</strong>;\r
+ */\r
selectElement : function( element )\r
{\r
if ( this.isLocked )\r
\r
this._.cache.selectedElement = element;\r
this._.cache.startElement = element;\r
- this._.cache.ranges = [ range ];\r
+ this._.cache.ranges = new CKEDITOR.dom.rangeList( range );\r
this._.cache.type = CKEDITOR.SELECTION_ELEMENT;\r
\r
return;\r
}\r
\r
- if ( CKEDITOR.env.ie )\r
- {\r
- this.getNative().empty();\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
-\r
- this.reset();\r
- }\r
- else\r
- {\r
- // Create the range for the element.\r
- range = this.document.$.createRange();\r
- range.selectNode( element.$ );\r
+ range = new CKEDITOR.dom.range( element.getDocument() );\r
+ range.setStartBefore( element );\r
+ range.setEndAfter( element );\r
+ range.select();\r
\r
- // Select the range.\r
- var sel = this.getNative();\r
- sel.removeAllRanges();\r
- sel.addRange( range );\r
+ this.document.fire( 'selectionchange' );\r
+ this.reset();\r
\r
- this.reset();\r
- }\r
},\r
\r
+ /**\r
+ * Clears the original selection and adds the specified ranges\r
+ * to the document selection.\r
+ * @param {Array} ranges An array of <code>{@link CKEDITOR.dom.range}</code> instances representing ranges to be added to the document.\r
+ * @example\r
+ * var ranges = new CKEDITOR.dom.range( editor.document );\r
+ * editor.getSelection().<strong>selectRanges( [ ranges ] )</strong>;\r
+ */\r
selectRanges : function( ranges )\r
{\r
if ( this.isLocked )\r
{\r
this._.cache.selectedElement = null;\r
- this._.cache.startElement = ranges[ 0 ].getTouchedStartNode();\r
- this._.cache.ranges = ranges;\r
+ this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();\r
+ this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );\r
this._.cache.type = CKEDITOR.SELECTION_TEXT;\r
\r
return;\r
\r
if ( CKEDITOR.env.ie )\r
{\r
- // IE doesn't accept multiple ranges selection, so we just\r
- // select the first one.\r
+ if ( ranges.length > 1 )\r
+ {\r
+ // IE doesn't accept multiple ranges selection, so we join all into one.\r
+ var last = ranges[ ranges.length -1 ] ;\r
+ ranges[ 0 ].setEnd( last.endContainer, last.endOffset );\r
+ ranges.length = 1;\r
+ }\r
+\r
if ( ranges[ 0 ] )\r
ranges[ 0 ].select();\r
\r
else\r
{\r
var sel = this.getNative();\r
- sel.removeAllRanges();\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
+ // Joining sequential ranges introduced by\r
+ // readonly elements protection.\r
+ if ( i < ranges.length -1 )\r
+ {\r
+ var left = ranges[ i ], right = ranges[ i +1 ],\r
+ between = left.clone();\r
+ between.setStart( left.endContainer, left.endOffset );\r
+ between.setEnd( right.startContainer, right.startOffset );\r
+\r
+ // Don't confused by Firefox adjancent multi-ranges\r
+ // introduced by table cells selection.\r
+ if ( !between.collapsed )\r
+ {\r
+ between.shrink( CKEDITOR.NODE_ELEMENT, true );\r
+ var ancestor = between.getCommonAncestor(),\r
+ enclosed = between.getEnclosedNode();\r
+\r
+ // The following cases has to be considered:\r
+ // 1. <span contenteditable="false">[placeholder]</span>\r
+ // 2. <input contenteditable="false" type="radio"/> (#6621)\r
+ if ( ancestor.isReadOnly() || enclosed && enclosed.isReadOnly() )\r
+ {\r
+ right.setStart( left.startContainer, left.startOffset );\r
+ ranges.splice( i--, 1 );\r
+ continue;\r
+ }\r
+ }\r
+ }\r
+\r
var range = ranges[ i ];\r
var nativeRange = this.document.$.createRange();\r
var startContainer = range.startContainer;\r
// In FF2, if we have a collapsed range, inside an empty\r
// element, we must add something to it otherwise the caret\r
// will not be visible.\r
+ // In Opera instead, the selection will be moved out of the\r
+ // element. (#4657)\r
if ( range.collapsed &&\r
- ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) &&\r
+ ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&\r
startContainer.type == CKEDITOR.NODE_ELEMENT &&\r
!startContainer.getChildCount() )\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
}\r
\r
+ // Don't miss selection change event for non-IEs.\r
+ this.document.fire( 'selectionchange' );\r
this.reset();\r
}\r
},\r
\r
+ /**\r
+ * Creates a bookmark for each range of this selection (from <code>#getRanges</code>)\r
+ * by calling the <code>{@link CKEDITOR.dom.range.prototype.createBookmark}</code> method,\r
+ * with extra care taken to avoid interference among those ranges. The arguments\r
+ * received are the same as with the underlying range method.\r
+ * @returns {Array} Array of bookmarks for each range.\r
+ * @example\r
+ * var bookmarks = editor.getSelection().<strong>createBookmarks()</strong>;\r
+ */\r
createBookmarks : function( serializable )\r
{\r
- var retval = [],\r
- ranges = this.getRanges(),\r
- length = ranges.length,\r
- bookmark;\r
- for ( var i = 0; i < length ; i++ )\r
- {\r
- retval.push( bookmark = ranges[ i ].createBookmark( serializable, true ) );\r
-\r
- serializable = bookmark.serializable;\r
-\r
- var bookmarkStart = serializable ? this.document.getById( bookmark.startNode ) : bookmark.startNode,\r
- bookmarkEnd = serializable ? this.document.getById( bookmark.endNode ) : bookmark.endNode;\r
-\r
- // Updating the offset values for rest of ranges which have been mangled(#3256).\r
- for ( var j = i + 1 ; j < length ; j++ )\r
- {\r
- var dirtyRange = ranges[ j ],\r
- rangeStart = dirtyRange.startContainer,\r
- rangeEnd = dirtyRange.endContainer;\r
-\r
- rangeStart.equals( bookmarkStart.getParent() ) && dirtyRange.startOffset++;\r
- rangeStart.equals( bookmarkEnd.getParent() ) && dirtyRange.startOffset++;\r
- rangeEnd.equals( bookmarkStart.getParent() ) && dirtyRange.endOffset++;\r
- rangeEnd.equals( bookmarkEnd.getParent() ) && dirtyRange.endOffset++;\r
- }\r
- }\r
-\r
- return retval;\r
+ return this.getRanges().createBookmarks( serializable );\r
},\r
\r
+ /**\r
+ * Creates a bookmark for each range of this selection (from <code>#getRanges</code>)\r
+ * by calling the <code>{@link CKEDITOR.dom.range.prototype.createBookmark2}</code> method,\r
+ * with extra care taken to avoid interference among those ranges. The arguments\r
+ * received are the same as with the underlying range method.\r
+ * @returns {Array} Array of bookmarks for each range.\r
+ * @example\r
+ * var bookmarks = editor.getSelection().<strong>createBookmarks2()</strong>;\r
+ */\r
createBookmarks2 : function( normalized )\r
{\r
- var bookmarks = [],\r
- ranges = this.getRanges();\r
-\r
- for ( var i = 0 ; i < ranges.length ; i++ )\r
- bookmarks.push( ranges[i].createBookmark2( normalized ) );\r
-\r
- return bookmarks;\r
+ return this.getRanges().createBookmarks2( normalized );\r
},\r
\r
+ /**\r
+ * Selects the virtual ranges denoted by the bookmarks by calling <code>#selectRanges</code>.\r
+ * @param {Array} bookmarks The bookmarks representing ranges to be selected.\r
+ * @returns {CKEDITOR.dom.selection} This selection object, after the ranges were selected.\r
+ * @example\r
+ * var bookmarks = editor.getSelection().createBookmarks();\r
+ * editor.getSelection().<strong>selectBookmarks( bookmarks )</strong>;\r
+ */\r
selectBookmarks : function( bookmarks )\r
{\r
var ranges = [];\r
return this;\r
},\r
\r
+ /**\r
+ * Retrieves the common ancestor node of the first range and the last range.\r
+ * @returns {CKEDITOR.dom.element} The common ancestor of the selection.\r
+ * @example\r
+ * var ancestor = editor.getSelection().<strong>getCommonAncestor()</strong>;\r
+ */\r
getCommonAncestor : function()\r
{\r
var ranges = this.getRanges(),\r
return startNode.getCommonAncestor( endNode );\r
},\r
\r
- // Moving scroll bar to the current selection's start position.\r
+ /**\r
+ * Moves the scrollbar to the starting position of the current selection.\r
+ * @example\r
+ * editor.getSelection().<strong>scrollIntoView()</strong>;\r
+ */\r
scrollIntoView : function()\r
{\r
// If we have split the block, adds a temporary span at the\r
start.scrollIntoView();\r
}\r
};\r
-})();\r
-( function()\r
-{\r
-var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true );\r
-var fillerTextRegex = /\ufeff|\u00a0/;\r
-var nonCells = { table:1,tbody:1,tr:1 };\r
-\r
-CKEDITOR.dom.range.prototype.select =\r
- CKEDITOR.env.ie ?\r
- // V2\r
- function( forceExpand )\r
- {\r
- var collapsed = this.collapsed;\r
- var isStartMarkerAlone;\r
- var dummySpan;\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
- if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells\r
- || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )\r
- {\r
- this.shrink( CKEDITOR.NODE_ELEMENT, true );\r
- }\r
\r
- var bookmark = this.createBookmark();\r
+ var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),\r
+ isVisible = CKEDITOR.dom.walker.invisible( 1 ),\r
+ fillerTextRegex = /\ufeff|\u00a0/,\r
+ nonCells = { table:1,tbody:1,tr:1 };\r
\r
- // Create marker tags for the start and end boundaries.\r
- var startNode = bookmark.startNode;\r
+ CKEDITOR.dom.range.prototype.select =\r
+ CKEDITOR.env.ie ?\r
+ // V2\r
+ function( forceExpand )\r
+ {\r
+ var collapsed = this.collapsed,\r
+ isStartMarkerAlone, dummySpan, ieRange;\r
\r
- var endNode;\r
- if ( !collapsed )\r
- endNode = bookmark.endNode;\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
- // Create the main range which will be used for the selection.\r
- var ieRange = this.document.$.body.createTextRange();\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
+ if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells\r
+ || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )\r
+ {\r
+ this.shrink( CKEDITOR.NODE_ELEMENT, true );\r
+ }\r
\r
- // Position the range at the start boundary.\r
- ieRange.moveToElementText( startNode.$ );\r
- ieRange.moveStart( 'character', 1 );\r
+ var bookmark = this.createBookmark();\r
\r
- if ( endNode )\r
- {\r
- // Create a tool range for the end.\r
- var ieRangeEnd = this.document.$.body.createTextRange();\r
+ // Create marker tags for the start and end boundaries.\r
+ var startNode = bookmark.startNode;\r
\r
- // Position the tool range at the end.\r
- ieRangeEnd.moveToElementText( endNode.$ );\r
+ var endNode;\r
+ if ( !collapsed )\r
+ endNode = bookmark.endNode;\r
\r
- // Move the end boundary of the main range to match the tool range.\r
- ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );\r
- ieRange.moveEnd( 'character', -1 );\r
- }\r
- else\r
- {\r
- // The isStartMarkerAlone logic comes from V2. It guarantees that the lines\r
- // will expand and that the cursor will be blinking on the right place.\r
- // Actually, we are using this flag just to avoid using this hack in all\r
- // situations, but just on those needed.\r
- var next = startNode.getNext( notWhitespaces );\r
- isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?\r
- && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );\r
-\r
- // Append a temporary <span></span> before the selection.\r
- // This is needed to avoid IE destroying selections inside empty\r
- // inline elements, like <b></b> (#253).\r
- // It is also needed when placing the selection right after an inline\r
- // element to avoid the selection moving inside of it.\r
- dummySpan = this.document.createElement( 'span' );\r
- dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.\r
- dummySpan.insertBefore( startNode );\r
-\r
- if ( isStartMarkerAlone )\r
- {\r
- // To expand empty blocks or line spaces after <br>, we need\r
- // instead to have any char, which will be later deleted using the\r
- // selection.\r
- // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)\r
- this.document.createText( '\ufeff' ).insertBefore( startNode );\r
- }\r
- }\r
+ // Create the main range which will be used for the selection.\r
+ ieRange = this.document.$.body.createTextRange();\r
\r
- // Remove the markers (reset the position, because of the changes in the DOM tree).\r
- this.setStartBefore( startNode );\r
- startNode.remove();\r
+ // Position the range at the start boundary.\r
+ ieRange.moveToElementText( startNode.$ );\r
+ ieRange.moveStart( 'character', 1 );\r
\r
- if ( collapsed )\r
- {\r
- if ( isStartMarkerAlone )\r
+ if ( endNode )\r
{\r
- // Move the selection start to include the temporary \ufeff.\r
- ieRange.moveStart( 'character', -1 );\r
+ // Create a tool range for the end.\r
+ var ieRangeEnd = this.document.$.body.createTextRange();\r
\r
- ieRange.select();\r
+ // Position the tool range at the end.\r
+ ieRangeEnd.moveToElementText( endNode.$ );\r
\r
- // Remove our temporary stuff.\r
- this.document.$.selection.clear();\r
+ // Move the end boundary of the main range to match the tool range.\r
+ ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );\r
+ ieRange.moveEnd( 'character', -1 );\r
}\r
else\r
- ieRange.select();\r
+ {\r
+ // The isStartMarkerAlone logic comes from V2. It guarantees that the lines\r
+ // will expand and that the cursor will be blinking on the right place.\r
+ // Actually, we are using this flag just to avoid using this hack in all\r
+ // situations, but just on those needed.\r
+ var next = startNode.getNext( notWhitespaces );\r
+ isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) ) // already a filler there?\r
+ && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );\r
+\r
+ // Append a temporary <span></span> before the selection.\r
+ // This is needed to avoid IE destroying selections inside empty\r
+ // inline elements, like <b></b> (#253).\r
+ // It is also needed when placing the selection right after an inline\r
+ // element to avoid the selection moving inside of it.\r
+ dummySpan = this.document.createElement( 'span' );\r
+ dummySpan.setHtml( '' ); // Zero Width No-Break Space (U+FEFF). See #1359.\r
+ dummySpan.insertBefore( startNode );\r
+\r
+ if ( isStartMarkerAlone )\r
+ {\r
+ // To expand empty blocks or line spaces after <br>, we need\r
+ // instead to have any char, which will be later deleted using the\r
+ // selection.\r
+ // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)\r
+ this.document.createText( '\ufeff' ).insertBefore( startNode );\r
+ }\r
+ }\r
\r
- this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );\r
- dummySpan.remove();\r
- }\r
- else\r
- {\r
- this.setEndBefore( endNode );\r
- endNode.remove();\r
- ieRange.select();\r
- }\r
+ // Remove the markers (reset the position, because of the changes in the DOM tree).\r
+ this.setStartBefore( startNode );\r
+ startNode.remove();\r
\r
- this.document.fire( 'selectionchange' );\r
- }\r
- :\r
- function()\r
- {\r
- var startContainer = this.startContainer;\r
+ if ( collapsed )\r
+ {\r
+ if ( isStartMarkerAlone )\r
+ {\r
+ // Move the selection start to include the temporary \ufeff.\r
+ ieRange.moveStart( 'character', -1 );\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
+ ieRange.select();\r
\r
- var nativeRange = this.document.$.createRange();\r
- nativeRange.setStart( startContainer.$, this.startOffset );\r
+ // Remove our temporary stuff.\r
+ this.document.$.selection.clear();\r
+ }\r
+ else\r
+ ieRange.select();\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
+ this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );\r
+ dummySpan.remove();\r
}\r
else\r
- throw( e );\r
- }\r
+ {\r
+ this.setEndBefore( endNode );\r
+ endNode.remove();\r
+ ieRange.select();\r
+ }\r
\r
- var selection = this.document.getSelection().getNative();\r
- selection.removeAllRanges();\r
- selection.addRange( nativeRange );\r
- };\r
+ this.document.fire( 'selectionchange' );\r
+ }\r
+ :\r
+ function()\r
+ {\r
+ this.document.getSelection().selectRanges( [ this ] );\r
+ };\r
} )();\r