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

Allow adjacent forward slashes in plain CSS expressions #2190

Merged
merged 2 commits into from
Mar 12, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.72.0

* Support adjacent `/`s without whitespace in between when parsing plain CSS
expressions.

* Allow the Node.js `pkg:` importer to load Sass stylesheets for `package.json`
`exports` field entries without extensions.

Expand Down
4 changes: 3 additions & 1 deletion lib/src/parse/css.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class CssParser extends ScssParser {

CssParser(super.contents, {super.url, super.logger});

void silentComment() {
bool silentComment() {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why we allow silent comments at all when parsing plain CSS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't, but we do want to throw an explicit error when we encounter them. This is particularly true because historically LibSass parsed .css files as SCSS and some frameworks took advantage of this, so there are definitely real world cases where the user needs a better error than whatever parsing failure they'd get in plain CSS.

if (inExpression) return false;

var start = scanner.state;
super.silentComment();
error("Silent comments aren't allowed in plain CSS.",
Expand Down
8 changes: 5 additions & 3 deletions lib/src/parse/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ class Parser {

switch (scanner.peekChar(1)) {
case $slash:
silentComment();
return true;
return silentComment();
case $asterisk:
loudComment();
return true;
Expand All @@ -135,12 +134,15 @@ class Parser {
}

/// Consumes and ignores a silent (Sass-style) comment.
///
/// Returns whether the comment was consumed.
@protected
void silentComment() {
bool silentComment() {
scanner.expect("//");
while (!scanner.isDone && !scanner.peekChar().isNewline) {
scanner.readChar();
}
return true;
}

/// Consumes and ignores a loud (CSS-style) comment.
Expand Down
10 changes: 10 additions & 0 deletions lib/src/parse/stylesheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ abstract class StylesheetParser extends Parser {
/// Whether the parser is currently within a parenthesized expression.
var _inParentheses = false;

/// Whether the parser is currently within an expression.
@protected
bool get inExpression => _inExpression;
var _inExpression = false;

/// A map from all variable names that are assigned with `!global` in the
/// current stylesheet to the nodes where they're defined.
///
Expand Down Expand Up @@ -1686,7 +1691,9 @@ abstract class StylesheetParser extends Parser {
}

var start = scanner.state;
var wasInExpression = _inExpression;
var wasInParentheses = _inParentheses;
_inExpression = true;

// We use the convention below of referring to nullable variables that are
// shared across anonymous functions in this method with a trailing
Expand Down Expand Up @@ -2039,11 +2046,13 @@ abstract class StylesheetParser extends Parser {
_inParentheses = wasInParentheses;
var singleExpression = singleExpression_;
if (singleExpression != null) commaExpressions.add(singleExpression);
_inExpression = wasInExpression;
return ListExpression(commaExpressions, ListSeparator.comma,
scanner.spanFrom(beforeBracket ?? start),
brackets: bracketList);
} else if (bracketList && spaceExpressions != null) {
resolveOperations();
_inExpression = wasInExpression;
return ListExpression(spaceExpressions..add(singleExpression_!),
ListSeparator.space, scanner.spanFrom(beforeBracket!),
brackets: true);
Expand All @@ -2054,6 +2063,7 @@ abstract class StylesheetParser extends Parser {
ListSeparator.undecided, scanner.spanFrom(beforeBracket!),
brackets: true);
}
_inExpression = wasInExpression;
return singleExpression_!;
}
}
Expand Down
Loading