Lightpanda: the headless browser designed for AI and automation
webapi: accept arbitrary button/location values; legacy keyboard init members
Francis Bouvier5ebc61907ec7
diff --git a/src/browser/webapi/event/DragEvent.zig b/src/browser/webapi/event/DragEvent.zigindex fe0def984..bbca62c9d 100644--- a/src/browser/webapi/event/DragEvent.zig+++ b/src/browser/webapi/event/DragEvent.zig@@ -69,7 +69,7 @@ fn initWithTrusted(typ: []const u8, _opts: ?Options, trusted: bool, frame: *Fram._shift_key = opts.shiftKey,._alt_key = opts.altKey,._meta_key = opts.metaKey,- ._button = std.meta.intToEnum(MouseEvent.MouseButton, opts.button) catch return error.TypeError,+ ._button = @truncate(opts.button),._buttons = opts.buttons,._related_target = opts.relatedTarget,},diff --git a/src/browser/webapi/event/KeyboardEvent.zig b/src/browser/webapi/event/KeyboardEvent.zigindex cd3c44787..b6fa83433 100644--- a/src/browser/webapi/event/KeyboardEvent.zig+++ b/src/browser/webapi/event/KeyboardEvent.zig@@ -37,9 +37,14 @@ _ctrl_key: bool,_shift_key: bool,_alt_key: bool,_meta_key: bool,-_location: Location,+// Per spec an unsigned long; the Location enum only names the+// DOM_KEY_LOCATION_* constants, any value is allowed.+_location: u32,_repeat: bool,_is_composing: bool,+// Legacy KeyboardEventInit values, reported as-is by synthetic events.+_char_code_init: u32 = 0,+_key_code_init: u32 = 0,// https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_valuespub const Key = union(enum) {@@ -272,13 +277,16 @@ pub const Location = enum(i32) {pub const KeyboardEventOptions = struct {key: []const u8 = "",code: ?[]const u8 = null,- location: i32 = 0,+ location: u32 = 0,repeat: bool = false,isComposing: bool = false,ctrlKey: bool = false,shiftKey: bool = false,altKey: bool = false,metaKey: bool = false,+ // Legacy KeyboardEventInit members: returned as-is by synthetic events.+ charCode: u32 = 0,+ keyCode: u32 = 0,};const Options = Event.inheritOptions(@@ -308,7 +316,7 @@ fn initWithTrusted(arena: Allocator, typ: String, _opts: ?Options, trusted: boolKeyboardEvent{._proto = undefined,._key = try Key.fromString(arena, opts.key),- ._location = std.meta.intToEnum(Location, opts.location) catch return error.TypeError,+ ._location = opts.location,._code = if (opts.code) |c| try arena.dupe(u8, c) else "",._repeat = opts.repeat,._is_composing = opts.isComposing,@@ -316,6 +324,8 @@ fn initWithTrusted(arena: Allocator, typ: String, _opts: ?Options, trusted: bool._shift_key = opts.shiftKey,._alt_key = opts.altKey,._meta_key = opts.metaKey,+ ._char_code_init = opts.charCode,+ ._key_code_init = opts.keyCode,},);@@ -358,8 +368,8 @@ pub fn getCode(self: *const KeyboardEvent) []const u8 {return self._code;}-pub fn getLocation(self: *const KeyboardEvent) i32 {- return @intFromEnum(self._location);+pub fn getLocation(self: *const KeyboardEvent) u32 {+ return self._location;}pub fn getMetaKey(self: *const KeyboardEvent) bool {@@ -384,7 +394,8 @@ pub fn getShiftKey(self: *const KeyboardEvent) bool {// gate on `_is_trusted` to match.pub fn getCharCode(self: *const KeyboardEvent) u32 {const event = self._proto._proto;- if (event._is_trusted == false) return 0;+ // Synthetic events report the (legacy) KeyboardEventInit value.+ if (event._is_trusted == false) return self._char_code_init;if (event._type_string.eql(comptime .wrap("keypress")) == false) return 0;return self._key.charCode();}@@ -393,7 +404,8 @@ pub fn getCharCode(self: *const KeyboardEvent) u32 {//// As with `charCode`, Chrome returns 0 for synthetic events.pub fn getKeyCode(self: *const KeyboardEvent) u32 {- if (self._proto._proto._is_trusted == false) return 0;+ // Synthetic events report the (legacy) KeyboardEventInit value.+ if (self._proto._proto._is_trusted == false) return self._key_code_init;return self._key.keyCode();}@@ -422,7 +434,7 @@ pub fn initKeyboardEvent(event._cancelable = cancelable orelse false;ui._view = view;self._key = try Key.fromString(arena, key orelse "");- self._location = std.meta.intToEnum(Location, location orelse 0) catch return error.TypeError;+ self._location = location orelse 0;self._ctrl_key = ctrl_key orelse false;self._alt_key = alt_key orelse false;self._shift_key = shift_key orelse false;diff --git a/src/browser/webapi/event/MouseEvent.zig b/src/browser/webapi/event/MouseEvent.zigindex 04a07ffed..eac879547 100644--- a/src/browser/webapi/event/MouseEvent.zig+++ b/src/browser/webapi/event/MouseEvent.zig@@ -52,7 +52,9 @@ _type: Type,_proto: *UIEvent,_alt_key: bool,-_button: MouseButton,+// Per spec a short; the MouseButton enum only names the standard buttons,+// any value is allowed.+_button: i16,_buttons: u16,_client_x: f64,_client_y: f64,@@ -113,7 +115,7 @@ fn initWithTrusted(arena: Allocator, typ: String, _opts: ?Options, trusted: bool._shift_key = opts.shiftKey,._alt_key = opts.altKey,._meta_key = opts.metaKey,- ._button = std.meta.intToEnum(MouseButton, opts.button) catch return error.TypeError,+ ._button = @truncate(opts.button),._buttons = opts.buttons,._related_target = opts.relatedTarget,},@@ -145,8 +147,8 @@ pub fn getAltKey(self: *const MouseEvent) bool {return self._alt_key;}-pub fn getButton(self: *const MouseEvent) u8 {- return @intFromEnum(self._button);+pub fn getButton(self: *const MouseEvent) i16 {+ return self._button;}pub fn getButtons(self: *const MouseEvent) u16 {@@ -250,7 +252,7 @@ pub fn initMouseEvent(self._alt_key = alt_key orelse false;self._shift_key = shift_key orelse false;self._meta_key = meta_key orelse false;- self._button = std.meta.intToEnum(MouseButton, button orelse 0) catch return error.TypeError;+ self._button = button orelse 0;self._related_target = related_target;}diff --git a/src/browser/webapi/event/PointerEvent.zig b/src/browser/webapi/event/PointerEvent.zigindex 4d0897d75..d1c082798 100644--- a/src/browser/webapi/event/PointerEvent.zig+++ b/src/browser/webapi/event/PointerEvent.zig@@ -113,7 +113,7 @@ fn initWithTrusted(typ: []const u8, _opts: ?Options, trusted: bool, frame: *Fram._shift_key = opts.shiftKey,._alt_key = opts.altKey,._meta_key = opts.metaKey,- ._button = std.meta.intToEnum(MouseEvent.MouseButton, opts.button) catch return error.TypeError,+ ._button = @truncate(opts.button),._buttons = opts.buttons,._related_target = opts.relatedTarget,},diff --git a/src/browser/webapi/event/UIEvent.zig b/src/browser/webapi/event/UIEvent.zigindex e66afd7ad..c5145e7f7 100644--- a/src/browser/webapi/event/UIEvent.zig+++ b/src/browser/webapi/event/UIEvent.zig@@ -124,8 +124,11 @@ pub fn getView(self: *UIEvent, frame: *Frame) ?*Window {// Legacy: see https://w3c.github.io/uievents/#dom-uievent-whichpub fn getWhich(self: *const UIEvent) u32 {return switch (self._type) {- .mouse_event => |me| @as(u32, @intCast(me.getButton())) + 1,- .keyboard_event => 0,+ .mouse_event => |me| blk: {+ const button = me.getButton();+ break :blk if (button < 0) 0 else @as(u32, @intCast(button)) + 1;+ },+ .keyboard_event => |ke| ke.getKeyCode(),else => 0,};}diff --git a/src/browser/webapi/event/WheelEvent.zig b/src/browser/webapi/event/WheelEvent.zigindex e0e6b3914..89d5bef5f 100644--- a/src/browser/webapi/event/WheelEvent.zig+++ b/src/browser/webapi/event/WheelEvent.zig@@ -80,7 +80,7 @@ fn initWithTrusted(typ: []const u8, _opts: ?Options, trusted: bool, frame: *Fram._shift_key = opts.shiftKey,._alt_key = opts.altKey,._meta_key = opts.metaKey,- ._button = std.meta.intToEnum(MouseEvent.MouseButton, opts.button) catch return error.TypeError,+ ._button = @truncate(opts.button),._buttons = opts.buttons,._related_target = opts.relatedTarget,},