Skip to content

Truly independent web browser

LibWeb: Finish a parser closed from a script that has paused it

Andreas Klinga2fc4f29b7e0
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 22b7fe6b3d8..16ad5aa1d8f 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -1722,7 +1722,9 @@ WebIDL::ExceptionOr<void> Document::close()
return {};
// run() may have paused on a blocking script (e.g. from document.write inside an inline script).
- if (has_pending_parsing_blocking_script()) {
+ // NB: An SVG script that the parser is executing keeps the parser paused, so run() did not tokenize anything. The
+ // outermost invocation of the parser reaches the explicit "EOF" character once that script is done.
+ if (has_pending_parsing_blocking_script() || parser->is_paused()) {
parser->set_post_parse_action(move(finish_script_created_parser));
return {};
}
diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
index af55071d47e..4cbd284e8c3 100644
--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp
@@ -285,10 +285,20 @@ void HTMLParser::run(HTMLTokenizer::StopAtInsertionPoint stop_at_insertion_point
{
m_stop_parsing = false;
+ // NB: document.close() leaves the explicit "EOF" character to us when a script that we are executing has paused
+ // the parser. Once such a script is done, the outermost invocation tokenizes up to that character and finishes.
+ bool finish_script_created_parser = false;
+
for (;;) {
if (m_parser_pause_flag)
break;
+ if (m_script_created && m_script_nesting_level == 0 && m_post_parse_action && m_tokenizer.is_input_stream_closed()
+ && stop_at_insertion_point == HTMLTokenizer::StopAtInsertionPoint::Yes) {
+ stop_at_insertion_point = HTMLTokenizer::StopAtInsertionPoint::No;
+ finish_script_created_parser = true;
+ }
+
auto result = rust_html_parser_run_document(
m_rust_parser,
m_tokenizer.ffi_handle({}),
@@ -319,6 +329,9 @@ void HTMLParser::run(HTMLTokenizer::StopAtInsertionPoint stop_at_insertion_point
m_tokenizer.parser_did_run({});
+ if (finish_script_created_parser && !m_parser_pause_flag)
+ invoke_post_parse_action();
+
if (m_document->parser().ptr() == this)
m_document->schedule_text_fragment_search_after_parser_progress({});
}
diff --git a/Tests/LibWeb/Text/expected/HTML/document-close-in-written-svg-script.txt b/Tests/LibWeb/Text/expected/HTML/document-close-in-written-svg-script.txt
new file mode 100644
index 00000000000..4fa84134c8c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/document-close-in-written-svg-script.txt
@@ -0,0 +1,3 @@
+After close: <svg><script>document.close();</script></svg><p>first</p>
+readyState: interactive
+After write: <html><head><style></style></head><body><p>second</p></body></html>
diff --git a/Tests/LibWeb/Text/input/HTML/document-close-in-written-svg-script.html b/Tests/LibWeb/Text/input/HTML/document-close-in-written-svg-script.html
new file mode 100644
index 00000000000..c63948d74c3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/document-close-in-written-svg-script.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<iframe id="iframe"></iframe>
+<script>
+ test(() => {
+ const iframeDocument = document.getElementById("iframe").contentDocument;
+ iframeDocument.open();
+ iframeDocument.write("<body><svg><script>document.close();<\/script></svg><p>first</p>");
+ println(`After close: ${iframeDocument.body.innerHTML}`);
+ println(`readyState: ${iframeDocument.readyState}`);
+
+ iframeDocument.write("<style></style><p>second</p>");
+ println(`After write: ${iframeDocument.documentElement.outerHTML}`);
+ iframeDocument.close();
+ });
+</script>