Skip to main content

find_within_compiled

Function find_within_compiled 

Source
pub fn find_within_compiled(
    doc: &Document,
    scope: NodeId,
    selector: &CompiledSelector,
) -> Option<NodeId>
Expand description

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

ยงExamples

use scrape_core::{
    Html5everParser, Parser,
    query::{CompiledSelector, find_within_compiled},
};

let parser = Html5everParser;
let doc = parser
    .parse("<div id=\"a\"><span>A</span></div><div id=\"b\"><span>B</span></div>")
    .unwrap();
let selector = CompiledSelector::compile("span").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_compiled(&doc, scope, &selector);
assert!(result.is_some());