Skip to content

fix: avoid querySelector SyntaxError when option value contains special characters - #674

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/active-scroll-selector-crash
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/active-scroll-selector-crash

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Nov 30, 2026 •

Copy link
Copy Markdown

When the user moves the active option with the keyboard, RawOptionList scrolls the active cell of each column into view by looking up its li with querySelector('li[data-path-key="..."]'). The path key goes into a CSS attribute selector after escaping only the double quotes, so an option value with a backslash or a newline makes an invalid selector and the whole component throws:

SyntaxError: 'li[data-path-key="line1
line2"]' is not a valid selector

A value such as a Windows path (C:\) is a plausible case.

The effect now collects the li[data-path-key] elements once and finds the active one by comparing the data-path-key attribute to the key, so no selector has to be built from option values and no escaping is needed.

Test: should not crash when scrolling active option with backslash or newline in value opens a cascader with options C:\, a\"b and line1\nline2, moves the active option with ArrowDown and checks that each one becomes the active item and is scrolled into view. It fails on master with the SyntaxError above and passes now. npm test (12 suites, 129 passed, 2 skipped as before), npm run tsc and npm run lint (0 errors) pass.

Summary by CodeRabbit

  • 修复
    • 修正键盘导航时活动选项的自动滚动行为。选项值包含反斜杠、引号或换行符时,当前选项仍能正确滚动到视野内。

@vercel

vercel Bot commented Nov 30, 2026

Copy link
Copy Markdown

@mokshupadhyay is attempting to deploy a commit to the React Component 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

活动选项滚动逻辑改为按 data-path-key 属性值精确查找目标元素。新增键盘导航回归测试,覆盖包含反斜杠、引号和换行符的选项值。

Changes

活动选项滚动

层 / 文件 摘要
精确匹配与回归测试
src/OptionList/List.tsx, tests/index.spec.tsx
活动项查找改为收集带 data-path-key 的 li 并按属性值匹配。新增测试验证含特殊字符的选项值在每次按下 ArrowDown 后,仍能匹配活动项并传给 scrollIntoParentView。

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

Change: Bug fix

Suggested reviewers: zombiej

Merge Risk: Minimal · up to 7ce50

This change stops keyboard navigation from crashing when option values contain backslashes, quotes, or newlines, and adds a regression test. No remaining 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-path-key 构造的 querySelector 选择器,改为收集 li[data-path-key] 元素并精确比较属性值,从而避免含反斜杠、双引号或换行符的选项值引发 SyntaxError。标题准确描述了主要修复点,且符合简洁、具体的要求。
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

小兔沿着选项行,
反斜杠和引号都不慌。
换行也能准确找到,
活动元素轻轻滚到旁。
键盘每按一下,春风绕过清单。

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: a2cccf42-eff7-4e14-bfb1-184077a1d053
Commits

Reviewing files that changed from the base of the PR and between 3d31b12 and 7ce5008.

Files selected for processing: 2

  • src/OptionList/List.tsx
  • tests/index.spec.tsx

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 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