pub struct Tree { /* private fields */ }Expand description
An implementation of arena-tree.
Implementations§
Source§impl Tree
impl Tree
Sourcepub fn new_element(&self, name: &str) -> NodeRef<'_>
pub fn new_element(&self, name: &str) -> NodeRef<'_>
Creates a new element with the given name, without parent
Sourcepub fn new_text<T: Into<StrTendril>>(&self, text: T) -> NodeRef<'_>
pub fn new_text<T: Into<StrTendril>>(&self, text: T) -> NodeRef<'_>
Creates a new text node with the given text, without parent
Sourcepub fn get_name<'a>(&'a self, id: &NodeId) -> Option<Ref<'a, QualName>>
pub fn get_name<'a>(&'a self, id: &NodeId) -> Option<Ref<'a, QualName>>
Gets node’s name by by id
Sourcepub fn base_uri(&self) -> Option<StrTendril>
pub fn base_uri(&self) -> Option<StrTendril>
Finds the base URI of the tree by looking for <base> tags in document’s head.
The base URI is the value of the href attribute of the first
<base> tag in the document’s head. If no such tag is found,
the method returns None.
This is a very fast method compare to crate::Document::select.
Sourcepub fn body(&self) -> Option<NodeRef<'_>>
pub fn body(&self) -> Option<NodeRef<'_>>
Finds the <body> node element in the tree.
For fragments (crate::NodeData::Fragment), this typically returns None.
Sourcepub fn head(&self) -> Option<NodeRef<'_>>
pub fn head(&self) -> Option<NodeRef<'_>>
Finds the <head> node element in the tree.
For fragments (crate::NodeData::Fragment), this typically returns None.
Sourcepub fn is_mathml_annotation_xml_integration_point(
&self,
node_id: &NodeId,
) -> bool
pub fn is_mathml_annotation_xml_integration_point( &self, node_id: &NodeId, ) -> bool
Checks if the node is a MathML annotation-xml integration point.
Returns false if the node does not exist or is not an element.
Source§impl Tree
impl Tree
Sourcepub fn create_node(&self, data: NodeData) -> NodeId
pub fn create_node(&self, data: NodeData) -> NodeId
Creates a new node with the given data.
Sourcepub fn get_unchecked(&self, id: &NodeId) -> NodeRef<'_>
pub fn get_unchecked(&self, id: &NodeId) -> NodeRef<'_>
Gets node by id
Sourcepub fn html_root(&self) -> NodeRef<'_>
pub fn html_root(&self) -> NodeRef<'_>
Gets the element root node.
Even if crate::Document was constructed with an empty string,
it will still have a root element node (<html>).
§Returns
NodeRef: The root element (<html>) node.
Sourcepub fn ancestor_ids_of_it(
&self,
id: &NodeId,
max_depth: Option<usize>,
) -> AncestorNodes<'_>
pub fn ancestor_ids_of_it( &self, id: &NodeId, max_depth: Option<usize>, ) -> AncestorNodes<'_>
Sourcepub fn children_of(&self, id: &NodeId) -> Vec<NodeRef<'_>>
pub fn children_of(&self, id: &NodeId) -> Vec<NodeRef<'_>>
Sourcepub fn child_ids_of_it(&self, id: &NodeId, rev: bool) -> ChildNodes<'_>
pub fn child_ids_of_it(&self, id: &NodeId, rev: bool) -> ChildNodes<'_>
Returns an iterator of the child node ids of a node by id
§Arguments
id- The id of the node.rev- Iftrue, returns the children in reverse order.
Sourcepub fn child_ids_of(&self, id: &NodeId) -> Vec<NodeId>
pub fn child_ids_of(&self, id: &NodeId) -> Vec<NodeId>
Sourcepub fn descendant_ids_of_it(&self, id: &NodeId) -> DescendantNodes<'_>
pub fn descendant_ids_of_it(&self, id: &NodeId) -> DescendantNodes<'_>
Sourcepub fn first_child_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
pub fn first_child_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
Gets the first child node of a node by id
Sourcepub fn last_child_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
pub fn last_child_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
Gets the last child node of a node by id
Sourcepub fn parent_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
pub fn parent_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
Gets the parent node of a node by id
Sourcepub fn prev_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
pub fn prev_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
Gets the previous sibling node of a node by id
Sourcepub fn next_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
pub fn next_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
Gets the next sibling node of a node by id
Sourcepub fn last_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
pub fn last_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>
Gets the last sibling node of a node by id
Sourcepub fn query_node<F, B>(&self, id: &NodeId, f: F) -> Option<B>where
F: FnOnce(&TreeNode) -> B,
pub fn query_node<F, B>(&self, id: &NodeId, f: F) -> Option<B>where
F: FnOnce(&TreeNode) -> B,
A helper function to get the node from the tree and apply a function to it.
Sourcepub fn query_node_or<F, B>(&self, id: &NodeId, default: B, f: F) -> Bwhere
F: FnOnce(&TreeNode) -> B,
pub fn query_node_or<F, B>(&self, id: &NodeId, default: B, f: F) -> Bwhere
F: FnOnce(&TreeNode) -> B,
A helper function to get the node from the tree and apply a function to it. Accepts a default value to return for a case if the node doesn’t exist.
Sourcepub fn update_node<F, B>(&self, id: &NodeId, f: F) -> Option<B>where
F: FnOnce(&mut TreeNode) -> B,
pub fn update_node<F, B>(&self, id: &NodeId, f: F) -> Option<B>where
F: FnOnce(&mut TreeNode) -> B,
A helper function to get the node from the tree and apply a function to it that modifies it.
Source§impl Tree
impl Tree
Sourcepub fn append_child_data_of(&self, id: &NodeId, data: NodeData)
pub fn append_child_data_of(&self, id: &NodeId, data: NodeData)
Creates a new element from data and appends it to a node by id
Sourcepub fn append_child_of(&self, id: &NodeId, new_child_id: &NodeId)
pub fn append_child_of(&self, id: &NodeId, new_child_id: &NodeId)
Appends a child node by new_child_id to a node by id. new_child_id must exist in the tree.
Sourcepub fn prepend_child_of(&self, id: &NodeId, new_child_id: &NodeId)
pub fn prepend_child_of(&self, id: &NodeId, new_child_id: &NodeId)
Prepend a child node by new_child_id to a node by id. new_child_id must exist in the tree.
Sourcepub fn remove_from_parent(&self, id: &NodeId)
pub fn remove_from_parent(&self, id: &NodeId)
Remove a node from the its parent by id. The node remains in the tree. It is possible to assign it to another node in the tree after this operation.
Sourcepub fn insert_before_of(&self, id: &NodeId, new_sibling_id: &NodeId)
pub fn insert_before_of(&self, id: &NodeId, new_sibling_id: &NodeId)
Append a sibling node in the tree before the given node.
Sourcepub fn insert_after_of(&self, id: &NodeId, new_sibling_id: &NodeId)
pub fn insert_after_of(&self, id: &NodeId, new_sibling_id: &NodeId)
Append a sibling node in the tree after the given node.
Sourcepub fn reparent_children_of(&self, id: &NodeId, new_parent_id: Option<NodeId>)
pub fn reparent_children_of(&self, id: &NodeId, new_parent_id: Option<NodeId>)
Changes the parent of children nodes of a node.
Sourcepub fn remove_children_of(&self, id: &NodeId)
pub fn remove_children_of(&self, id: &NodeId)
Detaches the children of a node.
Source§impl Tree
impl Tree
Sourcepub fn validate(&self) -> Result<(), String>
pub fn validate(&self) -> Result<(), String>
Validates the structural integrity of the tree.
This function checks for:
-
Root Uniqueness:
- Ensures exactly one root node exists (
parent == None) and it isNodeId(0).
- Ensures exactly one root node exists (
-
Parent-Child Link Consistency:
first_child.parent == Selffirst_child.prev_sibling == Nonelast_child.parent == Selflast_child.next_sibling == None- All children in the chain from
first_childtolast_childhave correctparent,prev_sibling, andnext_siblingreferences.
-
Sibling Link Consistency:
prev_sibling.next_sibling == Selfnext_sibling.prev_sibling == Self
-
Valid Node References:
- All
NodeIdreferences must point to valid nodes within the tree.
- All
-
Cycle Detection:
- No cycles in parent chains (traverse
parentlinks). - No cycles in sibling chains (traverse
next_siblinglinks).
- No cycles in parent chains (traverse
Returns:
Ok(())if the tree structure is valid.Err(String)with a descriptive message if any inconsistency or cycle is detected.
Orphaned nodes (non-root nodes with parent == None) are allowed and not considered invalid.