Skip to content

Lightpanda: the headless browser designed for AI and automation

fix(input): run the mousedown focus default action on a chorded press too

Ramiro_quai45e6cd47f951
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 21cdbcaf2..bf42159e4 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -338,7 +338,10 @@ 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) {
- _ = try dispatchMouseEventOn(frame, target, "mousedown", x, y, button, buttons, 0, .{});
+ const suppress_focus = try dispatchMouseEventOn(frame, target, "mousedown", x, y, button, buttons, 0, .{});
+ if (!suppress_focus) {
+ try focusForMouseDown(frame, target);
+ }
}
}
}
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index 6c6486401..635e46b6e 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -744,6 +744,61 @@ test "cdp.input: a mouse chord fires pointermove for the mid-gesture button chan
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();
+ defer ctx.deinit();
+
+ const bc = try ctx.loadBrowserContext(.{});
+ const page = try bc.session.createPage();
+ const frame = page.frame().?;
+ try frame.navigate("http://localhost:9582/src/browser/tests/mcp_actions.html", .{ .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.cancelAt = '" ++ cancel ++ "';" ++
+ \\document.getElementById('inp').focus();
+ \\document.getElementById('inp').addEventListener('pointerdown', e => {
+ \\ if (window.cancelAt === 'pointerdown') e.preventDefault();
+ \\});
+ \\window.chordMouseDowns = 0;
+ \\document.getElementById('keyTarget').addEventListener('mousedown', e => {
+ \\ window.chordMouseDowns++;
+ \\ if (window.cancelAt === 'mousedown') e.preventDefault();
+ \\});
+ , null);
+
+ const first_x = try (try ls.local.compileAndRun("document.getElementById('inp').getBoundingClientRect().x", null)).toF64();
+ const first_y = try (try ls.local.compileAndRun("document.getElementById('inp').getBoundingClientRect().y", null)).toF64();
+ const second_x = try (try ls.local.compileAndRun("document.getElementById('keyTarget').getBoundingClientRect().x", null)).toF64();
+ const second_y = try (try ls.local.compileAndRun("document.getElementById('keyTarget').getBoundingClientRect().y", null)).toF64();
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mousePressed", .x = first_x, .y = first_y, .button = "left" },
+ });
+ try ctx.processMessage(.{
+ .id = 2,
+ .method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mousePressed", .x = second_x, .y = second_y, .button = "right" },
+ });
+
+ // Check before any release/click activation can change focus.
+ const result = try ls.local.compileAndRun(
+ \\document.activeElement.id === (window.cancelAt === 'none' ? 'keyTarget' : 'inp') &&
+ \\window.chordMouseDowns === (window.cancelAt === 'pointerdown' ? 0 : 1)
+ , null);
+ try testing.expect(result.isTrue());
+ }
+}
+
test "cdp.input: dispatchKeyEvent Tab runs sequential focus navigation" {
var ctx = try testing.context();
defer ctx.deinit();