Skip to content

Replace CSS escapes that are not needed during canonicalization - #20697

Merged
RobinMalfait merged 2 commits into
mainfrom
robin/open-pr-normalize-escapes-in-canonicalization
Nov 10, 2026
Merged

RobinMalfait merged 2 commits into
mainfrom
robin/open-pr-normalize-escapes-in-canonicalization

Conversation

@RobinMalfait

@RobinMalfait RobinMalfait commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

canonicalizeCandidates now replaces CSS escapes in […] with the character that they stand for, if the escape is not needed:

bg-[u\72l('a.png')]  → bg-[url('a.png')]
bg-[u\72_l('a.png')] → bg-[url('a.png')]
[\63olor:red]        → text-[red]
[&_.f\6f_o]:flex     → [&_.foo]:flex

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:

Candidate main + this PR With #20690 + this PR
bg-[u\72l('a.png')] parses, → bg-[url('a.png')] does not parse, → bg-[url('a.png')]
bg-[u\72_l('a.png')] parses, → bg-[url('a.png')] does not parse, → bg-[url('a.png')]
bg-[u\72l(foo'bar)/*')] parses, → bg-[url(foo'bar)/*')] does not parse, stays as it is
bg-[u\72l(a_b.png)] parses, stays as it is does not parse, stays as it is

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.
  • With the commits of Ignore arbitrary values that start a CSS comment #20690 cherry-picked, canonicalize-candidates.test.ts and candidate.test.ts pass.
  • pnpm run build, and all tests of packages/tailwindcss.

@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 10, 2026 17:54
@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Reviews (4) · Last reviewed commit: "Keep an escaped `url` name if a string i..." · Reviewed by Greptile

RobinMalfait added a commit that referenced this pull request Nov 10, 2026
`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.
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

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: Passed 4

Check name Status Explanation
Title check Passed The title clearly summarizes the main change: replacing unnecessary CSS escapes during canonicalization.
Description check Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

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: fd1d1358-0cc3-48b9-a4a2-7774f696b594
Commits

Reviewing files that changed from the base of the PR and between 8fdccb7 and f7dce03.

Files selected for processing: 2

  • packages/tailwindcss/src/canonicalize-candidates.test.ts
  • packages/tailwindcss/src/canonicalize-candidates.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Nitpick comments: 2

packages/tailwindcss/src/canonicalize-candidates.test.ts: 2

2107-2107: Trivial · Maintainability & Code Quality · Quick win

The fuzz pieces never put _ inside a string.

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.


2132-2135: Trivial · Maintainability & Code Quality · Quick win

The LCG loses precision in JavaScript doubles.

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.

♻️ Proposed fix

-      seed = (seed * 1103515245 + 12345) & 0x7fffffff
-      return seed % max
+      seed = (Math.imul(seed, 1103515245) + 12345) & 0x7fffffff
+      return (seed >>> 8) % max

The replaced > 5_000 threshold may need a recheck after this change.

Review info

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: 85fc2404-532d-4e67-b061-577a0018ede8
Commits

Reviewing files that changed from the base of the PR and between b10c862 and 8fdccb7.

Files selected for processing: 3

  • .changelog/normalize-escapes-in-canonicalization.md
  • packages/tailwindcss/src/canonicalize-candidates.test.ts
  • packages/tailwindcss/src/canonicalize-candidates.ts

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.
@RobinMalfait
RobinMalfait force-pushed the robin/open-pr-normalize-escapes-in-canonicalization branch from f7dce03 to 35adcc4 Compare November 10, 2026 21:29
@RobinMalfait
RobinMalfait merged commit bb21271 into main Nov 10, 2026
13 checks passed
@RobinMalfait
RobinMalfait deleted the robin/open-pr-normalize-escapes-in-canonicalization branch November 10, 2026 21:40
RobinMalfait added a commit that referenced this pull request Nov 16, 2026
… 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.
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.

1 participant