Skip to main content

Selection

Struct Selection 

Source
pub struct Selection<'a> { /* private fields */ }
Expand description

Selection represents a collection of nodes matching some criteria. The initial Selection object can be created by using Document::select, and then manipulated using methods itself.

Implementations§

Source§

impl Selection<'_>

Source

pub fn attr(&self, name: &str) -> Option<Tendril<UTF8>>

Gets the specified attribute’s value for the first element in the selection. To get the value for each element individually, use a looping construct such as map method.

Source

pub fn attrs(&self) -> Vec<Attribute>

Gets all attributes` values for the first element in the selection. To get the value for each element individually, use a looping construct such as map method.

Source

pub fn has_attr(&self, name: &str) -> bool

Checks if the first element in the selection has an attribute with the name.

Source

pub fn attr_or(&self, name: &str, default: &str) -> Tendril<UTF8>

Works like attr but returns default value if attribute is not present.

Source

pub fn set_attr(&self, name: &str, val: &str)

Sets the given attribute to each element in the set of matched elements.

Source

pub fn remove_attr(&self, name: &str)

Removes the named attribute from each element in the set of matched elements.

Source

pub fn remove_attrs(&self, names: &[&str])

Removes named attributes from each element in the set of matched elements.

§Arguments
  • names - A list of attribute names to remove. Empty slice removes no attributes.
Source

pub fn retain_attrs(&self, names: &[&str])

Retains only the attributes with the specified names from each element in the set of matched elements.

§Arguments
  • names - A list of attribute names to retain. Empty slice retains no attributes.
Source

pub fn remove_all_attrs(&self)

Removes all attributes from each element in the set of matched elements.

Source

pub fn rename(&self, name: &str)

Renames tag of each element in the set of matched elements.

Source

pub fn strip_elements(&self, names: &[&str])

Removes matching elements from the descendants, but keeps their children (if any) in the tree.

Unlike Self::remove, this method only deletes the elements themselves, promoting their children to the parent level, thus preserving the nested structure of the remaining nodes.

§Arguments
  • names - A list of element names to strip.
Source

pub fn id(&self) -> Option<Tendril<UTF8>>

Returns the id of the first element in the set of matched elements.

Source

pub fn class(&self) -> Option<Tendril<UTF8>>

Returns the class name of the first element in the set of matched elements.

Source

pub fn add_class(&self, class: &str)

Adds the given class to each element in the set of matched elements. Multiple class names can be specified, separated by a space via multiple arguments.

Source

pub fn has_class(&self, class: &str) -> bool

Determines whether any of the matched elements are assigned the given class.

Source

pub fn remove_class(&self, class: &str)

Removes the given class from each element in the set of matched elements. Multiple class names can be specified, separated by a space via multiple arguments.

Source

pub fn length(&self) -> usize

Returns the number of elements in the selection object.

Source

pub fn size(&self) -> usize

Is an alias for length.

Source

pub fn exists(&self) -> bool

Is there any matched elements.

Source

pub fn is_empty(&self) -> bool

Source

pub fn html(&self) -> Tendril<UTF8>

Gets the HTML contents of the first element in the set of matched elements. It includes the first matching element and its children nodes.

Source

pub fn inner_html(&self) -> Tendril<UTF8>

Gets the HTML contents of the first element in the set of matched elements. It includes only children nodes.

Source

pub fn try_html(&self) -> Option<Tendril<UTF8>>

Gets the HTML contents of the first element in the set of matched elements. It includes the first matching element and its children nodes.

Source

pub fn try_inner_html(&self) -> Option<Tendril<UTF8>>

Gets the HTML contents of the first element in the set of matched elements. It includes only children nodes.

Source

pub fn text(&self) -> Tendril<UTF8>

Gets the combined text content of each element in the set of matched elements, including their descendants.

Source

pub fn immediate_text(&self) -> Tendril<UTF8>

Gets the combined text content of each element in the set of matched, without their descendants.

Source

pub fn formatted_text(&self) -> Tendril<UTF8>

Returns the formatted text of the selected nodes and their descendants. This is the same as the text() method, but with a few differences:

  • Whitespace is normalized so that there is only one space between words.
  • All whitespace is removed from the beginning and end of the text.
  • After block elements, a double newline is added.
  • For elements like br, ‘hr’, ‘li’, ‘tr’ a single newline is added.
Source§

impl<'a> Selection<'a>

Source

pub fn is(&self, sel: &str) -> bool

Checks the current matched set of elements against a selector and returns true if at least one of these elements matches.

Source

pub fn is_matcher(&self, matcher: &Matcher) -> bool

Checks the current matched set of elements against a matcher and returns true if at least one of these elements matches.

Source

pub fn is_selection(&self, other: &Selection<'_>) -> bool

Checks the current matches set of elements against a selection and returns true if at least one of these elements matches.

Source

pub fn filter(&self, sel: &str) -> Selection<'a>

Filters the current set of matched elements to those that match the given CSS selector.

§Panics
§Arguments
  • sel - The CSS selector to match against.
§Returns

A new Selection object containing the matched elements.

Source

pub fn try_filter(&self, sel: &str) -> Option<Selection<'a>>

Reduces the current set of matched elements to those that match the given CSS selector.

§Arguments
  • sel - The CSS selector to match against.
§Returns

None if the selector was invalid, otherwise a new Selection object containing the matched elements.

Source

pub fn filter_matcher(&self, matcher: &Matcher) -> Selection<'a>

Reduces the current set of matched elements to those that match the given matcher.

§Arguments
  • matcher - The matcher to match against.
§Returns

A new Selection object containing the matched elements.

Source

pub fn filter_selection(&self, other: &Selection<'_>) -> Selection<'a>

Reduces the set of matched elements to those that match a node in the specified Selection. It returns a new Selection for this subset of elements.

Source

pub fn add(&self, sel: &str) -> Selection<'a>

Adds nodes that match the given CSS selector to the current selection.

§Panics

If matcher contains invalid CSS selector it panics.

§Arguments
  • sel - The CSS selector to match against.
§Returns

The new Selection containing the original nodes and the new nodes.

Source

pub fn try_add(&self, sel: &str) -> Option<Selection<'_>>

Adds nodes that match the given CSS selector to the current selection.

If matcher contains invalid CSS selector it returns None.

§Arguments
  • sel - The CSS selector to match against.
§Returns

The new Selection containing the original nodes and the new nodes.

Source

pub fn add_matcher(&self, matcher: &Matcher) -> Selection<'a>

Adds nodes that match the given matcher to the current selection.

§Arguments
  • matcher - The matcher to match against.
§Returns

The new Selection containing the original nodes and the new nodes.

Source

pub fn add_selection(&self, other: &'a Selection<'_>) -> Selection<'a>

Adds a selection to the current selection.

Behaves like Union for sets.

§Arguments
  • other - The selection to add to the current selection.
§Returns

A new Selection object containing the combined elements.

Source§

impl Selection<'_>

Source

pub fn remove(&self)

Removes the set of matched elements from the document.

Source

pub fn replace_with_selection(&self, sel: &Selection<'_>)

Replaces each element in the set of matched element with the nodes from the given selection.

This follows the same rules as append.

Note: goquery’s behavior is taken as the basis.

Source

pub fn append_selection(&self, sel: &Selection<'_>)

Appends the elements in the selection to the end of each element in the set of matched elements. Note: goquery’s behavior is taken as the basis.

Source

pub fn prepend_selection(&self, sel: &Selection<'_>)

Prepends the elements in the selection to the beginning of each element in the set of matched elements. Note: goquery’s behavior is taken as the basis.

Source

pub fn set_html<T>(&self, html: T)
where T: Into<Tendril<UTF8>>,

Set the html contents of each element in the selection to specified parsed HTML.

Source

pub fn replace_with_html<T>(&self, html: T)
where T: Into<Tendril<UTF8>>,

Replaces each element in the set of matched elements with the parsed HTML.

This follows the same rules as append.

Source

pub fn append_html<T>(&self, html: T)
where T: Into<Tendril<UTF8>>,

Parses the html and appends it to the set of matched elements.

Source

pub fn prepend_html<T>(&self, html: T)
where T: Into<Tendril<UTF8>>,

Parses the html and prepends it to the set of matched elements.

Source

pub fn before_html<T>(&self, html: T)
where T: Into<Tendril<UTF8>>,

Parses the html and inserts it before the set of matched elements.

Source

pub fn after_html<T>(&self, html: T)
where T: Into<Tendril<UTF8>>,

Parses the html and inserts it after the set of matched elements.

Source

pub fn set_text(&self, text: &str)

Sets the content of each element in the selection to specified content. Doesn’t escapes the text.

If simple text needs to be inserted, this method is preferable to Selection::set_html, because it is more lightweight – it does not create a fragment tree underneath.

Source§

impl<'a> Selection<'a>

Source

pub fn select<'b>(&self, sel: &'b str) -> Selection<'a>
where 'a: 'b,

Gets the descendants of each element in the current set of matched elements, filter by a selector. It returns a new Selection object containing these matched elements.

§Panics

Panics if failed to parse the given CSS selector.

Source

pub fn select_matcher(&self, matcher: &Matcher) -> Selection<'a>

Gets the descendants of each element in the current set of matched elements, filter by a matcher. It returns a new Selection object containing these matched elements.

Source

pub fn nip(&self, sel: &'a str) -> Selection<'a>

Alias for select, it gets the descendants of each element in the current set of matched elements, filter by a selector. It returns a new Selection object containing these matched elements.

§Panics

Panics if failed to parse the given CSS selector.

Source

pub fn try_select(&self, sel: &str) -> Option<Selection<'a>>

Gets the descendants of each element in the current set of matched elements, filter by a selector. It returns a new Selection object containing these matched elements.

Source

pub fn select_single_matcher(&self, matcher: &Matcher) -> Selection<'a>

Gets the descendants of each element in the current set of matched elements, filter by a matcher. It returns a new Selection object containing elements of the single (first) match..

Source

pub fn select_single(&self, sel: &str) -> Selection<'a>

Gets the descendants of each element in the current set of matched elements, filter by a selector. It returns a new selection object containing elements of the single (first) match.

§Panics

Panics if failed to parse the given CSS selector.

Source

pub fn nodes(&self) -> &[NodeRef<'a>]

Returns a slice of underlying nodes.

Source

pub fn iter(&self) -> Selections<NodeRef<'a>>

Creates an iterator over these matched elements.

Source

pub fn parent(&self) -> Selection<'a>

Gets the parent of each element in the selection. It returns a mew Selection object containing these elements.

Source

pub fn children(&self) -> Selection<'a>

Gets the child elements of each element in the selection. It returns a new Selection object containing these elements.

Source

pub fn ancestors(&self, max_depth: Option<usize>) -> Selection<'a>

Gets the ancestor elements of each element in the selection.

§Arguments
  • max_depth - The maximum depth of the ancestors to retrieve.
§Returns

A new Selection object containing these elements.

Source

pub fn next_sibling(&self) -> Selection<'a>

Gets the immediately following sibling of each element in the selection. It returns a new Selection object containing these elements.

Source

pub fn prev_sibling(&self) -> Selection<'a>

Gets the immediately previous sibling of each element in the selection. It returns a new Selection object containing these elements.

Source

pub fn first(&self) -> Selection<'a>

Reduces the set of matched elements to the first in the set. It returns a new selection object, and an empty selection object if the selection is empty.

Source

pub fn last(&self) -> Selection<'a>

Reduces the set of matched elements to the last in the set. It returns a new selection object, and an empty selection object if the selection is empty.

Source

pub fn get(&self, index: usize) -> Option<&NodeRef<'a>>

Retrieves the underlying node at the specified index.

Source§

impl Selection<'_>

This impl block contains no public items.

internal methods

Source§

impl<'a> Selection<'a>

Source

pub fn select_matcher_iter<'b>( &self, matcher: &'b Matcher, ) -> Box<dyn Iterator<Item = NodeRef<'a>> + 'b>
where 'a: 'b,

Iterates over all nodes that match the given matcher. Useful for read-only operations.

If elements assumed to be changed during iteration, use Selection::select_matcher instead or it will panic with std::cell::BorrowMutError.

Trait Implementations§

Source§

impl<'a> Clone for Selection<'a>

Source§

fn clone(&self) -> Selection<'a>

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<'a> Debug for Selection<'a>

Source§

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

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

impl<'a> Default for Selection<'a>

Source§

fn default() -> Selection<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> From<NodeRef<'a>> for Selection<'a>

Source§

fn from(node: NodeRef<'a>) -> Selection<'a>

Converts to this type from the input type.
Source§

impl<'a> From<Vec<NodeRef<'a>>> for Selection<'a>

Source§

fn from(nodes: Vec<NodeRef<'a>>) -> Selection<'a>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> Freeze for Selection<'a>

§

impl<'a> !RefUnwindSafe for Selection<'a>

§

impl<'a> !Send for Selection<'a>

§

impl<'a> !Sync for Selection<'a>

§

impl<'a> Unpin for Selection<'a>

§

impl<'a> UnsafeUnpin for Selection<'a>

§

impl<'a> !UnwindSafe for Selection<'a>

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.