pub struct Node<'a, T: 'a> {
pub data: T,
/* private fields */
}
Expand description
A node inside a DOM-like tree.
Fields§
§data: T
The data held by the node.
Implementations§
Source§impl<'a, T> Node<'a, T>
impl<'a, T> Node<'a, T>
Sourcepub fn new(data: T) -> Node<'a, T>
pub fn new(data: T) -> Node<'a, T>
Create a new node from its associated data.
Typically, this node needs to be moved into an arena allocator before it can be used in a tree.
Sourcepub fn parent(&self) -> Option<&'a Node<'a, T>>
pub fn parent(&self) -> Option<&'a Node<'a, T>>
Return a reference to the parent node, unless this node is the root of the tree.
Sourcepub fn first_child(&self) -> Option<&'a Node<'a, T>>
pub fn first_child(&self) -> Option<&'a Node<'a, T>>
Return a reference to the first child of this node, unless it has no child.
Sourcepub fn last_child(&self) -> Option<&'a Node<'a, T>>
pub fn last_child(&self) -> Option<&'a Node<'a, T>>
Return a reference to the last child of this node, unless it has no child.
Sourcepub fn previous_sibling(&self) -> Option<&'a Node<'a, T>>
pub fn previous_sibling(&self) -> Option<&'a Node<'a, T>>
Return a reference to the previous sibling of this node, unless it is a first child.
Sourcepub fn next_sibling(&self) -> Option<&'a Node<'a, T>>
pub fn next_sibling(&self) -> Option<&'a Node<'a, T>>
Return a reference to the next sibling of this node, unless it is a last child.
Sourcepub fn same_node(&self, other: &Node<'a, T>) -> bool
pub fn same_node(&self, other: &Node<'a, T>) -> bool
Returns whether two references point to the same node.
Sourcepub fn ancestors(&'a self) -> Ancestors<'a, T> ⓘ
pub fn ancestors(&'a self) -> Ancestors<'a, T> ⓘ
Return an iterator of references to this node and its ancestors.
Call .next().unwrap()
once on the iterator to skip the node itself.
Sourcepub fn preceding_siblings(&'a self) -> PrecedingSiblings<'a, T> ⓘ
pub fn preceding_siblings(&'a self) -> PrecedingSiblings<'a, T> ⓘ
Return an iterator of references to this node and the siblings before it.
Call .next().unwrap()
once on the iterator to skip the node itself.
Sourcepub fn following_siblings(&'a self) -> FollowingSiblings<'a, T> ⓘ
pub fn following_siblings(&'a self) -> FollowingSiblings<'a, T> ⓘ
Return an iterator of references to this node and the siblings after it.
Call .next().unwrap()
once on the iterator to skip the node itself.
Sourcepub fn children(&'a self) -> Children<'a, T> ⓘ
pub fn children(&'a self) -> Children<'a, T> ⓘ
Return an iterator of references to this node’s children.
Sourcepub fn reverse_children(&'a self) -> ReverseChildren<'a, T> ⓘ
pub fn reverse_children(&'a self) -> ReverseChildren<'a, T> ⓘ
Return an iterator of references to this node’s children, in reverse order.
Sourcepub fn descendants(&'a self) -> Descendants<'a, T> ⓘ
pub fn descendants(&'a self) -> Descendants<'a, T> ⓘ
Return an iterator of references to this Node
and its descendants, in tree order.
Parent nodes appear before the descendants.
Call .next().unwrap()
once on the iterator to skip the node itself.
Similar Functions: Use traverse()
or reverse_traverse
if you need
references to the NodeEdge
structs associated with each Node
Sourcepub fn traverse(&'a self) -> Traverse<'a, T> ⓘ
pub fn traverse(&'a self) -> Traverse<'a, T> ⓘ
Return an iterator of references to NodeEdge
enums for each Node
and its descendants,
in tree order.
NodeEdge
enums represent the Start
or End
of each node.
Similar Functions: Use descendants()
if you don’t need Start
and End
.
Sourcepub fn reverse_traverse(&'a self) -> ReverseTraverse<'a, T> ⓘ
pub fn reverse_traverse(&'a self) -> ReverseTraverse<'a, T> ⓘ
Return an iterator of references to NodeEdge
enums for each Node
and its descendants,
in reverse order.
NodeEdge
enums represent the Start
or End
of each node.
Similar Functions: Use descendants()
if you don’t need Start
and End
.
Sourcepub fn append(&'a self, new_child: &'a Node<'a, T>)
pub fn append(&'a self, new_child: &'a Node<'a, T>)
Append a new child to this node, after existing children.
Sourcepub fn prepend(&'a self, new_child: &'a Node<'a, T>)
pub fn prepend(&'a self, new_child: &'a Node<'a, T>)
Prepend a new child to this node, before existing children.
Sourcepub fn insert_after(&'a self, new_sibling: &'a Node<'a, T>)
pub fn insert_after(&'a self, new_sibling: &'a Node<'a, T>)
Insert a new sibling after this node.
Sourcepub fn insert_before(&'a self, new_sibling: &'a Node<'a, T>)
pub fn insert_before(&'a self, new_sibling: &'a Node<'a, T>)
Insert a new sibling before this node.
Source§impl<'a> Node<'a, RefCell<Ast>>
impl<'a> Node<'a, RefCell<Ast>>
Sourcepub fn validate(&'a self) -> Result<(), ValidationError<'a>>
pub fn validate(&'a self) -> Result<(), ValidationError<'a>>
The comrak representation of a markdown node in Rust isn’t strict enough to rule out invalid trees according to the CommonMark specification. One simple example is that block containers, such as lists, should only contain blocks, but it’s possible to put naked inline text in a list item. Such invalid trees can lead comrak to generate incorrect output if rendered.
This method performs additional structural checks to ensure that a markdown AST is valid according to the CommonMark specification.
Note that those invalid trees can only be generated programmatically. Parsing markdown with comrak, on the other hand, should always produce a valid tree.