@@ -1218,6 +1218,34 @@ abortRuleParser:
12181218 if path , r , ok := p .expectURLOrString (); ok {
12191219 var conditions css_ast.ImportConditions
12201220 importConditionsStart := p .index
1221+
1222+ // Parse the optional "layer()"
1223+ p .eat (css_lexer .TWhitespace )
1224+ if (p .peek (css_lexer .TIdent ) || p .peek (css_lexer .TFunction )) && strings .EqualFold (p .decoded (), "layer" ) {
1225+ p .parseComponentValue ()
1226+ conditions .Layers = p .convertTokens (p .tokens [importConditionsStart :p .index ])
1227+ importConditionsStart = p .index
1228+
1229+ // Remove leading and trailing whitespace
1230+ if len (conditions .Layers ) > 0 {
1231+ conditions .Layers [0 ].Whitespace &= ^ (css_ast .WhitespaceBefore | css_ast .WhitespaceAfter )
1232+ }
1233+ }
1234+
1235+ // Parse the optional "supports()"
1236+ p .eat (css_lexer .TWhitespace )
1237+ if p .peek (css_lexer .TFunction ) && strings .EqualFold (p .decoded (), "supports" ) {
1238+ p .parseComponentValue ()
1239+ conditions .Supports = p .convertTokens (p .tokens [importConditionsStart :p .index ])
1240+ importConditionsStart = p .index
1241+
1242+ // Remove leading and trailing whitespace
1243+ if len (conditions .Supports ) > 0 {
1244+ conditions .Supports [0 ].Whitespace &= ^ (css_ast .WhitespaceBefore | css_ast .WhitespaceAfter )
1245+ }
1246+ }
1247+
1248+ // Parse the optional media query list
12211249 for {
12221250 if kind := p .current ().Kind ; kind == css_lexer .TSemicolon || kind == css_lexer .TOpenBrace ||
12231251 kind == css_lexer .TCloseBrace || kind == css_lexer .TEndOfFile {
@@ -1229,37 +1257,15 @@ abortRuleParser:
12291257 break // Avoid parsing an invalid "@import" rule
12301258 }
12311259 conditions .Media = p .convertTokens (p .tokens [importConditionsStart :p .index ])
1260+ if n := len (conditions .Media ); n > 0 {
1261+ conditions .Media [0 ].Whitespace &= ^ css_ast .WhitespaceBefore
1262+ conditions .Media [n - 1 ].Whitespace &= ^ css_ast .WhitespaceAfter
1263+ }
12321264
1233- // Insert or remove whitespace before the first token
1265+ // Check whether any import conditions are present
12341266 var importConditions * css_ast.ImportConditions
1235- if len (conditions .Media ) > 0 {
1267+ if len (conditions .Layers ) > 0 || len ( conditions . Supports ) > 0 || len ( conditions . Media ) > 0 {
12361268 importConditions = & conditions
1237-
1238- // Handle "layer()"
1239- if t := conditions .Media [0 ]; (t .Kind == css_lexer .TIdent || t .Kind == css_lexer .TFunction ) && strings .EqualFold (t .Text , "layer" ) {
1240- conditions .Layers = conditions .Media [:1 ]
1241- conditions .Media = conditions .Media [1 :]
1242- }
1243-
1244- // Handle "supports()"
1245- if len (conditions .Media ) > 0 {
1246- if t := conditions .Media [0 ]; t .Kind == css_lexer .TFunction && strings .EqualFold (t .Text , "supports" ) {
1247- conditions .Supports = conditions .Media [:1 ]
1248- conditions .Media = conditions .Media [1 :]
1249- }
1250- }
1251-
1252- // Remove leading and trailing whitespace
1253- if len (conditions .Layers ) > 0 {
1254- conditions .Layers [0 ].Whitespace &= ^ (css_ast .WhitespaceBefore | css_ast .WhitespaceAfter )
1255- }
1256- if len (conditions .Supports ) > 0 {
1257- conditions .Supports [0 ].Whitespace &= ^ (css_ast .WhitespaceBefore | css_ast .WhitespaceAfter )
1258- }
1259- if n := len (conditions .Media ); n > 0 {
1260- conditions .Media [0 ].Whitespace &= ^ css_ast .WhitespaceBefore
1261- conditions .Media [n - 1 ].Whitespace &= ^ css_ast .WhitespaceAfter
1262- }
12631269 }
12641270
12651271 p .expect (css_lexer .TSemicolon )
0 commit comments