Lightpanda: the headless browser designed for AI and automation
refactor(input): share pointer/mouse click dispatch across click paths
Ramiro_quai8a57a492a814
diff --git a/src/browser/Page.zig b/src/browser/Page.zigindex 045d0d93d..accd34861 100644--- a/src/browser/Page.zig+++ b/src/browser/Page.zig@@ -186,6 +186,13 @@ input_modifiers: if (lp.build_config.wpt_extensions) @import("frame/user_input.z// The element the synthetic pointer is currently overinput_hover_target: ?*Element = null,+// Whether the in-flight mouse gesture's pointerdown suppressed the+// compatibility mousedown/mouseup (preventDefault() was called). CDP's+// mousePressed/mouseReleased arrive as two independent+// Input.dispatchMouseEvent messages, so the release half reads this instead+// of carrying the state itself.+input_mousedown_suppressed: bool = false,+// Popup Frames opened by window.open. They are top-level browsing contexts// (parent == null, no iframe element) but share this Page's factory, arena,// and identity map.diff --git a/src/browser/actions.zig b/src/browser/actions.zigindex 481d88e9a..cf097ba40 100644--- a/src/browser/actions.zig+++ b/src/browser/actions.zig@@ -22,7 +22,6 @@ const DOMNode = @import("webapi/Node.zig");const Element = @import("webapi/Element.zig");const Event = @import("webapi/Event.zig");const MouseEvent = @import("webapi/event/MouseEvent.zig");-const PointerEvent = @import("webapi/event/PointerEvent.zig");const KeyboardEvent = @import("webapi/event/KeyboardEvent.zig");const Frame = @import("Frame.zig");const Session = @import("Session.zig");@@ -39,41 +38,12 @@ fn dispatchInputAndChangeEvents(el: *Element, frame: *Frame) !void {};}-fn dispatch(el: *Element, event: *Event, comptime typ: []const u8, frame: *Frame) !bool {- return frame._event_manager.dispatchCancelable(el.asEventTarget(), event) catch |err| {- lp.log.err(.app, "click " ++ typ ++ " failed", .{ .err = err });- return error.ActionFailed;- };-}--fn dispatchPointer(el: *Element, comptime typ: []const u8, buttons: u16, detail: u32, frame: *Frame) !bool {- const event: *PointerEvent = try .initTrusted(typ, .{- .bubbles = true,- .cancelable = true,- .composed = true,- .buttons = buttons,- .detail = detail,- .pointerId = 1,- .pointerType = "mouse",- .isPrimary = true,- .pressure = if (buttons != 0) 0.5 else 0.0,- }, frame);- return dispatch(el, event.asEvent(), typ, frame);-}--fn dispatchMouse(el: *Element, comptime typ: []const u8, buttons: u16, frame: *Frame) !bool {- const event: *MouseEvent = try .initTrusted(comptime .wrap(typ), .{- .bubbles = true,- .cancelable = true,- .composed = true,- .buttons = buttons,- .detail = 1,- }, frame);- return dispatch(el, event.asEvent(), typ, frame);-}-/// The trusted primary-button gesture a real user click produces; widgets key-/// off pointerdown/mousedown, not click alone.+/// off pointerdown/mousedown, not click alone. Shares its dispatch mechanics+/// with `WebDriver.click` and the CDP mouse-press/release path (both go+/// through `Frame.user_input.dispatchPointer{Press,Release}`); this call+/// site's own error-handling choice — a focus failure is worth a warning,+/// not aborting the click — lives here, not in the shared code.pub fn click(node: *DOMNode, frame: *Frame) !void {const el = node.is(Element) orelse return error.InvalidNodeType;@@ -83,25 +53,26 @@ pub fn click(node: *DOMNode, frame: *Frame) !void {Frame.user_input.updateHoverTarget(frame, el, .{ .with_pointer = true });- // preventDefault() on pointerdown suppresses both compatibility mouse- // events (mousedown and mouseup) for the rest of this gesture; click- // still fires.- const suppress_mouse = try dispatchPointer(el, "pointerdown", 1, 0, frame);- if (!suppress_mouse) {- const suppress_focus = try dispatchMouse(el, "mousedown", 1, frame);- if (!suppress_focus) {- Frame.user_input.focusForMouseDown(frame, el) catch |err| {- lp.log.warn(.app, "click mousedown focus", .{ .err = err });- };- }+ const main = Frame.user_input.mouse_button.main;+ const press_result = Frame.user_input.dispatchPointerPress(frame, el, 0, 0, main, 1, .{}) catch |err| {+ lp.log.err(.app, "click press failed", .{ .err = err });+ return error.ActionFailed;+ };+ if (!press_result.suppress_mouse and !press_result.suppress_focus) {+ Frame.user_input.focusForMouseDown(frame, el) catch |err| {+ lp.log.warn(.app, "click mousedown focus", .{ .err = err });+ };}- _ = try dispatchPointer(el, "pointerup", 0, 0, frame);- if (!suppress_mouse) {- _ = try dispatchMouse(el, "mouseup", 0, frame);- }+ Frame.user_input.dispatchPointerRelease(frame, el, 0, 0, main, press_result.suppress_mouse, 1, .{}) catch |err| {+ lp.log.err(.app, "click release failed", .{ .err = err });+ return error.ActionFailed;+ };- _ = try dispatchPointer(el, "click", 0, 1, frame);+ Frame.user_input.dispatchClickAsPointer(frame, el, 0, 0, 1, .{}) catch |err| {+ lp.log.err(.app, "click click failed", .{ .err = err });+ return error.ActionFailed;+ };}pub fn hover(node: *DOMNode, frame: *Frame) !void {diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 5ac120f65..d70377daa 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -184,7 +184,7 @@ fn dispatchBoundaryEvent(frame: *Frame, target: *Element, comptime mouse_typ: []/// the pressed button and pointer position. `detail` is the click count (used for/// click/dblclick); 0 for events where it does not apply. Reports whether the/// event was cancelled via preventDefault().-fn dispatchMouseEventOn(frame: *Frame, target: *Element, comptime typ: []const u8, x: f64, y: f64, button: i32, detail: u32) !bool {+fn dispatchMouseEventOn(frame: *Frame, target: *Element, comptime typ: []const u8, x: f64, y: f64, button: i32, buttons: u16, detail: u32, modifiers: Modifiers) !bool {const event: *MouseEvent = try .initTrusted(comptime .wrap(typ), .{.bubbles = true,.cancelable = true,@@ -192,13 +192,109 @@ fn dispatchMouseEventOn(frame: *Frame, target: *Element, comptime typ: []const u.clientX = x,.clientY = y,.button = button,+ .buttons = buttons,.detail = detail,+ .ctrlKey = modifiers.ctrl,+ .shiftKey = modifiers.shift,+ .altKey = modifiers.alt,+ .metaKey = modifiers.meta,}, frame);return frame._event_manager.dispatchCancelable(target.asEventTarget(), event.asEvent());}+/// Dispatch a single trusted pointer event of the given type on `target`.+/// This engine only ever synthesizes mouse-sourced pointer input, so+/// pointerType is always "mouse" and pointerId/isPrimary are fixed.+fn dispatchPointerEventOn(frame: *Frame, target: *Element, comptime typ: []const u8, x: f64, y: f64, button: i32, buttons: u16, detail: u32, modifiers: Modifiers) !bool {+ const event: *PointerEvent = try .initTrusted(typ, .{+ .bubbles = true,+ .cancelable = true,+ .composed = true,+ .clientX = x,+ .clientY = y,+ .button = button,+ .buttons = buttons,+ .detail = detail,+ .pointerId = 1,+ .pointerType = "mouse",+ .isPrimary = true,+ .pressure = if (buttons != 0) 0.5 else 0.0,+ .ctrlKey = modifiers.ctrl,+ .shiftKey = modifiers.shift,+ .altKey = modifiers.alt,+ .metaKey = modifiers.meta,+ }, frame);+ return frame._event_manager.dispatchCancelable(target.asEventTarget(), event.asEvent());+}++/// MouseEvent/PointerEvent.buttons bitmask for a MouseEvent.button value.+/// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons+fn buttonsBitmask(button: i32) u16 {+ return switch (button) {+ mouse_button.main => 1,+ mouse_button.secondary => 2,+ mouse_button.auxiliary => 4,+ mouse_button.fourth => 8,+ mouse_button.fifth => 16,+ else => 0,+ };+}++pub const PressResult = struct {+ /// Whether pointerdown's preventDefault() suppressed the compatibility+ /// mousedown fired here, and (the caller should record this) the+ /// paired mouseup on release.+ suppress_mouse: bool,+ /// Whether mousedown's own preventDefault() suppressed just its focus+ /// default action. Only meaningful when `suppress_mouse` is false —+ /// mousedown itself never fires when it is true.+ suppress_focus: bool,+};++/// The trusted press half of a click gesture, shared by `actions.click`,+/// `WebDriver.click`, and the CDP `Input.dispatchMouseEvent` (`mousePressed`)+/// path: pointerdown, then — unless its preventDefault() suppresses the+/// compatibility mouse event — mousedown. `mouse_detail` is mousedown's+/// click count (0 where the caller doesn't track one; pointerdown itself is+/// always detail 0, per spec). Does not itself run the focus default+/// action: `focusForMouseDown` can fail, and the three callers don't agree+/// on what a failure there should mean for the click, so each runs it (or+/// not) against `PressResult` with its own error handling.+pub fn dispatchPointerPress(frame: *Frame, target: *Element, x: f64, y: f64, button: i32, mouse_detail: u32, modifiers: Modifiers) !PressResult {+ const buttons = buttonsBitmask(button);+ const suppress_mouse = try dispatchPointerEventOn(frame, target, "pointerdown", x, y, button, buttons, 0, modifiers);+ const suppress_focus = if (suppress_mouse)+ true+ else+ try dispatchMouseEventOn(frame, target, "mousedown", x, y, button, buttons, mouse_detail, modifiers);+ return .{ .suppress_mouse = suppress_mouse, .suppress_focus = suppress_focus };+}++/// The release half of a click gesture: pointerup, then mouseup (carrying+/// `mouse_detail`, its click count) unless the paired pointerdown suppressed+/// it (`suppress_mouse`, from `dispatchPointerPress`). Click still fires+/// even when suppressed — callers dispatch it separately via+/// `dispatchClickAsPointer`.+pub fn dispatchPointerRelease(frame: *Frame, target: *Element, x: f64, y: f64, button: i32, suppress_mouse: bool, mouse_detail: u32, modifiers: Modifiers) !void {+ _ = try dispatchPointerEventOn(frame, target, "pointerup", x, y, button, 0, 0, modifiers);+ if (!suppress_mouse) {+ _ = try dispatchMouseEventOn(frame, target, "mouseup", x, y, button, 0, mouse_detail, modifiers);+ }+}++/// The primary-button click, dispatched as a PointerEvent — matching how+/// the engine dispatches HTMLElement.click() and keyboard-triggered clicks.+pub fn dispatchClickAsPointer(frame: *Frame, target: *Element, x: f64, y: f64, detail: u32, modifiers: Modifiers) !void {+ _ = try dispatchPointerEventOn(frame, target, "click", x, y, mouse_button.main, 0, detail, modifiers);+}+pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32) !void {- const target = (try frame.window._document.elementFromPoint(x, y, frame)) orelse return;+ const target = (try frame.window._document.elementFromPoint(x, y, frame)) orelse {+ // No gesture started on this page; don't leave a prior gesture's+ // suppression outcome to be misread by the next mouseReleased.+ frame._page.input_mousedown_suppressed = false;+ return;+ };if (comptime lp.IS_DEBUG) {log.debug(.frame, "frame mouse press", .{.url = frame.url,@@ -209,8 +305,15 @@ pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32) !void {.type = frame._type,});}- const suppressed = try dispatchMouseEventOn(frame, target, "mousedown", x, y, button, 0);- if (!suppressed) {+ // CDP's mousedown has never carried a click count (params.clickCount is+ // only read on the release side); preserve that rather than fixing it+ // here. mousePressed/mouseReleased arrive as two independent CDP+ // messages, so the release half (below) can't observe this gesture's+ // pointerdown directly — stash the outcome on the page first, before+ // the fallible focus call, mirroring input_hover_target.+ const press = try dispatchPointerPress(frame, target, x, y, button, 0, .{});+ frame._page.input_mousedown_suppressed = press.suppress_mouse;+ if (!press.suppress_mouse and !press.suppress_focus) {try focusForMouseDown(frame, target);}}@@ -240,6 +343,12 @@ pub fn triggerMouseMove(frame: *Frame, x: f64, y: f64) !void {}pub fn triggerMouseRelease(frame: *Frame, x: f64, y: f64, button: i32, click_count: i32) !void {+ // Consumed unconditionally, before any early return below, so a+ // mouseReleased that misses every element (or a later error) can never+ // leave this gesture's outcome to be misread by the next one.+ const suppress_mouse = frame._page.input_mousedown_suppressed;+ frame._page.input_mousedown_suppressed = false;+const target = (try frame.window._document.elementFromPoint(x, y, frame)) orelse return;if (comptime lp.IS_DEBUG) {log.debug(.frame, "frame mouse release", .{@@ -254,19 +363,19 @@ pub fn triggerMouseRelease(frame: *Frame, x: f64, y: f64, button: i32, click_couconst detail: u32 = if (click_count > 0) @intCast(click_count) else 1;- _ = try dispatchMouseEventOn(frame, target, "mouseup", x, y, button, detail);+ try dispatchPointerRelease(frame, target, x, y, button, suppress_mouse, detail, .{});// After mouseup, the activation event depends on the button.switch (button) {mouse_button.main => {- _ = try dispatchMouseEventOn(frame, target, "click", x, y, button, detail);+ try dispatchClickAsPointer(frame, target, x, y, detail, .{});// A second click in quick succession also fires dblclick.if (click_count == 2) {- _ = try dispatchMouseEventOn(frame, target, "dblclick", x, y, button, detail);+ _ = try dispatchMouseEventOn(frame, target, "dblclick", x, y, button, 0, detail, .{});}},- mouse_button.auxiliary => _ = try dispatchMouseEventOn(frame, target, "auxclick", x, y, button, detail),- mouse_button.secondary => _ = try dispatchMouseEventOn(frame, target, "contextmenu", x, y, button, detail),+ mouse_button.auxiliary => _ = try dispatchMouseEventOn(frame, target, "auxclick", x, y, button, 0, detail, .{}),+ mouse_button.secondary => _ = try dispatchMouseEventOn(frame, target, "contextmenu", x, y, button, 0, detail, .{}),else => {},}}diff --git a/src/browser/tests/mcp_actions.html b/src/browser/tests/mcp_actions.htmlindex 01f38ce6e..ef9c91a9f 100644--- a/src/browser/tests/mcp_actions.html+++ b/src/browser/tests/mcp_actions.html@@ -34,7 +34,7 @@window.seq = [];const btn = document.getElementById('btn');for (const t of ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click']) {- btn.addEventListener(t, function(e) { window.seq.push([e.type, e.button, e.buttons, e.pointerType, e.isTrusted].join(':')); });+ btn.addEventListener(t, function(e) { window.seq.push([e.type, e.button, e.buttons, e.detail, e.pointerType, e.isTrusted].join(':')); });}// Regression coverage for pointerdown's preventDefault() suppressingdiff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zigindex da36d393e..980e65a53 100644--- a/src/browser/webapi/WebDriver.zig+++ b/src/browser/webapi/WebDriver.zig@@ -67,11 +67,31 @@ pub fn click(_: *const WebDriver, element: *Element, frame: *Frame) !void {}}- dispatchPointer(element, "pointerdown", 0, 1, frame);- _ = dispatchMouse(element, "mousedown", 0, 1, 1, frame);- dispatchPointer(element, "pointerup", 0, 0, frame);- _ = dispatchMouse(element, "mouseup", 0, 0, 1, frame);- _ = dispatchMouse(element, "click", 0, 0, 1, frame);+ // Shares its dispatch mechanics with actions.click and the CDP mouse+ // press/release path (Frame.user_input.dispatchPointer{Press,Release}),+ // which also gives this call its suppress/focus handling and its+ // PointerEvent click for the first time — previously this dispatched a+ // fixed five-event sequence unconditionally. Every dispatch here keeps+ // this function's pre-existing contract of never failing: a dispatch+ // error is a warning, not a rejection of the testdriver command.+ const main = Frame.user_input.mouse_button.main;+ const modifiers = frame._page.input_modifiers;+ const press = Frame.user_input.dispatchPointerPress(frame, element, 0, 0, main, 1, modifiers) catch |err| {+ log.warn(.app, "webdriver click press", .{ .err = err });+ return;+ };+ if (!press.suppress_mouse and !press.suppress_focus) {+ Frame.user_input.focusForMouseDown(frame, element) catch |err| {+ log.warn(.app, "webdriver click focus", .{ .err = err });+ };+ }+ Frame.user_input.dispatchPointerRelease(frame, element, 0, 0, main, press.suppress_mouse, 1, modifiers) catch |err| {+ log.warn(.app, "webdriver click release", .{ .err = err });+ return;+ };+ Frame.user_input.dispatchClickAsPointer(frame, element, 0, 0, 1, modifiers) catch |err| {+ log.warn(.app, "webdriver click click", .{ .err = err });+ };}const WebDriverCookie = struct {diff --git a/src/mcp/tools.zig b/src/mcp/tools.zigindex 048a01bd6..f965b02f4 100644--- a/src/mcp/tools.zig+++ b/src/mcp/tools.zig@@ -1292,8 +1292,8 @@ test "MCP - Actions: click, fill, scroll, hover, press, selectOption, setCheckedconst result = try ls.local.exec(\\ JSON.stringify(window.seq) === JSON.stringify([- \\ 'pointerdown:0:1:mouse:true', 'mousedown:0:1::true',- \\ 'pointerup:0:0:mouse:true', 'mouseup:0:0::true', 'click:0:0:mouse:true'+ \\ 'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:1::true',+ \\ 'pointerup:0:0:0:mouse:true', 'mouseup:0:0:1::true', 'click:0:0:1:mouse:true'\\ ]) &&\\ JSON.stringify(window.seqPrevented) === JSON.stringify(['pointerdown', 'pointerup', 'click']) &&\\ JSON.stringify(window.disabledEvents) === '[]' &&diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex 05a30355c..1ed759818 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -596,6 +596,102 @@ test "cdp.input: dispatchMouseEvent right button fires contextmenu, double-clicktry testing.expect(result.isTrue());}+// Regression for the shared click-dispatch refactor: mousePressed/+// mouseReleased now go through the same Frame.user_input.dispatchPointer*+// primitives as actions.click and WebDriver.click, so CDP (i.e. Puppeteer,+// Playwright) gets pointerdown/pointerup for the first time. #btn's+// listeners (mcp_actions.html) record the same five-event sequence the MCP+// click test asserts on the actions.click path, modulo one pre-existing,+// deliberately-untouched CDP quirk: mousedown's detail (click count) has+// never been threaded from clickCount here, unlike mouseup/click below.+test "cdp.input: dispatchMouseEvent mousePressed/mouseReleased fires the full pointer/mouse sequence" {+ 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('btn').getBoundingClientRect().x", null)).toF64();+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().y", null)).toF64();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mousePressed", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 1 },+ });+ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 1 },+ });++ const result = try ls.local.compileAndRun(+ \\JSON.stringify(window.seq) === JSON.stringify([+ \\ 'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:0::true',+ \\ 'pointerup:0:0:0:mouse:true', 'mouseup:0:0:1::true', 'click:0:0:1:mouse:true'+ \\])+ , null);+ try testing.expect(result.isTrue());+}++// Regression for the same refactor's page-level suppression state+// (Page.input_mousedown_suppressed): mousePressed and mouseReleased are two+// independent CDP messages, so a pointerdown cancelled on the press half+// must still suppress mouseup on the release half, with no state carried by+// the caller between the two calls.+test "cdp.input: a cancelled pointerdown suppresses mousedown and mouseup across the split mousePressed/mouseReleased calls" {+ 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('btnPreventDefault').getBoundingClientRect().x", null)).toF64();+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('btnPreventDefault').getBoundingClientRect().y", null)).toF64();++ try ctx.processMessage(.{+ .id = 1,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mousePressed", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 1 },+ });+ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 1 },+ });++ const result = try ls.local.compileAndRun(+ \\JSON.stringify(window.seqPrevented) === JSON.stringify(['pointerdown', 'pointerup', 'click'])+ , null);+ try testing.expect(result.isTrue());+}+test "cdp.input: dispatchKeyEvent Tab runs sequential focus navigation" {var ctx = try testing.context();defer ctx.deinit();