Lightpanda: the headless browser designed for AI and automation
Make the scroll target and the scroll result explicit
Adrià Arrufat86904f63baa2
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zigindex e18e9db9b..0187fba5a 100644--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@ -715,12 +715,12 @@ pub fn hasPointerEventsNone(self: *StyleManager, el: *Element) bool {return self.anyInChain(el, .pointer_events, .{});}-/// Whether `el` is a scroll container along any of `axes`: its own computed-/// overflow on that axis is auto, scroll or overlay. No ancestor walk.-pub fn scrolls(self: *StyleManager, el: *Element, axes: Element.ScrollAxes) bool {- self.rebuildIfDirty() catch return false;+/// The axes along which `el` is a scroll container: its own computed overflow+/// on that axis is auto, scroll or overlay. No ancestor walk.+pub fn overflowAxes(self: *StyleManager, el: *Element) Element.ScrollAxes {+ self.rebuildIfDirty() catch return .{};const p = self.ownProps(el);- return (axes.x and p.overflow_x_scrolls) or (axes.y and p.overflow_y_scrolls);+ return .{ .x = p.overflow_x_scrolls, .y = p.overflow_y_scrolls };}fn anyInChain(self: *StyleManager, el: *Element, comptime what: Probe, options: CheckVisibilityOptions) bool {@@ -1752,10 +1752,8 @@ test "StyleManager: memo: reuse and invalidation" {try testing.expectEqual(false, sm.hasPointerEventsNone(b));try b.setStyle("overflow: hidden auto", frame);- try testing.expectEqual(false, sm.scrolls(b, .{ .x = true }));- try testing.expectEqual(true, sm.scrolls(b, .{ .y = true }));- try testing.expectEqual(true, sm.scrolls(b, .{ .x = true, .y = true }));- try testing.expectEqual(false, sm.scrolls(p, .{ .x = true, .y = true }));+ try testing.expectEqual(Element.ScrollAxes{ .x = false, .y = true }, sm.overflowAxes(b));+ try testing.expectEqual(Element.ScrollAxes{}, sm.overflowAxes(p));// A stylesheet change resets the memosm.sheetModified();diff --git a/src/browser/actions.zig b/src/browser/actions.zigindex a60505d04..d3bf93d13 100644--- a/src/browser/actions.zig+++ b/src/browser/actions.zig@@ -288,9 +288,13 @@ pub fn fill(node: *DOMNode, text: []const u8, frame: *Frame) !void {}pub const ScrollResult = struct {- /// What moved: the given node, its nearest scroll container, or null for- /// the window.- scrolled: ?*DOMNode,+ target: union(enum) {+ window,+ /// The given node scrolled itself.+ node: *DOMNode,+ /// The given node's nearest scroll container scrolled instead of it.+ container: struct { node: *DOMNode, container: *DOMNode },+ },x: u32,y: u32,};@@ -301,17 +305,22 @@ pub fn scroll(node: ?*DOMNode, x: ?i32, y: ?i32, frame: *Frame) !ScrollResult {lp.log.err(.app, "scroll failed", .{ .err = err });return error.ActionFailed;};- return .{ .scrolled = null, .x = frame.window.getScrollX(), .y = frame.window.getScrollY() };+ return .{ .target = .window, .x = frame.window.getScrollX(), .y = frame.window.getScrollY() };};const el = n.is(Element) orelse return error.InvalidNodeType;- const target = el.scrollContainer(.{ .x = x != null, .y = y != null }, frame) orelse el;+ // A node with no scroll container scrolls itself, not the viewport: the+ // caller named it.+ const target = switch (el.scrollContainers(.{ .x = x != null, .y = y != null }, frame).nearest) {+ .container => |container| container,+ .viewport => el,+ };target.scrollTo(.{ .opts = .{ .left = x, .top = y } }, null, frame) catch |err| {lp.log.err(.app, "scroll failed", .{ .err = err });return error.ActionFailed;};return .{- .scrolled = target.asNode(),+ .target = if (target == el) .{ .node = n } else .{ .container = .{ .node = n, .container = target.asNode() } },.x = target.getScrollLeft(frame),.y = target.getScrollTop(frame),};diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 82f1b7f25..c5677fd4f 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -326,20 +326,20 @@ pub fn wheel(frame: *Frame, target: *Element, x: f64, y: f64, delta_x: f64, delt/// else the viewport. Relative deltas may land on different scrollers per/// axis, unlike an absolute position.pub fn wheelScroll(target: *Element, delta_x: i32, delta_y: i32, frame: *Frame) !void {- try scrollAlong(target, .{ .x = true }, delta_x, frame);- try scrollAlong(target, .{ .y = true }, delta_y, frame);-}--fn scrollAlong(target: *Element, axes: Element.ScrollAxes, delta: i32, frame: *Frame) !void {- if (delta == 0) {- return;+ const targets = target.scrollContainers(.{ .x = delta_x != 0, .y = delta_y != 0 }, frame);+ if (delta_x != 0) {+ try scrollBy(targets.x, delta_x, 0, frame);}- const left: i32 = if (axes.x) delta else 0;- const top: i32 = if (axes.y) delta else 0;- if (target.scrollContainer(axes, frame)) |container| {- return container.scrollBy(.{ .opts = .{ .left = left, .top = top } }, null, frame);+ if (delta_y != 0) {+ try scrollBy(targets.y, 0, delta_y, frame);}- return frame.window.scrollBy(.{ .opts = .{ .left = left, .top = top } }, null, frame);+}++fn scrollBy(target: Element.ScrollTarget, left: i32, top: i32, frame: *Frame) !void {+ return switch (target) {+ .container => |el| el.scrollBy(.{ .opts = .{ .left = left, .top = top } }, null, frame),+ .viewport => frame.window.scrollBy(.{ .opts = .{ .left = left, .top = top } }, null, frame),+ };}fn deltaToScroll(d: f64) i32 {diff --git a/src/browser/tools.zig b/src/browser/tools.zigindex d22868708..c92314ec5 100644--- a/src/browser/tools.zig+++ b/src/browser/tools.zig@@ -1885,16 +1885,28 @@ fn execScroll(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeRegconst result = lp.actions.scroll(target_node, args.x, args.y, page) catch |err| return mapActionError(err);- const body = (if (result.scrolled) |scrolled| blk: {- const moved: ActionTarget = .{ .backend_node_id = (registry.register(scrolled) catch return ToolError.InternalError).id };- break :blk if (scrolled == target_node)- std.fmt.allocPrint(arena, "Scrolled element ({f}) to x: {d}, y: {d}", .{ moved, result.x, result.y })- else- std.fmt.allocPrint(arena, "Scrolled scroll container ({f}) of element ({f}) to x: {d}, y: {d}", .{ moved, ActionTarget{ .backend_node_id = args.backendNodeId.? }, result.x, result.y });- } else std.fmt.allocPrint(arena, "Scrolled window to x: {d}, y: {d}", .{ result.x, result.y })) catch return ToolError.InternalError;+ const body = (switch (result.target) {+ .window => std.fmt.allocPrint(arena, "Scrolled window to x: {d}, y: {d}", .{ result.x, result.y }),+ .node => |node| std.fmt.allocPrint(arena, "Scrolled element ({f}) to x: {d}, y: {d}", .{+ try registeredTarget(registry, node),+ result.x,+ result.y,+ }),+ .container => |c| std.fmt.allocPrint(arena, "Scrolled scroll container ({f}) of element ({f}) to x: {d}, y: {d}", .{+ try registeredTarget(registry, c.container),+ try registeredTarget(registry, c.node),+ result.x,+ result.y,+ }),+ }) catch return ToolError.InternalError;return finalizeAction(arena, session, registry, scope, body);}+fn registeredTarget(registry: *NodeRegistry, node: *DOMNode) ToolError!ActionTarget {+ const registered = registry.register(node) catch return ToolError.InternalError;+ return .{ .backend_node_id = registered.id };+}+/// Default timeout for the `waitFor*` tools — short, since they wait on an/// already-loaded page rather than a full navigation (which uses 10000).const default_wait_timeout_ms: u32 = 5000;diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex bc8d7e42a..ea43a26eb 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -1602,21 +1602,50 @@ pub fn setScrollLeft(self: *Element, value: i32, frame: *Frame) !void {pub const ScrollAxes = struct { x: bool = false, y: bool = false };-/// Nearest ancestor-or-self that is a scroll container along any of `axes`.-/// null once the chain reaches html/body: those scroll the viewport.-pub fn scrollContainer(self: *Element, axes: ScrollAxes, frame: *Frame) ?*Element {- if (!axes.x and !axes.y) return null;- const owner = self.ownerFrame(frame) orelse return null;+/// What a scroll along one axis lands on. html and body scroll the viewport,+/// so does a detached element.+pub const ScrollTarget = union(enum) {+ viewport,+ container: *Element,+};++pub const ScrollTargets = struct {+ x: ScrollTarget = .viewport,+ y: ScrollTarget = .viewport,+ /// Whichever of the two is closest to the element, for callers that+ /// position one scroller with an absolute offset.+ nearest: ScrollTarget = .viewport,+};++/// Nearest ancestor-or-self scroll container per requested axis, in one walk.+/// An axis not in `axes` stays `.viewport`.+pub fn scrollContainers(self: *Element, axes: ScrollAxes, frame: *Frame) ScrollTargets {+ var targets: ScrollTargets = .{};+ var pending = axes;+ const owner = self.ownerFrame(frame) orelse return targets;const style_manager = &owner._style_manager;var current: ?*Element = self;while (current) |el| : (current = el.parentElement()) {+ if (!pending.x and !pending.y) break;const tag = el.getTag();- if (tag == .html or tag == .body) return null;- if (style_manager.scrolls(el, axes)) {- return el;+ if (tag == .html or tag == .body) break;+ const scrolls = style_manager.overflowAxes(el);+ const hit_x = pending.x and scrolls.x;+ const hit_y = pending.y and scrolls.y;+ if (!hit_x and !hit_y) continue;+ if (targets.nearest == .viewport) {+ targets.nearest = .{ .container = el };+ }+ if (hit_x) {+ targets.x = .{ .container = el };+ pending.x = false;+ }+ if (hit_y) {+ targets.y = .{ .container = el };+ pending.y = false;}}- return null;+ return targets;}pub fn getScrollHeight(self: *Element, frame: *Frame) f64 {diff --git a/src/server/cdp/domains/lp.zig b/src/server/cdp/domains/lp.zigindex 109b429d4..b4b1411c0 100644--- a/src/server/cdp/domains/lp.zig+++ b/src/server/cdp/domains/lp.zig@@ -399,10 +399,11 @@ fn scrollNode(cmd: anytype) !void {return error.InternalError;};- const target_id: ?NodeRegistry.Id = if (result.scrolled) |scrolled|- (try bc.node_registry.register(scrolled)).id- else- null;+ const target_id: ?NodeRegistry.Id = switch (result.target) {+ .window => null,+ .node => |node| (try bc.node_registry.register(node)).id,+ .container => |c| (try bc.node_registry.register(c.container)).id,+ };return cmd.sendResult(.{ .backendNodeId = target_id, .x = result.x, .y = result.y }, .{});}