pub struct NodeRef(/* private fields */);Expand description
A strong reference to a node.
A node is destroyed when the last strong reference to it dropped.
Each node holds a strong reference to its first child and next sibling (if any), but only a weak reference to its last child, previous sibling, and parent. This is to avoid strong reference cycles, which would cause memory leaks.
As a result, a single NodeRef is sufficient to keep alive a node
and nodes that are after it in tree order
(its descendants, its following siblings, and their descendants)
but not other nodes in a tree.
To avoid detroying nodes prematurely, programs typically hold a strong reference to the root of a document until they’re done with that document.
Implementations§
Source§impl NodeRef
impl NodeRef
Sourcepub fn inclusive_ancestors(&self) -> Ancestors ⓘ
pub fn inclusive_ancestors(&self) -> Ancestors ⓘ
Return an iterator of references to this node and its ancestors.
Sourcepub fn ancestors(&self) -> Ancestors ⓘ
pub fn ancestors(&self) -> Ancestors ⓘ
Return an iterator of references to this node’s ancestors.
Sourcepub fn inclusive_preceding_siblings(&self) -> Rev<Siblings>
pub fn inclusive_preceding_siblings(&self) -> Rev<Siblings>
Return an iterator of references to this node and the siblings before it.
§Panics
Panics if the node has a parent but that parent has no first child (internal tree inconsistency).
Sourcepub fn preceding_siblings(&self) -> Rev<Siblings>
pub fn preceding_siblings(&self) -> Rev<Siblings>
Return an iterator of references to this node’s siblings before it.
§Panics
Panics if the node has a parent but that parent has no first child (internal tree inconsistency).
Sourcepub fn inclusive_following_siblings(&self) -> Siblings ⓘ
pub fn inclusive_following_siblings(&self) -> Siblings ⓘ
Return an iterator of references to this node and the siblings after it.
§Panics
Panics if the node has a parent but that parent has no last child (internal tree inconsistency).
Sourcepub fn following_siblings(&self) -> Siblings ⓘ
pub fn following_siblings(&self) -> Siblings ⓘ
Return an iterator of references to this node’s siblings after it.
§Panics
Panics if the node has a parent but that parent has no last child (internal tree inconsistency).
Sourcepub fn inclusive_descendants(&self) -> Descendants ⓘ
pub fn inclusive_descendants(&self) -> Descendants ⓘ
Return an iterator of references to this node and its descendants, in tree order.
Parent nodes appear before the descendants.
Note: this is the NodeEdge::Start items from traverse().
Sourcepub fn descendants(&self) -> Descendants ⓘ
pub fn descendants(&self) -> Descendants ⓘ
Return an iterator of references to this node’s descendants, in tree order.
Parent nodes appear before the descendants.
Note: this is the NodeEdge::Start items from traverse().
Sourcepub fn traverse_inclusive(&self) -> Traverse ⓘ
pub fn traverse_inclusive(&self) -> Traverse ⓘ
Return an iterator of the start and end edges of this node and its descendants, in tree order.
Sourcepub fn traverse(&self) -> Traverse ⓘ
pub fn traverse(&self) -> Traverse ⓘ
Return an iterator of the start and end edges of this node’s descendants, in tree order.
Sourcepub fn select(
&self,
selectors: &str,
) -> Result<Select<Elements<Descendants>>, ()>
pub fn select( &self, selectors: &str, ) -> Result<Select<Elements<Descendants>>, ()>
Return an iterator of the inclusive descendants element that match the given selector list.
§Errors
Returns Err(()) if the selector string fails to parse.
Sourcepub fn select_first(
&self,
selectors: &str,
) -> Result<NodeDataRef<ElementData>, ()>
pub fn select_first( &self, selectors: &str, ) -> Result<NodeDataRef<ElementData>, ()>
Return the first inclusive descendants element that match the given selector list.
§Errors
Returns Err(()) if the selector string fails to parse or if no element matches.
Source§impl NodeRef
impl NodeRef
Sourcepub fn into_element_ref(self) -> Option<NodeDataRef<ElementData>>
pub fn into_element_ref(self) -> Option<NodeDataRef<ElementData>>
If this node is an element, return a strong reference to element-specific data.
Sourcepub fn into_text_ref(self) -> Option<NodeDataRef<RefCell<String>>>
pub fn into_text_ref(self) -> Option<NodeDataRef<RefCell<String>>>
If this node is a text node, return a strong reference to its contents.
Sourcepub fn into_comment_ref(self) -> Option<NodeDataRef<RefCell<String>>>
pub fn into_comment_ref(self) -> Option<NodeDataRef<RefCell<String>>>
If this node is a comment, return a strong reference to its contents.
Sourcepub fn into_doctype_ref(self) -> Option<NodeDataRef<Doctype>>
pub fn into_doctype_ref(self) -> Option<NodeDataRef<Doctype>>
If this node is a doctype, return a strong reference to doctype-specific data.
Sourcepub fn into_document_ref(self) -> Option<NodeDataRef<DocumentData>>
pub fn into_document_ref(self) -> Option<NodeDataRef<DocumentData>>
If this node is a document, return a strong reference to document-specific data.
Sourcepub fn into_processing_instruction_ref(
self,
) -> Option<NodeDataRef<RefCell<(String, String)>>>
pub fn into_processing_instruction_ref( self, ) -> Option<NodeDataRef<RefCell<(String, String)>>>
If this node is a processing instruction, return a strong reference to its contents.
Sourcepub fn into_document_fragment_ref(self) -> Option<NodeDataRef<()>>
pub fn into_document_fragment_ref(self) -> Option<NodeDataRef<()>>
If this node is a document fragment, return a strong reference to it.
Source§impl NodeRef
Methods for HTML serialization.
impl NodeRef
Methods for HTML serialization.
Provides convenient methods for serializing DOM nodes to HTML strings, byte streams, and files.
Source§impl NodeRef
Factory methods and tree manipulation for NodeRef.
impl NodeRef
Factory methods and tree manipulation for NodeRef.
Provides constructors for all node types (elements, text, comments, etc.) and methods for reading and modifying the tree structure.
Sourcepub fn new_element<I>(name: QualName, attributes: I) -> NodeRef
pub fn new_element<I>(name: QualName, attributes: I) -> NodeRef
Create a new element node.
Sourcepub fn new_comment<T: Into<String>>(value: T) -> NodeRef
pub fn new_comment<T: Into<String>>(value: T) -> NodeRef
Create a new comment node.
Sourcepub fn new_processing_instruction<T1, T2>(target: T1, data: T2) -> NodeRef
pub fn new_processing_instruction<T1, T2>(target: T1, data: T2) -> NodeRef
Create a new processing instruction node.
Sourcepub fn new_doctype<T1, T2, T3>(
name: T1,
public_id: T2,
system_id: T3,
) -> NodeRef
pub fn new_doctype<T1, T2, T3>( name: T1, public_id: T2, system_id: T3, ) -> NodeRef
Create a new doctype node.
Sourcepub fn new_document() -> NodeRef
pub fn new_document() -> NodeRef
Create a new document node.
Sourcepub fn text_contents(&self) -> String
pub fn text_contents(&self) -> String
Return the concatenation of all text nodes in this subtree.
Sourcepub fn append(&self, new_child: NodeRef)
pub fn append(&self, new_child: NodeRef)
Append a new child to this node, after existing children.
The new child is detached from its previous position.
Sourcepub fn prepend(&self, new_child: NodeRef)
pub fn prepend(&self, new_child: NodeRef)
Prepend a new child to this node, before existing children.
The new child is detached from its previous position.
Sourcepub fn insert_after(&self, new_sibling: NodeRef)
pub fn insert_after(&self, new_sibling: NodeRef)
Insert a new sibling after this node.
The new sibling is detached from its previous position.
§Panics
Panics in debug mode if internal tree invariants are violated.
Sourcepub fn insert_before(&self, new_sibling: NodeRef)
pub fn insert_before(&self, new_sibling: NodeRef)
Insert a new sibling before this node.
The new sibling is detached from its previous position.
§Panics
Panics in debug mode if internal tree invariants are violated.
Methods from Deref<Target = Node>§
Sourcepub fn as_element(&self) -> Option<&ElementData>
pub fn as_element(&self) -> Option<&ElementData>
If this node is an element, return a reference to element-specific data.
Sourcepub fn as_text(&self) -> Option<&RefCell<String>>
pub fn as_text(&self) -> Option<&RefCell<String>>
If this node is a text node, return a reference to its contents.
Sourcepub fn as_comment(&self) -> Option<&RefCell<String>>
pub fn as_comment(&self) -> Option<&RefCell<String>>
If this node is a comment, return a reference to its contents.
Sourcepub fn as_doctype(&self) -> Option<&Doctype>
pub fn as_doctype(&self) -> Option<&Doctype>
If this node is a document, return a reference to doctype-specific data.
Sourcepub fn as_document(&self) -> Option<&DocumentData>
pub fn as_document(&self) -> Option<&DocumentData>
If this node is a document, return a reference to document-specific data.
Sourcepub fn as_processing_instruction(&self) -> Option<&RefCell<(String, String)>>
pub fn as_processing_instruction(&self) -> Option<&RefCell<(String, String)>>
If this node is a processing instruction, return a reference to its contents.
Sourcepub fn as_document_fragment(&self) -> Option<&()>
pub fn as_document_fragment(&self) -> Option<&()>
If this node is a document fragment, return a reference to the unit value.
Sourcepub fn parent(&self) -> Option<NodeRef>
pub fn parent(&self) -> Option<NodeRef>
Return a reference to the parent node, unless this node is the root of the tree.
Sourcepub fn first_child(&self) -> Option<NodeRef>
pub fn first_child(&self) -> Option<NodeRef>
Return a reference to the first child of this node, unless it has no child.
Sourcepub fn last_child(&self) -> Option<NodeRef>
pub fn last_child(&self) -> Option<NodeRef>
Return a reference to the last child of this node, unless it has no child.
Sourcepub fn previous_sibling(&self) -> Option<NodeRef>
pub fn previous_sibling(&self) -> Option<NodeRef>
Return a reference to the previous sibling of this node, unless it is a first child.
Sourcepub fn next_sibling(&self) -> Option<NodeRef>
pub fn next_sibling(&self) -> Option<NodeRef>
Return a reference to the next sibling of this node, unless it is a last child.
Trait Implementations§
Source§impl Deref for NodeRef
Implements Deref to allow transparent access to the underlying Node.
impl Deref for NodeRef
Implements Deref to allow transparent access to the underlying Node.
This allows NodeRef to be used like a reference to Node, automatically dereferencing to access Node’s methods and fields.
Source§impl Display for NodeRef
Implements Display for NodeRef.
impl Display for NodeRef
Implements Display for NodeRef.
Formats the node and its descendants as an HTML string. Uses the Serialize implementation to generate the HTML output.
Source§impl PartialEq for NodeRef
Implements PartialEq for NodeRef using pointer equality.
impl PartialEq for NodeRef
Implements PartialEq for NodeRef using pointer equality.
Compares the memory addresses of the underlying Node instances. Returns true only if both NodeRefs point to the exact same Node.
Source§impl Serialize for NodeRef
Implements Serialize for NodeRef.
impl Serialize for NodeRef
Implements Serialize for NodeRef.
Enables HTML serialization of DOM nodes using html5ever’s serialization infrastructure. Handles all node types including elements, text, comments, doctypes, processing instructions, documents, and document fragments.
Source§fn serialize<S: Serializer>(
&self,
serializer: &mut S,
traversal_scope: TraversalScope,
) -> Result<()>
fn serialize<S: Serializer>( &self, serializer: &mut S, traversal_scope: TraversalScope, ) -> Result<()>
impl Eq for NodeRef
Implements Eq for NodeRef.
Two NodeRefs are equal if they point to the same Node instance (pointer equality), not if their data is equivalent.
Auto Trait Implementations§
impl Freeze for NodeRef
impl !RefUnwindSafe for NodeRef
impl !Send for NodeRef
impl !Sync for NodeRef
impl Unpin for NodeRef
impl !UnwindSafe for NodeRef
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.