Skip to main content

NodeRef

Struct NodeRef 

Source
pub struct NodeRef<'a> { /* private fields */ }
Expand description

A borrowed reference to a node inside the document.

Provides convenience methods for querying node properties, traversing the tree, and extracting content.

Implementations§

Source§

impl<'a> NodeRef<'a>

Source

pub fn id(&self) -> NodeId

The node id.

Source

pub fn tag(&self) -> Tag

The tag type of this node.

Source

pub fn depth(&self) -> u16

The nesting depth.

Source

pub fn is_text(&self) -> bool

Whether this is a text node.

Source

pub fn is_comment(&self) -> bool

Whether this is a comment node.

Source

pub fn is_doctype(&self) -> bool

Whether this is a doctype node.

Source

pub fn is_void(&self) -> bool

Whether this is a void element.

Source

pub fn has_children(&self) -> bool

Whether this node has any children.

Source

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

Direct text content of this node (not recursive).

For element nodes, returns "". For text nodes, returns the text.

Source

pub fn text_content(&self) -> String

Recursively collect all text content from this node and its descendants.

Source

pub fn inner_html(&self) -> String

Reconstruct the inner HTML of this node.

Source

pub fn outer_html(&self) -> String

Reconstruct the outer HTML of this node (including the tag itself).

Source

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

Get the value of an attribute by name.

Source

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

Check if the node has a given CSS class.

Splits the class attribute on whitespace and checks if any segment matches.

Source

pub fn attrs(&self) -> &'a [Attribute]

Get all attributes.

Source

pub fn children(&self) -> Children<'a>

Iterate over direct children.

Source

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

Get the parent node, if any.

Source

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

Get the first child, if any.

Source

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

Get the next sibling, if any.

Source

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

Get the previous sibling, if any.

Source

pub fn ancestors(&self) -> Ancestors<'a>

Iterate over ancestors (parent chain, not including self).

Source

pub fn siblings(&self) -> Siblings<'a>

Iterate over next siblings (not including self).

Source

pub fn descendants(&self) -> DepthFirst<'a>

Pre-order depth-first traversal of the subtree rooted at this node.

Source

pub fn descendants_bfs(&self) -> BreadthFirst<'a>

Breadth-first traversal of the subtree rooted at this node.

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

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

Performs copy-assignment from source. Read more
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> Copy for NodeRef<'a>

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.