Repository navigation
css: keep required grouping parens in @container conditions when minifying - #31330
Conversation
|
Updated 4:29 AM PT - May 24th, 2026
✅ @robobun, your commit 1f051aaf12f61fa3b1fb3b1c792612e86e90efea passed in 🧪 To try this PR locally: bunx bun-pr 31330That installs a local version of the PR into your bun-31330 --bun |
|
Warning Review limit reached
Your plan currently allows 1 review/hour. Refill in 35 minutes and 19 seconds. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more review capacity refills, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
Comment |
There was a problem hiding this comment.
LGTM — two-character fix flipping an inverted operator comparison to match MediaCondition::needs_parens, with thorough test coverage.
Extended reasoning...
Overview
This PR flips == to != in two needs_parens implementations (StyleQuery and ContainerCondition) in src/css/rules/container.rs. The previous comparison was inverted: it added grouping parentheses when a nested @container operation had the same operator as its parent (redundant) and dropped them when the operator differed or the parent was not (required by the CSS grammar). The result was minified output that Bun's own parser rejected. A new describe("container") block in test/js/bun/css/css.test.ts adds 16 minification/roundtrip cases covering not, mixed-operator, and style() queries.
Security risks
None. This is pure CSS serialization logic — no I/O, auth, or untrusted-input handling beyond what the CSS parser already does.
Level of scrutiny
Low. The fix is a minimal, mechanical correction that brings these two impls into exact alignment with MediaCondition::needs_parens (media_query.rs:881, which already uses !=) and with SupportsCondition::needs_parens (which follows the same "parens when parent differs" pattern). I verified the reference implementation in the codebase matches the PR description's claim. The grammar reasoning is correct: not takes a single <query-in-parens>, and and/or cannot be mixed at the same level without grouping, so Some(op) != parent_operator (which is true both under not where parent_operator == None and under a different operator) is the right predicate.
Other factors
The bug-hunting system found no issues. The added tests are comprehensive — they cover both the negative cases (parens preserved under not and mixed operators) and the positive flattening case (same-operator nesting drops redundant parens), plus idempotence checks that the minified forms reparse to themselves. The PR description notes outputs now match lightningcss 1.32.0 byte-for-byte.
* oven/main (20 new commits): webcore: free Blob's owned content type on drop (oven-sh#31358) Support cross-compiling macOS binaries from Linux (oven-sh#31303) test: forward keep-alive requests in proxy.test.ts's mock proxy (oven-sh#31352) Port Bun.stringWidth to C++ with explicit SIMD (oven-sh#31351) Fix quadratic hang reporting duplicate-binding parse errors in the transpiler (oven-sh#31341) shell: don't abort when a glob's directory prefix doesn't exist (oven-sh#31367) Error instead of crashing on deeply nested statements in the transpiler (oven-sh#31333) Fix JSX transform panic when a bare `key` prop precedes `key` with a value (oven-sh#31350) Cap ANSI markdown indentation so deeply nested lists render in linear time (oven-sh#31366) css: bound selector-list expansion when compiling nesting for older targets (oven-sh#31277) node:http2: reassemble HEADERS+CONTINUATION before HPACK decoding (oven-sh#31323) Fix `await using` expression printing `using` as `await` (oven-sh#31324) Parenthesize `async` when it starts a for-of loop initializer (oven-sh#31326) Print Infinity and negative numeric property keys as computed properties (oven-sh#31328) css: keep required grouping parens in @container conditions when minifying (oven-sh#31330) Fix panic on anonymous export default class with an auto-accessor field (oven-sh#31331) node:http2: send GOAWAY frames on stream 0 (oven-sh#31353) parser: fix Scope mismatch while visiting panic from decorators on dropped class members (oven-sh#31340) webcrypto: reject oversized BufferSource inputs instead of aborting (oven-sh#31356) Error instead of crashing on deeply nested TypeScript types in the transpiler (oven-sh#31361) Resolved conflicts: - scripts/build.ts: kept both OHOS and macOS-cross argv entries - scripts/build/config.ts: kept both OHOS and macOS-cross config fields - scripts/build/deps/webkit.ts: kept OHOS fno-pic exclusion, adopted upstream -flto=thin
What does this PR do?
Fixes a CSS fuzzer invariant violation (
invariant:css:minified CSS does not reparse):@containerconditions with a nested and/or group lose the grouping parentheses the grammar requires, so Bun's own CSS parser can't read its minified output back.Repro:
notonly takes a single parenthesized query, sonot ((a) and (b))must keep the group — the re-printednot (a) and (b)is a parse error. The same happens for realistic inputs, in both size and style container queries, and also when mixing operators:@container not ((width > 100px) and (height > 100px))@container not (width>100px) and (height>100px)Unexpected token: and@container style(not ((width: 1px) and (height: 2px)))@container style(not (width:1px) and (height:2px))@container ((width > 100px) or (height > 100px)) and (orientation: landscape)@container (width>100px) or (height>100px) and (orientation:landscape)and/orwithout parens)Any such stylesheet breaks when Bun-minified CSS is fed back through
bun build/ CSS imports, and@mediahandles all of these correctly — only@containeris affected.Root cause
needs_parensforContainerConditionandStyleQueryinsrc/css/rules/container.rshas the operator comparison inverted:It adds parentheses when a nested group has the same operator as its parent (where they're redundant) and drops them when the operator differs or the parent is
not(parent_operator == None) — exactly the positions where the grammar requires<query-in-parens>/<style-in-parens>.MediaCondition::needs_parensinmedia_query.rsalready uses!=, which is why the equivalent@mediaqueries roundtrip fine.The fix
Flip the comparison to
!=in bothStyleQuery::needs_parensandContainerCondition::needs_parens, matchingMediaConditionand current lightningcss. Nested groups now keep their parens undernotand under a parent with a different operator, and same-operator nesting flattens (((a) and (b)) and (c)→(a) and (b) and (c)), which is smaller and still equivalent.All the outputs above now match lightningcss 1.32.0 byte-for-byte, and the fuzzer input roundtrips:
@container style(not ((w:)and (r:))){{c:→@container style(not ((w:) and (r:))){{c:}}→ reparses to itself.Verification
describe("container")block intest/js/bun/css/css.test.ts: minification ofnot/mixed-operator/style() groups plus reparsing of the minified forms. Without the fix 12 of the 16 tests fail; with it all pass.test/js/bun/css/css.test.ts+doesnt_crash.test.ts+color.test.tson the debug build: no new failures (the only failures are pre-existing timeouts of iteration-heavy fuzz/ANSI-color tests that also time out on unmodified main in the same environment and don't involve@container).