|
| 1 | +// Copyright 2023 Google Inc. Use of this source code is governed by an |
| 2 | +// MIT-style license that can be found in the LICENSE file or at |
| 3 | +// https://opensource.org/licenses/MIT. |
| 4 | + |
| 5 | +import 'package:collection/collection.dart'; |
| 6 | +import 'package:node_interop/js.dart'; |
| 7 | +import 'package:sass/src/node/immutable.dart'; |
| 8 | +import 'package:sass/src/node/utils.dart'; |
| 9 | + |
| 10 | +import '../../value.dart'; |
| 11 | +import '../reflection.dart'; |
| 12 | + |
| 13 | +/// Check that [arg] is a valid argument to a calculation function. |
| 14 | +void _assertCalculationValue(Object arg) => switch (arg) { |
| 15 | + SassNumber() || |
| 16 | + SassString(hasQuotes: false) || |
| 17 | + SassCalculation() || |
| 18 | + CalculationOperation() || |
| 19 | + CalculationInterpolation() => |
| 20 | + null, |
| 21 | + _ => jsThrow(JsError( |
| 22 | + 'Argument `$arg` must be one of SassNumber, unquoted SassString, ' |
| 23 | + 'SassCalculation, CalculationOperation, CalculationInterpolation')), |
| 24 | + }; |
| 25 | + |
| 26 | +/// Check that [arg] is an unquoted string or interpolation. |
| 27 | +bool _isValidClampArg(Object? arg) => switch (arg) { |
| 28 | + CalculationInterpolation() || SassString(hasQuotes: false) => true, |
| 29 | + _ => false, |
| 30 | + }; |
| 31 | + |
| 32 | +/// The JavaScript `SassCalculation` class. |
| 33 | +final JSClass calculationClass = () { |
| 34 | + var jsClass = |
| 35 | + createJSClass('sass.SassCalculation', (Object self, [Object? _]) { |
| 36 | + jsThrow(JsError("new sass.SassCalculation() isn't allowed")); |
| 37 | + }); |
| 38 | + |
| 39 | + jsClass.defineStaticMethods({ |
| 40 | + 'calc': (Object argument) { |
| 41 | + _assertCalculationValue(argument); |
| 42 | + return SassCalculation.unsimplified('calc', [argument]); |
| 43 | + }, |
| 44 | + 'min': (Object arguments) { |
| 45 | + var argList = jsToDartList(arguments).cast<Object>(); |
| 46 | + argList.forEach(_assertCalculationValue); |
| 47 | + return SassCalculation.unsimplified('min', argList); |
| 48 | + }, |
| 49 | + 'max': (Object arguments) { |
| 50 | + var argList = jsToDartList(arguments).cast<Object>(); |
| 51 | + argList.forEach(_assertCalculationValue); |
| 52 | + return SassCalculation.unsimplified('max', argList); |
| 53 | + }, |
| 54 | + 'clamp': (Object min, [Object? value, Object? max]) { |
| 55 | + if ((value == null && !_isValidClampArg(min)) || |
| 56 | + (max == null && ![min, value].any(_isValidClampArg))) { |
| 57 | + jsThrow(JsError('Expected at least one SassString or ' |
| 58 | + 'CalculationInterpolation in `${[ |
| 59 | + min, |
| 60 | + value, |
| 61 | + max |
| 62 | + ].whereNotNull()}`')); |
| 63 | + } |
| 64 | + [min, value, max].whereNotNull().forEach(_assertCalculationValue); |
| 65 | + return SassCalculation.unsimplified( |
| 66 | + 'clamp', [min, value, max].whereNotNull()); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + jsClass.defineMethods({ |
| 71 | + 'assertCalculation': (SassCalculation self, [String? name]) => self, |
| 72 | + }); |
| 73 | + |
| 74 | + jsClass.defineGetters({ |
| 75 | + // The `name` getter is included by default by `createJSClass` |
| 76 | + 'arguments': (SassCalculation self) => ImmutableList(self.arguments), |
| 77 | + }); |
| 78 | + |
| 79 | + getJSClass(SassCalculation.unsimplified('calc', [SassNumber(1)])) |
| 80 | + .injectSuperclass(jsClass); |
| 81 | + return jsClass; |
| 82 | +}(); |
| 83 | + |
| 84 | +/// The JavaScript `CalculationOperation` class. |
| 85 | +final JSClass calculationOperationClass = () { |
| 86 | + var jsClass = createJSClass('sass.CalculationOperation', |
| 87 | + (Object self, String strOperator, Object left, Object right) { |
| 88 | + var operator = CalculationOperator.values |
| 89 | + .firstWhereOrNull((value) => value.operator == strOperator); |
| 90 | + if (operator == null) { |
| 91 | + jsThrow(JsError('Invalid operator: $strOperator')); |
| 92 | + } |
| 93 | + _assertCalculationValue(left); |
| 94 | + _assertCalculationValue(right); |
| 95 | + return SassCalculation.operateInternal(operator, left, right, |
| 96 | + inMinMax: false, simplify: false); |
| 97 | + }); |
| 98 | + |
| 99 | + jsClass.defineMethods({ |
| 100 | + 'equals': (CalculationOperation self, Object other) => self == other, |
| 101 | + 'hashCode': (CalculationOperation self) => self.hashCode, |
| 102 | + }); |
| 103 | + |
| 104 | + jsClass.defineGetters({ |
| 105 | + 'operator': (CalculationOperation self) => self.operator.operator, |
| 106 | + 'left': (CalculationOperation self) => self.left, |
| 107 | + 'right': (CalculationOperation self) => self.right, |
| 108 | + }); |
| 109 | + |
| 110 | + getJSClass(SassCalculation.operateInternal( |
| 111 | + CalculationOperator.plus, SassNumber(1), SassNumber(1), |
| 112 | + inMinMax: false, simplify: false)) |
| 113 | + .injectSuperclass(jsClass); |
| 114 | + return jsClass; |
| 115 | +}(); |
| 116 | + |
| 117 | +/// The JavaScript `CalculationInterpolation` class. |
| 118 | +final JSClass calculationInterpolationClass = () { |
| 119 | + var jsClass = createJSClass('sass.CalculationInterpolation', |
| 120 | + (Object self, String value) => CalculationInterpolation(value)); |
| 121 | + |
| 122 | + jsClass.defineMethods({ |
| 123 | + 'equals': (CalculationInterpolation self, Object other) => self == other, |
| 124 | + 'hashCode': (CalculationInterpolation self) => self.hashCode, |
| 125 | + }); |
| 126 | + |
| 127 | + jsClass.defineGetters({ |
| 128 | + 'value': (CalculationInterpolation self) => self.value, |
| 129 | + }); |
| 130 | + |
| 131 | + getJSClass(CalculationInterpolation('')).injectSuperclass(jsClass); |
| 132 | + return jsClass; |
| 133 | +}(); |
0 commit comments