# ModelTreeWalker

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/model/treewalker.ts#L28)

<a id="properties">

## Properties

<a id="member-boundaries">

### `boundaries: ModelRange | 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/model/treewalker.ts#L42)

<a id="member-direction">

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

Walking direction. Defaults `'forward'`.

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

<a id="member-ignoreElementEnd">

### `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 [`ModelElement`](module_engine_model_element-ModelElement.md) will be returned once, while if the option is `false` they might be returned twice: for `'elementStart'` and `'elementEnd'`.

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

<a id="member-position">

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

Iterator position. This is always static position, even if the initial position was a [live position](module_engine_model_liveposition-ModelLivePosition.md). 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/model/treewalker.ts#L152)

<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/model/treewalker.ts#L54)

<a id="member-singleCharacters">

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

Flag indicating whether all consecutive characters with the same attributes should be returned as one [`ModelTextProxy`](module_engine_model_textproxy-ModelTextProxy.md) (`true`) or one by one (`false`).

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

<a id="member-_boundaryEndParent">

### `_boundaryEndParent: ModelElement | ModelDocumentFragment | null` _(private)_

End boundary cached for optimization purposes.

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

<a id="member-_boundaryStartParent">

### `_boundaryStartParent: ModelElement | ModelDocumentFragment | null` _(private)_

Start boundary cached for optimization purposes.

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

<a id="member-_position">

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

Iterator position. This is always static position, even if the initial position was a [live position](module_engine_model_liveposition-ModelLivePosition.md). 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/model/treewalker.ts#L70)

<a id="member-_visitedParent">

### `_visitedParent: ModelElement | ModelDocumentFragment` _(private)_

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

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

<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/model/treewalker.ts#L92)

#### Parameters

* `options: ModelTreeWalkerOptions`

  Object with configuration.

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

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

Iterable interface.

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

#### Returns

* `IterableIterator<ModelTreeWalkerValue>`

<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/model/treewalker.ts#L201)

#### Parameters

* `position: ModelPosition`

  Position to jump to.

#### Returns

* `void`

<a id="function-next">

### `next() → IteratorResult<ModelTreeWalkerValue>`

Gets the next tree walker's value.

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

#### Returns

* `IteratorResult<ModelTreeWalkerValue>`

<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' ); // <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.
```

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

#### Parameters

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

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

#### Returns

* `void`

<a id="function-_next">

### `_next() → IteratorResult<ModelTreeWalkerValue>` _(private)_

Makes a step forward in model. 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/model/treewalker.ts#L226)

#### Returns

* `IteratorResult<ModelTreeWalkerValue>`

<a id="function-_previous">

### `_previous() → IteratorResult<ModelTreeWalkerValue>` _(private)_

Makes a step backward in model. 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/model/treewalker.ts#L306)

#### Returns

* `IteratorResult<ModelTreeWalkerValue>`

---

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