Skip to content

Lightpanda: the headless browser designed for AI and automation

feat(input): match Chrome's touch slop, scroll and tap count

Adrià Arrufat10c758f49322
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 23ee7b739..ee6de4e48 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -473,6 +473,11 @@ pub fn deinit(self: *Frame) void {
page.input_touch_contact = null;
}
}
+ if (page.input_last_tap) |tap| {
+ if (tap.pinned) |el| if (el.ownerFrame(self) == self) {
+ page.input_last_tap = null;
+ };
+ }
if (comptime lp.IS_DEBUG) {
log.debug(.frame, "frame.deinit", .{ .url = self.url, .type = self._type });
diff --git a/src/browser/Page.zig b/src/browser/Page.zig
index 2e8047fab..ed732abc7 100644
--- a/src/browser/Page.zig
+++ b/src/browser/Page.zig
@@ -193,6 +193,8 @@ input_hover_target: ?*Element = null,
// The active touch contact; single-touch, so at most one.
input_touch_contact: ?@import("frame/user_input.zig").TouchContact = null,
input_touch_next_pointer_id: i32 = @import("frame/user_input.zig").first_touch_pointer_id,
+// The last tap that activated, for counting the next one.
+input_last_tap: ?@import("frame/user_input.zig").LastTap = null,
// Per-gesture button state for the synthetic mouse pointer; see
// user_input.PointerButtons.
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 2c8ddfa54..31c04b8d9 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -682,6 +682,14 @@ pub fn overscrollContainAxes(self: *StyleManager, el: *Element) Element.ScrollAx
return .{ .x = p.overscroll_x_contains, .y = p.overscroll_y_contains };
}
+/// The axes along which `el`'s own computed touch-action forbids a touch from
+/// panning. No ancestor walk.
+pub fn touchPanBlockedAxes(self: *StyleManager, el: *Element) Element.ScrollAxes {
+ self.rebuildIfDirty() catch return .{};
+ const p = self.geometryProps(el);
+ return .{ .x = p.touch_pan_x_blocked, .y = p.touch_pan_y_blocked };
+}
+
/// Own computed width or height in px, from inline style or a sheet rule.
/// Null when undeclared or when it needs layout to resolve.
pub fn declaredSize(self: *StyleManager, el: *Element, comptime axis: Element.Axis) ?f64 {
@@ -1346,12 +1354,14 @@ const Visibility = struct {
/// An element's box: its size and how it scrolls.
const Geometry = struct {
const Declared = struct {
- const names = [_][]const u8{ "overflow-x", "overflow-y", "overscroll-behavior-x", "overscroll-behavior-y", "width", "height" };
+ const names = [_][]const u8{ "overflow-x", "overflow-y", "overscroll-behavior-x", "overscroll-behavior-y", "touch-action", "width", "height" };
overflow_x_scrolls: ?bool = null,
overflow_y_scrolls: ?bool = null,
overscroll_x_contains: ?bool = null,
overscroll_y_contains: ?bool = null,
+ touch_pan_x_blocked: ?bool = null,
+ touch_pan_y_blocked: ?bool = null,
width: ?Length = null,
height: ?Length = null,
@@ -1364,6 +1374,10 @@ const Geometry = struct {
self.overscroll_x_contains = overscrollContains(value);
} else if (std.ascii.eqlIgnoreCase(name, "overscroll-behavior-y")) {
self.overscroll_y_contains = overscrollContains(value);
+ } else if (std.ascii.eqlIgnoreCase(name, "touch-action")) {
+ const pans = touchActionPans(value) orelse return;
+ self.touch_pan_x_blocked = !pans.x;
+ self.touch_pan_y_blocked = !pans.y;
} else if (std.ascii.eqlIgnoreCase(name, "width")) {
self.width = Length.parse(value);
} else if (std.ascii.eqlIgnoreCase(name, "height")) {
@@ -1384,13 +1398,38 @@ const Geometry = struct {
return std.ascii.eqlIgnoreCase(value, "contain") or
std.ascii.eqlIgnoreCase(value, "none");
}
+
+ // The axes a touch may pan along; null for an invalid value. The
+ // directional keywords count for their whole axis.
+ fn touchActionPans(value: []const u8) ?Element.ScrollAxes {
+ if (std.ascii.eqlIgnoreCase(value, "auto") or std.ascii.eqlIgnoreCase(value, "manipulation")) {
+ return .{ .x = true, .y = true };
+ }
+ if (std.ascii.eqlIgnoreCase(value, "none")) {
+ return .{};
+ }
+ var pans: Element.ScrollAxes = .{};
+ var it = std.mem.tokenizeAny(u8, value, " \t\n");
+ while (it.next()) |token| {
+ if (std.ascii.eqlIgnoreCase(token, "pan-x") or std.ascii.eqlIgnoreCase(token, "pan-left") or std.ascii.eqlIgnoreCase(token, "pan-right")) {
+ pans.x = true;
+ } else if (std.ascii.eqlIgnoreCase(token, "pan-y") or std.ascii.eqlIgnoreCase(token, "pan-up") or std.ascii.eqlIgnoreCase(token, "pan-down")) {
+ pans.y = true;
+ } else if (!std.ascii.eqlIgnoreCase(token, "pinch-zoom")) {
+ return null;
+ }
+ }
+ return pans;
+ }
};
- const Computed = packed struct(u72) {
+ const Computed = packed struct(u74) {
overflow_x_scrolls: bool = false,
overflow_y_scrolls: bool = false,
overscroll_x_contains: bool = false,
overscroll_y_contains: bool = false,
+ touch_pan_x_blocked: bool = false,
+ touch_pan_y_blocked: bool = false,
width: Length = .{},
height: Length = .{},
};
@@ -2110,6 +2149,14 @@ test "StyleManager: memo: reuse and invalidation" {
try b.setStyle("overscroll-behavior: contain; overscroll-behavior-x: auto", frame);
try testing.expectEqual(Element.ScrollAxes{ .x = false, .y = true }, sm.overscrollContainAxes(b));
+ try b.setStyle("touch-action: pan-y pinch-zoom", frame);
+ try testing.expectEqual(Element.ScrollAxes{ .x = true, .y = false }, sm.touchPanBlockedAxes(b));
+ try b.setStyle("touch-action: none", frame);
+ try testing.expectEqual(Element.ScrollAxes{ .x = true, .y = true }, sm.touchPanBlockedAxes(b));
+ try b.setStyle("touch-action: manipulation", frame);
+ try testing.expectEqual(Element.ScrollAxes{}, sm.touchPanBlockedAxes(b));
+ try testing.expectEqual(Element.ScrollAxes{}, sm.touchPanBlockedAxes(p));
+
// A stylesheet change resets the memo
sm.sheetModified();
try testing.expectEqual(false, sm.isHidden(p, .{}));
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 00e2ea83e..137a71b92 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -601,15 +601,28 @@ fn deltaToScroll(d: f64) i32 {
// pointerId 1 is the mouse.
pub const first_touch_pointer_id: i32 = 2;
+// Chrome's touch slop, also the tap slop: inside it touchmove is suppressed.
const tap_slop_px = 15;
+// Chrome's multi-tap window: the next tap must start within this time of the
+// last release and within this distance of the last tap's start.
+const multi_tap_ms = 400;
+const multi_tap_slop_px = 20;
pub const TouchContact = struct {
target: *Element,
point: Touch.Point,
start: Touch.Point,
pointer_id: i32,
+ tap_count: u32 = 1,
suppress_mouse: bool = false,
suppress_click: bool = false,
+ start_prevented: bool = false,
+ // Moved past the slop, so touchmove is no longer suppressed.
+ dragging: bool = false,
+ // The drag became a scroll: the touch events left are not cancelable.
+ scrolling: bool = false,
+ // A scroll the touch-action allows: the pointer was cancelled.
+ panning: bool = false,
/// A released contact reports a 1x1 point; a cancelled one keeps its
/// geometry.
@@ -618,7 +631,7 @@ pub const TouchContact = struct {
return .{
.button = if (phase == .cancel) -1 else mouse_button.main,
.buttons_down = if (phase == .down) 1 else 0,
- .click_count = 1,
+ .click_count = self.tap_count,
.x = point.x,
.y = point.y,
.modifiers = modifiers,
@@ -650,21 +663,36 @@ const TouchType = enum {
touchcancel,
};
+pub const LastTap = struct {
+ count: u32,
+ x: f64,
+ y: f64,
+ released_ms: u64,
+ // WebDriver's pinned target, standing in for the point it doesn't have.
+ pinned: ?*Element,
+};
+
+const TouchDispatch = struct {
+ modifiers: Modifiers = .{},
+ // Event.cancelable: false for touchcancel, and once a drag scrolls.
+ cancelable: bool = true,
+};
+
/// Fires the touch event on `target` (no hit-test); returns whether
/// preventDefault() cancelled it.
-fn dispatchTouchEvent(frame: *Frame, target: *Element, typ: TouchType, point: Touch.Point, modifiers: Modifiers) !bool {
+fn dispatchTouchEvent(frame: *Frame, target: *Element, typ: TouchType, point: Touch.Point, opts: TouchDispatch) !bool {
const event: *TouchEvent = try .initTrustedWithTouch(@tagName(typ), .{
.bubbles = true,
.composed = true,
- .altKey = modifiers.alt,
- .ctrlKey = modifiers.ctrl,
- .metaKey = modifiers.meta,
- .shiftKey = modifiers.shift,
+ .altKey = opts.modifiers.alt,
+ .ctrlKey = opts.modifiers.ctrl,
+ .metaKey = opts.modifiers.meta,
+ .shiftKey = opts.modifiers.shift,
}, target, point, frame);
- // touchcancel is never cancelable per spec; the others follow the same
- // passive-listener-dependent rule as wheel (see EventManager).
- if (typ != .touchcancel) {
+ // A cancelable touch event follows the same passive-listener-dependent
+ // rule as wheel (see EventManager).
+ if (opts.cancelable) {
event.asEvent()._cancelable_unless_passive = true;
}
@@ -696,17 +724,34 @@ pub fn touchStart(frame: *Frame, target: ?*Element, point: Touch.Point, modifier
.point = point,
.start = point,
.pointer_id = page.input_touch_next_pointer_id,
+ .tap_count = nextTapCount(page.input_last_tap, point, target),
};
+ page.input_last_tap = null;
page.input_touch_next_pointer_id = if (contact.pointer_id == std.math.maxInt(i32)) first_touch_pointer_id else contact.pointer_id + 1;
const g = contact.gesture(point, .down, modifiers);
dispatchBoundaryEvents(frame, null, resolved, TouchBoundary{ .g = g });
contact.suppress_mouse = try emitPointer(frame, resolved, "pointerdown", g, 0);
- contact.suppress_click = try dispatchTouchEvent(frame, resolved, .touchstart, point, modifiers);
+ contact.start_prevented = try dispatchTouchEvent(frame, resolved, .touchstart, point, .{ .modifiers = modifiers });
+ contact.suppress_click = contact.start_prevented;
page.input_touch_contact = contact;
}
-/// Stays on the touchstart element. A drag never activates: past the slop, or
+/// Taps count 1, 2, 3, then start over, as in Chrome.
+fn nextTapCount(last_tap: ?LastTap, start: Touch.Point, pinned: ?*Element) u32 {
+ const last = last_tap orelse return 1;
+ if (lp.datetime.milliTimestamp(.boot) -| last.released_ms > multi_tap_ms) return 1;
+ if (last.pinned != pinned) return 1;
+ const dx = start.x - last.x;
+ const dy = start.y - last.y;
+ if (dx * dx + dy * dy > multi_tap_slop_px * multi_tap_slop_px) return 1;
+ return last.count % 3 + 1;
+}
+
+/// Stays on the touchstart element. Inside the slop only the pointer moves, as
+/// in Chrome. Leaving it is a drag, which never activates: past the slop, or
/// for WebDriver, which has no viewport distance, onto another element `over`.
+/// A drag the page doesn't cancel scrolls: its touch events stop being
+/// cancelable, and where touch-action allows the pan the pointer is cancelled.
pub fn touchMove(frame: *Frame, over: ?*Element, point: Touch.Point, modifiers: Modifiers) !void {
const page = frame.page;
var contact = page.input_touch_contact orelse return;
@@ -714,18 +759,50 @@ pub fn touchMove(frame: *Frame, over: ?*Element, point: Touch.Point, modifiers:
const dx = point.x - contact.start.x;
const dy = point.y - contact.start.y;
const moved_off = if (over) |el| el != contact.target else false;
- if (moved_off or dx * dx + dy * dy > tap_slop_px * tap_slop_px) {
+ const leaves_slop = !contact.dragging and (moved_off or dx * dx + dy * dy > tap_slop_px * tap_slop_px);
+ if (leaves_slop) {
+ contact.dragging = true;
contact.suppress_click = true;
}
var g = contact.gesture(point, .down, modifiers);
g.button = -1;
- _ = try emitPointer(frame, contact.target, "pointermove", g, 0);
- if (try dispatchTouchEvent(frame, contact.target, .touchmove, point, modifiers)) {
- contact.suppress_click = true;
+ if (!contact.panning) {
+ _ = try emitPointer(frame, contact.target, "pointermove", g, 0);
+ }
+ if (!contact.dragging) {
+ page.input_touch_contact = contact;
+ return;
+ }
+ const prevented = try dispatchTouchEvent(frame, contact.target, .touchmove, point, .{ .modifiers = modifiers, .cancelable = !contact.scrolling });
+ if (leaves_slop and !prevented and !contact.start_prevented) {
+ contact.scrolling = true;
+ if (touchActionPans(frame, contact.target, dx, dy)) {
+ contact.panning = true;
+ const cg = contact.gesture(point, .cancel, modifiers);
+ _ = try emitPointer(frame, contact.target, "pointercancel", cg, 0);
+ dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = cg });
+ }
}
page.input_touch_contact = contact;
}
+/// Whether the effective touch-action of `target`, which intersects its
+/// ancestors', allows a pan in the drag's dominant direction. A drag with no
+/// dominant direction, as WebDriver's, needs either axis.
+fn touchActionPans(frame: *Frame, target: *Element, dx: f64, dy: f64) bool {
+ const owner = target.ownerFrame(frame) orelse frame;
+ var blocked: Element.ScrollAxes = .{};
+ var current: ?*Element = target;
+ while (current) |el| : (current = el.parentElement()) {
+ const own = owner._style_manager.touchPanBlockedAxes(el);
+ blocked.x = blocked.x or own.x;
+ blocked.y = blocked.y or own.y;
+ }
+ if (@abs(dx) > @abs(dy)) return !blocked.x;
+ if (@abs(dy) > @abs(dx)) return !blocked.y;
+ return !blocked.x or !blocked.y;
+}
+
/// A touch pointer moving with no contact down, which only WebDriver expresses.
pub fn touchHover(frame: *Frame, target: *Element, modifiers: Modifiers) !void {
_ = try emitPointer(frame, target, "pointermove", .{
@@ -747,10 +824,19 @@ pub fn touchEnd(frame: *Frame, point: ?Touch.Point, modifiers: Modifiers, compat
const lift = point orelse contact.point;
const g = contact.gesture(lift, .up, modifiers);
- _ = try emitPointer(frame, contact.target, "pointerup", g, 0);
- dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = g });
- const prevented = try dispatchTouchEvent(frame, contact.target, .touchend, lift, modifiers);
+ if (!contact.panning) {
+ _ = try emitPointer(frame, contact.target, "pointerup", g, 0);
+ dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = g });
+ }
+ const prevented = try dispatchTouchEvent(frame, contact.target, .touchend, lift, .{ .modifiers = modifiers, .cancelable = !contact.scrolling });
if (contact.suppress_click or prevented) return;
+ frame.page.input_last_tap = .{
+ .count = contact.tap_count,
+ .x = contact.start.x,
+ .y = contact.start.y,
+ .released_ms = lp.datetime.milliTimestamp(.boot),
+ .pinned = compat_target,
+ };
var target = try touchCompatTarget(frame, lift, compat_target) orelse return;
updateHoverTarget(frame, target, .{ .x = lift.x, .y = lift.y, .modifiers = modifiers, .with_pointer = false });
@@ -776,10 +862,12 @@ pub fn touchEnd(frame: *Frame, point: ?Touch.Point, modifiers: Modifiers, compat
pub fn touchCancel(frame: *Frame, modifiers: Modifiers) !void {
const contact = frame.page.input_touch_contact orelse return;
frame.page.input_touch_contact = null;
- const g = contact.gesture(contact.point, .cancel, modifiers);
- _ = try emitPointer(frame, contact.target, "pointercancel", g, 0);
- dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = g });
- _ = try dispatchTouchEvent(frame, contact.target, .touchcancel, contact.point, modifiers);
+ if (!contact.panning) {
+ const g = contact.gesture(contact.point, .cancel, modifiers);
+ _ = try emitPointer(frame, contact.target, "pointercancel", g, 0);
+ dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = g });
+ }
+ _ = try dispatchTouchEvent(frame, contact.target, .touchcancel, contact.point, .{ .modifiers = modifiers, .cancelable = false });
}
fn touchCompatTarget(frame: *Frame, point: Touch.Point, pinned: ?*Element) !?*Element {
@@ -1484,7 +1572,7 @@ pub const Modifiers = struct {
const testing = @import("../../testing.zig");
-test "user_input: dispatchTouchEvent touchcancel is never cancelable" {
+test "user_input: touchcancel is never cancelable" {
const page = try testing.pageTest("mcp_actions.html", .{});
defer page.close();
const frame = page.frame().?;
@@ -1504,7 +1592,8 @@ test "user_input: dispatchTouchEvent touchcancel is never cancelable" {
\\t;
, null);
const target = try ls.local.jsValueToZig(*Element, value);
- _ = try dispatchTouchEvent(frame, target, .touchcancel, .{ .x = 10, .y = 20 }, .{});
+ try touchStart(frame, target, .{ .x = 10, .y = 20 }, .{});
+ try touchCancel(frame, .{});
try testing.expect((try ls.local.compileAndRun("window.result === true", null)).isTrue());
}
diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zig
index 8706533d2..b0c55f594 100644
--- a/src/browser/webapi/WebDriver.zig
+++ b/src/browser/webapi/WebDriver.zig
@@ -499,6 +499,8 @@ test "WebApi: WebDriver touchmove/touchend stay on the touchstart target" {
_ = try ls.local.compileAndRun(
\\const a = document.getElementById('hoverTarget');
\\const b = document.getElementById('btn');
+ \\// Without it the drag scrolls and cancels the pointer before the pointerup.
+ \\a.style.touchAction = 'none';
\\window.moveOnA = false;
\\window.moveOnB = false;
\\window.endOnA = false;
@@ -635,6 +637,7 @@ test "WebApi: WebDriver a touch tap activates and a drag does not" {
\\window.tapIds = [];
\\box.addEventListener('pointerdown', e => { if (e.pointerType === 'touch') window.tapIds.push(e.pointerId); });
\\box.addEventListener('pointerup', e => { if (e.pointerType === 'touch') window.tapIds.push(e.pointerId); });
+ \\box.addEventListener('pointercancel', e => { if (e.pointerType === 'touch') window.tapIds.push(e.pointerId); });
\\window.webdriver.actionSequence([{
\\ type: 'pointer',
\\ parameters: { pointerType: 'touch' },
diff --git a/src/server/bidi/input.zig b/src/server/bidi/input.zig
index 76085015d..8096b6798 100644
--- a/src/server/bidi/input.zig
+++ b/src/server/bidi/input.zig
@@ -815,7 +815,7 @@ test "bidi.input: touch tap" {
try ctx.expectSentResult(.{ .nodes = .{.{ .sharedId = "1" }} }, .{ .id = 1 });
try evaluate(&ctx, 2, context_id,
- \\for (const t of ['pointerdown', 'pointerup', 'touchstart', 'touchmove', 'touchend']) {
+ \\for (const t of ['pointerdown', 'pointerup', 'pointercancel', 'touchstart', 'touchmove', 'touchend']) {
\\ document.addEventListener(t, (e) => window.events.push(t + ':' + (e.pointerType ?? e.changedTouches[0].target.id)));
\\}
\\'ok'
@@ -843,7 +843,8 @@ test "bidi.input: touch tap" {
.value = "pointerdown:touch touchstart:btn pointerup:touch touchend:btn mousemove@btn mousedown@btn mouseup@btn click@btn",
} }, .{ .id = 4 });
- // A drag past the slop delivers the touch events but doesn't click.
+ // A drag past the slop scrolls: the touch events go on, but the pointer is
+ // cancelled and nothing clicks.
try ctx.processMessage(.{
.id = 5,
.method = "input.performActions",
@@ -862,7 +863,7 @@ test "bidi.input: touch tap" {
try evaluate(&ctx, 6, context_id, "window.events.slice(8).join(' ')");
try ctx.expectSentResult(.{ .type = "success", .result = .{
.type = "string",
- .value = "pointerdown:touch touchstart:btn touchmove:btn pointerup:touch touchend:btn",
+ .value = "pointerdown:touch touchstart:btn touchmove:btn pointercancel:touch touchend:btn",
} }, .{ .id = 6 });
// The source's pointerType is fixed once it exists.
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index c3decc29c..f8e5562c0 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -1512,6 +1512,7 @@ test "cdp.input: dispatchTouchEvent preserves each event's modifier keys" {
\\}
, null);
+ // The move leaves the slop, or no touchmove would fire.
const steps = .{
.{ "touchStart", 1 },
.{ "touchMove", 2 },
@@ -1524,7 +1525,7 @@ test "cdp.input: dispatchTouchEvent preserves each event's modifier keys" {
try ctx.processMessage(.{
.id = id,
.method = "Input.dispatchTouchEvent",
- .params = .{ .type = step[0], .modifiers = step[1], .touchPoints = if (is_lift) &.{} else &.{.{ .x = 0, .y = 0 }} },
+ .params = .{ .type = step[0], .modifiers = step[1], .touchPoints = if (is_lift) &.{} else &.{.{ .x = 0, .y = if (comptime std.mem.eql(u8, step[0], "touchMove")) 20 else 0 }} },
});
}
// Omitted modifiers reset to false, even after a modified gesture.
@@ -1744,13 +1745,13 @@ test "cdp.input: dispatchTouchEvent tracks the client's chosen id and rejects mi
try ctx.processMessage(.{
.id = 4,
.method = "Input.dispatchTouchEvent",
- .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = x + 1, .y = y + 2, .id = 7 }} },
+ .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = x + 1, .y = y + 20, .id = 7 }} },
});
try ctx.processMessage(.{
.id = 5,
.method = "Input.dispatchTouchEvent",
- .params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x + 1, .y = y + 2, .id = 8 }} },
+ .params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x + 1, .y = y + 20, .id = 8 }} },
});
try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 5 });
try testing.expect(frame.page.input_touch_contact != null);
@@ -1758,14 +1759,14 @@ test "cdp.input: dispatchTouchEvent tracks the client's chosen id and rejects mi
try ctx.processMessage(.{
.id = 6,
.method = "Input.dispatchTouchEvent",
- .params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x + 1, .y = y + 2, .id = 7 }} },
+ .params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x + 1, .y = y + 20, .id = 7 }} },
});
try testing.expect(frame.page.input_touch_contact == null);
try testing.expect((try ls.local.compileAndRun(
\\JSON.stringify(touchEvents) === JSON.stringify([
\\ ['touchstart', 7, target.getBoundingClientRect().x, target.getBoundingClientRect().y],
- \\ ['touchmove', 7, target.getBoundingClientRect().x + 1, target.getBoundingClientRect().y + 2],
- \\ ['touchend', 7, target.getBoundingClientRect().x + 1, target.getBoundingClientRect().y + 2]
+ \\ ['touchmove', 7, target.getBoundingClientRect().x + 1, target.getBoundingClientRect().y + 20],
+ \\ ['touchend', 7, target.getBoundingClientRect().x + 1, target.getBoundingClientRect().y + 20]
\\])
, null)).isTrue());
}
@@ -2743,7 +2744,7 @@ test "cdp.input: touch tap cancellation distinguishes touch pointer and mouse de
const y = try (try ls.local.compileAndRun("tapTarget.getBoundingClientRect().y + 1", null)).toF64();
const cases = .{
.{ "touchstart", "!tapTarget.checked && !tapEvents.includes('mousedown') && !tapEvents.includes('click')" },
- .{ "touchmove", "!tapTarget.checked && !tapEvents.includes('mousedown') && !tapEvents.includes('click')" },
+ .{ "touchmove", "!tapTarget.checked && !tapEvents.includes('pointercancel') && tapEvents.includes('pointerup') && !tapEvents.includes('click')" },
.{ "touchend", "!tapTarget.checked && !tapEvents.includes('mousedown') && !tapEvents.includes('click')" },
.{ "pointerdown", "tapTarget.checked && tapEvents.includes('click') && !tapEvents.includes('mousedown') && !tapEvents.includes('mouseup') && !tapEvents.includes('mousemove') && document.activeElement.id === 'inp'" },
.{ "pointerup", "tapTarget.checked && tapEvents.includes('mousedown') && tapEvents.includes('mouseup')" },
@@ -2763,7 +2764,7 @@ test "cdp.input: touch tap cancellation distinguishes touch pointer and mouse de
try ctx.processMessage(.{
.id = 2,
.method = "Input.dispatchTouchEvent",
- .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = x + 1, .y = y }} },
+ .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = x + 20, .y = y }} },
});
}
try ctx.processMessage(.{
@@ -2793,7 +2794,7 @@ test "cdp.input: touch pointer retains its target while compatibility events hit
\\const startTarget = document.getElementById('btn');
\\const releaseTarget = document.getElementById('inp');
\\window.tapEvents = [];
- \\for (const type of ['pointerdown', 'pointermove', 'pointerup', 'touchstart', 'touchmove', 'touchend', 'mousedown', 'mouseup', 'click']) {
+ \\for (const type of ['pointerdown', 'pointermove', 'pointerup', 'pointercancel', 'touchstart', 'touchmove', 'touchend', 'mousedown', 'mouseup', 'click']) {
\\ document.addEventListener(type, e => tapEvents.push(e.type + ':' + e.target.id), {passive: false});
\\}
, null);
@@ -2813,7 +2814,8 @@ test "cdp.input: touch pointer retains its target while compatibility events hit
});
try testing.expect((try ls.local.compileAndRun("tapEvents.join(',') === 'pointerdown:btn,touchstart:btn,pointerup:btn,touchend:btn,mousedown:inp,mouseup:inp,click:inp'", null)).isTrue());
- // A drag retains pointer/touch delivery but must not activate on release.
+ // A drag scrolls: the touch events stay on the start target, the pointer is
+ // cancelled, and the release doesn't activate.
_ = try ls.local.compileAndRun("tapEvents = [];", null);
try ctx.processMessage(.{
.id = 3,
@@ -2830,7 +2832,117 @@ test "cdp.input: touch pointer retains its target while compatibility events hit
.method = "Input.dispatchTouchEvent",
.params = .{ .type = "touchEnd", .touchPoints = &.{} },
});
- try testing.expect((try ls.local.compileAndRun("tapEvents.join(',') === 'pointerdown:btn,touchstart:btn,pointermove:btn,touchmove:btn,pointerup:btn,touchend:btn'", null)).isTrue());
+ try testing.expect((try ls.local.compileAndRun("tapEvents.join(',') === 'pointerdown:btn,touchstart:btn,pointermove:btn,touchmove:btn,pointercancel:btn,touchend:btn'", null)).isTrue());
+}
+
+// Expectations recorded from Chrome over CDP. Inside the 15px slop only the
+// pointer moves. Past it, a drag the page doesn't cancel scrolls: the touch
+// events stop being cancelable, and where touch-action allows the pan, the
+// pointer is cancelled instead of released.
+test "cdp.input: touch slop, scroll and touch-action" {
+ 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();
+ _ = try ls.local.compileAndRun(
+ \\const t = document.getElementById('btn');
+ \\window.log = [];
+ \\window.preventType = '';
+ \\for (const type of ['pointerdown', 'pointermove', 'pointerup', 'pointercancel', 'touchstart', 'touchmove', 'touchend', 'click']) {
+ \\ t.addEventListener(type, e => {
+ \\ log.push(e.type + (e.type.startsWith('touch') && !e.cancelable ? '(nc)' : ''));
+ \\ if (e.type === preventType) e.preventDefault();
+ \\ }, {passive: false});
+ \\}
+ , null);
+ const x = try (try ls.local.compileAndRun("t.getBoundingClientRect().x + 1", null)).toF64();
+ const y = try (try ls.local.compileAndRun("t.getBoundingClientRect().y + 1", null)).toF64();
+
+ const Move = struct { f64, f64 };
+ const cancelled = "pointerdown touchstart pointermove touchmove pointercancel touchmove(nc) touchend(nc)";
+ const held = "pointerdown touchstart pointermove touchmove pointermove touchmove(nc) pointerup touchend(nc)";
+ const cases = [_]struct { setup: []const u8, moves: []const Move, expect: []const u8 }{
+ .{ .setup = "", .moves = &.{.{ 10, 0 }}, .expect = "pointerdown touchstart pointermove pointerup touchend click" },
+ .{ .setup = "", .moves = &.{.{ 15, 0 }}, .expect = "pointerdown touchstart pointermove pointerup touchend click" },
+ .{ .setup = "", .moves = &.{ .{ 5, 0 }, .{ 20, 0 }, .{ 2, 0 } }, .expect = "pointerdown touchstart pointermove pointermove touchmove pointercancel touchmove(nc) touchend(nc)" },
+ .{ .setup = "", .moves = &.{ .{ 0, 20 }, .{ 0, 40 } }, .expect = cancelled },
+ .{ .setup = "t.style.touchAction = 'none'", .moves = &.{ .{ 0, 20 }, .{ 0, 40 } }, .expect = held },
+ .{ .setup = "t.parentElement.style.touchAction = 'none'", .moves = &.{ .{ 0, 20 }, .{ 0, 40 } }, .expect = held },
+ .{ .setup = "t.style.touchAction = 'pan-x'", .moves = &.{ .{ 0, 20 }, .{ 0, 40 } }, .expect = held },
+ .{ .setup = "t.style.touchAction = 'pan-x'", .moves = &.{ .{ 20, 0 }, .{ 40, 0 } }, .expect = cancelled },
+ .{ .setup = "t.style.touchAction = 'pinch-zoom'", .moves = &.{.{ 0, 20 }}, .expect = "pointerdown touchstart pointermove touchmove pointerup touchend(nc)" },
+ .{ .setup = "preventType = 'touchstart'", .moves = &.{ .{ 0, 20 }, .{ 0, 40 } }, .expect = "pointerdown touchstart pointermove touchmove pointermove touchmove pointerup touchend" },
+ .{ .setup = "preventType = 'touchmove'", .moves = &.{ .{ 0, 20 }, .{ 0, 40 } }, .expect = "pointerdown touchstart pointermove touchmove pointermove touchmove pointerup touchend" },
+ };
+
+ for (cases) |c| {
+ _ = try ls.local.compileAndRun("log = []; preventType = ''; t.style.touchAction = ''; t.parentElement.style.touchAction = '';", null);
+ if (c.setup.len > 0) _ = try ls.local.compileAndRun(c.setup, null);
+ frame.page.input_last_tap = null;
+ try ctx.processMessage(.{ .id = 1, .method = "Input.dispatchTouchEvent", .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = x, .y = y }} } });
+ for (c.moves) |m| {
+ try ctx.processMessage(.{ .id = 2, .method = "Input.dispatchTouchEvent", .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = x + m[0], .y = y + m[1] }} } });
+ }
+ // Released where it started: the faux layout's button is too small
+ // for the release point to stay on it.
+ try ctx.processMessage(.{ .id = 3, .method = "Input.dispatchTouchEvent", .params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x, .y = y }} } });
+ const got = try (try ls.local.compileAndRun("log.join(' ')", null)).toStringSlice();
+ try testing.expectEqualSlices(u8, c.expect, got);
+ }
+}
+
+// As in Chrome: taps within 400ms and 20px of the last one count 1, 2, 3 and
+// start over, and the second fires a dblclick. A drag in between resets.
+test "cdp.input: consecutive taps count up" {
+ 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();
+ _ = try ls.local.compileAndRun(
+ \\window.log = [];
+ \\for (const type of ['mousedown', 'click', 'dblclick']) {
+ \\ document.addEventListener(type, e => log.push(e.type + ':' + e.detail));
+ \\}
+ , null);
+ const x = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().x + 1", null)).toF64();
+ const y = try (try ls.local.compileAndRun("document.getElementById('btn').getBoundingClientRect().y + 1", null)).toF64();
+
+ const Tap = struct {
+ fn run(c: anytype, tx: f64, ty: f64, drag: bool) !void {
+ try c.processMessage(.{ .id = 1, .method = "Input.dispatchTouchEvent", .params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = tx, .y = ty }} } });
+ if (drag) {
+ try c.processMessage(.{ .id = 2, .method = "Input.dispatchTouchEvent", .params = .{ .type = "touchMove", .touchPoints = &.{.{ .x = tx, .y = ty + 30 }} } });
+ }
+ try c.processMessage(.{ .id = 3, .method = "Input.dispatchTouchEvent", .params = .{ .type = "touchEnd", .touchPoints = &.{} } });
+ }
+ };
+
+ for (0..4) |_| try Tap.run(&ctx, x, y, false);
+ try testing.expectEqualSlices(u8, "mousedown:1 click:1 mousedown:2 click:2 dblclick:2 mousedown:3 click:3 mousedown:1 click:1", try (try ls.local.compileAndRun("log.join(' ')", null)).toStringSlice());
+
+ _ = try ls.local.compileAndRun("log = []", null);
+ try Tap.run(&ctx, x + 20, y, false);
+ try Tap.run(&ctx, x + 20, y + 21, false);
+ try testing.expectEqualSlices(u8, "mousedown:2 click:2 dblclick:2 mousedown:1 click:1", try (try ls.local.compileAndRun("log.join(' ')", null)).toStringSlice());
+
+ _ = try ls.local.compileAndRun("log = []", null);
+ frame.page.input_last_tap.?.released_ms -= 401;
+ try Tap.run(&ctx, x + 20, y + 21, false);
+ try Tap.run(&ctx, x + 20, y + 21, true);
+ try Tap.run(&ctx, x + 20, y + 21, false);
+ try testing.expectEqualSlices(u8, "mousedown:1 click:1 mousedown:1 click:1", try (try ls.local.compileAndRun("log.join(' ')", null)).toStringSlice());
}
test "cdp.input: passive prevention and finger jitter do not suppress repeated taps" {