Lightpanda: the headless browser designed for AI and automation
input: make every release consume the latest press
Ramiro_quai9235eb55edd0
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 763e934e9..d7ba66151 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -259,23 +259,16 @@ pub const PointerButtons = struct {/// Whether the gesture's opening pointerdown suppressed the compat mouse/// events; held for the whole gesture so each split message reads it here.mousedown_suppressed: bool = false,- /// Where the gesture's pointerdown landed, until its first release fires- /// the gesture's one click.+ /// Where the latest press landed, until the release that follows it fires+ /// its click. A release with no press behind it has no target, and+ /// reports a click count of 0.down_target: ?*Element = null,- /// Whether another button was held when a button pressed, at any point- /// in the gesture.- chorded: bool = false,/// `g.buttons_down` is ignored: the held mask supplies it.pub fn press(self: *PointerButtons, frame: *Frame, target: *Element, g: Gesture) !void {const bit = buttonsBitmask(g.button);const starts_gesture = self.held & ~bit == 0;- if (starts_gesture) {- self.down_target = target;- self.chorded = false;- } else {- self.chorded = true;- }+ self.down_target = target;self.held |= bit;var pg = g;@@ -291,26 +284,26 @@ pub const PointerButtons = struct {pub fn release(self: *PointerButtons, frame: *Frame, target: *Element, g: Gesture) !?*Element {const click_target = if (self.down_target) |down| commonClickTarget(down, target) else null;const was_suppressed = self.mousedown_suppressed;- self.down_target = null;- const was_chorded = self.chorded;+ const was_pressed = self.down_target != null;self.releaseButton(g.button);var rg = g;rg.buttons_down = self.held;- // Chrome reports the release that ends a chorded gesture with a click- // count of 0, whatever the client sent.- if (self.held == 0 and was_chorded) rg.click_count = 0;+ // Chrome reports a release with no press since the previous one (the+ // later releases of a chord, or a lone release) with a click count of+ // 0, whatever the client sent.+ if (!was_pressed) rg.click_count = 0;try releaseSequence(frame, target, rg, was_suppressed, click_target);return click_target;}- /// The last held button releasing ends the gesture and clears its state.+ /// Every release consumes the latest press, even if it hit no element.+ /// The last held button releasing also clears the gesture's suppression.fn releaseButton(self: *PointerButtons, button: i32) void {+ self.down_target = null;self.held &= ~buttonsBitmask(button);if (self.held == 0) {self.mousedown_suppressed = false;- self.down_target = null;- self.chorded = false;}}diff --git a/src/server/bidi/input.zig b/src/server/bidi/input.zigindex 8a785c6a6..7dd4aa5f6 100644--- a/src/server/bidi/input.zig+++ b/src/server/bidi/input.zig@@ -781,6 +781,53 @@ test "bidi.input: click via element origin" {} }, .{ .id = 9 });}+test "bidi.input: later releases of a three-button chord report detail 0" {+ var ctx = try testing.context();+ defer ctx.deinit();+ const context_id = try ctx.createContext(.{ .url = "bidi/input.html" });++ try evaluate(&ctx, 1, context_id,+ \\window.releases = [];+ \\for (const type of ['mouseup', 'click', 'auxclick', 'dblclick']) {+ \\ document.addEventListener(type, e => releases.push(`${e.type}:${e.button}:${e.buttons}:${e.detail}`));+ \\}+ \\'ready'+ );+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "string", .value = "ready" } }, .{ .id = 1 });++ try ctx.processMessage(.{+ .id = 2,+ .method = "browsingContext.locateNodes",+ .params = .{ .context = context_id, .locator = .{ .type = "css", .value = "#btn" } },+ });+ try ctx.expectSentResult(.{ .nodes = .{.{ .sharedId = "1" }} }, .{ .id = 2 });++ try ctx.processMessage(.{+ .id = 3,+ .method = "input.performActions",+ .params = .{ .context = context_id, .actions = .{.{+ .type = "pointer",+ .id = "mouse",+ .actions = .{+ .{ .type = "pointerMove", .x = 0, .y = 0, .origin = .{ .type = "element", .element = .{ .sharedId = "1" } } },+ .{ .type = "pointerDown", .button = 0 },+ .{ .type = "pointerDown", .button = 2 },+ .{ .type = "pointerDown", .button = 1 },+ .{ .type = "pointerUp", .button = 2 },+ .{ .type = "pointerUp", .button = 1 },+ .{ .type = "pointerUp", .button = 0 },+ },+ }} },+ });+ try ctx.expectSentResult(null, .{ .id = 3 });++ try evaluate(&ctx, 4, context_id, "window.releases.join(' ')");+ try ctx.expectSentResult(.{ .type = "success", .result = .{+ .type = "string",+ .value = "mouseup:2:5:1 auxclick:2:5:1 mouseup:1:1:0 mouseup:0:0:0",+ } }, .{ .id = 4 });+}+test "bidi.input: keys and modifiers" {var ctx = try testing.context();defer ctx.deinit();diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex f9b6999b2..f118e5feb 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -1982,7 +1982,18 @@ test "cdp.input: re-navigating an iframe drops the pointer state on its elementstry testing.expectEqual(0, page.input_pointer.held);}-test "cdp.input: an explicit clickCount is still invalidated on the button that ends a chorded gesture" {+const MouseStep = struct {+ type: []const u8,+ button: []const u8,+ x: ?f64 = null,+ y: ?f64 = null,+};++/// Dispatches `steps` as Input.dispatchMouseEvent at #btn unless overridden,+/// clickCount 2 throughout,+/// and checks the whole `window.seq` log against `expected` (JS array elements).+/// `setup` runs first, e.g. to add listeners that log into `window.seq`.+fn expectMouseSequence(setup: []const u8, steps: []const MouseStep, comptime expected: []const u8) !void {var ctx = try testing.context();defer ctx.deinit();@@ -2002,84 +2013,135 @@ test "cdp.input: an explicit clickCount is still invalidated on the button thattry_catch.init(&ls.local);defer try_catch.deinit();+ _ = try ls.local.compileAndRun(setup, 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();- // press-left, press-right (chord), release-right (mid-chord), release-left- // (ends the gesture) — clickCount 2 throughout.- 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 = "mousePressed", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 2 },- });- try ctx.processMessage(.{- .id = 3,- .method = "Input.dispatchMouseEvent",- .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 2 },- });- try ctx.processMessage(.{- .id = 4,- .method = "Input.dispatchMouseEvent",- .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 2 },- });+ for (steps, 1..) |step, id| {+ try ctx.processMessage(.{+ .id = @as(i32, @intCast(id)),+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = step.type, .x = step.x orelse rect_x, .y = step.y orelse rect_y, .button = step.button, .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',- \\ 'mousedown:2:3:2::true', 'mouseup:2:1:2::true',- \\ 'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'- \\])- , null);+ const result = try ls.local.compileAndRun("JSON.stringify(window.seq) === JSON.stringify([" ++ expected ++ "])", null);try testing.expect(result.isTrue());}-test "cdp.input: an explicit clickCount is still invalidated on the secondary button that ends a chorded gesture" {- 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);+test "cdp.input: the release that ends a chord reports detail 0" {+ try expectMouseSequence("", &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right" },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\'mousedown:2:3:2::true', 'mouseup:2:1:2::true',+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+}- var ls: lp.js.Local.Scope = undefined;- frame.js.localScope(&ls);- defer ls.deinit();+test "cdp.input: the release that ends a chord reports detail 0 when the secondary button ends it" {+ try expectMouseSequence("", &.{+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mouseReleased", .button = "left" },+ .{ .type = "mouseReleased", .button = "right" },+ },+ \\'pointerdown:2:2:0:mouse:true', 'mousedown:2:2:2::true',+ \\'mousedown:0:3:2::true', 'mouseup:0:2:2::true', 'click:0:2:2:mouse:true',+ \\'pointerup:2:0:0:mouse:true', 'mouseup:2:0:0::true'+ );+}- var try_catch: lp.js.TryCatch = undefined;- try_catch.init(&ls.local);- defer try_catch.deinit();+test "cdp.input: every release after the first of a three-button chord reports detail 0" {+ try expectMouseSequence("", &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mousePressed", .button = "middle" },+ .{ .type = "mouseReleased", .button = "right" },+ .{ .type = "mouseReleased", .button = "middle" },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\'mousedown:2:3:2::true', 'mousedown:1:7:2::true',+ \\'mouseup:2:5:2::true', 'mouseup:1:1:0::true',+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+}- 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();+test "cdp.input: a release with no press behind it reports detail 0" {+ try expectMouseSequence("", &.{+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+}- // press-right, press-left (chord), release-left (mid-chord), release-right- // (ends the gesture) - clickCount 2 throughout.- const steps = [_]struct { type: []const u8, button: []const u8 }{- .{ .type = "mousePressed", .button = "right" },+test "cdp.input: pressing a button again mid-chord re-arms its auxclick" {+ try expectMouseSequence(+ \\document.getElementById('btn').addEventListener('auxclick', e => window.seq.push(`auxclick:${e.button}:${e.buttons}:${e.detail}`));+ , &.{.{ .type = "mousePressed", .button = "left" },- .{ .type = "mouseReleased", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right" },+ .{ .type = "mousePressed", .button = "right" },.{ .type = "mouseReleased", .button = "right" },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\'mousedown:2:3:2::true', 'mouseup:2:1:2::true', 'auxclick:2:1:2',+ \\'mousedown:2:3:2::true', 'mouseup:2:1:2::true', 'auxclick:2:1:2',+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );+}++test "cdp.input: a release that misses every element consumes the latest press" {+ const points = [_]struct { x: f64, y: f64 }{+ .{ .x = -1000, .y = -1000 },+ .{ .x = 700, .y = 500 },};- for (steps, 1..) |step, id| {- try ctx.processMessage(.{- .id = @as(i32, @intCast(id)),- .method = "Input.dispatchMouseEvent",- .params = .{ .type = step.type, .x = rect_x, .y = rect_y, .button = step.button, .clickCount = 2 },- });+ for (points) |point| {+ try expectMouseSequence(+ // Bound the fixture's root boxes so (700,500) is a genuine miss.+ \\document.body.lastElementChild.remove();+ \\for (const el of [document.documentElement, document.body]) {+ \\ el.style.width = '200px'; el.style.height = '100px';+ \\}+ \\if (document.elementFromPoint(700, 500) !== null) throw new Error('expected an empty hit-test point');+ \\document.getElementById('btn').addEventListener('dblclick', e => window.seq.push(`dblclick:${e.detail}`));+ , &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right", .x = point.x, .y = point.y },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'mousedown:0:1:2::true',+ \\'mousedown:2:3:2::true',+ \\'pointerup:0:0:0:mouse:true', 'mouseup:0:0:0::true'+ );}+}- const result = try ls.local.compileAndRun(- \\const tail = window.seq.slice(-2);- \\tail[0] === 'pointerup:2:0:0:mouse:true' && tail[1] === 'mouseup:2:0:0::true'- , null);- try testing.expect(result.isTrue());+test "cdp.input: a missed release preserves held buttons and mouse suppression" {+ try expectMouseSequence(+ \\{+ \\ const button = document.getElementById('btn');+ \\ button.addEventListener('pointerdown', e => e.preventDefault());+ \\ button.addEventListener('pointermove', e => window.seq.push(`pointermove:${e.button}:${e.buttons}:${e.detail}`));+ \\ button.addEventListener('auxclick', e => window.seq.push(`auxclick:${e.button}:${e.buttons}:${e.detail}`));+ \\}+ , &.{+ .{ .type = "mousePressed", .button = "left" },+ .{ .type = "mousePressed", .button = "right" },+ .{ .type = "mouseReleased", .button = "right", .x = -1000, .y = -1000 },+ .{ .type = "mousePressed", .button = "middle" },+ .{ .type = "mouseReleased", .button = "middle" },+ .{ .type = "mouseReleased", .button = "left" },+ },+ \\'pointerdown:0:1:0:mouse:true', 'pointermove:2:3:0',+ \\'pointermove:1:5:0', 'pointermove:1:1:0', 'auxclick:1:1:2',+ \\'pointerup:0:0:0:mouse:true'+ );}