Skip to main content

NodeRef

Struct NodeRef 

Source
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>

Source

pub fn id(&self) -> NodeId

This node’s arena id.

Source

pub fn kind(&self) -> NodeKind<'a>

The node’s kind (element / text / comment / doctype) as a borrowed view.

Source

pub fn is_element(&self) -> bool

Whether this node is an element.

Source

pub fn is_text(&self) -> bool

Whether this node is a text node.

Source

pub fn tag_name(&self) -> Option<&'a str>

The tag name (lower-cased), if this node is an element.

Source

pub fn attr(&self, name: &str) -> Option<&'a str>

The value of attribute name, if this is an element with that attribute.

Source

pub fn has_attr(&self, name: &str) -> bool

Whether attribute name is present (including valueless boolean attrs).

Source

pub fn attributes(&self) -> impl Iterator<Item = (&'a str, &'a str)>

All (name, value) attribute pairs (empty iterator for non-elements).

Source

pub fn classes(&self) -> impl Iterator<Item = &'a str>

The class names on this element.

Source

pub fn has_class(&self, class: &str) -> bool

Whether this element has the given class.

Source

pub fn text(&self) -> Option<&'a str>

This node’s own text, if it is a text node (already entity-decoded).

Source

pub fn comment(&self) -> Option<&'a str>

The body text of this comment node, if it is one.

Source

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.

Source

pub fn parent(&self) -> Option<NodeRef<'a>>

This node’s parent, if any.

Source

pub fn first_child(&self) -> Option<NodeRef<'a>>

The first child, if any.

Source

pub fn last_child(&self) -> Option<NodeRef<'a>>

The last child, if any.

Source

pub fn next_sibling(&self) -> Option<NodeRef<'a>>

The next sibling, if any.

Source

pub fn prev_sibling(&self) -> Option<NodeRef<'a>>

The previous sibling, if any.

Source

pub fn children(&self) -> impl Iterator<Item = NodeRef<'a>>

This node’s direct children, in order.

Source

pub fn child_elements(&self) -> impl Iterator<Item = NodeRef<'a>>

Child elements only (skips text/comment/doctype nodes).

Source

pub fn ancestors(&self) -> impl Iterator<Item = NodeRef<'a>>

This node’s ancestors, from parent up to the root.

Source

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>

Source

pub fn to_html(&self) -> String

Serialize just this node and its subtree back to an HTML string.

let dom = domtree::parse("<div><img src=a.png><br></div>");
let div = dom.find_by_tag("div").next().unwrap();
assert_eq!(div.to_html(), r#"<div><img src="a.png"><br></div>"#);
Source§

impl<'a> NodeRef<'a>

Source

pub fn to_json(&self) -> String

Available on crate feature 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"}]}"#);

Trait Implementations§

Source§

impl<'a> Clone for NodeRef<'a>

Source§

fn clone(&self) -> NodeRef<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for NodeRef<'a>

Source§

impl<'a> Debug for NodeRef<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Serialize for NodeRef<'a>

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for NodeRef<'a>

§

impl<'a> RefUnwindSafe for NodeRef<'a>

§

impl<'a> Send for NodeRef<'a>

§

impl<'a> Sync for NodeRef<'a>

§

impl<'a> Unpin for NodeRef<'a>

§

impl<'a> UnsafeUnpin for NodeRef<'a>

§

impl<'a> UnwindSafe for NodeRef<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.