Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix @import url("...") in plain CSS #2398

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`,
Expand Down
31 changes: 27 additions & 4 deletions lib/src/parse/css.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would work for most of the cases. However, this would attempt to parse a "interpolated" string. In pure css, technically I can do @import url("a.css#{url-with-a-weird-hash}"); and it should still be valid, but I suppose this will throw interpolation is not allowed in css?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, strings in plain css should probably never be parsed as interpolated, even if they contain sass interpolation syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code isn't actually parsing anything, it's just handling the various formats that the dynamicUrl() function can return. But yes, interpolations will throw errors in plain CSS mode even when they are technically valid CSS—largely because LibSass incorrectly interpreted .css files as SCSS, so we want to highlight places where that's not working. It might be worth revisiting that decision for Dart Sass 3.0.0, but probably not for 2.0.0.

: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() {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading