Tree

Struct Tree 

Source
pub struct Tree { /* private fields */ }
Expand description

An implementation of arena-tree.

Implementations§

Source§

impl Tree

Source

pub fn new_element(&self, name: &str) -> NodeRef<'_>

Creates a new element with the given name, without parent

Source

pub fn new_text<T: Into<StrTendril>>(&self, text: T) -> NodeRef<'_>

Creates a new text node with the given text, without parent

Source

pub fn get_name<'a>(&'a self, id: &NodeId) -> Option<Ref<'a, QualName>>

Gets node’s name by by id

Source

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.

Source

pub fn body(&self) -> Option<NodeRef<'_>>

Finds the <body> node element in the tree. For fragments (crate::NodeData::Fragment), this typically returns None.

Source

pub fn head(&self) -> Option<NodeRef<'_>>

Finds the <head> node element in the tree. For fragments (crate::NodeData::Fragment), this typically returns None.

Source

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

Source

pub fn root_id(&self) -> NodeId

Returns the root node.

Source

pub fn new(root: NodeData) -> Self

Creates a new tree with the given root.

Source

pub fn create_node(&self, data: NodeData) -> NodeId

Creates a new node with the given data.

Source

pub fn get(&self, id: &NodeId) -> Option<NodeRef<'_>>

Gets node by id

Source

pub fn get_unchecked(&self, id: &NodeId) -> NodeRef<'_>

Gets node by id

Source

pub fn root(&self) -> NodeRef<'_>

Gets the root node

Source

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

pub fn ancestors_of( &self, id: &NodeId, max_depth: Option<usize>, ) -> Vec<NodeRef<'_>>

Gets the ancestors nodes of a node by id.

§Arguments
  • id - The id of the node.
  • max_depth - The maximum depth of the ancestors. If None, or Some(0) the maximum depth is unlimited.
§Returns

Vec<NodeRef<'_>> - A vector of ancestors nodes.

Source

pub fn ancestor_ids_of( &self, id: &NodeId, max_depth: Option<usize>, ) -> Vec<NodeId>

Returns the ancestor node ids of a node by id.

§Arguments
  • id - The id of the node.
  • max_depth - The maximum depth of the ancestors. If None, or Some(0) the maximum depth is unlimited.
§Returns

Vec<NodeId> - A vector of ancestor node ids.

Source

pub fn ancestor_ids_of_it( &self, id: &NodeId, max_depth: Option<usize>, ) -> AncestorNodes<'_>

Returns an iterator of the ancestor node ids of a node by id

§Arguments
  • id - The id of the node.
  • max_depth - The maximum depth of the ancestors. If None, or Some(0) the maximum depth is unlimited.
§Returns

AncestorNodes<'a, T> - An iterator of ancestor node ids.

Source

pub fn children_of(&self, id: &NodeId) -> Vec<NodeRef<'_>>

Returns children of the selected node.

§Arguments
  • id - The id of the node.
§Returns

Vec<NodeRef<T>> - A vector of children nodes.

Source

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 - If true, returns the children in reverse order.
Source

pub fn child_ids_of(&self, id: &NodeId) -> Vec<NodeId>

Returns a vector of the child node ids of a node by id

§Arguments
  • id - The id of the node.
Source

pub fn descendant_ids_of_it(&self, id: &NodeId) -> DescendantNodes<'_>

Returns an iterator of the descendant node ids of a node by id

§Arguments
  • id - The id of the node.
§Returns

DescendantNodes<'_>

Source

pub fn first_child_of(&self, id: &NodeId) -> Option<NodeRef<'_>>

Gets the first child node of a node by id

Source

pub fn last_child_of(&self, id: &NodeId) -> Option<NodeRef<'_>>

Gets the last child node of a node by id

Source

pub fn parent_of(&self, id: &NodeId) -> Option<NodeRef<'_>>

Gets the parent node of a node by id

Source

pub fn prev_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>

Gets the previous sibling node of a node by id

Source

pub fn next_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>

Gets the next sibling node of a node by id

Source

pub fn last_sibling_of(&self, id: &NodeId) -> Option<NodeRef<'_>>

Gets the last sibling node of a node by id

Source

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.

Source

pub fn query_node_or<F, B>(&self, id: &NodeId, default: B, f: F) -> B
where 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.

Source

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

pub fn compare_node<F, B>(&self, a: &NodeId, b: &NodeId, f: F) -> Option<B>
where F: FnOnce(&TreeNode, &TreeNode) -> B,

This function is some kind of: get two nodes from a tree and apply some closure to them. Possibly will be removed in the future.

Source§

impl Tree

Source

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

Source

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.

Source

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.

Source

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.

Source

pub fn insert_before_of(&self, id: &NodeId, new_sibling_id: &NodeId)

Append a sibling node in the tree before the given node.

Source

pub fn insert_after_of(&self, id: &NodeId, new_sibling_id: &NodeId)

Append a sibling node in the tree after the given node.

Source

pub fn reparent_children_of(&self, id: &NodeId, new_parent_id: Option<NodeId>)

Changes the parent of children nodes of a node.

Source

pub fn remove_children_of(&self, id: &NodeId)

Detaches the children of a node.

Source§

impl Tree

Source

pub fn validate(&self) -> Result<(), String>

Validates the structural integrity of the tree.

This function checks for:

  1. Root Uniqueness:

    • Ensures exactly one root node exists (parent == None) and it is NodeId(0).
  2. Parent-Child Link Consistency:

    • first_child.parent == Self
    • first_child.prev_sibling == None
    • last_child.parent == Self
    • last_child.next_sibling == None
    • All children in the chain from first_child to last_child have correct parent, prev_sibling, and next_sibling references.
  3. Sibling Link Consistency:

    • prev_sibling.next_sibling == Self
    • next_sibling.prev_sibling == Self
  4. Valid Node References:

    • All NodeId references must point to valid nodes within the tree.
  5. Cycle Detection:

    • No cycles in parent chains (traverse parent links).
    • No cycles in sibling chains (traverse next_sibling links).

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.

Trait Implementations§

Source§

impl Clone for Tree

Source§

fn clone(&self) -> Self

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 Debug for Tree

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Tree

§

impl !RefUnwindSafe for Tree

§

impl !Send for Tree

§

impl !Sync for Tree

§

impl Unpin for Tree

§

impl UnwindSafe for Tree

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.