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 ) → selection
CKEDITOR.dom.selection#constructor
Creates 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 | selection
The 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 ) → Array
CKEDITOR.dom.selection#createBookmarks
Creates 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
Array
Array of bookmarks for each range.
-
createBookmarks2( normalized ) → Array
CKEDITOR.dom.selection#createBookmarks2
Creates 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
Array
Array of bookmarks for each range.
-
fake( element, [ ariaLabel ] )
CKEDITOR.dom.selection#fake
Makes 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 : element
The element to be "selected".
[ ariaLabel ] : String
A string to be used by the screen reader to describe the selection.
-
getCommonAncestor() → element
CKEDITOR.dom.selection#getCommonAncestor
Retrieves the common ancestor node of the first range and the last range.
var ancestor = editor.getSelection().getCommonAncestor();
Returns
element
The common ancestor of the selection or
null
if selection is empty.
-
getNative() → Object
CKEDITOR.dom.selection#getNative
Gets the native selection object from the browser.
var selection = editor.getSelection().getNative();
Returns
Object
The native browser selection object or null if this is a fake selection.
-
getRanges( [ onlyEditables ] ) → Array
CKEDITOR.dom.selection#getRanges
Retrieves 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 ] : Boolean
If set to
true
, this function retrieves editable ranges only.
Returns
Array
Range instances that represent the current selection.
-
getSelectedElement() → element | null
CKEDITOR.dom.selection#getSelectedElement
Gets the currently selected element.
var element = editor.getSelection().getSelectedElement(); alert( element.getName() );
Returns
element | null
The selected element.
null
if 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
String
A string of text within the current selection.
-
getStartElement() → element
CKEDITOR.dom.selection#getStartElement
Gets the DOM element in which the selection starts.
var element = editor.getSelection().getStartElement(); alert( element.getName() );
Returns
element
The element at the beginning of the selection.
-
getType() → Number
CKEDITOR.dom.selection#getType
Gets 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
Number
One 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() → Boolean
CKEDITOR.dom.selection#isHidden
Checks 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
false
for text selection inside a table (e.g. it will returnfalse
for text selected in one cell).editor.getSelection().isInTable();
Parameters
[ allowPartialSelection ] : Boolean
Whether a partial cell selection should be included. Added in 4.7.2.
Defaults to
false
Returns
Boolean
-
lock()
CKEDITOR.dom.selection#lock
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.
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#removeAllRanges
Remove all the selection ranges from the document.
-
reset()
CKEDITOR.dom.selection#reset
-
scrollIntoView()
CKEDITOR.dom.selection#scrollIntoView
Moves 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 : Array
The bookmarks representing ranges to be selected.
Returns
selection
This selection object, after the ranges were selected.
-
selectElement( element )
CKEDITOR.dom.selection#selectElement
Makes 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 : element
The element to enclose in the selection.
-
selectRanges( ranges )
CKEDITOR.dom.selection#selectRanges
Clears 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 : Array
An array of CKEDITOR.dom.range instances representing ranges to be added to the document.
-
unlock( restore )
CKEDITOR.dom.selection#unlock
Static 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