From 19f4a65d4ba8fd19fc7ad23cd881f40693cb4e0c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 23 Sep 2026 17:55:11 +0000 Subject: [PATCH] fix: guard keybindHandler Escape check against non-Element event targets event.composedPath()[0] is typed as HTMLElement but is not guaranteed to actually be one. When no element has focus, some browsers (Safari) fire the keydown with document as the target instead of document.body, and Document has no closest() method, throwing `TypeError: target.closest is not a function` from keybindHandler. Guard the role="menu"/menubar Escape check with `target instanceof Element` before calling closest() so a non-Element target falls through to the normal Escape handling instead of throwing. (cherry picked from commit 8c71085132e81c116ac7f945d19af9d0756f0c36) --- .../keybindings/keybindingService.escape.test.ts | 12 ++++++++++++ src/platform/keybindings/keybindingService.ts | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/src/platform/keybindings/keybindingService.escape.test.ts b/src/platform/keybindings/keybindingService.escape.test.ts index a24be013662..986e40d833e 100644 --- a/src/platform/keybindings/keybindingService.escape.test.ts +++ b/src/platform/keybindings/keybindingService.escape.test.ts @@ -141,6 +141,18 @@ describe('keybindingService - Escape key handling', () => { } ) + it('does not throw when Escape fires with a non-Element target (e.g. document, in Safari when nothing has focus)', async () => { + const event = createKeyboardEvent('Escape', { + target: document as unknown as Element + }) + + await expect(keybindingService.keybindHandler(event)).resolves.not.toThrow() + + expect(useCommandStore().execute).toHaveBeenCalledWith( + 'Comfy.Graph.ExitSubgraph' + ) + }) + describe('registered Escape override', () => { it('suppresses ExitSubgraph when a registered override handles the event', async () => { const override = vi.fn().mockReturnValue(true) diff --git a/src/platform/keybindings/keybindingService.ts b/src/platform/keybindings/keybindingService.ts index e1470254816..3266ad72ccb 100644 --- a/src/platform/keybindings/keybindingService.ts +++ b/src/platform/keybindings/keybindingService.ts @@ -24,8 +24,12 @@ export function useKeybindingService() { const target = event.composedPath()[0] as HTMLElement // Let the active menu own Escape without also triggering the global shortcut. + // `target` is usually the focused element, but when nothing has focus some + // browsers (e.g. Safari) target the event at `document` instead of + // `document.body`, which has no `closest` method. if ( event.key === 'Escape' && + target instanceof Element && target.closest('[role="menu"], [role="menubar"]') ) { return