Skip to main content

Selectable

Trait Selectable 

Source
pub trait Selectable {
    // Required methods
    fn select(&self, css: &str) -> Result<Selection<'_>, SelectorError>;
    fn select_compiled(
        &self,
        sel: &CompiledSelector,
    ) -> Result<Selection<'_>, SelectorError>;
    fn select_first_compiled(
        &self,
        sel: &CompiledSelector,
    ) -> Result<Option<NodeRef<'_>>, SelectorError>;
    fn select_first(
        &self,
        css: &str,
    ) -> Result<Option<NodeRef<'_>>, SelectorError>;
    fn find_by_tag(&self, tag: Tag) -> Selection<'_>;
    fn find_by_id(&self, id: &str) -> Option<NodeRef<'_>>;
    fn find_by_class(&self, class: &str) -> Selection<'_>;
    fn find_by_attr(&self, name: &str, value: &str) -> Selection<'_>;
    fn xpath(&self, expr: &str) -> Result<XPathResult, XPathError>;
}
Expand description

Extension trait that adds CSS selector methods to Document.

Import this trait to use .select() and convenience methods on a document.

Required Methods§

Source

fn select(&self, css: &str) -> Result<Selection<'_>, SelectorError>

Select all nodes matching a CSS selector.

§Errors

Returns SelectorError::Invalid if the selector syntax is invalid.

§Example
use fhp_tree::parse;
use fhp_selector::Selectable;

let doc = parse("<div><p>Hello</p></div>").unwrap();
let sel = doc.select("p").unwrap();
assert_eq!(sel.len(), 1);
Source

fn select_compiled( &self, sel: &CompiledSelector, ) -> Result<Selection<'_>, SelectorError>

Select all nodes matching a pre-compiled CSS selector.

This avoids re-parsing the selector string on every call, which is beneficial when the same selector is reused across many documents.

Source

fn select_first_compiled( &self, sel: &CompiledSelector, ) -> Result<Option<NodeRef<'_>>, SelectorError>

Select the first node matching a pre-compiled CSS selector.

Source

fn select_first(&self, css: &str) -> Result<Option<NodeRef<'_>>, SelectorError>

Select the first node matching a CSS selector.

Source

fn find_by_tag(&self, tag: Tag) -> Selection<'_>

Find all elements with the given tag.

Source

fn find_by_id(&self, id: &str) -> Option<NodeRef<'_>>

Find an element by its id attribute.

Scans all nodes linearly. For repeated lookups, build a DocumentIndex instead.

Source

fn find_by_class(&self, class: &str) -> Selection<'_>

Find all elements with the given CSS class.

Source

fn find_by_attr(&self, name: &str, value: &str) -> Selection<'_>

Find all elements with an attribute matching a value.

Source

fn xpath(&self, expr: &str) -> Result<XPathResult, XPathError>

Evaluate an XPath expression against the document.

§Errors

Returns XPathError::Invalid if the expression syntax is invalid.

§Example
use fhp_tree::parse;
use fhp_selector::Selectable;
use fhp_selector::xpath::ast::XPathResult;

let doc = parse("<div><p>Hello</p></div>").unwrap();
let result = doc.xpath("//p").unwrap();
match result {
    XPathResult::Nodes(nodes) => assert_eq!(nodes.len(), 1),
    _ => panic!("expected nodes"),
}

Implementations on Foreign Types§

Source§

impl Selectable for Document

Implementors§