Skip to content

Commit 1d268b9

Browse files
authored
fix parsing bug in treebuilder testsuite, fix crash in scriptdata state (#98)
1 parent 116d325 commit 1d268b9

File tree

8 files changed

+73
-7
lines changed

8 files changed

+73
-7
lines changed

src/emitters/callback.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,14 @@ where
491491
return false;
492492
}
493493

494-
crate::utils::trace_log!("last_start_tag = {:?}", self.emitter_state.last_start_tag);
495-
crate::utils::trace_log!("current_tag = {:?}", self.emitter_state.current_tag_name);
494+
crate::utils::trace_log!(
495+
"current_is_appropriate_end_tag_token: last_start_tag = {:?}",
496+
self.emitter_state.last_start_tag
497+
);
498+
crate::utils::trace_log!(
499+
"current_is_appropriate_end_tag_token: current_tag = {:?}",
500+
self.emitter_state.current_tag_name
501+
);
496502
self.emitter_state.last_start_tag == self.emitter_state.current_tag_name
497503
}
498504
}

src/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ pub(crate) mod states {
418418
c => {
419419
slf.emitter.emit_string(b"</");
420420
slf.machine_helper.flush_buffer_characters(&mut slf.emitter);
421-
reconsume_in!(slf, c, Data)
421+
reconsume_in!(slf, c, ScriptData)
422422
}
423423
}
424424
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#data
2+
<script></code><a href=foo></a></script>
3+
#errors
4+
#document
5+
| <html>
6+
| <head>
7+
| <script>
8+
| "</code><a href=foo></a>"
9+
| <body>
10+
#errors

tests/html5lib_tokenizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ fn main() {
385385
produce_testcases_from_file(&mut tests, &entry.unwrap());
386386
}
387387

388-
for entry in glob("tests/custom-html5lib-tests/*.test").unwrap() {
388+
for entry in glob("tests/custom-html5lib-tests/tokenizer/*.test").unwrap() {
389389
produce_testcases_from_file(&mut tests, &entry.unwrap());
390390
}
391391

tests/html5lib_tree_builder.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ impl Testcase {
9292
}
9393
}
9494

95-
None
95+
if has_errors {
96+
Some(rv)
97+
} else {
98+
None
99+
}
96100
}
97101
}
98102

@@ -138,6 +142,7 @@ fn map_tokenizer_state(input: State) -> html5gum::State {
138142
State::Plaintext => html5gum::State::PlainText,
139143
State::RawData(RawKind::Rcdata) => html5gum::State::RcData,
140144
State::RawData(RawKind::Rawtext) => html5gum::State::RawText,
145+
State::RawData(RawKind::ScriptData) => html5gum::State::ScriptData,
141146
x => todo!("{:?}", x),
142147
}
143148
}
@@ -170,7 +175,7 @@ fn build_test(testcase: Testcase, fname: &str, i: usize, scripting: bool) -> Tri
170175

171176
let token_emitter = Html5everEmitter::new(&mut tree_builder);
172177

173-
let input = &testcase.data[..testcase.data.len() - 1];
178+
let input = &testcase.data[..testcase.data.len().saturating_sub(1)];
174179
let mut tokenizer = Tokenizer::new_with_emitter(input, token_emitter);
175180
if let Some(state) = initial_state {
176181
tokenizer.set_state(state);
@@ -290,6 +295,10 @@ fn main() {
290295
let args = Arguments::from_args();
291296
let mut tests = Vec::new();
292297

298+
for entry in glob("tests/custom-html5lib-tests/tree-construction/*.dat").unwrap() {
299+
produce_testcases_from_file(&mut tests, &entry.unwrap());
300+
}
301+
293302
for entry in glob("tests/html5lib-tests/tree-construction/*.dat").unwrap() {
294303
produce_testcases_from_file(&mut tests, &entry.unwrap());
295304
}

tests/testutils.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1+
use std::backtrace::BacktraceStatus;
12
use std::ops::Deref;
2-
use std::panic::UnwindSafe;
3+
use std::panic::{self, UnwindSafe};
4+
use std::sync::Once;
35

46
use libtest_mimic::Failed;
57

68
use html5gum::testutils::OUTPUT;
79

10+
/// Improved panic messages for the html5lib-tests test suite.
11+
///
12+
/// This function catches panics, prepends the log message to the panic message, and captures
13+
/// stacktraces.
14+
///
15+
/// libtest_mimic already catches panics but doesn't provide stacktraces for some reason.
16+
///
17+
/// Because custom test harnesses in Rust do not support capturing of stdout, we have to implement
18+
/// our own "log buffer", and append it to the error message in case of test failure. OUTPUT is the
19+
/// log buffer -- it is bound in size and compiled out in release mode. Code can use the
20+
/// `crate::trace_log` macro to write lines to it.
821
pub fn catch_unwind_and_report(f: impl FnOnce() + UnwindSafe) -> Result<(), Failed> {
22+
static PANIC_HOOK: Once = Once::new();
23+
PANIC_HOOK.call_once(|| {
24+
panic::set_hook(Box::new(|_info| {
25+
let backtrace = std::backtrace::Backtrace::capture();
26+
if backtrace.status() != BacktraceStatus::Captured {
27+
html5gum::testutils::trace_log(
28+
"PANIC BACKTRACE: did not capture, use RUST_BACKTRACE=1",
29+
);
30+
} else {
31+
// clean up noisy frames from backtrace
32+
let mut backtrace_str = String::new();
33+
let mut seen_begin_unwind = false;
34+
for line in format!("{:#?}", backtrace).lines() {
35+
if line.contains("\"std::panicking::try::do_call\"") {
36+
break;
37+
} else if seen_begin_unwind {
38+
backtrace_str.push_str(line);
39+
backtrace_str.push('\n');
40+
} else if line.contains("\"core::panicking::panic_fmt\"") {
41+
seen_begin_unwind = true;
42+
}
43+
}
44+
html5gum::testutils::trace_log(&format!("\nPANIC BACKTRACE:\n{}", backtrace_str));
45+
}
46+
}));
47+
});
48+
949
let result = std::panic::catch_unwind(f);
1050

1151
let mut msg = String::new();
@@ -28,6 +68,7 @@ pub fn catch_unwind_and_report(f: impl FnOnce() + UnwindSafe) -> Result<(), Fail
2868
// If that fails, try to turn it into a &'static str
2969
.or_else(|| e.downcast_ref::<&'static str>().map(Deref::deref))
3070
{
71+
msg.push_str("PANIC: ");
3172
msg.push_str(s);
3273
}
3374

0 commit comments

Comments
 (0)