Selection Class

The selection of a document (https://w3c.github.io/selection-api/#selection-interface): at most one live Range and a direction. The selection changes through this API (modify() moves it over the layout as the keyboard does), the Range methods, the DOM mutations, and the caret movement keys in editable content.

public sealed class Selection

Properties

NameDescription
AnchorNodeGets the node in which the selection starts, or null when there is no range in the document tree.
AnchorOffsetGets the offset of the anchor of the selection within the anchor node.
Direction"none", "forward" or "backward".
FocusNodeGets the node in which the selection ends, or null when there is no range in the document tree.
FocusOffsetGets the offset of the focus of the selection within the focus node.
IsCollapsedWhether the anchor and the focus are the same, including when the selection is empty.
RangeCountGets the number of ranges in the selection (0 or 1).
Type"None", "Caret" or "Range".

AnchorNode

Selection.cs

Gets the node in which the selection starts, or null when there is no range in the document tree.

public Node? AnchorNode { get; }

AnchorOffset

Selection.cs

Gets the offset of the anchor of the selection within the anchor node.

public uint AnchorOffset { get; }

Direction

Selection.cs

"none", "forward" or "backward".

public string Direction { get; }

FocusNode

Selection.cs

Gets the node in which the selection ends, or null when there is no range in the document tree.

public Node? FocusNode { get; }

FocusOffset

Selection.cs

Gets the offset of the focus of the selection within the focus node.

public uint FocusOffset { get; }

IsCollapsed

Selection.cs

Whether the anchor and the focus are the same, including when the selection is empty.

public bool IsCollapsed { get; }

RangeCount

Selection.cs

Gets the number of ranges in the selection (0 or 1).

public uint RangeCount { get; }

"None", "Caret" or "Range".

public string Type { get; }

Methods

NameDescription
AddRange(Range)Associates the range (not a copy) with the selection, when the selection is empty.
Collapse(Node?, uint)Collapses the selection to the boundary point, or empties it when node is null.
CollapseToEnd()Collapses the selection to the end of its range.
CollapseToStart()Collapses the selection to the start of its range.
ContainsNode(Node, bool)Whether the node is selected (or partially selected when allowPartialContainment is true). There is no rendering, so boundary points are never visually equivalent: the boundary points are compared.
DeleteFromDocument()Deletes the contents of the range of the selection (the range is mutated, not replaced).
Empty()Removes all the ranges from the selection. This is an alias of removeAllRanges().
Extend(Node, uint)Moves the focus of the selection to the boundary point, keeping the anchor.
GetComposedRanges(GetComposedRangesOptions?)Returns the range of the selection, with the boundary points in the shadow trees that are not in options rescoped to their shadow host.
GetRangeAt(uint)Returns the range of the selection (not a copy).
Modify(string?, string?, string?)Moves the focus of the selection (alter extend), or the selection collapsed at its focus (move), by a character, a word, a sentence, a line or a paragraph, or to a line, sentence, paragraph or document boundary, in the direction direction (forward, backward, left or right), as the user would with the keyboard. The movements follow the layout: see CaretNavigator.
RemoveAllRanges()Removes all the ranges from the selection.
RemoveRange(Range)Removes the given range from the selection.
SelectAllChildren(Node)Sets the selection to contain all the children of the given node.
SetBaseAndExtent(Node, uint, Node, uint)Sets the anchor and the focus of the selection to the given nodes and offsets.
SetPosition(Node?, uint)Collapses the selection to the given node and offset. This is an alias of collapse().
ToString()The stringification behavior (https://w3c.github.io/selection-api/#dom-selection-stringifier): the rendered text of the range (the innerText of its content, see RenderedText), or its text when it is not rendered.

AddRange(Range)

Selection.cs

Associates the range (not a copy) with the selection, when the selection is empty.

public void AddRange(Range range)

Collapse(Node?, uint)

Selection.cs

Collapses the selection to the boundary point, or empties it when node is null.

public void Collapse(Node? node, uint offset = 0U)

CollapseToEnd()

Selection.cs

Collapses the selection to the end of its range.

public void CollapseToEnd()

CollapseToStart()

Selection.cs

Collapses the selection to the start of its range.

public void CollapseToStart()

ContainsNode(Node, bool)

Selection.cs

Whether the node is selected (or partially selected when allowPartialContainment is true). There is no rendering, so boundary points are never visually equivalent: the boundary points are compared.

public bool ContainsNode(Node node, bool allowPartialContainment = false)

Parameters

node Node
allowPartialContainment bool

Returns

bool

See also

DeleteFromDocument()

Selection.cs

Deletes the contents of the range of the selection (the range is mutated, not replaced).

public void DeleteFromDocument()

Removes all the ranges from the selection. This is an alias of removeAllRanges().

public void Empty()

Extend(Node, uint)

Selection.cs

Moves the focus of the selection to the boundary point, keeping the anchor.

public void Extend(Node node, uint offset = 0U)

GetComposedRanges(GetComposedRangesOptions?)

Selection.cs

Returns the range of the selection, with the boundary points in the shadow trees that are not in options rescoped to their shadow host.

public IReadOnlyList<StaticRange> GetComposedRanges(GetComposedRangesOptions? options = null)

GetRangeAt(uint)

Selection.cs

Returns the range of the selection (not a copy).

public Range GetRangeAt(uint index)

Modify(string?, string?, string?)

Selection.Modify.cs

Moves the focus of the selection (alter extend), or the selection collapsed at its focus (move), by a character, a word, a sentence, a line or a paragraph, or to a line, sentence, paragraph or document boundary, in the direction direction (forward, backward, left or right), as the user would with the keyboard. The movements follow the layout: see CaretNavigator.

public void Modify(string? alter = null, string? direction = null, string? granularity = null)

RemoveAllRanges()

Selection.cs

Removes all the ranges from the selection.

public void RemoveAllRanges()

RemoveRange(Range)

Selection.cs

Removes the given range from the selection.

public void RemoveRange(Range range)

SelectAllChildren(Node)

Selection.cs

Sets the selection to contain all the children of the given node.

public void SelectAllChildren(Node node)

SetBaseAndExtent(Node, uint, Node, uint)

Selection.cs

Sets the anchor and the focus of the selection to the given nodes and offsets.

public void SetBaseAndExtent(Node anchorNode, uint anchorOffset, Node focusNode, uint focusOffset)

Parameters

anchorNode Node
anchorOffset uint
focusNode Node
focusOffset uint

See also

SetPosition(Node?, uint)

Selection.cs

Collapses the selection to the given node and offset. This is an alias of collapse().

public void SetPosition(Node? node, uint offset = 0U)

ToString()

Selection.cs

The stringification behavior (https://w3c.github.io/selection-api/#dom-selection-stringifier): the rendered text of the range (the innerText of its content, see RenderedText), or its text when it is not rendered.

public override string ToString()

Returns

string