Skip to content

Truly independent web browser

LibWeb: Load lazy images near the edge of a scroller

sideshowbarkercd08b4fcb70d
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 279d6d7558e..13a2fbad49e 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -7663,28 +7663,29 @@ void Document::queue_an_intersection_observer_entry(IntersectionObserver::Inters
queue_intersection_observer_task();
}
+static CSSPixelRect apply_observer_scroll_margin(void const* context, CSSPixelRect clip_rect)
+{
+ auto const& scroll_margin = static_cast<IntersectionObserver::IntersectionObserver const*>(context)->scroll_margin_values();
+ if (scroll_margin.is_empty())
+ return clip_rect;
+ clip_rect.inflate(
+ scroll_margin[0].to_px(clip_rect.height()),
+ scroll_margin[1].to_px(clip_rect.width()),
+ scroll_margin[2].to_px(clip_rect.height()),
+ scroll_margin[3].to_px(clip_rect.width()));
+ return clip_rect;
+}
+
// https://www.w3.org/TR/intersection-observer/#compute-the-intersection
static Optional<CSSPixelRect> compute_intersection(Layout::BegunRead const& read, GC::Ref<Element> target, CSSPixelRect target_rect, IntersectionObserver::IntersectionObserver const& observer, Layout::Box const* root_layout_box, CSSPixelRect const& root_bounds, MonotonicTime visual_animation_sample_time)
{
auto& document = target->document();
- auto const& scroll_margin = observer.scroll_margin_values();
- auto inflate_scroll_container_clip_rect_by_scroll_margin = [](void* context, CSSPixelRect clip_rect) -> CSSPixelRect {
- auto const& scroll_margin = *static_cast<Vector<CSS::LengthPercentage> const*>(context);
- if (scroll_margin.is_empty())
- return clip_rect;
- clip_rect.inflate(
- scroll_margin[0].to_px(clip_rect.height()),
- scroll_margin[1].to_px(clip_rect.width()),
- scroll_margin[2].to_px(clip_rect.height()),
- scroll_margin[3].to_px(clip_rect.width()));
- return clip_rect;
- };
CSSPixelRect rect;
auto intersects = Layout::RustFFI::layout_script_intersection_observer_intersection_rect(
document.layout_node_arena().host(), Layout::Node::slot_id(target->layout_node(read)), target_rect,
Layout::Node::slot_id(root_layout_box), root_bounds,
Painting::viewport_mapping_with_visual_animations_sampled_at(document, visual_animation_sample_time),
- const_cast<Vector<CSS::LengthPercentage>*>(&scroll_margin), inflate_scroll_container_clip_rect_by_scroll_margin, &rect);
+ &observer, apply_observer_scroll_margin, &rect);
if (!intersects)
return {};
return rect;
@@ -7880,10 +7881,7 @@ void Document::start_intersection_observing_a_lazy_loading_element(Element& elem
// 2. If doc's lazy load intersection observer is null, set it to a new IntersectionObserver instance, initialized as follows:
if (!m_lazy_load_intersection_observer) {
auto options = IntersectionObserver::IntersectionObserverOptions {};
- // The HTML lazy-loading algorithm uses a generous implicit root margin so that resources are fetched
- // before they enter the viewport. In particular, this also handles elements whose box is initially empty
- // while their intrinsic dimensions are waiting on the fetch itself.
- options.root_margin = "1250px"_utf16;
+ options.scroll_margin = "1250px 625px"_utf16;
// The callback is these steps, with arguments entries and observer:
auto wrapped_callback = IntersectionObserver::create_lazy_load_intersection_observer_callback(*this);
@@ -8706,14 +8704,14 @@ struct CompositorAnimationUpdatePass {
return kind == Animations::RustFFI::FfiCompositorFrameKind::EffectsLayer ? layout_nodes_with_stale_forced_effects_layer : layout_nodes_with_stale_forced_background_color_frame;
}
- bool transform_subtree_is_clipped_outside(Element const& animated_target, CSSPixelRect const& root_bounds) const
+ bool transform_subtree_is_clipped_outside(Element const& animated_target, CSSPixelRect const& root_bounds, IntersectionObserver::IntersectionObserver const* observer = nullptr, Layout::Node const* intersection_root = nullptr) const
{
auto const* layout_node = animated_target.unsafe_layout_node(read);
if (!layout_node)
return false;
return Layout::RustFFI::layout_script_transform_subtree_is_clipped_outside(
- layout_node->document_host(), Layout::Node::slot_id(layout_node), root_bounds,
- viewport_mapping);
+ layout_node->document_host(), Layout::Node::slot_id(layout_node), Layout::Node::slot_id(intersection_root), root_bounds,
+ viewport_mapping, observer, observer ? apply_observer_scroll_margin : nullptr);
}
static Compositing::RustFFI::NodeSlotId layout_node(void* context, Animations::RustFFI::FfiCompositorTarget target)
@@ -9080,7 +9078,10 @@ bool CompositorAnimationUpdatePass::transform_affects_intersection_observation(v
auto root = observer.intersection_root_node();
if (root->is_element() && animated_target.is_shadow_including_inclusive_ancestor_of(*root))
return true;
- bool subtree_is_clipped_outside_root = pass.transform_subtree_is_clipped_outside(animated_target, observer.root_intersection_rectangle(pass.read));
+ auto const* root_layout_node = !observer.is_implicit_root() && &root->document() == &animated_target.document()
+ ? root->unsafe_layout_node(pass.read)
+ : nullptr;
+ bool subtree_is_clipped_outside_root = pass.transform_subtree_is_clipped_outside(animated_target, observer.root_intersection_rectangle(pass.read), &observer, root_layout_node);
for (auto const& observation : observer.observation_targets()) {
auto target = observation.target.ptr();
if (!target)
diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
index 65253a5bafe..f2263afb951 100644
--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
@@ -70,20 +70,33 @@ pub extern "C" fn layout_script_bounding_client_rect(
});
}
+/// # Safety
+///
+/// A supplied callback must be sound to call with `context`.
#[unsafe(no_mangle)]
-pub extern "C" fn layout_script_transform_subtree_is_clipped_outside(
+pub unsafe extern "C" fn layout_script_transform_subtree_is_clipped_outside(
host: &DocumentHost,
target: NodeSlotId,
+ intersection_root: NodeSlotId,
root_bounds: FfiCssPixelRect,
viewport_mapping: FfiViewportMapping<'_>,
+ context: *const c_void,
+ inflate_scroll_clip: Option<unsafe extern "C" fn(*const c_void, FfiCssPixelRect) -> FfiCssPixelRect>,
) -> bool {
let viewport_mapping = viewport_mapping.resolve();
read_rows(host, |arena| {
crate::painting::intersection_observer::transform_subtree_is_clipped_outside(
arena,
target,
+ intersection_root,
root_bounds.into(),
&viewport_mapping.mapping(),
+ inflate_scroll_clip.map(|inflate| {
+ move |rect: CssPixelRect| {
+ // SAFETY: The callback copies the plain-data rect synchronously.
+ unsafe { inflate(context, rect.into()) }.into()
+ }
+ }),
)
})
}
@@ -99,9 +112,9 @@ pub unsafe extern "C" fn layout_script_intersection_observer_intersection_rect(
intersection_root: NodeSlotId,
root_bounds: FfiCssPixelRect,
viewport_mapping: FfiViewportMapping<'_>,
- context: *mut c_void,
+ context: *const c_void,
inflate_scroll_container_clip_rect_by_scroll_margin: unsafe extern "C" fn(
- *mut c_void,
+ *const c_void,
FfiCssPixelRect,
) -> FfiCssPixelRect,
out: &mut FfiCssPixelRect,
diff --git a/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs b/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
index 55f7ed62cac..d1ed2242b7d 100644
--- a/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
+++ b/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
@@ -12,6 +12,21 @@ use crate::painting::paint_read::{PaintRead, PaintRow};
use crate::painting::style_queries;
use crate::painting::viewport_mapping::{ViewportMapping, ViewportSpace};
+fn content_clip_rect(
+ arena: &impl PaintRead,
+ container: NodeSlotId,
+ viewport_mapping: &ViewportMapping<'_>,
+ inflate_scroll_clip: Option<&mut impl FnMut(CssPixelRect) -> CssPixelRect>,
+) -> Option<CssPixelRect> {
+ let clip = viewport_mapping.content_clip_rect(arena, container, ViewportSpace::LayoutViewport)?;
+ if let Some(inflate) = inflate_scroll_clip
+ && style_queries::is_scroll_container(arena, container)
+ {
+ return Some(inflate(clip));
+ }
+ Some(clip)
+}
+
// https://www.w3.org/TR/intersection-observer/#compute-the-intersection
// Answers none where a clip or the root intersection rectangle leaves nothing of target, not even an edge, so that the
// caller can tell a target clipped away from one whose intersection has zero area.
@@ -22,7 +37,7 @@ pub(crate) fn intersection_rect(
intersection_root: NodeSlotId,
root_bounds: CssPixelRect,
viewport_mapping: &ViewportMapping<'_>,
- mut inflate_scroll_container_clip_rect_by_scroll_margin: impl FnMut(CssPixelRect) -> CssPixelRect,
+ mut inflate_scroll_clip: impl FnMut(CssPixelRect) -> CssPixelRect,
) -> Option<CssPixelRect> {
// 1. Let intersectionRect be the result of getting the bounding box for target.
// 2. Let container be the containing block of target.
@@ -41,12 +56,9 @@ pub(crate) fn intersection_rect(
while let Some(container_slot) =
container.filter(|slot| *slot != intersection_root && arena.paintable_row_is_populated(*slot))
{
- if let Some(mut clip) =
- viewport_mapping.content_clip_rect(arena, container_slot, ViewportSpace::LayoutViewport)
+ if let Some(clip) =
+ content_clip_rect(arena, container_slot, viewport_mapping, Some(&mut inflate_scroll_clip))
{
- if style_queries::is_scroll_container(arena, container_slot) {
- clip = inflate_scroll_container_clip_rect_by_scroll_margin(clip);
- }
if !intersection_rect.edge_adjacent_intersects(clip) {
return None;
}
@@ -70,8 +82,10 @@ pub(crate) fn intersection_rect(
pub(crate) fn transform_subtree_is_clipped_outside(
arena: &impl PaintRead,
target: NodeSlotId,
+ intersection_root: NodeSlotId,
root_bounds: CssPixelRect,
viewport_mapping: &ViewportMapping<'_>,
+ mut inflate_scroll_clip: Option<impl FnMut(CssPixelRect) -> CssPixelRect>,
) -> bool {
if !arena.paintable_row_is_populated(target) || root_bounds.is_empty() {
return false;
@@ -86,7 +100,9 @@ pub(crate) fn transform_subtree_is_clipped_outside(
let mut has_disjoint_clip = false;
let mut ancestor = arena.node_parent_if_live(target).unwrap_or(NodeSlotId::INVALID);
- while let Some(ancestor_row) = arena.node(ancestor) {
+ while ancestor != intersection_root
+ && let Some(ancestor_row) = arena.node(ancestor)
+ {
let parent = ancestor_row.parent();
if node_facts::kind_is_box(ancestor_row.kind()) {
if !arena.paintable_row_is_populated(ancestor) {
@@ -100,7 +116,7 @@ pub(crate) fn transform_subtree_is_clipped_outside(
return false;
}
- if let Some(clip_rect) = viewport_mapping.content_clip_rect(arena, ancestor, ViewportSpace::LayoutViewport)
+ if let Some(clip_rect) = content_clip_rect(arena, ancestor, viewport_mapping, inflate_scroll_clip.as_mut())
&& !clip_rect.edge_adjacent_intersects(root_bounds)
{
has_disjoint_clip = true;
diff --git a/Tests/LibWeb/Text/expected/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.txt b/Tests/LibWeb/Text/expected/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.txt
index 0ea40607c6e..57e716cadcf 100644
--- a/Tests/LibWeb/Text/expected/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.txt
+++ b/Tests/LibWeb/Text/expected/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.txt
@@ -1,2 +1,3 @@
spans reported as intersecting: 0, 1, 2, 3
-lazy images loaded: 0, 1, 2, 3
+tenth image loaded: true
+eleventh image loaded: false
diff --git a/Tests/LibWeb/Text/expected/css/compositor-animation-observer-scroll-margin.txt b/Tests/LibWeb/Text/expected/css/compositor-animation-observer-scroll-margin.txt
new file mode 100644
index 00000000000..7fe537b8025
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/compositor-animation-observer-scroll-margin.txt
@@ -0,0 +1,9 @@
+overflow auto, scroll margin 100px: sampling true
+overflow auto, scroll margin 5px: sampling false
+overflow clip, scroll margin 100px: sampling false
+overflow hidden, scroll margin 0px 100px 0px 20px: sampling true
+overflow hidden, scroll margin 0px 100px 0px 19px: sampling false
+explicit root inner, negative bottom margin: sampling true
+explicit root outer, negative bottom margin: sampling false
+image starts unloaded: true
+image loaded during the transform
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-far.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-far.txt
new file mode 100644
index 00000000000..9d69cca8e8b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-far.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test that lazy-loaded images do not load when far from viewport.
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-horizontal.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-horizontal.txt
new file mode 100644
index 00000000000..2a1e3d49ce4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-horizontal.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test that lazy-loaded images load when near viewport.
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-nested.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-nested.txt
new file mode 100644
index 00000000000..2a1e3d49ce4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-nested.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test that lazy-loaded images load when near viewport.
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller.txt
new file mode 100644
index 00000000000..2a1e3d49ce4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test that lazy-loaded images load when near viewport.
diff --git a/Tests/LibWeb/Text/input/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.html b/Tests/LibWeb/Text/input/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.html
index 92343a5c9c6..99efcb27073 100644
--- a/Tests/LibWeb/Text/input/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.html
+++ b/Tests/LibWeb/Text/input/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.html
@@ -24,7 +24,7 @@
for (let i = 0; i < 10; i++)
spans.append(document.createElement("span"));
const images = document.getElementById("images");
- for (let i = 0; i < 30; i++) {
+ for (let i = 0; i < 40; i++) {
const image = document.createElement("img");
image.loading = "lazy";
image.src = `../../../Assets/120.png?${i}`;
@@ -41,11 +41,12 @@
});
println(`spans reported as intersecting: ${intersecting.join(", ")}`);
- const visibleImages = [...images.children].slice(0, 4);
- await Promise.all(visibleImages.map(image => image.complete ? null : new Promise(resolve => image.addEventListener("load", resolve))));
+ const nearbyImage = images.children[9];
+ if (!nearbyImage.complete)
+ await new Promise(resolve => nearbyImage.addEventListener("load", resolve, { once: true }));
await animationFrame();
await animationFrame();
- const loaded = [...images.children].flatMap((image, index) => image.naturalWidth ? [index] : []);
- println(`lazy images loaded: ${loaded.join(", ")}`);
+ println(`tenth image loaded: ${nearbyImage.naturalWidth > 0}`);
+ println(`eleventh image loaded: ${images.children[10].naturalWidth > 0}`);
});
</script>
diff --git a/Tests/LibWeb/Text/input/css/compositor-animation-observer-scroll-margin.html b/Tests/LibWeb/Text/input/css/compositor-animation-observer-scroll-margin.html
new file mode 100644
index 00000000000..7e45268e06c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/compositor-animation-observer-scroll-margin.html
@@ -0,0 +1,67 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ #scroller { position: absolute; left: calc(100vw + 120px); width: 100px; height: 100px; }
+ #target { width: 20px; height: 20px; }
+ #outer { position: absolute; top: 0; width: 220px; height: 100px; overflow: hidden; }
+ #inner { position: absolute; top: 90px; width: 100px; height: 10px; }
+ #moving { margin-left: 150px; width: 5px; height: 5px; }
+ @keyframes move { to { transform: translateX(-100px); } }
+</style>
+<div id="scroller"><div id="target"></div></div>
+<div id="outer"><div id="inner"><div id="moving"></div></div></div>
+<script>
+ promiseTest(async () => {
+ const scroller = document.getElementById("scroller");
+ const target = document.getElementById("target");
+ for (const [overflow, margin] of [
+ ["auto", "100px"],
+ ["auto", "5px"],
+ ["clip", "100px"],
+ ["hidden", "0px 100px 0px 20px"],
+ ["hidden", "0px 100px 0px 19px"],
+ ]) {
+ scroller.style.overflow = overflow;
+ target.style.animation = "move 100s linear";
+ const observer = new IntersectionObserver(() => {}, { scrollMargin: margin });
+ observer.observe(target);
+ await new Promise(requestAnimationFrame);
+ await new Promise(requestAnimationFrame);
+ const sampling = internals.dumpCompositorAnimations().includes("observation timer armed");
+ println(`overflow ${overflow}, scroll margin ${margin}: sampling ${sampling}`);
+ observer.disconnect();
+ target.style.animation = "";
+ }
+
+ const moving = document.getElementById("moving");
+ for (const id of ["inner", "outer"]) {
+ const root = document.getElementById(id);
+ const observer = new IntersectionObserver(() => {}, { root, scrollMargin: "0px 0px -20px 0px" });
+ observer.observe(moving);
+ moving.style.animation = "move 100s linear";
+ await new Promise(requestAnimationFrame);
+ await new Promise(requestAnimationFrame);
+ const sampling = internals.dumpCompositorAnimations().includes("observation timer armed");
+ println(`explicit root ${id}, negative bottom margin: sampling ${sampling}`);
+ observer.disconnect();
+ moving.style.animation = "";
+ }
+
+ const image = new Image(20, 20);
+ image.loading = "lazy";
+ const loaded = new Promise(resolve => image.onload = resolve);
+ scroller.style.cssText = "overflow: auto; left: calc(100vw + 650px)";
+ scroller.replaceChildren(image);
+ image.src = "../../../Assets/120.png";
+ await new Promise(requestAnimationFrame);
+ await new Promise(requestAnimationFrame);
+ println(`image starts unloaded: ${!image.complete}`);
+ image.animate([
+ { transform: "translateX(0)" },
+ { transform: "translateX(-100px)", offset: 0.01 },
+ { transform: "translateX(-100px)" },
+ ], 60000);
+ await loaded;
+ println("image loaded during the transform");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-far.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-far.html
new file mode 100644
index 00000000000..a44f878db14
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-far.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<link rel="help" href="https://html.spec.whatwg.org/#lazy-load-root-margin">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+
+<style>
+ #scroller {
+ width: 100px;
+ height: 100px;
+ overflow: scroll;
+ background-color: gray;
+ }
+
+ #spacer {
+ width: 130px;
+ height: 10000vh;
+ }
+
+ #target {
+ width: 100px;
+ height: 100px;
+ }
+</style>
+
+<div id="scroller">
+ <div id="spacer"></div>
+ <img
+ id="target"
+ src="resources/green.png"
+ loading="lazy"
+ onload="img_onload();"
+ >
+</div>
+
+<script>
+ const t = async_test(
+ "Test that lazy-loaded images do not load when far from viewport."
+ );
+
+ function img_onload() {
+ t.unreached_func(
+ "Lazy-loading image far from viewport should not load."
+ )();
+ }
+
+ t.step_timeout(() => {
+ t.done();
+ }, 2000);
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-horizontal.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-horizontal.html
new file mode 100644
index 00000000000..c86cd0deac2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-horizontal.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<link rel="help" href="https://html.spec.whatwg.org/#lazy-load-root-margin">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+
+<style>
+ #scroller {
+ width: 100px;
+ height: 100px;
+ overflow: scroll;
+ background-color: gray;
+ display: flex;
+ }
+
+ #spacer {
+ width: 130px;
+ height: 130px;
+ flex-shrink: 0;
+ }
+
+ #target {
+ width: 100px;
+ height: 100px;
+ flex-shrink: 0;
+ }
+</style>
+
+<div id="scroller">
+ <div id="spacer"></div>
+ <img
+ id="target"
+ src="resources/green.png"
+ loading="lazy"
+ onload="img_onload();"
+ >
+</div>
+
+<script>
+ const t = async_test(
+ "Test that lazy-loaded images load when near viewport."
+ );
+
+ function img_onload() {
+ t.done();
+ }
+
+ t.step_timeout(() => {
+ t.unreached_func(
+ "Timed out while waiting for image to load."
+ )();
+ }, 2000);
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-nested.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-nested.html
new file mode 100644
index 00000000000..f283e31e027
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller-nested.html
@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<link rel="help" href="https://html.spec.whatwg.org/#lazy-load-root-margin">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+
+<style>
+ #scroller {
+ width: 100px;
+ height: 100px;
+ overflow: scroll;
+ background-color: gray;
+ }
+
+ #scroller2 {
+ width: 110px;
+ height: 110px;
+ overflow: scroll;
+ }
+
+ #spacer {
+ width: 130px;
+ height: 130px;
+ }
+
+ #target {
+ width: 100px;
+ height: 100px;
+ }
+</style>
+
+<div id=scroller2>
+ <div id="scroller">
+ <div id="spacer"></div>
+ <img
+ id="target"
+ src="resources/green.png"
+ loading="lazy"
+ onload="img_onload();"
+ >
+ </div>
+</div>
+
+<script>
+ const t = async_test(
+ "Test that lazy-loaded images load when near viewport."
+ );
+
+ function img_onload() {
+ t.done();
+ }
+
+ t.step_timeout(() => {
+ t.unreached_func(
+ "Timed out while waiting for image to load."
+ )();
+ }, 2000);
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller.html
new file mode 100644
index 00000000000..9cf7ce1aa8e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/image-loading-lazy-in-scroller.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<link rel="help" href="https://html.spec.whatwg.org/#lazy-load-root-margin">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+
+<style>
+ #scroller {
+ width: 100px;
+ height: 100px;
+ overflow: scroll;
+ background-color: gray;
+ }
+
+ #spacer {
+ width: 130px;
+ height: 130px;
+ }
+
+ #target {
+ width: 100px;
+ height: 100px;
+ }
+</style>
+
+<div id="scroller">
+ <div id="spacer"></div>
+ <img
+ id="target"
+ src="resources/green.png"
+ loading="lazy"
+ onload="img_onload();"
+ >
+</div>
+
+<script>
+ const t = async_test(
+ "Test that lazy-loaded images load when near viewport."
+ );
+
+ function img_onload() {
+ t.done();
+ }
+
+ t.step_timeout(() => {
+ t.unreached_func(
+ "Timed out while waiting for image to load."
+ )();
+ }, 2000);
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/resources/green.png b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/resources/green.png
new file mode 100644
index 00000000000..25b76c3c6f2
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/resources/green.png differ