@tbela99/css-parser 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -82
- package/dist/index-umd-web.js +6956 -51524
- package/dist/index.cjs +6955 -51528
- package/dist/index.d.ts +180 -65
- package/dist/lib/ast/expand.js +34 -9
- package/dist/lib/ast/features/calc.js +76 -12
- package/dist/lib/ast/features/inlinecssvariables.js +6 -1
- package/dist/lib/ast/features/prefix.js +17 -9
- package/dist/lib/ast/features/shorthand.js +1 -0
- package/dist/lib/ast/math/expression.js +299 -11
- package/dist/lib/ast/math/math.js +7 -1
- package/dist/lib/ast/minify.js +1 -1
- package/dist/lib/ast/types.js +58 -49
- package/dist/lib/ast/walk.js +80 -18
- package/dist/lib/parser/declaration/list.js +1 -0
- package/dist/lib/parser/declaration/map.js +1 -0
- package/dist/lib/parser/declaration/set.js +1 -0
- package/dist/lib/parser/parse.js +285 -72
- package/dist/lib/parser/tokenize.js +16 -3
- package/dist/lib/parser/utils/declaration.js +2 -2
- package/dist/lib/parser/utils/type.js +6 -6
- package/dist/lib/renderer/color/a98rgb.js +1 -0
- package/dist/lib/renderer/color/color.js +1 -0
- package/dist/lib/renderer/color/colormix.js +1 -0
- package/dist/lib/renderer/color/hex.js +1 -0
- package/dist/lib/renderer/color/hsl.js +1 -0
- package/dist/lib/renderer/color/hwb.js +1 -0
- package/dist/lib/renderer/color/lab.js +1 -0
- package/dist/lib/renderer/color/lch.js +1 -0
- package/dist/lib/renderer/color/oklab.js +1 -0
- package/dist/lib/renderer/color/oklch.js +1 -0
- package/dist/lib/renderer/color/p3.js +1 -0
- package/dist/lib/renderer/color/prophotoRgb.js +56 -0
- package/dist/lib/renderer/color/rec2020.js +1 -0
- package/dist/lib/renderer/color/relativecolor.js +52 -28
- package/dist/lib/renderer/color/rgb.js +1 -0
- package/dist/lib/renderer/color/srgb.js +1 -0
- package/dist/lib/renderer/color/utils/components.js +1 -0
- package/dist/lib/renderer/color/utils/constants.js +1 -0
- package/dist/lib/renderer/color/xyz.js +1 -0
- package/dist/lib/renderer/color/xyzd50.js +1 -0
- package/dist/lib/renderer/render.js +28 -6
- package/dist/lib/syntax/syntax.js +27 -4
- package/dist/lib/validation/at-rules/counter-style.js +78 -0
- package/dist/lib/validation/at-rules/document.js +114 -0
- package/dist/lib/validation/at-rules/font-feature-values.js +49 -0
- package/dist/lib/validation/at-rules/import.js +196 -0
- package/dist/lib/validation/at-rules/keyframes.js +70 -0
- package/dist/lib/validation/at-rules/layer.js +27 -0
- package/dist/lib/validation/at-rules/media.js +166 -0
- package/dist/lib/validation/at-rules/namespace.js +85 -0
- package/dist/lib/validation/at-rules/page-margin-box.js +56 -0
- package/dist/lib/validation/at-rules/page.js +88 -0
- package/dist/lib/validation/at-rules/supports.js +262 -0
- package/dist/lib/validation/atrule.js +172 -0
- package/dist/lib/validation/config.js +30 -2
- package/dist/lib/validation/config.json.js +1560 -50902
- package/dist/lib/validation/declaration.js +72 -0
- package/dist/lib/validation/parser/parse.js +1059 -7
- package/dist/lib/validation/parser/types.js +27 -12
- package/dist/lib/validation/selector.js +23 -444
- package/dist/lib/validation/syntax.js +1429 -0
- package/dist/lib/validation/syntaxes/complex-selector-list.js +41 -0
- package/dist/lib/validation/syntaxes/complex-selector.js +283 -0
- package/dist/lib/validation/syntaxes/family-name.js +91 -0
- package/dist/lib/validation/syntaxes/keyframe-block-list.js +27 -0
- package/dist/lib/validation/syntaxes/keyframe-selector.js +137 -0
- package/dist/lib/validation/syntaxes/layer-name.js +67 -0
- package/dist/lib/validation/syntaxes/relative-selector-list.js +27 -0
- package/dist/lib/validation/syntaxes/relative-selector.js +36 -0
- package/dist/lib/validation/syntaxes/selector-list.js +5 -0
- package/dist/lib/validation/syntaxes/selector.js +5 -0
- package/dist/lib/validation/syntaxes/url.js +75 -0
- package/dist/lib/validation/utils/list.js +24 -0
- package/dist/lib/validation/utils/whitespace.js +22 -0
- package/dist/node/index.js +4 -4
- package/dist/web/index.js +4 -0
- package/dist/web/load.js +1 -0
- package/package.json +15 -13
- package/dist/lib/ast/utils/minifyfeature.js +0 -9
- package/dist/lib/iterable/weakset.js +0 -58
- package/dist/lib/parser/utils/syntax.js +0 -450
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
|
-
import {
|
|
3
|
-
import { walkValues } from '../walk.js';
|
|
2
|
+
import { getSyntaxConfig } from '../../validation/config.js';
|
|
4
3
|
import { ValidationTokenEnum } from '../../validation/parser/types.js';
|
|
5
4
|
import '../../validation/parser/parse.js';
|
|
5
|
+
import '../minify.js';
|
|
6
|
+
import { walkValues } from '../walk.js';
|
|
7
|
+
import '../../parser/parse.js';
|
|
8
|
+
import '../../renderer/color/utils/constants.js';
|
|
9
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
10
|
+
import '../../parser/utils/config.js';
|
|
11
|
+
import '../../validation/syntaxes/complex-selector.js';
|
|
6
12
|
|
|
7
|
-
const config =
|
|
13
|
+
const config = getSyntaxConfig();
|
|
8
14
|
class ComputePrefixFeature {
|
|
9
15
|
static get ordering() {
|
|
10
16
|
return 2;
|
|
@@ -30,7 +36,7 @@ class ComputePrefixFeature {
|
|
|
30
36
|
const node = ast.chi[k];
|
|
31
37
|
if (node.typ == EnumToken.DeclarationNodeType) {
|
|
32
38
|
if (node.nam.charAt(0) == '-') {
|
|
33
|
-
const match = node.nam.match(/^-(
|
|
39
|
+
const match = node.nam.match(/^-([^-]+)-(.+)$/);
|
|
34
40
|
if (match != null) {
|
|
35
41
|
const nam = match[2];
|
|
36
42
|
if (nam.toLowerCase() in config.declarations) {
|
|
@@ -67,7 +73,6 @@ function matchToken(token, matches) {
|
|
|
67
73
|
case ValidationTokenEnum.Comma:
|
|
68
74
|
break;
|
|
69
75
|
case ValidationTokenEnum.Keyword:
|
|
70
|
-
console.error(matches[i], token);
|
|
71
76
|
if (token.typ == EnumToken.IdenTokenType && token.val == matches[i].val) {
|
|
72
77
|
return token;
|
|
73
78
|
}
|
|
@@ -90,6 +95,13 @@ function matchToken(token, matches) {
|
|
|
90
95
|
}
|
|
91
96
|
break;
|
|
92
97
|
case ValidationTokenEnum.PipeToken:
|
|
98
|
+
for (let j = 0; j < matches[i].chi.length; j++) {
|
|
99
|
+
result = matchToken(token, matches[i].chi[j]);
|
|
100
|
+
if (result != null) {
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
93
105
|
case ValidationTokenEnum.ColumnToken:
|
|
94
106
|
case ValidationTokenEnum.AmpersandToken:
|
|
95
107
|
result = matchToken(token, matches[i].l);
|
|
@@ -106,10 +118,6 @@ function matchToken(token, matches) {
|
|
|
106
118
|
return result;
|
|
107
119
|
}
|
|
108
120
|
break;
|
|
109
|
-
// default:
|
|
110
|
-
//
|
|
111
|
-
// console.error(token, matches[i]);
|
|
112
|
-
// throw new Error('bar bar');
|
|
113
121
|
}
|
|
114
122
|
}
|
|
115
123
|
return null;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PropertyList } from '../../parser/declaration/list.js';
|
|
2
2
|
import { EnumToken } from '../types.js';
|
|
3
3
|
import '../minify.js';
|
|
4
|
+
import '../walk.js';
|
|
4
5
|
import '../../parser/parse.js';
|
|
5
6
|
import '../../renderer/color/utils/constants.js';
|
|
6
7
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
|
-
import { compute } from './math.js';
|
|
2
|
+
import { compute, rem } from './math.js';
|
|
3
3
|
import { reduceNumber } from '../../renderer/render.js';
|
|
4
|
+
import { mathFuncs } from '../../syntax/syntax.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* evaluate an array of tokens
|
|
@@ -8,15 +9,45 @@ import { reduceNumber } from '../../renderer/render.js';
|
|
|
8
9
|
*/
|
|
9
10
|
function evaluate(tokens) {
|
|
10
11
|
let nodes;
|
|
12
|
+
if (tokens.length == 1 && tokens[0].typ == EnumToken.FunctionTokenType && tokens[0].val != 'calc' && mathFuncs.includes(tokens[0].val)) {
|
|
13
|
+
const chi = tokens[0].chi.reduce((acc, t) => {
|
|
14
|
+
if (acc.length == 0 || t.typ == EnumToken.CommaTokenType) {
|
|
15
|
+
acc.push([]);
|
|
16
|
+
}
|
|
17
|
+
if ([EnumToken.WhitespaceTokenType, EnumToken.CommaTokenType, EnumToken.CommaTokenType].includes(t.typ)) {
|
|
18
|
+
return acc;
|
|
19
|
+
}
|
|
20
|
+
acc.at(-1).push(t);
|
|
21
|
+
return acc;
|
|
22
|
+
}, []);
|
|
23
|
+
for (let i = 0; i < chi.length; i++) {
|
|
24
|
+
chi[i] = evaluate(chi[i]);
|
|
25
|
+
}
|
|
26
|
+
tokens[0].chi = chi.reduce((acc, t) => {
|
|
27
|
+
if (acc.length > 0) {
|
|
28
|
+
acc.push({ typ: EnumToken.CommaTokenType });
|
|
29
|
+
}
|
|
30
|
+
acc.push(...t);
|
|
31
|
+
return acc;
|
|
32
|
+
});
|
|
33
|
+
return evaluateFunc(tokens[0]);
|
|
34
|
+
}
|
|
11
35
|
try {
|
|
12
36
|
nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
|
|
13
37
|
}
|
|
14
38
|
catch (e) {
|
|
15
|
-
// console.error({tokens});
|
|
16
|
-
// console.error(e);
|
|
17
39
|
return tokens;
|
|
18
40
|
}
|
|
19
41
|
if (nodes.length <= 1) {
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
if (nodes.length == 1 && nodes[0].typ == EnumToken.IdenTokenType && typeof Math[nodes[0].val.toUpperCase()] == 'number') {
|
|
44
|
+
return [{
|
|
45
|
+
...nodes[0],
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
val: '' + Math[nodes[0].val.toUpperCase()],
|
|
48
|
+
typ: EnumToken.NumberTokenType
|
|
49
|
+
}];
|
|
50
|
+
}
|
|
20
51
|
return nodes;
|
|
21
52
|
}
|
|
22
53
|
const map = new Map;
|
|
@@ -71,9 +102,33 @@ function doEvaluate(l, r, op) {
|
|
|
71
102
|
l,
|
|
72
103
|
r
|
|
73
104
|
};
|
|
74
|
-
if (!isScalarToken(l) || !isScalarToken(r)) {
|
|
105
|
+
if (!isScalarToken(l) || !isScalarToken(r) || (l.typ == r.typ && 'unit' in l && 'unit' in r && l.unit != r.unit)) {
|
|
75
106
|
return defaultReturn;
|
|
76
107
|
}
|
|
108
|
+
if (l.typ == EnumToken.FunctionTokenType) {
|
|
109
|
+
const val = evaluateFunc(l);
|
|
110
|
+
if (val.length == 1) {
|
|
111
|
+
l = val[0];
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
return defaultReturn;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (r.typ == EnumToken.FunctionTokenType) {
|
|
118
|
+
const val = evaluateFunc(r);
|
|
119
|
+
if (val.length == 1) {
|
|
120
|
+
r = val[0];
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
return defaultReturn;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (l.typ == EnumToken.FunctionTokenType) {
|
|
127
|
+
const val = evaluateFunc(l);
|
|
128
|
+
if (val.length == 1) {
|
|
129
|
+
l = val[0];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
77
132
|
if ((op == EnumToken.Add || op == EnumToken.Sub)) {
|
|
78
133
|
// @ts-ignore
|
|
79
134
|
if (l.typ != r.typ) {
|
|
@@ -85,11 +140,13 @@ function doEvaluate(l, r, op) {
|
|
|
85
140
|
![EnumToken.NumberTokenType, EnumToken.PercentageTokenType].includes(r.typ)) {
|
|
86
141
|
return defaultReturn;
|
|
87
142
|
}
|
|
88
|
-
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
let v1 = typeof l.val == 'string' ? +l.val : l.val;
|
|
143
|
+
let typ = l.typ == EnumToken.NumberTokenType ? r.typ : (r.typ == EnumToken.NumberTokenType ? l.typ : (l.typ == EnumToken.PercentageTokenType ? r.typ : l.typ));
|
|
91
144
|
// @ts-ignore
|
|
92
|
-
let
|
|
145
|
+
let v1 = getValue(l);
|
|
146
|
+
let v2 = getValue(r);
|
|
147
|
+
if (v1 == null || v2 == null) {
|
|
148
|
+
return defaultReturn;
|
|
149
|
+
}
|
|
93
150
|
if (op == EnumToken.Mul) {
|
|
94
151
|
if (l.typ != EnumToken.NumberTokenType && r.typ != EnumToken.NumberTokenType) {
|
|
95
152
|
if (typeof v1 == 'number' && l.typ == EnumToken.PercentageTokenType) {
|
|
@@ -110,11 +167,238 @@ function doEvaluate(l, r, op) {
|
|
|
110
167
|
}
|
|
111
168
|
// @ts-ignore
|
|
112
169
|
const val = compute(v1, v2, op);
|
|
113
|
-
|
|
170
|
+
// typ = typeof val == 'number' ? EnumToken.NumberTokenType : EnumToken.FractionTokenType;
|
|
171
|
+
const token = {
|
|
114
172
|
...(l.typ == EnumToken.NumberTokenType ? r : l),
|
|
115
173
|
typ,
|
|
116
174
|
val: typeof val == 'number' ? reduceNumber(val) : val
|
|
117
175
|
};
|
|
176
|
+
if (token.typ == EnumToken.IdenTokenType) {
|
|
177
|
+
// @ts-ignore
|
|
178
|
+
token.typ = EnumToken.NumberTokenType;
|
|
179
|
+
}
|
|
180
|
+
return token;
|
|
181
|
+
}
|
|
182
|
+
function getValue(t) {
|
|
183
|
+
let v1;
|
|
184
|
+
if (t.typ == EnumToken.FunctionTokenType) {
|
|
185
|
+
v1 = evaluateFunc(t);
|
|
186
|
+
if (v1.length != 1 || v1[0].typ == EnumToken.BinaryExpressionTokenType) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
t = v1[0];
|
|
190
|
+
}
|
|
191
|
+
if (t.typ == EnumToken.IdenTokenType) {
|
|
192
|
+
// @ts-ignore
|
|
193
|
+
return Math[t.val.toUpperCase()];
|
|
194
|
+
}
|
|
195
|
+
if (t.val.typ == EnumToken.FractionTokenType) {
|
|
196
|
+
// @ts-ignore
|
|
197
|
+
return t.val.l.val / t.val.r.val;
|
|
198
|
+
}
|
|
199
|
+
// @ts-ignore
|
|
200
|
+
return t.typ == EnumToken.FractionTokenType ? t.l.val / t.r.val : +t.val;
|
|
201
|
+
}
|
|
202
|
+
function evaluateFunc(token) {
|
|
203
|
+
const values = token.chi.slice();
|
|
204
|
+
switch (token.val) {
|
|
205
|
+
case 'abs':
|
|
206
|
+
case 'sin':
|
|
207
|
+
case 'cos':
|
|
208
|
+
case 'tan':
|
|
209
|
+
case 'asin':
|
|
210
|
+
case 'acos':
|
|
211
|
+
case 'atan':
|
|
212
|
+
case 'sign':
|
|
213
|
+
case 'sqrt':
|
|
214
|
+
case 'exp': {
|
|
215
|
+
const value = evaluate(values);
|
|
216
|
+
if (value.length != 1 || (value[0].typ != EnumToken.NumberTokenType && value[0].typ != EnumToken.FractionTokenType) || (value[0].typ == EnumToken.FractionTokenType && (+value[0].r.val == 0 || !Number.isFinite(+value[0].l.val) || !Number.isFinite(+value[0].r.val)))) {
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
// @ts-ignore
|
|
220
|
+
let val = value[0].typ == EnumToken.NumberTokenType ? +value[0].val : value[0].l.val / value[0].r.val;
|
|
221
|
+
return [{
|
|
222
|
+
typ: EnumToken.NumberTokenType,
|
|
223
|
+
val: '' + Math[token.val](val)
|
|
224
|
+
}];
|
|
225
|
+
}
|
|
226
|
+
case 'hypot': {
|
|
227
|
+
const chi = values.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ));
|
|
228
|
+
let all = [];
|
|
229
|
+
let ref = chi[0];
|
|
230
|
+
let value = 0;
|
|
231
|
+
if (![EnumToken.NumberTokenType, EnumToken.PercentageTokenType].includes(ref.typ) && !('unit' in ref)) {
|
|
232
|
+
return [token];
|
|
233
|
+
}
|
|
234
|
+
for (let i = 0; i < chi.length; i++) {
|
|
235
|
+
// @ts-ignore
|
|
236
|
+
if (chi[i].typ != ref.typ || ('unit' in chi[i] && 'unit' in ref && chi[i].unit != ref.unit)) {
|
|
237
|
+
return [token];
|
|
238
|
+
}
|
|
239
|
+
// @ts-ignore
|
|
240
|
+
const val = getValue(chi[i]);
|
|
241
|
+
if (val == null) {
|
|
242
|
+
return [token];
|
|
243
|
+
}
|
|
244
|
+
all.push(val);
|
|
245
|
+
value += val * val;
|
|
246
|
+
}
|
|
247
|
+
return [
|
|
248
|
+
{
|
|
249
|
+
...ref,
|
|
250
|
+
val: Math.sqrt(value).toFixed(rem(...all))
|
|
251
|
+
}
|
|
252
|
+
];
|
|
253
|
+
}
|
|
254
|
+
case 'atan2':
|
|
255
|
+
case 'pow':
|
|
256
|
+
case 'rem':
|
|
257
|
+
case 'mod': {
|
|
258
|
+
const chi = values.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
259
|
+
if (chi.length != 3 || chi[1].typ != EnumToken.CommaTokenType) {
|
|
260
|
+
return [token];
|
|
261
|
+
}
|
|
262
|
+
if (token.val == 'pow' && (chi[0].typ != EnumToken.NumberTokenType || chi[2].typ != EnumToken.NumberTokenType)) {
|
|
263
|
+
return [token];
|
|
264
|
+
}
|
|
265
|
+
if (['rem', 'mod'].includes(token.val) &&
|
|
266
|
+
(chi[0].typ != chi[2].typ) || ('unit' in chi[0] && 'unit' in chi[2] &&
|
|
267
|
+
chi[0].unit != chi[2].unit)) {
|
|
268
|
+
return [token];
|
|
269
|
+
}
|
|
270
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/mod
|
|
271
|
+
const v1 = evaluate([chi[0]]);
|
|
272
|
+
const v2 = evaluate([chi[2]]);
|
|
273
|
+
const types = [EnumToken.PercentageTokenType, EnumToken.DimensionTokenType, EnumToken.AngleTokenType, EnumToken.NumberTokenType, EnumToken.LengthTokenType, EnumToken.TimeTokenType, EnumToken.FrequencyTokenType, EnumToken.ResolutionTokenType];
|
|
274
|
+
if (v1.length != 1 || v2.length != 1 || !types.includes(v1[0].typ) || !types.includes(v2[0].typ) || v1[0].unit != v2[0].unit) {
|
|
275
|
+
return [token];
|
|
276
|
+
}
|
|
277
|
+
// @ts-ignore
|
|
278
|
+
const val1 = getValue(v1[0]);
|
|
279
|
+
// @ts-ignore
|
|
280
|
+
const val2 = getValue(v2[0]);
|
|
281
|
+
if (val1 == null || val2 == null || (v1[0].typ != v2[0].typ && val1 != 0 && val2 != 0)) {
|
|
282
|
+
return [token];
|
|
283
|
+
}
|
|
284
|
+
if (token.val == 'rem') {
|
|
285
|
+
if (val2 == 0) {
|
|
286
|
+
return [token];
|
|
287
|
+
}
|
|
288
|
+
return [
|
|
289
|
+
{
|
|
290
|
+
...v1[0],
|
|
291
|
+
val: (val1 % val2).toFixed(rem(val1, val2))
|
|
292
|
+
}
|
|
293
|
+
];
|
|
294
|
+
}
|
|
295
|
+
if (token.val == 'pow') {
|
|
296
|
+
return [
|
|
297
|
+
{
|
|
298
|
+
...v1[0],
|
|
299
|
+
val: String(Math.pow(val1, val2))
|
|
300
|
+
}
|
|
301
|
+
];
|
|
302
|
+
}
|
|
303
|
+
if (token.val == 'atan2') {
|
|
304
|
+
return [
|
|
305
|
+
{
|
|
306
|
+
...{}, ...v1[0],
|
|
307
|
+
val: String(Math.atan2(val1, val2))
|
|
308
|
+
}
|
|
309
|
+
];
|
|
310
|
+
}
|
|
311
|
+
return [
|
|
312
|
+
{
|
|
313
|
+
...v1[0],
|
|
314
|
+
val: String(val2 == 0 ? val1 : val1 - (Math.floor(val1 / val2) * val2))
|
|
315
|
+
}
|
|
316
|
+
];
|
|
317
|
+
}
|
|
318
|
+
case 'clamp':
|
|
319
|
+
token.chi = values;
|
|
320
|
+
return [token];
|
|
321
|
+
case 'log':
|
|
322
|
+
case 'round':
|
|
323
|
+
case 'min':
|
|
324
|
+
case 'max':
|
|
325
|
+
{
|
|
326
|
+
const strategy = token.val == 'round' && values[0]?.typ == EnumToken.IdenTokenType ? values.shift().val : null;
|
|
327
|
+
const valuesMap = new Map;
|
|
328
|
+
for (const curr of values) {
|
|
329
|
+
if (curr.typ == EnumToken.CommaTokenType || curr.typ == EnumToken.WhitespaceTokenType || curr.typ == EnumToken.CommentTokenType) {
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
const result = evaluate([curr]);
|
|
333
|
+
if (result.length != 1 || result[0].typ == EnumToken.FunctionTokenType) {
|
|
334
|
+
return [token];
|
|
335
|
+
}
|
|
336
|
+
const key = result[0].typ + ('unit' in result[0] ? result[0].unit : '');
|
|
337
|
+
if (!valuesMap.has(key)) {
|
|
338
|
+
valuesMap.set(key, []);
|
|
339
|
+
}
|
|
340
|
+
valuesMap.get(key).push(result[0]);
|
|
341
|
+
}
|
|
342
|
+
if (valuesMap.size == 1) {
|
|
343
|
+
const values = valuesMap.values().next().value;
|
|
344
|
+
if (token.val == 'log') {
|
|
345
|
+
if (values[0].typ != EnumToken.NumberTokenType || values.length > 2) {
|
|
346
|
+
return [token];
|
|
347
|
+
}
|
|
348
|
+
const val1 = getValue(values[0]);
|
|
349
|
+
const val2 = values.length == 2 ? getValue(values[1]) : null;
|
|
350
|
+
if (values.length == 1) {
|
|
351
|
+
return [
|
|
352
|
+
{
|
|
353
|
+
...values[0],
|
|
354
|
+
val: String(Math.log(val1))
|
|
355
|
+
}
|
|
356
|
+
];
|
|
357
|
+
}
|
|
358
|
+
return [
|
|
359
|
+
{
|
|
360
|
+
...values[0],
|
|
361
|
+
val: String(Math.log(val1) / Math.log(val2))
|
|
362
|
+
}
|
|
363
|
+
];
|
|
364
|
+
}
|
|
365
|
+
if (token.val == 'min' || token.val == 'max') {
|
|
366
|
+
let val = getValue(values[0]);
|
|
367
|
+
let val2 = val;
|
|
368
|
+
let ret = values[0];
|
|
369
|
+
for (const curr of values.slice(1)) {
|
|
370
|
+
val2 = getValue(curr);
|
|
371
|
+
if (val2 < val && token.val == 'min') {
|
|
372
|
+
val = val2;
|
|
373
|
+
ret = curr;
|
|
374
|
+
}
|
|
375
|
+
else if (val2 > val && token.val == 'max') {
|
|
376
|
+
val = val2;
|
|
377
|
+
ret = curr;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return [ret];
|
|
381
|
+
}
|
|
382
|
+
if (token.val == 'round') {
|
|
383
|
+
let val = getValue(values[0]);
|
|
384
|
+
let val2 = getValue(values[1]);
|
|
385
|
+
if (Number.isNaN(val) || Number.isNaN(val2)) {
|
|
386
|
+
return [token];
|
|
387
|
+
}
|
|
388
|
+
if (strategy == null || strategy == 'down') {
|
|
389
|
+
val = val - (val % val2);
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
val = strategy == 'to-zero' ? Math.trunc(val / val2) * val2 : (strategy == 'nearest' ? Math.round(val / val2) * val2 : Math.ceil(val / val2) * val2);
|
|
393
|
+
}
|
|
394
|
+
// @ts-ignore
|
|
395
|
+
return [{ ...values[0], val: String(val) }];
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return [token];
|
|
400
|
+
}
|
|
401
|
+
return [token];
|
|
118
402
|
}
|
|
119
403
|
/**
|
|
120
404
|
* convert BinaryExpression into an array
|
|
@@ -155,7 +439,11 @@ function evaluateExpression(token) {
|
|
|
155
439
|
return doEvaluate(token.l, token.r, token.op);
|
|
156
440
|
}
|
|
157
441
|
function isScalarToken(token) {
|
|
158
|
-
return 'unit' in token ||
|
|
442
|
+
return 'unit' in token ||
|
|
443
|
+
(token.typ == EnumToken.FunctionTokenType && mathFuncs.includes(token.val)) ||
|
|
444
|
+
// @ts-ignore
|
|
445
|
+
(token.typ == EnumToken.IdenTokenType && typeof Math[token.val.toUpperCase()] == 'number') ||
|
|
446
|
+
[EnumToken.NumberTokenType, EnumToken.FractionTokenType, EnumToken.PercentageTokenType].includes(token.typ);
|
|
159
447
|
}
|
|
160
448
|
/**
|
|
161
449
|
*
|
|
@@ -225,4 +513,4 @@ function factor(tokens, ops) {
|
|
|
225
513
|
return tokens;
|
|
226
514
|
}
|
|
227
515
|
|
|
228
|
-
export { evaluate };
|
|
516
|
+
export { evaluate, evaluateFunc };
|
|
@@ -87,9 +87,15 @@ function compute(a, b, op) {
|
|
|
87
87
|
r: { typ: EnumToken.NumberTokenType, val: reduceNumber(a2[1]) }
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
|
+
function rem(...a) {
|
|
91
|
+
if (a.some((i) => !Number.isInteger(i))) {
|
|
92
|
+
return a.reduce((a, b) => Math.max(a, String(b).split('.')[1]?.length ?? 0), 0);
|
|
93
|
+
}
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
90
96
|
function simplify(a, b) {
|
|
91
97
|
const g = gcd(a, b);
|
|
92
98
|
return g > 1 ? [a / g, b / g] : [a, b];
|
|
93
99
|
}
|
|
94
100
|
|
|
95
|
-
export { compute, gcd, simplify };
|
|
101
|
+
export { compute, gcd, rem, simplify };
|
package/dist/lib/ast/minify.js
CHANGED
|
@@ -995,4 +995,4 @@ function reduceRuleSelector(node) {
|
|
|
995
995
|
}
|
|
996
996
|
}
|
|
997
997
|
|
|
998
|
-
export { combinators, definedPropertySettings, hasDeclaration, minify, reduceSelector, splitRule };
|
|
998
|
+
export { combinators, definedPropertySettings, hasDeclaration, matchSelectors, minify, reduceSelector, splitRule };
|
package/dist/lib/ast/types.js
CHANGED
|
@@ -44,61 +44,70 @@ var EnumToken;
|
|
|
44
44
|
EnumToken[EnumToken["WhitespaceTokenType"] = 36] = "WhitespaceTokenType";
|
|
45
45
|
EnumToken[EnumToken["IncludeMatchTokenType"] = 37] = "IncludeMatchTokenType";
|
|
46
46
|
EnumToken[EnumToken["DashMatchTokenType"] = 38] = "DashMatchTokenType";
|
|
47
|
-
EnumToken[EnumToken["
|
|
48
|
-
EnumToken[EnumToken["
|
|
49
|
-
EnumToken[EnumToken["
|
|
50
|
-
EnumToken[EnumToken["
|
|
51
|
-
EnumToken[EnumToken["
|
|
52
|
-
EnumToken[EnumToken["
|
|
53
|
-
EnumToken[EnumToken["
|
|
54
|
-
EnumToken[EnumToken["
|
|
55
|
-
EnumToken[EnumToken["
|
|
56
|
-
EnumToken[EnumToken["
|
|
57
|
-
EnumToken[EnumToken["
|
|
58
|
-
EnumToken[EnumToken["
|
|
59
|
-
EnumToken[EnumToken["
|
|
60
|
-
EnumToken[EnumToken["
|
|
61
|
-
EnumToken[EnumToken["
|
|
62
|
-
EnumToken[EnumToken["
|
|
63
|
-
EnumToken[EnumToken["
|
|
64
|
-
EnumToken[EnumToken["
|
|
65
|
-
EnumToken[EnumToken["
|
|
47
|
+
EnumToken[EnumToken["EqualMatchTokenType"] = 39] = "EqualMatchTokenType";
|
|
48
|
+
EnumToken[EnumToken["LtTokenType"] = 40] = "LtTokenType";
|
|
49
|
+
EnumToken[EnumToken["LteTokenType"] = 41] = "LteTokenType";
|
|
50
|
+
EnumToken[EnumToken["GtTokenType"] = 42] = "GtTokenType";
|
|
51
|
+
EnumToken[EnumToken["GteTokenType"] = 43] = "GteTokenType";
|
|
52
|
+
EnumToken[EnumToken["PseudoClassTokenType"] = 44] = "PseudoClassTokenType";
|
|
53
|
+
EnumToken[EnumToken["PseudoClassFuncTokenType"] = 45] = "PseudoClassFuncTokenType";
|
|
54
|
+
EnumToken[EnumToken["DelimTokenType"] = 46] = "DelimTokenType";
|
|
55
|
+
EnumToken[EnumToken["UrlTokenTokenType"] = 47] = "UrlTokenTokenType";
|
|
56
|
+
EnumToken[EnumToken["EOFTokenType"] = 48] = "EOFTokenType";
|
|
57
|
+
EnumToken[EnumToken["ImportantTokenType"] = 49] = "ImportantTokenType";
|
|
58
|
+
EnumToken[EnumToken["ColorTokenType"] = 50] = "ColorTokenType";
|
|
59
|
+
EnumToken[EnumToken["AttrTokenType"] = 51] = "AttrTokenType";
|
|
60
|
+
EnumToken[EnumToken["BadCommentTokenType"] = 52] = "BadCommentTokenType";
|
|
61
|
+
EnumToken[EnumToken["BadCdoTokenType"] = 53] = "BadCdoTokenType";
|
|
62
|
+
EnumToken[EnumToken["BadUrlTokenType"] = 54] = "BadUrlTokenType";
|
|
63
|
+
EnumToken[EnumToken["BadStringTokenType"] = 55] = "BadStringTokenType";
|
|
64
|
+
EnumToken[EnumToken["BinaryExpressionTokenType"] = 56] = "BinaryExpressionTokenType";
|
|
65
|
+
EnumToken[EnumToken["UnaryExpressionTokenType"] = 57] = "UnaryExpressionTokenType";
|
|
66
|
+
EnumToken[EnumToken["FlexTokenType"] = 58] = "FlexTokenType";
|
|
66
67
|
/* catch all */
|
|
67
|
-
EnumToken[EnumToken["ListToken"] =
|
|
68
|
+
EnumToken[EnumToken["ListToken"] = 59] = "ListToken";
|
|
68
69
|
/* arithmetic tokens */
|
|
69
|
-
EnumToken[EnumToken["Add"] =
|
|
70
|
-
EnumToken[EnumToken["Mul"] =
|
|
71
|
-
EnumToken[EnumToken["Div"] =
|
|
72
|
-
EnumToken[EnumToken["Sub"] =
|
|
70
|
+
EnumToken[EnumToken["Add"] = 60] = "Add";
|
|
71
|
+
EnumToken[EnumToken["Mul"] = 61] = "Mul";
|
|
72
|
+
EnumToken[EnumToken["Div"] = 62] = "Div";
|
|
73
|
+
EnumToken[EnumToken["Sub"] = 63] = "Sub";
|
|
73
74
|
/* new tokens */
|
|
74
|
-
EnumToken[EnumToken["ColumnCombinatorTokenType"] =
|
|
75
|
-
EnumToken[EnumToken["ContainMatchTokenType"] =
|
|
76
|
-
EnumToken[EnumToken["StartMatchTokenType"] =
|
|
77
|
-
EnumToken[EnumToken["EndMatchTokenType"] =
|
|
78
|
-
EnumToken[EnumToken["MatchExpressionTokenType"] =
|
|
79
|
-
EnumToken[EnumToken["NameSpaceAttributeTokenType"] =
|
|
80
|
-
EnumToken[EnumToken["FractionTokenType"] =
|
|
81
|
-
EnumToken[EnumToken["IdenListTokenType"] =
|
|
82
|
-
EnumToken[EnumToken["GridTemplateFuncTokenType"] =
|
|
83
|
-
EnumToken[EnumToken["KeyFrameRuleNodeType"] =
|
|
84
|
-
EnumToken[EnumToken["ClassSelectorTokenType"] =
|
|
85
|
-
EnumToken[EnumToken["UniversalSelectorTokenType"] =
|
|
86
|
-
EnumToken[EnumToken["ChildCombinatorTokenType"] =
|
|
87
|
-
EnumToken[EnumToken["DescendantCombinatorTokenType"] =
|
|
88
|
-
EnumToken[EnumToken["NextSiblingCombinatorTokenType"] =
|
|
89
|
-
EnumToken[EnumToken["SubsequentSiblingCombinatorTokenType"] =
|
|
90
|
-
EnumToken[EnumToken["NestingSelectorTokenType"] =
|
|
91
|
-
EnumToken[EnumToken["InvalidRuleTokenType"] =
|
|
92
|
-
EnumToken[EnumToken["InvalidClassSelectorTokenType"] =
|
|
93
|
-
EnumToken[EnumToken["InvalidAttrTokenType"] =
|
|
75
|
+
EnumToken[EnumToken["ColumnCombinatorTokenType"] = 64] = "ColumnCombinatorTokenType";
|
|
76
|
+
EnumToken[EnumToken["ContainMatchTokenType"] = 65] = "ContainMatchTokenType";
|
|
77
|
+
EnumToken[EnumToken["StartMatchTokenType"] = 66] = "StartMatchTokenType";
|
|
78
|
+
EnumToken[EnumToken["EndMatchTokenType"] = 67] = "EndMatchTokenType";
|
|
79
|
+
EnumToken[EnumToken["MatchExpressionTokenType"] = 68] = "MatchExpressionTokenType";
|
|
80
|
+
EnumToken[EnumToken["NameSpaceAttributeTokenType"] = 69] = "NameSpaceAttributeTokenType";
|
|
81
|
+
EnumToken[EnumToken["FractionTokenType"] = 70] = "FractionTokenType";
|
|
82
|
+
EnumToken[EnumToken["IdenListTokenType"] = 71] = "IdenListTokenType";
|
|
83
|
+
EnumToken[EnumToken["GridTemplateFuncTokenType"] = 72] = "GridTemplateFuncTokenType";
|
|
84
|
+
EnumToken[EnumToken["KeyFrameRuleNodeType"] = 73] = "KeyFrameRuleNodeType";
|
|
85
|
+
EnumToken[EnumToken["ClassSelectorTokenType"] = 74] = "ClassSelectorTokenType";
|
|
86
|
+
EnumToken[EnumToken["UniversalSelectorTokenType"] = 75] = "UniversalSelectorTokenType";
|
|
87
|
+
EnumToken[EnumToken["ChildCombinatorTokenType"] = 76] = "ChildCombinatorTokenType";
|
|
88
|
+
EnumToken[EnumToken["DescendantCombinatorTokenType"] = 77] = "DescendantCombinatorTokenType";
|
|
89
|
+
EnumToken[EnumToken["NextSiblingCombinatorTokenType"] = 78] = "NextSiblingCombinatorTokenType";
|
|
90
|
+
EnumToken[EnumToken["SubsequentSiblingCombinatorTokenType"] = 79] = "SubsequentSiblingCombinatorTokenType";
|
|
91
|
+
EnumToken[EnumToken["NestingSelectorTokenType"] = 80] = "NestingSelectorTokenType";
|
|
92
|
+
EnumToken[EnumToken["InvalidRuleTokenType"] = 81] = "InvalidRuleTokenType";
|
|
93
|
+
EnumToken[EnumToken["InvalidClassSelectorTokenType"] = 82] = "InvalidClassSelectorTokenType";
|
|
94
|
+
EnumToken[EnumToken["InvalidAttrTokenType"] = 83] = "InvalidAttrTokenType";
|
|
95
|
+
EnumToken[EnumToken["InvalidAtRuleTokenType"] = 84] = "InvalidAtRuleTokenType";
|
|
96
|
+
EnumToken[EnumToken["MediaQueryConditionTokenType"] = 85] = "MediaQueryConditionTokenType";
|
|
97
|
+
EnumToken[EnumToken["MediaFeatureTokenType"] = 86] = "MediaFeatureTokenType";
|
|
98
|
+
EnumToken[EnumToken["MediaFeatureOnlyTokenType"] = 87] = "MediaFeatureOnlyTokenType";
|
|
99
|
+
EnumToken[EnumToken["MediaFeatureNotTokenType"] = 88] = "MediaFeatureNotTokenType";
|
|
100
|
+
EnumToken[EnumToken["MediaFeatureAndTokenType"] = 89] = "MediaFeatureAndTokenType";
|
|
101
|
+
EnumToken[EnumToken["MediaFeatureOrTokenType"] = 90] = "MediaFeatureOrTokenType";
|
|
102
|
+
EnumToken[EnumToken["PseudoPageTokenType"] = 91] = "PseudoPageTokenType";
|
|
94
103
|
/* aliases */
|
|
95
104
|
EnumToken[EnumToken["Time"] = 25] = "Time";
|
|
96
105
|
EnumToken[EnumToken["Iden"] = 7] = "Iden";
|
|
97
|
-
EnumToken[EnumToken["EOF"] =
|
|
106
|
+
EnumToken[EnumToken["EOF"] = 48] = "EOF";
|
|
98
107
|
EnumToken[EnumToken["Hash"] = 28] = "Hash";
|
|
99
|
-
EnumToken[EnumToken["Flex"] =
|
|
108
|
+
EnumToken[EnumToken["Flex"] = 58] = "Flex";
|
|
100
109
|
EnumToken[EnumToken["Angle"] = 24] = "Angle";
|
|
101
|
-
EnumToken[EnumToken["Color"] =
|
|
110
|
+
EnumToken[EnumToken["Color"] = 50] = "Color";
|
|
102
111
|
EnumToken[EnumToken["Comma"] = 9] = "Comma";
|
|
103
112
|
EnumToken[EnumToken["String"] = 20] = "String";
|
|
104
113
|
EnumToken[EnumToken["Length"] = 23] = "Length";
|
|
@@ -111,9 +120,9 @@ var EnumToken;
|
|
|
111
120
|
EnumToken[EnumToken["Frequency"] = 26] = "Frequency";
|
|
112
121
|
EnumToken[EnumToken["Resolution"] = 27] = "Resolution";
|
|
113
122
|
EnumToken[EnumToken["Whitespace"] = 36] = "Whitespace";
|
|
114
|
-
EnumToken[EnumToken["IdenList"] =
|
|
123
|
+
EnumToken[EnumToken["IdenList"] = 71] = "IdenList";
|
|
115
124
|
EnumToken[EnumToken["DashedIden"] = 8] = "DashedIden";
|
|
116
|
-
EnumToken[EnumToken["GridTemplateFunc"] =
|
|
125
|
+
EnumToken[EnumToken["GridTemplateFunc"] = 72] = "GridTemplateFunc";
|
|
117
126
|
EnumToken[EnumToken["ImageFunc"] = 19] = "ImageFunc";
|
|
118
127
|
EnumToken[EnumToken["CommentNodeType"] = 0] = "CommentNodeType";
|
|
119
128
|
EnumToken[EnumToken["CDOCOMMNodeType"] = 1] = "CDOCOMMNodeType";
|