Skip to content

css: Only transform local class names that form a whole selector token - #1810

Open
m1san wants to merge 1 commit into
vanilla-extract-css:masterfrom
m1san:fix-local-class-name-boundaries
Open

m1san wants to merge 1 commit into
vanilla-extract-css:masterfrom
m1san:fix-local-class-name-boundaries

Conversation

@m1san

@m1san m1san commented Nov 18, 2026 •

Copy link
Copy Markdown

Fixes #1811

Problem

transformSelector finds local class names in a selector with a plain substring search. The only boundary check is "skip if preceded by .", so any local class name that is also a substring of other selector text gets a . inserted into the middle of it:

Local class name Selector Output
aria x [aria-selected="true"] .x [.aria-selected="true"]
role x [role="option"] .x [.role="option"]
ocus x:focus .x:f.ocus
hover x:hover .x:.hover
tton x button .x bu.tton

With the default hashed identifiers this is rare. With custom identifiers from the bundler plugins' identifiers option, it becomes possible. It also depends on which identifier happens to be assigned to which style, so an unrelated change, such as adding a style or reordering modules, can suddenly produce invalid CSS. In our case the result was an invalid attribute selector that failed the build at minification (lightningcss: No qualified name in attribute selector), in a stylesheet nobody had touched.

Fix

The existing right-to-left replacement loop now accepts a match only if it forms a whole class token:

  • The next character is not an identifier character. Glued class names like && still pass, because the match to the right has already been prefixed with ..
  • It is not inside an attribute selector (an unclosed [ before it).
  • The previous character is not : or #. If it is an identifier character, another match must end there (&&), or the word before it must be an element name (body&).

This adds a few cheap checks per match, with no allocations in the common path. In a synthetic benchmark of 4,000 selectors, transformCss took about 0.2µs more per selector. There is no extra pass over the selector, and the fast path that skips the search is unchanged. &&, body&, :not(.x)&, &[attr], :nth-child(2n of &) and the existing debugName_hash1/debugName_hash1d overlap case all keep working.

Testing

  • Added transformCss tests for the collision cases and the glued cases. The collision test fails on master and passes with this change.
  • pnpm vitest run: all tests pass.

@changeset-bot

changeset-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 495c7e6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@vanilla-extract/css Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@m1san
m1san force-pushed the fix-local-class-name-boundaries branch from ca33fd4 to 9ad2d2c Compare November 18, 2026 05:04
@m1san
m1san force-pushed the fix-local-class-name-boundaries branch from 9ad2d2c to 495c7e6 Compare November 18, 2026 05:11
@m1san
m1san marked this pull request as ready for review November 18, 2026 05:15

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.

Local class names are matched inside other selector tokens (e.g. [aria-selected] → [.aria-selected])

1 participant