Repository navigation
Make empty ^=, $= and ~= attribute selectors match nothing - #344
Merged
Merged
Conversation
Feldt
requested review from
a team,
Noah-Kennedy,
jasnell and
orium
as code owners
October 20, 2026 22:56
kornelski
force-pushed
the
fix/empty-attr-value-selectors
branch
from
October 27, 2026 01:45
b82265c to
9e3d7c7
Compare
kornelski
reviewed
Oct 27, 2026
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
force-pushed
the
fix/empty-attr-value-selectors
branch
from
October 27, 2026 14:17
9e3d7c7 to
dfd59d4
Compare
kornelski
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #341
[att^=""],[att$=""]and[att~=""](and theiriforms) 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_matchesflag from theselectorscrate. #264 (v2.4.0) dropped that flag and added anis_empty()check to the prefix and suffix matchers, but on the element's attribute value instead of the selector's value. So[a^=""]stopped matchinga=""but still matched every non-empty value.matches_splitted_by_whitespacegot no check, so for~=the empty selector value equals the empty item thatsplit()yields fora=""and for leading, trailing or doubled whitespace.*=was already right becausehas_attr_with_substringreturns 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_suffixandmatches_splitted_by_whitespacereturn false when the selector value is empty. This is the check theselectorscrate does inAttrSelectorOperator::eval_str.src/selectors_vm/tests.rs:compiled_attr_expressionnow checks^=,$=,*=and~=with an empty value, with and withouti, 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_strfor<i a="x"><i a=""><i a=" x"><i a="x "><i a="x y"><i a="-x"><i b="x">:[a^=""],[a^="" i][a$=""],[a$="" i][a~=""],[a~="" i][a*=""],[a*="" i]i:not([a^=""])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_expressionfails 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 fourcargo clippycommands from CI andcargo doc --no-deps --allpass withRUSTFLAGS=-Dwarningson 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