Lightpanda: the headless browser designed for AI and automation
fix(input): track a chorded button mask on the CDP/BiDi mouse path
Ramiro_quaie0b919435070
diff --git a/src/browser/Page.zig b/src/browser/Page.zigindex accd34861..7fdb18abd 100644--- a/src/browser/Page.zig+++ b/src/browser/Page.zig@@ -186,11 +186,19 @@ 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.+// The aggregate mask of mouse buttons currently held down, tracked across+// individual mousePressed/mouseReleased (CDP) or equivalent (BiDi) messages+// so a chorded press (a second button pressed before the first is released)+// can be told apart from the start of a new gesture.+input_pressed_buttons: u16 = 0,++// Whether the pointerdown that started the current gesture (the 0-to-nonzero+// transition of input_pressed_buttons) suppressed the compatibility+// mousedown/mouseup (preventDefault() was called). Set once at the start of+// the gesture and held for its whole duration, including any chorded+// button presses/releases in between — CDP's mousePressed/mouseReleased+// arrive as independent Input.dispatchMouseEvent messages, so each 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 contextsdiff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex b23bd677b..21cdbcaf2 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -291,7 +291,8 @@ pub fn dispatchClickAsPointer(frame: *Frame, target: *Element, x: f64, y: f64, dpub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: 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- // suppression outcome to be misread by the next mouseReleased.+ // state to be misread by the next mousePressed/mouseReleased.+ frame._page.input_pressed_buttons = 0;frame._page.input_mousedown_suppressed = false;return;};@@ -305,16 +306,40 @@ pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32) !void {.type = frame._type,});}- // 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, .{});- frame._page.input_mousedown_suppressed = press.suppress_mouse;- if (!press.suppress_mouse and !press.suppress_focus) {- try focusForMouseDown(frame, target);++ const button_bit = buttonsBitmask(button);+ // Whether some *other* button was already down — the same button+ // pressed again without a release in between (some callers do this+ // deliberately to simulate two independent, unrelated presses) starts a+ // fresh gesture rather than continuing a chord.+ 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;++ 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, .{});+ frame._page.input_mousedown_suppressed = press.suppress_mouse;+ if (!press.suppress_mouse and !press.suppress_focus) {+ try focusForMouseDown(frame, target);+ }+ } else {+ // A chorded press: another button going down while one is already+ // held is a buttons-mask change on the same pointer, not a second+ // pointerdown — https://www.w3.org/TR/pointerevents3/#chorded-button-interactions.+ // The compatibility mousedown for this button still fires unless+ // the gesture's own pointerdown was already cancelled.+ _ = try dispatchPointerEventOn(frame, target, "pointermove", x, y, button, buttons, 0, .{});+ if (!frame._page.input_mousedown_suppressed) {+ _ = try dispatchMouseEventOn(frame, target, "mousedown", x, y, button, buttons, 0, .{});+ }}}@@ -345,9 +370,14 @@ 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;+ // leave this gesture's state to be misread by the next one.+ const was_suppressed = frame._page.input_mousedown_suppressed;+ const remaining = frame._page.input_pressed_buttons & ~buttonsBitmask(button);+ frame._page.input_pressed_buttons = remaining;+ const ends_gesture = remaining == 0;+ if (ends_gesture) {+ frame._page.input_mousedown_suppressed = false;+ }const target = (try frame.window._document.elementFromPoint(x, y, frame)) orelse return;if (comptime lp.IS_DEBUG) {@@ -363,7 +393,18 @@ 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 dispatchPointerRelease(frame, target, x, y, button, suppress_mouse, detail, .{});+ if (ends_gesture) {+ // The last held button releasing ends the gesture: full pointerup,+ // then (unless suppressed) mouseup.+ try dispatchPointerRelease(frame, target, x, y, button, was_suppressed, detail, .{});+ } else {+ // A chorded release: another button remains held, so this is a+ // buttons-mask change on the same pointer, not pointerup.+ _ = try dispatchPointerEventOn(frame, target, "pointermove", x, y, button, remaining, 0, .{});+ if (!was_suppressed) {+ _ = try dispatchMouseEventOn(frame, target, "mouseup", x, y, button, remaining, detail, .{});+ }+ }// After mouseup, the activation event depends on the button.switch (button) {@@ -371,11 +412,11 @@ pub fn triggerMouseRelease(frame: *Frame, x: f64, y: f64, button: i32, click_coutry 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, 0, detail, .{});+ _ = try dispatchMouseEventOn(frame, target, "dblclick", x, y, button, remaining, 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, .{}),+ mouse_button.auxiliary => _ = try dispatchMouseEventOn(frame, target, "auxclick", x, y, button, remaining, detail, .{}),+ mouse_button.secondary => _ = try dispatchMouseEventOn(frame, target, "contextmenu", x, y, button, remaining, detail, .{}),else => {},}}diff --git a/src/browser/tests/mcp_actions.html b/src/browser/tests/mcp_actions.htmlindex ef9c91a9f..c6e409ce5 100644--- a/src/browser/tests/mcp_actions.html+++ b/src/browser/tests/mcp_actions.html@@ -27,6 +27,7 @@<input id="chk" type="checkbox"><input id="rad" type="radio" name="group1"><button id="btnPreventDefault">Prevent Default</button>+ <button id="btnChord">Chord</button><button id="btnDisabled" disabled>Disabled</button><div id="focusTarget" tabindex="0" onclick="window.focusTargetFocused = document.activeElement === this;">Focus me</div><div id="plain" onclick="window.plainBlurred = document.activeElement === document.body;">Not focusable</div>@@ -46,6 +47,18 @@btnPreventDefault.addEventListener(t, function(e) { window.seqPrevented.push(e.type); });}+ // Regression coverage for a multi-button chord: the left button's+ // pointerdown is cancelled, then right is pressed while left is+ // still held. Only the left press/release should be full+ // pointerdown/pointerup; the chorded right press/release in between+ // must report as pointermove, not a second pointerdown/pointerup.+ window.seqChord = [];+ const btnChord = document.getElementById('btnChord');+ btnChord.addEventListener('pointerdown', function(e) { if (e.button === 0) e.preventDefault(); });+ for (const t of ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'pointermove', 'click', 'auxclick', 'contextmenu']) {+ btnChord.addEventListener(t, function(e) { window.seqChord.push([e.type, e.buttons].join(':')); });+ }+window.disabledEvents = [];const btnDisabled = document.getElementById('btnDisabled');for (const t of ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click']) {diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex ee0fd2994..6c6486401 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -687,6 +687,63 @@ test "cdp.input: a cancelled pointerdown suppresses mousedown and mouseup acrosstry testing.expect(result.isTrue());}+// A second button pressed while the first is still held is a chord on one+// pointer, not two independent gestures: the aggregate buttons mask changes+// (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.+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();++ 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 },+ });+ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 1 },+ });+ try ctx.processMessage(.{+ .id = 4,+ .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.seqChord) === JSON.stringify([+ \\ 'pointerdown:1', 'pointermove:3', 'pointermove:1', 'contextmenu:1', 'pointerup:0', 'click:0'+ \\])+ , null);+ try testing.expect(result.isTrue());+}+test "cdp.input: dispatchKeyEvent Tab runs sequential focus navigation" {var ctx = try testing.context();defer ctx.deinit();