Skip to content

Type Editor


Type Editor / @type-editor/selection-util / selection-collapsed / selectionCollapsed

Function: selectionCollapsed()

ts
function selectionCollapsed(domSel): boolean;

Defined in: selection-collapsed.ts:22

Checks if a DOM selection is collapsed (has no range).

Work around Chrome issue https://bugs.chromium.org/p/chromium/issues/detail?id=447523 (isCollapsed inappropriately returns true in shadow DOM)

This function uses isEquivalentPosition to properly detect collapsed selections by comparing the focus and anchor positions, which works correctly in Shadow DOM.

Parameters

ParameterTypeDescription
domSelDOMSelectionRangeThe DOM selection range to check

Returns

boolean

True if the selection is collapsed, false otherwise

Example

typescript
const selection = window.getSelection();
const isCollapsed = selectionCollapsed(selection);