Truly independent web browser
LibWeb: Treat the navigables of a removed iframe as discarded
Andreas Kling6ff618a0de6c
diff --git a/Libraries/LibWeb/HTML/NavigableContainer.cpp b/Libraries/LibWeb/HTML/NavigableContainer.cppindex d21b13c7a57..33f41a35a3b 100644--- a/Libraries/LibWeb/HTML/NavigableContainer.cpp+++ b/Libraries/LibWeb/HTML/NavigableContainer.cpp@@ -199,6 +199,12 @@ HTML::WindowProxy* NavigableContainer::content_window(){if (!m_content_navigable)return nullptr;++ // INTEROP: Browsers discard the navigables of a removed navigable container right away, while the spec only+ // destroys their documents in a later task. So a navigable in a destroyed subtree has no WindowProxy here.+ if (m_content_navigable->is_in_a_destroyed_subtree())+ return nullptr;+return m_content_navigable->active_window_proxy().ptr();}diff --git a/Libraries/LibWeb/HTML/RemoteWindow.cpp b/Libraries/LibWeb/HTML/RemoteWindow.cppindex e2a1c03d3de..474587463a8 100644--- a/Libraries/LibWeb/HTML/RemoteWindow.cpp+++ b/Libraries/LibWeb/HTML/RemoteWindow.cpp@@ -247,10 +247,11 @@ WebIDL::ExceptionOr<void> RemoteWindow::post_message(JS::Realm& realm, JS::Value// process, which forwards it there. That process represents the source's navigable, which names the source.// A source whose [[Window]] has no navigable is null there.Optional<CrossProcessId> source_navigable_id;+ // NB: A navigable in a destroyed subtree is discarded, like it is for the source's window.parent.if (auto remote_window = prepared.source->remote_window()) {- if (auto source_navigable = remote_window->navigable())+ if (auto source_navigable = remote_window->navigable(); source_navigable && !source_navigable->is_in_a_destroyed_subtree())source_navigable_id = source_navigable->id();- } else if (auto source_navigable = prepared.source->window()->navigable()) {+ } else if (auto source_navigable = prepared.source->window()->navigable(); source_navigable && !source_navigable->is_in_a_destroyed_subtree()) {source_navigable_id = source_navigable->id();}PostedMessageDescriptor posted_message {diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cppindex 51f46b6b7bb..85dcaa8bf3d 100644--- a/Libraries/LibWeb/HTML/Window.cpp+++ b/Libraries/LibWeb/HTML/Window.cpp@@ -1403,12 +1403,20 @@ u32 Window::length()return associated_document().document_tree_child_navigables().size();}+// INTEROP: Browsers discard the navigables of a removed navigable container right away, while the spec only destroys+// their documents in a later task. So we treat a navigable in a destroyed subtree as discarded, except while+// the document is being unloaded: browsers unload it during the removal, when it is not discarded yet.+static bool has_been_discarded(Navigable const& navigable, DOM::Document const& document)+{+ return navigable.is_in_a_destroyed_subtree() && document.unload_counter() == 0;+}+// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-topGC::Ptr<WindowProxy const> Window::top() const{// 1. If this's navigable is null, then return null.auto navigable = this->navigable();- if (!navigable)+ if (!navigable || has_been_discarded(*navigable, associated_document()))return {};// 2. Return this's navigable's top-level traversable's active WindowProxy.@@ -1460,7 +1468,7 @@ GC::Ptr<WindowProxy const> Window::parent() constGC::Ptr<Navigable> navigable = this->navigable();// 2. If navigable is null, then return null.- if (!navigable)+ if (!navigable || has_been_discarded(*navigable, associated_document()))return {};// 3. If navigable's parent is not null, then set navigable to navigable's parent.diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/windows/nested-browsing-contexts/window-parent-null.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/windows/nested-browsing-contexts/window-parent-null.txtindex 9259aaeca86..6571be8badf 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/windows/nested-browsing-contexts/window-parent-null.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/windows/nested-browsing-contexts/window-parent-null.txt@@ -2,6 +2,6 @@ Harness status: OKFound 2 tests-2 Fail-Fail `window.parent` is null when browsing context container element removed-Fail `window.parent` null when parent browsing context container removed\ No newline at end of file+2 Pass+Pass `window.parent` is null when browsing context container element removed+Pass `window.parent` null when parent browsing context container removed\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/windows/nested-browsing-contexts/window-top-null.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/windows/nested-browsing-contexts/window-top-null.txtindex 69bac9c9159..a74341f885a 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/windows/nested-browsing-contexts/window-top-null.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/windows/nested-browsing-contexts/window-top-null.txt@@ -2,6 +2,6 @@ Harness status: OKFound 2 tests-2 Fail-Fail `window.top` is null when browsing context container element removed-Fail `window.top`null when any ancestor browsing context container removed\ No newline at end of file+2 Pass+Pass `window.top` is null when browsing context container element removed+Pass `window.top`null when any ancestor browsing context container removed\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-append-to-child-document.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-append-to-child-document.txtindex 348202de80e..4459baa881a 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-append-to-child-document.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-append-to-child-document.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail Append iframe element to its own child document\ No newline at end of file+1 Pass+Pass Append iframe element to its own child document\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-synchronously-discard.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-synchronously-discard.txtindex c7c962b8a52..c5ab68e81b4 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-synchronously-discard.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-synchronously-discard.txt@@ -2,5 +2,5 @@ Harness status: OKFound 1 tests-1 Fail-Fail IFrame discards are processed synchronously\ No newline at end of file+1 Pass+Pass IFrame discards are processed synchronously\ No newline at end of file