Skip to content

Truly independent web browser

LibWeb: Don’t report a target clipped away as intersecting

sideshowbarkeradc326e6921c
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 61327e2d3d1..f7a6594b805 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -8032,7 +8032,7 @@ void Document::queue_an_intersection_observer_entry(IntersectionObserver::Inters
}
// https://www.w3.org/TR/intersection-observer/#compute-the-intersection
-static 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, Compositing::AccumulatedVisualContextTree const& visual_context_tree)
+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, Compositing::AccumulatedVisualContextTree const& visual_context_tree)
{
auto& document = target->document();
auto const& scroll_margin = observer.scroll_margin_values();
@@ -8048,11 +8048,13 @@ static CSSPixelRect compute_intersection(Layout::BegunRead const& read, GC::Ref<
return clip_rect;
};
CSSPixelRect rect;
- Layout::RustFFI::layout_script_intersection_observer_intersection_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(document, visual_context_tree),
const_cast<Vector<CSS::LengthPercentage>*>(&scroll_margin), inflate_scroll_container_clip_rect_by_scroll_margin, &rect);
+ if (!intersects)
+ return {};
return rect;
}
@@ -8170,7 +8172,9 @@ void Document::run_the_update_intersection_observations_steps(HighResolutionTime
// 5. Let intersectionRect be the result of running the compute the intersection algorithm on target and
// observer’s intersection root.
- intersection_rect = compute_intersection(target_read, target, target_rect, *observer, root_layout_box, root_bounds, *target_visual_context_tree);
+ auto maybe_intersection_rect = compute_intersection(target_read, target, target_rect, *observer, root_layout_box, root_bounds, *target_visual_context_tree);
+ if (maybe_intersection_rect.has_value())
+ intersection_rect = *maybe_intersection_rect;
// 6. Let targetArea be targetRect’s area.
auto target_area = target_rect.width() * target_rect.height();
@@ -8180,7 +8184,9 @@ void Document::run_the_update_intersection_observations_steps(HighResolutionTime
// 8. Let isIntersecting be true if targetRect and rootBounds intersect or are edge-adjacent, even if the
// intersection has zero area (because rootBounds or targetRect have zero area).
- intersection_geometry_intersects = target_rect.edge_adjacent_intersects(root_bounds);
+ // AD-HOC: By that step, a target that its ancestors clip away entirely is intersecting while it lies
+ // within rootBounds. Check what is left of it after the clips instead, edge-inclusively.
+ intersection_geometry_intersects = maybe_intersection_rect.has_value();
// 9. If targetArea is non-zero, let intersectionRatio be intersectionArea divided by targetArea.
// Otherwise, let intersectionRatio be 1 if isIntersecting is true, or 0 if isIntersecting is false.
diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
index f3453492164..7282e81cb38 100644
--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
@@ -105,9 +105,9 @@ pub unsafe extern "C" fn layout_script_intersection_observer_intersection_rect(
FfiCssPixelRect,
) -> FfiCssPixelRect,
out: &mut FfiCssPixelRect,
-) {
+) -> bool {
let viewport_mapping = viewport_mapping.resolve();
- *out = read_rows(host, |arena| {
+ let intersection_rect = read_rows(host, |arena| {
crate::painting::intersection_observer::intersection_rect(
arena,
target,
@@ -120,8 +120,9 @@ pub unsafe extern "C" fn layout_script_intersection_observer_intersection_rect(
unsafe { inflate_scroll_container_clip_rect_by_scroll_margin(context, clip_rect.into()) }.into()
},
)
- .into()
});
+ *out = intersection_rect.unwrap_or_default().into();
+ intersection_rect.is_some()
}
#[unsafe(no_mangle)]
diff --git a/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs b/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
index 083dd31559b..55f7ed62cac 100644
--- a/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
+++ b/Libraries/LibWeb/Rust/src/painting/intersection_observer.rs
@@ -13,6 +13,8 @@ use crate::painting::style_queries;
use crate::painting::viewport_mapping::{ViewportMapping, ViewportSpace};
// 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.
pub(crate) fn intersection_rect(
arena: &impl PaintRead,
target: NodeSlotId,
@@ -20,8 +22,8 @@ pub(crate) fn intersection_rect(
intersection_root: NodeSlotId,
root_bounds: CssPixelRect,
viewport_mapping: &ViewportMapping<'_>,
- inflate_scroll_container_clip_rect_by_scroll_margin: impl FnMut(CssPixelRect) -> CssPixelRect,
-) -> CssPixelRect {
+ mut inflate_scroll_container_clip_rect_by_scroll_margin: 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.
// 3. While container is not root:
@@ -33,21 +35,36 @@ pub(crate) fn intersection_rect(
// 3.3. If container is a scroll container, apply the observer’s [[scrollMargin]] to the container’s clip rect.
// 3.4. If container has a content clip or a css clip-path property, update intersectionRect by applying
// container’s clip.
- let intersection_rect = viewport_mapping.visible_rect_in_ancestor(
- arena,
- target,
- target_rect,
- intersection_root,
- ViewportSpace::LayoutViewport,
- inflate_scroll_container_clip_rect_by_scroll_margin,
- );
+ let mut intersection_rect = target_rect;
+ if arena.paintable_row_is_populated(target) {
+ let mut container = arena.node_containing_block_if_live(target);
+ 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 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;
+ }
+ intersection_rect = intersection_rect.intersected(clip);
+ }
+ container = arena.node_containing_block_if_live(container_slot);
+ }
+ }
// FIXME: 4. Map intersectionRect to the coordinate space of root.
// 5. Update intersectionRect by intersecting it with the root intersection rectangle.
// FIXME: 6. Map intersectionRect to the coordinate space of the viewport of the document containing target.
// 7. Return intersectionRect.
- intersection_rect.intersected(root_bounds)
+ if !intersection_rect.edge_adjacent_intersects(root_bounds) {
+ return None;
+ }
+ Some(intersection_rect.intersected(root_bounds))
}
pub(crate) fn transform_subtree_is_clipped_outside(
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
new file mode 100644
index 00000000000..0ea40607c6e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.txt
@@ -0,0 +1,2 @@
+spans reported as intersecting: 0, 1, 2, 3
+lazy images loaded: 0, 1, 2, 3
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/padding-clip.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/padding-clip.txt
new file mode 100644
index 00000000000..4efd5bbb394
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/padding-clip.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Scrollport is used rather than content rect to compute intersection ratio
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-4-val.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-4-val.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-4-val.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-clip-path.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-clip-path.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-clip-path.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-horizontal.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-horizontal.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-horizontal.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested-2.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested-2.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested-2.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested-3.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested-3.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested-3.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-nested.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-no-intersect.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-no-intersect.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-no-intersect.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-non-scrolling-root.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-non-scrolling-root.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-non-scrolling-root.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-not-contained.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-not-contained.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-not-contained.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-percent.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-percent.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-percent.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-with-border-outline.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-with-border-outline.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-with-border-outline.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-zero.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-zero.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin-zero.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin.txt
new file mode 100644
index 00000000000..4f232ba9382
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/scroll-margin.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test scroll margin intersection
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/svg-clipped-rect-target.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/svg-clipped-rect-target.txt
new file mode 100644
index 00000000000..e057e8eeb24
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/svg-clipped-rect-target.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 4 tests
+
+3 Pass
+1 Fail
+Pass IntersectionObserver observing a clipped SVG <rect> element
+Pass First rAF.
+Fail document.scrollingElement.scrollTop = 300
+Pass document.scrollingElement.scrollTop = 100
\ No newline at end of file
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
new file mode 100644
index 00000000000..92343a5c9c6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/IntersectionObserver/target-clipped-by-overflow-hidden-ancestor.html
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ .clip {
+ width: 300px;
+ overflow: hidden;
+ white-space: nowrap;
+ }
+ .clip > * {
+ display: inline-block;
+ width: 100px;
+ height: 100px;
+ vertical-align: top;
+ }
+</style>
+<div class="clip" id="spans"></div>
+<div class="clip" id="images"></div>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const spans = document.getElementById("spans");
+ for (let i = 0; i < 10; i++)
+ spans.append(document.createElement("span"));
+ const images = document.getElementById("images");
+ for (let i = 0; i < 30; i++) {
+ const image = document.createElement("img");
+ image.loading = "lazy";
+ image.src = `../../../Assets/120.png?${i}`;
+ images.append(image);
+ }
+
+ const intersecting = await new Promise(resolve => {
+ const observer = new IntersectionObserver(entries => {
+ observer.disconnect();
+ resolve(entries.filter(entry => entry.isIntersecting).map(entry => [...spans.children].indexOf(entry.target)));
+ });
+ for (const span of spans.children)
+ observer.observe(span);
+ });
+ 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))));
+ await animationFrame();
+ await animationFrame();
+ const loaded = [...images.children].flatMap((image, index) => image.naturalWidth ? [index] : []);
+ println(`lazy images loaded: ${loaded.join(", ")}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/padding-clip.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/padding-clip.html
new file mode 100644
index 00000000000..bd52c9097a5
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/padding-clip.html
@@ -0,0 +1,45 @@
+<!doctype html>
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<title>Scrollport is used rather than content rect to compute intersection ratio</title>
+<link rel="author" href="mailto:[email protected]" title="Emilio Cobos Álvarez">
+<link rel="author" href="https://mozilla.org" title="Mozilla">
+<link rel="help" href="https://github.com/w3c/IntersectionObserver/issues/504">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<style>
+#root {
+ overflow: hidden;
+ position: relative;
+ box-sizing: border-box;
+ width: 100px;
+ height: 100px;
+ padding: 10px;
+}
+#target {
+ width: 100px;
+ height: 100px;
+ background-color: green;
+ /* Shift into the padding area */
+ position: relative;
+ left: -10px;
+ top: -10px;
+}
+</style>
+<div id="root">
+ <div id="target"></div>
+</div>
+<script>
+promise_test(async function(t) {
+ const target = document.getElementById("target");
+ assert_true(!!target, "target exists");
+ const root = document.getElementById("root");
+ assert_true(!!root, "root exists");
+ const entries = await new Promise(resolve => {
+ const observer = new IntersectionObserver(resolve, { root });
+ observer.observe(target);
+ });
+ assert_equals(entries.length, 1, "Should have one entry.");
+ assert_equals(entries[0].intersectionRatio, 1, "Should be completely visible.");
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-4-val.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-4-val.html
new file mode 100644
index 00000000000..4b6e14e9fd0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-4-val.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#spacer { width: 50px; height: 100px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "1px 2px 10px 3px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.2, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-clip-path.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-clip-path.html
new file mode 100644
index 00000000000..4087fc61fb6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-clip-path.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller {
+ width: 100px;
+ height: 100px;
+ overflow: hidden;
+ background-color: gray;
+ clip-path: inset(10%);
+}
+#spacer { width: 50px; height: 100px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "20px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.2, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-horizontal.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-horizontal.html
new file mode 100644
index 00000000000..0e022490180
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-horizontal.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; display: flex; }
+#spacer { width: 100px; height: 50px; flex-shrink: 0; }
+#target { width: 50px; height: 50px; background-color: green; flex-shrink: 0; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.2, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested-2.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested-2.html
new file mode 100644
index 00000000000..8d40a2988c2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested-2.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#scroller2 { width: 130px; height: 130px; overflow: hidden; }
+#spacer { width: 50px; height: 120px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller2>
+ <div id=spacer></div>
+ <div id=scroller>
+ <div id=target></div>
+ </div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.4, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested-3.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested-3.html
new file mode 100644
index 00000000000..a608843931d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested-3.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#scroller2 { width: 130px; height: 130px; overflow: hidden; }
+#scroller3 { width: 160px; height: 160px; overflow: hidden; }
+#spacer { width: 50px; height: 100px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller3>
+ <div id=scroller2>
+ <div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+ </div>
+ </div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.2, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested.html
new file mode 100644
index 00000000000..93a62931380
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-nested.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#scroller2 { width: 130px; height: 130px; overflow: hidden; }
+#spacer { width: 50px; height: 100px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller2>
+ <div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+ </div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.2, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-no-intersect.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-no-intersect.html
new file mode 100644
index 00000000000..9af8f9f8e2e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-no-intersect.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#spacer { width: 50px; height: 120px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_false(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.0, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-non-scrolling-root.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-non-scrolling-root.html
new file mode 100644
index 00000000000..972e2eeb6b3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-non-scrolling-root.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#spacer { width: 50px; height: 110px; background-color: darkgray; }
+#root { height: 75; width: 75; background-color: blue; }
+#target { width: 50px; height: 50px; background-color: green; }
+#spacer2 { width: 10px; height: 10px; background-color: lightgray; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=root>
+ <div id=spacer2></div>
+ <div id=target></div>
+ </div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ root: document.querySelector("#root"),
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 1.0, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-not-contained.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-not-contained.html
new file mode 100644
index 00000000000..b9ed7cec73a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-not-contained.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; position: absolute; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=root>
+ <div id=scroller>
+ <div id=target></div>
+ </div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ root: document.querySelector("#root"),
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ // Not insecting because root is not in the containing block chain of target.
+ assert_false(entries[0].isIntersecting, "isIntersecting");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-percent.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-percent.html
new file mode 100644
index 00000000000..680a582be51
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-percent.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#spacer { width: 50px; height: 100px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "10%"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.2, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-with-border-outline.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-with-border-outline.html
new file mode 100644
index 00000000000..d79234f0050
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-with-border-outline.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; border: 10px solid black; outline: 10px solid blue; }
+#spacer { width: 50px; height: 100px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.2, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-zero.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-zero.html
new file mode 100644
index 00000000000..4d59bc3dd69
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin-zero.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#spacer { width: 50px; height: 110px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "0px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_false(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.0, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin.html
new file mode 100644
index 00000000000..6de31bcba6c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/scroll-margin.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<link rel="help" href="https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-scrollmargin">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="./resources/intersection-observer-test-utils.js"></script>
+
+<style>
+#scroller { width: 100px; height: 100px; overflow: hidden; background-color: gray; }
+#spacer { width: 50px; height: 100px; }
+#target { width: 50px; height: 50px; background-color: green; }
+</style>
+
+<div id=scroller>
+ <div id=spacer></div>
+ <div id=target></div>
+</div>
+
+<script>
+let entries = [];
+
+window.onload = function() {
+ runTestCycle(testIntersection, "Test scroll margin intersection");
+
+ const observer = new IntersectionObserver(
+ es => entries = entries.concat(es),
+ {
+ scrollMargin: "10px"
+ }
+ );
+ observer.observe(target);
+};
+
+function testIntersection() {
+ assert_equals(entries.length, 1, "IntersectionObserverEntryCount");
+ assert_true(entries[0].isIntersecting, "isIntersecting");
+ assert_approx_equals(entries[0].intersectionRatio, 0.2, 0.001, "intersectionRatio");
+}
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/intersection-observer/svg-clipped-rect-target.html b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/svg-clipped-rect-target.html
new file mode 100644
index 00000000000..330f327bd62
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/intersection-observer/svg-clipped-rect-target.html
@@ -0,0 +1,71 @@
+<!DOCTYPE html>
+<title>IntersectionObserver observing a clipped SVG &lt;rect> element</title>
+<meta name="viewport" content="width=device-width,initial-scale=1">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="resources/intersection-observer-test-utils.js"></script>
+<style>
+.spacer {
+ height: calc(100vh + 100px);
+}
+</style>
+<div class="spacer"></div>
+<svg viewBox="0 0 200 100" width="400">
+ <clipPath id="clip">
+ <circle cx="75" cy="50" r="20"/>
+ </clipPath>
+ <g clip-path="url(#clip)">
+ <rect id="target" x="50" y="25" width="100" height="50" fill="blue"/>
+ </g>
+</svg>
+<div class="spacer"></div>
+<script>
+const viewportWidth = document.documentElement.clientWidth;
+const viewportHeight = document.documentElement.clientHeight;
+setup(() => {
+ window.entries = [];
+ window.target = document.getElementById("target");
+ window.targetRect = target.getBoundingClientRect();
+});
+runTestCycle(function() {
+ assert_true(!!target, "target exists");
+ const observer = new IntersectionObserver(function(changes) {
+ entries = entries.concat(changes)
+ });
+ observer.observe(target);
+ entries = entries.concat(observer.takeRecords());
+ assert_equals(entries.length, 0, "No initial notifications");
+ runTestCycle(step0, "First rAF.");
+});
+function step0() {
+ document.scrollingElement.scrollTop = 300;
+ runTestCycle(step1, "document.scrollingElement.scrollTop = 300");
+ // The numbers in brackets are target client rect; intersection rect;
+ // and root bounds.
+ checkLastEntry(entries, 0, [
+ 108, 108 + targetRect.width, viewportHeight + 158, viewportHeight + 158 + targetRect.height,
+ 0, 0, 0, 0,
+ 0, viewportWidth, 0, viewportHeight,
+ false,
+ ]);
+}
+function step1() {
+ document.scrollingElement.scrollTop = 100;
+ runTestCycle(step2, "document.scrollingElement.scrollTop = 100");
+ checkLastEntry(entries, 1, [
+ 108, 108 + targetRect.width, viewportHeight - 142, viewportHeight - 142 + targetRect.height,
+ 118, 198, viewportHeight - 132, viewportHeight - 52,
+ 0, viewportWidth, 0, viewportHeight,
+ true,
+ ]);
+}
+function step2() {
+ document.scrollingElement.scrollTop = 0;
+ checkLastEntry(entries, 2, [
+ 108, 108 + targetRect.width, viewportHeight + 58, viewportHeight + 58 + targetRect.height,
+ 0, 0, 0, 0,
+ 0, viewportWidth, 0, viewportHeight,
+ false,
+ ]);
+}
+</script>