Skip to content

fix: look up menu items by data-menu-id attribute without unescaped selector interpolation - #907

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/escape-menu-id-selector
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/escape-menu-id-selector

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Nov 30, 2026 •

Copy link
Copy Markdown

The keyboard navigation of Menu (refreshElements in src/hooks/useAccessibility.ts) finds the DOM node of each item with document.querySelector("[data-menu-id='<id>']"), with the item key written into the selector as it is. A key that contains an apostrophe, a backslash or a newline makes an invalid selector, and the arrow keys then throw:

SyntaxError: '[data-menu-id='rc-menu-uuid-test-id-men's']' is not a valid selector

An item key such as Women's (or one that comes from user data) is enough. It is the same class of bug as react-component/cascader#674 and react-component/tabs#1031.

refreshElements now collects the [data-menu-id] nodes once into a Map (the first node wins, as querySelector returned the first match in document order) and looks each key up by getMenuId(id, key), so no selector is built from keys. It also scans the document once instead of once per key.

Test: navigates items whose keys contain quotes, backslashes, or newlines in tests/Keyboard.spec.tsx fails on master with the SyntaxError above and passes now. npm test (15 suites, 159 tests), npm run tsc, npm run lint (0 errors) and prettier on the changed files pass.

Summary by CodeRabbit

  • 修复
    • 修复菜单项键值包含单引号、反斜杠或换行符时,连续按下向下键无法按顺序切换焦点的问题。

@vercel

vercel Bot commented Nov 30, 2026

Copy link
Copy Markdown

@mokshupadhyay is attempting to deploy a commit to the afc163's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

refreshElements 现在通过 data-menu-id 元素映射查找菜单项,并保留重复 ID 中首次出现的元素。新增键盘导航测试,覆盖含单引号、反斜杠和换行符的菜单项。

Changes

菜单元素查找

Layer / File(s) Summary
菜单元素映射与导航验证
src/hooks/useAccessibility.ts, tests/Keyboard.spec.tsx
refreshElements 按 data-menu-id 建立映射,并通过菜单 ID 查找元素。重复 ID 保留首次出现的元素。新增测试验证含单引号、反斜杠和换行符的菜单项可通过连续按下 DOWN 依次获得焦点。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: zyf665, nrps9909, yoyo837

Merge Risk: Minimal · up to fc7bf

This change keeps keyboard navigation from throwing when menu item keys contain special characters. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题描述了主要变更:通过 data-menu-id 属性查找菜单项,并避免在选择器中拼接未转义的 key。raw_summary 显示 refreshElements 改为先收集所有 [data-menu-id] 元素并建立映射,再由 getMenuId 查找,与标题一致。标题未包含测试新增内容,但这属于可接受的范围。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

兔子轻敲键盘,耳朵轻轻摇,
特殊键值排好队,焦点逐个跳。
单引号,反斜线,换行也不慌,
菜单映射找得准,兔子笑声扬。
三步按下 DOWN,顺序正闪亮。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 155cf637-4f37-4776-b7f6-229ae6a45d8f
Commits

Reviewing files that changed from the base of the PR and between 765a2d5 and fc7bf9f.

Files selected for processing: 2

  • src/hooks/useAccessibility.ts
  • tests/Keyboard.spec.tsx

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant