Skip to main content

Selector

Struct Selector 

Source
pub struct Selector { /* private fields */ }
Expand description

A wrapper around a node in an HTML tree, providing CSS selection, text extraction, DOM navigation, regex, and JSON parsing.

Cloning is cheap: all Selectors produced from the same document share one reference-counted tree, and a clone only copies the node handle. Query methods (Selector::css, Selector::children, …) return new Selectors pointing into that same shared tree.

Implementations§

Source§

impl Selector

Source

pub fn element_data(&self) -> ElementData

Capture this element’s unique properties for adaptive relocation.

Source

pub fn save( &self, storage: &SqliteStorage, identifier: &str, ) -> Result<(), StorageError>

Save this element’s unique properties to storage under identifier for later relocation.

Source

pub fn retrieve( storage: &SqliteStorage, identifier: &str, ) -> Result<Option<ElementData>, StorageError>

Retrieve the element properties stored under identifier, if any.

Source

pub fn relocate(&self, original: &ElementData, percentage: f64) -> Selectors

Search this tree again for a previously saved element. Every element is scored against original; the group with the highest score is returned if it reaches percentage (see DEFAULT_RELOCATION_PERCENTAGE), otherwise an empty collection.

Source

pub fn css_adaptive( &self, selector: &str, identifier: &str, storage: &SqliteStorage, auto_save: bool, percentage: f64, ) -> Result<Selectors, StorageError>

CSS search with adaptive relocation: when selector matches nothing, the element previously saved under identifier is relocated by similarity. With auto_save, the first match (or the relocated element) is (re-)saved under identifier so future relocations track the page as it drifts.

Source§

impl Selector

Source

pub fn markdown(&self) -> TextHandler

Convert this node’s subtree to clean, sanitized Markdown.

Scripts, styles, and hidden / prompt-injection content are always removed before conversion. See the markdown module docs for details and output conventions.

Source§

impl Selector

Source

pub fn from_html(html: &str) -> Self

Parse an HTML document string into a Selector pointing at the document root.

Source

pub fn from_html_with_url(html: &str, url: &str) -> Self

Parse an HTML document string with a base URL used by Selector::urljoin to resolve relative links.

Source

pub fn node_id(&self) -> NodeId

Stable identity of the underlying DOM node. Useful for comparing two Selectors that reference the same node without resorting to HTML string equality (which is ambiguous for identical siblings).

Source

pub fn tag(&self) -> &str

Return the tag name of this element.

Non-element nodes get placeholder names: "html" for the document root, "#text" for text nodes, "#comment" for comments, "#doctype" for doctypes, and "#pi" for processing instructions.

Source

pub fn text(&self) -> TextHandler

Return the direct (non-recursive) text content of this element. Only collects immediate text children, not text inside child elements — for the full recursive text use Selector::get_all_text.

Source

pub fn attrib(&self) -> AttributesHandler

Return the element’s attributes as an AttributesHandler (empty for non-element nodes).

Source

pub fn html_content(&self) -> TextHandler

Return the inner HTML of this element (its children serialized, without the element’s own tag). For the document root this is the whole serialized document; for other non-element nodes it is empty.

Source

pub fn outer_html(&self) -> TextHandler

Return the outer HTML of this element (the element itself including its tag and children). For the document root this is the whole serialized document; for other non-element nodes it is empty.

Source

pub fn get_all_text( &self, separator: &str, strip: bool, ignore_tags: &[&str], valid_values: Option<&[&str]>, ) -> TextHandler

Recursively extract all text under this node, joined with separator.

Elements whose tag is in ignore_tags are skipped along with their entire subtree (e.g. pass &["script", "style"] to drop scripts). With strip, each text node is trimmed and empty ones are dropped before joining. If valid_values is Some, only text nodes whose trimmed content appears in the slice are included.

Source

pub fn css(&self, selector: &str) -> Selectors

Run a CSS selector query and return matching descendant elements.

Matches are returned in document order. An invalid selector yields an empty collection rather than an error. Only plain CSS is accepted here — for ::text / ::attr(name) extraction use Selector::css_get / Selector::css_getall.

