From 31a69ce8878daf24031652716090414f91327df1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 16 Feb 2025 12:17:07 +0100 Subject: [PATCH] LibWeb+LibGfx: Make IntersectionObserver checks edge-inclusive This fixes an issue where 0x0 rectangles were not considered to be intersecting, even when they fell inside (or were adjacent to) the viewport. --- Libraries/LibGfx/Rect.h | 6 ++++++ Libraries/LibWeb/DOM/Document.cpp | 2 +- .../edge-inclusive-intersection.txt | 9 ++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Libraries/LibGfx/Rect.h b/Libraries/LibGfx/Rect.h index fcf8c331510..25231df8072 100644 --- a/Libraries/LibGfx/Rect.h +++ b/Libraries/LibGfx/Rect.h @@ -395,6 +395,12 @@ public: && other.top() < bottom(); } + [[nodiscard]] bool edge_adjacent_intersects(Rect const& other) const + { + return max(left(), other.left()) <= min(right(), other.right()) + && max(top(), other.top()) <= min(bottom(), other.bottom()); + } + template [[nodiscard]] bool intersects(Container const& others) const { diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 9cc494e2596..bd7e0951c07 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -4583,7 +4583,7 @@ 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). CSSPixelRect target_rect_as_pixel_rect(target_rect->x(), target_rect->y(), target_rect->width(), target_rect->height()); - is_intersecting = target_rect_as_pixel_rect.intersects(root_bounds); + is_intersecting = target_rect_as_pixel_rect.edge_adjacent_intersects(root_bounds); // 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/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/edge-inclusive-intersection.txt b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/edge-inclusive-intersection.txt index ef612a6d2b7..66648fb31d5 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/edge-inclusive-intersection.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/intersection-observer/edge-inclusive-intersection.txt @@ -2,10 +2,9 @@ Harness status: OK Found 5 tests -2 Pass -3 Fail +5 Pass Pass IntersectionObserver should detect and report edge-adjacent and zero-area intersections. Pass First rAF. -Fail Set transform=translateY(200px) on target. -Fail Set transform=translateY(201px) on target. -Fail Set transform=translateY(185px) on target. \ No newline at end of file +Pass Set transform=translateY(200px) on target. +Pass Set transform=translateY(201px) on target. +Pass Set transform=translateY(185px) on target. \ No newline at end of file