pub struct Dom { /* private fields */ }Expand description
A parsed HTML document: an arena of nodes, a single string buffer they reference, the top-level (root) nodes, and any recovery diagnostics.
Build one with crate::parse. It is immutable once built; traverse it with
NodeRef handles from Dom::root, Dom::roots, Dom::get, or the
find_by_* helpers.
Implementations§
Source§impl Dom
impl Dom
Sourcepub fn errors(&self) -> &[ParseError]
pub fn errors(&self) -> &[ParseError]
Recovery diagnostics collected during parsing (empty for clean input).
Sourcepub fn get(&self, id: NodeId) -> Option<NodeRef<'_>>
pub fn get(&self, id: NodeId) -> Option<NodeRef<'_>>
A handle to a node by id, or None if the id is out of range.
Sourcepub fn root(&self) -> Option<NodeRef<'_>>
pub fn root(&self) -> Option<NodeRef<'_>>
The first top-level node (the document element for a typical page).
Sourcepub fn roots(&self) -> impl Iterator<Item = NodeRef<'_>>
pub fn roots(&self) -> impl Iterator<Item = NodeRef<'_>>
All top-level nodes, in document order.
Sourcepub fn nodes(&self) -> impl Iterator<Item = NodeRef<'_>>
pub fn nodes(&self) -> impl Iterator<Item = NodeRef<'_>>
Every node in the arena, in document (pre-order) order.
Sourcepub fn find_by_tag<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = NodeRef<'a>> + 'a
pub fn find_by_tag<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = NodeRef<'a>> + 'a
All elements whose tag name equals name (ASCII case-insensitive).
Sourcepub fn find_by_id<'a>(&'a self, id: &'a str) -> Option<NodeRef<'a>>
pub fn find_by_id<'a>(&'a self, id: &'a str) -> Option<NodeRef<'a>>
The first element with id="…" equal to id, if any.
Sourcepub fn find_by_class<'a>(
&'a self,
class: &'a str,
) -> impl Iterator<Item = NodeRef<'a>> + 'a
pub fn find_by_class<'a>( &'a self, class: &'a str, ) -> impl Iterator<Item = NodeRef<'a>> + 'a
All elements whose class attribute contains class.
Source§impl Dom
impl Dom
Sourcepub fn to_json(&self) -> String
Available on crate feature json only.
pub fn to_json(&self) -> String
json only.Serialize the document to a compact JSON string.
The shape is self-describing: every node is an object with a "type" of
element / text / comment / doctype, elements carry tag,
attrs (an object) and children (an array), and the whole document is
wrapped in { "type": "document", "errors": N, "children": [...] }.
let dom = domtree::parse("<a href=\"/x\">hi</a>");
let json = dom.to_json();
assert!(json.contains("\"tag\":\"a\""));
assert!(json.contains("\"href\":\"/x\""));
assert!(json.contains("\"value\":\"hi\""));Source§impl Dom
impl Dom
Sourcepub fn select<'a>(
&'a self,
selector: &str,
) -> impl Iterator<Item = NodeRef<'a>> + 'a
Available on crate feature query only.
pub fn select<'a>( &'a self, selector: &str, ) -> impl Iterator<Item = NodeRef<'a>> + 'a
query only.Find every element matching a CSS selector, in document order.
let dom = domtree::parse(
"<ul><li class='x'>a</li><li>b</li></ul><ol><li>c</li></ol>",
);
// descendant + class
assert_eq!(dom.select("ul li.x").count(), 1);
// child combinator
assert_eq!(dom.select("ol > li").count(), 1);
// selector list
assert_eq!(dom.select("ul, ol").count(), 2);