pub struct Selection(/* private fields */);Expand description
Represents the range of text selected by the user or the current position of the caret. To obtain a Selection object for examination or modification, call Window.get_selection().
A user may make a selection from left to right (in document order) or right to left (reverse of document order). The anchor is where the user began the selection and the focus is where the user ends the selection. If you make a selection with a desktop mouse, the anchor is placed where you pressed the mouse button and the focus is placed where you released the mouse button. Anchor and focus should not be confused with the start and end positions of a selection, since anchor can be placed before the focus or vice versa, depending on the direction you made your selection.
Implementations§
Source§impl Selection
impl Selection
Sourcepub fn anchor_node(&self) -> Option<Node>
pub fn anchor_node(&self) -> Option<Node>
Returns the Node in which the selection begins.
A user may make a selection from left to right (in document order) or right to left (reverse of document order). The anchor is where the user began the selection. This can be visualized by holding the Shift key and pressing the arrow keys on your keyboard. The selection’s anchor does not move, but the selection’s focus, the other end of the selection, does move.
Sourcepub fn anchor_offset(&self) -> u32
pub fn anchor_offset(&self) -> u32
Returns the number of characters that the selection’s anchor is offset within the anchor_node.
This number is zero-based. If the selection begins with the first character in the anchor_node, 0 is returned.
Sourcepub fn focus_node(&self) -> Option<Node>
pub fn focus_node(&self) -> Option<Node>
Returns the Node in which the selection ends.
A user may make a selection from left to right (in document order) or right to left (reverse of document order). The focus is where the user ended the selection. This can be visualized by holding the Shift key and pressing the arrow keys on your keyboard to modify the current selection. The selection’s focus moves, but the selection’s anchor, the other end of the selection, does not move.
Sourcepub fn focus_offset(&self) -> u32
pub fn focus_offset(&self) -> u32
Returns the number of characters that the selection’s anchor is offset within the focus_node.
This number is zero-based. If the selection begins with the first character in the focus_node, 0 is returned.
Sourcepub fn is_collapsed(&self) -> bool
pub fn is_collapsed(&self) -> bool
Returns a boolean which indicates whether or not there is currently any text selected; That is to say that the selection’s start and end points are at the same position in the content.
Keep in mind that a collapsed selection may still have one (or more, in Gecko) Ranges, so range_count may not be zero. In that scenario, calling a Selection object’s get_range_at method may return a Range object which is collapsed.
Sourcepub fn range_count(&self) -> u32
pub fn range_count(&self) -> u32
Returns the number of ranges in the selection.
Before the user has clicked a freshly loaded page, the range_count is 0. After the user clicks on the page, range_count even if no selection is visible.
A user can normally only select one range at a time, so the range_count will usually be 1. Scripting can be used to make the selection contain more than 1 range.
Gecko browsers allow multiple selections across table cells.
Sourcepub fn kind(&self) -> SelectionType
pub fn kind(&self) -> SelectionType
Returns the type of the current selection.
Possible values are:
None: No selection has currently been made.Caret: The selection is collapsed (i.e. the caret is placed on some text, but no range has been selected).Range: A range has been selected.
Sourcepub fn get_range_at(&self, index: u32) -> Result<Range, IndexSizeError>
pub fn get_range_at(&self, index: u32) -> Result<Range, IndexSizeError>
Returns a Range object representing one of the ranges currently selected.
Sourcepub fn remove_range(&self, range: &Range) -> Result<(), NotFoundError>
pub fn remove_range(&self, range: &Range) -> Result<(), NotFoundError>
Sourcepub fn remove_all_ranges(&self)
pub fn remove_all_ranges(&self)
Removes all ranges from the Selection, leaving the
anchor_node and
focus_node properties equal to
None and leaving nothing selected.
Sourcepub fn collapse<N: INode>(&self, node: &N)
pub fn collapse<N: INode>(&self, node: &N)
Collapses the Selection to a single point. The document is not modified. If the content is focused or editable, the caret will blink there.
Sourcepub fn collapse_with_offset<N: INode>(
&self,
node: &N,
offset: Option<u32>,
) -> Result<(), IndexSizeError>
pub fn collapse_with_offset<N: INode>( &self, node: &N, offset: Option<u32>, ) -> Result<(), IndexSizeError>
Collapses the Selection to a single point. The document is not modified. If the content is focused or editable, the caret will blink there.
Sourcepub fn collapse_to_start(&self) -> Result<(), InvalidStateError>
pub fn collapse_to_start(&self) -> Result<(), InvalidStateError>
Collapses the Selection to the start of the first range in the selection. If the content is focused or editable, the caret will blink there.
Sourcepub fn collapse_to_end(&self) -> Result<(), InvalidStateError>
pub fn collapse_to_end(&self) -> Result<(), InvalidStateError>
Collapses the Selection to the end of the last range in the selection. If the content is focused or editable, the caret will blink there.
Sourcepub fn extend<N: INode>(
&self,
node: &N,
offset: Option<u32>,
) -> Result<(), InvalidStateError>
pub fn extend<N: INode>( &self, node: &N, offset: Option<u32>, ) -> Result<(), InvalidStateError>
Moves the focus of the selection to a specified point. The anchor of the selection does not move. The selection will be from the anchor node to the new focus regardless of direction.
Sourcepub fn set_base_and_extent<N: INode, M: INode>(
&self,
anchor_node: &N,
anchor_offset: u32,
focus_node: &M,
focus_offset: u32,
) -> Result<(), IndexSizeError>
pub fn set_base_and_extent<N: INode, M: INode>( &self, anchor_node: &N, anchor_offset: u32, focus_node: &M, focus_offset: u32, ) -> Result<(), IndexSizeError>
Sets the selection to be a range including all or parts of the two specified Nodes, and any content located between them.
Sourcepub fn select_all_children<N: INode>(&self, node: &N)
pub fn select_all_children<N: INode>(&self, node: &N)
Adds all the children of the specified Node to the selection. Previous selection is lost.
Sourcepub fn delete_from_document(&self)
pub fn delete_from_document(&self)
Deletes the actual text being represented by the Selection from the document’s DOM.
Sourcepub fn contains_whole<N: INode>(&self, node: &N) -> bool
pub fn contains_whole<N: INode>(&self, node: &N) -> bool
Indicates if the entire node is part of the selection.
Sourcepub fn contains_part_of<N: INode>(&self, node: &N) -> bool
pub fn contains_part_of<N: INode>(&self, node: &N) -> bool
Indicates if atleast some of the node is part of the selection.