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
impl Selector
Sourcepub fn element_data(&self) -> ElementData
pub fn element_data(&self) -> ElementData
Capture this element’s unique properties for adaptive relocation.
Sourcepub fn save(
&self,
storage: &SqliteStorage,
identifier: &str,
) -> Result<(), StorageError>
pub fn save( &self, storage: &SqliteStorage, identifier: &str, ) -> Result<(), StorageError>
Save this element’s unique properties to storage under
identifier for later relocation.
Sourcepub fn retrieve(
storage: &SqliteStorage,
identifier: &str,
) -> Result<Option<ElementData>, StorageError>
pub fn retrieve( storage: &SqliteStorage, identifier: &str, ) -> Result<Option<ElementData>, StorageError>
Retrieve the element properties stored under identifier, if any.
Sourcepub fn relocate(&self, original: &ElementData, percentage: f64) -> Selectors
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.
Sourcepub fn css_adaptive(
&self,
selector: &str,
identifier: &str,
storage: &SqliteStorage,
auto_save: bool,
percentage: f64,
) -> Result<Selectors, StorageError>
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
impl Selector
Sourcepub fn markdown(&self) -> TextHandler
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
impl Selector
Sourcepub fn from_html(html: &str) -> Self
pub fn from_html(html: &str) -> Self
Parse an HTML document string into a Selector pointing at the document root.
Sourcepub fn from_html_with_url(html: &str, url: &str) -> Self
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.
Sourcepub fn node_id(&self) -> NodeId
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).
Sourcepub fn tag(&self) -> &str
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.
Sourcepub fn text(&self) -> TextHandler
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.
Sourcepub fn attrib(&self) -> AttributesHandler
pub fn attrib(&self) -> AttributesHandler
Return the element’s attributes as an AttributesHandler
(empty for non-element nodes).
Sourcepub fn html_content(&self) -> TextHandler
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.
Sourcepub fn outer_html(&self) -> TextHandler
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.
Sourcepub fn get_all_text(
&self,
separator: &str,
strip: bool,
ignore_tags: &[&str],
valid_values: Option<&[&str]>,
) -> TextHandler
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.
Sourcepub fn css(&self, selector: &str) -> Selectors
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.
Sourcepub fn css_getall(&self, query: &str) -> Vec<TextHandler>
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 distinguishesp::text(direct children) fromp ::text(descendants) — here::textis 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.
Sourcepub fn css_get(&self, query: &str) -> Option<TextHandler>
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.
Sourcepub fn children(&self) -> Selectors
pub fn children(&self) -> Selectors
Return direct element children (text and comment nodes are skipped).
Sourcepub fn parent(&self) -> Option<Selector>
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.
Sourcepub fn siblings(&self) -> Selectors
pub fn siblings(&self) -> Selectors
Return sibling elements (all of the parent’s element children except this node), in document order.
Sourcepub fn next(&self) -> Option<Selector>
pub fn next(&self) -> Option<Selector>
Return the next sibling element, skipping intervening text and comment nodes.
Sourcepub fn previous(&self) -> Option<Selector>
pub fn previous(&self) -> Option<Selector>
Return the previous sibling element, skipping intervening text and comment nodes.
Sourcepub fn has_class(&self, class_name: &str) -> bool
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).
Sourcepub fn urljoin(&self, relative_url: &str) -> String
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.
Sourcepub fn re(
&self,
pattern: &str,
replace_entities: bool,
clean_match: bool,
case_sensitive: bool,
) -> Vec<TextHandler>
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.
Sourcepub fn re_first(
&self,
pattern: &str,
replace_entities: bool,
clean_match: bool,
case_sensitive: bool,
) -> Option<TextHandler>
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).
Sourcepub fn json(&self) -> Result<Value, Error>
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.
Sourcepub fn find_by_text(
&self,
text: &str,
first_match: bool,
partial: bool,
case_sensitive: bool,
) -> Option<Selector>
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.
Sourcepub fn find_all_by_text(
&self,
text: &str,
partial: bool,
case_sensitive: bool,
) -> Selectors
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.
Sourcepub fn find_by_regex(
&self,
pattern: &str,
first_match: bool,
case_sensitive: bool,
) -> Option<Selector>
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.
Sourcepub fn get_attribute(&self, key: &str) -> Option<TextHandler>
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.