@tbela99/css-parser 1.1.1-alpha4 → 1.2.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/CHANGELOG.md +8 -1
- package/README.md +53 -6
- package/dist/index-umd-web.js +4195 -3375
- package/dist/index.cjs +5468 -4648
- package/dist/index.d.ts +42 -31
- package/dist/lib/ast/expand.js +81 -65
- package/dist/lib/ast/features/calc.js +37 -35
- package/dist/lib/ast/features/inlinecssvariables.js +25 -17
- package/dist/lib/ast/features/prefix.js +22 -19
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +17 -2
- package/dist/lib/ast/math/expression.js +184 -159
- package/dist/lib/ast/math/math.js +22 -20
- package/dist/lib/ast/minify.js +249 -200
- package/dist/lib/ast/transform/compute.js +48 -38
- package/dist/lib/ast/transform/matrix.js +6 -5
- package/dist/lib/ast/transform/minify.js +31 -34
- package/dist/lib/ast/transform/utils.js +76 -16
- package/dist/lib/ast/types.js +32 -1
- package/dist/lib/fs/resolve.js +1 -14
- package/dist/lib/parser/declaration/list.js +1 -1
- package/dist/lib/parser/declaration/map.js +1 -1
- package/dist/lib/parser/declaration/set.js +1 -1
- package/dist/lib/parser/parse.js +19 -95
- package/dist/lib/parser/tokenize.js +1 -13
- package/dist/lib/parser/utils/declaration.js +1 -1
- package/dist/lib/parser/utils/type.js +1 -1
- package/dist/lib/renderer/render.js +44 -168
- package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
- package/dist/lib/syntax/color/cmyk.js +104 -0
- package/dist/lib/{renderer → syntax}/color/color-mix.js +20 -21
- package/dist/lib/syntax/color/color.js +581 -0
- package/dist/lib/syntax/color/hex.js +179 -0
- package/dist/lib/syntax/color/hsl.js +201 -0
- package/dist/lib/syntax/color/hwb.js +185 -0
- package/dist/lib/syntax/color/lab.js +262 -0
- package/dist/lib/syntax/color/lch.js +194 -0
- package/dist/lib/syntax/color/oklab.js +237 -0
- package/dist/lib/syntax/color/oklch.js +166 -0
- package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
- package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
- package/dist/lib/{renderer → syntax}/color/relativecolor.js +53 -40
- package/dist/lib/syntax/color/rgb.js +140 -0
- package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
- package/dist/lib/{renderer → syntax}/color/utils/components.js +7 -7
- package/dist/lib/{renderer → syntax}/color/utils/constants.js +7 -34
- package/dist/lib/syntax/color/utils/distance.js +30 -0
- package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
- package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
- package/dist/lib/syntax/syntax.js +77 -67
- package/dist/lib/syntax/utils.js +70 -0
- package/dist/lib/validation/at-rules/container.js +1 -1
- package/dist/lib/validation/at-rules/counter-style.js +1 -1
- package/dist/lib/validation/at-rules/custom-media.js +1 -1
- package/dist/lib/validation/at-rules/document.js +1 -1
- package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
- package/dist/lib/validation/at-rules/import.js +1 -1
- package/dist/lib/validation/at-rules/keyframes.js +14 -13
- package/dist/lib/validation/at-rules/layer.js +1 -1
- package/dist/lib/validation/at-rules/media.js +1 -1
- package/dist/lib/validation/at-rules/namespace.js +1 -1
- package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
- package/dist/lib/validation/at-rules/page.js +1 -1
- package/dist/lib/validation/at-rules/supports.js +1 -1
- package/dist/lib/validation/at-rules/when.js +1 -1
- package/dist/lib/validation/atrule.js +2 -2
- package/dist/lib/validation/config.js +4 -3
- package/dist/lib/validation/config.json.js +1 -1
- package/dist/lib/validation/parser/parse.js +12 -7
- package/dist/lib/validation/selector.js +9 -8
- package/dist/lib/validation/syntax.js +171 -132
- package/dist/lib/validation/syntaxes/complex-selector-list.js +13 -19
- package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
- package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
- package/dist/lib/validation/syntaxes/family-name.js +36 -39
- package/dist/lib/validation/syntaxes/keyframe-selector.js +14 -22
- package/dist/lib/validation/syntaxes/layer-name.js +1 -1
- package/dist/lib/validation/syntaxes/relative-selector-list.js +27 -25
- package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
- package/dist/lib/validation/syntaxes/url.js +35 -33
- package/dist/lib/validation/utils/list.js +10 -9
- package/dist/lib/validation/utils/whitespace.js +1 -1
- package/dist/node/index.js +4 -2
- package/dist/web/index.js +4 -2
- package/package.json +4 -4
- package/.editorconfig +0 -484
- package/dist/lib/ast/transform/convert.js +0 -33
- package/dist/lib/ast/utils/utils.js +0 -104
- package/dist/lib/renderer/color/color.js +0 -654
- package/dist/lib/renderer/color/hex.js +0 -105
- package/dist/lib/renderer/color/hsl.js +0 -125
- package/dist/lib/renderer/color/hwb.js +0 -103
- package/dist/lib/renderer/color/lab.js +0 -148
- package/dist/lib/renderer/color/lch.js +0 -90
- package/dist/lib/renderer/color/oklab.js +0 -131
- package/dist/lib/renderer/color/oklch.js +0 -75
- package/dist/lib/renderer/color/rgb.js +0 -50
- package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
- package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
- package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
- /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ValidationTokenEnum } from './parser/types.js';
|
|
2
2
|
import { renderSyntax } from './parser/parse.js';
|
|
3
|
-
import { EnumToken, SyntaxValidationResult } from '../ast/types.js';
|
|
3
|
+
import { EnumToken, SyntaxValidationResult, ColorType } from '../ast/types.js';
|
|
4
4
|
import '../ast/minify.js';
|
|
5
5
|
import '../ast/walk.js';
|
|
6
6
|
import '../parser/parse.js';
|
|
@@ -8,7 +8,7 @@ import '../parser/tokenize.js';
|
|
|
8
8
|
import '../parser/utils/config.js';
|
|
9
9
|
import { wildCardFuncs, isIdentColor, mathFuncs } from '../syntax/syntax.js';
|
|
10
10
|
import { renderToken } from '../renderer/render.js';
|
|
11
|
-
import { funcLike,
|
|
11
|
+
import { funcLike, colorsFunc } from '../syntax/color/utils/constants.js';
|
|
12
12
|
import { getSyntaxConfig, getParsedSyntax, getSyntax } from './config.js';
|
|
13
13
|
import './syntaxes/complex-selector.js';
|
|
14
14
|
|
|
@@ -17,12 +17,12 @@ const config = getSyntaxConfig();
|
|
|
17
17
|
const allValues = getSyntaxConfig()["declarations" /* ValidationSyntaxGroupEnum.Declarations */].all.syntax.trim().split(/[\s|]+/g);
|
|
18
18
|
function createContext(input) {
|
|
19
19
|
const values = input.slice();
|
|
20
|
-
const result = values.slice();
|
|
21
|
-
if (result.at(-1)?.typ == EnumToken.WhitespaceTokenType) {
|
|
22
|
-
result.pop();
|
|
23
|
-
}
|
|
20
|
+
const result = values.filter(token => token.typ != EnumToken.CommentTokenType).slice();
|
|
24
21
|
return {
|
|
25
22
|
index: -1,
|
|
23
|
+
get length() {
|
|
24
|
+
return this.index < 0 ? result.length : this.index > result.length ? 0 : result.length - this.index;
|
|
25
|
+
},
|
|
26
26
|
peek() {
|
|
27
27
|
let index = this.index + 1;
|
|
28
28
|
if (index >= result.length) {
|
|
@@ -42,15 +42,15 @@ function createContext(input) {
|
|
|
42
42
|
},
|
|
43
43
|
consume(token, howMany) {
|
|
44
44
|
let newIndex = result.indexOf(token, this.index + 1);
|
|
45
|
-
if (newIndex == -1 || newIndex < this.index) {
|
|
46
|
-
|
|
47
|
-
}
|
|
45
|
+
// if (newIndex == -1 || newIndex < this.index) {
|
|
46
|
+
// return false;
|
|
47
|
+
// }
|
|
48
48
|
howMany ??= 0;
|
|
49
49
|
let splice = 1;
|
|
50
|
-
if (result[newIndex - 1]?.typ == EnumToken.WhitespaceTokenType) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
50
|
+
// if (result[newIndex - 1]?.typ == EnumToken.WhitespaceTokenType) {
|
|
51
|
+
// splice++;
|
|
52
|
+
// newIndex--;
|
|
53
|
+
// }
|
|
54
54
|
result.splice(this.index + 1, 0, ...result.splice(newIndex, splice + howMany));
|
|
55
55
|
this.index += howMany + splice;
|
|
56
56
|
return true;
|
|
@@ -72,9 +72,10 @@ function createContext(input) {
|
|
|
72
72
|
this.index = index;
|
|
73
73
|
return result[this.index] ?? null;
|
|
74
74
|
},
|
|
75
|
-
tokens() {
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
// tokens<Token>(): Token[] {
|
|
76
|
+
//
|
|
77
|
+
// return result as Token[];
|
|
78
|
+
// },
|
|
78
79
|
slice() {
|
|
79
80
|
return result.slice(this.index + 1);
|
|
80
81
|
},
|
|
@@ -84,13 +85,14 @@ function createContext(input) {
|
|
|
84
85
|
return context;
|
|
85
86
|
},
|
|
86
87
|
// @ts-ignore
|
|
87
|
-
toJSON() {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
88
|
+
// toJSON(): object {
|
|
89
|
+
//
|
|
90
|
+
// return {
|
|
91
|
+
// index: this.index,
|
|
92
|
+
// slice: this.slice(),
|
|
93
|
+
// tokens: this.tokens()
|
|
94
|
+
// }
|
|
95
|
+
// }
|
|
94
96
|
};
|
|
95
97
|
}
|
|
96
98
|
function evaluateSyntax(node, options) {
|
|
@@ -102,7 +104,6 @@ function evaluateSyntax(node, options) {
|
|
|
102
104
|
break;
|
|
103
105
|
}
|
|
104
106
|
ast = getParsedSyntax("declarations" /* ValidationSyntaxGroupEnum.Declarations */, node.nam);
|
|
105
|
-
// console.error({ast: ast.reduce((acc, curr) => acc + renderSyntax(curr), '')});
|
|
106
107
|
if (ast != null) {
|
|
107
108
|
let token = null;
|
|
108
109
|
const values = node.val.slice();
|
|
@@ -122,13 +123,13 @@ function evaluateSyntax(node, options) {
|
|
|
122
123
|
}
|
|
123
124
|
}
|
|
124
125
|
result = doEvaluateSyntax(ast, createContext(values), { ...options, visited: new WeakMap() });
|
|
125
|
-
// console.error(JSON.stringify({ast, values, result}, null, 1));
|
|
126
126
|
if (result.valid == SyntaxValidationResult.Valid && !result.context.done()) {
|
|
127
127
|
let token = null;
|
|
128
|
-
|
|
129
|
-
if (token.typ == EnumToken.WhitespaceTokenType || token.typ == EnumToken.CommentTokenType) {
|
|
130
|
-
|
|
131
|
-
|
|
128
|
+
if ((token = result.context.next()) != null) {
|
|
129
|
+
// if (token.typ == EnumToken.WhitespaceTokenType || token.typ == EnumToken.CommentTokenType) {
|
|
130
|
+
//
|
|
131
|
+
// continue;
|
|
132
|
+
// }
|
|
132
133
|
return {
|
|
133
134
|
...result,
|
|
134
135
|
valid: SyntaxValidationResult.Drop,
|
|
@@ -144,10 +145,10 @@ function evaluateSyntax(node, options) {
|
|
|
144
145
|
};
|
|
145
146
|
}
|
|
146
147
|
break;
|
|
147
|
-
case EnumToken.RuleNodeType:
|
|
148
|
-
case EnumToken.AtRuleNodeType:
|
|
149
|
-
case EnumToken.KeyframeAtRuleNodeType:
|
|
150
|
-
case EnumToken.KeyFrameRuleNodeType:
|
|
148
|
+
// case EnumToken.RuleNodeType:
|
|
149
|
+
// case EnumToken.AtRuleNodeType:
|
|
150
|
+
// case EnumToken.KeyframeAtRuleNodeType:
|
|
151
|
+
// case EnumToken.KeyFrameRuleNodeType:
|
|
151
152
|
// default:
|
|
152
153
|
//
|
|
153
154
|
// throw new Error(`Not implemented: ${node.typ}`);
|
|
@@ -180,8 +181,20 @@ function doEvaluateSyntax(syntaxes, context, options) {
|
|
|
180
181
|
let syntax;
|
|
181
182
|
let i = 0;
|
|
182
183
|
let result;
|
|
184
|
+
let tmpResult;
|
|
183
185
|
let token = null;
|
|
184
|
-
|
|
186
|
+
if (context.done()) {
|
|
187
|
+
const success = syntaxes.some(s => s.isOptional || s.isRepeatable || s.isRepeatableGroup);
|
|
188
|
+
return {
|
|
189
|
+
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
190
|
+
node: null,
|
|
191
|
+
syntax: null,
|
|
192
|
+
error: '',
|
|
193
|
+
context
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
// console.error(`>> ${syntaxes.reduce((acc, curr) => acc + renderSyntax(curr), '')}\n>> ${context.slice<Token>().reduce((acc, curr) => acc + renderToken(curr), '')}`);
|
|
197
|
+
// console.error(new Error('doEvaluateSyntax'));
|
|
185
198
|
for (; i < syntaxes.length; i++) {
|
|
186
199
|
syntax = syntaxes[i];
|
|
187
200
|
if (context.done()) {
|
|
@@ -191,6 +204,10 @@ function doEvaluateSyntax(syntaxes, context, options) {
|
|
|
191
204
|
break;
|
|
192
205
|
}
|
|
193
206
|
token = context.peek();
|
|
207
|
+
// if var() is the last token, then match the remaining syntax and return
|
|
208
|
+
if (context.length == 1 && token.typ == EnumToken.FunctionTokenType && 'var'.localeCompare(token.val, undefined, { sensitivity: 'base' }) == 0) {
|
|
209
|
+
return doEvaluateSyntax(getParsedSyntax("functions" /* ValidationSyntaxGroupEnum.Functions */, 'var')?.[0]?.chi ?? [], createContext(token.chi), options);
|
|
210
|
+
}
|
|
194
211
|
if (syntax.typ == ValidationTokenEnum.Whitespace) {
|
|
195
212
|
if (context.peek()?.typ == EnumToken.WhitespaceTokenType) {
|
|
196
213
|
context.next();
|
|
@@ -201,7 +218,13 @@ function doEvaluateSyntax(syntaxes, context, options) {
|
|
|
201
218
|
result = matchList(syntax, context, options);
|
|
202
219
|
}
|
|
203
220
|
else if (options.isRepeatable !== false && syntax.isRepeatable) {
|
|
204
|
-
|
|
221
|
+
tmpResult = matchRepeatable(syntax, context, options);
|
|
222
|
+
if (tmpResult.valid == SyntaxValidationResult.Valid) {
|
|
223
|
+
result = tmpResult;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
205
228
|
}
|
|
206
229
|
else if (options.occurence !== false && syntax.occurence != null) {
|
|
207
230
|
result = matchOccurence(syntax, context, options);
|
|
@@ -211,7 +234,6 @@ function doEvaluateSyntax(syntaxes, context, options) {
|
|
|
211
234
|
}
|
|
212
235
|
else {
|
|
213
236
|
if (isVisited(token, syntax, 'doEvaluateSyntax', options)) {
|
|
214
|
-
// console.error(`cyclic dependency: ${renderSyntax(syntax)}`);
|
|
215
237
|
return {
|
|
216
238
|
valid: SyntaxValidationResult.Drop,
|
|
217
239
|
node: token,
|
|
@@ -256,9 +278,9 @@ function matchAtLeastOnce(syntax, context, options) {
|
|
|
256
278
|
}
|
|
257
279
|
return {
|
|
258
280
|
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
259
|
-
node: context.
|
|
281
|
+
node: context.peek(),
|
|
260
282
|
syntax,
|
|
261
|
-
error: success ? '' : `could not match
|
|
283
|
+
error: success ? '' : `could not match syntax: ${renderSyntax(syntax)}`,
|
|
262
284
|
context
|
|
263
285
|
};
|
|
264
286
|
}
|
|
@@ -281,7 +303,7 @@ function matchRepeatable(syntax, context, options) {
|
|
|
281
303
|
};
|
|
282
304
|
}
|
|
283
305
|
function matchList(syntax, context, options) {
|
|
284
|
-
let success
|
|
306
|
+
let success;
|
|
285
307
|
let result;
|
|
286
308
|
let count = 0;
|
|
287
309
|
let con = context.clone();
|
|
@@ -290,15 +312,16 @@ function matchList(syntax, context, options) {
|
|
|
290
312
|
while (!con.done() && con.peek()?.typ != EnumToken.CommaTokenType) {
|
|
291
313
|
tokens.push(con.next());
|
|
292
314
|
}
|
|
293
|
-
if (tokens.length == 0) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
315
|
+
// if (tokens.length == 0) {
|
|
316
|
+
//
|
|
317
|
+
// return {
|
|
318
|
+
// valid: SyntaxValidationResult.Drop,
|
|
319
|
+
// node: context.peek(),
|
|
320
|
+
// syntax,
|
|
321
|
+
// error: `could not match syntax: ${renderSyntax(syntax)}`,
|
|
322
|
+
// context
|
|
323
|
+
// }
|
|
324
|
+
// }
|
|
302
325
|
result = doEvaluateSyntax([syntax], createContext(tokens), {
|
|
303
326
|
...options,
|
|
304
327
|
isList: false,
|
|
@@ -319,7 +342,7 @@ function matchList(syntax, context, options) {
|
|
|
319
342
|
}
|
|
320
343
|
}
|
|
321
344
|
success = count > 0;
|
|
322
|
-
if (
|
|
345
|
+
if (success && syntax.occurence != null) {
|
|
323
346
|
success = count >= syntax.occurence.min;
|
|
324
347
|
if (success && syntax.occurence.max != null) {
|
|
325
348
|
success = count <= syntax.occurence.max;
|
|
@@ -327,7 +350,7 @@ function matchList(syntax, context, options) {
|
|
|
327
350
|
}
|
|
328
351
|
return {
|
|
329
352
|
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
330
|
-
node: context.
|
|
353
|
+
node: context.peek(),
|
|
331
354
|
syntax,
|
|
332
355
|
error: '',
|
|
333
356
|
context
|
|
@@ -352,14 +375,13 @@ function matchOccurence(syntax, context, options) {
|
|
|
352
375
|
}
|
|
353
376
|
return {
|
|
354
377
|
valid: sucesss ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
355
|
-
node: context.
|
|
378
|
+
node: context.peek(),
|
|
356
379
|
syntax,
|
|
357
380
|
error: sucesss ? '' : `expected ${renderSyntax(syntax)} ${syntax.occurence.min} to ${syntax.occurence.max} occurences, got ${counter}`,
|
|
358
381
|
context
|
|
359
382
|
};
|
|
360
383
|
}
|
|
361
384
|
function match(syntax, context, options) {
|
|
362
|
-
// console.error(`>> match(): ${renderSyntax(syntax)}\n>> ${JSON.stringify({syntax}, null, 1)}>> context: ${context.slice<Token>().reduce((acc, curr) => acc + renderToken(curr), '')}`);
|
|
363
385
|
let success = false;
|
|
364
386
|
let result;
|
|
365
387
|
let token = context.peek();
|
|
@@ -377,26 +399,29 @@ function match(syntax, context, options) {
|
|
|
377
399
|
}
|
|
378
400
|
return {
|
|
379
401
|
valid: SyntaxValidationResult.Drop,
|
|
380
|
-
node: context.
|
|
402
|
+
node: context.peek(),
|
|
381
403
|
syntax,
|
|
382
404
|
error: `expected '${ValidationTokenEnum[syntax.typ].toLowerCase()}', got '${context.done() ? null : renderToken(context.peek())}'`,
|
|
383
405
|
context
|
|
384
406
|
};
|
|
385
407
|
}
|
|
386
408
|
}
|
|
387
|
-
if (token.typ == EnumToken.WhitespaceTokenType) {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
409
|
+
// if (token.typ == EnumToken.WhitespaceTokenType) {
|
|
410
|
+
//
|
|
411
|
+
// context.next();
|
|
412
|
+
//
|
|
413
|
+
// // @ts-ignore
|
|
414
|
+
// if (syntax?.typ == ValidationTokenEnum.Whitespace) {
|
|
415
|
+
//
|
|
416
|
+
// return {
|
|
417
|
+
// valid: SyntaxValidationResult.Valid,
|
|
418
|
+
// node: null,
|
|
419
|
+
// syntax,
|
|
420
|
+
// error: '',
|
|
421
|
+
// context
|
|
422
|
+
// }
|
|
423
|
+
// }
|
|
424
|
+
// }
|
|
400
425
|
if (syntax.typ != ValidationTokenEnum.PropertyType && (token?.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val))) {
|
|
401
426
|
const result = doEvaluateSyntax(getParsedSyntax("functions" /* ValidationSyntaxGroupEnum.Functions */, token.val)?.[0]?.chi ?? [], createContext(token.chi), {
|
|
402
427
|
...options,
|
|
@@ -432,7 +457,7 @@ function match(syntax, context, options) {
|
|
|
432
457
|
return matchPropertyType(syntax, context, options);
|
|
433
458
|
case ValidationTokenEnum.ValidationFunctionDefinition:
|
|
434
459
|
token = context.peek();
|
|
435
|
-
if (token.typ == EnumToken.ParensTokenType || !funcLike.concat(EnumToken.ColorTokenType) || (!('chi' in token))) {
|
|
460
|
+
if (token.typ == EnumToken.ParensTokenType || !funcLike.concat(EnumToken.ColorTokenType).includes(token.typ) || (!('chi' in token))) {
|
|
436
461
|
return {
|
|
437
462
|
valid: SyntaxValidationResult.Drop,
|
|
438
463
|
node: context.next(),
|
|
@@ -441,17 +466,13 @@ function match(syntax, context, options) {
|
|
|
441
466
|
context
|
|
442
467
|
};
|
|
443
468
|
}
|
|
444
|
-
// {
|
|
445
|
-
// console.error(JSON.stringify({funcDef: (getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, (syntax as ValidationFunctionDefinitionToken).val + '()')?.[0] as ValidationFunctionToken).chi}, null, 1))
|
|
446
|
-
//
|
|
447
|
-
// const child = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, (syntax as ValidationFunctionDefinitionToken).val + '()')?.[0] as ValidationFunctionToken;
|
|
448
469
|
result = match(getParsedSyntax("syntaxes" /* ValidationSyntaxGroupEnum.Syntaxes */, syntax.val + '()')?.[0], context, options);
|
|
470
|
+
// console.error(`>>[]
|
|
449
471
|
if (result.valid == SyntaxValidationResult.Valid) {
|
|
450
472
|
context.next();
|
|
451
473
|
result.context = context;
|
|
452
474
|
return result;
|
|
453
475
|
}
|
|
454
|
-
// }
|
|
455
476
|
break;
|
|
456
477
|
case ValidationTokenEnum.DeclarationType:
|
|
457
478
|
return doEvaluateSyntax(getParsedSyntax("declarations" /* ValidationSyntaxGroupEnum.Declarations */, syntax.val), context, {
|
|
@@ -461,37 +482,49 @@ function match(syntax, context, options) {
|
|
|
461
482
|
occurence: null,
|
|
462
483
|
atLeastOnce: null
|
|
463
484
|
});
|
|
464
|
-
case ValidationTokenEnum.Parens:
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
485
|
+
// case ValidationTokenEnum.Parens:
|
|
486
|
+
//
|
|
487
|
+
// token = context.peek() as Token;
|
|
488
|
+
// if (token.typ != EnumToken.ParensTokenType) {
|
|
489
|
+
//
|
|
490
|
+
// break;
|
|
491
|
+
// }
|
|
492
|
+
//
|
|
493
|
+
// success = doEvaluateSyntax((syntax as ValidationParensToken).chi as ValidationToken[], createContext((token as ParensToken).chi), {
|
|
494
|
+
// ...options,
|
|
495
|
+
// isRepeatable: null,
|
|
496
|
+
// isList: null,
|
|
497
|
+
// occurence: null,
|
|
498
|
+
// atLeastOnce: null
|
|
499
|
+
// } as ValidationOptions).valid == SyntaxValidationResult.Valid;
|
|
500
|
+
// break;
|
|
477
501
|
case ValidationTokenEnum.Comma:
|
|
478
502
|
success = context.peek()?.typ == EnumToken.CommaTokenType;
|
|
479
503
|
if (success) {
|
|
480
504
|
context.next();
|
|
481
505
|
}
|
|
482
506
|
break;
|
|
483
|
-
case ValidationTokenEnum.Number:
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
507
|
+
// case ValidationTokenEnum.Number:
|
|
508
|
+
//
|
|
509
|
+
// success = context.peek<Token>()?.typ == EnumToken.NumberTokenType;
|
|
510
|
+
//
|
|
511
|
+
// if (success) {
|
|
512
|
+
//
|
|
513
|
+
// context.next();
|
|
514
|
+
// }
|
|
515
|
+
//
|
|
516
|
+
// break;
|
|
517
|
+
//
|
|
518
|
+
// case ValidationTokenEnum.Whitespace:
|
|
519
|
+
//
|
|
520
|
+
// success = context.peek<Token>()?.typ == EnumToken.WhitespaceTokenType;
|
|
521
|
+
//
|
|
522
|
+
// if (success) {
|
|
523
|
+
//
|
|
524
|
+
// context.next();
|
|
525
|
+
// }
|
|
526
|
+
//
|
|
527
|
+
// break;
|
|
495
528
|
case ValidationTokenEnum.Separator:
|
|
496
529
|
{
|
|
497
530
|
const token = context.peek();
|
|
@@ -506,21 +539,22 @@ function match(syntax, context, options) {
|
|
|
506
539
|
if (!wildCardFuncs.includes(syntax.val) && syntax.val != token.val) {
|
|
507
540
|
break;
|
|
508
541
|
}
|
|
509
|
-
if (
|
|
510
|
-
success = doEvaluateSyntax(syntax.chi, createContext(token.chi),
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
542
|
+
if (syntax.typ == ValidationTokenEnum.Function) {
|
|
543
|
+
success = funcLike.includes(token.typ) && syntax.val.localeCompare(token.val, undefined, { sensitivity: 'base' }) == 0 && doEvaluateSyntax(syntax.chi, createContext(token.chi), options).valid == SyntaxValidationResult.Valid;
|
|
544
|
+
// console.error(`>> match: ${JSON.stringify({
|
|
545
|
+
// syntax,
|
|
546
|
+
// token,
|
|
547
|
+
// // result,
|
|
548
|
+
// tr2: syntax,
|
|
549
|
+
// success
|
|
550
|
+
// }, null, 1)}`);
|
|
517
551
|
if (success) {
|
|
518
552
|
context.next();
|
|
519
553
|
}
|
|
520
554
|
break;
|
|
521
555
|
}
|
|
522
|
-
if (
|
|
523
|
-
success =
|
|
556
|
+
if (token.typ != EnumToken.ParensTokenType && funcLike.includes(token.typ)) {
|
|
557
|
+
success = doEvaluateSyntax(syntax.chi, createContext(token.chi), {
|
|
524
558
|
...options,
|
|
525
559
|
isRepeatable: null,
|
|
526
560
|
isList: null,
|
|
@@ -549,7 +583,9 @@ function match(syntax, context, options) {
|
|
|
549
583
|
function matchPropertyType(syntax, context, options) {
|
|
550
584
|
if (![
|
|
551
585
|
'bg-position',
|
|
552
|
-
'
|
|
586
|
+
'integer',
|
|
587
|
+
'length-percentage', 'flex', 'calc-sum', 'color',
|
|
588
|
+
'color-base', 'system-color', 'deprecated-system-color',
|
|
553
589
|
'pseudo-class-selector', 'pseudo-element-selector'
|
|
554
590
|
].includes(syntax.val)) {
|
|
555
591
|
if (syntax.val in config["syntaxes" /* ValidationSyntaxGroupEnum.Syntaxes */]) {
|
|
@@ -611,7 +647,7 @@ function matchPropertyType(syntax, context, options) {
|
|
|
611
647
|
valid: SyntaxValidationResult.Drop,
|
|
612
648
|
node: token,
|
|
613
649
|
syntax,
|
|
614
|
-
error: `expected <length>`,
|
|
650
|
+
error: `expected <length-percentage>`,
|
|
615
651
|
context
|
|
616
652
|
};
|
|
617
653
|
}
|
|
@@ -693,10 +729,10 @@ function matchPropertyType(syntax, context, options) {
|
|
|
693
729
|
success = token.typ == EnumToken.DashedIdenTokenType;
|
|
694
730
|
break;
|
|
695
731
|
case 'system-color':
|
|
696
|
-
success = (token.typ == EnumToken.ColorTokenType && token.kin ==
|
|
732
|
+
success = (token.typ == EnumToken.ColorTokenType && token.kin == ColorType.SYS) || (token.typ == EnumToken.IdenTokenType && token.val.localeCompare('currentcolor', 'en', { sensitivity: 'base' }) == 0) || (token.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val));
|
|
697
733
|
break;
|
|
698
734
|
case 'deprecated-system-color':
|
|
699
|
-
success = (token.typ == EnumToken.ColorTokenType && token.kin ==
|
|
735
|
+
success = (token.typ == EnumToken.ColorTokenType && token.kin == ColorType.DPSYS) || (token.typ == EnumToken.IdenTokenType && token.val.localeCompare('currentcolor', 'en', { sensitivity: 'base' }) == 0) || (token.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val));
|
|
700
736
|
break;
|
|
701
737
|
case 'color':
|
|
702
738
|
case 'color-base':
|
|
@@ -712,10 +748,10 @@ function matchPropertyType(syntax, context, options) {
|
|
|
712
748
|
}
|
|
713
749
|
break;
|
|
714
750
|
case 'hex-color':
|
|
715
|
-
success = (token.typ == EnumToken.ColorTokenType && token.kin ==
|
|
751
|
+
success = (token.typ == EnumToken.ColorTokenType && token.kin == ColorType.HEX) || (token.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val));
|
|
716
752
|
break;
|
|
717
753
|
case 'integer':
|
|
718
|
-
success = (token.typ == EnumToken.NumberTokenType && Number.isInteger(+(token.val))) || (token.typ == EnumToken.FunctionTokenType && token.val ==
|
|
754
|
+
success = (token.typ == EnumToken.NumberTokenType && Number.isInteger(+(token.val))) || (token.typ == EnumToken.FunctionTokenType && mathFuncs.includes(token.val.toLowerCase()) || (token.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val)));
|
|
719
755
|
if ('range' in syntax) {
|
|
720
756
|
success = success && +token.val >= +syntax.range[0] && +token.val <= +syntax.range[1];
|
|
721
757
|
}
|
|
@@ -759,7 +795,7 @@ function matchPropertyType(syntax, context, options) {
|
|
|
759
795
|
success = token.typ == EnumToken.HashTokenType;
|
|
760
796
|
break;
|
|
761
797
|
case 'string':
|
|
762
|
-
success = token.typ == EnumToken.StringTokenType || token.typ == EnumToken.IdenTokenType || (token.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val));
|
|
798
|
+
success = token.typ == EnumToken.StringTokenType || token.typ == EnumToken.UrlTokenTokenType || token.typ == EnumToken.HashTokenType || token.typ == EnumToken.IdenTokenType || (token.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val));
|
|
763
799
|
break;
|
|
764
800
|
case 'time':
|
|
765
801
|
success = token.typ == EnumToken.TimeTokenType || (token.typ == EnumToken.FunctionTokenType && token.val == 'calc');
|
|
@@ -794,7 +830,6 @@ function matchPropertyType(syntax, context, options) {
|
|
|
794
830
|
['length-percentage', 'length', 'number', 'number-token', 'angle', 'percentage', 'dimension'].includes(syntax.val)) {
|
|
795
831
|
if (!success) {
|
|
796
832
|
success = mathFuncs.includes(token.val.toLowerCase()) &&
|
|
797
|
-
// (token as FunctionToken).val + '()' in config[ValidationSyntaxGroupEnum.Syntaxes] &&
|
|
798
833
|
doEvaluateSyntax(getParsedSyntax("syntaxes" /* ValidationSyntaxGroupEnum.Syntaxes */, token.val + '()')?.[0]?.chi ?? [], createContext(token.chi), {
|
|
799
834
|
...options,
|
|
800
835
|
isRepeatable: null,
|
|
@@ -824,10 +859,12 @@ function someOf(syntaxes, context, options) {
|
|
|
824
859
|
let success = false;
|
|
825
860
|
const matched = [];
|
|
826
861
|
for (i = 0; i < syntaxes.length; i++) {
|
|
827
|
-
if (context.peek()?.typ == EnumToken.WhitespaceTokenType) {
|
|
828
|
-
|
|
829
|
-
|
|
862
|
+
// if (context.peek<Token>()?.typ == EnumToken.WhitespaceTokenType) {
|
|
863
|
+
//
|
|
864
|
+
// context.next();
|
|
865
|
+
// }
|
|
830
866
|
result = doEvaluateSyntax(syntaxes[i], context.clone(), options);
|
|
867
|
+
// console.error(`>> someOf: ${JSON.stringify({result}, null, 1)}`);
|
|
831
868
|
if (result.valid == SyntaxValidationResult.Valid) {
|
|
832
869
|
success = true;
|
|
833
870
|
if (result.context.done()) {
|
|
@@ -840,11 +877,13 @@ function someOf(syntaxes, context, options) {
|
|
|
840
877
|
// pick the best match
|
|
841
878
|
matched.sort((a, b) => a.context.done() ? -1 : b.context.done() ? 1 : b.context.index - a.context.index);
|
|
842
879
|
}
|
|
880
|
+
// console.error(JSON.stringify({matched}, null, 1));
|
|
881
|
+
// console.error(new Error('someOf'));
|
|
843
882
|
return matched[0] ?? {
|
|
844
883
|
valid: SyntaxValidationResult.Drop,
|
|
845
|
-
node: context.
|
|
884
|
+
node: context.peek(),
|
|
846
885
|
syntax: null,
|
|
847
|
-
error: success ? '' : `could not match
|
|
886
|
+
error: success ? '' : `could not match syntax: ${syntaxes.reduce((acc, curr) => acc + (acc.length > 0 ? ' | ' : '') + curr.reduce((acc, curr) => acc + renderSyntax(curr), ''), '')}`,
|
|
848
887
|
context
|
|
849
888
|
};
|
|
850
889
|
}
|
|
@@ -866,9 +905,9 @@ function anyOf(syntaxes, context, options) {
|
|
|
866
905
|
}
|
|
867
906
|
return {
|
|
868
907
|
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
869
|
-
node: context.
|
|
908
|
+
node: context.peek(),
|
|
870
909
|
syntax: null,
|
|
871
|
-
error: success ? '' : `could not match
|
|
910
|
+
error: success ? '' : `could not match syntax: ${syntaxes.reduce((acc, curr) => acc + '[' + curr.reduce((acc, curr) => acc + renderSyntax(curr), '') + ']', '')}`,
|
|
872
911
|
context
|
|
873
912
|
};
|
|
874
913
|
}
|
|
@@ -895,9 +934,10 @@ function allOf(syntax, context, options) {
|
|
|
895
934
|
for (i = 0; i < slice.length; i++) {
|
|
896
935
|
if (slice[i].typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(slice[i].val.toLowerCase())) {
|
|
897
936
|
vars.push(slice[i]);
|
|
898
|
-
if (slice[i + 1]?.typ == EnumToken.WhitespaceTokenType) {
|
|
899
|
-
|
|
900
|
-
|
|
937
|
+
// if (slice[i + 1]?.typ == EnumToken.WhitespaceTokenType) {
|
|
938
|
+
//
|
|
939
|
+
// vars.push(slice[++i]);
|
|
940
|
+
// }
|
|
901
941
|
continue;
|
|
902
942
|
}
|
|
903
943
|
if (slice[i].typ == EnumToken.CommaTokenType || (slice[i].typ == EnumToken.LiteralTokenType && slice[i].val == '/')) {
|
|
@@ -914,18 +954,17 @@ function allOf(syntax, context, options) {
|
|
|
914
954
|
let j;
|
|
915
955
|
for (i = 0; i < syntax.length; i++) {
|
|
916
956
|
if (syntax[i].length == 1 && syntax[i][0].isOptional) {
|
|
917
|
-
syntax[i][0].isOptional = false;
|
|
918
957
|
j = 0;
|
|
919
958
|
cp = con.clone();
|
|
920
959
|
slice = cp.slice();
|
|
921
960
|
if (cp.done()) {
|
|
922
|
-
syntax[i][0].isOptional = true;
|
|
923
961
|
syntax.splice(i, 1);
|
|
924
962
|
i = -1;
|
|
925
963
|
continue;
|
|
926
964
|
}
|
|
927
965
|
while (!cp.done()) {
|
|
928
|
-
result = doEvaluateSyntax(syntax[i], cp.clone(), options);
|
|
966
|
+
result = doEvaluateSyntax(syntax[i], cp.clone(), { ...options, isOptional: false });
|
|
967
|
+
// console.error(`>> allOf: syntaxes[${i}]: ${syntax[i].length} ${syntax[i].reduce((acc, curr) => acc + renderSyntax(curr), '')}\n>> ${cp.slice<Token>().reduce((acc, curr) => acc + renderToken(curr), '')}\n>> ${JSON.stringify({result}, null, 1)}`);
|
|
929
968
|
if (result.valid == SyntaxValidationResult.Valid) {
|
|
930
969
|
let end = slice.indexOf(cp.current());
|
|
931
970
|
if (end == -1) {
|
|
@@ -940,9 +979,8 @@ function allOf(syntax, context, options) {
|
|
|
940
979
|
cp.next();
|
|
941
980
|
j++;
|
|
942
981
|
}
|
|
943
|
-
syntax[i][0].isOptional = true;
|
|
944
982
|
// @ts-ignore
|
|
945
|
-
if (result?.valid == SyntaxValidationResult.Valid) {
|
|
983
|
+
if (result?.valid == SyntaxValidationResult.Valid || (syntax[i].length == 1 && syntax[i][0].isOptional)) {
|
|
946
984
|
syntax.splice(i, 1);
|
|
947
985
|
i = -1;
|
|
948
986
|
}
|
|
@@ -955,12 +993,13 @@ function allOf(syntax, context, options) {
|
|
|
955
993
|
i = -1;
|
|
956
994
|
}
|
|
957
995
|
}
|
|
996
|
+
// console.error()
|
|
958
997
|
const success = syntax.length == 0;
|
|
959
998
|
return {
|
|
960
999
|
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
961
|
-
node: context.
|
|
1000
|
+
node: context.peek(),
|
|
962
1001
|
syntax: syntax?.[0]?.[0] ?? null,
|
|
963
|
-
error: `could not match
|
|
1002
|
+
error: `could not match syntax: ${syntax.reduce((acc, curr) => acc + '[' + curr.reduce((acc, curr) => acc + renderSyntax(curr), '') + ']', '')}`,
|
|
964
1003
|
context: success ? con : context
|
|
965
1004
|
};
|
|
966
1005
|
}
|
|
@@ -4,7 +4,7 @@ import '../../ast/walk.js';
|
|
|
4
4
|
import '../../parser/parse.js';
|
|
5
5
|
import '../../parser/tokenize.js';
|
|
6
6
|
import '../../parser/utils/config.js';
|
|
7
|
-
import '../../
|
|
7
|
+
import '../../syntax/color/utils/constants.js';
|
|
8
8
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
9
9
|
import { validateSelector } from './selector.js';
|
|
10
10
|
import { consumeWhitespace } from '../utils/whitespace.js';
|
|
@@ -13,16 +13,17 @@ import { splitTokenList } from '../utils/list.js';
|
|
|
13
13
|
function validateComplexSelectorList(tokens, root, options) {
|
|
14
14
|
tokens = tokens.slice();
|
|
15
15
|
consumeWhitespace(tokens);
|
|
16
|
-
if (tokens.length == 0) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
16
|
+
// if (tokens.length == 0) {
|
|
17
|
+
//
|
|
18
|
+
// return {
|
|
19
|
+
// valid: SyntaxValidationResult.Drop,
|
|
20
|
+
// context: [],
|
|
21
|
+
// // @ts-ignore
|
|
22
|
+
// node: root,
|
|
23
|
+
// syntax: null,
|
|
24
|
+
// error: 'expecting complex selector list'
|
|
25
|
+
// }
|
|
26
|
+
// }
|
|
26
27
|
let result = null;
|
|
27
28
|
for (const t of splitTokenList(tokens)) {
|
|
28
29
|
result = validateSelector(t, root, options);
|
|
@@ -31,14 +32,7 @@ function validateComplexSelectorList(tokens, root, options) {
|
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
// @ts-ignore
|
|
34
|
-
return result
|
|
35
|
-
valid: SyntaxValidationResult.Drop,
|
|
36
|
-
context: [],
|
|
37
|
-
// @ts-ignore
|
|
38
|
-
node: root,
|
|
39
|
-
syntax: null,
|
|
40
|
-
error: 'expecting complex selector list'
|
|
41
|
-
};
|
|
35
|
+
return result;
|
|
42
36
|
}
|
|
43
37
|
|
|
44
38
|
export { validateComplexSelectorList };
|