Report an issue
Class

DocumentSelection (engine/model)

@ckeditor/ckeditor5-engine/src/model/documentselection

class

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

  • anchor : Position | null

    readonly

    Selection anchor. Anchor may be described as a position where the most recent part of the selection starts. Together with focus they define the direction of selection, which is important when expanding/shrinking selection. Anchor is always start or end position of the most recently added range.

    Is set to null if there are no ranges in selection.

  • focus : Position | null

    readonly

    Selection focus. Focus is a position where the selection ends.

    Is set to null if there are no ranges in selection.

  • hasOwnRange : Boolean

    readonly

    Describes whether Documentselection has own range(s) set, or if it is defaulted to document's default range.

  • isBackward : Boolean

    readonly

    Specifies whether the focus precedes anchor.

  • isCollapsed : Boolean

    readonly

    Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is collapsed.

  • 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.

  • rangeCount : Number

    readonly

    Returns number of ranges in selection.

  • _ranges

    protected

    Used for the compatibility with the isEqual method.

  • _selection

    protected

    Selection used internally by that class (DocumentSelection is a proxy to that selection).

Methods

  • constructor( doc )

    Creates an empty live selection for given Document.

    Parameters

    doc : Document

    Document which owns this selection.

  • containsEntireContent( [ element ] ) → Boolean

    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
  • destroy()

    Unbinds all events previously bound by document selection.

  • getAttribute( key ) → *

    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>

    Returns iterable that iterates over this selection's attribute keys.

    Returns

    Iterable.<String>
  • getAttributes() → Iterable.<*>

    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 in Node constructor.

    Returns

    Iterable.<*>
  • getFirstPosition() → Position | null

    Returns the first position in the selection. First position is the position that is before any other position in the selection.

    Returns null if there are no ranges in selection.

    Returns

    Position | null
  • getFirstRange() → Range | null

    Returns a copy of the first range in the selection. First range is the one which start position is before start position of all other ranges (not to confuse with the first range added to the selection).

    Returns null if there are no ranges in selection.

    Returns

    Range | null
  • getLastPosition() → Position | null

    Returns the last position in the selection. Last position is the position that is after any other position in the selection.

    Returns null if there are no ranges in selection.

    Returns

    Position | null
  • getLastRange() → Range | null

    Returns a copy of the last range in the selection. Last range is the one which end position is after end position of all other ranges (not to confuse with the range most recently added to the selection).

    Returns null if there are no ranges in selection.

    Returns

    Range | null
  • getRanges() → Iterable.<Range>

    Returns an iterable that iterates over copies of selection ranges.

    Returns

    Iterable.<Range>
  • getSelectedBlocks() → Iterator.<Element>

    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() always returns the deepest block.

    In this case the function will return exactly all 3 paragraphs:

    <paragraph>[a</paragraph>
    <quote>
        <paragraph>b</paragraph>
    </quote>
    <paragraph>c]d</paragraph>

    In this case the paragraph will also be returned, despite the collapsed selection:

    <paragraph>[]a</paragraph>

    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

    Iterator.<Element>
  • getSelectedElement() → Element | null

    Returns the selected element. Element is considered as selected if there is only one range in the selection, and that range contains exactly one element. Returns null if there is no selected element.

    Returns

    Element | null
  • hasAttribute( key ) → Boolean

    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.

  • _getStoredAttributes() → Iterable.<*>

    protected

    Returns an iterable that iterates through all selection attributes stored in current selection's parent.

    Returns

    Iterable.<*>
  • _overrideGravity() → String

    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.

  • _removeAttribute( key )

    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 the removeSelectionAttribute method.

    Parameters

    key : String

    Key of the attribute to remove.

  • _restoreGravity( uid )

    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.

  • _setAttribute( key, value )

    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.

  • _setFocus( itemOrPosition, [ offset ] )

    protected

    Moves focus to the specified location. Should be used only within the setSelectionFocus method.

    The location can be specified in the same form as createAt 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.

  • _setTo( selectable, [ placeOrOffset ], [ options ] = { [options.backward] } )

    protected

    Sets this selection's ranges and direction to the specified location based on the given selection, position, node, position, range, an iterable of ranges or null. Should be used only within the setSelection method.

    Parameters

    selectable : Selection | DocumentSelection | Position | Node | Iterable.<Range> | Range | null
    [ 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.

Static methods

  • _getStoreAttributeKey( key ) → String

    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

    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 )

    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 to true which indicates that the selection change was caused by a direct use of selection's API. The DocumentSelection, 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:range( eventInfo, directChange )

    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 to true which indicates that the selection change was caused by a direct use of selection's API. The DocumentSelection, 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.