Skip to content

Truly independent web browser

LibWeb: Stop lazy loading an iframe when it is removed

Andreas Klingdd50c5394fa5
diff --git a/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp b/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
index 8c2663ae42c..6b60390211e 100644
--- a/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
@@ -212,6 +212,19 @@ void HTMLIFrameElement::removed_from(IsSubtreeRoot is_subtree_root, DOM::Node* o
// When an iframe element is removed from a document, the user agent must destroy the nested navigable of the element.
destroy_the_child_navigable();
+
+ // AD-HOC: A lazy loading iframe's resumption steps navigate its content navigable, which is gone now. Inserting
+ // the iframe again processes its attributes, and lazy loads it anew.
+ stop_lazy_loading();
+}
+
+// Stops intersection-observing the iframe as a lazy loading element, and sets its lazy load resumption steps to null.
+// NB: Only an iframe that has lazy load resumption steps is intersection-observed. One without a src never started lazy
+// loading, and its document can have no lazy load intersection observer to stop.
+void HTMLIFrameElement::stop_lazy_loading()
+{
+ if (take_lazy_load_resumption_steps_internal())
+ document().stop_intersection_observing_a_lazy_loading_element(*this);
}
// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps
diff --git a/Libraries/LibWeb/HTML/HTMLIFrameElement.h b/Libraries/LibWeb/HTML/HTMLIFrameElement.h
index 2d7c79e4148..834029bd482 100644
--- a/Libraries/LibWeb/HTML/HTMLIFrameElement.h
+++ b/Libraries/LibWeb/HTML/HTMLIFrameElement.h
@@ -35,6 +35,8 @@ public:
void set_current_navigation_was_lazy_loaded(bool value);
+ void stop_lazy_loading();
+
Optional<HighResolutionTime::DOMHighResTimeStamp> const& pending_resource_start_time() const { return m_pending_resource_start_time; }
void set_pending_resource_start_time(Optional<HighResolutionTime::DOMHighResTimeStamp> time) { m_pending_resource_start_time = time; }
diff --git a/Tests/LibWeb/Crash/HTML/lazy-iframe-removed-before-lazy-load-observer-runs.html b/Tests/LibWeb/Crash/HTML/lazy-iframe-removed-before-lazy-load-observer-runs.html
new file mode 100644
index 00000000000..a4944f10e59
--- /dev/null
+++ b/Tests/LibWeb/Crash/HTML/lazy-iframe-removed-before-lazy-load-observer-runs.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<!-- The page's intersection observer runs before the lazy load intersection observer, and removes the lazy iframe
+ the latter goes on to navigate. -->
+<html class="test-wait">
+<body>
+<script>
+ const iframe = document.createElement("iframe");
+ iframe.loading = "lazy";
+ iframe.src = "data:text/html,hi";
+ new IntersectionObserver(() => {
+ iframe.remove();
+ requestAnimationFrame(() => requestAnimationFrame(() => document.documentElement.classList.remove("test-wait")));
+ }).observe(iframe);
+ document.body.appendChild(iframe);
+</script>
+</body>
+</html>
diff --git a/Tests/LibWeb/Crash/HTML/lazy-iframe-removed-before-lazy-load-observer-runs.html.headers b/Tests/LibWeb/Crash/HTML/lazy-iframe-removed-before-lazy-load-observer-runs.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Crash/HTML/lazy-iframe-removed-before-lazy-load-observer-runs.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html