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<'_>
impl Selection<'_>
Sourcepub fn attr(&self, name: &str) -> Option<Tendril<UTF8>>
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.
Sourcepub fn attrs(&self) -> Vec<Attribute>
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.
Sourcepub fn has_attr(&self, name: &str) -> bool
pub fn has_attr(&self, name: &str) -> bool
Checks if the first element in the selection has an attribute with the name.
Sourcepub fn attr_or(&self, name: &str, default: &str) -> Tendril<UTF8>
pub fn attr_or(&self, name: &str, default: &str) -> Tendril<UTF8>
Works like attr but returns default value if attribute is not present.
Sourcepub fn set_attr(&self, name: &str, val: &str)
pub fn set_attr(&self, name: &str, val: &str)
Sets the given attribute to each element in the set of matched elements.
Sourcepub fn remove_attr(&self, name: &str)
pub fn remove_attr(&self, name: &str)
Removes the named attribute from each element in the set of matched elements.
Sourcepub fn remove_attrs(&self, names: &[&str])
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.
Sourcepub fn retain_attrs(&self, names: &[&str])
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.
Sourcepub fn remove_all_attrs(&self)
pub fn remove_all_attrs(&self)
Removes all attributes from each element in the set of matched elements.
Sourcepub fn strip_elements(&self, names: &[&str])
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.
Sourcepub fn id(&self) -> Option<Tendril<UTF8>>
pub fn id(&self) -> Option<Tendril<UTF8>>
Returns the id of the first element in the set of matched elements.
Sourcepub fn class(&self) -> Option<Tendril<UTF8>>
pub fn class(&self) -> Option<Tendril<UTF8>>
Returns the class name of the first element in the set of matched elements.
Sourcepub fn add_class(&self, class: &str)
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.
Sourcepub fn has_class(&self, class: &str) -> bool
pub fn has_class(&self, class: &str) -> bool
Determines whether any of the matched elements are assigned the given class.
Sourcepub fn remove_class(&self, class: &str)
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.
pub fn is_empty(&self) -> bool
Sourcepub fn html(&self) -> Tendril<UTF8>
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.
Sourcepub fn inner_html(&self) -> Tendril<UTF8>
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.
Sourcepub fn try_html(&self) -> Option<Tendril<UTF8>>
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.
Sourcepub fn try_inner_html(&self) -> Option<Tendril<UTF8>>
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.
Sourcepub fn text(&self) -> Tendril<UTF8>
pub fn text(&self) -> Tendril<UTF8>
Gets the combined text content of each element in the set of matched elements, including their descendants.
Sourcepub fn immediate_text(&self) -> Tendril<UTF8>
pub fn immediate_text(&self) -> Tendril<UTF8>
Gets the combined text content of each element in the set of matched, without their descendants.
Sourcepub fn formatted_text(&self) -> Tendril<UTF8>
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>
impl<'a> Selection<'a>
Sourcepub fn is(&self, sel: &str) -> bool
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.
Sourcepub fn is_matcher(&self, matcher: &Matcher) -> bool
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.
Sourcepub fn is_selection(&self, other: &Selection<'_>) -> bool
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.
Sourcepub fn try_filter(&self, sel: &str) -> Option<Selection<'a>>
pub fn try_filter(&self, sel: &str) -> Option<Selection<'a>>
Sourcepub fn filter_matcher(&self, matcher: &Matcher) -> Selection<'a>
pub fn filter_matcher(&self, matcher: &Matcher) -> Selection<'a>
Sourcepub fn filter_selection(&self, other: &Selection<'_>) -> Selection<'a>
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.
Sourcepub fn add_matcher(&self, matcher: &Matcher) -> Selection<'a>
pub fn add_matcher(&self, matcher: &Matcher) -> Selection<'a>
Source§impl Selection<'_>
impl Selection<'_>
Sourcepub fn replace_with_selection(&self, sel: &Selection<'_>)
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.
Sourcepub fn append_selection(&self, sel: &Selection<'_>)
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.
Sourcepub fn prepend_selection(&self, sel: &Selection<'_>)
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.
Sourcepub fn set_html<T>(&self, html: T)
pub fn set_html<T>(&self, html: T)
Set the html contents of each element in the selection to specified parsed HTML.
Sourcepub fn replace_with_html<T>(&self, html: T)
pub fn replace_with_html<T>(&self, html: T)
Replaces each element in the set of matched elements with the parsed HTML.
This follows the same rules as append.
Sourcepub fn append_html<T>(&self, html: T)
pub fn append_html<T>(&self, html: T)
Parses the html and appends it to the set of matched elements.
Sourcepub fn prepend_html<T>(&self, html: T)
pub fn prepend_html<T>(&self, html: T)
Parses the html and prepends it to the set of matched elements.
Sourcepub fn before_html<T>(&self, html: T)
pub fn before_html<T>(&self, html: T)
Parses the html and inserts it before the set of matched elements.
Sourcepub fn after_html<T>(&self, html: T)
pub fn after_html<T>(&self, html: T)
Parses the html and inserts it after the set of matched elements.
Sourcepub fn set_text(&self, text: &str)
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>
impl<'a> Selection<'a>
Sourcepub fn select<'b>(&self, sel: &'b str) -> Selection<'a>where
'a: 'b,
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.
Sourcepub fn select_matcher(&self, matcher: &Matcher) -> Selection<'a>
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.
Sourcepub fn nip(&self, sel: &'a str) -> Selection<'a>
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.
Sourcepub fn try_select(&self, sel: &str) -> Option<Selection<'a>>
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.
Sourcepub fn select_single_matcher(&self, matcher: &Matcher) -> Selection<'a>
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..
Sourcepub fn select_single(&self, sel: &str) -> Selection<'a>
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.
Sourcepub fn parent(&self) -> Selection<'a>
pub fn parent(&self) -> Selection<'a>
Gets the parent of each element in the selection. It returns a mew Selection object containing these elements.
Sourcepub fn children(&self) -> Selection<'a>
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.
Sourcepub fn next_sibling(&self) -> Selection<'a>
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.
Sourcepub fn prev_sibling(&self) -> Selection<'a>
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.
Sourcepub fn first(&self) -> Selection<'a>
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.
impl Selection<'_>
internal methods
Source§impl<'a> Selection<'a>
impl<'a> Selection<'a>
Sourcepub fn select_matcher_iter<'b>(
&self,
matcher: &'b Matcher,
) -> Box<dyn Iterator<Item = NodeRef<'a>> + 'b>where
'a: 'b,
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.