Lightpanda: the headless browser designed for AI and automation
webapi: element(s)FromPoint returns null for points outside viewport
Karl Seguince5065c52c4d
diff --git a/src/browser/tests/document/element_from_point.html b/src/browser/tests/document/element_from_point.htmlindex 00a9468d1..31fc07cb6 100644--- a/src/browser/tests/document/element_from_point.html+++ b/src/browser/tests/document/element_from_point.html@@ -75,6 +75,44 @@}</script>+<script id="viewport_bound">+{+ // Content extends past the viewport, but points outside it hit nothing.+ const tall = document.createElement('div');+ tall.style.height = (innerHeight * 3) + 'px';+ tall.style.width = (innerWidth * 3) + 'px';+ document.body.appendChild(tall);+ // Chrome's viewport excludes its scrollbars; Lightpanda draws none.+ document.documentElement.style.scrollbarWidth = 'none';++ const W = innerWidth;+ const H = innerHeight;+ testing.expectTrue(document.elementFromPoint(10, 10) !== null);+ testing.expectTrue(document.elementFromPoint(W - 1, 10) !== null);+ testing.expectTrue(document.elementFromPoint(10, H - 1) !== null);++ testing.expectEqual(null, document.elementFromPoint(10, H + 10));+ testing.expectEqual(null, document.elementFromPoint(W + 10, 10));+ testing.expectEqual(null, document.elementFromPoint(W, 10));+ testing.expectEqual(null, document.elementFromPoint(10, H));+ testing.expectEqual(0, document.elementsFromPoint(10, H + 10).length);++ // Chrome rounds to the nearest pixel before the bound check.+ testing.expectTrue(document.elementFromPoint(-0.4, 10) !== null);+ testing.expectEqual(null, document.elementFromPoint(-0.6, 10));+ testing.expectEqual(null, document.elementFromPoint(W - 0.4, 10));++ // Scrolling moves content into view, not the bound.+ scrollTo(0, H);+ testing.expectTrue(document.elementFromPoint(10, 10) !== null);+ testing.expectEqual(null, document.elementFromPoint(10, H + 10));+ scrollTo(0, 0);++ document.documentElement.style.scrollbarWidth = '';+ tall.remove();+}+</script>+<script id="at_element_center">{// Test querying at the center of an elementdiff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex 7026c16b8..fd38baa3f 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -951,10 +951,27 @@ fn elementFromPoint(self: *Document, x: f64, y: f64, frame: *Frame) !?*Element {/// DocumentOrShadowRoot.elementFromPoint for `scope`, this document or one of/// its shadow roots.pub fn elementFromPointIn(self: *Document, scope: *Node, x: f64, y: f64, frame: *Frame) !?*Element {- const hit = (try self.hitTest(x, y, frame)) orelse return null;+ const point = viewportPoint(x, y, frame) orelse return null;+ const hit = (try self.hitTest(point.x, point.y, frame)) orelse return null;return retarget(hit, scope);}+const ViewportPoint = struct { x: f64, y: f64 };++// elementFromPoint/elementsFromPoint find nothing outside the viewport+fn viewportPoint(x: f64, y: f64, frame: *Frame) ?ViewportPoint {+ const viewport = frame.page.getViewport();+ const rx = @round(x);+ const ry = @round(y);+ const inside = rx >= 0 and ry >= 0 and+ rx < @as(f64, @floatFromInt(viewport.width)) and+ ry < @as(f64, @floatFromInt(viewport.height));+ if (inside == false) {+ return null;+ }+ return .{ .x = rx, .y = ry };+}+// The spec's retarget: climbs out of shadow trees until `element` is in a tree// that `scope` is in or inside of. A hit in a shadow tree nested in `scope`'s// reports its host in `scope`'s tree; a hit outside it is returned as is.@@ -1057,7 +1074,8 @@ fn elementsFromPoint(self: *Document, x: f64, y: f64, frame: *Frame) ![]const *E/// hit and its flat-tree ancestors that can be hit, each retargeted against/// `scope` with repeats dropped, then the root element.pub fn elementsFromPointIn(self: *Document, scope: *Node, x: f64, y: f64, frame: *Frame) ![]const *Element {- const hit = (try self.hitTest(x, y, frame)) orelse return &.{};+ const point = viewportPoint(x, y, frame) orelse return &.{};+ const hit = (try self.hitTest(point.x, point.y, frame)) orelse return &.{};const style_manager = &(self.asNode().ownerFrame(frame) orelse return &.{})._style_manager;var result: std.ArrayList(*Element) = .empty;var current: ?*Element = hit;