Skip to content

Commit c625d31

Browse files
authored
fix #4497: preserve nested ampersands during minification (#4500)
1 parent 34474e2 commit c625d31

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

‎internal/css_parser/css_parser_selector.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,11 @@ const (
252252
func analyzeLeadingAmpersand(sel css_ast.ComplexSelector, isDeclarationContext bool) leadingAmpersand {
253253
if len(sel.Selectors) > 1 {
254254
if first := sel.Selectors[0]; first.IsSingleAmpersand() {
255-
if second := sel.Selectors[1]; second.Combinator.Byte == 0 && len(second.NestingSelectorLocs) > 0 {
255+
second := sel.Selectors[1]
256+
rest := css_ast.ComplexSelector{Selectors: sel.Selectors[1:]}
257+
if second.Combinator.Byte == 0 && rest.ContainsNestingCombinator() {
256258
// ".foo { & &.bar {} }" => ".foo { & &.bar {} }"
259+
// ".foo { & .bar:not(& .baz) {} }" => ".foo { & .bar:not(& .baz) {} }"
257260
} else if second.Combinator.Byte != 0 || second.TypeSelector == nil || !isDeclarationContext {
258261
// "& + div {}" => "+ div {}"
259262
// "& div {}" => "div {}"

‎internal/css_parser/css_parser_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,9 @@ func TestNestedSelector(t *testing.T) {
11371137
expectPrintedMangle(t, "a { @media screen { & div { color: red } } }", "a {\n @media screen {\n & div {\n color: red;\n }\n }\n}\n", "")
11381138
expectPrintedMangle(t, "a { :has(& b) { color: red } }", "a {\n :has(& b) {\n color: red;\n }\n}\n", "")
11391139
expectPrintedMangle(t, "a { :has(& + b) { color: red } }", "a {\n :has(& + b) {\n color: red;\n }\n}\n", "")
1140+
expectPrintedMangle(t, "a { & .b:not(& .c) { color: red } }", "a {\n & .b:not(& .c) {\n color: red;\n }\n}\n", "")
1141+
expectPrintedMangle(t, "a { & .b:is(.c &) { color: red } }", "a {\n & .b:is(.c &) {\n color: red;\n }\n}\n", "")
1142+
expectPrintedMangle(t, "a { & + .b:not(& .c) { color: red } }", "a {\n + .b:not(& .c) {\n color: red;\n }\n}\n", "")
11401143

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

0 commit comments

Comments
 (0)