Skip to content

Make empty ^=, $= and ~= attribute selectors match nothing - #344

Merged
kornelski merged 2 commits into
cloudflare:mainfrom
Feldt:fix/empty-attr-value-selectors
Oct 27, 2026
Merged

kornelski merged 2 commits into
cloudflare:mainfrom
Feldt:fix/empty-attr-value-selectors

Conversation

@Feldt

@Feldt Feldt commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #341

[att^=""], [att$=""] and [att~=""] (and their i forms) match elements, and their :not() forms match too few. The Selectors spec says ~= with an empty value never represents anything (attribute-representation), and says the same for ^=, $= and *= (attribute-substrings).

What was wrong

v2.3.0 handled this with the never_matches flag from the selectors crate. #264 (v2.4.0) dropped that flag and added an is_empty() check to the prefix and suffix matchers, but on the element's attribute value instead of the selector's value. So [a^=""] stopped matching a="" but still matched every non-empty value. matches_splitted_by_whitespace got no check, so for ~= the empty selector value equals the empty item that split() yields for a="" and for leading, trailing or doubled whitespace. *= was already right because has_attr_with_substring returns false for an empty operand.

The W3C fixtures css3-modsel-184a-f only use class="" and a missing attribute, so they kept passing.

What changed

  • src/selectors_vm/attribute_matcher.rs: has_attr_with_prefix, has_attr_with_suffix and matches_splitted_by_whitespace return false when the selector value is empty. This is the check the selectors crate does in AttrSelectorOperator::eval_str.
  • src/selectors_vm/tests.rs: compiled_attr_expression now checks ^=, $=, *= and ~= with an empty value, with and without i, against non-empty, empty, valueless and whitespace-padded attributes. The existing helper also checks each :not() form.

[a=""] and [a|=""] keep their current behaviour.

Element counts through rewrite_str for <i a="x"><i a=""><i a=" x"><i a="x "><i a="x y"><i a="-x"><i b="x">:

selector before after
[a^=""], [a^="" i] 5 0
[a$=""], [a$="" i] 5 0
[a~=""], [a~="" i] 4 0
[a*=""], [a*="" i] 0 0
i:not([a^=""]) 2 7

Bun proposed the same check as a patch on its vendored copy in oven-sh/bun#34180, which was closed without being merged.

How it was tested

  • cargo test --lib selectors_vm::tests::compiler::compiled_attr_expression fails on main (selector: "[foo^=\"\"]", input: "<div foo='bar'>") and passes with the fix. Reverting any one of the three matcher changes makes it fail again on that operator.
  • ./scripts/test.sh, cargo fmt -- --check, the four cargo clippy commands from CI and cargo doc --no-deps --all pass with RUSTFLAGS=-Dwarnings on rustc 1.99.1, macOS arm64. The js-api wasm tests were not run.

CHANGELOG.md is not touched, since entries look like they are added at release time. I can add a line if you prefer.

This change was prepared with an AI coding agent (Claude Code). I am responsible for it and will answer review comments.

🤖 Generated with Claude Code

@Feldt
Feldt requested review from a team, Noah-Kennedy, jasnell and orium as code owners October 20, 2026 22:56
@kornelski
kornelski force-pushed the fix/empty-attr-value-selectors branch from b82265c to 9e3d7c7 Compare October 27, 2026 01:45
Comment thread src/selectors_vm/attribute_matcher.rs Resolved
Per the Selectors spec, `[att^=""]`, `[att$=""]`, `[att*=""]` and
`[att~=""]` represent nothing. Since the selectors/cssparser upgrade
in cloudflare#264 (v2.4.0), the prefix and suffix matchers checked that the
element's attribute value is non-empty instead of the selector's
value, and the whitespace-list matcher had no check at all, so an
empty value matched the empty item produced by an empty value or by
leading, trailing or repeated whitespace. `[att*=""]` was already
right.

Check the selector's value instead, as the selectors crate does in
`AttrSelectorOperator::eval_str`. The W3C css3-modsel-184 fixtures
only cover `class=""` and a missing attribute, so they passed with
the old check.

Fixes cloudflare#341

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kornelski
kornelski force-pushed the fix/empty-attr-value-selectors branch from 9e3d7c7 to dfd59d4 Compare October 27, 2026 14:17
@kornelski
kornelski merged commit 64e9f38 into cloudflare:main Oct 27, 2026
3 checks passed
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.

[a^=""], [a$=""] and [a~=""] match elements

2 participants