# ModelSchemaContext

class

A schema context – a list of ancestors of a given position in the document.

Considering such position:

```xml
<$root>
	<blockQuote>
		<paragraph>
			^
		</paragraph>
	</blockQuote>
</$root>
```

The context of this position is its [lists of ancestors](module_engine_model_position-ModelPosition.md#function-getAncestors):

```
	[ rootElement, blockQuoteElement, paragraphElement ]
```

Contexts are used in the [`Schema#checkChild`](module_engine_model_schema-ModelSchema.md#event-checkChild) and [`Schema#checkAttribute`](module_engine_model_schema-ModelSchema.md#event-checkAttribute) events as a definition of a place in the document where the check occurs. The context instances are created based on the first arguments of the [`Schema#checkChild()`](module_engine_model_schema-ModelSchema.md#function-checkChild) and [`Schema#checkAttribute()`](module_engine_model_schema-ModelSchema.md#function-checkAttribute) methods so when using these methods you need to use [`ModelSchemaContextDefinition`](module_engine_model_schema-ModelSchemaContextDefinition.md)s.

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

<a id="properties">

## Properties

<a id="member-last">

### `last: ModelSchemaContextItem` _(readonly)_

The last item (the lowest node).

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

<a id="member-length">

### `length: number` _(readonly)_

The number of items.

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

<a id="member-_items">

### `_items: Array<ModelSchemaContextItem>` _(private)_

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

<a id="methods">

## Methods

<a id="function-constructor">

### `constructor( context )`

Creates an instance of the context.

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

#### Parameters

* `context: ModelSchemaContextDefinition`

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

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

Iterable interface.

Iterates over all context items.

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

#### Returns

* `IterableIterator<ModelSchemaContextItem>`

<a id="function-endsWith">

### `endsWith( query ) → boolean`

Checks whether the context ends with the given nodes.

```typescript
const ctx = new ModelSchemaContext( [ rootElement, paragraphElement, textNode ] );

ctx.endsWith( '$text' ); // -> true
ctx.endsWith( 'paragraph $text' ); // -> true
ctx.endsWith( '$root' ); // -> false
ctx.endsWith( 'paragraph' ); // -> false
```

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

#### Parameters

* `query: string`

#### Returns

* `boolean`

<a id="function-getItem">

### `getItem( index ) → ModelSchemaContextItem`

Gets an item on the given index.

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

#### Parameters

* `index: number`

#### Returns

* `ModelSchemaContextItem`

<a id="function-getNames">

### `getNames() → IterableIterator<string>`

Returns the names of items.

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

#### Returns

* `IterableIterator<string>`

<a id="function-push">

### `push( item ) → ModelSchemaContext`

Returns a new schema context instance with an additional item.

Item can be added as:

```typescript
const context = new ModelSchemaContext( [ '$root' ] );

// An element.
const fooElement = writer.createElement( 'fooElement' );
const newContext = context.push( fooElement ); // [ '$root', 'fooElement' ]

// A text node.
const text = writer.createText( 'foobar' );
const newContext = context.push( text ); // [ '$root', '$text' ]

// A string (element name).
const newContext = context.push( 'barElement' ); // [ '$root', 'barElement' ]
```

**Note** [`ModelNode`](module_engine_model_node-ModelNode.md) that is already in the model tree will be added as the only item (without ancestors).

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

#### Parameters

* `item: string | ModelNode`

  An item that will be added to the current context.

#### Returns

* `ModelSchemaContext`

  A new schema context instance with an additional item.

<a id="function-startsWith">

### `startsWith( query ) → boolean`

Checks whether the context starts with the given nodes.

```typescript
const ctx = new ModelSchemaContext( [ rootElement, paragraphElement, textNode ] );

ctx.endsWith( '$root' ); // -> true
ctx.endsWith( '$root paragraph' ); // -> true
ctx.endsWith( '$text' ); // -> false
ctx.endsWith( 'paragraph' ); // -> false
```

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

#### Parameters

* `query: string`

#### Returns

* `boolean`

<a id="function-trimLast">

### `trimLast() → ModelSchemaContext`

Returns a new schema context that is based on this context but has the last item removed.

```typescript
const ctxParagraph = new ModelSchemaContext( [ '$root', 'blockQuote', 'paragraph' ] );
const ctxBlockQuote = ctxParagraph.trimLast(); // Items in `ctxBlockQuote` are: `$root` an `blockQuote`.
const ctxRoot = ctxBlockQuote.trimLast(); // Items in `ctxRoot` are: `$root`.
```

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

#### Returns

* `ModelSchemaContext`

  A new reduced schema context instance.

---

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