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
use std::borrow::Cow;
use ego_tree::Tree;
use ego_tree::iter::Nodes;
use html5ever::driver;
use html5ever::tree_builder::QuirksMode;
use html5ever::QualName;
use tendril::TendrilSink;
use {Node, ElementRef};
use selector::Selector;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Html {
pub errors: Vec<Cow<'static, str>>,
pub quirks_mode: QuirksMode,
pub tree: Tree<Node>,
}
impl Html {
pub fn new_document() -> Self {
Html {
errors: Vec::new(),
quirks_mode: QuirksMode::NoQuirks,
tree: Tree::new(Node::Document),
}
}
pub fn new_fragment() -> Self {
Html {
errors: Vec::new(),
quirks_mode: QuirksMode::NoQuirks,
tree: Tree::new(Node::Fragment),
}
}
pub fn parse_document(document: &str) -> Self {
let parser = driver::parse_document(Self::new_document(), Default::default());
parser.one(document)
}
pub fn parse_fragment(fragment: &str) -> Self {
let parser = driver::parse_fragment(
Self::new_fragment(),
Default::default(),
QualName::new(None, ns!(html), local_name!("body")),
Vec::new(),
);
parser.one(fragment)
}
pub fn select<'a, 'b>(&'a self, selector: &'b Selector) -> Select<'a, 'b> {
Select {
inner: self.tree.nodes(),
selector: selector,
}
}
pub fn root_element(&self) -> ElementRef {
let root_node = self
.tree
.root()
.children()
.find(|child| child.value().is_element())
.expect("html node missing");
ElementRef::wrap(root_node).unwrap()
}
}
#[derive(Debug)]
pub struct Select<'a, 'b> {
inner: Nodes<'a, Node>,
selector: &'b Selector,
}
impl<'a, 'b> Iterator for Select<'a, 'b> {
type Item = ElementRef<'a>;
fn next(&mut self) -> Option<ElementRef<'a>> {
for node in self.inner.by_ref() {
if let Some(element) = ElementRef::wrap(node) {
if element.parent().is_some() && self.selector.matches(&element) {
return Some(element);
}
}
}
None
}
}
mod tree_sink;
#[cfg(test)]
mod tests {
use super::Html;
use super::Selector;
#[test]
fn root_element_fragment() {
let html = Html::parse_fragment(r#"<a href="http://github.com">1</a>"#);
let root_ref = html.root_element();
let href = root_ref.select(&Selector::parse("a").unwrap()).next().unwrap();
assert_eq!(href.inner_html(), "1");
assert_eq!(href.value().attr("href").unwrap(), "http://github.com");
}
#[test]
fn root_element_document_doctype() {
let html = Html::parse_document("<!DOCTYPE html>\n<title>abc</title>");
let root_ref = html.root_element();
let title = root_ref.select(&Selector::parse("title").unwrap()).next().unwrap();
assert_eq!(title.inner_html(), "abc");
}
#[test]
fn root_element_document_comment() {
let html = Html::parse_document("<!-- comment --><title>abc</title>");
let root_ref = html.root_element();
let title = root_ref.select(&Selector::parse("title").unwrap()).next().unwrap();
assert_eq!(title.inner_html(), "abc");
}
}