-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathhtml-tree-builder.rs
303 lines (270 loc) · 9.11 KB
/
html-tree-builder.rs
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
// Copyright 2014-2017 The html5ever Project Developers. See the
// COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
extern crate markup5ever_rcdom as rcdom;
#[macro_use]
extern crate html5ever;
mod foreach_html5lib_test;
use foreach_html5lib_test::foreach_html5lib_test;
use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::io::BufRead;
use std::path::Path;
use std::{env, fs, io, iter, mem};
use html5ever::tendril::{StrTendril, TendrilSink};
use html5ever::{parse_document, parse_fragment, ParseOpts};
use html5ever::{LocalName, QualName};
use rcdom::{Handle, NodeData, RcDom};
use util::runner::{run_all, Test};
mod util {
pub mod runner;
}
fn parse_tests<It: Iterator<Item = String>>(mut lines: It) -> Vec<HashMap<String, String>> {
let mut tests = vec![];
let mut test = HashMap::new();
let mut key: Option<String> = None;
let mut val = String::new();
macro_rules! finish_val ( () => (
match key.take() {
None => (),
Some(key) => {
assert!(test.insert(key, mem::take(&mut val)).is_none());
}
}
));
macro_rules! finish_test ( () => (
if !test.is_empty() {
tests.push(mem::take(&mut test));
}
));
loop {
match lines.next() {
None => break,
Some(line) => {
if let Some(rest) = line.strip_prefix('#') {
finish_val!();
if line == "#data" {
finish_test!();
}
key = Some(rest.to_owned());
} else {
val.push_str(&line);
val.push('\n');
}
},
}
}
finish_val!();
finish_test!();
tests
}
fn serialize(buf: &mut String, indent: usize, handle: Handle) {
buf.push('|');
buf.extend(iter::repeat(" ").take(indent));
let node = handle;
match node.data {
NodeData::Document => panic!("should not reach Document"),
NodeData::Doctype {
ref name,
ref public_id,
ref system_id,
} => {
buf.push_str("<!DOCTYPE ");
buf.push_str(name);
if !public_id.is_empty() || !system_id.is_empty() {
buf.push_str(&format!(" \"{}\" \"{}\"", public_id, system_id));
}
buf.push_str(">\n");
},
NodeData::Text { ref contents } => {
buf.push('"');
buf.push_str(&contents.borrow());
buf.push_str("\"\n");
},
NodeData::Comment { ref contents } => {
buf.push_str("<!-- ");
buf.push_str(contents);
buf.push_str(" -->\n");
},
NodeData::Element {
ref name,
ref attrs,
..
} => {
buf.push('<');
match name.ns {
ns!(svg) => buf.push_str("svg "),
ns!(mathml) => buf.push_str("math "),
_ => (),
}
buf.push_str(&name.local);
buf.push_str(">\n");
let mut attrs = attrs.borrow().clone();
attrs.sort_by(|x, y| x.name.local.cmp(&y.name.local));
// FIXME: sort by UTF-16 code unit
for attr in attrs.into_iter() {
buf.push('|');
buf.extend(iter::repeat(" ").take(indent + 2));
match attr.name.ns {
ns!(xlink) => buf.push_str("xlink "),
ns!(xml) => buf.push_str("xml "),
ns!(xmlns) => buf.push_str("xmlns "),
_ => (),
}
buf.push_str(&format!("{}=\"{}\"\n", attr.name.local, attr.value));
}
},
NodeData::ProcessingInstruction { .. } => unreachable!(),
}
for child in node.children.borrow().iter() {
serialize(buf, indent + 2, child.clone());
}
if let NodeData::Element {
ref template_contents,
..
} = node.data
{
if let Some(ref content) = &*template_contents.borrow() {
buf.push('|');
buf.extend(iter::repeat(" ").take(indent + 2));
buf.push_str("content\n");
for child in content.children.borrow().iter() {
serialize(buf, indent + 4, child.clone());
}
}
}
}
fn make_test(
tests: &mut Vec<Test>,
ignores: &HashSet<String>,
filename: &str,
idx: usize,
fields: HashMap<String, String>,
) {
let scripting_flags = &[false, true];
let scripting_flags = if fields.contains_key("script-off") {
&scripting_flags[0..1]
} else if fields.contains_key("script-on") {
&scripting_flags[1..2]
} else {
&scripting_flags[0..2]
};
let name = format!("tb: {}-{}", filename, idx);
for scripting_enabled in scripting_flags {
let test = make_test_desc_with_scripting_flag(ignores, &name, &fields, *scripting_enabled);
tests.push(test);
}
}
fn make_test_desc_with_scripting_flag(
ignores: &HashSet<String>,
name: &str,
fields: &HashMap<String, String>,
scripting_enabled: bool,
) -> Test {
let expect_field = |key| {
fields
.get(key)
.unwrap_or_else(|| panic!("missing field {}, testcase: {:?}", key, fields))
.to_string()
};
let mut data = expect_field("data");
data.pop();
let expected = expect_field("document").trim_end_matches('\n').to_string();
let context = fields
.get("document-fragment")
.map(|field| context_name(field.trim_end_matches('\n')));
let skip = ignores.contains(name);
let mut name = name.to_owned();
if scripting_enabled {
name.push_str(" (scripting enabled)");
} else {
name.push_str(" (scripting disabled)");
};
Test {
name,
skip,
test: Box::new(move || {
// Do this here because Tendril isn't Send.
let data = StrTendril::from_slice(&data);
let mut opts: ParseOpts = Default::default();
opts.tree_builder.scripting_enabled = scripting_enabled;
let mut result = String::new();
match context {
None => {
let dom = parse_document(RcDom::default(), opts).one(data.clone());
for child in dom.document.children.borrow().iter() {
serialize(&mut result, 1, child.clone());
}
},
Some(ref context) => {
let dom = parse_fragment(RcDom::default(), opts, context.clone(), vec![])
.one(data.clone());
// fragment case: serialize children of the html element
// rather than children of the document
let doc = &dom.document;
let root = &doc.children.borrow()[0];
for child in root.children.borrow().iter() {
serialize(&mut result, 1, child.clone());
}
},
};
let len = result.len();
result.truncate(len - 1); // drop the trailing newline
if result != expected {
panic!(
"\ninput: {}\ngot:\n{}\nexpected:\n{}\n",
data, result, expected
);
}
}),
}
}
fn context_name(context: &str) -> QualName {
if let Some(cx) = context.strip_prefix("svg ") {
QualName::new(None, ns!(svg), LocalName::from(cx))
} else if let Some(cx) = context.strip_prefix("math ") {
QualName::new(None, ns!(mathml), LocalName::from(cx))
} else {
QualName::new(None, ns!(html), LocalName::from(context))
}
}
fn tests(src_dir: &Path, ignores: &HashSet<String>) -> Vec<Test> {
let mut tests = vec![];
foreach_html5lib_test(
src_dir,
"html5lib-tests/tree-construction",
OsStr::new("dat"),
|path, file| {
let buf = io::BufReader::new(file);
let lines = buf.lines().map(|res| res.expect("couldn't read"));
let data = parse_tests(lines);
for (i, test) in data.into_iter().enumerate() {
make_test(
&mut tests,
ignores,
path.file_name().unwrap().to_str().unwrap(),
i,
test,
);
}
},
);
tests
}
fn main() {
let src_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let mut ignores = HashSet::new();
{
let f = fs::File::open(src_dir.join("data/test/ignore")).unwrap();
let r = io::BufReader::new(f);
for ln in r.lines() {
ignores.insert(ln.unwrap().trim_end().to_string());
}
}
run_all(tests(src_dir, &ignores));
}