TreeWalker (engine/view)
@ckeditor/ckeditor5-engine/src/view/treewalker
Position iterator class. It allows to iterate forward and backward over the document.
Filtering
Properties
-
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.
-
Walking direction. Defaults
'forward'. -
Flag indicating whether iterator should ignore
elementEndtags. If set totrue, walker will not return a parent node of the start position. EachElementwill be returned once. When set tofalseeach element might be returned twice: for'elementStart'and'elementEnd'. -
Iterator 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. -
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
elementEndtag. -
private readonly
_boundaryEndParent : null | Node | DocumentFragmentmodule:engine/view/treewalker~TreeWalker#_boundaryEndParentEnd boundary parent.
-
private readonly
_boundaryStartParent : null | Node | DocumentFragmentmodule:engine/view/treewalker~TreeWalker#_boundaryStartParentStart boundary parent.
-
Iterator 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.
Methods
-
constructor( options )module:engine/view/treewalker~TreeWalker#constructorCreates a range iterator. All parameters are optional, but you have to specify either
boundariesorstartPosition.Parameters
options : TreeWalkerOptionsObject with configuration.
Defaults to
{}
-
Symbol.iterator() → IterableIterator<TreeWalkerValue>module:engine/view/treewalker~TreeWalker#Symbol.iterator -
jumpTo( position ) → voidmodule:engine/view/treewalker~TreeWalker#jumpToMoves tree walker
positionto providedposition. Tree walker will continue traversing from that position.Note: in contrary to
skip, this method does not iterate over the nodes along the way. It simply sets the current tree walker position to a new one. From the performance standpoint, it is better to usejumpTorather thanskip.If the provided position is before the start boundary, the position will be set to the start boundary. If the provided position is after the end boundary, the position will be set to the end boundary. This is done to prevent the treewalker from traversing outside the boundaries.
Parameters
position : PositionPosition to jump to.
Returns
void
-
next() → IteratorResult<TreeWalkerValue, undefined>module:engine/view/treewalker~TreeWalker#nextGets the next tree walker's value.
Returns
IteratorResult<TreeWalkerValue, undefined>Object implementing iterator interface, returning information about taken step.
-
skip( skip ) → voidmodule:engine/view/treewalker~TreeWalker#skipMoves
positionin thedirectionskipping values as long as the callback function returnstrue.For example:
walker.skip( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p> walker.skip( value => true ); // Move the position to the end: <p>{}foo</p> -> <p>foo</p>[] walker.skip( value => false ); // Do not move the position.Parameters
skip : ( value: TreeWalkerValue ) => booleanCallback function. Gets
TreeWalkerValueand should returntrueif the value should be skipped orfalseif not.
Returns
void
-
private
_formatReturnValue( type, item, previousPosition, nextPosition, [ length ] ) → IteratorYieldResult<TreeWalkerValue>module:engine/view/treewalker~TreeWalker#_formatReturnValueFormat returned data and adjust
previousPositionandnextPositionif reach the bound of theText.Parameters
type : TreeWalkerValueTypeType of step.
item : ItemItem between old and new position.
previousPosition : PositionPrevious position of iterator.
nextPosition : PositionNext position of iterator.
[ length ] : numberLength of the item.
Returns
IteratorYieldResult<TreeWalkerValue>
-
private
_next() → IteratorResult<TreeWalkerValue, undefined>module:engine/view/treewalker~TreeWalker#_nextMakes a step forward in view. Moves the
positionto the next position and returns the encountered value.Returns
IteratorResult<TreeWalkerValue, undefined>
-
private
_previous() → IteratorResult<TreeWalkerValue, undefined>module:engine/view/treewalker~TreeWalker#_previousMakes a step backward in view. Moves the
positionto the previous position and returns the encountered value.Returns
IteratorResult<TreeWalkerValue, undefined>
Every day, we work hard to keep our documentation complete. Have you spotted outdated information? Is something missing? Please report it via our issue tracker.
With the release of version 42.0.0, we have rewritten much of our documentation to reflect the new import paths and features. We appreciate your feedback to help us ensure its accuracy and completeness.