Skip to content

Lightpanda: the headless browser designed for AI and automation

click: match Chrome's activation events and share gesture state

Adrià Arrufate7cd6a1e70d2
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 6750bb56b..8e4f1c089 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -56,9 +56,6 @@ const HoverContext = struct {
y: f64 = 0,
buttons: u16 = 0,
modifiers: Modifiers = .{},
- // WebDriver's pointer source also fires the pointerover/out/enter/leave
- // twins; the CDP mouse path only synthesizes mouse events.
- with_pointer: bool = false,
};
// Update the element being hovered. The page always tracks the currently
@@ -76,13 +73,7 @@ pub fn updateHoverTarget(frame: *Frame, to: ?*Element, ctx: HoverContext) void {
const pivot: ?*Node = blk: {
const a = from orelse break :blk null;
const b = to orelse break :blk null;
- var current: ?*Node = a.asNode();
- while (current) |node| : (current = node.parentNode()) {
- if (node.contains(b.asNode())) {
- break :blk node;
- }
- }
- break :blk null;
+ break :blk commonAncestor(a.asNode(), b.asNode());
};
if (from) |old| {
@@ -134,30 +125,30 @@ fn dispatchBoundaryEvent(frame: *Frame, target: *Element, comptime mouse_typ: []
const modifiers = ctx.modifiers;
const related_target = if (related) |r| r.asEventTarget() else null;
- if (ctx.with_pointer) {
- const pointer_event = PointerEvent.initTrusted(pointer_typ, .{
- .bubbles = bubbling,
- .cancelable = bubbling,
- .composed = bubbling,
- .clientX = ctx.x,
- .clientY = ctx.y,
- .buttons = ctx.buttons,
- .pointerId = 1,
- .pointerType = "mouse",
- .isPrimary = true,
- .relatedTarget = related_target,
- .ctrlKey = modifiers.ctrl,
- .shiftKey = modifiers.shift,
- .altKey = modifiers.alt,
- .metaKey = modifiers.meta,
- }, frame) catch |err| {
- log.debug(.frame, "boundary pointer event", .{ .err = err, .type = pointer_typ });
- return;
- };
- frame._event_manager.dispatch(target.asEventTarget(), pointer_event.asEvent()) catch |err| {
- log.debug(.frame, "boundary pointer dispatch", .{ .err = err, .type = pointer_typ });
- };
- }
+ const pointer_event = PointerEvent.initTrusted(pointer_typ, .{
+ .bubbles = bubbling,
+ .cancelable = bubbling,
+ .composed = bubbling,
+ .clientX = ctx.x,
+ .clientY = ctx.y,
+ // No button changed state: https://www.w3.org/TR/pointerevents3/#the-button-property
+ .button = -1,
+ .buttons = ctx.buttons,
+ .pointerId = 1,
+ .pointerType = "mouse",
+ .isPrimary = true,
+ .relatedTarget = related_target,
+ .ctrlKey = modifiers.ctrl,
+ .shiftKey = modifiers.shift,
+ .altKey = modifiers.alt,
+ .metaKey = modifiers.meta,
+ }, frame) catch |err| {
+ log.debug(.frame, "boundary pointer event", .{ .err = err, .type = pointer_typ });
+ return;
+ };
+ frame._event_manager.dispatch(target.asEventTarget(), pointer_event.asEvent()) catch |err| {
+ log.debug(.frame, "boundary pointer dispatch", .{ .err = err, .type = pointer_typ });
+ };
const mouse_event = MouseEvent.initTrusted(comptime .wrap(mouse_typ), .{
.bubbles = bubbling,
@@ -268,56 +259,92 @@ 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.
+ /// Where the gesture's pointerdown landed, until its first release fires
+ /// the gesture's one click.
down_target: ?*Element = null,
- /// `starts_gesture` is false for a chorded press (another button held).
- pub fn press(self: *PointerButtons, button: i32, target: *Element) struct { starts_gesture: bool, held: u16 } {
- const bit = buttonsBitmask(button);
+ /// `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.held |= bit;
- return .{ .starts_gesture = starts_gesture, .held = self.held };
+
+ var pg = g;
+ pg.buttons_down = self.held;
+ const suppressed = try pressSequence(frame, target, pg, self.mousedown_suppressed);
+ if (starts_gesture) {
+ self.mousedown_suppressed = suppressed;
+ }
}
- /// The last held button releasing ends the gesture and clears its state;
- /// `was_suppressed` is the suppression flag for this gesture.
- pub fn release(self: *PointerButtons, button: i32) struct { held: u16, was_suppressed: bool } {
+ /// Returns the element the click fired at, if any. `g.buttons_down` is
+ /// ignored: the held mask supplies it.
+ 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;
+ self.releaseButton(g.button);
+
+ var rg = g;
+ rg.buttons_down = self.held;
+ try releaseSequence(frame, target, rg, was_suppressed, click_target);
+ return click_target;
+ }
+
+ /// The last held button releasing ends the gesture and clears its state.
+ fn releaseButton(self: *PointerButtons, button: i32) void {
self.held &= ~buttonsBitmask(button);
if (self.held == 0) {
self.mousedown_suppressed = false;
self.down_target = null;
}
- return .{ .held = self.held, .was_suppressed = was_suppressed };
}
- /// Discards an in-progress gesture (a press/release that hit no element).
+ /// Discards an in-progress gesture (a press that hit no element).
pub fn reset(self: *PointerButtons) void {
self.* = .{};
}
};
+fn commonAncestor(a: *Node, b: *Node) ?*Node {
+ var current: ?*Node = a;
+ while (current) |node| : (current = node.parentNode()) {
+ if (node.contains(b)) {
+ return node;
+ }
+ }
+ return null;
+}
+
/// A click whose mousedown and mouseup landed on different elements fires at
/// their nearest common inclusive ancestor element.
-pub fn commonClickTarget(down: *Element, up: *Element) *Element {
+fn commonClickTarget(down: *Element, up: *Element) *Element {
if (down == up) {
return up;
}
- var current: ?*Node = down.asNode();
- while (current) |node| : (current = node.parentNode()) {
- if (node.contains(up.asNode())) {
- return node.is(Element) orelse break;
- }
+ const ancestor = commonAncestor(down.asNode(), up.asNode()) orelse return up;
+ return ancestor.is(Element) orelse up;
+}
+
+pub fn moveSequence(frame: *Frame, target: *Element, g: Gesture) !void {
+ if (g.emit_mouse_compat) {
+ updateHoverTarget(frame, target, .{ .x = g.x, .y = g.y, .buttons = g.buttons_down, .modifiers = g.modifiers });
+ }
+ // A move changes no button: https://www.w3.org/TR/pointerevents3/#the-button-property
+ var pg = g;
+ pg.button = -1;
+ _ = try emitPointer(frame, target, "pointermove", pg, 0);
+ if (g.emit_mouse_compat) {
+ _ = try emitMouse(frame, target, "mousemove", g, 0);
}
- return up;
}
/// Returns whether the gesture's compat mouse events are suppressed: by the
/// opening pointerdown, or for a chorded press, `chord_suppressed` carried over.
-pub fn pressSequence(frame: *Frame, target: *Element, g: Gesture, chord_suppressed: bool) !bool {
+fn pressSequence(frame: *Frame, target: *Element, g: Gesture, chord_suppressed: bool) !bool {
if (target.isDisabled()) {
return true;
}
@@ -331,46 +358,46 @@ pub fn pressSequence(frame: *Frame, target: *Element, g: Gesture, chord_suppress
_ = try emitPointer(frame, target, "pointermove", g, 0);
break :blk chord_suppressed;
};
- if (suppressed or !g.emit_mouse_compat) {
+ if (!g.emit_mouse_compat) {
return suppressed;
}
- if (!try emitMouse(frame, target, "mousedown", g, g.click_count)) {
+ if (!suppressed and !try emitMouse(frame, target, "mousedown", g, g.click_count)) {
focusForMouseDown(frame, target) catch |err| log.debug(.app, "mousedown focus", .{ .err = err });
}
- return false;
+ // Chrome on Linux and macOS fires contextmenu on press, even when the
+ // pointerdown was cancelled.
+ if (g.button == mouse_button.secondary) {
+ _ = try emitPointer(frame, target, "contextmenu", g, 0);
+ }
+ return suppressed;
}
-pub fn releaseSequence(frame: *Frame, up_target: *Element, g: Gesture, suppressed: bool, click_target: *Element) !void {
+/// A null `click_target` releases without a click, as for a later release in
+/// a chord.
+fn releaseSequence(frame: *Frame, up_target: *Element, g: Gesture, suppressed: bool, click_target: ?*Element) !void {
if (up_target.isDisabled()) {
return;
}
- const detail = @max(g.click_count, 1);
_ = try emitPointer(frame, up_target, if (g.buttons_down == 0) "pointerup" else "pointermove", g, 0);
if (g.emit_mouse_compat and !suppressed) {
- _ = try emitMouse(frame, up_target, "mouseup", g, detail);
+ _ = try emitMouse(frame, up_target, "mouseup", g, g.click_count);
}
- if (!g.emit_mouse_compat or (click_target != up_target and click_target.isDisabled())) {
+ // clickCount 0 releases without a click, as in Chrome.
+ const click_el = click_target orelse return;
+ if (!g.emit_mouse_compat or g.click_count == 0 or (click_el != up_target and click_el.isDisabled())) {
return;
}
- switch (g.button) {
- mouse_button.main => {
- // click is a PointerEvent, matching HTMLElement.click().
- _ = try emitPointer(frame, click_target, "click", g, detail);
- if (detail % 2 == 0) {
- _ = try emitMouse(frame, click_target, "dblclick", g, detail);
- }
- },
- else => {
- if (g.button == mouse_button.secondary) {
- _ = try emitMouse(frame, click_target, "contextmenu", g, detail);
- }
- // Every non-primary button gets an auxclick, the right one included.
- _ = try emitMouse(frame, click_target, "auxclick", g, detail);
- },
+ if (g.button == mouse_button.main) {
+ _ = try emitPointer(frame, click_el, "click", g, g.click_count);
+ if (g.click_count % 2 == 0) {
+ _ = try emitMouse(frame, click_el, "dblclick", g, g.click_count);
+ }
+ } else {
+ _ = try emitPointer(frame, click_el, "auxclick", g, g.click_count);
}
}
@@ -381,12 +408,12 @@ pub fn triggerClick(frame: *Frame, target: *Element, modifiers: Modifiers) !void
if (target.isDisabled()) {
return;
}
- updateHoverTarget(frame, target, .{ .modifiers = modifiers, .with_pointer = true });
+ try moveSequence(frame, target, .{ .modifiers = modifiers });
- var g: Gesture = .{ .buttons_down = 1, .click_count = 1, .modifiers = modifiers };
- const suppressed = try pressSequence(frame, target, g, false);
- g.buttons_down = 0;
- try releaseSequence(frame, target, g, suppressed, target);
+ const g: Gesture = .{ .click_count = 1, .modifiers = modifiers };
+ var pointer: PointerButtons = .{};
+ try pointer.press(frame, target, g);
+ _ = try pointer.release(frame, target, g);
}
pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32, click_count: i32) !void {
@@ -406,21 +433,14 @@ pub fn triggerMousePress(frame: *Frame, x: f64, y: f64, button: i32, click_count
});
}
- const pointer = &frame.page.input_pointer;
- const gesture = pointer.press(button, target);
- const g: Gesture = .{
+ try frame.page.input_pointer.press(frame, target, .{
.button = button,
- .buttons_down = gesture.held,
// clickCount 0 (omitted) stays 0, not forced to 1: Chrome and Firefox
// both fire mousedown with detail 0 in that case.
.click_count = if (click_count > 0) @intCast(click_count) else 0,
.x = x,
.y = y,
- };
- const suppressed = try pressSequence(frame, target, g, pointer.mousedown_suppressed);
- if (gesture.starts_gesture) {
- pointer.mousedown_suppressed = suppressed;
- }
+ });
}
pub fn triggerMouseMove(frame: *Frame, x: f64, y: f64) !void {
@@ -435,25 +455,21 @@ pub fn triggerMouseMove(frame: *Frame, x: f64, y: f64) !void {
});
}
- updateHoverTarget(frame, target, .{ .x = x, .y = y });
-
- const move_event: *MouseEvent = try .initTrusted(comptime .wrap("mousemove"), .{
- .bubbles = true,
- .cancelable = true,
- .composed = true,
- .clientX = x,
- .clientY = y,
- }, frame);
- try frame._event_manager.dispatch(target.asEventTarget(), move_event.asEvent());
+ try moveSequence(frame, target, .{ .buttons_down = frame.page.input_pointer.held, .x = x, .y = y });
}
pub fn triggerMouseRelease(frame: *Frame, x: f64, y: f64, button: i32, click_count: i32) !void {
- // Consume the state before any early return, so a release that misses
- // every element can't leave it for the next message to misread.
- const down_target = frame.page.input_pointer.down_target;
- const gesture = frame.page.input_pointer.release(button);
-
- const target = (try frame.window._document.elementFromPoint(x, y, frame)) orelse return;
+ const pointer = &frame.page.input_pointer;
+ // A release that misses every element still lets go of the button, so
+ // the next message doesn't read it as held.
+ const hit = frame.window._document.elementFromPoint(x, y, frame) catch |err| {
+ pointer.releaseButton(button);
+ return err;
+ };
+ const target = hit orelse {
+ pointer.releaseButton(button);
+ return;
+ };
if (comptime lp.IS_DEBUG) {
log.debug(.frame, "frame mouse release", .{
.url = frame.url,
@@ -465,15 +481,12 @@ pub fn triggerMouseRelease(frame: *Frame, x: f64, y: f64, button: i32, click_cou
});
}
- const click_target = if (down_target) |dt| commonClickTarget(dt, target) else target;
- const g: Gesture = .{
+ _ = try pointer.release(frame, target, .{
.button = button,
- .buttons_down = gesture.held,
.click_count = if (click_count > 0) @intCast(click_count) else 0,
.x = x,
.y = y,
- };
- try releaseSequence(frame, target, g, gesture.was_suppressed, click_target);
+ });
}
pub fn triggerMouseWheel(frame: *Frame, x: f64, y: f64, delta_x: f64, delta_y: f64) !void {
diff --git a/src/browser/tests/webdriver/input.html b/src/browser/tests/webdriver/input.html
index 06e5d0fd4..c9650a1ac 100644
--- a/src/browser/tests/webdriver/input.html
+++ b/src/browser/tests/webdriver/input.html
@@ -18,8 +18,9 @@
<script>
window.events = [];
window.over = [];
- document.addEventListener('mouseover', (e) => window.over.push(e.buttons + '@' + e.target.id));
- document.addEventListener('pointerover', (e) => window.over.push('p' + e.buttons + '@' + e.target.id));
+ for (const type of ['pointerover', 'mouseover']) {
+ document.addEventListener(type, (e) => window.over.push(`${type}:${e.button}:${e.buttons}@${e.target.id}`));
+ }
for (const type of ['click', 'input', 'change', 'keydown', 'focus', 'blur']) {
document.addEventListener(type, (e) => {
const key = type === 'keydown' ? ':' + (e.shiftKey ? 'S-' : '') + e.key : '';
diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zig
index cf09f76dc..a9cc0b76d 100644
--- a/src/browser/webapi/WebDriver.zig
+++ b/src/browser/webapi/WebDriver.zig
@@ -221,12 +221,7 @@ fn performPointerSource(source: js.Object, frame: *Frame) !void {
// The element the pointer is currently over, set by the last pointerMove
// whose origin resolved to an element.
var target: ?*Element = null;
- var pressed = false;
- // The buttons bitmask of the currently depressed button, carried on move
- // and boundary events while dragging.
- var pressed_mask: u16 = 0;
- var down_target: ?*Element = null;
- var pointer_suppressed: bool = false;
+ var pointer: Frame.user_input.PointerButtons = .{};
var click_count: u32 = 0;
var last_click_button: i32 = 0;
var last_click_target: ?*Element = null;
@@ -252,65 +247,44 @@ fn performPointerSource(source: js.Object, frame: *Frame) !void {
}
}
const el = target orelse continue;
- const g: Frame.user_input.Gesture = .{
- .buttons_down = pressed_mask,
+ Frame.user_input.moveSequence(frame, el, .{
+ .buttons_down = pointer.held,
.modifiers = frame.page.input_modifiers,
- };
- if (!is_touch) {
- Frame.user_input.updateHoverTarget(frame, el, .{
- .buttons = pressed_mask,
- .modifiers = frame.page.input_modifiers,
- .with_pointer = true,
- });
- }
- _ = Frame.user_input.emitPointer(frame, el, "pointermove", g, 0) catch {};
- if (!is_touch) {
- _ = Frame.user_input.emitMouse(frame, el, "mousemove", g, 0) catch {};
- } else if (pressed) {
+ .emit_mouse_compat = !is_touch,
+ }) catch {};
+ if (is_touch and pointer.held != 0) {
dispatchTouch(el, "touchmove", frame);
}
} else if (action_type.eql(comptime .wrap("pointerDown"))) {
const el = target orelse continue;
const button = readI32(action, "button", 0);
- pressed = true;
- pressed_mask = Frame.user_input.buttonsBitmask(button);
- down_target = el;
if (last_click_target == el and last_click_button == button) {
click_count += 1;
} else {
click_count = 1;
}
- const g: Frame.user_input.Gesture = .{
+ pointer.press(frame, el, .{
.button = button,
- .buttons_down = pressed_mask,
.click_count = click_count,
.modifiers = frame.page.input_modifiers,
.emit_mouse_compat = !is_touch,
- };
- pointer_suppressed = Frame.user_input.pressSequence(frame, el, g, pointer_suppressed) catch false;
+ }) catch {};
if (is_touch) {
dispatchTouch(el, "touchstart", frame);
}
} else if (action_type.eql(comptime .wrap("pointerUp"))) {
const el = target orelse continue;
const button = readI32(action, "button", 0);
- pressed = false;
- pressed_mask = 0;
- const click_target = Frame.user_input.commonClickTarget(down_target orelse el, el);
last_click_button = button;
- last_click_target = click_target;
- const g: Frame.user_input.Gesture = .{
+ last_click_target = pointer.release(frame, el, .{
.button = button,
.click_count = click_count,
.modifiers = frame.page.input_modifiers,
.emit_mouse_compat = !is_touch,
- };
- Frame.user_input.releaseSequence(frame, el, g, pointer_suppressed, click_target) catch {};
+ }) catch null;
if (is_touch) {
dispatchTouch(el, "touchend", frame);
}
- down_target = null;
- pointer_suppressed = false;
}
// "pause" carries timing only and is ignored. ("pointerCancel" is not
// emitted by the testdriver Actions builder.)
diff --git a/src/server/Server.zig b/src/server/Server.zig
index c1b7d4390..daf0f00de 100644
--- a/src/server/Server.zig
+++ b/src/server/Server.zig
@@ -2237,7 +2237,7 @@ test "server: HTTP element input" {
try testing.expectEqual("{\"value\":null}", responseBody(try elementPost(&c, &session_id, btn, "/click", "{}")));
try testing.expectEqual("{\"value\":\"focus@btn click@btn\"}", try executeSync(&c, &session_id, take_events, "[]"));
// The pointer arrives before the press: no button is held yet.
- try testing.expectEqual("{\"value\":\"p0@btn 0@btn\"}", try executeSync(&c, &session_id, "return window.over.join(' ');", "[]"));
+ try testing.expectEqual("{\"value\":\"pointerover:-1:0@btn mouseover:0:0@btn\"}", try executeSync(&c, &session_id, "return window.over.join(' ');", "[]"));
{
const gone = try findElement(&c, &session_id, "css selector", "#gone");
diff --git a/src/server/bidi/input.zig b/src/server/bidi/input.zig
index da63d1332..60c5d67f4 100644
--- a/src/server/bidi/input.zig
+++ b/src/server/bidi/input.zig
@@ -207,6 +207,7 @@ const Source = struct {
last_click_ms: u64 = 0,
last_click_x: f64 = 0,
last_click_y: f64 = 0,
+ last_click_button: u8 = 0,
click_count: i32 = 0,
};
@@ -579,10 +580,13 @@ fn dispatch(bidi: *BiDi, frame: *Frame, source: *Source, action: *const Action)
}
try pointer.pressed.append(allocator, button);
- // A press near the last click, soon enough after it, counts up
+ // A press of the same button near the last click, soon enough
+ // after it, counts up
const now = lp.datetime.milliTimestamp(.boot);
const near = @abs(pointer.x - pointer.last_click_x) <= 2 and @abs(pointer.y - pointer.last_click_y) <= 2;
- pointer.click_count = if (near and now - pointer.last_click_ms <= 500) pointer.click_count + 1 else 1;
+ const repeat = near and button == pointer.last_click_button and now - pointer.last_click_ms <= 500;
+ pointer.click_count = if (repeat) pointer.click_count + 1 else 1;
+ pointer.last_click_button = button;
pointer.last_click_ms = now;
pointer.last_click_x = pointer.x;
pointer.last_click_y = pointer.y;
@@ -988,6 +992,26 @@ test "bidi.input: click via element origin" {
.type = "string",
.value = "mousemove@btn mousedown:b2@btn mouseup:b2@btn",
} }, .{ .id = 7 });
+
+ // A left click right after the right one starts a new count: no dblclick.
+ try ctx.processMessage(.{
+ .id = 8,
+ .method = "input.performActions",
+ .params = .{ .context = context_id, .actions = .{.{
+ .type = "pointer",
+ .id = "mouse",
+ .actions = .{
+ .{ .type = "pointerDown", .button = 0 },
+ .{ .type = "pointerUp", .button = 0 },
+ },
+ }} },
+ });
+ try ctx.expectSentResult(null, .{ .id = 8 });
+ try evaluate(&ctx, 9, context_id, "window.events.slice(11).join(' ')");
+ try ctx.expectSentResult(.{ .type = "success", .result = .{
+ .type = "string",
+ .value = "mousedown@btn mouseup@btn click@btn",
+ } }, .{ .id = 9 });
}
test "bidi.input: keys and modifiers" {
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index 98a6058ca..25b3b464b 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -315,7 +315,7 @@ test "cdp.input: dispatchMouseEvent mouseReleased fires mouseup" {
try testing.expect(result.isTrue());
}
-test "cdp.input: dispatchMouseEvent non-primary buttons fire auxclick" {
+test "cdp.input: dispatchMouseEvent button activation events match Chrome" {
var ctx = try testing.context();
defer ctx.deinit();
@@ -337,7 +337,7 @@ test "cdp.input: dispatchMouseEvent non-primary buttons fire auxclick" {
_ = try ls.local.compileAndRun(
\\window.clicks = [];
- \\for (const t of ['click', 'auxclick', 'contextmenu']) {
+ \\for (const t of ['mousedown', 'mouseup', 'click', 'auxclick', 'contextmenu']) {
\\ document.getElementById('hoverTarget')
\\ .addEventListener(t, (e) => { window.clicks.push(t + ':' + e.button); });
\\}
@@ -346,23 +346,32 @@ test "cdp.input: dispatchMouseEvent non-primary buttons fire auxclick" {
const rect_x = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().x", null)).toF64();
const rect_y = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().y", null)).toF64();
+ const presses = [_]struct { button: []const u8, click_count: i32, expected: []const u8 }{
+ .{ .button = "middle", .click_count = 1, .expected = "mousedown:1 mouseup:1 auxclick:1" },
+ .{ .button = "right", .click_count = 1, .expected = "mousedown:2 contextmenu:2 mouseup:2 auxclick:2" },
+ .{ .button = "back", .click_count = 1, .expected = "mousedown:3 mouseup:3 auxclick:3" },
+ .{ .button = "forward", .click_count = 1, .expected = "mousedown:4 mouseup:4 auxclick:4" },
+ .{ .button = "left", .click_count = 0, .expected = "mousedown:0 mouseup:0" },
+ .{ .button = "right", .click_count = 0, .expected = "mousedown:2 contextmenu:2 mouseup:2" },
+ };
var id: u32 = 1;
- for ([_][]const u8{ "middle", "right", "back", "forward" }) |button| {
+ for (presses) |press| {
+ _ = try ls.local.compileAndRun("window.clicks = []", null);
try ctx.processMessage(.{
.id = id,
.method = "Input.dispatchMouseEvent",
- .params = .{ .type = "mousePressed", .x = rect_x, .y = rect_y, .button = button, .clickCount = 1 },
+ .params = .{ .type = "mousePressed", .x = rect_x, .y = rect_y, .button = press.button, .clickCount = press.click_count },
});
try ctx.processMessage(.{
.id = id + 1,
.method = "Input.dispatchMouseEvent",
- .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = button, .clickCount = 1 },
+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = press.button, .clickCount = press.click_count },
});
id += 2;
- }
- const result = try ls.local.compileAndRun("window.clicks.join(' ') === 'auxclick:1 contextmenu:2 auxclick:2 auxclick:3 auxclick:4'", null);
- try testing.expect(result.isTrue());
+ const got = try (try ls.local.compileAndRun("window.clicks.join(' ')", null)).toStringSlice();
+ try testing.expectEqual(press.expected, got);
+ }
}
test "cdp.input: dispatchMouseEvent mousePressed honors preventDefault for focus" {
@@ -737,7 +746,7 @@ test "cdp.input: dispatchMouseEvent right button fires contextmenu, double-click
_ = try ls.local.compileAndRun(
\\const t = document.getElementById('hoverTarget');
- \\t.addEventListener('mousedown', (e) => { window.downButton = e.button; });
+ \\t.addEventListener('mousedown', (e) => { window.downButton ??= e.button; });
\\t.addEventListener('contextmenu', (e) => { window.ctxButton = e.button; });
\\t.addEventListener('dblclick', () => { window.dbl = true; });
, null);
@@ -745,7 +754,7 @@ test "cdp.input: dispatchMouseEvent right button fires contextmenu, double-click
const rect_x = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().x", null)).toF64();
const rect_y = try (try ls.local.compileAndRun("document.getElementById('hoverTarget').getBoundingClientRect().y", null)).toF64();
- // Right button: press carries button=2, release fires contextmenu (not click).
+ // Right button: press carries button=2 and fires contextmenu.
try ctx.processMessage(.{
.id = 1,
.method = "Input.dispatchMouseEvent",
@@ -761,6 +770,11 @@ test "cdp.input: dispatchMouseEvent right button fires contextmenu, double-click
try ctx.processMessage(.{
.id = 3,
.method = "Input.dispatchMouseEvent",
+ .params = .{ .type = "mousePressed", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 2 },
+ });
+ try ctx.processMessage(.{
+ .id = 4,
+ .method = "Input.dispatchMouseEvent",
.params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 2 },
});
@@ -1036,9 +1050,7 @@ test "cdp.input: mousePressed and mouseReleased on a disabled button emit no eve
try testing.expect(result.isTrue());
}
-// Asserts only the pointer events: pointerdown/pointerup fire at the mask's
-// 0/nonzero transitions and a mid-gesture button change is a pointermove (the
-// activation order below is a pre-existing, non-spec deviation from Chrome).
+// The pointerdown is cancelled, so no compat mouse events fire. Matches Chrome.
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();
@@ -1085,7 +1097,7 @@ test "cdp.input: a mouse chord fires pointermove for the mid-gesture button chan
const result = try ls.local.compileAndRun(
\\JSON.stringify(window.seqChord) === JSON.stringify([
- \\ 'pointerdown:1', 'pointermove:3', 'pointermove:1', 'contextmenu:1', 'auxclick:1', 'pointerup:0', 'click:0'
+ \\ 'pointerdown:1', 'pointermove:3', 'contextmenu:3', 'pointermove:1', 'auxclick:1', 'pointerup:0'
\\])
, null);
try testing.expect(result.isTrue());
@@ -1140,7 +1152,7 @@ test "cdp.input: a primary click fired mid-chord carries the still-held buttons
const result = try ls.local.compileAndRun(
\\JSON.stringify(window.seqChord) === JSON.stringify([
- \\ 'pointerdown:1', 'pointermove:3', 'pointermove:2', 'click:2', 'pointerup:0', 'contextmenu:0', 'auxclick:0'
+ \\ 'pointerdown:1', 'pointermove:3', 'contextmenu:3', 'pointermove:2', 'click:2', 'pointerup:0'
\\])
, null);
try testing.expect(result.isTrue());