Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(parser): preserve stylesheet pauses across encoding notices

ldm036ae738e6fe9
diff --git a/moli-parser/src/session.rs b/moli-parser/src/session.rs
index 3025906a4..de78b3a77 100644
--- a/moli-parser/src/session.rs
+++ b/moli-parser/src/session.rs
@@ -91,7 +91,15 @@ impl EmbedderPausingTreeBuilder {
_ => None,
};
let result = self.inner.process_token(token, line_number);
- if !matches!(result, TokenSinkResult::Continue) {
+ // Encoding notices are advisory for this already-decoded input. A tag
+ // such as <link rel=stylesheet charset=utf-8> can also require an
+ // embedder pause, which must be returned at this tag boundary. Leaving
+ // it pending would expose it on a later character or error token,
+ // where html5ever requires Continue and would otherwise panic.
+ if !matches!(
+ result,
+ TokenSinkResult::Continue | TokenSinkResult::EncodingIndicator(_)
+ ) {
return result;
}
let svg_script_handoff = if let Some(local_name) = foreign_end_tag {
@@ -130,7 +138,7 @@ impl EmbedderPausingTreeBuilder {
// actual script and custom-element handoffs using sink-owned state.
return TokenSinkResult::Script(ParseHandle::new(stylesheet, None));
}
- TokenSinkResult::Continue
+ result
}
}
diff --git a/moli-parser/src/stream.rs b/moli-parser/src/stream.rs
index fa14f3b82..5188b73be 100644
--- a/moli-parser/src/stream.rs
+++ b/moli-parser/src/stream.rs
@@ -2237,6 +2237,51 @@ mod tests {
);
}
+ #[test]
+ fn parser_stream_encoding_indicator_preserves_stylesheet_pause() {
+ for attributes in [
+ "charset=utf-8",
+ "charset=windows-1252",
+ "charset=unknown-encoding",
+ "http-equiv=content-type content='text/css; charset=utf-8'",
+ ] {
+ let stream = DocumentStream::new_scripting_enabled_parser_stream_for_testing(
+ Url::parse("https://example.test/page.html").expect("test url"),
+ );
+ let tail = "\n<script>window.afterStylesheet = true;</script><p>café</p>";
+ let first = stream.pump_parser_step(&format!(
+ "<!doctype html><body><link rel=stylesheet href='/slow.css' {attributes}>{tail}"
+ ));
+ assert!(
+ matches!(
+ first.result,
+ ParserPumpStep::Yield(ParserYield::BlockingStylesheet(_))
+ ),
+ "encoding notification must not bypass the stylesheet pause: {attributes}"
+ );
+ assert_eq!(first.discovered_blocking_stylesheet_inputs.len(), 1);
+ assert_eq!(stream.snapshot_pending_input(), tail);
+
+ let second = stream.pump_parser_step("");
+ assert!(matches!(
+ second.result,
+ ParserPumpStep::Yield(ParserYield::Script(_))
+ ));
+ assert_eq!(stream.snapshot_pending_input(), "<p>café</p>");
+ assert!(matches!(
+ stream.pump_parser_step("").result,
+ ParserPumpStep::InputDrained
+ ));
+ let document = stream.finish();
+ let paragraphs = document.elements_by_tag_name(document.document_node_id(), "p", false);
+ assert_eq!(paragraphs.len(), 1);
+ assert_eq!(
+ document.text_content(paragraphs[0]).as_deref(),
+ Some("café")
+ );
+ }
+ }
+
#[test]
fn parser_stream_feed_consumes_custom_element_handoff_without_runtime_owner() {
let stream = DocumentStream::new_scripting_enabled_parser_stream_for_testing(
diff --git a/moli/tests/fetch_cli.rs b/moli/tests/fetch_cli.rs
index 154c8e436..663aca3f5 100644
--- a/moli/tests/fetch_cli.rs
+++ b/moli/tests/fetch_cli.rs
@@ -1981,6 +1981,43 @@ fn cli_fetch_scopes_custom_headers_to_main_document_request() -> Result<()> {
Ok(())
}
+#[test]
+fn cli_dump_json_renders_body_stylesheet_with_charset_and_curly_quote_meta() -> Result<()> {
+ let html = concat!(
+ "<!doctype html><head><title>Parser regression</title>",
+ "<meta name=\"viewport\" content=\"initial-scale=0.4\">",
+ "<meta name=“viewport“ content=“width=device-width, initial-scale=1“>",
+ "</head><body>",
+ "<link rel=stylesheet href='data:text/css,body%7Bcolor:rgb(1,2,3)%7D' charset=utf-8>",
+ "\n<script>document.body.setAttribute('data-color', getComputedStyle(document.body).color);</script>",
+ "<main id=after>café</main></body>",
+ );
+ let url = format!(
+ "data:text/html;charset=utf-8;base64,{}",
+ BASE64_STANDARD.encode(html),
+ );
+ let output = run_fetch_cli_with_wait_until_and_dump(&url, "domcontentloaded", "json")?;
+ assert!(
+ output.status.success(),
+ "moli fetch failed: {}",
+ clean_output(&output.stderr),
+ );
+ let payload: Value = serde_json::from_slice(&output.stdout)?;
+ assert_json_dump_shape(&payload, &url, 200);
+ assert_eq!(payload["title"], "Parser regression");
+ let rendered = payload["html"].as_str().expect("rendered HTML");
+ assert!(rendered.contains("name=\"“viewport“\""), "{rendered}");
+ assert!(
+ rendered.contains("data-color=\"rgb(1, 2, 3)\""),
+ "{rendered}"
+ );
+ assert!(
+ rendered.contains("<main id=\"after\">café</main>"),
+ "{rendered}"
+ );
+ Ok(())
+}
+
#[test]
fn cli_dump_json_emits_final_url_status_and_html() -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;