You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Replace CSS escapes that are not needed during canonicalization - #20697
This covers arbitrary values, arbitrary properties, arbitrary variants and arbitrary modifiers (everything in […]). It does not cover the (…) shorthand, like bg-(--my\-color).
What we replace
Escapes of ASCII letters, digits and - in a name, and only if the CSS stays the same. A hex escape has up to 6 hex digits and one optional whitespace after them. In a candidate, an _ is that whitespace.
What we keep
\_, because it is a literal _ in a candidate.
Escapes in strings, e.g. content-['\61'].
Escapes of characters that are not name characters, e.g. \,, \) or \2c.
Escapes that make a name out of something else. E.g.: \31_0 is the name 10, but 10 is a number. 1\65_3 is 1 with the unit e3, but 1e3 is 1000. \-1 is a name, but -1 is a number.
An escape if a hex digit comes after the escape before it. E.g. the \61 in \31\61, because \31a is one escape.
An escape if --> or <!-- would take part of the name, e.g. -\->.
A hex escape with an _ after it, if the _ might not be a space. decodeArbitraryValue keeps an _ in a url(…) and in the first argument of var(…) and theme(…), and an arbitrary property name is not decoded at all. E.g. bg-[url(\61_b.png)] stays as it is.
The escapes in a function name that would become url, var or theme, if the function has an _ in it. E.g. bg-[u\72l(a_b.png)] stays as it is, because the _ would not be a space anymore in url(a_b.png).
Tailwind reads function names as written, so c\61lc(1+2) gets no math operator spacing today, but calc(1+2) does. CSS reads both as calc(…), so the new candidate does what the escaped one was meant to do.
Interaction with the PR that ignores CSS comments
#20690 rejects a function name with a CSS escape in it, like bg-[u\72l('a.png')], because u\72l(foo'bar)/*') could start a CSS comment. After it merges, such a candidate does not parse, and canonicalization could not suggest anything.
So this replacement runs on the raw candidate, before the parse. We use the new candidate only if it parses, and canonicalize it further from there. I tested both cases:
On main, the third candidate already produces the comment, with or without this PR. With #20690, the new candidate does not parse either, so we keep the old one.
The two PRs do not change the same files, and can merge in any order.
Test plan
Example tests in canonicalize-candidates.test.ts, for each strategy (default, with a variant, important, prefix).
A seeded random test: it builds 20,000 values from escaped and plain pieces, in a value, a property name and a selector. It compares the CSS before and after, minified by Lightning CSS, which decodes escapes and keeps only the needed ones. When I turn off one of the rules, this test fails. It found the <!-- case. I also ran it with 4 other seeds and 200,000 values each.
`decodeArbitraryValue` converts an `_` in a string to a space too, but
not in a `url(…)`. So `bg-[u\72l('a_b.png')]` has the URL `a b.png`, and
`bg-[url('a_b.png')]` has the URL `a_b.png`.
We kept the escapes in a function name that would become `url`, `var` or
`theme` only for an `_` outside of strings. Now we count an `_` in a
string too.
Reported by Greptile on #20697.
Review was skipped as selected files did not have any reviewable changes.
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 25226eb1-5de6-4ea0-a3f7-fe7b3972986b
Commits
Reviewing files that changed from the base of the PR and between f7dce03 and 35adcc4.
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
No actionable comments were generated in the recent review.
Walkthrough
The change adds normalizeEscapes to decode eligible CSS escapes within arbitrary candidate values. The canonicalization cache tries the normalized candidate when it differs and parses successfully; otherwise, it continues with the original candidate. Tests cover escaped names and values, preservation cases, and generated candidates whose minified CSS is compared before and after normalization.
Merge Risk: Minimal · previous review f7dce
No merge-blocking issue is established.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title clearly summarizes the main change: replacing unnecessary CSS escapes during canonicalization.
Description check
The description explains the escape-normalization behavior, its limits, interaction with another PR, and the reported test plan. It is directly related to the changeset.
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
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.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
The only quoted piece is 'q'. The seeded comparison cannot reach the string and underscore case in hasUnderscoreInFunction, where u\72l('a_b') changes meaning. Add a piece such as "'_'" or "'q_'" so the generator covers that case.
seed * 1103515245 can reach about 2^61. That is above 2^53, so the low bits are rounded away before & 0x7fffffff. The sequence is deterministic but has weaker low-bit quality than intended. seed % max uses those low bits, so coverage of the pieces can be skewed. Use Math.imul to get exact 32-bit multiplication.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
`canonicalizeCandidates` now replaces CSS escapes in `[…]` with the
character they stand for, when the escape is not needed. E.g.:
`bg-[u\72l('a.png')]` → `bg-[url('a.png')]`.
This runs on the raw candidate, before the parse, because a candidate
with an escape in a function name might not parse at all. We only use
the new candidate if it parses.
We only replace escapes of ASCII letters, digits and `-` in a name, and
only if the CSS stays the same. We keep:
- `\_`, because it is a literal `_` in a candidate.
- Escapes in strings.
- Escapes of other characters, like `\,` or `\)`.
- Escapes that make a name out of something else, like `\31 0px` (the
name `10px`) or `1\65 3` (`1` with the unit `e3`).
- An escape if a hex digit comes after the escape before it, like the
`\61` in `\31\61`.
- An escape if `-->` or `<!--` would take part of the name.
- A hex escape with an `_` after it, if the `_` might not be a space,
like in a `url(…)` or in the property name of an arbitrary property.
- The escapes in a function name that would become `url`, `var` or
`theme`, if the function has an `_` in it. `decodeArbitraryValue`
keeps an `_` in them, so it would not be a space anymore.
The test compares the CSS of random values before and after, minified
by Lightning CSS, which decodes escapes and only keeps those that are
needed.
`decodeArbitraryValue` converts an `_` in a string to a space too, but
not in a `url(…)`. So `bg-[u\72l('a_b.png')]` has the URL `a b.png`, and
`bg-[url('a_b.png')]` has the URL `a_b.png`.
We kept the escapes in a function name that would become `url`, `var` or
`theme` only for an `_` outside of strings. Now we count an `_` in a
string too.
Reported by Greptile on #20697.
… act on (#20728)
This PR keeps `### Changed` in the changelog for changes that users may
have to act on, and documents which section to use.
`[Unreleased]` had a `### Changed` section with two performance entries
(#20680 and
#20688). A performance
improvement is not a change that users have to act on, and the repo
already lists them under `### Fixed` (e.g.
#20694,
#20678 and
#20663).
## Changes
- `CHANGELOG.md`: remove the `### Changed` section of `[Unreleased]`.
Combine the two entries into one entry in `### Fixed`, because
#20688 only extends
#20680 to the other
integrations. The entry goes in the merge order of
#20680.
- `CHANGELOG.md`: move the canonicalization entry of
#20697 from `### Added`
to `### Fixed`, in merge order, and drop the empty `### Added` heading.
A canonicalization improvement is not a new feature for users, and the
canonicalization entries of 4.3.4 and 4.3.5 are in `### Fixed` too.
- `.github/CONTRIBUTING.md`: add a guide on which section to pick. The
examples for `### Changed` come from earlier releases: changes to the
CSS of existing utilities, renamed or replaced utilities (also when the
upgrade tool migrates them), theme defaults, APIs, stricter parsing,
source detection defaults, and supported versions.
- `.changelog/README.md`: link to that guide.
This PR edits `CHANGELOG.md` directly instead of adding a fragment,
because it fixes entries that the "Compile changelog" workflow already
compiled. Released sections stay as they are. The PR changes only docs,
so it needs no changelog entry of its own.
## Test plan
- `pnpm changelog check` passes.
- `pnpm --silent changelog` shows `[Unreleased]` with only `### Fixed`.
- `pnpm prettier --check` passes for the three files.
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
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.
canonicalizeCandidatesnow replaces CSS escapes in[…]with the character that they stand for, if the escape is not needed:This covers arbitrary values, arbitrary properties, arbitrary variants and arbitrary modifiers (everything in
[…]). It does not cover the(…)shorthand, likebg-(--my\-color).What we replace
Escapes of ASCII letters, digits and
-in a name, and only if the CSS stays the same. A hex escape has up to 6 hex digits and one optional whitespace after them. In a candidate, an_is that whitespace.What we keep
\_, because it is a literal_in a candidate.content-['\61'].\,,\)or\2c.\31_0is the name10, but10is a number.1\65_3is1with the unite3, but1e3is1000.\-1is a name, but-1is a number.\61in\31\61, because\31ais one escape.-->or<!--would take part of the name, e.g.-\->._after it, if the_might not be a space.decodeArbitraryValuekeeps an_in aurl(…)and in the first argument ofvar(…)andtheme(…), and an arbitrary property name is not decoded at all. E.g.bg-[url(\61_b.png)]stays as it is.url,varortheme, if the function has an_in it. E.g.bg-[u\72l(a_b.png)]stays as it is, because the_would not be a space anymore inurl(a_b.png).Tailwind reads function names as written, so
c\61lc(1+2)gets no math operator spacing today, butcalc(1+2)does. CSS reads both ascalc(…), so the new candidate does what the escaped one was meant to do.Interaction with the PR that ignores CSS comments
#20690 rejects a function name with a CSS escape in it, like
bg-[u\72l('a.png')], becauseu\72l(foo'bar)/*')could start a CSS comment. After it merges, such a candidate does not parse, and canonicalization could not suggest anything.So this replacement runs on the raw candidate, before the parse. We use the new candidate only if it parses, and canonicalize it further from there. I tested both cases:
main+ this PRbg-[u\72l('a.png')]bg-[url('a.png')]bg-[url('a.png')]bg-[u\72_l('a.png')]bg-[url('a.png')]bg-[url('a.png')]bg-[u\72l(foo'bar)/*')]bg-[url(foo'bar)/*')]bg-[u\72l(a_b.png)]On
main, the third candidate already produces the comment, with or without this PR. With #20690, the new candidate does not parse either, so we keep the old one.The two PRs do not change the same files, and can merge in any order.
Test plan
canonicalize-candidates.test.ts, for each strategy (default, with a variant, important, prefix).<!--case. I also ran it with 4 other seeds and 200,000 values each.canonicalize-candidates.test.tsandcandidate.test.tspass.pnpm run build, and all tests ofpackages/tailwindcss.