Skip to content

Commit 4384bad

Browse files
committed
fix #4395 close #4405 close #4406: parens for or
1 parent 9129e00 commit 4384bad

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Fix a regression with CSS media queries ([#4395](https://github.com/evanw/esbuild/issues/4395), [#4405](https://github.com/evanw/esbuild/issues/4405), [#4406](https://github.com/evanw/esbuild/issues/4406))
6+
7+
Version 0.25.11 of esbuild introduced support for parsing media queries. This unintentionally introduced a regression with printing media queries that use the `<media-type> and <media-condition-without-or>` grammar. Specifically, esbuild was failing to wrap an `or` clause with parentheses when inside `<media-condition-without-or>`. This release fixes the regression.
8+
9+
Here is an example:
10+
11+
```css
12+
/* Original code */
13+
@media only screen and ((min-width: 10px) or (min-height: 10px)) {
14+
a { color: red }
15+
}
16+
17+
/* Old output (incorrect) */
18+
@media only screen and (min-width: 10px) or (min-height: 10px) {
19+
a {
20+
color: red;
21+
}
22+
}
23+
24+
/* New output (correct) */
25+
@media only screen and ((min-width: 10px) or (min-height: 10px)) {
26+
a {
27+
color: red;
28+
}
29+
}
30+
```
31+
332
## 0.27.3
433

534
* Preserve URL fragments in data URLs ([#4370](https://github.com/evanw/esbuild/issues/4370))

internal/css_parser/css_parser_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,6 +2639,16 @@ func TestMangleAtMedia(t *testing.T) {
26392639
expectPrintedMangle(t, "@media (a) and ((b) or (c)) { a { color: red } }", "@media (a) and ((b) or (c)) {\n a {\n color: red;\n }\n}\n", "")
26402640
expectPrintedMangle(t, "@media (a) or ((b) and (c)) { a { color: red } }", "@media (a) or ((b) and (c)) {\n a {\n color: red;\n }\n}\n", "")
26412641

2642+
expectPrintedMangle(t, "@media screen and (((a) and (b)) and (c)) { a { color: red } }", "@media screen and (a) and (b) and (c) {\n a {\n color: red;\n }\n}\n", "")
2643+
expectPrintedMangle(t, "@media screen and (((a) or (b)) or (c)) { a { color: red } }", "@media screen and ((a) or (b) or (c)) {\n a {\n color: red;\n }\n}\n", "")
2644+
expectPrintedMangle(t, "@media screen and ((a) and ((b) and (c))) { a { color: red } }", "@media screen and (a) and (b) and (c) {\n a {\n color: red;\n }\n}\n", "")
2645+
expectPrintedMangle(t, "@media screen and ((a) or ((b) or (c))) { a { color: red } }", "@media screen and ((a) or (b) or (c)) {\n a {\n color: red;\n }\n}\n", "")
2646+
2647+
expectPrintedMangle(t, "@media screen and (((a) and (b)) or (c)) { a { color: red } }", "@media screen and (((a) and (b)) or (c)) {\n a {\n color: red;\n }\n}\n", "")
2648+
expectPrintedMangle(t, "@media screen and (((a) or (b)) and (c)) { a { color: red } }", "@media screen and ((a) or (b)) and (c) {\n a {\n color: red;\n }\n}\n", "")
2649+
expectPrintedMangle(t, "@media screen and ((a) and ((b) or (c))) { a { color: red } }", "@media screen and (a) and ((b) or (c)) {\n a {\n color: red;\n }\n}\n", "")
2650+
expectPrintedMangle(t, "@media screen and ((a) or ((b) and (c))) { a { color: red } }", "@media screen and ((a) or ((b) and (c))) {\n a {\n color: red;\n }\n}\n", "")
2651+
26422652
expectPrintedMangle(t, "@media not (not (color)) { a { color: red } }", "@media (color) {\n a {\n color: red;\n }\n}\n", "")
26432653
expectPrintedMangle(t, "@media not (not (not (color))) { a { color: red } }", "@media not (color) {\n a {\n color: red;\n }\n}\n", "")
26442654
expectPrintedMangle(t, "@media not (not (not (not (color)))) { a { color: red } }", "@media (color) {\n a {\n color: red;\n }\n}\n", "")

internal/css_printer/css_printer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,11 @@ func (p *printer) printMediaQuery(query css_ast.MediaQuery, flags mqFlags) {
444444
p.printIdent(q.Type, identNormal, 0)
445445
if q.AndOrNull.Data != nil {
446446
p.print(" and ")
447-
p.printMediaQuery(q.AndOrNull, 0)
447+
var flags mqFlags
448+
if binary, ok := q.AndOrNull.Data.(*css_ast.MQBinary); ok && binary.Op == css_ast.MQBinaryOpOr {
449+
flags = mqNeedsParens
450+
}
451+
p.printMediaQuery(q.AndOrNull, flags)
448452
}
449453

450454
case *css_ast.MQNot:

0 commit comments

Comments
 (0)