1use bumpalo::{Bump, BumpAllocSafe};
3/// A node is either a text node or an element.
9 /// An element potentially with attributes and children.
10 Element(ElementNode<'a>),
13/// Text nodes are just a string of text. They cannot have attributes or
15#[derive(Debug, Clone)]
16pub struct TextNode<'a> {
17 pub(crate) text: &'a str,
20/// Elements have a tag name, zero or more attributes, and zero or more
22#[derive(Debug, Clone)]
23pub struct ElementNode<'a> {
24 pub(crate) tag_name: &'a str,
25 pub(crate) attributes: &'a [Attribute<'a>],
26 pub(crate) children: &'a [Node<'a>],
29#[derive(Clone, Debug)]
30pub struct Attribute<'a> {
36 /// Is this node a text node?
37 pub fn is_text(&self) -> bool {
39 Node::Text(_) => true,
44 /// Is this node an element?
45 pub fn is_element(&self) -> bool {
47 Node::Element { .. } => true,
53impl<'a> TextNode<'a> {
54 /// Get this text node's text content.
55 pub fn text(&self) -> &'a str {
60impl<'a> ElementNode<'a> {
61 /// Get this element's tag name.
62 pub fn tag_name(&self) -> &'a str {
66 /// Get this element's attributes.
67 pub fn attributes(&self) -> &'a [Attribute<'a>] {
71 /// Get this element's attributes.
72 pub fn children(&self) -> &'a [Node<'a>] {
77impl<'a> BumpAllocSafe for Node<'a> {}
78impl<'a> BumpAllocSafe for Attribute<'a> {}
81 /// Construct a new text node with the given text.
83 pub fn text(text: &'a str) -> Node<'a> {
84 Node::Text(TextNode { text })
87 /// Construct a new element node with the given tag name and children.
89 pub fn element<Attributes, Children>(
92 attributes: Attributes,
96 Attributes: 'a + BumpAllocSafe + AsRef<[Attribute<'a>]>,
97 Children: 'a + BumpAllocSafe + AsRef<[Node<'a>]>,
99 let children: &'a Children = bump.alloc(children);
100 let children: &'a [Node<'a>] = children.as_ref();
102 let attributes: &'a Attributes = bump.alloc(attributes);
103 let attributes: &'a [Attribute<'a>] = attributes.as_ref();
105 Node::Element(ElementNode {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list