mod query;
mod tree;
pub use ego_tree::NodeId;
use scraper::Node;
pub struct Document {
pub(crate) tree: ego_tree::Tree<Node>,
}
impl Document {
pub fn parse(html: &str) -> Self {
let html_doc = scraper::Html::parse_document(html);
Self {
tree: html_doc.tree,
}
}
pub fn root(&self) -> NodeId {
self.tree.root().id()
}
pub fn body(&self) -> Option<NodeId> {
let html_id = self
.tree
.root()
.children()
.find(|n| matches!(n.value(), Node::Element(e) if e.name.local.as_ref() == "html"))?
.id();
self.tree
.get(html_id)?
.children()
.find(|n| matches!(n.value(), Node::Element(e) if e.name.local.as_ref() == "body"))
.map(|n| n.id())
}
}