Skip to content

Lightpanda: the headless browser designed for AI and automation

Cleanup pass over the scroll and wheel changes

Adrià Arrufat48d11e981c90
diff --git a/src/browser/EventManager.zig b/src/browser/EventManager.zig
index 0137b2f83..8fa5a79a5 100644
--- a/src/browser/EventManager.zig
+++ b/src/browser/EventManager.zig
@@ -153,26 +153,15 @@ pub fn hasDirectListeners(self: *EventManager, target: *EventTarget, typ: []cons
/// is non-passive. The UA dispatches scroll-blocking events as
/// non-cancelable when none is: it already knows preventDefault can't be
/// called.
-pub fn hasNonPassiveListener(self: *EventManager, node: *Node, typ: []const u8) bool {
+pub fn hasNonPassiveListener(self: *EventManager, node: *Node, comptime typ: []const u8) bool {
+ const event_type: lp.String = comptime .wrap(typ);
var current: ?*Node = node;
while (current) |n| : (current = n.parentNode()) {
- if (anyNonPassive(self.base.getListeners(n.asEventTarget(), .wrap(typ)))) {
+ if (self.base.hasNonPassiveListener(n.asEventTarget(), event_type)) {
return true;
}
}
- return anyNonPassive(self.base.getListeners(self.frame.window.asEventTarget(), .wrap(typ)));
-}
-
-fn anyNonPassive(list_: ?*std.DoublyLinkedList) bool {
- const list = list_ orelse return false;
- var link = list.first;
- while (link) |l| : (link = l.next) {
- const listener: *align(8) Listener = @fieldParentPtr("node", l);
- if (!listener.passive) {
- return true;
- }
- }
- return false;
+ return self.base.hasNonPassiveListener(self.frame.window.asEventTarget(), event_type);
}
fn dispatchNode(self: *EventManager, target: *Node, event: *Event) !void {
diff --git a/src/browser/EventManagerBase.zig b/src/browser/EventManagerBase.zig
index c648f9821..c6ad1a124 100644
--- a/src/browser/EventManagerBase.zig
+++ b/src/browser/EventManagerBase.zig
@@ -205,6 +205,21 @@ pub fn getListeners(self: *EventManagerBase, target: *EventTarget, event_type: S
});
}
+/// Whether the target has a listener for `event_type` that can call
+/// preventDefault. A removed listener isn't "in" the list anymore, same as
+/// findListener.
+pub fn hasNonPassiveListener(self: *EventManagerBase, target: *EventTarget, event_type: String) bool {
+ const list = self.getListeners(target, event_type) orelse return false;
+ var node = list.first;
+ while (node) |n| : (node = n.next) {
+ const listener: *align(8) Listener = @fieldParentPtr("node", n);
+ if (!listener.removed and !listener.passive) {
+ return true;
+ }
+ }
+ return false;
+}
+
// Dispatching can be recursive from the compiler's point of view, so we need to
// give it an explicit error set so that other parts of the code can use an
// inferred error.
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 0187fba5a..18f2ebd5e 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -34,7 +34,6 @@ const CSSRule = @import("webapi/css/CSSRule.zig");
const CSSStyleRule = @import("webapi/css/CSSStyleRule.zig");
const CSSStyleSheet = @import("webapi/css/CSSStyleSheet.zig");
const CSSStyleProperties = @import("webapi/css/CSSStyleProperties.zig");
-const CSSStyleDeclaration = @import("webapi/css/CSSStyleDeclaration.zig");
const log = lp.log;
const String = lp.String;
@@ -1025,9 +1024,8 @@ const property_names = [_][]const u8{ "display", "visibility", "opacity", "point
/// shorthand and its longhands the same precedence as the source text.
fn extractTrackedProperties(style: *CSSStyleProperties) TrackedProperties {
var slots: Slots = .{};
- var node = style.asCSSStyleDeclaration()._properties.first;
- while (node) |n| : (node = n.next) {
- const property = CSSStyleDeclaration.Property.fromNodeLink(n);
+ var it = style.asCSSStyleDeclaration().iterator();
+ while (it.next()) |property| {
slots.apply(property._name.str(), property._value.str(), property._important);
}
return slots.props();
@@ -1308,12 +1306,11 @@ const Slots = struct {
// `overflow: <x> [<y>]`; a single value applies to both axes.
var it = std.mem.tokenizeAny(u8, value, &std.ascii.whitespace);
const x = it.next() orelse "";
- const y = it.next() orelse x;
- self.slotFor("overflow-x").apply(x, important);
- self.slotFor("overflow-y").apply(y, important);
+ self.apply("overflow-x", x, important);
+ self.apply("overflow-y", it.next() orelse x, important);
return;
}
- for (property_names, &self.slots) |tracked, *slot| {
+ inline for (property_names, &self.slots) |tracked, *slot| {
if (std.ascii.eqlIgnoreCase(name, tracked)) {
slot.apply(value, important);
return;
@@ -1321,18 +1318,9 @@ const Slots = struct {
}
}
- fn slotFor(self: *Slots, comptime name: []const u8) *Slot {
- inline for (property_names, 0..) |tracked, i| {
- if (comptime std.mem.eql(u8, tracked, name)) {
- return &self.slots[i];
- }
- }
- comptime unreachable;
- }
-
fn props(self: Slots) TrackedProperties {
var p: TrackedProperties = .{};
- for (property_names, self.slots) |name, s| {
+ inline for (property_names, self.slots) |name, s| {
if (s.value) |value| {
p.apply(name, value);
}
diff --git a/src/browser/actions.zig b/src/browser/actions.zig
index d3bf93d13..d0cf990cc 100644
--- a/src/browser/actions.zig
+++ b/src/browser/actions.zig
@@ -288,12 +288,12 @@ pub fn fill(node: *DOMNode, text: []const u8, frame: *Frame) !void {
}
pub const ScrollResult = struct {
+ /// What scrolled. Always the node the caller named, its nearest scroll
+ /// container, or the window.
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 },
+ container: *DOMNode,
},
x: u32,
y: u32,
@@ -311,7 +311,7 @@ pub fn scroll(node: ?*DOMNode, x: ?i32, y: ?i32, frame: *Frame) !ScrollResult {
// 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) {
+ const target = switch (el.scrollContainer(.{ .x = x != null, .y = y != null }, frame)) {
.container => |container| container,
.viewport => el,
};
@@ -320,7 +320,7 @@ pub fn scroll(node: ?*DOMNode, x: ?i32, y: ?i32, frame: *Frame) !ScrollResult {
return error.ActionFailed;
};
return .{
- .target = if (target == el) .{ .node = n } else .{ .container = .{ .node = n, .container = target.asNode() } },
+ .target = if (target == el) .{ .node = n } else .{ .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.zig
index c5677fd4f..a20498158 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -327,19 +327,9 @@ pub fn wheel(frame: *Frame, target: *Element, x: f64, y: f64, delta_x: f64, delt
/// axis, unlike an absolute position.
pub fn wheelScroll(target: *Element, delta_x: i32, delta_y: i32, frame: *Frame) !void {
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);
- }
- if (delta_y != 0) {
- try scrollBy(targets.y, 0, delta_y, 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),
- };
+ // A zero delta resolves to .viewport and scrolls it by nothing.
+ try targets.x.scrollBy(delta_x, 0, frame);
+ try targets.y.scrollBy(0, delta_y, frame);
}
fn deltaToScroll(d: f64) i32 {
diff --git a/src/browser/tools.zig b/src/browser/tools.zig
index c92314ec5..88b91bfaa 100644
--- a/src/browser/tools.zig
+++ b/src/browser/tools.zig
@@ -1887,26 +1887,24 @@ fn execScroll(arena: std.mem.Allocator, session: *lp.Session, registry: *NodeReg
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),
+ .node => std.fmt.allocPrint(arena, "Scrolled element ({f}) to x: {d}, y: {d}", .{
+ ActionTarget{ .backend_node_id = args.backendNodeId.? },
result.x,
result.y,
}),
+ .container => |container| blk: {
+ const registered = registry.register(container) catch return ToolError.InternalError;
+ break :blk std.fmt.allocPrint(arena, "Scrolled scroll container ({f}) of element ({f}) to x: {d}, y: {d}", .{
+ ActionTarget{ .backend_node_id = registered.id },
+ ActionTarget{ .backend_node_id = args.backendNodeId.? },
+ 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.zig
index ea43a26eb..d5f4396a9 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -22,6 +22,7 @@ const lp = @import("lightpanda");
const js = @import("../js/js.zig");
const dump = @import("../dump.zig");
const Frame = @import("../Frame.zig");
+const StyleManager = @import("../StyleManager.zig");
const Factory = @import("../Factory.zig");
const CSS = @import("CSS.zig");
@@ -1603,51 +1604,78 @@ pub fn setScrollLeft(self: *Element, value: i32, frame: *Frame) !void {
pub const ScrollAxes = struct { x: bool = false, y: bool = false };
/// What a scroll along one axis lands on. html and body scroll the viewport,
-/// so does a detached element.
+/// and so does a detached element.
pub const ScrollTarget = union(enum) {
viewport,
container: *Element,
+
+ pub fn scrollBy(self: ScrollTarget, left: i32, top: i32, frame: *Frame) !void {
+ const opts: ScrollToOpts = .{ .opts = .{ .left = left, .top = top } };
+ return switch (self) {
+ .container => |el| el.scrollBy(opts, null, frame),
+ .viewport => frame.window.scrollBy(opts, null, frame),
+ };
+ }
};
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 along any of `axes`, for callers
+/// positioning one scroller with an absolute offset.
+pub fn scrollContainer(self: *Element, axes: ScrollAxes, frame: *Frame) ScrollTarget {
+ const style_manager = self.scrollStyleManager(axes, frame) orelse return .viewport;
+ var current: ?*Element = self;
+ while (current) |el| : (current = el.parentElement()) {
+ if (el.scrollsViewport()) break;
+ const scrolls = style_manager.overflowAxes(el);
+ if ((axes.x and scrolls.x) or (axes.y and scrolls.y)) {
+ return .{ .container = el };
+ }
+ }
+ return .viewport;
+}
+
/// Nearest ancestor-or-self scroll container per requested axis, in one walk.
-/// An axis not in `axes` stays `.viewport`.
+/// Relative deltas may land on a different scroller per axis. 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;
+ const style_manager = self.scrollStyleManager(axes, frame) orelse return targets;
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) break;
+ if (el.scrollsViewport()) 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) {
+ if (pending.x and scrolls.x) {
targets.x = .{ .container = el };
pending.x = false;
}
- if (hit_y) {
+ if (pending.y and scrolls.y) {
targets.y = .{ .container = el };
pending.y = false;
}
+ if (!pending.x and !pending.y) break;
}
return targets;
}
+/// null when the walk cannot find anything: no axis asked for, or detached.
+fn scrollStyleManager(self: *Element, axes: ScrollAxes, frame: *Frame) ?*StyleManager {
+ if (!axes.x and !axes.y) return null;
+ const owner = self.ownerFrame(frame) orelse return null;
+ return &owner._style_manager;
+}
+
+fn scrollsViewport(self: *const Element) bool {
+ return switch (self.getTag()) {
+ .html, .body => true,
+ else => false,
+ };
+}
+
pub fn getScrollHeight(self: *Element, frame: *Frame) f64 {
if (!self.isVisible(frame)) {
return 0.0;
diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zig
index 71c8d2e99..538adc955 100644
--- a/src/browser/webapi/WebDriver.zig
+++ b/src/browser/webapi/WebDriver.zig
@@ -378,6 +378,9 @@ fn performWheelSource(source: js.Object, frame: *Frame) !void {
continue;
}
+ const x = readI32(action, "x", 0);
+ const y = readI32(action, "y", 0);
+
const origin = try action.get("origin");
var el: ?*Element = null;
if (origin.isObject()) {
@@ -385,7 +388,6 @@ fn performWheelSource(source: js.Object, frame: *Frame) !void {
} else {
// "viewport"/"pointer" origins: approximate hit-testing with
// the faux layout's vertical axis, falling back to the root.
- const y = readI32(action, "y", 0);
el = frame.document.elementFromVerticalPoint(@floatFromInt(y), frame) catch null;
if (el == null) {
el = frame.document.getDocumentElement();
@@ -395,7 +397,7 @@ fn performWheelSource(source: js.Object, frame: *Frame) !void {
const delta_x = readI32(action, "deltaX", 0);
const delta_y = readI32(action, "deltaY", 0);
- dispatchWheel(target, delta_x, delta_y, frame);
+ dispatchWheel(target, x, y, delta_x, delta_y, frame);
}
}
@@ -585,9 +587,11 @@ fn dispatchMouse(el: *Element, comptime typ: []const u8, button: i32, buttons: u
};
}
-fn dispatchWheel(el: *Element, delta_x: i32, delta_y: i32, frame: *Frame) void {
- const rect = el.boundingClientRectValues(frame);
- Frame.user_input.wheel(frame, el, rect.x, rect.y, @floatFromInt(delta_x), @floatFromInt(delta_y)) catch |err| {
+// The action's x/y, which the caller already resolved the target from. An
+// element origin makes them offsets from its center, but the faux layout has
+// no center worth computing.
+fn dispatchWheel(el: *Element, x: i32, y: i32, delta_x: i32, delta_y: i32, frame: *Frame) void {
+ Frame.user_input.wheel(frame, el, @floatFromInt(x), @floatFromInt(y), @floatFromInt(delta_x), @floatFromInt(delta_y)) catch |err| {
log.warn(.app, "webdriver wheel", .{ .err = err });
};
}
@@ -606,11 +610,11 @@ fn dispatchTouch(el: *Element, comptime typ: []const u8, frame: *Frame) void {
.bubbles = true,
.cancelable = owner._event_manager.hasNonPassiveListener(el.asNode(), typ),
.composed = true,
- }, frame) catch |err| {
+ }, owner) catch |err| {
log.warn(.app, "webdriver touch event", .{ .err = err });
return;
};
- dispatch(el.asEventTarget(), event.asEvent(), frame, typ);
+ dispatch(el.asEventTarget(), event.asEvent(), owner, typ);
}
pub const JsApi = struct {
diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zig
index ed885ea12..887681123 100644
--- a/src/browser/webapi/css/CSSStyleDeclaration.zig
+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig
@@ -920,7 +920,7 @@ pub const Property = struct {
_important: bool = false,
_node: std.DoublyLinkedList.Node,
- pub fn fromNodeLink(n: *std.DoublyLinkedList.Node) *Property {
+ fn fromNodeLink(n: *std.DoublyLinkedList.Node) *Property {
return @alignCast(@fieldParentPtr("_node", n));
}
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index 4f131902a..69b9c8db1 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -448,41 +448,17 @@ test "cdp.input: dispatchMouseEvent mouseWheel scrolls a scroll container, not t
});
const split = try ls.local.compileAndRun("document.getElementById('scrollbox').scrollTop === 50 && document.getElementById('scrollbox').scrollLeft === 0 && window.scrollX === 30 && window.scrollY === 0", null);
try testing.expect(split.isTrue());
-}
-
-test "cdp.input: dispatchMouseEvent mouseWheel scrolls a stylesheet-declared scroll container" {
- var ctx = try testing.context();
- defer ctx.deinit();
-
- const bc = try ctx.loadBrowserContext(.{});
- const page = try bc.session.createPage();
- const frame = page.frame().?;
-
- const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";
- try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });
- try testing.waitForPage(bc);
-
- var ls: lp.js.Local.Scope = undefined;
- frame.js.localScope(&ls);
- defer ls.deinit();
-
- var try_catch: lp.js.TryCatch = undefined;
- try_catch.init(&ls.local);
- defer try_catch.deinit();
-
- const rect_x = try (try ls.local.compileAndRun("document.getElementById('sheetleaf').getBoundingClientRect().x", null)).toF64();
- const rect_y = try (try ls.local.compileAndRun("document.getElementById('sheetleaf').getBoundingClientRect().y", null)).toF64();
+ // The container may be declared in a stylesheet rather than inline.
+ const sheet_x = try (try ls.local.compileAndRun("document.getElementById('sheetleaf').getBoundingClientRect().x", null)).toF64();
+ const sheet_y = try (try ls.local.compileAndRun("document.getElementById('sheetleaf').getBoundingClientRect().y", null)).toF64();
try ctx.processMessage(.{
- .id = 1,
+ .id = 3,
.method = "Input.dispatchMouseEvent",
- .params = .{ .type = "mouseWheel", .x = rect_x, .y = rect_y, .deltaY = 40 },
+ .params = .{ .type = "mouseWheel", .x = sheet_x, .y = sheet_y, .deltaY = 40 },
});
-
- const result = try ls.local.compileAndRun("document.getElementById('sheetscroll').scrollTop === 40 && document.getElementById('sheetleaf').scrollTop === 0 && window.scrollY === 0", null);
- try testing.expect(result.isTrue());
-
- var runner = bc.session.runner(.{});
+ const sheet = try ls.local.compileAndRun("document.getElementById('sheetscroll').scrollTop === 40 && document.getElementById('sheetleaf').scrollTop === 0 && window.scrollY === 0", null);
+ try testing.expect(sheet.isTrue());
try runner.waitForScript(frame._frame_id, "window.sheetScrolled === true", 1000);
}
diff --git a/src/server/cdp/domains/lp.zig b/src/server/cdp/domains/lp.zig
index b4b1411c0..716eb5a46 100644
--- a/src/server/cdp/domains/lp.zig
+++ b/src/server/cdp/domains/lp.zig
@@ -401,8 +401,7 @@ fn scrollNode(cmd: anytype) !void {
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,
+ .node, .container => |scrolled| (try bc.node_registry.register(scrolled)).id,
};
return cmd.sendResult(.{ .backendNodeId = target_id, .x = result.x, .y = result.y }, .{});
}