CKEDITOR.dom.selection
Manipulates the selection within a DOM element. If the current browser selection spans outside of the element, an empty selection object is returned.
Despite the fact that selection's constructor allows to create selection instances, usually it's better to get selection from the editor instance:
var sel = editor.getSelection();
Filtering
Properties
-
The sequence used in a WebKit-based browser to create a Filling Character. By default it is a string of 7 zero-width space characters (U+200B).
-
Document in which selection is anchored.
-
-
-
Selection's revision. This value is incremented every time new selection is created or existing one is modified.
-
Selection's root element.
Methods
constructor( target ) → selectionCKEDITOR.dom.selection#constructorCreates a selection class instance.
// Selection scoped in document. var sel = new CKEDITOR.dom.selection( CKEDITOR.document ); // Selection scoped in element with 'editable' id. var sel = new CKEDITOR.dom.selection( CKEDITOR.document.getById( 'editable' ) ); // Cloning selection. var clone = new CKEDITOR.dom.selection( sel );Parameters
target : document | element | selectionThe DOM document/element that the DOM selection is restrained to. Only selection which spans within the target element is considered as valid.
If CKEDITOR.dom.selection is passed, then its clone will be created.
Returns
selection
createBookmarks( serializable ) → ArrayCKEDITOR.dom.selection#createBookmarksCreates a bookmark for each range of this selection (from getRanges) by calling the CKEDITOR.dom.range.createBookmark method, with extra care taken to avoid interference among those ranges. The arguments received are the same as with the underlying range method.
var bookmarks = editor.getSelection().createBookmarks();Parameters
serializable : Object
Returns
ArrayArray of bookmarks for each range.
createBookmarks2( normalized ) → ArrayCKEDITOR.dom.selection#createBookmarks2Creates a bookmark for each range of this selection (from getRanges) by calling the CKEDITOR.dom.range.createBookmark2 method, with extra care taken to avoid interference among those ranges. The arguments received are the same as with the underlying range method.
var bookmarks = editor.getSelection().createBookmarks2();Parameters
normalized : Object
Returns
ArrayArray of bookmarks for each range.
fake( element, [ ariaLabel ] )CKEDITOR.dom.selection#fakeMakes a "fake selection" of an element.
A fake selection does not render UI artifacts over the selected element. Additionally, the browser native selection system is not aware of the fake selection. In practice, the native selection is moved to a hidden place where no native selection UI artifacts are displayed to the user.
Parameters
element : elementThe element to be "selected".
[ ariaLabel ] : StringA string to be used by the screen reader to describe the selection.
getCommonAncestor() → elementCKEDITOR.dom.selection#getCommonAncestorRetrieves the common ancestor node of the first range and the last range.
var ancestor = editor.getSelection().getCommonAncestor();Returns
elementThe common ancestor of the selection or
nullif selection is empty.getNative() → ObjectCKEDITOR.dom.selection#getNativeGets the native selection object from the browser.
var selection = editor.getSelection().getNative();Returns
ObjectThe native browser selection object or null if this is a fake selection.
getRanges( [ onlyEditables ] ) → ArrayCKEDITOR.dom.selection#getRangesRetrieves the 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.
var ranges = selection.getRanges(); alert( ranges.length );Parameters
[ onlyEditables ] : BooleanIf set to
true, this function retrieves editable ranges only.Returns
ArrayRange instances that represent the current selection.
getSelectedElement() → element | nullCKEDITOR.dom.selection#getSelectedElementGets the currently selected element.
var element = editor.getSelection().getSelectedElement(); alert( element.getName() );Returns
element | nullThe selected element.
nullif no selection is available or the selection type is not CKEDITOR.SELECTION_ELEMENT.Retrieves the text contained within the range. An empty string is returned for non-text selection.
var text = editor.getSelection().getSelectedText(); alert( text );Returns
StringA string of text within the current selection.
getStartElement() → elementCKEDITOR.dom.selection#getStartElementGets the DOM element in which the selection starts.
var element = editor.getSelection().getStartElement(); alert( element.getName() );Returns
elementThe element at the beginning of the selection.
getType() → NumberCKEDITOR.dom.selection#getTypeGets the type of the current selection. The following values are available:
- CKEDITOR.SELECTION_NONE (1): No selection.
- CKEDITOR.SELECTION_TEXT (2): A text or a collapsed selection is selected.
- CKEDITOR.SELECTION_ELEMENT (3): An element is selected.
Example:
if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT ) alert( 'A text is selected' );Returns
NumberOne of the following constant values: CKEDITOR.SELECTION_NONE, CKEDITOR.SELECTION_TEXT or CKEDITOR.SELECTION_ELEMENT.
Checks if the selection contains only one range which is collapsed.
if ( editor.getSelection().isCollapsed() ) { // Do something when the selection is collapsed. }Returns
Boolean
isHidden() → BooleanCKEDITOR.dom.selection#isHiddenChecks whether selection is placed in hidden element.
This method is to be used to verify whether fake selection (see fake) is still hidden.
Note: this method should be executed on real selection - e.g.:
editor.getSelection( true ).isHidden();Returns
Boolean
Checks if the selection contains an HTML element inside a table. Returns
falsefor text selection inside a table (e.g. it will returnfalsefor text selected in one cell).editor.getSelection().isInTable();Parameters
[ allowPartialSelection ] : BooleanWhether a partial cell selection should be included. Added in 4.7.2.
Defaults to
falseReturns
Boolean
lock()CKEDITOR.dom.selection#lockLocks 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.
editor.getSelection().lock();Performs an optimization on the current selection if necessary.
The general idea is to shrink the range to text when:
- The range starts at the end of an element.
- The range ends at the start of an element.
- One of the range ends is anchored in a text node and another in an element.
For example:
<p>{foo</p> <p>]bar</p>is optimized too:
<p>{foo}</p> <p>bar</p>removeAllRanges()CKEDITOR.dom.selection#removeAllRangesRemove all the selection ranges from the document.
reset()CKEDITOR.dom.selection#resetscrollIntoView()CKEDITOR.dom.selection#scrollIntoViewMoves the scrollbar to the starting position of the current selection.
editor.getSelection().scrollIntoView();Selects the virtual ranges denoted by the bookmarks by calling selectRanges.
var bookmarks = editor.getSelection().createBookmarks(); editor.getSelection().selectBookmarks( bookmarks );Parameters
bookmarks : ArrayThe bookmarks representing ranges to be selected.
Returns
selectionThis selection object, after the ranges were selected.
selectElement( element )CKEDITOR.dom.selection#selectElementMakes the current selection of type CKEDITOR.SELECTION_ELEMENT by enclosing the specified element.
var element = editor.document.getById( 'sampleElement' ); editor.getSelection().selectElement( element );Parameters
element : elementThe element to enclose in the selection.
selectRanges( ranges )CKEDITOR.dom.selection#selectRangesClears the original selection and adds the specified ranges to the document selection.
// Move selection to the end of the editable element. var range = editor.createRange(); range.moveToPosition( range.root, CKEDITOR.POSITION_BEFORE_END ); editor.getSelection().selectRanges( [ ranges ] );Parameters
ranges : ArrayAn array of CKEDITOR.dom.range instances representing ranges to be added to the document.
unlock( restore )CKEDITOR.dom.selection#unlockStatic methods
-
Sets editor listeners up to optimize the selection.
Note: This method is called automatically during the editor initialization and should not be called manually.
CKEDITOR.dom.selection.optimizeInElementEnds
Parameters
editor : editor