/
value.dart
349 lines (304 loc) · 12.8 KB
/
value.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2016 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:meta/meta.dart';
import 'ast/selector.dart';
import 'exception.dart';
import 'value/boolean.dart';
import 'value/color.dart';
import 'value/external/value.dart' as ext;
import 'value/function.dart';
import 'value/list.dart';
import 'value/map.dart';
import 'value/number.dart';
import 'value/string.dart';
import 'visitor/interface/value.dart';
import 'visitor/serialize.dart';
export 'value/argument_list.dart';
export 'value/boolean.dart';
export 'value/color.dart';
export 'value/function.dart';
export 'value/list.dart';
export 'value/map.dart';
export 'value/null.dart';
export 'value/number.dart';
export 'value/string.dart';
// TODO(nweiz): Just mark members as @internal when sdk#28066 is fixed.
/// The implementation of [ext.Value].
///
/// This is a separate class to avoid exposing more API surface than necessary
/// to users outside this package.
abstract class Value implements ext.Value {
bool get isTruthy => true;
ListSeparator get separator => ListSeparator.undecided;
bool get hasBrackets => false;
List<Value> get asList => [this];
/// The length of [asList].
///
/// This is used to compute [sassIndexToListIndex] without allocating a new
/// list.
@protected
int get lengthAsList => 1;
/// Whether the value will be represented in CSS as the empty string.
bool get isBlank => false;
/// Whether this is a value that CSS may treat as a number, such as `calc()`
/// or `var()`.
///
/// Functions that shadow plain CSS functions need to gracefully handle when
/// these arguments are passed in.
bool get isSpecialNumber => false;
/// Whether this is a call to `var()`, which may be substituted in CSS for a
/// custom property value.
///
/// Functions that shadow plain CSS functions need to gracefully handle when
/// these arguments are passed in.
bool get isVar => false;
/// Returns Dart's `null` value if this is [sassNull], and returns [this]
/// otherwise.
Value get realNull => this;
const Value();
/// Calls the appropriate visit method on [visitor].
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
T accept<T>(ValueVisitor<T> visitor);
int sassIndexToListIndex(ext.Value sassIndex, [String name]) {
var index = sassIndex.assertNumber(name).assertInt(name);
if (index == 0) throw _exception("List index may not be 0.", name);
if (index.abs() > lengthAsList) {
throw _exception(
"Invalid index $sassIndex for a list with ${lengthAsList} elements.",
name);
}
return index < 0 ? lengthAsList + index : index - 1;
}
SassBoolean assertBoolean([String name]) =>
throw _exception("$this is not a boolean.", name);
SassColor assertColor([String name]) =>
throw _exception("$this is not a color.", name);
SassFunction assertFunction([String name]) =>
throw _exception("$this is not a function reference.", name);
SassMap assertMap([String name]) =>
throw _exception("$this is not a map.", name);
SassNumber assertNumber([String name]) =>
throw _exception("$this is not a number.", name);
SassString assertString([String name]) =>
throw _exception("$this is not a string.", name);
/// Parses [this] as a selector list, in the same manner as the
/// `selector-parse()` function.
///
/// Throws a [SassScriptException] if this isn't a type that can be parsed as a
/// selector, or if parsing fails. If [allowParent] is `true`, this allows
/// [ParentSelector]s. Otherwise, they're considered parse errors.
///
/// If this came from a function argument, [name] is the argument name
/// (without the `$`). It's used for error reporting.
SelectorList assertSelector({String name, bool allowParent = false}) {
var string = _selectorString(name);
try {
return SelectorList.parse(string, allowParent: allowParent);
} on SassFormatException catch (error) {
// TODO(nweiz): colorize this if we're running in an environment where
// that works.
throw _exception(error.toString().replaceFirst("Error: ", ""), name);
}
}
/// Parses [this] as a simple selector, in the same manner as the
/// `selector-parse()` function.
///
/// Throws a [SassScriptException] if this isn't a type that can be parsed as a
/// selector, or if parsing fails. If [allowParent] is `true`, this allows
/// [ParentSelector]s. Otherwise, they're considered parse errors.
///
/// If this came from a function argument, [name] is the argument name
/// (without the `$`). It's used for error reporting.
SimpleSelector assertSimpleSelector({String name, bool allowParent = false}) {
var string = _selectorString(name);
try {
return SimpleSelector.parse(string, allowParent: allowParent);
} on SassFormatException catch (error) {
// TODO(nweiz): colorize this if we're running in an environment where
// that works.
throw _exception(error.toString().replaceFirst("Error: ", ""), name);
}
}
/// Parses [this] as a compound selector, in the same manner as the
/// `selector-parse()` function.
///
/// Throws a [SassScriptException] if this isn't a type that can be parsed as a
/// selector, or if parsing fails. If [allowParent] is `true`, this allows
/// [ParentSelector]s. Otherwise, they're considered parse errors.
///
/// If this came from a function argument, [name] is the argument name
/// (without the `$`). It's used for error reporting.
CompoundSelector assertCompoundSelector(
{String name, bool allowParent = false}) {
var string = _selectorString(name);
try {
return CompoundSelector.parse(string, allowParent: allowParent);
} on SassFormatException catch (error) {
// TODO(nweiz): colorize this if we're running in an environment where
// that works.
throw _exception(error.toString().replaceFirst("Error: ", ""), name);
}
}
/// Converts a `selector-parse()`-style input into a string that can be
/// parsed.
///
/// Throws a [SassScriptException] if [this] isn't a type or a structure that
/// can be parsed as a selector.
String _selectorString([String name]) {
var string = _selectorStringOrNull();
if (string != null) return string;
throw _exception(
"$this is not a valid selector: it must be a string,\n"
"a list of strings, or a list of lists of strings.",
name);
}
/// Converts a `selector-parse()`-style input into a string that can be
/// parsed.
///
/// Returns `null` if [this] isn't a type or a structure that can be parsed as
/// a selector.
String _selectorStringOrNull() {
if (this is SassString) return (this as SassString).text;
if (this is! SassList) return null;
var list = this as SassList;
if (list.asList.isEmpty) return null;
var result = <String>[];
if (list.separator == ListSeparator.comma) {
for (var complex in list.asList) {
if (complex is SassString) {
result.add(complex.text);
} else if (complex is SassList &&
complex.separator == ListSeparator.space) {
var string = complex._selectorString();
if (string == null) return null;
result.add(string);
} else {
return null;
}
}
} else {
for (var compound in list.asList) {
if (compound is SassString) {
result.add(compound.text);
} else {
return null;
}
}
}
return result.join(list.separator == ListSeparator.comma ? ', ' : ' ');
}
/// Returns a new list containing [contents] that defaults to this value's
/// separator and brackets.
SassList changeListContents(Iterable<Value> contents,
{ListSeparator separator, bool brackets}) {
return SassList(contents, separator ?? this.separator,
brackets: brackets ?? this.hasBrackets);
}
/// The SassScript `=` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value singleEquals(Value other) =>
SassString("${toCssString()}=${other.toCssString()}", quotes: false);
/// The SassScript `>` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
SassBoolean greaterThan(Value other) =>
throw SassScriptException('Undefined operation "$this > $other".');
/// The SassScript `>=` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
SassBoolean greaterThanOrEquals(Value other) =>
throw SassScriptException('Undefined operation "$this >= $other".');
/// The SassScript `<` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
SassBoolean lessThan(Value other) =>
throw SassScriptException('Undefined operation "$this < $other".');
/// The SassScript `<=` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
SassBoolean lessThanOrEquals(Value other) =>
throw SassScriptException('Undefined operation "$this <= $other".');
/// The SassScript `*` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value times(Value other) =>
throw SassScriptException('Undefined operation "$this * $other".');
/// The SassScript `%` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value modulo(Value other) =>
throw SassScriptException('Undefined operation "$this % $other".');
/// The SassScript `+` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value plus(Value other) {
if (other is SassString) {
return SassString(toCssString() + other.text, quotes: other.hasQuotes);
} else {
return SassString(toCssString() + other.toCssString(), quotes: false);
}
}
/// The SassScript `-` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value minus(Value other) =>
SassString("${toCssString()}-${other.toCssString()}", quotes: false);
/// The SassScript `/` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value dividedBy(Value other) =>
SassString("${toCssString()}/${other.toCssString()}", quotes: false);
/// The SassScript unary `+` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value unaryPlus() => SassString("+${toCssString()}", quotes: false);
/// The SassScript unary `-` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value unaryMinus() => SassString("-${toCssString()}", quotes: false);
/// The SassScript unary `/` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value unaryDivide() => SassString("/${toCssString()}", quotes: false);
/// The SassScript unary `not` operation.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value unaryNot() => sassFalse;
/// Returns a copy of [this] without [SassNumber.asSlash] set.
///
/// If this isn't a [SassNumber], returns it as-is.
///
/// **Note:** this function should not be called outside the `sass` package.
/// It's not guaranteed to be stable across versions.
Value withoutSlash() => this;
/// Returns a valid CSS representation of [this].
///
/// Throws a [SassScriptException] if [this] can't be represented in plain
/// CSS. Use [toString] instead to get a string representation even if this
/// isn't valid CSS.
///
/// If [quote] is `false`, quoted strings are emitted without quotes.
String toCssString({bool quote = true}) => serializeValue(this, quote: quote);
String toString() => serializeValue(this, inspect: true);
/// Throws a [SassScriptException] with the given [message].
SassScriptException _exception(String message, [String name]) =>
SassScriptException(name == null ? message : "\$$name: $message");
}