Skip to content

Commit 209db54

Browse files
committed
release notes for css nesting bugfix
1 parent c625d31 commit 209db54

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@
1111
import Alias = Base.SomeType;
1212
```
1313

14+
* Fix CSS minification bug involving `&` ([#4497](https://github.com/evanw/esbuild/issues/4497))
15+
16+
This release fixes a bug where esbuild's CSS minifier incorrectly removed a `&` when it was unsafe to do so. Here is an example:
17+
18+
```css
19+
/* Original code */
20+
.a .b {
21+
& .b:not(& .c) {
22+
color: red;
23+
}
24+
}
25+
26+
/* Old output (with --minify) */
27+
.a .b{.b:not(& .c){color:red}}
28+
29+
/* New output (with --minify) */
30+
.a .b{& .b:not(& .c){color:red}}
31+
```
32+
33+
This should match `<span class="a"><span class="b"><span class="b">yes</span></span></span>` but not `<span class="a"><span class="b">no</span></span>`. The old output incorrectly matched both.
34+
1435
* Avoid overwriting input files without `--allow-overwrite` ([#4484](https://github.com/evanw/esbuild/issues/4484))
1536

1637
For example: `esbuild input.js --outfile=input.js` tells esbuild to overwrite `input.js` with the output of running esbuild on it. This was supposed to already be prevented by default, but it accidentally regressed in version 0.17.0 and apparently didn't have any test coverage. The error message was being printed but the input file was still being overwritten. Oops.

‎internal/css_parser/css_parser_selector.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ func analyzeLeadingAmpersand(sel css_ast.ComplexSelector, isDeclarationContext b
257257
if second.Combinator.Byte == 0 && rest.ContainsNestingCombinator() {
258258
// ".foo { & &.bar {} }" => ".foo { & &.bar {} }"
259259
// ".foo { & .bar:not(& .baz) {} }" => ".foo { & .bar:not(& .baz) {} }"
260+
261+
// The specification says: "If a selector in the <relative-selector-list>
262+
// does not start with a combinator but does contain the nesting selector,
263+
// it is interpreted as a non-relative selector." So we don't want to
264+
// remove the leading "&" here as that would change the meaning to a
265+
// non-relative selector. See: https://www.w3.org/TR/css-nesting-1/
260266
} else if second.Combinator.Byte != 0 || second.TypeSelector == nil || !isDeclarationContext {
261267
// "& + div {}" => "+ div {}"
262268
// "& div {}" => "div {}"
@@ -267,6 +273,30 @@ func analyzeLeadingAmpersand(sel css_ast.ComplexSelector, isDeclarationContext b
267273
} else {
268274
// ".foo { & div {} }"
269275
// ".foo { .bar, & div {} }" => ".foo { .bar, div {} }"
276+
277+
// The first iteration of CSS nesting didn't support an implicit "&" if
278+
// the selector started with an identifier. This was done to avoid
279+
// confusion with declarations in the parser, as the parser would
280+
// otherwise need unbounded lookahead to distinguish between the two.
281+
// Consider the following:
282+
//
283+
// span {
284+
// color:blue;
285+
// div:hover span {}
286+
// }
287+
//
288+
// The "color:blue" is a declaration while "div:hover" is actually a
289+
// relative selector in a nested style rule. The parser would need to
290+
// scan to the ";" or "{" to distinguish between the two cases.
291+
// Generally you want to avoid parsers with unbounded lookahead.
292+
//
293+
// However, this edge case is commonly encountered and it's confusing
294+
// that it doesn't work. So the CSS working group later added unbounded
295+
// lookahead to fix it: https://github.com/w3c/csswg-drafts/issues/7961.
296+
//
297+
// We deliberately don't take advantage of that newest syntax addition
298+
// here so that our output still works in browsers before that newest
299+
// syntax addition. We still keep a leading "&" before identifiers.
270300
return canRemoveLeadingAmpersandIfNotFirst
271301
}
272302
}

‎internal/css_parser/css_parser_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ func TestNestedSelector(t *testing.T) {
11401140
expectPrintedMangle(t, "a { & .b:not(& .c) { color: red } }", "a {\n & .b:not(& .c) {\n color: red;\n }\n}\n", "")
11411141
expectPrintedMangle(t, "a { & .b:is(.c &) { color: red } }", "a {\n & .b:is(.c &) {\n color: red;\n }\n}\n", "")
11421142
expectPrintedMangle(t, "a { & + .b:not(& .c) { color: red } }", "a {\n + .b:not(& .c) {\n color: red;\n }\n}\n", "")
1143+
expectPrintedMangle(t, "a { & + .b:is(& .c) { color: red } }", "a {\n + .b:is(& .c) {\n color: red;\n }\n}\n", "")
11431144

11441145
// Reorder selectors to enable removing "&"
11451146
expectPrintedMangle(t, "reorder { & first, .second { color: red } }", "reorder {\n .second,\n first {\n color: red;\n }\n}\n", "")

‎scripts/browser/index.html‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,34 @@
302302
`,
303303
})
304304
},
305+
306+
// See: https://github.com/evanw/esbuild/issues/4497
307+
async cssNestingIssue4497() {
308+
await assertSameColorsWithNestingTransform(esbuild, {
309+
css: `
310+
.a {
311+
color: red;
312+
313+
.b:not(.a .b .b) {
314+
color: green;
315+
316+
& .b:not(& .b .b) {
317+
color: blue;
318+
}
319+
}
320+
}
321+
`,
322+
html: `
323+
<div class="a">
324+
<span>1</span>
325+
<div class="b">
326+
<span>2</span>
327+
<div class="b"><span>3</span></div>
328+
</div>
329+
</div>
330+
`,
331+
})
332+
},
305333
})
306334

307335
async function runTest(test, fn) {

0 commit comments

Comments
 (0)