# utils/dom/getselection

module

<a id="classes">

## Classes

<a id="class-ShadowSelection">

### [ShadowSelection](module_utils_dom_getselection-ShadowSelection.md)

<a id="functions">

## Functions

<a id="function-getSelection">

### `getSelection( nodeOrNodes ) → Selection | ShadowSelection | null`

Returns the selection of the tree the given node lives in.

Use it instead of `window.getSelection()`, which cannot see into a shadow root: its `anchorNode` and `focusNode` point at the host of the tree, not at the nodes selected inside it.

Pass the node the selection is read for, usually an editable. Pass several nodes to cover several editables at once. A node in the light DOM gives the document selection, unchanged. Works with open and closed shadow roots.

The result behaves like a `Selection` – the part of one the editor uses – so read it and change it the same way. It is `null` when there is no selection to read: a detached node, or a document with no window.

One thing this helper cannot fix. In Safari and Chromium browsers, a selection made with the mouse inside a shadow root reports no direction, so a backward selection may look forward. Chromium is asked through its own API where possible. Safari has none.

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

#### Parameters

* `nodeOrNodes: ArrayOrItem<Node>`

  The node (or nodes) whose tree's selection should be returned.

#### Returns

* `Selection | ShadowSelection | null`

---

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