# ShadowSelection

class

A `Selection` view over the composed ranges of a set of shadow roots. Read accessors are resolved live from the composed ranges on each access, so the view stays in sync with the underlying document selection; selection mutations are delegated to it. Only the subset of the `Selection` interface consumed by the editor is implemented, so `getSelection()` returns it alongside the native `Selection`.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L121)

<a id="properties">

## Properties

<a id="member-anchorNode">

### `anchorNode: Node | null` _(readonly)_

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L158)

<a id="member-anchorOffset">

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

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L162)

<a id="member-direction">

### `direction: 'none' | 'forward' | 'backward'` _(readonly)_

The direction of the selection, resolved against the tree the first composed range lives in. It is already computed to orient the anchor and focus endpoints, so exposing it lets consumers read the orientation directly instead of re-deriving it from the endpoints.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L179)

<a id="member-focusNode">

### `focusNode: Node | null` _(readonly)_

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L166)

<a id="member-focusOffset">

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

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L170)

<a id="member-isCollapsed">

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

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L152)

<a id="member-rangeCount">

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

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L148)

<a id="member-_domSelection">

### `_domSelection: ComposedSelection` _(private)_

The underlying document-level selection that mutations are delegated to and composed ranges read from.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L126)

<a id="member-_shadowRoots">

### `_shadowRoots: Array<ShadowRoot>` _(private)_

The shadow roots the composed ranges are resolved against.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L131)

#### Static properties

<a id="static-member-_useLegacyComposedRanges">

### `_useLegacyComposedRanges: boolean` _(private)_

Whether the engine only accepts the legacy rest-parameter form of `getComposedRanges()`. This is a fixed property of the engine, so it is detected once (the first call throws on the dictionary form) and cached for all instances, keeping the exception off the selection-read hot path.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L138)

<a id="methods">

## Methods

<a id="function-constructor">

### `constructor( domSelection, shadowRoots )` _(internal)_

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L143)

#### Parameters

* `domSelection: Selection`
* `shadowRoots: Array<ShadowRoot>`

<a id="function-addRange">

### `addRange( range ) → void`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L212)

#### Parameters

* `range: Range`

#### Returns

* `void`

<a id="function-collapse">

### `collapse( node, offset? ) → void`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L216)

#### Parameters

* `node: Node | null`
* `offset?: number`

#### Returns

* `void`

<a id="function-extend">

### `extend( node, offset? ) → void`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L220)

#### Parameters

* `node: Node`
* `offset?: number`

#### Returns

* `void`

<a id="function-getRangeAt">

### `getRangeAt( index ) → Range`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L189)

#### Parameters

* `index: number`

#### Returns

* `Range`

<a id="function-removeAllRanges">

### `removeAllRanges() → void`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L208)

#### Returns

* `void`

<a id="function-setBaseAndExtent">

### `setBaseAndExtent( anchorNode, anchorOffset, focusNode, focusOffset ) → void`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L224)

#### Parameters

* `anchorNode: Node`
* `anchorOffset: number`
* `focusNode: Node`
* `focusOffset: number`

#### Returns

* `void`

<a id="function-_getComposedRanges">

### `_getComposedRanges() → Array<StaticRange>` _(private)_

The raw composed ranges reported for the shadow roots by the underlying selection.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L267)

#### Returns

* `Array<StaticRange>`

<a id="function-_getDirection">

### `_getDirection( rootNode ) → string` _(private)_

The direction of the underlying selection, preferring the one reported by Blink's shadow-scoped selection, which stays meaningful for a mouse selection made inside a shadow root.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L292)

#### Parameters

* `rootNode: Node`

#### Returns

* `string`

<a id="function-_getEndpoint">

### `_getEndpoint( endpoint ) → object` _(private)_

The anchor or focus endpoint of the first composed range.

`getComposedRanges()` returns ranges in document order (start before end); the anchor/focus orientation therefore has to come from `Selection#direction`. Known limitation: WebKit and Blink report `direction` as `'none'` for a selection made _with the mouse_ inside a shadow root (a keyboard selection reports it correctly). Blink's shadow-scoped selection still knows the direction in that case, so it is consulted first; it is of no use for the ranges themselves, as it covers a single tree only. Safari has no such API, so its mouse selections fall back to forward.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L326)

#### Parameters

* `endpoint: 'anchor' | 'focus'`

#### Returns

* `object`

<a id="function-_getRanges">

### `_getRanges() → Array<StaticRange>` _(private)_

The composed ranges fully contained in one of the trees on the path from the given nodes to the document, in document order (start before end).

Only ranges whose both endpoints resolve into the same tree can be represented as a live, single-tree `Range`. A range crossing a shadow boundary is therefore dropped, and so is one resolving into an unrelated tree, rather than surfacing foreign nodes. The document itself is one of those trees: it is where every node ends up when walking out of its shadow roots, and it is where a selection made in a sibling light-DOM editable lives, so such a selection is kept rather than dropped.

A range with an endpoint outside its container is dropped as well, see `isOffsetInNode()` below.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-utils/src/dom/getselection.ts#L240)

#### Returns

* `Array<StaticRange>`

---

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