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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
use std::{borrow::Cow, fmt::Display};
use crate::{
grammar::{Grammar, Rule},
ElementVariant, Error,
};
use super::{element::Element, formatting, options::FormattingOptions, span::SourceSpan, Result};
use pest::{
iterators::{Pair, Pairs},
Parser,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Node<'s> {
Element(Element<'s>),
#[serde(borrow)]
Text(Cow<'s, str>),
#[serde(borrow)]
Comment(Cow<'s, str>),
}
impl<'s> Node<'s> {
/// Get the text when it's a text node
pub fn text(&self) -> Option<&str> {
match self {
Node::Text(t) => Some(t),
_ => None,
}
}
/// Get the elemnt when it's a element node
pub fn element(&self) -> Option<&Element> {
match self {
Node::Element(e) => Some(e),
_ => None,
}
}
/// Get the comment when it's a comment node
pub fn comment(&self) -> Option<&str> {
match self {
Node::Comment(t) => Some(t),
_ => None,
}
}
/// Create a new text node
pub fn new_text(text: &'s str) -> Self {
Self::Text(Cow::Borrowed(text))
}
/// Create a new comment node
pub fn new_comment(comment: &'s str) -> Self {
Self::Comment(Cow::Borrowed(comment))
}
/// Parse a dom from a html string
pub fn parse(input: &'s str) -> Result<Vec<Self>> {
let pairs = match Grammar::parse(Rule::html, input) {
Ok(pairs) => pairs,
Err(error) => return Err(formatting::error_msg(error)),
};
Self::build_nodes(pairs)
}
/// Create the node from a json string
pub fn parse_json(json: &'s str) -> Result<Self> {
Ok(serde_json::from_str(json)?)
}
/// Output the node as a json formatted string
pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(self)?)
}
/// Output the node as a pretty json formatted string
pub fn to_json_pretty(&self) -> Result<String> {
Ok(serde_json::to_string_pretty(self)?)
}
pub fn fmt_opt<W>(&self, f: &mut W, o: &FormattingOptions, depth: usize) -> std::fmt::Result
where
W: std::fmt::Write,
{
match self {
Node::Element(elem) => {
elem.fmt_opt(f, o, depth)?;
}
Node::Text(text) => {
o.fmt_depth(f, depth)?;
write!(f, "{}", text.trim())?;
}
Node::Comment(comment) => {
o.fmt_depth(f, depth)?;
write!(f, "<!-- {comment} -->")?;
}
}
Ok(())
}
fn build_nodes(pairs: Pairs<'s, Rule>) -> Result<Vec<Self>> {
let mut nodes = Vec::new();
for pair in pairs {
match pair.as_rule() {
// A <!DOCTYPE> tag means a full-fledged document. Because it's a node, we don't use it
Rule::doctype => (),
// If we see an element, build the sub-tree and add it as a child.
// Warnings are ignored
Rule::node_element => match Self::build_node_element(pair, &mut Vec::new()) {
Ok(el) => {
if let Some(node) = el {
nodes.push(node);
}
}
Err(_) => {}
},
// Similar to an element, we add it as a child
Rule::node_text => {
let text = pair.as_str();
if !text.trim().is_empty() {
nodes.push(Node::Text(Cow::Borrowed(text)));
}
}
// Store comments as a child
Rule::node_comment => {
nodes.push(Node::Comment(Cow::Borrowed(pair.into_inner().as_str())));
}
// Ignore 'end of input', which then allows the catch-all unreachable!() arm to
// function properly.
Rule::EOI => (),
// This should be unreachable, due to the way the grammar is written
_ => unreachable!("[build nodes] unknown rule: {:?}", pair.as_rule()),
};
}
// The result are validated nodes
Ok(nodes)
}
pub(super) fn build_node_element(
pair: Pair<'s, Rule>,
warnings: &mut Vec<String>,
) -> Result<Option<Node<'s>>> {
let source_span = {
let pair_span = pair.as_span();
let (start_line, start_column) = pair_span.start_pos().line_col();
let (end_line, end_column) = pair_span.end_pos().line_col();
SourceSpan::new(
pair_span.as_str(),
start_line,
end_line,
start_column,
end_column,
)
};
let mut element = Element {
source_span,
..Element::default()
};
for pair in pair.into_inner() {
match pair.as_rule() {
Rule::node_element | Rule::el_raw_text => {
match Self::build_node_element(pair, warnings) {
Ok(el) => {
if let Some(child_element) = el {
element.children.push(child_element)
}
}
Err(error) => {
warnings.push(format!("{}", error));
}
}
}
Rule::node_text | Rule::el_raw_text_content => {
let text = pair.as_str();
if !text.trim().is_empty() {
element.children.push(Node::Text(Cow::Borrowed(text)));
}
}
Rule::node_comment => {
element
.children
.push(Node::Comment(Cow::Borrowed(pair.into_inner().as_str())));
}
// TODO: To enable some kind of validation we should probably align this with
// https://html.spec.whatwg.org/multipage/syntax.html#elements-2
// Also see element variants
Rule::el_name | Rule::el_void_name | Rule::el_raw_text_name => {
element.name = Cow::Borrowed(pair.as_str());
}
Rule::attr => match Self::build_attribute(pair.into_inner()) {
Ok((attr_key, attr_value)) => {
match attr_key {
"class" => {
if let Some(classes) = attr_value {
let classes = classes.split_whitespace().collect::<Vec<_>>();
for class in classes {
element.classes.push(Cow::Borrowed(class));
}
}
}
_ => {
element.attributes.insert(
Cow::Borrowed(attr_key),
attr_value.map(|s| Cow::Borrowed(s)),
);
}
};
}
Err(error) => {
warnings.push(format!("{}", error));
}
},
Rule::el_normal_end | Rule::el_raw_text_end => {
element.variant = ElementVariant::Normal;
break;
}
Rule::el_dangling => (),
Rule::EOI => (),
_ => {
return Err(Error::Parsing(format!(
"Failed to create element at rule: {:?}",
pair.as_rule()
)))
}
}
}
if element.name != "" {
Ok(Some(Node::Element(element)))
} else {
Ok(None)
}
}
fn build_attribute(pairs: Pairs<'s, Rule>) -> Result<(&'s str, Option<&'s str>)> {
let mut attribute = ("", None);
for pair in pairs {
match pair.as_rule() {
Rule::attr_key => {
attribute.0 = pair.as_str().trim();
}
Rule::attr_non_quoted => {
attribute.1 = Some(pair.as_str().trim());
}
Rule::attr_quoted => {
let inner_pair = pair
.into_inner()
.into_iter()
.next()
.expect("attribute value");
match inner_pair.as_rule() {
Rule::attr_value => attribute.1 = Some(inner_pair.as_str()),
_ => {
return Err(Error::Parsing(format!(
"Failed to parse attr value: {:?}",
inner_pair.as_rule()
)))
}
}
}
_ => {
return Err(Error::Parsing(format!(
"Failed to parse attr: {:?}",
pair.as_rule()
)))
}
}
}
Ok(attribute)
}
}
impl<'s> Display for Node<'s> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fmt_opt(f, &FormattingOptions::pretty(), 0)
}
}
impl<'a> IntoIterator for &'a Node<'a> {
type Item = &'a Node<'a>;
type IntoIter = NodeIntoIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
NodeIntoIterator {
node: self,
index: vec![],
}
}
}
pub struct NodeIntoIterator<'a> {
node: &'a Node<'a>,
// We add/remove to this vec each time we go up/down a node three
index: Vec<(usize, &'a Node<'a>)>,
}
impl<'a> Iterator for NodeIntoIterator<'a> {
type Item = &'a Node<'a>;
fn next(&mut self) -> Option<Self::Item> {
// Get first child
let child = match self.node {
Node::Element(ref e) => e.children.get(0),
_ => None,
};
let result = match child {
// If element has child, return child
Some(child) => {
self.index.push((0, self.node));
self.node = child;
Some(child)
}
// If element doesn't have a child, but is a child of another node
None if self.index.len() > 0 => {
let mut has_finished = false;
let mut next_node = None;
while !has_finished {
// Try to get the next sibling of the parent node
if let Some((sibling_index, parent)) = self.index.pop() {
let next_sibling = sibling_index + 1;
let sibling = if let Node::Element(ref e) = parent {
e.children.get(next_sibling)
} else {
None
};
if sibling.is_some() {
has_finished = true;
self.index.push((next_sibling, parent));
next_node = sibling;
} else {
continue;
}
// Break of there are no more parents
} else {
has_finished = true;
}
}
if let Some(next_node) = next_node {
self.node = next_node;
}
next_node
}
_ => None,
};
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn node_utillity_functions() {
let node = Node::Text(Cow::Borrowed("test"));
assert_eq!(node.text(), Some("test"));
assert_eq!(node.element(), None);
assert_eq!(node.comment(), None);
let node = Node::Element(Element::default());
assert_eq!(node.text(), None);
assert_eq!(node.element(), Some(&Element::default()));
assert_eq!(node.comment(), None);
let node = Node::Comment(Cow::Borrowed("test"));
assert_eq!(node.text(), None);
assert_eq!(node.element(), None);
assert_eq!(node.comment(), Some("test"));
}
}