Lightpanda: the headless browser designed for AI and automation
refactor(input): route touch through the shared pointer helpers
Adrià Arrufat9f0731cb66ed
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 7438bb08d..42f1e2c23 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -35,6 +35,7 @@ const TreeWalker = @import("../webapi/TreeWalker.zig");const TextEvent = @import("../webapi/event/TextEvent.zig");const InputEvent = @import("../webapi/event/InputEvent.zig");const MouseEvent = @import("../webapi/event/MouseEvent.zig");+const Touch = @import("../webapi/event/Touch.zig");const TouchEvent = @import("../webapi/event/TouchEvent.zig");const WheelEvent = @import("../webapi/event/WheelEvent.zig");const PointerEvent = @import("../webapi/event/PointerEvent.zig");@@ -58,6 +59,10 @@ const HoverContext = struct {buttons: u16 = 0,modifiers: Modifiers = .{},with_pointer: bool = true,++ fn emit(ctx: HoverContext, frame: *Frame, target: *Element, comptime suffix: []const u8, related: ?*Element, bubbling: bool) void {+ dispatchBoundaryEvent(frame, target, "mouse" ++ suffix, "pointer" ++ suffix, related, bubbling, ctx);+ }};// Update the element being hovered. The page always tracks the currently@@ -71,7 +76,13 @@ pub fn updateHoverTarget(frame: *Frame, to: ?*Element, ctx: HoverContext) void {return;}page.input_hover_target = to;+ dispatchBoundaryEvents(frame, from, to, ctx);+}+/// out and leave up from `from`, then over and enter down to `to`, the leave+/// and enter chains stopping at their common ancestor. `emitter.emit` fires one+/// boundary event, named by its suffix ("out", "leave", "over", "enter").+fn dispatchBoundaryEvents(frame: *Frame, from: ?*Element, to: ?*Element, emitter: anytype) void {const pivot: ?*Node = blk: {const a = from orelse break :blk null;const b = to orelse break :blk null;@@ -79,20 +90,20 @@ pub fn updateHoverTarget(frame: *Frame, to: ?*Element, ctx: HoverContext) void {};if (from) |old| {- dispatchBoundaryEvent(frame, old, "mouseout", "pointerout", to, true, ctx);+ emitter.emit(frame, old, "out", to, true);var current: ?*Node = old.asNode();while (current) |node| : (current = node.parentNode()) {if (node == pivot) {break;}if (node.is(Element)) |element| {- dispatchBoundaryEvent(frame, element, "mouseleave", "pointerleave", to, false, ctx);+ emitter.emit(frame, element, "leave", to, false);}}}if (to) |new| {- dispatchBoundaryEvent(frame, new, "mouseover", "pointerover", from, true, ctx);+ emitter.emit(frame, new, "over", from, true);// Enter fires outermost-first. The chain is walked without allocating:// count the elements between the target and the pivot, then re-walk to@@ -114,7 +125,7 @@ pub fn updateHoverTarget(frame: *Frame, to: ?*Element, ctx: HoverContext) void {const element = node.is(Element) orelse continue;remaining -= 1;if (remaining == 0) {- dispatchBoundaryEvent(frame, element, "mouseenter", "pointerenter", from, false, ctx);+ emitter.emit(frame, element, "enter", from, false);break;}}@@ -183,6 +194,12 @@ const Gesture = struct {y: f64 = 0,modifiers: Modifiers = .{},emit_mouse_compat: bool = true,+ pointer_type: []const u8 = "mouse",+ pointer_id: i32 = 1,+ width: f64 = 1,+ height: f64 = 1,+ /// Reported while a button is held, 0 otherwise.+ pressure: f64 = 0.5,};/// Dispatch a trusted pointer event; returns whether preventDefault() cancelled it.@@ -194,19 +211,22 @@ fn emitPointer(detail: u32,) !bool {const owner = target.ownerFrame(frame) orelse frame;+ const boundary = std.mem.eql(u8, typ, "pointerenter") or std.mem.eql(u8, typ, "pointerleave");const event: *PointerEvent = try .initTrusted(typ, .{- .bubbles = true,- .cancelable = true,- .composed = true,+ .bubbles = !boundary,+ .cancelable = !boundary and !std.mem.eql(u8, typ, "pointercancel"),+ .composed = !boundary,.clientX = g.x,.clientY = g.y,.button = g.button,.buttons = g.buttons_down,.detail = detail,- .pointerId = 1,- .pointerType = "mouse",+ .pointerId = g.pointer_id,+ .pointerType = g.pointer_type,.isPrimary = true,- .pressure = if (g.buttons_down != 0) 0.5 else 0.0,+ .width = g.width,+ .height = g.height,+ .pressure = if (g.buttons_down != 0) g.pressure else 0.0,.ctrlKey = g.modifiers.ctrl,.shiftKey = g.modifiers.shift,.altKey = g.modifiers.alt,@@ -362,10 +382,8 @@ fn pressSequence(frame: *Frame, target: *Element, g: Gesture, chord_suppressed:return suppressed;}- // A disabled control gets the pointer events, contextmenu and auxclick,- // but no mouse events, click or focus, as in Chrome.- if (!suppressed and !target.isDisabled() and !try emitMouse(frame, target, "mousedown", g, g.click_count)) {- focusForMouseDown(frame, target) catch |err| log.debug(.app, "mousedown focus", .{ .err = err });+ if (!suppressed) {+ try mouseDown(frame, target, g);}// Chrome on Linux and macOS fires contextmenu on press, even when the// pointerdown was cancelled.@@ -375,17 +393,33 @@ fn pressSequence(frame: *Frame, target: *Element, g: Gesture, chord_suppressed:return suppressed;}+/// A disabled control gets the pointer events, contextmenu and auxclick, but+/// no mouse events, click or focus, as in Chrome.+fn mouseDown(frame: *Frame, target: *Element, g: Gesture) !void {+ if (!target.isDisabled() and !try emitMouse(frame, target, "mousedown", g, g.click_count)) {+ focusForMouseDown(frame, target) catch |err| log.debug(.app, "mousedown focus", .{ .err = err });+ }+}+/// 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 {_ = try emitPointer(frame, up_target, if (g.buttons_down == 0) "pointerup" else "pointermove", g, 0);- if (g.emit_mouse_compat and !suppressed and !up_target.isDisabled()) {+ if (g.emit_mouse_compat) {+ try mouseUpAndClick(frame, up_target, g, suppressed, click_target);+ }+}++/// `suppressed` drops the mouseup but keeps the click, as a cancelled+/// pointerdown does.+fn mouseUpAndClick(frame: *Frame, up_target: *Element, g: Gesture, suppressed: bool, click_target: ?*Element) !void {+ if (!suppressed and !up_target.isDisabled()) {_ = try emitMouse(frame, up_target, "mouseup", g, g.click_count);}// 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) {+ if (g.click_count == 0) {return;}@@ -574,28 +608,46 @@ fn deltaToScroll(d: f64) i32 {return @trunc(std.math.clamp(d, std.math.minInt(i32), std.math.maxInt(i32)));}-/// One contact as the client described it. The id is whatever the client-/// picked on touchstart (Puppeteer counts up from 1; a bare CDP call with no-/// id defaults to 0). The radius, angle and force defaults are Chrome's for a-/// point that leaves them out, and Puppeteer overrides all three per tap.-pub const TouchPoint = struct {- x: f64,- y: f64,- identifier: i32 = 0,- radius_x: f64 = 1,- radius_y: f64 = 1,- rotation_angle: f64 = 0,- force: f64 = 1,-};-/// The CDP-tracked touch contact. Single-touch scope: at most one.pub const TouchContact = struct {target: *Element,- point: TouchPoint,- start: TouchPoint,+ point: Touch.Point,+ start: Touch.Point,pointer_id: i32,suppress_mouse: bool = false,suppress_click: bool = false,++ /// The touch pointer's attributes at each phase. Pressed, it reports the+ /// contact's size and force; released, a 1x1 point at no pressure; and a+ /// cancelled contact keeps its geometry.+ fn gesture(self: TouchContact, point: Touch.Point, phase: enum { down, up, cancel }, modifiers: Modifiers) Gesture {+ const released = phase == .up;+ return .{+ .button = if (phase == .cancel) -1 else mouse_button.main,+ .buttons_down = if (phase == .down) 1 else 0,+ .click_count = 1,+ .x = point.x,+ .y = point.y,+ .modifiers = modifiers,+ .pointer_type = "touch",+ .pointer_id = self.pointer_id,+ .width = if (released) 1 else point.radiusX * 2,+ .height = if (released) 1 else point.radiusY * 2,+ .pressure = point.force,+ };+ }+};++/// Fires only the pointer half of each boundary event: touch has no hover, so+/// the mouse half comes later, with the tap's compatibility events.+const TouchBoundary = struct {+ g: Gesture,++ fn emit(self: TouchBoundary, frame: *Frame, target: *Element, comptime suffix: []const u8, _: ?*Element, _: bool) void {+ _ = emitPointer(frame, target, "pointer" ++ suffix, self.g, 0) catch |err| {+ log.debug(.frame, "touch boundary event", .{ .err = err, .type = suffix });+ };+ }};pub const TouchType = enum {@@ -603,43 +655,20 @@ pub const TouchType = enum {touchmove,touchend,touchcancel,-- fn name(self: TouchType) []const u8 {- return @tagName(self);- }-- fn isLift(self: TouchType) bool {- return self == .touchend or self == .touchcancel;- }};-/// The caller supplies the target (no hit-test), so touchmove/touchend/-/// touchcancel can stay pinned to the touchstart element instead of-/// re-resolving at the current point.-pub fn dispatchTouchEventOn(frame: *Frame, target: *Element, typ: TouchType, point: TouchPoint, modifiers: Modifiers) !void {- _ = try dispatchTouchEventCancelable(frame, target, typ, point, modifiers);-}--fn dispatchTouchEventCancelable(frame: *Frame, target: *Element, typ: TouchType, point: TouchPoint, modifiers: Modifiers) !bool {- const active = !typ.isLift();-- const event: *TouchEvent = try .initTrustedWithTouch(typ.name(), .{+/// Fires the touch event on `target` (no hit-test); returns whether+/// preventDefault() cancelled it.+pub fn dispatchTouchEvent(frame: *Frame, target: *Element, typ: TouchType, point: Touch.Point, modifiers: Modifiers) !bool {+ const active = typ == .touchstart or typ == .touchmove;+ const event: *TouchEvent = try .initTrustedWithTouch(@tagName(typ), .{.bubbles = true,.composed = true,.altKey = modifiers.alt,.ctrlKey = modifiers.ctrl,.metaKey = modifiers.meta,.shiftKey = modifiers.shift,- }, .{- .identifier = point.identifier,- .target = target,- .clientX = point.x,- .clientY = point.y,- .radiusX = point.radius_x,- .radiusY = point.radius_y,- .rotationAngle = point.rotation_angle,- .force = point.force,- }, active, frame);+ }, target, point, active, frame);// touchcancel is never cancelable per spec; the others follow the same// passive-listener-dependent rule as wheel (see EventManager).@@ -650,85 +679,15 @@ fn dispatchTouchEventCancelable(frame: *Frame, target: *Element, typ: TouchType,return frame._event_manager.dispatchCancelable(target.asEventTarget(), event.asEvent());}-fn dispatchTouchPointerEventOn(frame: *Frame, target: *Element, comptime typ: []const u8, contact: TouchContact, point: TouchPoint, modifiers: Modifiers, cancelled: bool) !bool {- const lift = comptime std.mem.eql(u8, typ, "pointerup") or std.mem.eql(u8, typ, "pointercancel") or- std.mem.eql(u8, typ, "pointerout") or std.mem.eql(u8, typ, "pointerleave") or std.mem.eql(u8, typ, "click");- const boundary = comptime std.mem.eql(u8, typ, "pointerenter") or std.mem.eql(u8, typ, "pointerleave");- const event: *PointerEvent = try .initTrusted(typ, .{- .bubbles = !boundary,- .cancelable = !boundary and !comptime std.mem.eql(u8, typ, "pointercancel"),- .composed = !boundary,- .clientX = point.x,- .clientY = point.y,- .button = if (cancelled or comptime std.mem.eql(u8, typ, "pointermove")) -1 else mouse_button.main,- .buttons = if (lift) 0 else 1,- .detail = if (comptime std.mem.eql(u8, typ, "click")) 1 else 0,- .pointerId = contact.pointer_id,- .pointerType = "touch",- .isPrimary = true,- .width = if (lift and !cancelled) 1 else point.radius_x * 2,- .height = if (lift and !cancelled) 1 else point.radius_y * 2,- .pressure = if (lift) 0 else point.force,- .ctrlKey = modifiers.ctrl,- .shiftKey = modifiers.shift,- .altKey = modifiers.alt,- .metaKey = modifiers.meta,- }, frame);- return frame._event_manager.dispatchCancelable(target.asEventTarget(), event.asEvent());-}--fn dispatchTouchPointerEnter(frame: *Frame, contact: TouchContact, modifiers: Modifiers) !void {- _ = try dispatchTouchPointerEventOn(frame, contact.target, "pointerover", contact, contact.point, modifiers, false);- var count: usize = 0;- var current: ?*Node = contact.target.asNode();- while (current) |node| : (current = node.parentNode()) {- if (node.is(Element) != null) count += 1;- }- while (count > 0) : (count -= 1) {- var remaining = count;- current = contact.target.asNode();- while (current) |node| : (current = node.parentNode()) {- const element = node.is(Element) orelse continue;- remaining -= 1;- if (remaining == 0) {- _ = try dispatchTouchPointerEventOn(frame, element, "pointerenter", contact, contact.point, modifiers, false);- break;- }- }- }-}--fn dispatchTouchPointerLeave(frame: *Frame, contact: TouchContact, point: TouchPoint, modifiers: Modifiers, cancelled: bool) !void {- _ = try dispatchTouchPointerEventOn(frame, contact.target, "pointerout", contact, point, modifiers, cancelled);- var current: ?*Node = contact.target.asNode();- while (current) |node| : (current = node.parentNode()) {- const element = node.is(Element) orelse continue;- _ = try dispatchTouchPointerEventOn(frame, element, "pointerleave", contact, point, modifiers, cancelled);- }-}--pub fn hasActiveTouch(frame: *Frame) bool {- return frame.page.input_touch_contact != null;-}--/// When the point misses every element (e.g. past the end of a short faux-/// layout), fall back to the document element rather than dropping the-/// contact silently, the same fallback WebDriver's pointerMove uses.-pub fn triggerTouch(frame: *Frame, typ: TouchType, point: TouchPoint, modifiers: Modifiers) !void {- return dispatchTouchContact(frame, null, typ, point, modifiers);-}--/// `target` is the touchstart element. WebDriver actions name an element and-/// have no viewport point, so they cannot hit-test. touchmove and later stay-/// pinned to the contact either way.-pub fn triggerTouchOn(frame: *Frame, target: *Element, typ: TouchType, point: TouchPoint, modifiers: Modifiers) !void {- return dispatchTouchContact(frame, target, typ, point, modifiers);-}--fn dispatchTouchContact(frame: *Frame, explicit: ?*Element, typ: TouchType, point: TouchPoint, modifiers: Modifiers) !void {+/// touchstart or touchmove. `target` names the touchstart element, for+/// WebDriver actions that have an element and no viewport point; null+/// hit-tests the point instead. A point that misses every element falls back+/// to the document element rather than dropping the contact, as WebDriver's+/// pointerMove does. touchmove stays pinned to the contact either way.+pub fn triggerTouch(frame: *Frame, target: ?*Element, typ: TouchType, point: Touch.Point, modifiers: Modifiers) !void {const page = frame.page;const pinned = if (typ == .touchstart) null else if (page.input_touch_contact) |c| c.target else null;- const resolved = pinned orelse explicit orelse+ const resolved = pinned orelse target orelse(try frame.window._document.elementFromPoint(point.x, point.y, frame)) orelseframe.window._document.getDocumentElement() orelse return;if (comptime lp.IS_DEBUG) {@@ -747,46 +706,48 @@ fn dispatchTouchContact(frame: *Frame, explicit: ?*Element, typ: TouchType, poin.pointer_id = page.input_touch_next_pointer_id,} else page.input_touch_contact orelse return;contact.point = point;+ var g = contact.gesture(point, .down, modifiers);if (typ == .touchstart) {page.input_touch_next_pointer_id = if (contact.pointer_id == std.math.maxInt(i32)) 2 else contact.pointer_id + 1;- try dispatchTouchPointerEnter(frame, contact, modifiers);- contact.suppress_mouse = try dispatchTouchPointerEventOn(frame, resolved, "pointerdown", contact, point, modifiers, false);+ dispatchBoundaryEvents(frame, null, resolved, TouchBoundary{ .g = g });+ contact.suppress_mouse = try emitPointer(frame, resolved, "pointerdown", g, 0);} else {// Keep small finger jitter tappable, but never activate after a drag.const dx = point.x - contact.start.x;const dy = point.y - contact.start.y;contact.suppress_click = contact.suppress_click or dx * dx + dy * dy > 15 * 15;- _ = try dispatchTouchPointerEventOn(frame, resolved, "pointermove", contact, point, modifiers, false);+ g.button = -1;+ _ = try emitPointer(frame, resolved, "pointermove", g, 0);}- const prevented = try dispatchTouchEventCancelable(frame, resolved, typ, point, modifiers);+ const prevented = try dispatchTouchEvent(frame, resolved, typ, point, modifiers);contact.suppress_click = contact.suppress_click or prevented;page.input_touch_contact = contact;}-/// The element compatibility mouse events hit. A caller-supplied element wins;-/// otherwise the release point is hit-tested, and a miss drops the click.-fn touchCompatTarget(frame: *Frame, point: TouchPoint, pinned: ?*Element) !?*Element {- if (pinned) |el| return el;- return frame.window._document.elementFromPoint(point.x, point.y, frame);+/// 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", .{+ .button = -1,+ .modifiers = modifiers,+ .pointer_type = "touch",+ .pointer_id = frame.page.input_touch_next_pointer_id,+ }, 0);}+/// touchend or touchcancel, then for a tap the compatibility mouse events and+/// click.+////// Playwright sends an empty touchPoints list, so there's nowhere to read a/// release position from but the stored contact. Puppeteer sends the point/// being released, and Chrome dispatches at that wire position rather than/// the last-seen one, so `point` (when given) wins over the stored/// coordinates.-pub fn triggerTouchLift(frame: *Frame, typ: TouchType, point: ?TouchPoint, modifiers: Modifiers) !void {- return triggerTouchLiftAt(frame, typ, point, modifiers, null);-}--/// Compatibility mouse events and the click land on `compat_target` instead-/// of a hit-test. WebDriver actions name an element and have no viewport point;-/// CDP uses `triggerTouchLift`, which hit-tests the release coordinate.-pub fn triggerTouchLiftOn(frame: *Frame, typ: TouchType, point: ?TouchPoint, modifiers: Modifiers, compat_target: *Element) !void {- return triggerTouchLiftAt(frame, typ, point, modifiers, compat_target);-}--fn triggerTouchLiftAt(frame: *Frame, typ: TouchType, point: ?TouchPoint, modifiers: Modifiers, compat_target: ?*Element) !void {+///+/// The compatibility events hit-test the release point, after touchend+/// listeners have had a chance to change the DOM, while pointer and touch+/// delivery stays pinned to the contact. `compat_target` replaces that+/// hit-test for WebDriver, whose actions have no viewport point.+pub fn triggerTouchLift(frame: *Frame, typ: TouchType, point: ?Touch.Point, modifiers: Modifiers, compat_target: ?*Element) !void {const contact = frame.page.input_touch_contact orelse return;// Consume the state before the fallible dispatch, so a dispatch that// fails partway through (e.g. a listener throws) can't leave a stale@@ -795,55 +756,42 @@ fn triggerTouchLiftAt(frame: *Frame, typ: TouchType, point: ?TouchPoint, modifie// The lift keeps the contact's id whatever the client re-sent, so a// released point can move the position but not rename the contact.var lift = point orelse contact.point;- lift.identifier = contact.point.identifier;- if (typ == .touchcancel) {- _ = try dispatchTouchPointerEventOn(frame, contact.target, "pointercancel", contact, lift, modifiers, true);- } else {- _ = try dispatchTouchPointerEventOn(frame, contact.target, "pointerup", contact, lift, modifiers, false);- }- try dispatchTouchPointerLeave(frame, contact, lift, modifiers, typ == .touchcancel);- const prevented = try dispatchTouchEventCancelable(frame, contact.target, typ, lift, modifiers);- if (typ == .touchcancel or contact.suppress_click or prevented) return;+ lift.id = contact.point.id;- // Compatibility mouse events use the release hit-test, after touchend- // listeners have had a chance to change the DOM. Pointer/touch delivery- // above remains pinned to the original contact target.- var target = (try touchCompatTarget(frame, lift, compat_target)) orelse return;+ const cancelled = typ == .touchcancel;+ const g = contact.gesture(lift, if (cancelled) .cancel else .up, modifiers);+ _ = try emitPointer(frame, contact.target, if (cancelled) "pointercancel" else "pointerup", g, 0);+ dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = g });+ const prevented = try dispatchTouchEvent(frame, contact.target, typ, lift, modifiers);+ if (cancelled or contact.suppress_click or prevented) return;++ var target = try touchCompatTarget(frame, lift, compat_target) orelse return;updateHoverTarget(frame, target, .{ .x = lift.x, .y = lift.y, .modifiers = modifiers, .with_pointer = false });- if (!contact.suppress_mouse) {- // Hover and move listeners can replace the element before the press.- target = (try touchCompatTarget(frame, lift, compat_target)) orelse return;- _ = try emitMouse(frame, target, "mousemove", .{ .x = lift.x, .y = lift.y, .modifiers = modifiers }, 0);- const down_target = (try touchCompatTarget(frame, lift, compat_target)) orelse return;- if (down_target.isDisabled()) return;- const suppress_focus = try emitMouse(frame, down_target, "mousedown", .{ .x = lift.x, .y = lift.y, .buttons_down = 1, .modifiers = modifiers }, 1);- if (!suppress_focus and down_target.asNode().isConnected() and !down_target.isDisabled()) {- try focusForMouseDown(frame, down_target);- }+ if (contact.suppress_mouse) {+ return mouseUpAndClick(frame, target, g, true, target);+ }++ // Each listener can replace the element under the finger, so every+ // compatibility event hit-tests again, as Chrome does.+ target = try touchCompatTarget(frame, lift, compat_target) orelse return;+ _ = try emitMouse(frame, target, "mousemove", g, 0);+ const down_target = try touchCompatTarget(frame, lift, compat_target) orelse return;+ var dg = g;+ dg.buttons_down = 1;+ try mouseDown(frame, down_target, dg);+ const up_target = try touchCompatTarget(frame, lift, compat_target) orelse return;+ // Chrome drops the click when mousedown's target is removed before the+ // release.+ const click_target = if (down_target.asNode().isConnected()) commonClickTarget(down_target, up_target) else null;+ try mouseUpAndClick(frame, up_target, g, false, click_target);+}- // Mousedown and focus/blur handlers may have removed or moved the- // pressed control. Release at the current hit-test, then click only- // the common ancestor of the connected press and release targets.- const up_target = (try touchCompatTarget(frame, lift, compat_target)) orelse return;- if (up_target.isDisabled()) return;- var click_target: ?*Element = null;- if (down_target.asNode().isConnected()) {- var current: ?*Node = down_target.asNode();- while (current) |node| : (current = node.parentNode()) {- if (node.contains(up_target.asNode())) {- click_target = node.is(Element);- break;- }- }- }- _ = try emitMouse(frame, up_target, "mouseup", .{ .x = lift.x, .y = lift.y, .modifiers = modifiers }, 1);- // Chrome retains the selected click target if mouseup removes it.- target = click_target orelse return;- }- // Recheck after mouseup too: disabling a control suppresses activation,- // including when pointerdown suppressed the compatibility mouse events.- if (target.isDisabled()) return;- _ = try dispatchTouchPointerEventOn(frame, target, "click", contact, lift, modifiers, false);+/// The element the tap's compatibility mouse events hit. A caller-supplied+/// element wins; otherwise the release point is hit-tested, and a miss drops+/// the click.+fn touchCompatTarget(frame: *Frame, point: Touch.Point, pinned: ?*Element) !?*Element {+ if (pinned) |el| return el;+ return frame.window._document.elementFromPoint(point.x, point.y, frame);}/// Whether the element has a click activation behavior that handleClickdiff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zigindex f9dab4f24..0a5bcd702 100644--- a/src/browser/webapi/WebDriver.zig+++ b/src/browser/webapi/WebDriver.zig@@ -29,7 +29,6 @@ const Element = @import("Element.zig");const EventTarget = @import("EventTarget.zig");const Cookie = @import("storage/Cookie.zig");-const PointerEvent = @import("event/PointerEvent.zig");const Label = @import("element/html/Label.zig");const log = lp.log;@@ -257,7 +256,7 @@ fn performPointerSource(source: js.Object, frame: *Frame) !void {// Touch implicitly captures the original down target.const captured = contact.target;const owner = captured.ownerFrame(frame) orelse continue;- Frame.user_input.triggerTouch(owner, .touchmove, .{ .x = 0, .y = 0 }, frame.page.input_modifiers) catch |err| {+ Frame.user_input.triggerTouch(owner, null, .touchmove, .{ .x = 0, .y = 0 }, frame.page.input_modifiers) catch |err| {log.debug(.app, "webdriver touch", .{ .err = err });};// No viewport distance, so a move whose origin element@@ -267,7 +266,9 @@ fn performPointerSource(source: js.Object, frame: *Frame) !void {}} else {const owner = el.ownerFrame(frame) orelse continue;- dispatchPointer(owner, el, "pointermove", -1, 0, "touch", frame.page.input_touch_next_pointer_id);+ Frame.user_input.touchHover(owner, el, frame.page.input_modifiers) catch |err| {+ log.debug(.app, "webdriver touch", .{ .err = err });+ };}} else {Frame.user_input.moveSequence(frame, el, .{@@ -288,7 +289,7 @@ fn performPointerSource(source: js.Object, frame: *Frame) !void {// fire another touchstart for the same finger.if (frame.page.input_touch_contact != null) continue;const owner = el.ownerFrame(frame) orelse continue;- Frame.user_input.triggerTouchOn(owner, el, .touchstart, .{ .x = 0, .y = 0 }, frame.page.input_modifiers) catch |err| {+ Frame.user_input.triggerTouch(owner, el, .touchstart, .{ .x = 0, .y = 0 }, frame.page.input_modifiers) catch |err| {log.debug(.app, "webdriver touch", .{ .err = err });};} else {@@ -305,7 +306,7 @@ fn performPointerSource(source: js.Object, frame: *Frame) !void {const contact = frame.page.input_touch_contact orelse continue;const captured = contact.target;const owner = captured.ownerFrame(frame) orelse continue;- Frame.user_input.triggerTouchLiftOn(owner, .touchend, null, frame.page.input_modifiers, captured) catch |err| {+ Frame.user_input.triggerTouchLift(owner, .touchend, null, frame.page.input_modifiers, captured) catch |err| {log.debug(.app, "webdriver touch", .{ .err = err });};} else {@@ -404,28 +405,6 @@ fn readI32(obj: js.Object, key: []const u8, default: i32) i32 {return val.toI32() catch default;}-fn dispatchPointer(frame: *Frame, el: *Element, comptime typ: []const u8, button: i32, buttons: u16, pointer_type: []const u8, pointer_id: i32) void {- const modifiers = frame.page.input_modifiers;- const event = PointerEvent.initTrusted(typ, .{- .bubbles = true,- .cancelable = true,- .composed = true,- .button = button,- .buttons = buttons,- .pointerId = pointer_id,- .pointerType = pointer_type,- .isPrimary = true,- .ctrlKey = modifiers.ctrl,- .shiftKey = modifiers.shift,- .altKey = modifiers.alt,- .metaKey = modifiers.meta,- }, frame) catch |err| {- log.debug(.app, "webdriver pointer event", .{ .err = err, .type = typ });- return;- };- dispatch(el.asEventTarget(), event.asEvent(), frame, typ);-}-// The action's x/y, which the caller already resolved the target from. An// element origin makes them offsets from its center, but the faux layout has// no center worth computing.diff --git a/src/browser/webapi/event/Touch.zig b/src/browser/webapi/event/Touch.zigindex eeca1625e..dcf1f839a 100644--- a/src/browser/webapi/event/Touch.zig+++ b/src/browser/webapi/event/Touch.zig@@ -28,17 +28,24 @@ const TouchEvent = @import("TouchEvent.zig");const Touch = @This();_event: *TouchEvent,-_identifier: i32,// EventTarget, not Element, per spec, and because dispatch retargets it// across shadow boundaries the same way it retargets Event.target (see// TouchEvent.touchTargetPtr / EventManager's AdjustedTargets)._target: ?*EventTarget,-_client_x: f64,-_client_y: f64,-_radius_x: f64,-_radius_y: f64,-_rotation_angle: f64,-_force: f64,+_point: Point,++/// One contact as the client describes it. The names and defaults are CDP's+/// Input.TouchPoint, so the protocol handler parses straight into it; the+/// defaults are Chrome's for a point that leaves them out.+pub const Point = struct {+ x: f64,+ y: f64,+ id: i32 = 0,+ radiusX: f64 = 1,+ radiusY: f64 = 1,+ rotationAngle: f64 = 0,+ force: f64 = 1,+};pub fn acquireRef(self: *Touch) void {self._event.asEvent().acquireRef();@@ -49,7 +56,7 @@ pub fn releaseRef(self: *Touch, page: *Page) void {}fn getIdentifier(self: *const Touch) i32 {- return self._identifier;+ return self._point.id;}fn getTarget(self: *const Touch) ?*EventTarget {@@ -57,45 +64,27 @@ fn getTarget(self: *const Touch) ?*EventTarget {}fn getClientX(self: *const Touch) f64 {- return self._client_x;+ return self._point.x;}fn getClientY(self: *const Touch) f64 {- return self._client_y;-}--/// This engine has no layout, so screenX/screenY and pageX/pageY alias-/// clientX/clientY, the same simplification MouseEvent.compatCoordinate makes.-fn getPageX(self: *const Touch) f64 {- return self._client_x;-}--fn getPageY(self: *const Touch) f64 {- return self._client_y;-}--fn getScreenX(self: *const Touch) f64 {- return self._client_x;-}--fn getScreenY(self: *const Touch) f64 {- return self._client_y;+ return self._point.y;}fn getRadiusX(self: *const Touch) f64 {- return self._radius_x;+ return self._point.radiusX;}fn getRadiusY(self: *const Touch) f64 {- return self._radius_y;+ return self._point.radiusY;}fn getRotationAngle(self: *const Touch) f64 {- return self._rotation_angle;+ return self._point.rotationAngle;}fn getForce(self: *const Touch) f64 {- return self._force;+ return self._point.force;}pub const JsApi = struct {@@ -111,10 +100,12 @@ pub const JsApi = struct {pub const target = bridge.accessor(Touch.getTarget, null, .{});pub const clientX = bridge.accessor(Touch.getClientX, null, .{});pub const clientY = bridge.accessor(Touch.getClientY, null, .{});- pub const pageX = bridge.accessor(Touch.getPageX, null, .{});- pub const pageY = bridge.accessor(Touch.getPageY, null, .{});- pub const screenX = bridge.accessor(Touch.getScreenX, null, .{});- pub const screenY = bridge.accessor(Touch.getScreenY, null, .{});+ // This engine has no layout, so page and screen coordinates alias the+ // client ones, the same simplification MouseEvent.compatCoordinate makes.+ pub const pageX = bridge.accessor(Touch.getClientX, null, .{});+ pub const pageY = bridge.accessor(Touch.getClientY, null, .{});+ pub const screenX = bridge.accessor(Touch.getClientX, null, .{});+ pub const screenY = bridge.accessor(Touch.getClientY, null, .{});pub const radiusX = bridge.accessor(Touch.getRadiusX, null, .{});pub const radiusY = bridge.accessor(Touch.getRadiusY, null, .{});pub const rotationAngle = bridge.accessor(Touch.getRotationAngle, null, .{});diff --git a/src/browser/webapi/event/TouchEvent.zig b/src/browser/webapi/event/TouchEvent.zigindex 113a3f176..7ab07c53a 100644--- a/src/browser/webapi/event/TouchEvent.zig+++ b/src/browser/webapi/event/TouchEvent.zig@@ -49,7 +49,7 @@ _touches_list: ?*TouchList = null,_target_touches_list: ?*TouchList = null,_changed_list: ?*TouchList = null,-pub const TouchEventOptions = struct {+const TouchEventOptions = struct {altKey: bool = false,ctrlKey: bool = false,metaKey: bool = false,@@ -61,17 +61,6 @@ pub const Options = Event.inheritOptions(TouchEventOptions,);-pub const TouchInit = struct {- identifier: i32 = 0,- target: *Element,- clientX: f64,- clientY: f64,- radiusX: f64 = 1,- radiusY: f64 = 1,- rotationAngle: f64 = 0,- force: f64 = 1,-};-pub fn init(typ: []const u8, _opts: ?Options, frame: *Frame) !*TouchEvent {return initWithTrusted(typ, _opts, false, frame);}@@ -82,19 +71,9 @@ pub fn initTrusted(typ: []const u8, _opts: ?Options, frame: *Frame) !*TouchEvent/// Assigning the touch is a plain value write (no arena allocation), so/// nothing can fail between creating the event and returning it.-pub fn initTrustedWithTouch(typ: []const u8, _opts: ?Options, touch_init: TouchInit, active: bool, frame: *Frame) !*TouchEvent {+pub fn initTrustedWithTouch(typ: []const u8, _opts: ?Options, target: *Element, point: Touch.Point, active: bool, frame: *Frame) !*TouchEvent {const event = try initWithTrusted(typ, _opts, true, frame);- event._touch = .{- ._event = event,- ._identifier = touch_init.identifier,- ._target = touch_init.target.asEventTarget(),- ._client_x = touch_init.clientX,- ._client_y = touch_init.clientY,- ._radius_x = touch_init.radiusX,- ._radius_y = touch_init.radiusY,- ._rotation_angle = touch_init.rotationAngle,- ._force = touch_init.force,- };+ event._touch = .{ ._event = event, ._target = target.asEventTarget(), ._point = point };event._touch_active = active;return event;}diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex 9a092591e..635f9543f 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -20,6 +20,7 @@ const std = @import("std");const CDP = @import("../CDP.zig");const Frame = @import("../../../browser/Frame.zig");const Element = @import("../../../browser/webapi/Element.zig");+const Touch = @import("../../../browser/webapi/event/Touch.zig");const dom_button = Frame.user_input.mouse_button;@@ -169,26 +170,15 @@ fn dispatchMouseEvent(cmd: *CDP.Command) !void {/// contact exists, but it is this engine's rule and not Chrome's.////// Puppeteer's ids start at 1, so the contact keeps the id from touchStart-/// instead of assuming 0.+/// instead of assuming 0. Touch.identifier is a DOM i32, so a fractional or+/// out-of-range id fails to parse and lands on the same InvalidParams a bad+/// shape does. Chrome instead truncates, and overflows to INT_MIN past the+/// range, which is not worth matching.fn dispatchTouchEvent(cmd: *CDP.Command) !void {const params = (cmd.params(struct {type: Type,modifiers: u4 = 0,- touchPoints: []const struct {- x: f64,- y: f64,- // Touch.identifier is a DOM i32, so a fractional or out-of-range- // id fails to parse and lands on the same InvalidParams a bad- // shape does. Chrome instead truncates, and overflows to- // INT_MIN past the range, which is not worth matching.- id: i32 = 0,- // Puppeteer sends 0.5 for all three on every tap, so a page- // reading pressure sees the client's value and not a default.- radiusX: f64 = 1,- radiusY: f64 = 1,- rotationAngle: f64 = 0,- force: f64 = 1,- },+ touchPoints: []const Touch.Point,const Type = enum {touchStart,@@ -218,11 +208,11 @@ fn dispatchTouchEvent(cmd: *CDP.Command) !void {// contact is still active would overwrite it, losing the original// target/coordinates and reporting two starts for one contact..touchStart => {- if (Frame.user_input.hasActiveTouch(frame)) return error.InvalidParams;+ if (frame.page.input_touch_contact != null) return error.InvalidParams;},.touchMove, .touchEnd, .touchCancel => {const contact = frame.page.input_touch_contact orelse return error.InvalidParams;- if (params.touchPoints.len == 1 and params.touchPoints[0].id != contact.point.identifier) {+ if (params.touchPoints.len == 1 and params.touchPoints[0].id != contact.point.id) {return error.InvalidParams;}},@@ -233,26 +223,14 @@ fn dispatchTouchEvent(cmd: *CDP.Command) !void {// Puppeteer's touchEnd carries the point being released, and Chrome// dispatches there rather than at the last touchmove; Playwright's empty// list and touchCancel fall back to the stored contact.- const lift_point: ?Frame.user_input.TouchPoint = if (params.touchPoints.len == 1) p: {- const wire = params.touchPoints[0];- break :p .{- .x = wire.x,- .y = wire.y,- .identifier = wire.id,- .radius_x = wire.radiusX,- .radius_y = wire.radiusY,- .rotation_angle = wire.rotationAngle,- .force = wire.force,- };- } else null;-+ const point: ?Touch.Point = if (params.touchPoints.len == 1) params.touchPoints[0] else null;const modifiers = cdpModifiers(params.modifiers);switch (params.type) {- .touchStart => try Frame.user_input.triggerTouch(frame, .touchstart, lift_point.?, modifiers),- .touchMove => try Frame.user_input.triggerTouch(frame, .touchmove, lift_point.?, modifiers),- .touchEnd => try Frame.user_input.triggerTouchLift(frame, .touchend, lift_point, modifiers),- .touchCancel => try Frame.user_input.triggerTouchLift(frame, .touchcancel, lift_point, modifiers),+ .touchStart => try Frame.user_input.triggerTouch(frame, null, .touchstart, point.?, modifiers),+ .touchMove => try Frame.user_input.triggerTouch(frame, null, .touchmove, point.?, modifiers),+ .touchEnd => try Frame.user_input.triggerTouchLift(frame, .touchend, point, modifiers, null),+ .touchCancel => try Frame.user_input.triggerTouchLift(frame, .touchcancel, point, modifiers, null),}}@@ -1659,14 +1637,14 @@ test "cdp.input: dispatchTouchEvent a touchStart past every element still opens.method = "Input.dispatchTouchEvent",.params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = 0, .y = 3000 }} },});- try testing.expect(Frame.user_input.hasActiveTouch(frame));+ try testing.expect(frame.page.input_touch_contact != null);try ctx.processMessage(.{.id = 2,.method = "Input.dispatchTouchEvent",.params = .{ .type = "touchEnd", .touchPoints = &.{} },});- try testing.expect(!Frame.user_input.hasActiveTouch(frame));+ try testing.expect(frame.page.input_touch_contact == null);}test "cdp.input: dispatchTouchEvent empty touchStart is InvalidParams" {@@ -1771,7 +1749,7 @@ test "cdp.input: dispatchTouchEvent tracks the client's chosen id and rejects mi.params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = x, .y = y, .id = 0.5 }} },});try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 1 });- try testing.expect(!Frame.user_input.hasActiveTouch(frame));+ try testing.expect(frame.page.input_touch_contact == null);// Puppeteer's id generator starts at 1 and increments; any integer id// (including one Chrome would also accept, like a negative one) opens@@ -1781,7 +1759,7 @@ test "cdp.input: dispatchTouchEvent tracks the client's chosen id and rejects mi.method = "Input.dispatchTouchEvent",.params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = x, .y = y, .id = 7 }} },});- try testing.expectEqual(@as(i32, 7), frame.page.input_touch_contact.?.point.identifier);+ try testing.expectEqual(@as(i32, 7), frame.page.input_touch_contact.?.point.id);// A touchMove for a different id than the open contact is rejected and// leaves the contact untouched.@@ -1808,7 +1786,7 @@ test "cdp.input: dispatchTouchEvent tracks the client's chosen id and rejects mi.params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x + 1, .y = y + 2, .id = 8 }} },});try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 5 });- try testing.expect(Frame.user_input.hasActiveTouch(frame));+ try testing.expect(frame.page.input_touch_contact != null);// touchEnd with the matching id (Puppeteer) lifts the contact.try ctx.processMessage(.{@@ -1816,7 +1794,7 @@ test "cdp.input: dispatchTouchEvent tracks the client's chosen id and rejects mi.method = "Input.dispatchTouchEvent",.params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = x + 1, .y = y + 2, .id = 7 }} },});- try testing.expect(!Frame.user_input.hasActiveTouch(frame));+ 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],@@ -1931,7 +1909,7 @@ test "cdp.input: dispatchTouchEvent a touchEnd carrying the released point is ac.method = "Input.dispatchTouchEvent",.params = .{ .type = "touchEnd", .touchPoints = &.{.{ .x = end_x, .y = end_y }} },});- try testing.expect(!Frame.user_input.hasActiveTouch(frame));+ try testing.expect(frame.page.input_touch_contact == null);const result = try ls.local.compileAndRun("window.endTarget === true && window.endX === " ++ "document.getElementById('hoverTarget').getBoundingClientRect().x + 5" ++@@ -1946,7 +1924,7 @@ test "cdp.input: dispatchTouchEvent a touchEnd carrying the released point is ac.method = "Input.dispatchTouchEvent",.params = .{ .type = "touchStart", .touchPoints = &.{.{ .x = rect_x, .y = rect_y }} },});- try testing.expect(Frame.user_input.hasActiveTouch(frame));+ try testing.expect(frame.page.input_touch_contact != null);}// Playwright's empty touchPoints list has no position to read: the lift@@ -2036,14 +2014,14 @@ test "cdp.input: dispatchTouchEvent rejects populated touchCancel without liftin.params = .{ .type = "touchCancel", .touchPoints = &.{.{ .x = cancel_x, .y = cancel_y }} },});try ctx.expectSentError(-31998, "InvalidParams", .{ .id = 2 });- try testing.expect(Frame.user_input.hasActiveTouch(frame));+ try testing.expect(frame.page.input_touch_contact != null);try ctx.processMessage(.{.id = 3,.method = "Input.dispatchTouchEvent",.params = .{ .type = "touchCancel", .touchPoints = &.{} },});- try testing.expect(!Frame.user_input.hasActiveTouch(frame));+ try testing.expect(frame.page.input_touch_contact == null);const result = try ls.local.compileAndRun("window.cancelable === false && window.cancelX === " ++ "document.getElementById('hoverTarget').getBoundingClientRect().x" ++@@ -2086,7 +2064,7 @@ test "cdp.input: dispatchTouchEvent a multi-point touchEnd/touchCancel is Invalitry ctx.expectSentError(-31998, "InvalidParams", .{ .id = 2 });}-// The next four tests call Frame.user_input.dispatchTouchEventOn directly+// The next four tests call Frame.user_input.dispatchTouchEvent directly// (bypassing CDP message dispatch) to exercise per-type event shape, which// Input.dispatchTouchEvent doesn't itself vary by target.test "cdp.input: dispatchTouchEvent touchcancel is never cancelable" {@@ -2109,7 +2087,7 @@ test "cdp.input: dispatchTouchEvent touchcancel is never cancelable" {\\t;, null);const target = try ls.local.jsValueToZig(*Element, value);- try Frame.user_input.dispatchTouchEventOn(frame, target, .touchcancel, .{ .x = 10, .y = 20 }, .{});+ _ = try Frame.user_input.dispatchTouchEvent(frame, target, .touchcancel, .{ .x = 10, .y = 20 }, .{});try testing.expect((try ls.local.compileAndRun("window.result === true", null)).isTrue());}@@ -2130,7 +2108,7 @@ test "cdp.input: dispatchTouchEvent a passive-only touchstart listener is not ca\\t;, null);const target = try ls.local.jsValueToZig(*Element, value);- try Frame.user_input.dispatchTouchEventOn(frame, target, .touchstart, .{ .x = 10, .y = 20 }, .{});+ _ = try Frame.user_input.dispatchTouchEvent(frame, target, .touchstart, .{ .x = 10, .y = 20 }, .{});try testing.expect((try ls.local.compileAndRun("window.result === true", null)).isTrue());}@@ -2151,7 +2129,7 @@ test "cdp.input: dispatchTouchEvent a non-passive listener makes touchstart canc\\t;, null);const target = try ls.local.jsValueToZig(*Element, value);- try Frame.user_input.dispatchTouchEventOn(frame, target, .touchstart, .{ .x = 10, .y = 20 }, .{});+ _ = try Frame.user_input.dispatchTouchEvent(frame, target, .touchstart, .{ .x = 10, .y = 20 }, .{});try testing.expect((try ls.local.compileAndRun("window.result === true", null)).isTrue());}@@ -2179,7 +2157,7 @@ test "cdp.input: dispatchTouchEvent a closed-shadow touch target retargets to th\\inner;, null);const target = try ls.local.jsValueToZig(*Element, value);- try Frame.user_input.dispatchTouchEventOn(frame, target, .touchstart, .{ .x = 10, .y = 20 }, .{});+ _ = try Frame.user_input.dispatchTouchEvent(frame, target, .touchstart, .{ .x = 10, .y = 20 }, .{});const result = try ls.local.compileAndRun("window.eventTargetOK === true && window.touchTargetOK === true", null);try testing.expect(result.isTrue());}@@ -2192,11 +2170,7 @@ test "cdp.input: dispatchTouchEvent repeated touches reads have bounded arena stconst frame = page.frame().?;const target = frame.document.getDocumentElement().?;- const event = try TouchEvent.initTrustedWithTouch("touchstart", null, .{- .target = target,- .clientX = 10,- .clientY = 20,- }, true, frame);+ const event = try TouchEvent.initTrustedWithTouch("touchstart", null, target, .{ .x = 10, .y = 20 }, true, frame);event.asEvent().acquireRef();defer event.asEvent().releaseRef(frame.page);