History

Ignore non-keydown terminal shortcuts

- guard terminal clear/navigation shortcut handlers to only process `keydown` - add tests verifying `keyup` events are ignored

Julius Marminge committed 8 months ago

Commit b7bbcb21 parent 95477182 files · +22 −0Browse files

Unified · Split

2 files
  1. apps/web/src/keybindings.test.ts +13 −0
  2. apps/web/src/keybindings.ts +9 −0
apps/web/src/keybindings.test.ts+13−0
@@ -325,6 +325,10 @@ describe("isTerminalClearShortcut", () => { · 324 unmodified lines
325325 it("matches Cmd+K on macOS", () => {
326326 assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
327327 });
328+
329+ it("ignores non-keydown events", () => {
330+ assert.isFalse(isTerminalClearShortcut(event({ type: "keyup", key: "l", ctrlKey: true }), "Linux"));
331+ });
328332 });
329333
330334 describe("terminalNavigationShortcutData", () => {
@@ -371,6 +375,15 @@ describe("terminalNavigationShortcutData", () => { · 40 unmodified lines
371375 assert.isNull(terminalNavigationShortcutData(event({ key: "ArrowLeft", metaKey: true }), "Linux"));
372376 assert.isNull(terminalNavigationShortcutData(event({ key: "a", altKey: true }), "MacIntel"));
373377 });
378+
379+ it("ignores non-keydown events", () => {
380+ assert.isNull(
381+ terminalNavigationShortcutData(
382+ event({ type: "keyup", key: "ArrowLeft", altKey: true }),
383+ "MacIntel",
384+ ),
385+ );
386+ });
374387 });
375388
376389 describe("plus key parsing", () => {
apps/web/src/keybindings.ts+9−0
@@ -7,6 +7,7 @@ import { · 6 unmodified lines
77 import { isMacPlatform } from "./lib/utils";
88
99 export interface ShortcutEventLike {
10+ type?: string;
1011 key: string;
1112 metaKey: boolean;
1213 ctrlKey: boolean;
@@ -209,6 +210,10 @@ export function isTerminalClearShortcut( · 196 unmodified lines
209210 event: ShortcutEventLike,
210211 platform = navigator.platform,
211212 ): boolean {
213+ if (event.type !== undefined && event.type !== "keydown") {
214+ return false;
215+ }
216+
212217 const key = event.key.toLowerCase();
213218
214219 if (key === "l" && event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
@@ -229,6 +234,10 @@ export function terminalNavigationShortcutData( · 14 unmodified lines
229234 event: ShortcutEventLike,
230235 platform = navigator.platform,
231236 ): string | null {
237+ if (event.type !== undefined && event.type !== "keydown") {
238+ return null;
239+ }
240+
232241 if (event.shiftKey) return null;
233242
234243 const key = normalizeEventKey(event.key);