Skip to content

fix: [#2034] QuerySelector fails to match attribute values containing quotes#2036

Closed
JalilArfaoui wants to merge 1 commit into
capricorn86:masterfrom
JalilArfaoui:fix/attribute-selector-quote-in-value
Closed

fix: [#2034] QuerySelector fails to match attribute values containing quotes#2036
JalilArfaoui wants to merge 1 commit into
capricorn86:masterfrom
JalilArfaoui:fix/attribute-selector-quote-in-value

Conversation

@JalilArfaoui
Copy link
Copy Markdown

Fixes #2034

Root cause

The regex that captures attribute values used [^"']* which excludes both quote types regardless of which delimiter is actually used:

["']{1}([^"']*)["']{1}

Fix

Use lookbehind/lookahead assertions to match the value based on the actual delimiter:

["']((?<=")[^"]*(?=")|(?<=')[^']*(?='))["']

This captures the value correctly without adding any new capture groups.

Test plan

  • Added test for apostrophe in double-quoted value
  • Added test for double quote in single-quoted value
  • All existing tests pass (7149 tests)

… quotes

When using querySelector with an attribute selector, values containing
apostrophes in double-quoted selectors (or double quotes in single-quoted
selectors) were not matched correctly.

For example, [data-value="it's a test"] would fail to find elements
with that attribute value.

The issue was in the regex that captures attribute values: it excluded
both quote types regardless of which delimiter was used.

The fix uses lookbehind/lookahead assertions to correctly match the
value based on the actual delimiter used.
@JalilArfaoui JalilArfaoui force-pushed the fix/attribute-selector-quote-in-value branch from 63c9ea1 to 9a0eec3 Compare January 23, 2026 09:45
@TrevorBurnham
Copy link
Copy Markdown
Contributor

This should be addressed by #2010, which replaces the custom CSS selector parser completely. I've added your test cases to that PR to confirm.

@capricorn86
Copy link
Copy Markdown
Owner

Thank you for contributing @JalilArfaoui

I believe this was fixed by #2068, so I will close this now.

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.

querySelector fails to match attribute values containing apostrophes

3 participants