Skip to content

Truly independent web browser

LibWeb: Resume a lazy iframe’s loading when it’s made eager

sideshowbarker01ab62a6a9ab
diff --git a/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp b/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
index fcc9f5eded3..45d4897bd33 100644
--- a/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp
@@ -88,6 +88,23 @@ void HTMLIFrameElement::attribute_changed(Utf16FlyString const& name, Optional<U
document().page().client().page_did_change_navigable_container_state(m_content_navigable->id(), replicated_container_state());
}
+ // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading
+ // When the loading attribute's state is changed to the Eager state, the user agent must run these steps:
+ if (name == AttributeNames::loading && lazy_loading_state(old_value) == LazyLoading::Lazy && lazy_loading_state(value) == LazyLoading::Eager) {
+ // 1. Let resumptionSteps be the iframe element's lazy load resumption steps.
+ // 3. Set the iframe's lazy load resumption steps to null.
+ auto resumption_steps = take_lazy_load_resumption_steps_internal();
+
+ // 2. If resumptionSteps is null, then return.
+ if (resumption_steps) {
+ // NB: Only an iframe with lazy load resumption steps is intersection-observed, so this one no longer is.
+ document().stop_intersection_observing_a_lazy_loading_element(*this);
+
+ // 4. Invoke resumptionSteps.
+ resumption_steps->function()();
+ }
+ }
+
if (name == HTML::AttributeNames::width || name == HTML::AttributeNames::height)
CSS::Invalidation::invalidate_style_after_embedded_content_geometry_change(*this);
diff --git a/Libraries/LibWeb/HTML/LazyLoadingElement.h b/Libraries/LibWeb/HTML/LazyLoadingElement.h
index c4e89a249db..4ed42be7b5d 100644
--- a/Libraries/LibWeb/HTML/LazyLoadingElement.h
+++ b/Libraries/LibWeb/HTML/LazyLoadingElement.h
@@ -32,16 +32,19 @@ template<typename T>
class LazyLoadingElement {
public:
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-loading-attributes
- [[nodiscard]] LazyLoading lazy_loading_attribute() const
+ [[nodiscard]] static LazyLoading lazy_loading_state(Optional<Utf16String> const& value)
{
- auto& element = static_cast<T const&>(*this);
-
- auto value = element.attribute(HTML::AttributeNames::loading);
if (value.has_value() && value->equals_ignoring_ascii_case(u"lazy"sv))
return LazyLoading::Lazy;
return LazyLoading::Eager;
}
+ [[nodiscard]] LazyLoading lazy_loading_attribute() const
+ {
+ auto& element = static_cast<T const&>(*this);
+ return lazy_loading_state(element.attribute(HTML::AttributeNames::loading));
+ }
+
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#will-lazy-load-element-steps
[[nodiscard]] bool will_lazy_load_element() const
{
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/lazy-iframe-made-eager-loads-without-scrolling-into-view.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/lazy-iframe-made-eager-loads-without-scrolling-into-view.txt
new file mode 100644
index 00000000000..7ffbd7a9df5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/lazy-iframe-made-eager-loads-without-scrolling-into-view.txt
@@ -0,0 +1,5 @@
+loaded after loading=eager
+loaded after src change
+frames after scrolling into view: 1
+WebContent#0
+ iframe#0: remote WebContent#1
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-loading-lazy-to-eager.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-loading-lazy-to-eager.txt
new file mode 100644
index 00000000000..a42c7de471d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-loading-lazy-to-eager.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Below-viewport iframes with loading='lazy' load when set to loading='eager' or the `loading` attribute is removed
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/lazy-iframe-made-eager-loads-without-scrolling-into-view.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/lazy-iframe-made-eager-loads-without-scrolling-into-view.html
new file mode 100644
index 00000000000..86b4d0e4e7e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/lazy-iframe-made-eager-loads-without-scrolling-into-view.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+ #spacer {
+ height: 3000px;
+ }
+ iframe {
+ display: block;
+ width: 200px;
+ height: 100px;
+ border: 0;
+ }
+</style>
+<div id="spacer"></div>
+<script>
+ // A lazy iframe below the fold loads as soon as its loading attribute goes to the Eager state, and has nothing left
+ // to load once it's scrolled into view — even after a later navigation hands its document to another process.
+ promiseTest(async () => {
+ const httpServer = httpTestServer();
+ const crossSiteUrl = async name => {
+ const url = new URL(
+ await httpServer.createEcho("GET", `/oop-lazy-iframe-made-eager-${name}`, {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html>${name}`,
+ })
+ );
+ url.hostname = uniqueLocalhostHostname(`oop-lazy-iframe-made-eager-${name}`);
+ return url;
+ };
+ const firstUrl = await crossSiteUrl("first");
+ const secondUrl = await crossSiteUrl("second");
+
+ const iframe = document.createElement("iframe");
+ const nextLoad = () => new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
+ iframe.loading = "lazy";
+ iframe.src = firstUrl;
+ document.getElementById("spacer").after(iframe);
+
+ let loaded = nextLoad();
+ iframe.loading = "eager";
+ await loaded;
+ println("loaded after loading=eager");
+
+ loaded = nextLoad();
+ iframe.src = secondUrl;
+ await loaded;
+ println("loaded after src change");
+
+ window.scrollTo(0, iframe.getBoundingClientRect().top + scrollY);
+ for (let i = 0; i < 5; ++i)
+ await animationFrame();
+ println(`frames after scrolling into view: ${window.length}`);
+ println(internals.dumpSiteIsolationProcessTree().trimEnd());
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-loading-lazy-to-eager.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-loading-lazy-to-eager.html
new file mode 100644
index 00000000000..216c3c4db56
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/iframe-loading-lazy-to-eager.html
@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<head>
+ <title>Below-viewport iframes with loading='lazy' load when set to
+ loading='eager' or the `loading` attribute is removed</title>
+ <link rel="author" title="Dom Farolino" href="mailto:[email protected]">
+ <link rel="help" href="https://github.com/whatwg/html/pull/5579">
+ <script src="../../../../resources/testharness.js"></script>
+ <script src="../../../../resources/testharnessreport.js"></script>
+</head>
+
+<script>
+ const t = async_test("Below-viewport iframes with loading='lazy' load when " +
+ "set to loading='eager' or the `loading` attribute is " +
+ "removed");
+
+ const iframe_1_onload = t.unreached_func("#iframe_1 should not load before " +
+ "the window load event");
+ const iframe_2_onload = t.unreached_func("#iframe_2 should not load before " +
+ "the window load event");
+
+ window.addEventListener("load", t.step_func(() => {
+ const iframe_1 = document.querySelector('#iframe_1');
+ const iframe_2 = document.querySelector('#iframe_2');
+
+ const iframe_1_promise = new Promise((resolve, reject) => {
+ iframe_1.onerror = reject;
+ iframe_1.onload = resolve;
+ });
+
+ const iframe_2_promise = new Promise((resolve, reject) => {
+ iframe_2.onerror = reject;
+ iframe_2.onload = resolve;
+ });
+
+ Promise.all([iframe_1_promise, iframe_2_promise])
+ .then(t.step_func_done())
+ .catch(t.unreached_func("The iframes should load successfully"));
+
+ // Kick off the requests.
+ iframe_1.loading = 'eager';
+ iframe_2.removeAttribute('loading'); // unset the attribute, putting it in
+ // the default (eager) state.
+ }));
+
+</script>
+
+<body>
+ <div style="height:1000vh;"></div>
+ <iframe id="iframe_1"
+ src="resources/subframe.html?1"
+ loading="lazy" onload="iframe_1_onload();"></iframe>
+ <iframe id="iframe_2"
+ src="resources/subframe.html?2"
+ loading="lazy" onload="iframe_2_onload();"></iframe>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/resources/subframe.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/resources/subframe.html
new file mode 100644
index 00000000000..07cb999afae
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-iframe-element/resources/subframe.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<body>
+ <p>Subframe</p>
+</body>