ViewTreeWalker (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
elementEnd
tags. If set totrue
, walker will not return a parent node of the start position. EachViewElement
will be returned once. When set tofalse
each 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
elementEnd
tag. -
Flag indicating whether all characters from
ViewText
should be returned as oneViewText
or one by one asViewTextProxy
. -
private readonly
_boundaryEndParent : null | ViewNode | ViewDocumentFragment
module:engine/view/treewalker~ViewTreeWalker#_boundaryEndParent
End boundary parent.
-
private readonly
_boundaryStartParent : null | ViewNode | ViewDocumentFragment
module:engine/view/treewalker~ViewTreeWalker#_boundaryStartParent
Start 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~ViewTreeWalker#constructor
Creates a range iterator. All parameters are optional, but you have to specify either
boundaries
orstartPosition
.Parameters
options : ViewTreeWalkerOptions
Object with configuration.
Defaults to
{}
-
Symbol.iterator() → IterableIterator<ViewTreeWalkerValue>
module:engine/view/treewalker~ViewTreeWalker#Symbol.iterator
-
jumpTo( position ) → void
module:engine/view/treewalker~ViewTreeWalker#jumpTo
Moves tree walker
position
to 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 usejumpTo
rather 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 : ViewPosition
Position to jump to.
Returns
void
-
next() → IteratorResult<ViewTreeWalkerValue, undefined>
module:engine/view/treewalker~ViewTreeWalker#next
Gets the next tree walker's value.
Returns
IteratorResult<ViewTreeWalkerValue, undefined>
Object implementing iterator interface, returning information about taken step.
-
skip( skip ) → void
module:engine/view/treewalker~ViewTreeWalker#skip
Moves
position
in thedirection
skipping 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: ViewTreeWalkerValue ) => boolean
Callback function. Gets
ViewTreeWalkerValue
and should returntrue
if the value should be skipped orfalse
if not.
Returns
void
-
private
_formatReturnValue( type, item, previousPosition, nextPosition, [ length ] ) → IteratorYieldResult<ViewTreeWalkerValue>
module:engine/view/treewalker~ViewTreeWalker#_formatReturnValue
Format returned data and adjust
previousPosition
andnextPosition
if reach the bound of theViewText
.Parameters
type : ViewTreeWalkerValueType
Type of step.
item : ViewItem
Item between old and new position.
previousPosition : ViewPosition
Previous position of iterator.
nextPosition : ViewPosition
Next position of iterator.
[ length ] : number
Length of the item.
Returns
IteratorYieldResult<ViewTreeWalkerValue>
-
private
_next() → IteratorResult<ViewTreeWalkerValue, undefined>
module:engine/view/treewalker~ViewTreeWalker#_next
Makes a step forward in view. Moves the
position
to the next position and returns the encountered value.Returns
IteratorResult<ViewTreeWalkerValue, undefined>
-
private
_previous() → IteratorResult<ViewTreeWalkerValue, undefined>
module:engine/view/treewalker~ViewTreeWalker#_previous
Makes a step backward in view. Moves the
position
to the previous position and returns the encountered value.Returns
IteratorResult<ViewTreeWalkerValue, 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.