-
Notifications
You must be signed in to change notification settings - Fork 371
Add support for plain-CSS if() #2693
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
Open
nex3
wants to merge
4
commits into
main
Choose a base branch
from
css-if
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Copyright 2025 Google Inc. Use of this source code is governed by an | ||
| // MIT-style license that can be found in the LICENSE file or at | ||
| // https://opensource.org/licenses/MIT. | ||
|
|
||
| /// An enum for binary boolean operations. | ||
| /// | ||
| /// Currently CSS only supports conjunctions (`and`) and disjunctions (`or`). | ||
| enum BooleanOperator { | ||
| and, | ||
| or; | ||
|
|
||
| String toString() => name; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,269 @@ | ||
| // Copyright 2016 Google Inc. Use of this source code is governed by an | ||
| // Copyright 2025 Google Inc. Use of this source code is governed by an | ||
| // MIT-style license that can be found in the LICENSE file or at | ||
| // https://opensource.org/licenses/MIT. | ||
|
|
||
| import 'package:charcode/charcode.dart'; | ||
| import 'package:meta/meta.dart'; | ||
| import 'package:source_span/source_span.dart'; | ||
|
|
||
| import '../../../ast/node.dart'; | ||
| import '../../../ast/sass.dart'; | ||
| import '../../../interpolation_buffer.dart'; | ||
| import '../../../util/lazy_file_span.dart'; | ||
| import '../../../visitor/interface/expression.dart'; | ||
| import '../../../visitor/interface/if_condition_expression.dart'; | ||
|
|
||
| /// A ternary expression. | ||
| /// A CSS `if()` expression. | ||
| /// | ||
| /// This is defined as a separate syntactic construct rather than a normal | ||
| /// function because only one of the `$if-true` and `$if-false` arguments are | ||
| /// evaluated. | ||
| /// In addition to supporting the plain-CSS syntax, this supports a `sass()` | ||
| /// condition that evaluates SassScript expressions. | ||
| /// | ||
| /// {@category AST} | ||
| final class IfExpression extends Expression implements CallableInvocation { | ||
| /// The declaration of `if()`, as though it were a normal function. | ||
| static final declaration = ParameterList.parse( | ||
| r"@function if($condition, $if-true, $if-false) {", | ||
| ); | ||
|
|
||
| /// The arguments passed to `if()`. | ||
| final ArgumentList arguments; | ||
| final class IfExpression extends Expression { | ||
| /// The conditional branches that make up the `if()`. | ||
| /// | ||
| /// A `null` expression indicates an `else` branch that is always evaluated. | ||
| final List<(IfConditionExpression?, Expression)> branches; | ||
|
|
||
| final FileSpan span; | ||
|
|
||
| IfExpression(this.arguments, this.span); | ||
| IfExpression( | ||
| Iterable<(IfConditionExpression?, Expression)> branches, this.span) | ||
| : branches = List.unmodifiable(branches) { | ||
| if (this.branches.isEmpty) { | ||
| throw ArgumentError.value(this.branches, "branches", "may not be empty"); | ||
| } | ||
| } | ||
|
|
||
| T accept<T>(ExpressionVisitor<T> visitor) => visitor.visitIfExpression(this); | ||
|
|
||
| String toString() => "if$arguments"; | ||
| String toString() { | ||
| var buffer = StringBuffer("if("); | ||
| var first = true; | ||
| for (var (condition, expression) in branches) { | ||
| if (first) { | ||
| first = false; | ||
| } else { | ||
| buffer.write("; "); | ||
| } | ||
|
|
||
| buffer.write(condition ?? "else"); | ||
| buffer.write(": "); | ||
| buffer.write(expression); | ||
| } | ||
| buffer.writeCharCode($rparen); | ||
| return buffer.toString(); | ||
| } | ||
| } | ||
|
|
||
| /// The parent class of conditions in an [IfExpression]. | ||
| /// | ||
| /// {@category AST} | ||
| sealed class IfConditionExpression implements SassNode { | ||
| /// Returns whether this is an arbitrary substitution expression which may be | ||
| /// replaced with multiple tokens at evaluation or render time. | ||
| /// | ||
| /// @nodoc | ||
| @internal | ||
| bool get isArbitrarySubstitution => false; | ||
|
|
||
| /// Converts this expression into an interpolation that produces the same | ||
| /// value. | ||
| /// | ||
| /// Throws a [SourceSpanFormatException] if this contains an | ||
| /// [IfConditionSass]. [arbitrarySubstitution]'s span is used for this error. | ||
| /// | ||
| /// @nodoc | ||
| @internal | ||
| Interpolation toInterpolation(AstNode arbitrarySubstitution); | ||
|
|
||
| /// Calls the appropriate visit method on [visitor]. | ||
| T accept<T>(IfConditionExpressionVisitor<T> visitor); | ||
| } | ||
|
|
||
| /// A parenthesized condition. | ||
| /// | ||
| /// {@category AST} | ||
| final class IfConditionParenthesized extends IfConditionExpression { | ||
| /// The parenthesized expression. | ||
| final IfConditionExpression expression; | ||
|
|
||
| final FileSpan span; | ||
|
|
||
| IfConditionParenthesized(this.expression, this.span); | ||
|
|
||
| /// @nodoc | ||
| @internal | ||
| Interpolation toInterpolation(AstNode arbitrarySubstitution) => | ||
| (InterpolationBuffer() | ||
| ..writeCharCode($lparen) | ||
| ..addInterpolation( | ||
| expression.toInterpolation(arbitrarySubstitution)) | ||
| ..writeCharCode($rparen)) | ||
| .interpolation(span); | ||
|
|
||
| T accept<T>(IfConditionExpressionVisitor<T> visitor) => | ||
| visitor.visitIfConditionParenthesized(this); | ||
|
|
||
| String toString() => "($expression)"; | ||
| } | ||
|
|
||
| /// A negated condition. | ||
| /// | ||
| /// {@category AST} | ||
| final class IfConditionNegation extends IfConditionExpression { | ||
| /// The expression negated by this. | ||
| final IfConditionExpression expression; | ||
|
|
||
| final FileSpan span; | ||
|
|
||
| IfConditionNegation(this.expression, this.span); | ||
|
|
||
| /// @nodoc | ||
| @internal | ||
| Interpolation toInterpolation(AstNode arbitrarySubstitution) => | ||
| (InterpolationBuffer() | ||
| ..write('not ') | ||
| ..addInterpolation( | ||
| expression.toInterpolation(arbitrarySubstitution))) | ||
| .interpolation(span); | ||
|
|
||
| T accept<T>(IfConditionExpressionVisitor<T> visitor) => | ||
| visitor.visitIfConditionNegation(this); | ||
|
|
||
| String toString() => "not $expression"; | ||
| } | ||
|
|
||
| /// A sequence of `and`s or `or`s. | ||
| /// | ||
| /// {@category AST} | ||
| final class IfConditionOperation extends IfConditionExpression { | ||
| /// The expressions conjoined or disjoined by this operation. | ||
| final List<IfConditionExpression> expressions; | ||
|
|
||
| final BooleanOperator op; | ||
|
|
||
| FileSpan get span => expressions.first.span.expand(expressions.last.span); | ||
|
|
||
| IfConditionOperation(Iterable<IfConditionExpression> expressions, this.op) | ||
| : expressions = List.unmodifiable(expressions) { | ||
| if (this.expressions.length < 2) { | ||
| throw ArgumentError.value( | ||
| this.expressions, "expressions", "must have length >= 2"); | ||
| } | ||
| } | ||
|
|
||
| /// @nodoc | ||
| @internal | ||
| Interpolation toInterpolation(AstNode arbitrarySubstitution) { | ||
| var buffer = InterpolationBuffer(); | ||
| var first = true; | ||
| for (var expression in expressions) { | ||
| if (first) { | ||
| first = false; | ||
| } else { | ||
| buffer.write(' $op '); | ||
| } | ||
| buffer | ||
| .addInterpolation(expression.toInterpolation(arbitrarySubstitution)); | ||
| } | ||
| return buffer.interpolation(LazyFileSpan(() => span)); | ||
| } | ||
|
|
||
| T accept<T>(IfConditionExpressionVisitor<T> visitor) => | ||
| visitor.visitIfConditionOperation(this); | ||
|
|
||
| String toString() => expressions.join(" $op "); | ||
| } | ||
|
|
||
| /// A plain-CSS function-style condition. | ||
| /// | ||
| /// {@category AST} | ||
| final class IfConditionFunction extends IfConditionExpression { | ||
| /// The name of the function being called. | ||
| final Interpolation name; | ||
|
|
||
| /// The arguments passed to the function call. | ||
| final Interpolation arguments; | ||
|
|
||
| final FileSpan span; | ||
|
|
||
| /// @nodoc | ||
| @internal | ||
| bool get isArbitrarySubstitution => switch (name.asPlain?.toLowerCase()) { | ||
| "if" || "var" || "attr" => true, | ||
| var str? when str.startsWith("--") => true, | ||
| _ => false, | ||
| }; | ||
|
|
||
| IfConditionFunction(this.name, this.arguments, this.span); | ||
|
|
||
| /// @nodoc | ||
| @internal | ||
| Interpolation toInterpolation(AstNode _) => (InterpolationBuffer() | ||
| ..addInterpolation(name) | ||
| ..writeCharCode($lparen) | ||
| ..addInterpolation(arguments) | ||
| ..writeCharCode($rparen)) | ||
| .interpolation(span); | ||
|
|
||
| T accept<T>(IfConditionExpressionVisitor<T> visitor) => | ||
| visitor.visitIfConditionFunction(this); | ||
|
|
||
| String toString() => "$name($arguments)"; | ||
| } | ||
|
|
||
| /// A Sass condition that will evaluate to true or false at compile time. | ||
| /// | ||
| /// {@category AST} | ||
| final class IfConditionSass extends IfConditionExpression { | ||
| /// The expression that determines whether this condition matches. | ||
| final Expression expression; | ||
|
|
||
| final FileSpan span; | ||
|
|
||
| IfConditionSass(this.expression, this.span); | ||
|
|
||
| /// @nodoc | ||
| @internal | ||
| Interpolation toInterpolation(AstNode arbitrarySubstitution) => | ||
| throw MultiSourceSpanFormatException( | ||
| 'if() conditions with arbitrary substitutions may not contain sass() ' | ||
| 'expressions.', | ||
| arbitrarySubstitution.span, | ||
| "arbitrary substitution", | ||
| {span: "sass() expression"}); | ||
|
|
||
| T accept<T>(IfConditionExpressionVisitor<T> visitor) => | ||
| visitor.visitIfConditionSass(this); | ||
|
|
||
| String toString() => "sass($expression)"; | ||
| } | ||
|
|
||
| /// A chunk of raw text, possibly with interpolations. | ||
| /// | ||
| /// This is used to represent explicit interpolation, as well as whole | ||
| /// expressions where arbitrary substitutions are used in place of operators. | ||
| /// | ||
| /// {@category AST} | ||
| final class IfConditionRaw extends IfConditionExpression { | ||
| /// The text that encompasses this condition. | ||
| final Interpolation text; | ||
|
|
||
| FileSpan get span => text.span; | ||
|
|
||
| /// @nodoc | ||
| @internal | ||
| bool get isArbitrarySubstitution => true; | ||
|
|
||
| IfConditionRaw(this.text); | ||
|
|
||
| /// @nodoc | ||
| @internal | ||
| Interpolation toInterpolation(AstNode _) => text; | ||
|
|
||
| T accept<T>(IfConditionExpressionVisitor<T> visitor) => | ||
| visitor.visitIfConditionRaw(this); | ||
|
|
||
| String toString() => text.toString(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if()with not branches is considered a valid (if not useful) plain CSS expression, at least according to MDN. Is the decision to require at least one branch in Sass intentional here?