Lightpanda: the headless browser designed for AI and automation
fix(input): address review on the CDP/chord click dispatch
Ramiro_quai51a92dd0fd8f
diff --git a/src/browser/actions.zig b/src/browser/actions.zigindex 729c9eae5..423b91a0c 100644--- a/src/browser/actions.zig+++ b/src/browser/actions.zig@@ -66,7 +66,7 @@ pub fn click(node: *DOMNode, frame: *Frame) !void {return error.ActionFailed;};- Frame.user_input.dispatchClickAsPointer(frame, el, 0, 0, 1, .{}) catch |err| {+ Frame.user_input.dispatchClickAsPointer(frame, el, 0, 0, 1, 0, .{}) catch |err| {lp.log.err(.app, "click click failed", .{ .err = err });return error.ActionFailed;};diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex bf42159e4..73bbbca2e 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -284,11 +284,13 @@ pub fn dispatchPointerRelease(frame: *Frame, target: *Element, x: f64, y: f64, b/// 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);+/// `buttons` is the mask still held when the click fires (nonzero when the+/// primary button releases mid-chord, i.e. another button is still down).+pub fn dispatchClickAsPointer(frame: *Frame, target: *Element, x: f64, y: f64, detail: u32, buttons: u16, modifiers: Modifiers) !void {+ _ = try dispatchPointerEventOn(frame, target, "click", x, y, mouse_button.main, buttons, detail, modifiers);}-pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32) !void {+pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32, click_count: i32) !void {const target = (try frame.window._document.elementFromPoint(x, y, frame)) orelse {// No gesture started on this page; don't leave a prior gesture's// state to be misread by the next mousePressed/mouseReleased.@@ -315,17 +317,20 @@ pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32) !void {const other_already_held = frame._page.input_pressed_buttons & ~button_bit != 0;frame._page.input_pressed_buttons |= button_bit;const buttons = frame._page.input_pressed_buttons;+ // A caller that doesn't track click counts (or a CDP message that omits+ // clickCount) sends 0 — preserved as-is rather than forced to 1, since+ // Chrome and Firefox both fire mousedown with detail 0 in that case.+ const detail: u32 = if (click_count > 0) @intCast(click_count) else 0;if (!other_already_held) {// Starts the gesture: full pointerdown, then (unless cancelled)- // mousedown and its focus default action. triggerMousePress takes- // no click-count parameter, so mousedown always fires with detail- // 0; only the release side (below) carries a click count.- // mousePressed/mouseReleased arrive as two independent CDP- // messages, so the release half 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, .{});+ // mousedown (carrying click_count as its detail, matching Chrome and+ // Firefox) and its focus default action. mousePressed/mouseReleased+ // arrive as two independent CDP messages, so the release half 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, detail, .{});frame._page.input_mousedown_suppressed = press.suppress_mouse;if (!press.suppress_mouse and !press.suppress_focus) {try focusForMouseDown(frame, target);@@ -338,7 +343,7 @@ pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32) !void {// the gesture's own pointerdown was already cancelled._ = try dispatchPointerEventOn(frame, target, "pointermove", x, y, button, buttons, 0, .{});if (!frame._page.input_mousedown_suppressed) {- const suppress_focus = try dispatchMouseEventOn(frame, target, "mousedown", x, y, button, buttons, 0, .{});+ const suppress_focus = try dispatchMouseEventOn(frame, target, "mousedown", x, y, button, buttons, detail, .{});if (!suppress_focus) {try focusForMouseDown(frame, target);}@@ -412,7 +417,7 @@ pub fn triggerMouseRelease(frame: *Frame, x: f64, y: f64, button: i32, click_cou// After mouseup, the activation event depends on the button.switch (button) {mouse_button.main => {- try dispatchClickAsPointer(frame, target, x, y, detail, .{});+ try dispatchClickAsPointer(frame, target, x, y, detail, remaining, .{});// A second click in quick succession also fires dblclick.if (click_count == 2) {_ = try dispatchMouseEventOn(frame, target, "dblclick", x, y, button, remaining, detail, .{});diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zigindex 769d2ad0b..129ea097f 100644--- a/src/browser/webapi/WebDriver.zig+++ b/src/browser/webapi/WebDriver.zig@@ -84,7 +84,7 @@ pub fn click(_: *const WebDriver, element: *Element, frame: *Frame) !void {log.warn(.app, "webdriver click release", .{ .err = err });return;};- Frame.user_input.dispatchClickAsPointer(frame, element, 0, 0, 1, modifiers) catch |err| {+ Frame.user_input.dispatchClickAsPointer(frame, element, 0, 0, 1, 0, modifiers) catch |err| {log.warn(.app, "webdriver click click", .{ .err = err });};}diff --git a/src/server/bidi/input.zig b/src/server/bidi/input.zigindex b63cfef0a..6185a084a 100644--- a/src/server/bidi/input.zig+++ b/src/server/bidi/input.zig@@ -570,7 +570,7 @@ fn dispatch(bidi: *BiDi, frame: *Frame, source: *Source, action: *const Action)pointer.last_click_x = pointer.x;pointer.last_click_y = pointer.y;- try user_input.triggerMousePress(frame, pointer.x, pointer.y, button);+ try user_input.triggerMousePress(frame, pointer.x, pointer.y, button, pointer.click_count);},.pointer_up => |button| {const pointer = &source.pointer;diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex 635e46b6e..7503ae69e 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -125,7 +125,7 @@ fn dispatchMouseEvent(cmd: *CDP.Command) !void {};switch (params.type) {- .mousePressed => try Frame.user_input.triggerMousePress(frame, params.x, params.y, button),+ .mousePressed => try Frame.user_input.triggerMousePress(frame, params.x, params.y, button, params.clickCount),.mouseReleased => try Frame.user_input.triggerMouseRelease(frame, params.x, params.y, button, params.clickCount),.mouseMoved => try Frame.user_input.triggerMouseMove(frame, params.x, params.y),.mouseWheel => try Frame.user_input.triggerMouseWheel(frame, params.x, params.y, params.deltaX, params.deltaY),@@ -598,9 +598,8 @@ test "cdp.input: dispatchMouseEvent right button fires contextmenu, double-click// A CDP mousePressed/mouseReleased pair fires the full pointerdown/mousedown/// pointerup/mouseup/click sequence, matching actions.click's five-event-// sequence (asserted in the MCP click test) except that mousedown's detail-// (click count) is always 0 — triggerMousePress takes no click-count-// parameter, so only the release half's mouseup/click carries one.+// sequence (asserted in the MCP click test) — mousedown carries the press+// message's own clickCount as its detail, matching Chrome and Firefox.test "cdp.input: dispatchMouseEvent mousePressed/mouseReleased fires the full pointer/mouse sequence" {var ctx = try testing.context();defer ctx.deinit();@@ -637,13 +636,158 @@ test "cdp.input: dispatchMouseEvent mousePressed/mouseReleased fires the full poconst result = try ls.local.compileAndRun(\\JSON.stringify(window.seq) === JSON.stringify([- \\ 'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:0::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'\\]), null);try testing.expect(result.isTrue());}+// clickCount 0 (the struct default, same as omitting the field entirely) is+// preserved as mousedown's detail rather than forced to 1 — Chrome and+// Firefox both fire detail 0 for an omitted/zero clickCount, distinct from+// the detail-1 case a plain single click sends.+test "cdp.input: dispatchMouseEvent mousePressed's mousedown detail matches the message's clickCount" {+ const cases = .{+ .{ .click_count = 0, .expect_detail = 0 },+ .{ .click_count = 1, .expect_detail = 1 },+ .{ .click_count = 2, .expect_detail = 2 },+ };+ inline for (cases) |case| {+ 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 = case.click_count },+ });++ const expected = std.fmt.comptimePrint("'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:{d}::true'", .{case.expect_detail});+ const script = std.fmt.comptimePrint(+ "JSON.stringify(window.seq) === JSON.stringify([{s}])",+ .{expected},+ );+ const result = try ls.local.compileAndRun(script, null);+ try testing.expect(result.isTrue());+ }+}++// Independent check that clickCount is not press-only: a clickCount-2+// press+release pair must carry detail 2 on mousedown, mouseup, and click,+// and still fire dblclick from the release half.+test "cdp.input: clickCount 2 on press and release puts detail 2 on mousedown, mouseup, click and fires dblclick" {+ 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();++ _ = try ls.local.compileAndRun(+ \\window.sawDbl = false;+ \\document.getElementById('btn').addEventListener('dblclick', () => { window.sawDbl = true; });+ , null);++ 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 = 2 },+ });+ try ctx.processMessage(.{+ .id = 2,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 2 },+ });++ const result = try ls.local.compileAndRun(+ \\JSON.stringify(window.seq) === JSON.stringify([+ \\ 'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\ 'pointerup:0:0:0:mouse:true', 'mouseup:0:0:2::true', 'click:0:0:2:mouse:true'+ \\]) && window.sawDbl === true+ , null);+ try testing.expect(result.isTrue());+}++// The chorded-press branch has its own mousedown dispatch; clickCount must+// reach it too, not only the first-button dispatchPointerPress path.+test "cdp.input: a chorded mousedown carries the press message's clickCount as its detail" {+ 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 = "mousePressed", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 2 },+ });++ const result = try ls.local.compileAndRun(+ \\JSON.stringify(window.seq) === JSON.stringify([+ \\ 'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:1::true',+ \\ 'mousedown:2:3:2::true'+ \\])+ , null);+ try testing.expect(result.isTrue());+}+// 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.@@ -692,6 +836,12 @@ test "cdp.input: a cancelled pointerdown suppresses mousedown and mouseup across// (as pointermove), but pointerdown/pointerup fire only at the 0/nonzero// transitions, and a cancelled pointerdown's mouse-event suppression holds// for the whole gesture — https://www.w3.org/TR/pointerevents3/#chorded-button-interactions.+// This asserts only the pointer events this PR owns. The pointer/mouse+// events pin the same chord in real Chrome exactly; the *activation* order+// below (contextmenu on release rather than on the right press, and a+// trailing left click instead of auxclick) is a pre-existing, known+// deviation from Chrome's chorded release switch, not something this test+// claims is spec-correct.test "cdp.input: a mouse chord fires pointermove for the mid-gesture button change, not a second pointerdown/pointerup" {var ctx = try testing.context();defer ctx.deinit();@@ -744,6 +894,65 @@ test "cdp.input: a mouse chord fires pointermove for the mid-gesture button chantry testing.expect(result.isTrue());}+// A primary-button release mid-chord (another button still held) reports+// that held mask on its click, not 0 — the click is a PointerEvent and+// PointerEvent.buttons reflects buttons still down after this one lifts.+// Confirmed against real Chrome and Firefox: both report the held button on+// this click too (and, like this PR, fire it as a genuine `click`, not+// folding it into `auxclick`/`contextmenu`).+test "cdp.input: a primary click fired mid-chord carries the still-held buttons mask" {+ 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('btnChord').getBoundingClientRect().x", null)).toF64();+ const rect_y = try (try ls.local.compileAndRun("document.getElementById('btnChord').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 = "mousePressed", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 1 },+ });+ // Left releases first this time, while right is still held.+ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 1 },+ });+ try ctx.processMessage(.{+ .id = 4,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 1 },+ });++ const result = try ls.local.compileAndRun(+ \\JSON.stringify(window.seqChord) === JSON.stringify([+ \\ 'pointerdown:1', 'pointermove:3', 'pointermove:2', 'click:2', 'pointerup:0', 'contextmenu:0'+ \\])+ , null);+ try testing.expect(result.isTrue());+}+test "cdp.input: chorded mousedown focuses its target unless pointerdown or mousedown was cancelled" {inline for (.{ "none", "mousedown", "pointerdown" }) |cancel| {var ctx = try testing.context();