Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: wheel events scroll the viewport unless over a scroll container

Adrià Arrufatf9eb4e437aca
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 70889bb85..f9d009cd0 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -269,7 +269,9 @@ pub fn triggerMouseRelease(frame: *Frame, x: f64, y: f64, button: i32, click_cou
}
pub fn triggerMouseWheel(frame: *Frame, x: f64, y: f64, delta_x: f64, delta_y: f64) !void {
- const target = (try frame.window._document.elementFromPoint(x, y, frame)) orelse return;
+ const document = frame.window._document;
+ const target = (try document.elementFromPoint(x, y, frame)) orelse
+ document.getDocumentElement() orelse return;
if (comptime lp.IS_DEBUG) {
log.debug(.frame, "frame mouse wheel", .{
.url = frame.url,
@@ -301,15 +303,48 @@ pub fn triggerMouseWheel(frame: *Frame, x: f64, y: f64, delta_x: f64, delta_y: f
return;
}
- // Apply the scroll and fire a trusted scroll event, mirroring WebDriver wheel.
+ // Scroll the nearest scroll container under the cursor, else the viewport.
// CDP deltas are untrusted, so guard NaN and saturate the addition.
- const new_left: i32 = @as(i32, @intCast(target.getScrollLeft(frame))) +| deltaToScroll(delta_x);
- const new_top: i32 = @as(i32, @intCast(target.getScrollTop(frame))) +| deltaToScroll(delta_y);
- try target.setScrollLeft(new_left, frame);
- try target.setScrollTop(new_top, frame);
+ 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);
+ return;
+ }
+ try frame.window.scrollBy(.{ .x = dx }, dy, frame);
+}
- const scroll_event = try Event.initTrusted(comptime .wrap("scroll"), .{ .bubbles = true }, frame._page);
- try frame._event_manager.dispatch(target.asEventTarget(), scroll_event);
+// Closest ancestor-or-self with overflow auto or scroll; html/body scroll the viewport.
+fn scrollContainerOf(start: *Element, 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)) {
+ 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;
}
fn deltaToScroll(d: f64) i32 {
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index b4b83fe12..f7fa40121 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -297,6 +297,90 @@ test "cdp.input: dispatchMouseEvent mouseWheel fires wheel event" {
try testing.expect(result.isTrue());
}
+test "cdp.input: dispatchMouseEvent mouseWheel scrolls a scroll container, not the viewport" {
+ 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('scrollbox').getBoundingClientRect().x", null)).toF64();
+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('scrollbox').getBoundingClientRect().y", null)).toF64();
+
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Input.dispatchMouseEvent",
+ .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);
+ try testing.expect(box.isTrue());
+ const win = try ls.local.compileAndRun("window.scrollY === 0", null);
+ try testing.expect(win.isTrue());
+}
+
+test "cdp.input: dispatchMouseEvent mouseWheel on page content scrolls the viewport" {
+ 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('hoverTarget').getBoundingClientRect().x", null)).toF64();
+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().y", null)).toF64();
+
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseWheel", .x = rect_x, .y = rect_y, .deltaX = 5, .deltaY = 600 },
+ });
+ var result = try ls.local.compileAndRun("window.scrollX === 5 && window.scrollY === 600", null);
+ try testing.expect(result.isTrue());
+
+ try ctx.processMessage(.{
+ .id = 2,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseWheel", .x = rect_x, .y = rect_y, .deltaY = -200 },
+ });
+ result = try ls.local.compileAndRun("window.scrollY === 400", null);
+ try testing.expect(result.isTrue());
+
+ // No element under the point.
+ try ctx.processMessage(.{
+ .id = 3,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mouseWheel", .x = 100_000, .y = 100_000, .deltaY = 100 },
+ });
+ result = try ls.local.compileAndRun("window.scrollY === 500", null);
+ try testing.expect(result.isTrue());
+}
+
test "cdp.input: dispatchMouseEvent right button fires contextmenu, double-click fires dblclick" {
var ctx = try testing.context();
defer ctx.deinit();