Lightpanda: the headless browser designed for AI and automation
input: one WebDriver key table and dispatch for BiDi, testdriver and press
Adrià Arrufatf36cbc2c9678
diff --git a/src/browser/actions.zig b/src/browser/actions.zigindex d88a686b0..d8e599916 100644--- a/src/browser/actions.zig+++ b/src/browser/actions.zig@@ -22,8 +22,8 @@ const DOMNode = @import("webapi/Node.zig");const Element = @import("webapi/Element.zig");const Event = @import("webapi/Event.zig");const MouseEvent = @import("webapi/event/MouseEvent.zig");-const KeyboardEvent = @import("webapi/event/KeyboardEvent.zig");const Frame = @import("Frame.zig");+const keyboard = @import("frame/keyboard.zig");const Session = @import("Session.zig");pub fn dispatchInputAndChangeEvents(el: *Element, frame: *Frame) !void {@@ -76,56 +76,36 @@ pub fn press(node: ?*DOMNode, key: []const u8, frame: *Frame) !void {(n.is(Element) orelse return error.InvalidNodeType)elseFrame.user_input.focusedElement(frame) orelse return error.ActionFailed;- const canonical = canonicalKey(key);+ const info = keyboard.named(alias(key));- const keydown_event: *KeyboardEvent = try .initTrusted(comptime .wrap("keydown"), .{- .bubbles = true,- .cancelable = true,- .composed = true,- .key = canonical,- }, frame);-- _ = Frame.user_input.pressKey(frame, target, keydown_event, Frame.user_input.textForKey(keydown_event)) catch |err| {- lp.log.debug(.app, "press keydown failed", .{ .err = err });- return error.ActionFailed;- };-- const keyup_event: *KeyboardEvent = try .initTrusted(comptime .wrap("keyup"), .{- .bubbles = true,- .cancelable = true,- .composed = true,- .key = canonical,- }, frame);-- frame._event_manager.dispatch(target.asEventTarget(), keyup_event.asEvent()) catch |err| {- lp.log.debug(.app, "press keyup failed", .{ .err = err });- return error.ActionFailed;- };+ for ([_]keyboard.Direction{ .down, .up }) |direction| {+ keyboard.dispatch(frame, target, direction, &info, &.{}) catch |err| {+ lp.log.debug(.app, "press failed", .{ .err = err, .direction = direction });+ return error.ActionFailed;+ };+ }}-/// Map common shorthand to the canonical KeyboardEvent.key string so users-/// can type "enter" instead of "Enter" without surprises.-fn canonicalKey(key: []const u8) []const u8 {- const aliases = [_]struct { in: []const u8, out: []const u8 }{- .{ .in = "enter", .out = "Enter" },- .{ .in = "return", .out = "Enter" },- .{ .in = "\n", .out = "Enter" },- .{ .in = "\\n", .out = "Enter" },- .{ .in = "esc", .out = "Escape" },- .{ .in = "escape", .out = "Escape" },- .{ .in = "tab", .out = "Tab" },- .{ .in = "\t", .out = "Tab" },- .{ .in = "space", .out = " " },- .{ .in = "backspace", .out = "Backspace" },- .{ .in = "delete", .out = "Delete" },- .{ .in = "del", .out = "Delete" },- .{ .in = "up", .out = "ArrowUp" },- .{ .in = "down", .out = "ArrowDown" },- .{ .in = "left", .out = "ArrowLeft" },- .{ .in = "right", .out = "ArrowRight" },+/// `keyboard.named` takes WebDriver's own key names, in any case.+fn alias(key: []const u8) []const u8 {+ const aliases = [_]struct { []const u8, []const u8 }{+ .{ "return", "Enter" },+ .{ "\n", "Enter" },+ .{ "\\n", "Enter" },+ .{ "esc", "Escape" },+ .{ "\t", "Tab" },+ .{ "space", " " },+ .{ "del", "Delete" },+ .{ "up", "ArrowUp" },+ .{ "down", "ArrowDown" },+ .{ "left", "ArrowLeft" },+ .{ "right", "ArrowRight" },};- for (aliases) |a| {- if (std.ascii.eqlIgnoreCase(key, a.in)) return a.out;+ for (aliases) |entry| {+ const from, const to = entry;+ if (std.ascii.eqlIgnoreCase(key, from)) {+ return to;+ }}return key;}diff --git a/src/browser/frame/keyboard.zig b/src/browser/frame/keyboard.zignew file mode 100644index 000000000..9d26c7026--- /dev/null+++ b/src/browser/frame/keyboard.zig@@ -0,0 +1,309 @@+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)+//+// Francis Bouvier <[email protected]>+// Pierre Tachoire <[email protected]>+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++// WebDriver's key table and key events, shared by BiDi, testdriver and the+// agent's press.++const std = @import("std");+const lp = @import("lightpanda");++const Frame = @import("../Frame.zig");+const Element = @import("../webapi/Element.zig");+const KeyboardEvent = @import("../webapi/event/KeyboardEvent.zig");+const user_input = @import("user_input.zig");++const Modifiers = user_input.Modifiers;++pub const Direction = enum { down, up };++pub fn keyAction(frame: *Frame, cp: u21, direction: Direction, modifiers: *Modifiers) !void {+ const info = webdriverKey(cp, modifiers.shift);+ // A modifier's own keydown already carries its flag; its keyup no longer+ // does.+ setModifier(modifiers, info.modifier, direction == .down);+ return dispatch(frame, null, direction, &info, modifiers);+}++/// Without a `target`, each event goes to the element focused at that moment:+/// a key's default action can move focus.+pub fn dispatch(frame: *Frame, target: ?*Element, direction: Direction, info: *const KeyInfo, modifiers: *const Modifiers) !void {+ var buf: [4]u8 = undefined;+ const name: []const u8 = switch (info.key) {+ .name => |n| n,+ .char => |cp| buf[0 .. std.unicode.utf8Encode(cp, &buf) catch unreachable],+ };+ const typ: lp.String = switch (direction) {+ .down => comptime .wrap("keydown"),+ .up => comptime .wrap("keyup"),+ };+ const event = try KeyboardEvent.initTrusted(typ, .{+ .key = name,+ .code = info.code,+ .location = info.location,+ .altKey = modifiers.alt,+ .ctrlKey = modifiers.ctrl,+ .metaKey = modifiers.meta,+ .shiftKey = modifiers.shift,+ }, frame);+ const text = user_input.textForKey(event);+ switch (direction) {+ .down => _ = try if (target) |t| user_input.pressKey(frame, t, event, text) else user_input.triggerKeyDown(frame, event, text),+ .up => try if (target) |t| frame._event_manager.dispatch(t.asEventTarget(), event.asEvent()) else user_input.triggerKeyUp(frame, event),+ }+}++pub fn singleCodepoint(value: []const u8) ?u21 {+ if (value.len == 0) {+ return null;+ }+ const len = std.unicode.utf8ByteSequenceLength(value[0]) catch return null;+ if (value.len != len) {+ return null;+ }+ return std.unicode.utf8Decode(value) catch null;+}++fn setModifier(modifiers: *Modifiers, which: ?Modifier, down: bool) void {+ switch (which orelse return) {+ .alt => modifiers.alt = down,+ .ctrl => modifiers.ctrl = down,+ .meta => modifiers.meta = down,+ .shift => modifiers.shift = down,+ }+}++pub const Modifier = enum { alt, ctrl, meta, shift };++pub const KeyInfo = struct {+ // a named key, or the character itself+ key: union(enum) { name: []const u8, char: u21 },+ code: []const u8,+ location: u32 = 0,+ modifier: ?Modifier = null,+};++// WebDriver's key table: the Private Use Area - names the+// non-printing keys, anything else is the character itself.+// https://w3c.github.io/webdriver/#keyboard-actions+pub fn webdriverKey(cp: u21, shift: bool) KeyInfo {+ if (cp >= 0xE000 and cp <= 0xE05D) {+ return specialKey(cp);+ }++ const char: u21 = if (shift and cp < 128) shiftedAscii(@intCast(cp)) else cp;+ return .{ .key = .{ .char = char }, .code = asciiCode(cp) };+}++/// Where the numpad shares a name, the main-keyboard key wins.+pub fn named(name: []const u8) KeyInfo {+ if (singleCodepoint(name)) |cp| {+ return webdriverKey(cp, false);+ }+ var cp: u21 = 0xE000;+ while (cp <= 0xE05D) : (cp += 1) {+ const info = specialKey(cp);+ if (std.ascii.eqlIgnoreCase(info.key.name, name)) {+ return info;+ }+ }+ return .{ .key = .{ .name = name }, .code = "" };+}++fn specialKey(cp: u21) KeyInfo {+ const k = struct {+ fn k(key: []const u8, code: []const u8) KeyInfo {+ return .{ .key = .{ .name = key }, .code = code };+ }+ fn m(key: []const u8, code: []const u8, location: u32, modifier: Modifier) KeyInfo {+ return .{ .key = .{ .name = key }, .code = code, .location = location, .modifier = modifier };+ }+ fn n(key: []const u8, code: []const u8) KeyInfo {+ return .{ .key = .{ .name = key }, .code = code, .location = 3 };+ }+ };+ return switch (cp) {+ 0xE000 => k.k("Unidentified", ""),+ 0xE001 => k.k("Cancel", "Abort"),+ 0xE002 => k.k("Help", "Help"),+ 0xE003 => k.k("Backspace", "Backspace"),+ 0xE004 => k.k("Tab", "Tab"),+ 0xE005 => k.k("Clear", "NumLock"),+ 0xE006 => k.k("Enter", "Enter"),+ 0xE007 => k.n("Enter", "NumpadEnter"),+ 0xE008 => k.m("Shift", "ShiftLeft", 1, .shift),+ 0xE009 => k.m("Control", "ControlLeft", 1, .ctrl),+ 0xE00A => k.m("Alt", "AltLeft", 1, .alt),+ 0xE00B => k.k("Pause", "Pause"),+ 0xE00C => k.k("Escape", "Escape"),+ 0xE00D => k.k(" ", "Space"),+ 0xE00E => k.k("PageUp", "PageUp"),+ 0xE00F => k.k("PageDown", "PageDown"),+ 0xE010 => k.k("End", "End"),+ 0xE011 => k.k("Home", "Home"),+ 0xE012 => k.k("ArrowLeft", "ArrowLeft"),+ 0xE013 => k.k("ArrowUp", "ArrowUp"),+ 0xE014 => k.k("ArrowRight", "ArrowRight"),+ 0xE015 => k.k("ArrowDown", "ArrowDown"),+ 0xE016 => k.k("Insert", "Insert"),+ 0xE017 => k.k("Delete", "Delete"),+ 0xE018 => k.k(";", "Semicolon"),+ 0xE019 => k.k("=", "Equal"),+ 0xE01A => k.n("0", "Numpad0"),+ 0xE01B => k.n("1", "Numpad1"),+ 0xE01C => k.n("2", "Numpad2"),+ 0xE01D => k.n("3", "Numpad3"),+ 0xE01E => k.n("4", "Numpad4"),+ 0xE01F => k.n("5", "Numpad5"),+ 0xE020 => k.n("6", "Numpad6"),+ 0xE021 => k.n("7", "Numpad7"),+ 0xE022 => k.n("8", "Numpad8"),+ 0xE023 => k.n("9", "Numpad9"),+ 0xE024 => k.n("*", "NumpadMultiply"),+ 0xE025 => k.n("+", "NumpadAdd"),+ 0xE026 => k.n(",", "NumpadComma"),+ 0xE027 => k.n("-", "NumpadSubtract"),+ 0xE028 => k.n(".", "NumpadDecimal"),+ 0xE029 => k.n("/", "NumpadDivide"),+ 0xE031 => k.k("F1", "F1"),+ 0xE032 => k.k("F2", "F2"),+ 0xE033 => k.k("F3", "F3"),+ 0xE034 => k.k("F4", "F4"),+ 0xE035 => k.k("F5", "F5"),+ 0xE036 => k.k("F6", "F6"),+ 0xE037 => k.k("F7", "F7"),+ 0xE038 => k.k("F8", "F8"),+ 0xE039 => k.k("F9", "F9"),+ 0xE03A => k.k("F10", "F10"),+ 0xE03B => k.k("F11", "F11"),+ 0xE03C => k.k("F12", "F12"),+ 0xE03D => k.m("Meta", "MetaLeft", 1, .meta),+ 0xE040 => k.k("ZenkakuHankaku", ""),+ 0xE050 => k.m("Shift", "ShiftRight", 2, .shift),+ 0xE051 => k.m("Control", "ControlRight", 2, .ctrl),+ 0xE052 => k.m("Alt", "AltRight", 2, .alt),+ 0xE053 => k.m("Meta", "MetaRight", 2, .meta),+ 0xE054 => k.n("PageUp", "Numpad9"),+ 0xE055 => k.n("PageDown", "Numpad3"),+ 0xE056 => k.n("End", "Numpad1"),+ 0xE057 => k.n("Home", "Numpad7"),+ 0xE058 => k.n("ArrowLeft", "Numpad4"),+ 0xE059 => k.n("ArrowUp", "Numpad8"),+ 0xE05A => k.n("ArrowRight", "Numpad6"),+ 0xE05B => k.n("ArrowDown", "Numpad2"),+ 0xE05C => k.n("Insert", "Numpad0"),+ 0xE05D => k.n("Delete", "NumpadDecimal"),+ else => k.k("Unidentified", ""),+ };+}++// slices into this literal are always valid+const key_codes = "KeyAKeyBKeyCKeyDKeyEKeyFKeyGKeyHKeyIKeyJKeyKKeyLKeyMKeyNKeyOKeyPKeyQKeyRKeySKeyTKeyUKeyVKeyWKeyXKeyYKeyZ";+const digit_codes = "Digit0Digit1Digit2Digit3Digit4Digit5Digit6Digit7Digit8Digit9";++// The `code` of a printable character on a US layout.+fn asciiCode(cp: u21) []const u8 {+ if (cp >= 128) {+ return "";+ }+ const c: u8 = @intCast(cp);+ return switch (c) {+ 'a'...'z' => key_codes[(c - 'a') * 4 ..][0..4],+ 'A'...'Z' => key_codes[(c - 'A') * 4 ..][0..4],+ '0'...'9' => digit_codes[(c - '0') * 6 ..][0..6],+ ' ' => "Space",+ '\n', '\r' => "Enter",+ '\t' => "Tab",+ '`', '~' => "Backquote",+ '-', '_' => "Minus",+ '=', '+' => "Equal",+ '[', '{' => "BracketLeft",+ ']', '}' => "BracketRight",+ '\\', '|' => "Backslash",+ ';', ':' => "Semicolon",+ '\'', '"' => "Quote",+ ',', '<' => "Comma",+ '.', '>' => "Period",+ '/', '?' => "Slash",+ '!' => "Digit1",+ '@' => "Digit2",+ '#' => "Digit3",+ '$' => "Digit4",+ '%' => "Digit5",+ '^' => "Digit6",+ '&' => "Digit7",+ '*' => "Digit8",+ '(' => "Digit9",+ ')' => "Digit0",+ else => "",+ };+}++// What a held Shift turns a US-layout key into.+fn shiftedAscii(c: u8) u8 {+ return switch (c) {+ 'a'...'z' => std.ascii.toUpper(c),+ '`' => '~',+ '1' => '!',+ '2' => '@',+ '3' => '#',+ '4' => '$',+ '5' => '%',+ '6' => '^',+ '7' => '&',+ '8' => '*',+ '9' => '(',+ '0' => ')',+ '-' => '_',+ '=' => '+',+ '[' => '{',+ ']' => '}',+ '\\' => '|',+ ';' => ':',+ '\'' => '"',+ ',' => '<',+ '.' => '>',+ '/' => '?',+ else => c,+ };+}++const testing = @import("../../testing.zig");+test "keyboard: named keys take WebDriver's code and location" {+ const cases = [_]struct { []const u8, []const u8, u32 }{+ .{ "Enter", "Enter", 0 },+ .{ "a", "KeyA", 0 },+ .{ " ", "Space", 0 },+ .{ "ArrowUp", "ArrowUp", 0 },+ .{ "Shift", "ShiftLeft", 1 },+ .{ "pageup", "PageUp", 0 },+ .{ "Bogus", "", 0 },+ };+ for (cases) |c| {+ const name, const code, const location = c;+ const info = named(name);+ try testing.expectEqual(code, info.code);+ try testing.expectEqual(location, info.location);+ }+}++test "keyboard: Shift on a WebDriver character" {+ const info = webdriverKey('a', true);+ try testing.expectEqual('A', info.key.char);+ try testing.expectEqual("KeyA", info.code);+ try testing.expectEqual(.shift, webdriverKey(0xE008, false).modifier.?);+}diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 88e34d377..e635f49c1 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -933,6 +933,8 @@ pub fn typeChar(frame: *Frame, target: *Element, keypress: *KeyboardEvent, text:fn keypressFor(frame: *Frame, keydown: *const KeyboardEvent) !*KeyboardEvent {return KeyboardEvent.initTrusted(comptime .wrap("keypress"), .{.key = keydown.getKey().asString(),+ .code = keydown._code,+ .location = keydown._location,.ctrlKey = keydown.getCtrlKey(),.shiftKey = keydown.getShiftKey(),.altKey = keydown.getAltKey(),diff --git a/src/browser/tests/mcp_actions.html b/src/browser/tests/mcp_actions.htmlindex f0b69d565..798f61f4b 100644--- a/src/browser/tests/mcp_actions.html+++ b/src/browser/tests/mcp_actions.html@@ -18,7 +18,7 @@<div style="height: 500px;"><p id="sheetleaf">Sheet leaf</p></div></div><div id="hoverTarget" onmouseover="window.hovered = true;">Hover Me</div>- <input id="keyTarget" onkeydown="window.keyPressed = event.key;" onkeyup="window.keyReleased = event.key;">+ <input id="keyTarget" onkeydown="window.keyPressed = event.key; window.keyCode = event.code;" onkeyup="window.keyReleased = event.key;"><select id="sel2" onchange="window.sel2Changed = this.value"><option value="a">Alpha</option><option value="b">Beta</option>diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zigindex 33cd3b104..4a2ae956d 100644--- a/src/browser/webapi/WebDriver.zig+++ b/src/browser/webapi/WebDriver.zig@@ -22,7 +22,7 @@ const lp = @import("lightpanda");const js = @import("../js/js.zig");const Page = @import("../Page.zig");const Frame = @import("../Frame.zig");-const Modifiers = @import("../frame/user_input.zig").Modifiers;+const keyboard = @import("../frame/keyboard.zig");const Event = @import("Event.zig");const Element = @import("Element.zig");@@ -30,7 +30,6 @@ const EventTarget = @import("EventTarget.zig");const Cookie = @import("storage/Cookie.zig");const TouchEvent = @import("event/TouchEvent.zig");-const KeyboardEvent = @import("event/KeyboardEvent.zig");const Label = @import("element/html/Label.zig");const log = lp.log;@@ -353,122 +352,16 @@ fn performKeySource(source: js.Object, frame: *Frame) !void {continue;}- const key = webdriverKey((try action.get("value")).toStringSlice() catch "");-- // A modifier's own keydown already carries its flag; its keyup no- // longer does.- setModifier(&frame.page.input_modifiers, key, is_down);-- dispatchKey(is_down, key, frame);- }-}--// WebDriver's normalized-key PUA codepoints to KeyboardEvent.key values-// (https://w3c.github.io/webdriver/#keyboard-actions). Any other value is the-// key itself. The U+E050-U+E053 right-hand variants map to the same key name,-// only the (untracked) location differs.-fn webdriverKey(value: []const u8) []const u8 {- // The U+E000-U+E05D PUA range always encodes as three UTF-8 bytes.- if (value.len != 3) {- return value;- }- const cp = std.unicode.utf8Decode(value) catch return value;- return switch (cp) {- 0xE000 => "Unidentified",- 0xE001 => "Cancel",- 0xE002 => "Help",- 0xE003 => "Backspace",- 0xE004 => "Tab",- 0xE005 => "Clear",- 0xE006, 0xE007 => "Enter",- 0xE008, 0xE050 => "Shift",- 0xE009, 0xE051 => "Control",- 0xE00A, 0xE052 => "Alt",- 0xE00B => "Pause",- 0xE00C => "Escape",- 0xE00D => " ",- 0xE00E => "PageUp",- 0xE00F => "PageDown",- 0xE010 => "End",- 0xE011 => "Home",- 0xE012 => "ArrowLeft",- 0xE013 => "ArrowUp",- 0xE014 => "ArrowRight",- 0xE015 => "ArrowDown",- 0xE016 => "Insert",- 0xE017 => "Delete",- 0xE018 => ";",- 0xE019 => "=",- 0xE01A => "0",- 0xE01B => "1",- 0xE01C => "2",- 0xE01D => "3",- 0xE01E => "4",- 0xE01F => "5",- 0xE020 => "6",- 0xE021 => "7",- 0xE022 => "8",- 0xE023 => "9",- 0xE024 => "*",- 0xE025 => "+",- 0xE026 => ",",- 0xE027 => "-",- 0xE028 => ".",- 0xE029 => "/",- 0xE031 => "F1",- 0xE032 => "F2",- 0xE033 => "F3",- 0xE034 => "F4",- 0xE035 => "F5",- 0xE036 => "F6",- 0xE037 => "F7",- 0xE038 => "F8",- 0xE039 => "F9",- 0xE03A => "F10",- 0xE03B => "F11",- 0xE03C => "F12",- 0xE03D, 0xE053 => "Meta",- else => value,- };-}--fn setModifier(modifiers: *Modifiers, key: []const u8, pressed: bool) void {- if (std.mem.eql(u8, key, "Shift")) {- modifiers.shift = pressed;- } else if (std.mem.eql(u8, key, "Control")) {- modifiers.ctrl = pressed;- } else if (std.mem.eql(u8, key, "Alt")) {- modifiers.alt = pressed;- } else if (std.mem.eql(u8, key, "Meta")) {- modifiers.meta = pressed;- }-}--// Key actions have no explicit target; they go to the focused element,-// resolved per action since a key's default action can move focus.-fn dispatchKey(is_down: bool, key: []const u8, frame: *Frame) void {- const typ: lp.String = if (is_down) comptime .wrap("keydown") else comptime .wrap("keyup");- const modifiers = frame.page.input_modifiers;- const event = KeyboardEvent.initTrusted(typ, .{- .bubbles = true,- .cancelable = true,- .composed = true,- .key = key,- .ctrlKey = modifiers.ctrl,- .shiftKey = modifiers.shift,- .altKey = modifiers.alt,- .metaKey = modifiers.meta,- }, frame) catch |err| {- log.debug(.app, "webdriver key event", .{ .err = err });- return;- };- if (is_down) {- _ = Frame.user_input.triggerKeyDown(frame, event, Frame.user_input.textForKey(event)) catch |err| {- log.debug(.app, "webdriver dispatch", .{ .err = err, .type = typ.str() });- };- } else {- Frame.user_input.triggerKeyUp(frame, event) catch |err| {- log.debug(.app, "webdriver dispatch", .{ .err = err, .type = typ.str() });+ const value = (try action.get("value")).toStringSlice() catch "";+ const direction: keyboard.Direction = if (is_down) .down else .up;+ const modifiers = &frame.page.input_modifiers;+ // A grapheme cluster of several code points is its own key name.+ const dispatched = if (keyboard.singleCodepoint(value)) |cp|+ keyboard.keyAction(frame, cp, direction, modifiers)+ else+ keyboard.dispatch(frame, null, direction, &.{ .key = .{ .name = value }, .code = "" }, modifiers);+ dispatched catch |err| {+ log.debug(.app, "webdriver key", .{ .err = err });};}}diff --git a/src/mcp/tools.zig b/src/mcp/tools.zigindex dc028ebb1..b98ef9312 100644--- a/src/mcp/tools.zig+++ b/src/mcp/tools.zig@@ -1426,7 +1426,7 @@ test "MCP - Actions: click, fill, scroll, hover, press, selectOption, setChecked\\ document.getElementById('plain').scrollTop === 7 &&\\ window.scrollX === 5 && window.scrollY === 20 &&\\ window.hovered === true &&- \\ window.keyPressed === 'Enter' && window.keyReleased === 'Enter' &&+ \\ window.keyPressed === 'Enter' && window.keyCode === 'Enter' && window.keyReleased === 'Enter' &&\\ window.sel2Changed === 'b' &&\\ window.chkClicked === true && window.chkChanged === true &&\\ window.radClicked === true && window.radChanged === truediff --git a/src/server/bidi/input.zig b/src/server/bidi/input.zigindex 1dbd4660c..8a785c6a6 100644--- a/src/server/bidi/input.zig+++ b/src/server/bidi/input.zig@@ -21,7 +21,7 @@ const lp = @import("lightpanda");const Frame = @import("../../browser/Frame.zig");const Node = @import("../../browser/webapi/Node.zig");-const KeyboardEvent = @import("../../browser/webapi/event/KeyboardEvent.zig");+const keyboard = @import("../../browser/frame/keyboard.zig");const BiDi = @import("BiDi.zig");const remote_value = @import("remote_value.zig");@@ -177,12 +177,7 @@ pub const State = struct {}};-const Modifiers = struct {- alt: bool = false,- ctrl: bool = false,- meta: bool = false,- shift: bool = false,-};+const Modifiers = user_input.Modifiers;const Source = struct {id: []const u8,@@ -466,15 +461,7 @@ fn keyValue(obj: std.json.ObjectMap) ParseError!u21 {.string => |s| s,else => return error.InvalidField,};- // one code point, no more, no less- if (str.len == 0) {- return error.InvalidKeyValue;- }- const len = std.unicode.utf8ByteSequenceLength(str[0]) catch return error.InvalidKeyValue;- if (str.len != len) {- return error.InvalidKeyValue;- }- return std.unicode.utf8Decode(str) catch return error.InvalidKeyValue;+ return keyboard.singleCodepoint(str) orelse error.InvalidKeyValue;}fn buttonField(obj: std.json.ObjectMap) ParseError!u8 {@@ -557,21 +544,17 @@ fn dispatch(bidi: *BiDi, frame: *Frame, source: *Source, action: *const Action).pause => {},.key_down => |cp| {const key = &source.key;- const info = keyInfo(cp, key.modifiers.shift);- setModifier(&key.modifiers, info.modifier, true);if (std.mem.findScalar(u21, key.pressed.items, cp) == null) {try key.pressed.append(allocator, cp);}- try dispatchKey(frame, "keydown", &info, &key.modifiers);+ try keyboard.keyAction(frame, cp, .down, &key.modifiers);},.key_up => |cp| {const key = &source.key;- const info = keyInfo(cp, key.modifiers.shift);- setModifier(&key.modifiers, info.modifier, false);if (std.mem.findScalar(u21, key.pressed.items, cp)) |i| {_ = key.pressed.orderedRemove(i);}- try dispatchKey(frame, "keyup", &info, &key.modifiers);+ try keyboard.keyAction(frame, cp, .up, &key.modifiers);},.pointer_down => |button| {const pointer = &source.pointer;@@ -634,21 +617,18 @@ pub fn typeText(frame: *Frame, text: []const u8) !void {continue;}- const info = keyInfo(cp, modifiers.shift);- if (info.modifier == null) {- try dispatchKey(frame, "keydown", &info, &modifiers);- try dispatchKey(frame, "keyup", &info, &modifiers);+ if (keyboard.webdriverKey(cp, false).modifier == null) {+ try keyboard.keyAction(frame, cp, .down, &modifiers);+ try keyboard.keyAction(frame, cp, .up, &modifiers);continue;}if (std.mem.findScalar(u21, held.items, cp)) |i| {_ = held.orderedRemove(i);- setModifier(&modifiers, info.modifier, false);- try dispatchKey(frame, "keyup", &info, &modifiers);+ try keyboard.keyAction(frame, cp, .up, &modifiers);} else {held.appendAssumeCapacity(cp);- setModifier(&modifiers, info.modifier, true);- try dispatchKey(frame, "keydown", &info, &modifiers);+ try keyboard.keyAction(frame, cp, .down, &modifiers);}}try releaseHeld(frame, &held, &modifiers);@@ -657,9 +637,7 @@ pub fn typeText(frame: *Frame, text: []const u8) !void {// in reverse press orderfn releaseHeld(frame: *Frame, held: *std.ArrayList(u21), modifiers: *Modifiers) !void {while (held.pop()) |cp| {- const info = keyInfo(cp, modifiers.shift);- setModifier(modifiers, info.modifier, false);- try dispatchKey(frame, "keyup", &info, modifiers);+ try keyboard.keyAction(frame, cp, .up, modifiers);}}@@ -681,217 +659,6 @@ fn resolveOrigin(bidi: *BiDi, frame: *Frame, source: *const Source, origin: Orig}}-fn dispatchKey(frame: *Frame, comptime typ: []const u8, info: *const KeyInfo, modifiers: *const Modifiers) !void {- var buf: [4]u8 = undefined;- const key: []const u8 = switch (info.key) {- .name => |name| name,- .char => |cp| buf[0 .. std.unicode.utf8Encode(cp, &buf) catch unreachable],- };- const event = try KeyboardEvent.initTrusted(comptime .wrap(typ), .{- .key = key,- .code = info.code,- .location = info.location,- .altKey = modifiers.alt,- .ctrlKey = modifiers.ctrl,- .metaKey = modifiers.meta,- .shiftKey = modifiers.shift,- }, frame);- if (comptime std.mem.eql(u8, typ, "keydown")) {- _ = try user_input.triggerKeyDown(frame, event, user_input.textForKey(event));- } else {- try user_input.triggerKeyUp(frame, event);- }-}--fn setModifier(modifiers: *Modifiers, which: ?Modifier, down: bool) void {- switch (which orelse return) {- .alt => modifiers.alt = down,- .ctrl => modifiers.ctrl = down,- .meta => modifiers.meta = down,- .shift => modifiers.shift = down,- }-}--const Modifier = enum { alt, ctrl, meta, shift };--const KeyInfo = struct {- // a named key, or the character itself- key: union(enum) { name: []const u8, char: u21 },- code: []const u8,- location: u32 = 0,- modifier: ?Modifier = null,-};--// WebDriver's key table: the Private Use Area - names the-// non-printing keys, anything else is the character itself.-// https://w3c.github.io/webdriver/#keyboard-actions-fn keyInfo(cp: u21, shift: bool) KeyInfo {- if (cp >= 0xE000 and cp <= 0xE05D) {- return specialKey(cp);- }-- const char: u21 = if (shift and cp < 128) shiftedAscii(@intCast(cp)) else cp;- return .{ .key = .{ .char = char }, .code = asciiCode(cp) };-}--fn specialKey(cp: u21) KeyInfo {- const k = struct {- fn k(key: []const u8, code: []const u8) KeyInfo {- return .{ .key = .{ .name = key }, .code = code };- }- fn m(key: []const u8, code: []const u8, location: u32, modifier: Modifier) KeyInfo {- return .{ .key = .{ .name = key }, .code = code, .location = location, .modifier = modifier };- }- fn n(key: []const u8, code: []const u8) KeyInfo {- return .{ .key = .{ .name = key }, .code = code, .location = 3 };- }- };- return switch (cp) {- 0xE000 => k.k("Unidentified", ""),- 0xE001 => k.k("Cancel", "Abort"),- 0xE002 => k.k("Help", "Help"),- 0xE003 => k.k("Backspace", "Backspace"),- 0xE004 => k.k("Tab", "Tab"),- 0xE005 => k.k("Clear", "NumLock"),- 0xE006 => k.k("Enter", "Enter"),- 0xE007 => k.n("Enter", "NumpadEnter"),- 0xE008 => k.m("Shift", "ShiftLeft", 1, .shift),- 0xE009 => k.m("Control", "ControlLeft", 1, .ctrl),- 0xE00A => k.m("Alt", "AltLeft", 1, .alt),- 0xE00B => k.k("Pause", "Pause"),- 0xE00C => k.k("Escape", "Escape"),- 0xE00D => k.k(" ", "Space"),- 0xE00E => k.k("PageUp", "PageUp"),- 0xE00F => k.k("PageDown", "PageDown"),- 0xE010 => k.k("End", "End"),- 0xE011 => k.k("Home", "Home"),- 0xE012 => k.k("ArrowLeft", "ArrowLeft"),- 0xE013 => k.k("ArrowUp", "ArrowUp"),- 0xE014 => k.k("ArrowRight", "ArrowRight"),- 0xE015 => k.k("ArrowDown", "ArrowDown"),- 0xE016 => k.k("Insert", "Insert"),- 0xE017 => k.k("Delete", "Delete"),- 0xE018 => k.k(";", "Semicolon"),- 0xE019 => k.k("=", "Equal"),- 0xE01A => k.n("0", "Numpad0"),- 0xE01B => k.n("1", "Numpad1"),- 0xE01C => k.n("2", "Numpad2"),- 0xE01D => k.n("3", "Numpad3"),- 0xE01E => k.n("4", "Numpad4"),- 0xE01F => k.n("5", "Numpad5"),- 0xE020 => k.n("6", "Numpad6"),- 0xE021 => k.n("7", "Numpad7"),- 0xE022 => k.n("8", "Numpad8"),- 0xE023 => k.n("9", "Numpad9"),- 0xE024 => k.n("*", "NumpadMultiply"),- 0xE025 => k.n("+", "NumpadAdd"),- 0xE026 => k.n(",", "NumpadComma"),- 0xE027 => k.n("-", "NumpadSubtract"),- 0xE028 => k.n(".", "NumpadDecimal"),- 0xE029 => k.n("/", "NumpadDivide"),- 0xE031 => k.k("F1", "F1"),- 0xE032 => k.k("F2", "F2"),- 0xE033 => k.k("F3", "F3"),- 0xE034 => k.k("F4", "F4"),- 0xE035 => k.k("F5", "F5"),- 0xE036 => k.k("F6", "F6"),- 0xE037 => k.k("F7", "F7"),- 0xE038 => k.k("F8", "F8"),- 0xE039 => k.k("F9", "F9"),- 0xE03A => k.k("F10", "F10"),- 0xE03B => k.k("F11", "F11"),- 0xE03C => k.k("F12", "F12"),- 0xE03D => k.m("Meta", "MetaLeft", 1, .meta),- 0xE040 => k.k("ZenkakuHankaku", ""),- 0xE050 => k.m("Shift", "ShiftRight", 2, .shift),- 0xE051 => k.m("Control", "ControlRight", 2, .ctrl),- 0xE052 => k.m("Alt", "AltRight", 2, .alt),- 0xE053 => k.m("Meta", "MetaRight", 2, .meta),- 0xE054 => k.n("PageUp", "Numpad9"),- 0xE055 => k.n("PageDown", "Numpad3"),- 0xE056 => k.n("End", "Numpad1"),- 0xE057 => k.n("Home", "Numpad7"),- 0xE058 => k.n("ArrowLeft", "Numpad4"),- 0xE059 => k.n("ArrowUp", "Numpad8"),- 0xE05A => k.n("ArrowRight", "Numpad6"),- 0xE05B => k.n("ArrowDown", "Numpad2"),- 0xE05C => k.n("Insert", "Numpad0"),- 0xE05D => k.n("Delete", "NumpadDecimal"),- else => k.k("Unidentified", ""),- };-}--// slices into this literal are always valid-const key_codes = "KeyAKeyBKeyCKeyDKeyEKeyFKeyGKeyHKeyIKeyJKeyKKeyLKeyMKeyNKeyOKeyPKeyQKeyRKeySKeyTKeyUKeyVKeyWKeyXKeyYKeyZ";-const digit_codes = "Digit0Digit1Digit2Digit3Digit4Digit5Digit6Digit7Digit8Digit9";--// The `code` of a printable character on a US layout.-fn asciiCode(cp: u21) []const u8 {- if (cp >= 128) {- return "";- }- const c: u8 = @intCast(cp);- return switch (c) {- 'a'...'z' => key_codes[(c - 'a') * 4 ..][0..4],- 'A'...'Z' => key_codes[(c - 'A') * 4 ..][0..4],- '0'...'9' => digit_codes[(c - '0') * 6 ..][0..6],- ' ' => "Space",- '\n', '\r' => "Enter",- '\t' => "Tab",- '`', '~' => "Backquote",- '-', '_' => "Minus",- '=', '+' => "Equal",- '[', '{' => "BracketLeft",- ']', '}' => "BracketRight",- '\\', '|' => "Backslash",- ';', ':' => "Semicolon",- '\'', '"' => "Quote",- ',', '<' => "Comma",- '.', '>' => "Period",- '/', '?' => "Slash",- '!' => "Digit1",- '@' => "Digit2",- '#' => "Digit3",- '$' => "Digit4",- '%' => "Digit5",- '^' => "Digit6",- '&' => "Digit7",- '*' => "Digit8",- '(' => "Digit9",- ')' => "Digit0",- else => "",- };-}--// What a held Shift turns a US-layout key into.-fn shiftedAscii(c: u8) u8 {- return switch (c) {- 'a'...'z' => std.ascii.toUpper(c),- '`' => '~',- '1' => '!',- '2' => '@',- '3' => '#',- '4' => '$',- '5' => '%',- '6' => '^',- '7' => '&',- '8' => '*',- '9' => '(',- '0' => ')',- '-' => '_',- '=' => '+',- '[' => '{',- ']' => '}',- '\\' => '|',- ';' => ':',- '\'' => '"',- ',' => '<',- '.' => '>',- '/' => '?',- else => c,- };-}-fn errorCode(err: anyerror) []const u8 {return switch (err) {error.NoSuchElement => "no such element",