Skip to content

Commit a20936d

Browse files
committed
Fix invalid selector in hotkey query
Using string concatentation with user controlled input to build a selector query results in syntax errors. > SyntaxError: Failed to execute 'querySelector' on 'Element': '[hotkey="""]' is not a valid selector. Instead, query potential hotkey buttons and test their attribute against the event's key value.
1 parent a561e30 commit a20936d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,18 @@ function focusKeydown(event: KeyboardEvent) {
363363

364364
const shortcutListeners = new WeakMap()
365365

366+
function findHotkey(toolbar: Element, key: string): HTMLElement | null {
367+
for (const el of toolbar.querySelectorAll<HTMLElement>('[hotkey]')) {
368+
if (el.getAttribute('hotkey') === key) {
369+
return el
370+
}
371+
}
372+
return null
373+
}
374+
366375
function shortcut(toolbar: Element, event: KeyboardEvent) {
367376
if ((event.metaKey && modifierKey === 'Meta') || (event.ctrlKey && modifierKey === 'Control')) {
368-
const button = toolbar.querySelector<HTMLElement>(`[hotkey="${event.key}"]`)
377+
const button = findHotkey(toolbar, event.key)
369378
if (button) {
370379
button.click()
371380
event.preventDefault()

0 commit comments

Comments
 (0)