Skip to content

Negate combined conditions as a whole in not-* variants - #20664

Open
b84330808 wants to merge 1 commit into
tailwindlabs:mainfrom
b84330808:fix-not-variant-compound-conditions
Open

b84330808 wants to merge 1 commit into
tailwindlabs:mainfrom
b84330808:fix-not-variant-compound-conditions

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Summary

not in a media, supports or container query only negates the condition right after it. When a variant's condition combines several conditions with and or or, not-* puts not in front of the first one only, which gives an invalid query:

@custom-variant tablet (@media (min-width: 768px) and (max-width: 1023px));

not-tablet:flex generates:

@media not (min-width: 768px) and (max-width: 1023px) {
  .not-tablet\:flex {
    display: flex;
  }
}

not (a) and (b) doesn't match the grammar, so browsers ignore the rule, and Lightning CSS drops it from an optimized build. The same happens with the built-in supports-* variant, e.g. not-supports-[(display:grid)_and_(gap:1rem)]:flex, with or, and with @container queries (named or not). @media only screen and (…) has a similar problem: not only screen and (…) is invalid too.

This PR wraps a combined condition in parentheses before negating it, and drops only from a negated media query:

Variant condition Before After
@media (a) and (b) @media not (a) and (b) @media not ((a) and (b))
@media (a) or (b) @media not (a) or (b) @media not ((a) or (b))
@media only screen and (a) @media not only screen and (a) @media not screen and (a)
@supports (a) and (b) @supports not (a) and (b) @supports not ((a) and (b))
@container (a) and (b) @container not (a) and (b) @container not ((a) and (b))
@container name (a) and (b) @container name not (a) and (b) @container name not ((a) and (b))

A single condition, a media type like screen and (a), and conditions that already start with not are negated as before.

Test plan

  • Added a case to the not test in variants.test.ts with custom variants that use and/or in @media and @container (with and without a name), only screen, and not-supports-[(display:grid)_and_(gap:1rem)].
  • With the fix reverted, the new case fails: every rule is dropped and the output is "". With the fix, it passes.
  • vitest run --project tailwindcss: 45 files, 5189 tests passed, 1 skipped.
  • prettier --check on the changed files and pnpm changelog check pass.

@b84330808
b84330808 requested a review from a team as a code owner November 4, 2026 10:47
@greptile-apps

greptile-apps Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with two previously reported, non-blocking query edge cases still open.

Reviews (2) · Last reviewed commit: "Negate combined conditions as a whole in..." · Reviewed by Greptile

