Skip to content

Truly independent web browser

LibWeb: Start the speculative parser while an SVG script blocks parsing

Tim Ledbetter8916566cf2a1
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index 8b8a32f280d..e79acc6cf94 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -612,6 +612,9 @@ bool HTMLParser::process_svg_script_end_tag_from_rust_parser(SVG::SVGScriptEleme
// pause the parser and schedule a resume check. The parser will resume from
// resume_after_parser_blocking_script when the fetch completes.
if (!m_parsing_fragment && document().pending_parsing_blocking_svg_script()) {
+ // AD-HOC: Start the speculative HTML parser while the script blocks the parser, as for an HTML script.
+ if (script_nesting_level() == 0)
+ start_the_speculative_html_parser();
m_parser_pause_flag = true;
schedule_resume_check();
}
diff --git a/Tests/LibWeb/Text/expected/HTML/speculative-html-parser/parser-blocking-svg-script.txt b/Tests/LibWeb/Text/expected/HTML/speculative-html-parser/parser-blocking-svg-script.txt
new file mode 100644
index 00000000000..e1942023eec
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/speculative-html-parser/parser-blocking-svg-script.txt
@@ -0,0 +1 @@
+The speculative parser fetched a script that follows a parser-blocking SVG script
diff --git a/Tests/LibWeb/Text/input/HTML/speculative-html-parser/parser-blocking-svg-script.html b/Tests/LibWeb/Text/input/HTML/speculative-html-parser/parser-blocking-svg-script.html
new file mode 100644
index 00000000000..ebd94d350d3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/speculative-html-parser/parser-blocking-svg-script.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const parserGate = `${crypto.randomUUID()}-parser`;
+ const path = name => `/speculative-parser-blocking-svg-script-${name}`;
+
+ const parserBlockerURL = await server.createEcho("GET", path("parser-blocker.js"), {
+ status: 200,
+ headers: { "Content-Type": "text/javascript" },
+ body: "",
+ wait_for_unblock: parserGate,
+ });
+ const laterScriptURL = await server.createEcho("GET", path("later.js"), {
+ status: 200,
+ headers: { "Content-Type": "text/javascript" },
+ body: "",
+ });
+
+ const scriptStartTag = "<scr" + "ipt";
+ const scriptEndTag = "</scr" + "ipt>";
+ const pageURL = await server.createEcho("GET", path("page.html"), {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html>
+ <svg>${scriptStartTag} href="${parserBlockerURL}">${scriptEndTag}</svg>
+ ${scriptStartTag} src="${laterScriptURL}">${scriptEndTag}`,
+ });
+
+ const frame = document.createElement("iframe");
+ frame.src = pageURL;
+ document.body.appendChild(frame);
+
+ await server.waitForRequest(laterScriptURL);
+ println("The speculative parser fetched a script that follows a parser-blocking SVG script");
+
+ await fetch(`${server.baseURL}/unblock/${parserGate}`);
+ });
+</script>