Safer query selectors

This commit is contained in:
Dustin Brett 2025-05-23 10:24:16 -07:00
parent e99c948b47
commit 296d41874f
4 changed files with 43 additions and 21 deletions

View File

@ -276,11 +276,16 @@ const useFileKeyboardShortcuts = (
if (focusOnEntry) {
blurEntry();
focusEntry(focusOnEntry);
fileManagerRef.current
?.querySelector(
`button[aria-label='${focusOnEntry.replace(SHORTCUT_EXTENSION, "")}']`
)
?.scrollIntoView();
try {
fileManagerRef.current
?.querySelector(
`button[aria-label='${CSS.escape(focusOnEntry.replace(SHORTCUT_EXTENSION, ""))}']`
)
?.scrollIntoView();
} catch {
// Ignore error getting/scrolling element
}
}
}
}

View File

@ -134,14 +134,18 @@ const useFocusableEntries = (
);
if (lines.length > 1) {
const element = fileManagerRef.current?.querySelector(
`[aria-label='${textLabel}'] figcaption`
);
try {
const element = fileManagerRef.current?.querySelector(
`[aria-label='${CSS.escape(textLabel)}'] figcaption`
);
if (element) {
$labelHeightOffset =
(lines.length - 1) *
Number.parseFloat(window.getComputedStyle(element).lineHeight);
if (element) {
$labelHeightOffset =
(lines.length - 1) *
Number.parseFloat(window.getComputedStyle(element).lineHeight);
}
} catch {
// Ignore error getting element
}
}
}

View File

@ -22,10 +22,17 @@ declare global {
}
}
export const getNavButtonByTitle = (title: string): HTMLButtonElement | null =>
document.querySelector(
`main > nav > div[title='${title}']`
) as HTMLButtonElement;
export const getNavButtonByTitle = (
title: string
): HTMLButtonElement | undefined => {
try {
return document.querySelector(
`main > nav > div[title='${CSS.escape(title)}']`
) as HTMLButtonElement;
} catch {
return undefined;
}
};
let metaDown = false;
let metaComboUsed = false;

View File

@ -510,11 +510,17 @@ export const updateIconPositionsIfEmpty = (
const entryUrl = join(url, entry);
if (!iconPositions[entryUrl]) {
const gridEntry = [...gridElement.children].find((element) =>
element.querySelector(
`button[aria-label="${entry.replace(SHORTCUT_EXTENSION, "")}"]`
)
);
let gridEntry: Element | undefined;
try {
gridEntry = [...gridElement.children].find((element) =>
element.querySelector(
`button[aria-label="${CSS.escape(entry.replace(SHORTCUT_EXTENSION, ""))}"]`
)
);
} catch {
// Ignore error getting element
}
if (gridEntry instanceof HTMLElement) {
const { x, y, height, width } = gridEntry.getBoundingClientRect();