Skip to main content

find_within

Function find_within 

Source
pub fn find_within(
    doc: &Document,
    scope: NodeId,
    selector: &str,
) -> QueryResult<Option<NodeId>>
Expand description

Finds the first element matching a CSS selector within a subtree.

The search starts from the given scope node and only includes its descendants.

§Errors

Returns [QueryError::InvalidSelector] if the selector syntax is invalid.

§Examples

use scrape_core::{Html5everParser, Parser, query::find_within};

let parser = Html5everParser;
let doc = parser
    .parse("<div id=\"a\"><span>A</span></div><div id=\"b\"><span>B</span></div>")
    .unwrap();

// Find div#a first
let scope = doc
    .nodes()
    .find(|(_, n)| n.kind.attributes().and_then(|a| a.get("id")) == Some(&"a".to_string()))
    .map(|(id, _)| id)
    .unwrap();

let result = find_within(&doc, scope, "span").unwrap();
assert!(result.is_some());