Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: wheel scrolls per axis and lets scrollBy schedule the scroll events

Adrià Arrufat55dcad50d104
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 33494acfb..1073f01de 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -301,48 +301,64 @@ pub fn triggerMouseWheel(frame: *Frame, x: f64, y: f64, delta_x: f64, delta_y: f
return;
}
- // Scroll the nearest scroll container under the cursor, else the viewport.
// CDP deltas are untrusted, so guard NaN and saturate the addition.
- const dx = deltaToScroll(delta_x);
- const dy = deltaToScroll(delta_y);
- if (scrollContainerOf(target, frame)) |container| {
- const new_left: i32 = @as(i32, @intCast(container.getScrollLeft(frame))) +| dx;
- const new_top: i32 = @as(i32, @intCast(container.getScrollTop(frame))) +| dy;
- try container.setScrollLeft(new_left, frame);
- try container.setScrollTop(new_top, frame);
-
- const scroll_event = try Event.initTrusted(comptime .wrap("scroll"), .{ .bubbles = true }, frame._page);
- try frame._event_manager.dispatch(container.asEventTarget(), scroll_event);
+ try scrollAlong(target, .x, deltaToScroll(delta_x), frame);
+ try scrollAlong(target, .y, deltaToScroll(delta_y), frame);
+}
+
+const ScrollAxis = enum { x, y };
+
+// Each axis scrolls the nearest ancestor-or-self that is a scroll container
+// along it, else the viewport. Both scrollBy paths schedule the trusted
+// scroll/scrollend events themselves.
+fn scrollAlong(target: *Element, axis: ScrollAxis, delta: i32, frame: *Frame) !void {
+ if (delta == 0) {
return;
}
- try frame.window.scrollBy(.{ .x = dx }, dy, frame);
+ const left: i32, const top: i32 = switch (axis) {
+ .x => .{ delta, 0 },
+ .y => .{ 0, delta },
+ };
+ if (scrollContainerOf(target, axis, frame)) |container| {
+ return container.scrollBy(.{ .opts = .{ .left = left, .top = top } }, null, frame);
+ }
+ return frame.window.scrollBy(.{ .opts = .{ .left = left, .top = top } }, null, frame);
}
-// Closest ancestor-or-self with overflow auto or scroll; html/body scroll the viewport.
-fn scrollContainerOf(start: *Element, frame: *Frame) ?*Element {
+// html/body scroll the viewport.
+fn scrollContainerOf(start: *Element, axis: ScrollAxis, frame: *Frame) ?*Element {
var current: ?*Element = start;
while (current) |el| : (current = el.parentElement()) {
switch (el.getTag()) {
.html, .body => return null,
else => {},
}
- if (isScrollContainer(el, frame)) {
+ if (isScrollContainer(el, axis, frame)) {
return el;
}
}
return null;
}
-fn isScrollContainer(el: *Element, frame: *Frame) bool {
- const style = frame.window.getComputedStyle(el, null, frame) catch return false;
- const decl = style.asCSSStyleDeclaration();
- inline for (.{ "overflow-y", "overflow-x", "overflow" }) |property| {
- const value = decl.getPropertyValue(property, frame);
- if (std.mem.indexOf(u8, value, "auto") != null or std.mem.indexOf(u8, value, "scroll") != null) {
- return true;
- }
- }
- return false;
+// Only inline `overflow` is resolved: computed styles don't cascade stylesheet
+// rules, so a sheet-declared scroll container is treated as page content.
+fn isScrollContainer(el: *Element, axis: ScrollAxis, frame: *Frame) bool {
+ const style_manager = &frame._style_manager;
+ const longhand = switch (axis) {
+ .x => style_manager.inlineStyleValue(el, comptime .wrap("overflow-x")),
+ .y => style_manager.inlineStyleValue(el, comptime .wrap("overflow-y")),
+ };
+ const value = longhand orelse blk: {
+ // `overflow: <x> [<y>]`; a single value applies to both axes.
+ const shorthand = style_manager.inlineStyleValue(el, comptime .wrap("overflow")) orelse return false;
+ var it = std.mem.tokenizeScalar(u8, shorthand, ' ');
+ const x = it.next() orelse return false;
+ break :blk switch (axis) {
+ .x => x,
+ .y => it.next() orelse x,
+ };
+ };
+ return std.mem.eql(u8, value, "auto") or std.mem.eql(u8, value, "scroll");
}
fn deltaToScroll(d: f64) i32 {
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index 70f8a5f87..bf807601e 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -1988,8 +1988,8 @@ pub fn scrollBy(self: *Element, opts: ?ScrollToOpts, y: ?i32, frame: *Frame) !vo
};
const old_x = gop.value_ptr.x;
const old_y = gop.value_ptr.y;
- gop.value_ptr.x = @intCast(@max(0, @as(i32, @intCast(gop.value_ptr.x)) + dx));
- gop.value_ptr.y = @intCast(@max(0, @as(i32, @intCast(gop.value_ptr.y)) + dy));
+ gop.value_ptr.x = @intCast(@max(0, @as(i32, @intCast(gop.value_ptr.x)) +| dx));
+ gop.value_ptr.y = @intCast(@max(0, @as(i32, @intCast(gop.value_ptr.y)) +| dy));
if (gop.value_ptr.x != old_x or gop.value_ptr.y != old_y) {
try self.scheduleScrollEvents(owner);
}
diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zig
index 614fae5fd..7aea96217 100644
--- a/src/browser/webapi/Window.zig
+++ b/src/browser/webapi/Window.zig
@@ -998,19 +998,19 @@ pub fn scrollTo(self: *Window, opts: ScrollToOpts, y: ?i32, frame: *Frame) !void
);
}
-fn scrollBy(self: *Window, opts: ScrollToOpts, y: ?i32, frame: *Frame) !void {
+pub fn scrollBy(self: *Window, opts: ScrollToOpts, y: ?i32, frame: *Frame) !void {
// The scroll is relative to the current position. So compute to new
// absolute position.
var absx: i32 = undefined;
var absy: i32 = undefined;
switch (opts) {
.x => |x| {
- absx = @as(i32, @intCast(self._scroll_pos.x)) + x;
- absy = @as(i32, @intCast(self._scroll_pos.y)) + (y orelse 0);
+ absx = @as(i32, @intCast(self._scroll_pos.x)) +| x;
+ absy = @as(i32, @intCast(self._scroll_pos.y)) +| (y orelse 0);
},
.opts => |o| {
- absx = @as(i32, @intCast(self._scroll_pos.x)) + o.left;
- absy = @as(i32, @intCast(self._scroll_pos.y)) + o.top;
+ absx = @as(i32, @intCast(self._scroll_pos.x)) +| o.left;
+ absy = @as(i32, @intCast(self._scroll_pos.y)) +| o.top;
},
}
return self.scrollTo(.{ .x = absx }, absy, frame);
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index f32e5b4c9..0b0299571 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -380,10 +380,22 @@ test "cdp.input: dispatchMouseEvent mouseWheel scrolls a scroll container, not t
.params = .{ .type = "mouseWheel", .x = rect_x, .y = rect_y, .deltaY = 40 },
});
- const box = try ls.local.compileAndRun("document.getElementById('scrollbox').scrollTop === 40 && window.scrolled === true", null);
+ const box = try ls.local.compileAndRun("document.getElementById('scrollbox').scrollTop === 40 && window.scrollY === 0", null);
try testing.expect(box.isTrue());
- const win = try ls.local.compileAndRun("window.scrollY === 0", null);
- try testing.expect(win.isTrue());
+
+ // The scroll event is scheduled, not fired inline with the wheel.
+ var runner = bc.session.runner(.{});
+ try runner.waitForScript(frame._frame_id, "window.scrolled === true", 1000);
+
+ // Per axis: x is not scrollable on the box, so it goes to the viewport.
+ _ = try ls.local.compileAndRun("document.getElementById('scrollbox').style.overflow = 'hidden scroll'", null);
+ try ctx.processMessage(.{
+ .id = 2,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseWheel", .x = rect_x, .y = rect_y, .deltaX = 30, .deltaY = 10 },
+ });
+ 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 on page content scrolls the viewport" {