pub struct NodeRef<'a> { /* private fields */ }Expand description
A lightweight, Copy read handle to a single node, bound to its Dom.
All traversal (parent, children, descendants, ancestors, siblings)
is O(1) per step thanks to the arena’s links.
Implementations§
Source§impl<'a> NodeRef<'a>
impl<'a> NodeRef<'a>
Sourcepub fn kind(&self) -> NodeKind<'a>
pub fn kind(&self) -> NodeKind<'a>
The node’s kind (element / text / comment / doctype) as a borrowed view.
Sourcepub fn is_element(&self) -> bool
pub fn is_element(&self) -> bool
Whether this node is an element.
Sourcepub fn tag_name(&self) -> Option<&'a str>
pub fn tag_name(&self) -> Option<&'a str>
The tag name (lower-cased), if this node is an element.
Sourcepub fn attr(&self, name: &str) -> Option<&'a str>
pub fn attr(&self, name: &str) -> Option<&'a str>
The value of attribute name, if this is an element with that attribute.
Sourcepub fn has_attr(&self, name: &str) -> bool
pub fn has_attr(&self, name: &str) -> bool
Whether attribute name is present (including valueless boolean attrs).
Sourcepub fn attributes(&self) -> impl Iterator<Item = (&'a str, &'a str)>
pub fn attributes(&self) -> impl Iterator<Item = (&'a str, &'a str)>
All (name, value) attribute pairs (empty iterator for non-elements).
Sourcepub fn text(&self) -> Option<&'a str>
pub fn text(&self) -> Option<&'a str>
This node’s own text, if it is a text node (already entity-decoded).
Sourcepub fn text_content(&self) -> String
pub fn text_content(&self) -> String
Concatenated text of this node and all its descendants, in document
order (entity-decoded). Equivalent to the DOM textContent.
Sourcepub fn first_child(&self) -> Option<NodeRef<'a>>
pub fn first_child(&self) -> Option<NodeRef<'a>>
The first child, if any.
Sourcepub fn last_child(&self) -> Option<NodeRef<'a>>
pub fn last_child(&self) -> Option<NodeRef<'a>>
The last child, if any.
Sourcepub fn next_sibling(&self) -> Option<NodeRef<'a>>
pub fn next_sibling(&self) -> Option<NodeRef<'a>>
The next sibling, if any.
Sourcepub fn prev_sibling(&self) -> Option<NodeRef<'a>>
pub fn prev_sibling(&self) -> Option<NodeRef<'a>>
The previous sibling, if any.
Sourcepub fn children(&self) -> impl Iterator<Item = NodeRef<'a>>
pub fn children(&self) -> impl Iterator<Item = NodeRef<'a>>
This node’s direct children, in order.
Sourcepub fn child_elements(&self) -> impl Iterator<Item = NodeRef<'a>>
pub fn child_elements(&self) -> impl Iterator<Item = NodeRef<'a>>
Child elements only (skips text/comment/doctype nodes).
Sourcepub fn ancestors(&self) -> impl Iterator<Item = NodeRef<'a>>
pub fn ancestors(&self) -> impl Iterator<Item = NodeRef<'a>>
This node’s ancestors, from parent up to the root.
Sourcepub fn descendants(&self) -> Descendants<'a> ⓘ
pub fn descendants(&self) -> Descendants<'a> ⓘ
All descendants of this node, in document (pre-order) order. Does not include the node itself. Allocation-free.
Source§impl<'a> NodeRef<'a>
impl<'a> NodeRef<'a>
Sourcepub fn to_json(&self) -> String
Available on crate feature json only.
pub fn to_json(&self) -> String
json only.Serialize just this node and its subtree to a compact JSON string,
using the same shape as Dom::to_json.
let dom = domtree::parse("<div><b>hi</b></div>");
let b = dom.find_by_tag("b").next().unwrap();
assert_eq!(b.to_json(), r#"{"type":"element","tag":"b","attrs":{},"children":[{"type":"text","value":"hi"}]}"#);