Class

TreeWalker (engine/model)

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

class

Position iterator class. It allows to iterate forward and backward over the document.

Filtering

Properties

  • boundaries : Range

    readonly

    Iterator boundaries.

    When the iterator is walking 'forward' on the end of boundary or is walking 'backward' on the start of boundary, then { done: true } is returned.

    If boundaries are not defined they are set before first and after last child of the root node.

  • direction : 'backward' | 'forward'

    readonly

    Walking direction. Defaults 'forward'.

  • ignoreElementEnd : Boolean

    readonly

    Flag indicating whether iterator should ignore elementEnd tags. If the option is true walker will not return a parent node of the start position. If this option is true each Element will be returned once, while if the option is false they might be returned twice: for 'elementStart' and 'elementEnd'.

  • position : Position

    readonly

    Iterator position. This is always static position, even if the initial position was a live position. If start position is not defined then position depends on direction. If direction is 'forward' position starts form the beginning, when direction is 'backward' position starts from the end.

  • shallow : Boolean

    readonly

    Flag indicating whether iterator should enter elements or not. If the iterator is shallow child nodes of any iterated node will not be returned along with elementEnd tag.

  • singleCharacters : Boolean

    readonly

    Flag indicating whether all consecutive characters with the same attributes should be returned as one TextProxy (true) or one by one (false).

  • _boundaryEndParent : Element

    private

    End boundary cached for optimization purposes.

  • _boundaryStartParent : Element

    private

    Start boundary cached for optimization purposes.

  • _visitedParent : Element | DocumentFragment

    private

    Parent of the most recently visited node. Cached for optimization purposes.

Methods

  • constructor( [ options ] = { [options.direction], [options.boundaries], [options.startPosition], [options.singleCharacters], [options.shallow], [options.ignoreElementEnd] } )

    Creates a range iterator. All parameters are optional, but you have to specify either boundaries or startPosition.

    Parameters

    [ options ] : Object

    Object with configuration.

    Properties
    [ options.direction ] : 'forward' | 'backward'

    Walking direction.

    Defaults to 'forward'

    [ options.boundaries ] : Range

    Range to define boundaries of the iterator.

    [ options.startPosition ] : Position

    Starting position.

    [ options.singleCharacters ] : Boolean

    Flag indicating whether all consecutive characters with the same attributes should be returned one by one as multiple TextProxy (true) objects or as one TextProxy (false).

    Defaults to false

    [ options.shallow ] : Boolean

    Flag indicating whether iterator should enter elements or not. If the iterator is shallow child nodes of any iterated node will not be returned along with elementEnd tag.

    Defaults to false

    [ options.ignoreElementEnd ] : Boolean

    Flag indicating whether iterator should ignore elementEnd tags. If the option is true walker will not return a parent node of start position. If this option is true each Element will be returned once, while if the option is false they might be returned twice: for 'elementStart' and 'elementEnd'.

    Defaults to false

    Defaults to {}

  • Symbol.iterator() → Iterable.<TreeWalkerValue>

    Iterable interface.

    Returns

    Iterable.<TreeWalkerValue>
  • next() → TreeWalkerValue

    Gets the next tree walker's value.

    Returns

    TreeWalkerValue

    Next tree walker's value.

  • skip( skip )

    Moves position in the direction skipping values as long as the callback function returns true.

    For example:

    walker.skip( value => value.type == 'text' ); // <paragraph>[]foo</paragraph> -> <paragraph>foo[]</paragraph>
    walker.skip( () => true ); // Move the position to the end: <paragraph>[]foo</paragraph> -> <paragraph>foo</paragraph>[]
    walker.skip( () => false ); // Do not move the position.

    Parameters

    skip : function

    Callback function. Gets TreeWalkerValue and should return true if the value should be skipped or false if not.

  • _next() → Object

    private

    Makes a step forward in model. Moves the position to the next position and returns the encountered value.

    Returns

    Object
    Boolean

    return.done True if iterator is done.

    TreeWalkerValue

    return.value Information about taken step.

  • _previous() → Object

    private

    Makes a step backward in model. Moves the position to the previous position and returns the encountered value.

    Returns

    Object
    Boolean

    return.done True if iterator is done.

    TreeWalkerValue

    return.value Information about taken step.