Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape strings used in query selectors #5565

Merged
merged 1 commit into from Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@react-aria/combobox/src/useComboBox.ts
Expand Up @@ -122,7 +122,7 @@ export function useComboBox<T>(props: AriaComboBoxOptions<T>, state: ComboBoxSta
// If the focused item is a link, trigger opening it. Items that are links are not selectable.
if (state.isOpen && state.selectionManager.focusedKey != null && state.selectionManager.isLink(state.selectionManager.focusedKey)) {
if (e.key === 'Enter') {
let item = listBoxRef.current.querySelector(`[data-key="${state.selectionManager.focusedKey}"]`);
let item = listBoxRef.current.querySelector(`[data-key="${CSS.escape(state.selectionManager.focusedKey.toString())}"]`);
if (item instanceof HTMLAnchorElement) {
router.open(item, e);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-aria/grid/src/GridKeyboardDelegate.ts
Expand Up @@ -270,7 +270,7 @@ export class GridKeyboardDelegate<T, C extends GridCollection<T>> implements Key
}

private getItem(key: Key): HTMLElement {
return this.ref.current.querySelector(`[data-key="${key}"]`);
return this.ref.current.querySelector(`[data-key="${CSS.escape(key.toString())}"]`);
}

private getItemRect(key: Key): Rect {
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-aria/selection/src/ListKeyboardDelegate.ts
Expand Up @@ -195,7 +195,7 @@ export class ListKeyboardDelegate<T> implements KeyboardDelegate {
}

private getItem(key: Key): HTMLElement {
return this.ref.current.querySelector(`[data-key="${key}"]`);
return this.ref.current.querySelector(`[data-key="${CSS.escape(key.toString())}"]`);
}

getKeyPageAbove(key: Key) {
Expand Down
6 changes: 3 additions & 3 deletions packages/@react-aria/selection/src/useSelectableCollection.ts
Expand Up @@ -140,7 +140,7 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
manager.setFocusedKey(key, childFocus);
});

let item = scrollRef.current.querySelector(`[data-key="${key}"]`);
let item = scrollRef.current.querySelector(`[data-key="${CSS.escape(key.toString())}"]`);
router.open(item, e);

return;
Expand Down Expand Up @@ -342,7 +342,7 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions

if (!isVirtualized && manager.focusedKey != null) {
// Refocus and scroll the focused item into view if it exists within the scrollable region.
let element = scrollRef.current.querySelector(`[data-key="${manager.focusedKey}"]`) as HTMLElement;
let element = scrollRef.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`) as HTMLElement;
if (element) {
// This prevents a flash of focus on the first/last element in the collection, or the collection itself.
if (!element.contains(document.activeElement)) {
Expand Down Expand Up @@ -404,7 +404,7 @@ export function useSelectableCollection(options: AriaSelectableCollectionOptions
useEffect(() => {
let modality = getInteractionModality();
if (manager.isFocused && manager.focusedKey != null && scrollRef?.current) {
let element = scrollRef.current.querySelector(`[data-key="${manager.focusedKey}"]`) as HTMLElement;
let element = scrollRef.current.querySelector(`[data-key="${CSS.escape(manager.focusedKey.toString())}"]`) as HTMLElement;
if (element && (modality === 'keyboard' || autoFocusRef.current)) {
if (!isVirtualized) {
scrollIntoView(scrollRef.current, element);
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/tabs/src/Tabs.tsx
Expand Up @@ -80,7 +80,7 @@ function Tabs<T extends object>(props: SpectrumTabsProps<T>, ref: DOMRef<HTMLDiv

useEffect(() => {
if (tablistRef.current) {
let selectedTab: HTMLElement = tablistRef.current.querySelector(`[data-key="${tabListState?.selectedKey}"]`);
let selectedTab: HTMLElement = tablistRef.current.querySelector(`[data-key="${CSS.escape(tabListState?.selectedKey?.toString())}"]`);

if (selectedTab != null) {
setSelectedTab(selectedTab);
Expand Down