1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::fmt::Debug;
use super::node_data::{Element, NodeData};
use crate::NodeId;
/// The inner node is a [`crate::Tree`] node.
#[derive(Debug, Clone)]
pub struct TreeNode {
/// The id of the node.
pub id: NodeId,
/// The optional parent's id, root and orphan nodes will have `None` as their parent.
pub parent: Option<NodeId>,
/// The optional previous sibling's id.
pub prev_sibling: Option<NodeId>,
/// The optional next sibling's id.
pub next_sibling: Option<NodeId>,
/// The optional first child's id.
pub first_child: Option<NodeId>,
/// The optional last child's id.
pub last_child: Option<NodeId>,
/// The node's data.
pub data: NodeData,
}
impl TreeNode {
/// Creates a new inner node.
pub(crate) const fn new(id: NodeId, data: NodeData) -> Self {
Self {
id,
parent: None,
prev_sibling: None,
next_sibling: None,
first_child: None,
last_child: None,
data,
}
}
}
impl TreeNode {
/// fixes node ids
pub(crate) fn adjust(&mut self, offset: usize) {
self.id = NodeId::new(self.id.value + offset);
let adjust_fn = |id: NodeId| NodeId::new(id.value + offset);
self.parent = self.parent.map(adjust_fn);
self.prev_sibling = self.prev_sibling.map(adjust_fn);
self.next_sibling = self.next_sibling.map(adjust_fn);
self.first_child = self.first_child.map(adjust_fn);
self.last_child = self.last_child.map(adjust_fn);
if let NodeData::Element(ref mut el) = self.data {
el.template_contents = el.template_contents.map(adjust_fn);
}
}
}
impl TreeNode {
/// Checks if the node is a document node.
pub const fn is_document(&self) -> bool {
matches!(self.data, NodeData::Document)
}
/// Checks if the node is an element node.
pub const fn is_element(&self) -> bool {
matches!(self.data, NodeData::Element(_))
}
/// Checks if the node is a text node.
pub const fn is_text(&self) -> bool {
matches!(self.data, NodeData::Text { .. })
}
/// Checks if the node is a comment node.
pub const fn is_comment(&self) -> bool {
matches!(self.data, NodeData::Comment { .. })
}
/// Checks if the node is a fragment node.
pub const fn is_fragment(&self) -> bool {
matches!(self.data, NodeData::Fragment)
}
/// Checks if the node is a doctype node.
pub const fn is_doctype(&self) -> bool {
matches!(self.data, NodeData::Doctype { .. })
}
/// Checks if node may have children nodes.
pub const fn may_have_children(&self) -> bool {
matches!(
self.data,
NodeData::Document | NodeData::Fragment | NodeData::Element(_)
)
}
/// Returns a reference to the node as an element. If the node is not an element, `None` is returned.
///
/// # Returns
/// `Option<&Element>`
pub const fn as_element(&self) -> Option<&Element> {
match self.data {
NodeData::Element(ref e) => Some(e),
_ => None,
}
}
/// Returns a mutable reference to the node as an element. If the node is not an element, `None` is returned.
pub fn as_element_mut(&mut self) -> Option<&mut Element> {
match self.data {
NodeData::Element(ref mut e) => Some(e),
_ => None,
}
}
/// Sets the value of the specified attribute to the node.
pub fn set_attr(&mut self, name: &str, val: &str) {
if let Some(element) = self.as_element_mut() {
element.set_attr(name, val);
}
}
/// Removes the specified attribute from the element.
pub fn remove_attr(&mut self, name: &str) {
if let Some(element) = self.as_element_mut() {
element.remove_attr(name);
}
}
/// Removes the specified attributes from the element.
///
/// # Arguments
/// - `names`: A slice of attribute names to remove. Empty slice removes no attributes.
pub fn remove_attrs(&mut self, names: &[&str]) {
if let Some(element) = self.as_element_mut() {
element.remove_attrs(names);
}
}
/// Retains only the attributes with the specified names.
///
/// # Arguments
/// - `names`: A slice of attribute names to retain. Empty slice retains no attributes.
pub fn retain_attrs(&mut self, names: &[&str]) {
if let Some(element) = self.as_element_mut() {
element.retain_attrs(names);
}
}
/// Removes all attributes from the element.
pub fn remove_all_attrs(&mut self) {
if let Some(element) = self.as_element_mut() {
element.remove_all_attrs();
}
}
/// Renames the node if node is an [`NodeData::Element`].
pub fn rename(&mut self, name: &str) {
if let Some(element) = self.as_element_mut() {
element.rename(name);
}
}
/// Adds a class to the node
pub fn add_class(&mut self, class: &str) {
if let Some(element) = self.as_element_mut() {
element.add_class(class);
}
}
/// Removes a class from the node
pub fn remove_class(&mut self, class: &str) {
if let Some(element) = self.as_element_mut() {
element.remove_class(class);
}
}
/// If element is link.
pub fn is_link(&self) -> bool {
if let Some(element) = self.as_element() {
element.is_link()
} else {
false
}
}
}