Skip to content

Commit 78f593e

Browse files
Sqrt calc function implementation
1 parent eb18526 commit 78f593e

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

lib/src/ast/sass/expression/calculation.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class CalculationExpression implements Expression {
5050
}
5151
}
5252

53+
/// Returns a `sqrt()` calculation expression.
54+
CalculationExpression.sqrt(Expression argument, FileSpan span)
55+
: this("sqrt", [argument], span);
56+
5357
/// Returns a `clamp()` calculation expression.
5458
CalculationExpression.clamp(
5559
Expression min, Expression value, Expression max, FileSpan span)

lib/src/parse/stylesheet.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2894,6 +2894,7 @@ abstract class StylesheetParser extends Parser {
28942894
assert(scanner.peekChar() == $lparen);
28952895
switch (name) {
28962896
case "calc":
2897+
case "sqrt":
28972898
var arguments = _calculationArguments(1);
28982899
return CalculationExpression(name, arguments, scanner.spanFrom(start));
28992900

lib/src/value/calculation.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import '../utils.dart';
1111
import '../value.dart';
1212
import '../visitor/interface/value.dart';
1313
import '../visitor/serialize.dart';
14+
import 'dart:math' as math;
1415

1516
/// A SassScript calculation.
1617
///
@@ -120,6 +121,25 @@ class SassCalculation extends Value {
120121
return SassCalculation._("max", args);
121122
}
122123

124+
/// Creates a `sqrt()` calculation with the given [argument].
125+
///
126+
/// Each argument must be either a [SassNumber], a [SassCalculation], an
127+
/// unquoted [SassString], a [CalculationOperation], or a
128+
/// [CalculationInterpolation]. It must be passed at least one argument.
129+
///
130+
/// This automatically simplifies the calculation, so it may return a
131+
/// [SassNumber] rather than a [SassCalculation]. It throws an exception if it
132+
/// can determine that the calculation will definitely produce invalid CSS.
133+
static Value sqrt(Object argument) {
134+
argument = _simplify(argument);
135+
if (argument is! SassNumber) {
136+
return SassCalculation.sqrt(argument);
137+
}
138+
argument.assertNoUnits();
139+
140+
return SassNumber(math.sqrt(argument.value));
141+
}
142+
123143
/// Creates a `clamp()` calculation with the given [min], [value], and [max].
124144
///
125145
/// Each argument must be either a [SassNumber], a [SassCalculation], an

lib/src/visitor/async_evaluate.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2292,7 +2292,7 @@ class _EvaluateVisitor
22922292
var arguments = [
22932293
for (var argument in node.arguments)
22942294
await _visitCalculationValue(argument,
2295-
inMinMax: node.name == 'min' || node.name == 'max')
2295+
inMinMax: {'min', 'max', 'sqrt'}.contains(node.name))
22962296
];
22972297
if (_inSupportsDeclaration) {
22982298
return SassCalculation.unsimplified(node.name, arguments);
@@ -2303,6 +2303,9 @@ class _EvaluateVisitor
23032303
case "calc":
23042304
assert(arguments.length == 1);
23052305
return SassCalculation.calc(arguments[0]);
2306+
case "sqrt":
2307+
assert(arguments.length == 1);
2308+
return SassCalculation.sqrt(arguments[0]);
23062309
case "min":
23072310
return SassCalculation.min(arguments);
23082311
case "max":

lib/src/visitor/evaluate.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// DO NOT EDIT. This file was generated from async_evaluate.dart.
66
// See tool/grind/synchronize.dart for details.
77
//
8-
// Checksum: 17862153344c8577d780b3e039a1ce5ebb774c17
8+
// Checksum: e62a93c1e80688b9131d8c0dedbcc87d061cc8a2
99
//
1010
// ignore_for_file: unused_import
1111

@@ -2281,7 +2281,7 @@ class _EvaluateVisitor
22812281
var arguments = [
22822282
for (var argument in node.arguments)
22832283
_visitCalculationValue(argument,
2284-
inMinMax: node.name == 'min' || node.name == 'max')
2284+
inMinMax: {'min', 'max', 'sqrt'}.contains(node.name))
22852285
];
22862286
if (_inSupportsDeclaration) {
22872287
return SassCalculation.unsimplified(node.name, arguments);
@@ -2292,6 +2292,9 @@ class _EvaluateVisitor
22922292
case "calc":
22932293
assert(arguments.length == 1);
22942294
return SassCalculation.calc(arguments[0]);
2295+
case "sqrt":
2296+
assert(arguments.length == 1);
2297+
return SassCalculation.sqrt(arguments[0]);
22952298
case "min":
22962299
return SassCalculation.min(arguments);
22972300
case "max":

0 commit comments

Comments
 (0)