DocumentSelection (engine/model)
@ckeditor/ckeditor5-engine/src/model/documentselection
DocumentSelection
is a special selection which is used as the
document's selection.
There can be only one instance of DocumentSelection
per document.
Document selection can only be changed by using the Writer
instance
inside the change()
block, as it provides a secure way to modify model.
DocumentSelection
is automatically updated upon changes in the document
to always contain valid ranges. Its attributes are inherited from the text unless set explicitly.
Differences between Selection
and DocumentSelection
are:
- there is always a range in
DocumentSelection
- even if no ranges were added there is a "default range" present in the selection, - ranges added to this selection updates automatically when the document changes,
- attributes of
DocumentSelection
are updated automatically according to selection ranges.
Since DocumentSelection
uses live ranges
and is updated when document
changes, it cannot be set on nodes
that are inside document fragment.
If you need to represent a selection in document fragment,
use Selection class instead.
Filtering
Properties
-
Selection focus. Focus is a position where the selection ends.
-
hasOwnRange : Boolean
module:engine/model/documentselection~DocumentSelection#hasOwnRange
readonly
Describes whether
Documentselection
has own range(s) set, or if it is defaulted to document's default range. -
isBackward : Boolean
module:engine/model/documentselection~DocumentSelection#isBackward
readonly
-
isCollapsed : Boolean
module:engine/model/documentselection~DocumentSelection#isCollapsed
readonly
Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is collapsed.
-
isGravityOverridden
module:engine/model/documentselection~DocumentSelection#isGravityOverridden
readonly
Describes whether the gravity is overridden (using
overrideSelectionGravity
) or not.Note that the gravity remains overridden as long as will not be restored the same number of times as it was overridden.
-
markers : Collection
module:engine/model/documentselection~DocumentSelection#markers
readonly
A collection of selection markers. Marker is a selection marker when selection range is inside the marker range.
Note: Only markers from observed markers groups are collected.
-
rangeCount : Number
module:engine/model/documentselection~DocumentSelection#rangeCount
readonly
Returns number of ranges in selection.
-
_ranges
module:engine/model/documentselection~DocumentSelection#_ranges
protected
Used for the compatibility with the
isEqual
method. -
_selection
module:engine/model/documentselection~DocumentSelection#_selection
protected
Selection used internally by that class (
DocumentSelection
is a proxy to that selection).
Methods
-
constructor( doc )
module:engine/model/documentselection~DocumentSelection#constructor
-
containsEntireContent( [ element ] ) → Boolean
module:engine/model/documentselection~DocumentSelection#containsEntireContent
Checks whether the selection contains the entire content of the given element. This means that selection must start at a position touching the element's start and ends at position touching the element's end.
By default, this method will check whether the entire content of the selection's current root is selected. Useful to check if e.g. the user has just pressed Ctrl + A.
Parameters
[ element ] : Element
-
Defaults to
this.anchor.root
Returns
Boolean
-
delegate( events ) → EmitterMixinDelegateChain
module:engine/model/documentselection~DocumentSelection#delegate
mixed
Delegates selected events to another
Emitter
. For instance:emitterA.delegate( 'eventX' ).to( emitterB ); emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
then
eventX
is delegated (fired by)emitterB
andemitterC
along withdata
:emitterA.fire( 'eventX', data );
and
eventY
is delegated (fired by)emitterC
along withdata
:emitterA.fire( 'eventY', data );
Parameters
events : String
Event names that will be delegated to another emitter.
Returns
-
destroy()
module:engine/model/documentselection~DocumentSelection#destroy
Unbinds all events previously bound by document selection.
-
fire( eventOrInfo, [ args ] ) → *
module:engine/model/documentselection~DocumentSelection#fire
mixed
Fires an event, executing all callbacks registered for it.
The first parameter passed to callbacks is an
EventInfo
object, followed by the optionalargs
provided in thefire()
method call.Parameters
eventOrInfo : String | EventInfo
The name of the event or
EventInfo
object if event is delegated.[ args ] : *
Additional arguments to be passed to the callbacks.
Returns
*
By default the method returns
undefined
. However, the return value can be changed by listeners through modification of theevt.return
's property (the event info is the first param of every callback).
-
getAttribute( key ) → *
module:engine/model/documentselection~DocumentSelection#getAttribute
Gets an attribute value for given key or
undefined
if that attribute is not set on the selection.Parameters
key : String
Key of attribute to look for.
Returns
*
Attribute value or
undefined
.
-
getAttributeKeys() → Iterable.<String>
module:engine/model/documentselection~DocumentSelection#getAttributeKeys
Returns iterable that iterates over this selection's attribute keys.
Returns
Iterable.<String>
-
getAttributes() → Iterable.<*>
module:engine/model/documentselection~DocumentSelection#getAttributes
Returns iterable that iterates over this selection's attributes.
Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value. This format is accepted by native
Map
object and also can be passed inNode
constructor.Returns
Iterable.<*>
-
getFirstPosition() → Position | null
module:engine/model/documentselection~DocumentSelection#getFirstPosition
Returns the first position in the selection. First position is the position that is before any other position in the selection.
-
getFirstRange() → Range | null
module:engine/model/documentselection~DocumentSelection#getFirstRange
-
getLastPosition() → Position | null
module:engine/model/documentselection~DocumentSelection#getLastPosition
Returns the last position in the selection. Last position is the position that is after any other position in the selection.
-
getLastRange() → Range | null
module:engine/model/documentselection~DocumentSelection#getLastRange
-
Returns an iterable that iterates over copies of selection ranges.
Returns
Iterable.<Range>
-
getSelectedBlocks() → Iterable.<Element>
module:engine/model/documentselection~DocumentSelection#getSelectedBlocks
Gets elements of type "block" touched by the selection.
This method's result can be used for example to apply block styling to all blocks covered by this selection.
Note:
getSelectedBlocks()
returns blocks that are nested in other non-block elements but will not return blocks nested in other blocks.In this case the function will return exactly all 3 paragraphs (note:
<blockQuote>
is not a block itself):<paragraph>[a</paragraph> <blockQuote> <paragraph>b</paragraph> </blockQuote> <paragraph>c]d</paragraph>
In this case the paragraph will also be returned, despite the collapsed selection:
<paragraph>[]a</paragraph>
In such a scenario, however, only blocks A, B & E will be returned as blocks C & D are nested in block B:
[<blockA></blockA> <blockB> <blockC></blockC> <blockD></blockD> </blockB> <blockE></blockE>]
If the selection is inside a block all the inner blocks (A & B) are returned:
<block> <blockA>[a</blockA> <blockB>b]</blockB> </block>
Special case: If a selection ends at the beginning of a block, that block is not returned as from user perspective this block wasn't selected. See #984 for more details.
<paragraph>[a</paragraph> <paragraph>b</paragraph> <paragraph>]c</paragraph> // this block will not be returned
Returns
Iterable.<Element>
-
getSelectedElement() → Element | null
module:engine/model/documentselection~DocumentSelection#getSelectedElement
-
hasAttribute( key ) → Boolean
module:engine/model/documentselection~DocumentSelection#hasAttribute
Checks if the selection has an attribute for given key.
Parameters
key : String
Key of attribute to check.
Returns
Boolean
true
if attribute with given key is set on selection,false
otherwise.
-
is( type ) → Boolean
module:engine/model/documentselection~DocumentSelection#is
Checks whether this object is of the given type.
selection.is( 'selection' ); // -> true selection.is( 'documentSelection' ); // -> true selection.is( 'model:selection' ); // -> true selection.is( 'model:documentSelection' ); // -> true selection.is( 'view:selection' ); // -> false selection.is( 'element' ); // -> false selection.is( 'node' ); // -> false
Check the entire list of model objects which implement the
is()
method.Parameters
type : String
Returns
Boolean
-
listenTo( emitter, event, callback, [ options ] = { [options.priority] } )
module:engine/model/documentselection~DocumentSelection#listenTo
mixed
Registers a callback function to be executed when an event is fired in a specific (emitter) object.
Events can be grouped in namespaces using
:
. When namespaced event is fired, it additionally fires all callbacks for that namespace.// myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ). myEmitter.on( 'myGroup', genericCallback ); myEmitter.on( 'myGroup:myEvent', specificCallback ); // genericCallback is fired. myEmitter.fire( 'myGroup' ); // both genericCallback and specificCallback are fired. myEmitter.fire( 'myGroup:myEvent' ); // genericCallback is fired even though there are no callbacks for "foo". myEmitter.fire( 'myGroup:foo' );
An event callback can stop the event and set the return value of the
fire
method.Parameters
emitter : Emitter
The object that fires the event.
event : String
The name of the event.
callback : function
The function to be called on event.
[ options ] : Object
Additional options.
Properties[ options.priority ] : PriorityString | Number
The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.
Defaults to
'normal'
Defaults to
{}
-
observeMarkers( prefixOrName )
module:engine/model/documentselection~DocumentSelection#observeMarkers
Registers a marker group prefix or a marker name to be collected in the selection markers collection.
See also
MarkerCollection#getMarkersGroup()
.Parameters
prefixOrName : String
The marker group prefix or marker name.
-
off( event, callback )
module:engine/model/documentselection~DocumentSelection#off
mixed
Stops executing the callback on the given event. Shorthand for
this.stopListening( this, event, callback )
.Parameters
event : String
The name of the event.
callback : function
The function to stop being called.
-
on( event, callback, [ options ] = { [options.priority] } )
module:engine/model/documentselection~DocumentSelection#on
mixed
Registers a callback function to be executed when an event is fired.
Shorthand for
this.listenTo( this, event, callback, options )
(it makes the emitter listen on itself).Parameters
event : String
The name of the event.
callback : function
The function to be called on event.
[ options ] : Object
Additional options.
Properties[ options.priority ] : PriorityString | Number
The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.
Defaults to
'normal'
Defaults to
{}
-
once( event, callback, [ options ] = { [options.priority] } )
module:engine/model/documentselection~DocumentSelection#once
mixed
Registers a callback function to be executed on the next time the event is fired only. This is similar to calling
on
followed byoff
in the callback.Parameters
event : String
The name of the event.
callback : function
The function to be called on event.
[ options ] : Object
Additional options.
Properties[ options.priority ] : PriorityString | Number
The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.
Defaults to
'normal'
Defaults to
{}
-
refresh()
module:engine/model/documentselection~DocumentSelection#refresh
Refreshes selection attributes and markers according to the current position in the model.
-
stopDelegating( [ event ], [ emitter ] )
module:engine/model/documentselection~DocumentSelection#stopDelegating
mixed
Stops delegating events. It can be used at different levels:
- To stop delegating all events.
- To stop delegating a specific event to all emitters.
- To stop delegating a specific event to a specific emitter.
Parameters
[ event ] : String
The name of the event to stop delegating. If omitted, stops it all delegations.
[ emitter ] : Emitter
(requires
event
) The object to stop delegating a particular event to. If omitted, stops delegation ofevent
to all emitters.
-
stopListening( [ emitter ], [ event ], [ callback ] )
module:engine/model/documentselection~DocumentSelection#stopListening
mixed
Stops listening for events. It can be used at different levels:
- To stop listening to a specific callback.
- To stop listening to a specific event.
- To stop listening to all events fired by a specific object.
- To stop listening to all events fired by all objects.
Parameters
[ emitter ] : Emitter
The object to stop listening to. If omitted, stops it for all objects.
[ event ] : String
(Requires the
emitter
) The name of the event to stop listening to. If omitted, stops it for all events fromemitter
.[ callback ] : function
(Requires the
event
) The function to be removed from the call list for the givenevent
.
-
_addEventListener( event, callback, [ options ] = { [options.priority] } )
module:engine/model/documentselection~DocumentSelection#_addEventListener
protected mixed
Adds callback to emitter for given event.
Parameters
event : String
The name of the event.
callback : function
The function to be called on event.
[ options ] : Object
Additional options.
Properties[ options.priority ] : PriorityString | Number
The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.
Defaults to
'normal'
Defaults to
{}
-
_getStoredAttributes() → Iterable.<*>
module:engine/model/documentselection~DocumentSelection#_getStoredAttributes
protected
Returns an iterable that iterates through all selection attributes stored in current selection's parent.
Returns
Iterable.<*>
-
_overrideGravity() → String
module:engine/model/documentselection~DocumentSelection#_overrideGravity
protected
Temporarily changes the gravity of the selection from the left to the right.
The gravity defines from which direction the selection inherits its attributes. If it's the default left gravity, the selection (after being moved by the the user) inherits attributes from its left hand side. This method allows to temporarily override this behavior by forcing the gravity to the right.
It returns an unique identifier which is required to restore the gravity. It guarantees the symmetry of the process.
Returns
String
The unique id which allows restoring the gravity.
Related:
-
_removeAttribute( key )
module:engine/model/documentselection~DocumentSelection#_removeAttribute
protected
Removes an attribute with given key from the selection. If the given attribute was set on the selection, fires the
event-change:range
event with removed attribute key. Should be used only within theremoveSelectionAttribute
method. -
_removeEventListener( event, callback )
module:engine/model/documentselection~DocumentSelection#_removeEventListener
protected mixed
Removes callback from emitter for given event.
Parameters
event : String
The name of the event.
callback : function
The function to stop being called.
-
_restoreGravity( uid )
module:engine/model/documentselection~DocumentSelection#_restoreGravity
protected
Restores the overridden gravity.
Restoring the gravity is only possible using the unique identifier returned by
_overrideGravity
. Note that the gravity remains overridden as long as won't be restored the same number of times it was overridden.Parameters
uid : String
The unique id returned by
_overrideGravity
.
Related:
-
_setAttribute( key, value )
module:engine/model/documentselection~DocumentSelection#_setAttribute
protected
Sets attribute on the selection. If attribute with the same key already is set, it's value is overwritten. Should be used only within the
setSelectionAttribute
method.Parameters
key : String
Key of the attribute to set.
value : *
Attribute value.
Related:
-
_setFocus( itemOrPosition, [ offset ] )
module:engine/model/documentselection~DocumentSelection#_setFocus
protected
Moves
focus
to the specified location. Should be used only within thesetSelectionFocus
method.The location can be specified in the same form as writer.createPositionAt() parameters.
Parameters
itemOrPosition : Item | Position
[ offset ] : Number | 'end' | 'before' | 'after'
Offset or one of the flags. Used only when first parameter is a model item.
Related:
-
_setTo( selectable, [ placeOrOffset ], [ options ] = { [options.backward] } )
module:engine/model/documentselection~DocumentSelection#_setTo
protected
Sets this selection's ranges and direction to the specified location based on the given selectable. Should be used only within the
setSelection
method.Parameters
selectable : Selectable
[ placeOrOffset ] : Number | 'before' | 'end' | 'after' | 'on' | 'in'
Sets place or offset of the selection.
[ options ] : Object
-
Properties
[ options.backward ] : Boolean
Sets this selection instance to be backward.
Related:
Static methods
-
_getStoreAttributeKey( key ) → String
module:engine/model/documentselection~DocumentSelection._getStoreAttributeKey
protected static
Generates and returns an attribute key for selection attributes store, basing on original attribute key.
Parameters
key : String
Attribute key to convert.
Returns
String
Converted attribute key, applicable for selection store.
-
_isStoreAttributeKey( key ) → Boolean
module:engine/model/documentselection~DocumentSelection._isStoreAttributeKey
protected static
Checks whether the given attribute key is an attribute stored on an element.
Parameters
key : String
Returns
Boolean
Events
-
change:attribute( eventInfo, directChange, attributeKeys )
module:engine/model/documentselection~DocumentSelection#event:change:attribute
Fired when selection attribute changed.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
directChange : Boolean
In case of
Selection
class it is always set totrue
which indicates that the selection change was caused by a direct use of selection's API. TheDocumentSelection
, however, may change because its attributes were directly changed through the writer or because its position was changed in the model and its attributes were refreshed (which means an indirect change). The indirect change does not occur in case of normal (detached) selections because they are "static" (as "not live") which mean that they are not updated once the document changes.attributeKeys : Array.<String>
Array containing keys of attributes that changed.
-
change:marker( eventInfo, directChange, oldMarkers )
module:engine/model/documentselection~DocumentSelection#event:change:marker
Fired when selection marker(s) changed.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
directChange : Boolean
This is always set to
false
in case ofchange:marker
event as there is no possibility to change markers directly throughDocumentSelection
API. See alsoevent-change:range
andevent-change:attribute
.oldMarkers : Array.<Marker>
Markers in which the selection was before the change.
-
change:range( eventInfo, directChange )
module:engine/model/documentselection~DocumentSelection#event:change:range
Fired when selection range(s) changed.
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
directChange : Boolean
In case of
Selection
class it is always set totrue
which indicates that the selection change was caused by a direct use of selection's API. TheDocumentSelection
, however, may change because its position was directly changed through the writer or because its position was changed because the structure of the model has been changed (which means an indirect change). The indirect change does not occur in case of normal (detached) selections because they are "static" (as "not live") which mean that they are not updated once the document changes.
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.