// `not (a) and (b)` is invalid, and has to be `not ((a) and (b))`.
function wrapCombinedConditions(query: string) {
query = query.trim()
if (segment(query, ' ').length > 1) return `(${query})`

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.

P2 Tab-separated queries lose styles

wrapCombinedConditions splits only on literal spaces, but the CSS parser preserves tabs. A custom variant with (width > 40rem)\tand\t(height > 30rem) still produces not (width > 40rem)\tand\t(height > 30rem), which browsers ignore. Mixed whitespace can also break previously valid queries: screen\tand (width > 40rem) now gets wrapped inside parentheses.

Read top-level CSS whitespace rather than only spaces, and add tests for these forms.

//
// `only` can't be used together with `not`, which already negates
// the whole media query.
if (parts[0] === 'only') {

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.

P2 Mixed-case queries lose styles

The new only check is case-sensitive, although CSS keywords are not. For @custom-variant tablet (@media ONLY screen and (width > 40rem)), ONLY falls through to the media-type branch and produces the invalid query not ONLY screen and (width > 40rem). Browsers ignore the resulting styles.

Compare the keyword without case and add a mixed-case test so the only fix covers valid CSS spellings.

Suggested change
if (parts[0] === 'only') {
if (parts[0].toLowerCase() === 'only') {

@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

The not-* variant logic now wraps combined conditions when negating container, media, and other conditional-rule conditions. Media negation also handles only queries and leading media types. Tests cover combined media and supports conditions, named and unnamed container conditions, and only screen. A changelog entry describes the change.

Merge Risk: Low · up to 90a76

The not-* variants now negate combined conditions as a whole, which fixes the dropped queries the PR describes. Unusual spellings, such as uppercase ONLY or escaped media-type names in custom variants, may still produce invalid queries. These are rare and can be handled as follow-ups.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: combined and/or conditions are now negated as a whole in not-* variants. It matches the changeset, which wraps combined media, supports, and container condition…
Description check Passed The description explains the bug: not-* variants put not only before the first condition in combined and/or queries, producing invalid CSS. It lists the affected media, supports, and container…
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.
Full details: Title check

Explanation

The title states the main change: combined and/or conditions are now negated as a whole in not-* variants. It matches the changeset, which wraps combined media, supports, and container conditions in parentheses before negating them.

Full details: Description check

Explanation

The description explains the bug: not-* variants put not only before the first condition in combined and/or queries, producing invalid CSS. It lists the affected media, supports, and container cases and the fix, and it describes the added test. This matches the changeset.

  • 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

@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

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: a3b50bf2-bea4-4817-ac9e-4c6439b75277
Commits

Reviewing files that changed from the base of the PR and between 2626ae6 and 1a3c9f5.

Files selected for processing: 3
  • .changelog/not-variant-combined-conditions.md
  • packages/tailwindcss/src/variants.test.ts
  • packages/tailwindcss/src/variants.ts

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

Comment on lines +387 to +395
// `not` only negates the condition right after it, so conditions that are
// combined with `and` or `or` have to be wrapped in parentheses. E.g.:
// `not (a) and (b)` is invalid, and has to be `not ((a) and (b))`.
function wrapCombinedConditions(query: string) {
query = query.trim()
if (segment(query, ' ').length > 1) return `(${query})`
return query
}

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.

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

set -eu
printf '%s\n' '--- variants.ts relevant blocks ---'
nl -ba packages/tailwindcss/src/variants.ts | sed -n '370,515p'
printf '%s\n' '--- segment.ts ---'
nl -ba packages/tailwindcss/src/utils/segment.ts | sed -n '1,180p'
printf '%s\n' '--- tokenizer candidates ---'
rg -n -F --glob '*.ts' -e 'quotes' -e 'quote' -e 'escape' -e 'string' -e 'tokenize' -e 'Tokenizer' packages/tailwindcss/src | head -120
printf '%s\n' '--- relevant tests ---'
rg -n -F --glob '*.test.ts' -e 'not-' -e 'custom-variant' packages/tailwindcss/src | head -160

Repository: tailwindlabs/tailwindcss

Length of output: 37063


Use quote- and escape-aware top-level whitespace handling in both condition paths.

The current space-only segmentation misses tab- and newline-separated conditions. This leaves combined conditions unwrapped and can make the media-type check wrap screen\tand (a) as a parenthesized media type.

Do not use a depth-only whitespace scanner. Reuse or extend the state machine in packages/tailwindcss/src/utils/segment.ts, which already handles quotes, escapes, and bracket nesting. Apply the same top-level whitespace logic to the media-type check and wrapCombinedConditions. Preserve whitespace inside quoted or nested CSS values.

@RobinMalfait
RobinMalfait self-requested a review November 16, 2026 21:32
@RobinMalfait RobinMalfait self-assigned this Nov 16, 2026
`not` only negates the condition right after it. For a condition that
combines several conditions with `and` or `or`, `not-*` produced an
invalid query, e.g. `@media not (min-width: 768px) and (max-width: 1023px)`,
which browsers and Lightning CSS drop. Wrap such conditions in
parentheses before negating them, and drop `only` from a negated media
query.
@RobinMalfait
RobinMalfait force-pushed the fix-not-variant-compound-conditions branch from 1a3c9f5 to 90a7618 Compare November 16, 2026 21:34
@RobinMalfait

Copy link
Copy Markdown
Contributor

I rebased this PR on the latest main and force pushed it.

Sent by an AI assistant on behalf of @RobinMalfait.

@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: 2

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: babbc2b5-d3d1-492a-b0a6-cb7d32390cb5
Commits

Reviewing files that changed from the base of the PR and between 1a3c9f5 and 90a7618.

Files selected for processing: 2

  • packages/tailwindcss/src/variants.test.ts
  • packages/tailwindcss/src/variants.ts

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

//
// `only` can't be used together with `not`, which already negates
// the whole media query.
if (parts[0] === 'only') {

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.

Minor · Functional Correctness · Quick win

Match the only modifier without case sensitivity.

For @custom-variant device (@media ONLY screen and (color)), not-device:flex produces not ONLY screen and (color). The comparison fails, then the media-type branch accepts ONLY. This query is invalid, so the utility does not apply. Media-query keywords are ASCII case-insensitive. Compare a normalized keyword and add an uppercase regression case. (drafts.csswg.org)

// @media {media-type} …
//
// A `not` in front of a media type negates the whole media query.
if (/^[a-z-]+$/i.test(parts[0])) {

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.

Minor · Functional Correctness · Quick win

Recognize escaped media-type identifiers before wrapping.

For @custom-variant device (@media \screen and (color)), this regex rejects the escaped spelling of screen. The fallback produces not (\screen and (color)). A media type cannot appear inside a media condition; this expression evaluates as unknown and never matches, instead of negating screen and (color). Recognize CSS identifier tokens, including escapes, before choosing the wrapping path. Add this case beside the plain media-type coverage. (w3.org)

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.

2 participants