From 76cfd6b331be205497935a94e922b2eac26b5394 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Fri, 18 Oct 2024 23:32:45 +0000 Subject: [PATCH] Fix `@import url("...")` in plain CSS (#2398) Closes #2397 --- CHANGELOG.md | 2 ++ lib/src/parse/css.dart | 31 +++++++++++++++++++++++++++---- pubspec.yaml | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b93fabaa..ab6796d6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## 1.80.3 +* Fix a bug where `@import url("...")` would crash in plain CSS files. + * Improve consistency of how warnings are emitted by different parts of the compiler. This should result in minimal user-visible changes, but different types of warnings should now respond more reliably to flags like `--quiet`, diff --git a/lib/src/parse/css.dart b/lib/src/parse/css.dart index 050d031ba..4fd9ae2df 100644 --- a/lib/src/parse/css.dart +++ b/lib/src/parse/css.dart @@ -7,6 +7,7 @@ import 'package:string_scanner/string_scanner.dart'; import '../ast/sass.dart'; import '../functions.dart'; +import '../interpolation_buffer.dart'; import 'scss.dart'; /// The set of all function names disallowed in plain CSS. @@ -86,16 +87,38 @@ class CssParser extends ScssParser { ImportRule _cssImportRule(LineScannerState start) { var urlStart = scanner.state; var url = switch (scanner.peekChar()) { - $u || $U => dynamicUrl() as StringExpression, + $u || $U => switch (dynamicUrl()) { + StringExpression string => string.text, + InterpolatedFunctionExpression( + :var name, + arguments: ArgumentInvocation( + positional: [StringExpression string], + named: Map(isEmpty: true), + rest: null, + keywordRest: null, + ), + :var span + ) => + (InterpolationBuffer() + ..addInterpolation(name) + ..writeCharCode($lparen) + ..addInterpolation(string.asInterpolation()) + ..writeCharCode($rparen)) + .interpolation(span), + // This shouldn't be reachable. + var expression => + error("Unsupported plain CSS import.", expression.span) + }, _ => StringExpression(interpolatedString().asInterpolation(static: true)) + .text }; whitespace(); var modifiers = tryImportModifiers(); expectStatementSeparator("@import rule"); - return ImportRule([ - StaticImport(url.text, scanner.spanFrom(urlStart), modifiers: modifiers) - ], scanner.spanFrom(start)); + return ImportRule( + [StaticImport(url, scanner.spanFrom(urlStart), modifiers: modifiers)], + scanner.spanFrom(start)); } ParenthesizedExpression parentheses() { diff --git a/pubspec.yaml b/pubspec.yaml index e63fe63e1..d3ed7ffac 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: sass -version: 1.80.3-dev +version: 1.80.3 description: A Sass implementation in Dart. homepage: https://github.com/sass/dart-sass