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 SelectionProperties
| Name | Description |
|---|---|
| AnchorNode | Gets the node in which the selection starts, or null when there is no range in the document tree. |
| AnchorOffset | Gets the offset of the anchor of the selection within the anchor node. |
| Direction | "none", "forward" or "backward". |
| FocusNode | Gets the node in which the selection ends, or null when there is no range in the document tree. |
| FocusOffset | Gets the offset of the focus of the selection within the focus node. |
| IsCollapsed | Whether the anchor and the focus are the same, including when the selection is empty. |
| RangeCount | Gets the number of ranges in the selection (0 or 1). |
| Type | "None", "Caret" or "Range". |
AnchorNode
Selection.csGets the node in which the selection starts, or null when there is no range in the document tree.
public Node? AnchorNode { get; }AnchorOffset
Selection.csGets 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.csGets the node in which the selection ends, or null when there is no range in the document tree.
public Node? FocusNode { get; }FocusOffset
Selection.csGets the offset of the focus of the selection within the focus node.
public uint FocusOffset { get; }IsCollapsed
Selection.csWhether the anchor and the focus are the same, including when the selection is empty.
public bool IsCollapsed { get; }RangeCount
Selection.csGets the number of ranges in the selection (0 or 1).
public uint RangeCount { get; }Type
Selection.cs"None", "Caret" or "Range".
public string Type { get; }Methods
| Name | Description |
|---|---|
| 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.csAssociates the range (not a copy) with the selection, when the selection is empty.
Collapse(Node?, uint)
Selection.csCollapseToEnd()
Selection.csCollapses the selection to the end of its range.
public void CollapseToEnd()CollapseToStart()
Selection.csCollapses the selection to the start of its range.
public void CollapseToStart()ContainsNode(Node, bool)
Selection.csWhether 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.
Parameters
Returns
See also
DeleteFromDocument()
Selection.csDeletes the contents of the range of the selection (the range is mutated, not replaced).
public void DeleteFromDocument()Empty()
Selection.csRemoves all the ranges from the selection. This is an alias of removeAllRanges().
public void Empty()Extend(Node, uint)
Selection.csGetComposedRanges(GetComposedRangesOptions?)
Selection.csReturns 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.csReturns the range of the selection (not a copy).
Modify(string?, string?, string?)
Selection.Modify.csMoves 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.
Parameters
See also
RemoveAllRanges()
Selection.csRemoves all the ranges from the selection.
public void RemoveAllRanges()RemoveRange(Range)
Selection.csRemoves the given range from the selection.
SelectAllChildren(Node)
Selection.csSets the selection to contain all the children of the given node.
SetBaseAndExtent(Node, uint, Node, uint)
Selection.csSets the anchor and the focus of the selection to the given nodes and offsets.