Struct Selection

Source
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.

(Javascript docs)

Implementations§

Source§

impl Selection

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

pub fn get_range_at(&self, index: u32) -> Result<Range, IndexSizeError>

Returns a Range object representing one of the ranges currently selected.

(Javascript docs)

Source

pub fn add_range(&self, range: &Range)

Source

pub fn remove_range(&self, range: &Range) -> Result<(), NotFoundError>

Removes a Range from the Selection.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

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.

(Javascript docs)

Source

pub fn delete_from_document(&self)

Deletes the actual text being represented by the Selection from the document’s DOM.

(Javascript docs)

Source

pub fn contains_whole<N: INode>(&self, node: &N) -> bool

Indicates if the entire node is part of the selection.

(Javascript docs)

Source

pub fn contains_part_of<N: INode>(&self, node: &N) -> bool

Indicates if atleast some of the node is part of the selection.

(Javascript docs)

Trait Implementations§

Source§

impl AsRef<Reference> for Selection

Source§

fn as_ref(&self) -> &Reference

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Selection

Source§

fn clone(&self) -> Selection

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Selection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Selection> for Reference

Source§

fn from(value: Selection) -> Self

Converts to this type from the input type.
Source§

impl InstanceOf for Selection

Source§

fn instance_of(reference: &Reference) -> bool

Checks whenever a given Reference if of type Self.
Source§

impl PartialEq for Selection

Source§

fn eq(&self, other: &Selection) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl ReferenceType for Selection

Source§

unsafe fn from_reference_unchecked(reference: Reference) -> Self

Converts a given reference into a concrete reference-like wrapper. Doesn’t do any type checking; highly unsafe to use!
Source§

impl<'_r> TryFrom<&'_r Reference> for Selection

Source§

type Error = ConversionError

The type returned in the event of a conversion error.
Source§

fn try_from(reference: &Reference) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'_r> TryFrom<&'_r Value> for Selection

Source§

type Error = ConversionError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &Value) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Reference> for Selection

Source§

type Error = ConversionError

The type returned in the event of a conversion error.
Source§

fn try_from(reference: Reference) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Selection> for Reference

Source§

type Error = Void

The type returned in the event of a conversion error.
Source§

fn try_from(value: Selection) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Value> for Selection

Source§

type Error = ConversionError

The type returned in the event of a conversion error.
Source§

fn try_from(value: Value) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for Selection

Source§

impl JsSerialize for Selection

Source§

impl StructuralPartialEq for Selection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.