Source

pub fn css_getall(&self, query: &str) -> Vec<TextHandler>

Parsel-inspired CSS query with ::text and ::attr(name) pseudo-element support, returning all extracted values:

  • "li::text" — the full recursive text of each matching element (including <script>/<style> content, like Parsel), joined without stripping so word spacing inside markup survives, then end-trimmed. Elements with no text yield nothing. Note this diverges from Parsel, which returns one string per text node and distinguishes p::text (direct children) from p ::text (descendants) — here ::text is always one joined string per matched element, fully recursive.
  • "a::attr(href)" — the attribute value of each matching element that has the attribute (elements without it are skipped, matching Parsel),
  • a plain selector — each match’s outer HTML.
Source

pub fn css_get(&self, query: &str) -> Option<TextHandler>

Like Self::css_getall, but returns only the first extracted value (Parsel’s .get()): for ::attr/::text queries this is the first element that actually has a value, not merely the first match.

Source

pub fn children(&self) -> Selectors

Return direct element children (text and comment nodes are skipped).

Source

pub fn parent(&self) -> Option<Selector>

Return the parent node, if any. Note the parent of a top-level element is the document root, not an element.

Source

pub fn siblings(&self) -> Selectors

Return sibling elements (all of the parent’s element children except this node), in document order.

Source

pub fn next(&self) -> Option<Selector>

Return the next sibling element, skipping intervening text and comment nodes.

Source

pub fn previous(&self) -> Option<Selector>

Return the previous sibling element, skipping intervening text and comment nodes.

Source

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

Check whether this element has a given CSS class (case-sensitive; false for non-element nodes).

Source

pub fn urljoin(&self, relative_url: &str) -> String

Join a relative URL with the base URL of this selector (set via Selector::from_html_with_url). Falls back to returning relative_url unchanged when there is no base URL or either URL fails to parse.

Source

pub fn re( &self, pattern: &str, replace_entities: bool, clean_match: bool, case_sensitive: bool, ) -> Vec<TextHandler>

Apply a regex pattern against the full recursive text of this element, returning all matches. When the pattern has capture groups, group 1 is returned for each match, otherwise the whole match. See TextHandler::re for the flag semantics.

Source

pub fn re_first( &self, pattern: &str, replace_entities: bool, clean_match: bool, case_sensitive: bool, ) -> Option<TextHandler>

Apply a regex against the full recursive text of this element and return only the first match (see Selector::re).

Source

pub fn json(&self) -> Result<Value, Error>

Parse the direct text content of this element (see Selector::text) as JSON — useful for <script type="application/ld+json"> blocks and similar.

Source

pub fn find_by_text( &self, text: &str, first_match: bool, partial: bool, case_sensitive: bool, ) -> Option<Selector>

Find a single element by its text content.

An element matches when its full recursive text (equivalent of Selector::get_all_text) equals text after trimming — or merely contains it when partial is set. Because a container’s recursive text includes everything inside it, matching prefers the deepest (most specific) element: with first_match the search descends before testing each element and returns the first, deepest match in document order; with first_match == false the last matching element is returned instead.

Source

pub fn find_all_by_text( &self, text: &str, partial: bool, case_sensitive: bool, ) -> Selectors

Find all elements whose full recursive text matches text (same matching rules as Selector::find_by_text), in document order with containers before their matching descendants.

Source

pub fn find_by_regex( &self, pattern: &str, first_match: bool, case_sensitive: bool, ) -> Option<Selector>

Find an element whose full recursive text matches a regex pattern.

With first_match the first matching element in a pre-order walk is returned (a container matches before its descendants, since its recursive text includes theirs); otherwise the last match wins. An invalid pattern returns None.

Source

pub fn get_attribute(&self, key: &str) -> Option<TextHandler>

Get the value of a specific attribute, or None when the attribute is absent or this is not an element node.

Trait Implementations§

Source§

impl Clone for Selector

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Selector

Source§

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

Formats the value using the given formatter. Read more

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more