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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
mod tokenizer;
use std::{collections::HashMap, error::Error};
use indextree::{Arena, Node, NodeId};
use tokenizer::Symbol;
pub struct HtmlTag {
pub name: String,
pub attributes: HashMap<String, String>,
}
impl HtmlTag {
pub fn new(name: String) -> HtmlTag {
HtmlTag {
name,
attributes: HashMap::new(),
}
}
}
pub enum HtmlNode {
Tag(HtmlTag),
Text(String),
}
pub struct HtmlDocument {
pub arena: Arena<HtmlNode>,
pub root_key: NodeId,
}
lazy_static! {
/// List of HTML tags that do not have end tags.
static ref UNPAIRED_TAGS: Vec<&'static str> = vec![
"meta",
"link",
"img",
"input",
"br",
];
}
pub fn parse(text: &str) -> Result<HtmlDocument, Box<dyn Error>> {
let tokens = tokenizer::lex(text)?;
let mut arena: Arena<HtmlNode> = Arena::new();
let mut root_key_o: Option<NodeId> = None;
let mut cur_key_o: Option<NodeId> = None;
let mut has_tag_open = false;
let mut tokens = tokens.into_iter();
while let Some(token) = tokens.next() {
match token {
Symbol::StartTag(tag_name) => {
// Skip the special doctype tag so a proper root is selected.
if is_doctype(&tag_name, &mut tokens)? {
continue;
}
if has_tag_open {
return Err("Start tag encountered before previous tag was closed.".into());
}
has_tag_open = true;
let node = HtmlNode::Tag(HtmlTag::new(tag_name));
let node_key = arena.new_node(node);
if let Some(cur_key) = cur_key_o {
cur_key.append(node_key, &mut arena);
}
cur_key_o = Some(node_key);
if root_key_o.is_none() {
root_key_o = cur_key_o;
}
},
Symbol::TagClose => {
if !has_tag_open {
return Err("Tag close encountered before a tag was opened.".into());
}
// This will be none if the root node was just closed.
if cur_key_o.is_some() {
let cur_tree_node = try_get_tree_node(cur_key_o, &arena)?;
match cur_tree_node.get() {
HtmlNode::Tag(cur_tag) => {
// If this is an unpaired tag, the tag begins and ends with this token.
if UNPAIRED_TAGS.contains(&cur_tag.name.as_str()) {
// Set current key to the parent of this tag.
cur_key_o = cur_tree_node.parent();
}
},
HtmlNode::Text(_) => return Err("End tag attempted to close a text node.".into()),
}
}
has_tag_open = false;
},
Symbol::EndTag(tag_name) => {
if has_tag_open {
return Err("End tag encountered before previous tag was closed.".into());
}
has_tag_open = true;
let cur_tree_node = try_get_tree_node(cur_key_o, &arena)?;
match cur_tree_node.get() {
HtmlNode::Tag(cur_tag) => {
if cur_tag.name != tag_name {
return Err(format!("End tag name `{}` mismatched open tag name `{}`.", tag_name, cur_tag.name).into());
}
},
HtmlNode::Text(_) => return Err("End tag attempted to close a text node.".into()),
}
// Set current key to the parent of this tag.
cur_key_o = cur_tree_node.parent();
},
Symbol::TagCloseAndEnd => {
if !has_tag_open {
return Err("Tag close encountered before a tag was opened.".into());
}
has_tag_open = false;
let cur_tree_node = try_get_tree_node(cur_key_o, &arena)?;
if let HtmlNode::Text(_) = cur_tree_node.get() {
return Err("End tag attempted to close a text node.".into());
}
// Set current key to the parent of this tag.
cur_key_o = cur_tree_node.parent();
}
Symbol::Identifier(iden) => {
if !has_tag_open {
return Err(format!("Identifier `{}` encountered outside of tag.", iden).into());
}
if let Some(token) = tokens.next() {
if let Symbol::AssignmentSign = token {
if let Some(token) = tokens.next() {
if let Symbol::Literal(lit) = token {
let cur_tree_node = try_get_mut_tree_node(cur_key_o, &mut arena)?;
match cur_tree_node.get_mut() {
HtmlNode::Tag(tag) => {
tag.attributes.insert(iden, lit);
},
HtmlNode::Text(_) => return Err("Attempted to add attribute to text node.".into()),
}
} else {
return Err("Expected literal after assignment sign.".into());
}
} else {
return Err("Unexpected end of tokens.".into());
}
} else {
return Err("Expected assignment sign after identifier.".into());
}
} else {
return Err("Unexpected end of tokens.".into());
}
},
Symbol::Text(text) => {
if has_tag_open {
return Err("Text encountered before previous tag was closed.".into());
}
let node = HtmlNode::Text(text);
let node_key = arena.new_node(node);
if let Some(cur_key) = cur_key_o {
cur_key.append(node_key, &mut arena);
}
}
_ => (),
}
}
if let Some(root_key) = root_key_o {
return Ok(HtmlDocument {
arena,
root_key,
});
}
Err("No root node found.".into())
}
fn is_doctype(tag_name: &String, tokens: &mut std::vec::IntoIter<Symbol>) -> Result<bool, Box<dyn Error>> {
if tag_name == "!DOCTYPE" {
if let Some(token) = tokens.next() {
if let Symbol::Identifier(iden) = token {
if iden != "html" {
return Err("Expected identifier `html` after !DOCSTRING.".into());
}
if let Some(token) = tokens.next() {
match token {
Symbol::TagClose => {
// we good
},
_ => return Err("Expected tag close after !DOCSTRING html".into()),
}
} else {
return Err("Unexpected end of tokens.".into());
}
} else {
return Err("Expected identifier `html` after !DOCSTRING.".into());
}
} else {
return Err("Unexpected end of tokens.".into());
}
return Ok(true);
}
return Ok(false);
}
fn try_get_tree_node(key: Option<NodeId>, arena: &Arena<HtmlNode>) -> Result<&Node<HtmlNode>, &'static str> {
match key {
Some(key) => {
match arena.get(key) {
Some(node) => Ok(node),
None => Err("Could not get tree node from arena."),
}
},
None => Err("Unexpected None key."),
}
}
fn try_get_mut_tree_node(key: Option<NodeId>, arena: &mut Arena<HtmlNode>) -> Result<&mut Node<HtmlNode>, &'static str> {
match key {
Some(key) => {
match arena.get_mut(key) {
Some(node) => Ok(node),
None => Err("Could not get tree node from arena."),
}
},
None => Err("Unexpected None key."),
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::*;
fn assert_tag(arena: &Arena<HtmlNode>, key: NodeId, tag_name: &str, attributes: Option<HashMap<&str, &str>>) -> Vec<NodeId> {
let tree_node = arena.get(key).unwrap();
let html_node = tree_node.get();
match html_node {
HtmlNode::Tag(tag) => {
assert_eq!(String::from(tag_name), tag.name);
if let Some(attributes) = attributes {
for attr in attributes {
assert_eq!(&String::from(attr.1), tag.attributes.get(attr.0).unwrap());
}
}
return key.children(&arena).collect();
},
_ => panic!("Expected Tag, got different variant instead."),
}
}
fn assert_text(arena: &Arena<HtmlNode>, key: NodeId, text: &str) {
let tree_node = arena.get(key).unwrap();
let html_node = tree_node.get();
match html_node {
HtmlNode::Text(node_text) => {
assert_eq!(String::from(text), node_text.trim());
},
_ => panic!("Expected Text, got different variant instead."),
}
}
#[test]
fn parse_works() {
// arrange
let text = "<html><a class=\"beans\"></a><b><ba/>yo</b></html>";
// act
let result = parse(text).unwrap();
// assert
let arena = &result.arena;
// <html>
let children = assert_tag(arena, result.root_key, "html", None);
// <html> -> <a class="beans">
{
let key = children[0];
let mut attributes = HashMap::new();
attributes.insert("class", "beans");
assert_tag(arena, key, "a", Some(attributes));
}
// <html> -> <b>
{
let key = children[1];
let children = assert_tag(arena, key, "b", None);
// <html> -> <b> -> <ba>
{
let key = children[0];
assert_tag(arena, key, "ba", None);
}
// <html> -> <b> -> text()
{
let key = children[1];
assert_text(arena, key, "yo");
}
}
}
#[test]
fn parse_should_work_with_html() {
// arrange
let html = r###"<!DOCTYPE html>
<!-- saved from url=(0026)https://www.rust-lang.org/ -->
<html lang="en-US">
<head>
<title>Rust Programming Language</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!-- Twitter card -->
<meta name="twitter:card" content="summary">
</head>
<body>
<main>
<section id="language-values" class="green">
<div class="w-100 mw-none ph3 mw8-m mw9-l center f3">
<header class="pb0">
<h2>
Why Rust?
</h2>
</header>
<div class="flex-none flex-l">
<section class="w-100 pv2 pv0-l mt4">
<h3 class="f2 f1-l">Performance</h3>
<p class="f3 lh-copy">
Rust is blazingly fast and memory-efficient: with no runtime or
garbage collector, it can power performance-critical services, run on
embedded devices, and easily integrate with other languages.
</p>
</section>
</div>
</div>
</section>
</main>
<script src="./Rust Programming Language_files/languages.js.download"/>
</body>
</html>"###;
// act
let result = parse(html).unwrap();
// assert
let arena = &result.arena;
// <html>
let children = assert_tag(arena, result.root_key, "html", None);
// <html> -> <head>
{
let key = children[0];
let children = assert_tag(arena, key, "head", None);
// <html> -> <head> -> <title>
{
let key = children[0];
let children = assert_tag(arena, key, "title", None);
// <html> -> head> -> <title> -> text()
{
let key = children[0];
assert_text(arena, key, "Rust Programming Language");
}
}
// <html> -> <head> -> <meta name="viewport" content="width=device-width,initial-scale=1.0">
{
let key = children[1];
let mut attributes = HashMap::new();
attributes.insert("name", "viewport");
attributes.insert("content", "width=device-width,initial-scale=1.0");
assert_tag(arena, key, "meta", Some(attributes));
}
// <html> -> <head> -> <meta name="twitter:card" content="summary">
{
let key = children[2];
let mut attributes = HashMap::new();
attributes.insert("name", "twitter:card");
attributes.insert("content", "summary");
assert_tag(arena, key, "meta", Some(attributes));
}
}
// <html> -> <body>
{
let key = children[1];
let children = assert_tag(arena, key, "body", None);
// <html> -> <body> -> <main>
{
let key = children[0];
let children = assert_tag(arena, key, "main", None);
// <html> -> <body> -> <main> -> <section id="language-values" class="green">
{
let key = children[0];
let mut attributes = HashMap::new();
attributes.insert("id", "language-values");
attributes.insert("class", "green");
let children = assert_tag(arena, key, "section", Some(attributes));
// <html> -> <body> -> <main> -> <section> -> <div class="w-100 mw-none ph3 mw8-m mw9-l center f3">
{
let key = children[0];
let mut attributes = HashMap::new();
attributes.insert("class", "w-100 mw-none ph3 mw8-m mw9-l center f3");
let children = assert_tag(arena, key, "div", Some(attributes));
// <html> -> <body> -> <main> -> <section> -> <div> -> <header class="pb0">
{
let key = children[0];
let mut attributes = HashMap::new();
attributes.insert("class", "pb0");
let children = assert_tag(arena, key, "header", Some(attributes));
// <html> -> <body> -> <main> -> <section> -> <div> -> <header> -> <h2>
{
let key = children[0];
let children = assert_tag(arena, key, "h2", None);
// <html> -> <body> -> <main> -> <section> -> <div> -> <header> -> <h2> -> text()
{
let key = children[0];
assert_text(arena, key, "Why Rust?")
}
}
}
// <html> -> <body> -> <main> -> <section> -> <div> -> <div class="flex-none flex-l">
{
let key = children[1];
let mut attributes = HashMap::new();
attributes.insert("class", "flex-none flex-l");
let children = assert_tag(arena, key, "div", Some(attributes));
// <html> -> <body> -> <main> -> <section> -> <div> -> <div> -> <section class="w-100 pv2 pv0-l mt4">
{
let key = children[0];
let mut attributes = HashMap::new();
attributes.insert("class", "w-100 pv2 pv0-l mt4");
let children = assert_tag(arena, key, "section", Some(attributes));
// <html> -> <body> -> <main> -> <section> -> <div> -> <div> -> <section> -> <h3 class="f2 f1-l">
{
let key = children[0];
let mut attributes = HashMap::new();
attributes.insert("class", "f2 f1-l");
let children = assert_tag(arena, key, "h3", Some(attributes));
// <html> -> <body> -> <main> -> <section> -> <div> -> <div> -> <section> -> <h3> -> text()
{
let key = children[0];
assert_text(arena, key, "Performance");
}
}
// <html> -> <body> -> <main> -> <section> -> <div> -> <div> -> <section> -> <p class="f3 lh-copy">
{
let key = children[1];
let mut attributes = HashMap::new();
attributes.insert("class", "f3 lh-copy");
let children = assert_tag(arena, key, "p", Some(attributes));
// <html> -> <body> -> <main> -> <section> -> <div> -> <div> -> <section> -> <p> -> text()
{
let key = children[0];
assert_text(arena, key, r###"Rust is blazingly fast and memory-efficient: with no runtime or
garbage collector, it can power performance-critical services, run on
embedded devices, and easily integrate with other languages."###);
}
}
}
}
}
}
}
// <html> -> <body> -> <script src="./Rust Programming Language_files/languages.js.download"/>
{
let key = children[1];
let mut attributes = HashMap::new();
attributes.insert("src", "./Rust Programming Language_files/languages.js.download");
assert_tag(arena, key, "script", Some(attributes));
}
}
}
}