diff --git a/pkgs/id/web/src/world-keyboard.test.ts b/pkgs/id/web/src/world-keyboard.test.ts index 0a98e1eaa0..996252ef6b 100644 --- a/pkgs/id/web/src/world-keyboard.test.ts +++ b/pkgs/id/web/src/world-keyboard.test.ts @@ -64,6 +64,34 @@ describe("terminal-compatible world key input", () => { expect(key("F5")).toBeNull(); }); + it("rejects every Object.prototype member name", () => { + for (const name of Object.getOwnPropertyNames(Object.prototype)) { + expect(key(name), name).toBeNull(); + } + }); + + it("preserves all unmodified special-key encodings", () => { + const encodings = { + Enter: "\r", + Tab: "\t", + Backspace: "\x7f", + Escape: "\x1b", + Delete: "\x1b[3~", + Home: "\x1b[H", + End: "\x1b[F", + PageUp: "\x1b[5~", + PageDown: "\x1b[6~", + ArrowUp: "\x1b[A", + ArrowDown: "\x1b[B", + ArrowRight: "\x1b[C", + ArrowLeft: "\x1b[D", + }; + + for (const [name, expected] of Object.entries(encodings)) { + expect(key(name), name).toBe(expected); + } + }); + it("encodes modified CSI tilde keys with the xterm modifier parameter", () => { expect(key("Delete", { ctrlKey: true })).toBe("\x1b[3;5~"); expect(key("Delete", { shiftKey: true })).toBe("\x1b[3;2~"); diff --git a/pkgs/id/web/src/world-keyboard.ts b/pkgs/id/web/src/world-keyboard.ts index ba8095e924..441e5a8b7d 100644 --- a/pkgs/id/web/src/world-keyboard.ts +++ b/pkgs/id/web/src/world-keyboard.ts @@ -67,8 +67,10 @@ export function terminalBytesForKey(event: KeyEventLike): Uint8Array | null { return null; } - if (event.key in specialKeys) { - const special = specialKeys[event.key]; + if (Object.hasOwn(Object.prototype, event.key)) return null; + + const special = Object.hasOwn(specialKeys, event.key) ? specialKeys[event.key] : undefined; + if (special) { const modified = event.ctrlKey || event.altKey || event.shiftKey; if (!modified) return new TextEncoder().encode(special.plain); const mod = modifierParameter(event);