# ViewTreeWalker

class

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

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L24)

<a id="properties">

## Properties

<a id="member-boundaries">

### `boundaries: ViewRange | null` _(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.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L38)

<a id="member-direction">

### `direction: ViewTreeWalkerDirection` _(readonly)_

Walking direction. Defaults `'forward'`.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L28)

<a id="member-ignoreElementEnd">

### `ignoreElementEnd: boolean` _(readonly)_

Flag indicating whether iterator should ignore `elementEnd` tags. If set to `true`, walker will not return a parent node of the start position. Each [`ViewElement`](module_engine_view_element-ViewElement.md) will be returned once. When set to `false` each element might be returned twice: for `'elementStart'` and `'elementEnd'`.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L57)

<a id="member-position">

### `position: ViewPosition` _(readonly)_

Iterator position. If start position is not defined then position depends on [`direction`](#member-direction). If direction is `'forward'` position starts form the beginning, when direction is `'backward'` position starts from the end.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L130)

<a id="member-shallow">

### `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.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L50)

<a id="member-singleCharacters">

### `singleCharacters: boolean` _(readonly)_

Flag indicating whether all characters from [`ViewText`](module_engine_view_text-ViewText.md) should be returned as one [`ViewText`](module_engine_view_text-ViewText.md) or one by one as [`ViewTextProxy`](module_engine_view_textproxy-ViewTextProxy.md).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L44)

<a id="member-_boundaryEndParent">

### `_boundaryEndParent: ViewNode | ViewDocumentFragment | null` _(private)_

End boundary parent.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L73)

<a id="member-_boundaryStartParent">

### `_boundaryStartParent: ViewNode | ViewDocumentFragment | null` _(private)_

Start boundary parent.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L68)

<a id="member-_position">

### `_position: ViewPosition` _(private)_

Iterator position. If start position is not defined then position depends on [`direction`](#member-direction). If direction is `'forward'` position starts form the beginning, when direction is `'backward'` position starts from the end.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L63)

<a id="methods">

## Methods

<a id="function-constructor">

### `constructor( options )`

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

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L80)

#### Parameters

* `options: ViewTreeWalkerOptions`

  Object with configuration.

  Defaults to `{}`

<a id="function-Symbol.iterator">

### `Symbol.iterator() → IterableIterator<ViewTreeWalkerValue>`

Iterable interface.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L122)

#### Returns

* `IterableIterator<ViewTreeWalkerValue>`

<a id="function-jumpTo">

### `jumpTo( position ) → void`

Moves tree walker [`position`](#member-position) to provided `position`. Tree walker will continue traversing from that position.

Note: in contrary to [`skip`](#function-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 use [`jumpTo`](#function-jumpTo) rather than [`skip`](#function-skip).

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.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L177)

#### Parameters

* `position: ViewPosition`

  Position to jump to.

#### Returns

* `void`

<a id="function-next">

### `next() → IteratorResult<ViewTreeWalkerValue, undefined>`

Gets the next tree walker's value.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L193)

#### Returns

* `IteratorResult<ViewTreeWalkerValue, undefined>`

  Object implementing iterator interface, returning information about taken step.

<a id="function-skip">

### `skip( skip ) → void`

Moves [`position`](#member-position) in the [`direction`](#member-direction) skipping values as long as the callback function returns `true`.

For example:

```typescript
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.
```

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L148)

#### Parameters

* `skip: ( value: ViewTreeWalkerValue ) => boolean`

  Callback function. Gets [`ViewTreeWalkerValue`](module_engine_view_treewalker-ViewTreeWalkerValue.md) and should return `true` if the value should be skipped or `false` if not.

#### Returns

* `void`

<a id="function-_formatReturnValue">

### `_formatReturnValue( type, item, previousPosition, nextPosition, length? ) → IteratorYieldResult<ViewTreeWalkerValue>` _(private)_

Format returned data and adjust `previousPosition` and `nextPosition` if reach the bound of the [`ViewText`](module_engine_view_text-ViewText.md).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L431)

#### 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>`

<a id="function-_next">

### `_next() → IteratorResult<ViewTreeWalkerValue, undefined>` _(private)_

Makes a step forward in view. Moves the [`position`](#member-position) to the next position and returns the encountered value.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L204)

#### Returns

* `IteratorResult<ViewTreeWalkerValue, undefined>`

<a id="function-_previous">

### `_previous() → IteratorResult<ViewTreeWalkerValue, undefined>` _(private)_

Makes a step backward in view. Moves the [`position`](#member-position) to the previous position and returns the encountered value.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/view/treewalker.ts#L314)

#### Returns

* `IteratorResult<ViewTreeWalkerValue, undefined>`

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
