Skip to content

Truly independent web browser

LibWeb: Keep reopened documents loaded once their parent has loaded

Andreas Kling76463ee8e9c9
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 7a8bfdfbe7f..aade86c7084 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -1500,7 +1500,14 @@ WebIDL::ExceptionOr<Document*> Document::open(Optional<Utf16String> const&, Opti
// INTEROP: The HTML Standard says the document open steps do not affect whether a Document is ready for post-load
// tasks. Blink and WebKit reset their corresponding frame load-completion state, while Gecko marks the
// document loader as opened but not loaded. Reset our load gating flag for the replacement contents too.
- set_ready_for_post_load_tasks(false);
+ // Blink only treats document.open() as part of a load when the parent document still has to fire
+ // its load event, so keep the flag when the container's document has already fired its load event:
+ // nothing waits on the replacement contents then, and a later load of an ancestor must not wait on
+ // them either. The replacement contents still fire a load event of their own, so a document that is
+ // reopened in turn below them is part of their load.
+ if (auto container_document = this->container_document(); !container_document || container_document->load_event_still_needed())
+ set_ready_for_post_load_tasks(false);
+ m_opened_contents_still_loading = true;
// INTEROP: The HTML Standard does not reset the page showing flag in the document open steps. Browsers allow a
// completed Document to be reopened and complete loading again, so mark the replaced contents as no longer
@@ -6083,6 +6090,7 @@ void Document::set_ready_for_post_load_tasks(bool ready)
{
m_ready_for_post_load_tasks = ready;
if (ready) {
+ m_opened_contents_still_loading = false;
if (auto navigable = this->navigable()) {
if (auto container = navigable->container())
container->document().schedule_html_parser_end_check();
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index bb6737caf22..b1dcc206cf4 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -843,6 +843,9 @@ public:
bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
void set_ready_for_post_load_tasks(bool ready);
+ // NB: Whether this document has yet to fire its load event, including for contents written after document.open().
+ bool load_event_still_needed() const { return !m_ready_for_post_load_tasks || m_opened_contents_still_loading; }
+
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#print-when-loaded
bool print_when_loaded() const { return m_print_when_loaded; }
void set_print_when_loaded() { m_print_when_loaded = true; }
@@ -1983,6 +1986,7 @@ private:
Optional<Utf16String> m_encoding;
bool m_ready_for_post_load_tasks { false };
+ bool m_opened_contents_still_loading { false };
bool m_print_when_loaded { false };
GC::Ptr<DOMImplementation> m_implementation;
diff --git a/Tests/LibWeb/Text/expected/HTML/document-open-reopened-child-of-loaded-container-delays-parent-load.txt b/Tests/LibWeb/Text/expected/HTML/document-open-reopened-child-of-loaded-container-delays-parent-load.txt
new file mode 100644
index 00000000000..3b62dda4d8c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/document-open-reopened-child-of-loaded-container-delays-parent-load.txt
@@ -0,0 +1,2 @@
+Parent load events before replacement close: 0
+Replacement child reached complete ready state
diff --git a/Tests/LibWeb/Text/expected/HTML/document-open-reopened-child-of-loaded-container-does-not-delay-ancestor-load.txt b/Tests/LibWeb/Text/expected/HTML/document-open-reopened-child-of-loaded-container-does-not-delay-ancestor-load.txt
new file mode 100644
index 00000000000..33cc9b843b4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/document-open-reopened-child-of-loaded-container-does-not-delay-ancestor-load.txt
@@ -0,0 +1,3 @@
+Parent document ready state after its load: complete
+Top-level document loaded before child reopen: false
+Top-level document loaded while child is still open: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/infrastructure/urls/base-url/document-base-url-changes-about-srcdoc-2.https.txt b/Tests/LibWeb/Text/expected/wpt-import/html/infrastructure/urls/base-url/document-base-url-changes-about-srcdoc-2.https.txt
new file mode 100644
index 00000000000..6a5ca1ea56b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/infrastructure/urls/base-url/document-base-url-changes-about-srcdoc-2.https.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 2 tests
+
+1 Pass
+1 Fail
+Pass wrapper promise test for timeout.
+Fail document-base-url-changes-about-srcdoc-2
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-of-loaded-container-delays-parent-load.html b/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-of-loaded-container-delays-parent-load.html
new file mode 100644
index 00000000000..5a9fbc24fd7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-of-loaded-container-delays-parent-load.html
@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const parentIframe = document.createElement("iframe");
+ const initialParentLoad = new Promise(resolve => parentIframe.addEventListener("load", resolve, { once: true }));
+ document.body.appendChild(parentIframe);
+ await initialParentLoad;
+ if (document.readyState !== "complete")
+ await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
+
+ let parentLoadCount = 0;
+ parentIframe.addEventListener("load", () => ++parentLoadCount);
+
+ const parentDocument = parentIframe.contentDocument;
+ parentDocument.open().write('<iframe id="child" srcdoc="Initial child document"></iframe>');
+ const childIframe = parentDocument.querySelector("#child");
+ const initialChildLoad = new Promise(resolve => childIframe.addEventListener("load", () => setTimeout(resolve), { once: true }));
+ await initialChildLoad;
+
+ const childDocument = childIframe.contentDocument;
+ const firstChildLoad = new Promise(resolve => childIframe.addEventListener("load", () => setTimeout(resolve), { once: true }));
+ childDocument.open().write("<body>First child document</body>");
+ childDocument.close();
+ await firstChildLoad;
+
+ childDocument.open().write("<body>Replacement child document</body>");
+
+ const firstMessage = "reopened-child-of-loaded-parent-load-step-1";
+ const secondMessage = "reopened-child-of-loaded-parent-load-step-2";
+ const checkParent = "reopened-child-of-loaded-parent-load-check";
+ parentDocument.addEventListener("DOMContentLoaded", () => window.postMessage(firstMessage, "*"));
+ window.addEventListener("message", event => {
+ if (event.data === firstMessage) {
+ window.postMessage(secondMessage, "*");
+ return;
+ }
+ if (event.data === secondMessage) {
+ window.postMessage(checkParent, "*");
+ return;
+ }
+ if (event.data !== checkParent)
+ return;
+
+ println(`Parent load events before replacement close: ${parentLoadCount}`);
+ childDocument.addEventListener("readystatechange", () => {
+ if (childDocument.readyState !== "complete")
+ return;
+ println("Replacement child reached complete ready state");
+ parentIframe.remove();
+ done();
+ });
+ childDocument.close();
+ });
+ parentDocument.close();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-of-loaded-container-does-not-delay-ancestor-load.html b/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-of-loaded-container-does-not-delay-ancestor-load.html
new file mode 100644
index 00000000000..d0a5f6e14c2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/document-open-reopened-child-of-loaded-container-does-not-delay-ancestor-load.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<body>
+<script>
+ let topLevelLoaded = false;
+ window.addEventListener("load", () => (topLevelLoaded = true), { once: true });
+
+ // Keep the top-level document loading until this iframe's reopened document is closed.
+ const holdIframe = document.createElement("iframe");
+ document.body.appendChild(holdIframe);
+ const holdDocument = holdIframe.contentDocument;
+ holdDocument.open();
+
+ const parentIframe = document.createElement("iframe");
+ document.body.appendChild(parentIframe);
+
+ asyncTest(async done => {
+ const parentDocument = parentIframe.contentDocument;
+ const parentLoad = new Promise(resolve => parentIframe.addEventListener("load", () => setTimeout(resolve), { once: true }));
+ parentDocument.open().write('<iframe id="child" srcdoc="Initial child document"></iframe>');
+ parentDocument.close();
+ await parentLoad;
+ println(`Parent document ready state after its load: ${parentDocument.readyState}`);
+ println(`Top-level document loaded before child reopen: ${topLevelLoaded}`);
+
+ const childDocument = parentDocument.querySelector("#child").contentDocument;
+ childDocument.open().write("<body>Replacement child document</body>");
+
+ const topLevelLoad = new Promise(resolve => {
+ if (topLevelLoaded)
+ resolve();
+ else
+ window.addEventListener("load", resolve, { once: true });
+ });
+ holdDocument.close();
+ await topLevelLoad;
+ println(`Top-level document loaded while child is still open: ${childDocument.readyState === "loading"}`);
+
+ childDocument.close();
+ parentIframe.remove();
+ holdIframe.remove();
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/infrastructure/urls/base-url/document-base-url-changes-about-srcdoc-2.https.html b/Tests/LibWeb/Text/input/wpt-import/html/infrastructure/urls/base-url/document-base-url-changes-about-srcdoc-2.https.html
new file mode 100644
index 00000000000..7e4099eab43
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/infrastructure/urls/base-url/document-base-url-changes-about-srcdoc-2.https.html
@@ -0,0 +1,67 @@
+<!DOCTYPE html>
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<script src="../../../../common/get-host-info.sub.js"></script>
+
+<iframe src='about:blank'></iframe>
+
+<script>
+// If document.open's behavior is modified to remove the url-rewriting behavior,
+// then this test can be deleted (https://github.com/whatwg/html/issues/3989).
+setup({ explicit_done: true });
+
+// This function is called by directly by the child iframe (above), with the
+// child iframe's document. The child iframe's document contains an about:srcdoc iframe.
+window.start = childDocument => {
+ const grandchildDocument =
+ childDocument.getElementById('foo').contentDocument;
+
+ test(() => {
+ assert_equals(childDocument.URL, 'about:blank',
+ 'Child document starting URL');
+ assert_equals(grandchildDocument.URL, 'about:srcdoc',
+ 'Grandchild document starting URL');
+ const originalChildBaseURL = childDocument.baseURI;
+
+ grandchildDocument.open("", "");
+ // Verify that the document.open() trick worked: the grandchild should now
+ // have the same url as the child, and have inherited the child's base url.
+ assert_equals(grandchildDocument.URL, 'about:blank',
+ 'Grandchild document after document.open() trick');
+ assert_equals(grandchildDocument.baseURI, originalChildBaseURL,
+ 'Grandchild base URL must match child base URL');
+
+ // Give child a new base URL.
+ const baseElement = childDocument.createElement('base');
+ baseElement.href = get_host_info().REMOTE_ORIGIN;
+ childDocument.head.append(baseElement);
+
+ // Verify that changing the child's base url succeeded and did not affect
+ // the grandchild's base url.
+ const newChildBaseURL = childDocument.baseURI;
+ assert_equals(grandchildDocument.URL, 'about:blank',
+ 'Grandchild document after child gets new base URL');
+ assert_not_equals(newChildBaseURL, originalChildBaseURL,
+ 'Child base URL must change');
+ assert_equals(grandchildDocument.baseURI, originalChildBaseURL,
+ 'Grandchild base URL must not change');
+ });
+
+ done();
+};
+
+let subframe_doc = document.querySelector('iframe').contentDocument;
+subframe_doc.body.innerHTML = '<iframe srcdoc="foo" id="foo"></iframe>';
+promise_test(async t => {
+ const grandchildIframe = subframe_doc.querySelector('iframe');
+ await new Promise(resolve => {
+ grandchildIframe.onload = resolve;
+ });
+
+ assert_equals(grandchildIframe.contentDocument.URL, 'about:srcdoc');
+
+ let script = subframe_doc.createElement('script');
+ script.innerHTML = 'parent.start(document);';
+ subframe_doc.body.appendChild(script);
+}, "wrapper promise test for timeout.");
+</script>