# engine/model/utils/selection-post-fixer

module

<a id="functions">

## Functions

<a id="function-injectSelectionPostFixer">

### `injectSelectionPostFixer( model ) → void` _(internal)_

Injects selection post-fixer to the model.

The role of the selection post-fixer is to ensure that the selection is in a correct place after a [`change()`](module_engine_model_model-Model.md#function-change) block was executed.

The correct position means that:

* All collapsed selection ranges are in a place where the [`ModelSchema`](module_engine_model_schema-ModelSchema.md) allows a `$text`.
* None of the selection's non-collapsed ranges crosses a [limit element](module_engine_model_schema-ModelSchema.md#function-isLimit) boundary (a range must be rooted within one limit element).
* Only [selectable elements](module_engine_model_schema-ModelSchema.md#function-isSelectable) can be selected from the outside (e.g. `[<paragraph>foo</paragraph>]` is invalid). This rule applies independently to both selection ends, so this selection is correct: `<paragraph>f[oo</paragraph><imageBlock></imageBlock>]`.

If the position is not correct, the post-fixer will automatically correct it.

##### Fixing a non-collapsed selection

See as an example a selection that starts in a P1 element and ends inside the text of a TD element (`[` and `]` are range boundaries and `(l)` denotes an element defined as `isLimit=true`):

```
root
 |- element P1
 |   |- "foo"                                      root
 |- element TABLE (l)                   P1         TABLE             P2
 |   |- element TR (l)                 f o[o     TR      TR         b a r
 |   |   |- element TD (l)                       TD      TD
 |   |       |- "aaa"                          a]a a    b b b
 |   |- element TR (l)
 |   |   |- element TD (l)                           ||
 |   |       |- "bbb"                                ||
 |- element P2                                       VV
 |   |- "bar"
                                                   root
                                        P1         TABLE]            P2
                                       f o[o     TR      TR         b a r
                                                 TD      TD
                                               a a a    b b b
```

In the example above, the TABLE, TR and TD are defined as `isLimit=true` in the schema. The range which is not contained within a single limit element must be expanded to select the outermost limit element. The range end is inside the text node of the TD element. As the TD element is a child of the TR and TABLE elements, where both are defined as `isLimit=true` in the schema, the range must be expanded to select the whole TABLE element.

**Note** If the selection contains multiple ranges, the method returns a minimal set of ranges that are not intersecting after expanding them to select `isLimit=true` elements.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/model/utils/selection-post-fixer.ts#L73)

#### Parameters

* `model: Model`

#### Returns

* `void`

<a id="function-mergeIntersectingRanges">

### `mergeIntersectingRanges( ranges ) → Array<ModelRange>` _(internal)_

Returns a minimal non-intersecting array of ranges without duplicates.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/model/utils/selection-post-fixer.ts#L282)

#### Parameters

* `ranges: Array<ModelRange>`

  Ranges to merge.

#### Returns

* `Array<ModelRange>`

  Array of unique and non-intersecting ranges.

<a id="function-tryFixingRange">

### `tryFixingRange( range, schema ) → ModelRange | null` _(internal)_

Tries fixing a range if it's incorrect.

**Note:** This helper is used by the selection post-fixer and to fix the `beforeinput` target ranges.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/model/utils/selection-post-fixer.ts#L124)

#### Parameters

* `range: ModelRange`
* `schema: ModelSchema`

#### Returns

* `ModelRange | null`

  Returns fixed range or null if range is valid.

---

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