Lightpanda: the headless browser designed for AI and automation
webapi: make client rects viewport-relative, implement CDP scrollIntoViewIfNeeded
Onni Hakalae11374b014e0
diff --git a/src/browser/tests/element/bounding_rect_scroll.html b/src/browser/tests/element/bounding_rect_scroll.htmlnew file mode 100644index 000000000..7c45f80b3--- /dev/null+++ b/src/browser/tests/element/bounding_rect_scroll.html@@ -0,0 +1,36 @@+<!DOCTYPE html>+<script src="../testing.js"></script>+<body>+<div id="top">top</div>+<div id="filler"></div>+<button id="far">far</button>+</body>++<script id="rectIsViewportRelative">+{+ const filler = $('#filler');+ for (let i = 0; i < 300; i++) {+ filler.appendChild(document.createElement('p'));+ }+ const far = $('#far');+ const before = far.getBoundingClientRect();+ testing.expectTrue(before.top > window.innerHeight);+ testing.expectEqual(far, document.elementFromPoint(before.x, before.y));++ window.scrollTo(0, 100);+ const after = far.getBoundingClientRect();+ testing.expectEqual(before.top - 100, after.top);+ testing.expectEqual(before.x, after.x);+ testing.expectEqual(far, document.elementFromPoint(after.x, after.y));++ // scrollIntoView brings it into the viewport+ far.scrollIntoView();+ const inView = far.getBoundingClientRect();+ testing.expectTrue(inView.top >= 0);+ testing.expectTrue(inView.bottom <= window.innerHeight);+ testing.expectEqual(far, document.elementFromPoint(inView.x, inView.y));++ window.scrollTo(0, 0);+ testing.expectEqual(before.top, far.getBoundingClientRect().top);+}+</script>diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex 2004f6240..6a63410d8 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -961,7 +961,7 @@ pub fn elementFromVerticalPoint(self: *Document, y: f64, frame: *Frame) !?*Elemereturn self.elementFromPointImpl(0, y, true, frame);}-fn elementFromPointImpl(self: *Document, x: f64, y: f64, ignore_x: bool, frame: *Frame) !?*Element {+fn elementFromPointImpl(self: *Document, viewport_x: f64, viewport_y: f64, ignore_x: bool, frame: *Frame) !?*Element {// DFS in document order; topmost = last visited element whose rect contains (x, y).//// Faux-layout shortcut: rect.top is calculateDocumentPosition × 5, which is@@ -975,7 +975,13 @@ fn elementFromPointImpl(self: *Document, x: f64, y: f64, ignore_x: bool, frame:var topmost: ?*Element = null;const root = self.asNode();- const style_manager = &(root.ownerFrame(frame) orelse return null)._style_manager;+ const owner = root.ownerFrame(frame) orelse return null;+ const style_manager = &owner._style_manager;++ // (x, y) is viewport-relative; the faux layout is in document coordinates.+ const x = viewport_x + @as(f64, @floatFromInt(owner.window.getScrollX()));+ const y = viewport_y + @as(f64, @floatFromInt(owner.window.getScrollY()));+const Entry = struct { node: *Node, hidden: bool };var stack: std.ArrayList(Entry) = .empty;try stack.append(frame.local_arena, .{ .node = root, .hidden = false });diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex e43faa8f4..921cb5f5b 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -1714,9 +1714,12 @@ pub fn boundingClientRectValues(self: *Element, frame: *Frame) DOMRect.Data {// Some cases need the bounding rect but have already done the visibility check.pub fn boundingClientRectValuesForVisible(self: *Element, frame: *Frame) DOMRect.Data {+ // Client rects are viewport-relative: offset the faux-layout document+ // position by the owning window's scroll position.+ const window = (self.ownerFrame(frame) orelse frame).window;return .{- .x = self.horizontalPosition(frame),- .y = calculateDocumentPosition(self.asNode()),+ .x = self.horizontalPosition(frame) - @as(f64, @floatFromInt(window.getScrollX())),+ .y = calculateDocumentPosition(self.asNode()) - @as(f64, @floatFromInt(window.getScrollY())),.width = self.boxAxis(frame, .width),.height = self.boxAxis(frame, .height),};diff --git a/src/server/cdp/domains/dom.zig b/src/server/cdp/domains/dom.zigindex 7c740aa2d..299f3a3ab 100644--- a/src/server/cdp/domains/dom.zig+++ b/src/server/cdp/domains/dom.zig@@ -483,7 +483,13 @@ fn scrollIntoViewIfNeeded(cmd: *CDP.Command) !void {const node = try getNode(cmd.arena, bc, cmd.input.session_id, params.nodeId, params.backendNodeId, params.objectId);switch (node.dom._type) {- .element => {},+ .element => {+ // Drivers (e.g. Playwright's click) scroll the target into view and+ // then require its content quads to intersect the viewport.+ const frame = bc.mainFrame() orelse return error.FrameNotLoaded;+ const element = node.dom.is(DOMNode.Element).?;+ element.scrollIntoViewIfNeeded(null, element.ownerFrame(frame) orelse frame);+ },.document => {},.cdata => {},else => return error.NodeDoesNotHaveGeometry,@@ -917,6 +923,41 @@ test "cdp.dom: focus makes the node activeElement and routes key events to it" {try testing.expect(result.isTrue());}+test "cdp.dom: scrollIntoViewIfNeeded brings the node's quads into the viewport" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "mcp_actions.html" });+ const frame = bc.mainFrame().?;++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ try ctx.processMessage(.{ .id = 1, .method = "DOM.performSearch", .params = .{ .query = "#btn" } });+ try ctx.expectSentResult(.{ .searchId = "0", .resultCount = 1 }, .{ .id = 1 });+ try ctx.processMessage(.{+ .id = 2,+ .method = "DOM.getSearchResults",+ .params = .{ .searchId = "0", .fromIndex = 0, .toIndex = 1 },+ });+ try ctx.expectSentResult(.{ .nodeIds = &.{1} }, .{ .id = 2 });++ // Push the target below the fold.+ _ = try ls.local.compileAndRun(+ \\const far = document.getElementById('btn');+ \\for (let i = 0; i < 300; i++) document.body.insertBefore(document.createElement('p'), far);+ , null);+ try testing.expect((try ls.local.compileAndRun("far.getBoundingClientRect().top > window.innerHeight", null)).isTrue());++ try ctx.processMessage(.{ .id = 3, .method = "DOM.scrollIntoViewIfNeeded", .params = .{ .nodeId = 1 } });+ try ctx.expectSentResult(null, .{ .id = 3 });++ const rect = "far.getBoundingClientRect()";+ try testing.expect((try ls.local.compileAndRun("window.scrollY > 0 && " ++ rect ++ ".top >= 0 && " ++ rect ++ ".bottom <= window.innerHeight", null)).isTrue());+ try testing.expect((try ls.local.compileAndRun("document.elementFromPoint(" ++ rect ++ ".x, " ++ rect ++ ".y) === far", null)).isTrue());+}+test "cdp.dom: focus errors on an element that can't take focus" {var ctx = try testing.context();defer ctx.deinit();