@tbela99/css-parser 1.1.1 → 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 +6 -0
- package/README.md +53 -6
- package/dist/index-umd-web.js +4195 -3363
- package/dist/index.cjs +4199 -3367
- 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 -199
- 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 +6 -33
- 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 +170 -120
- 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) {
|
|
@@ -123,10 +125,11 @@ function evaluateSyntax(node, options) {
|
|
|
123
125
|
result = doEvaluateSyntax(ast, createContext(values), { ...options, visited: new WeakMap() });
|
|
124
126
|
if (result.valid == SyntaxValidationResult.Valid && !result.context.done()) {
|
|
125
127
|
let token = null;
|
|
126
|
-
|
|
127
|
-
if (token.typ == EnumToken.WhitespaceTokenType || token.typ == EnumToken.CommentTokenType) {
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
if ((token = result.context.next()) != null) {
|
|
129
|
+
// if (token.typ == EnumToken.WhitespaceTokenType || token.typ == EnumToken.CommentTokenType) {
|
|
130
|
+
//
|
|
131
|
+
// continue;
|
|
132
|
+
// }
|
|
130
133
|
return {
|
|
131
134
|
...result,
|
|
132
135
|
valid: SyntaxValidationResult.Drop,
|
|
@@ -142,10 +145,10 @@ function evaluateSyntax(node, options) {
|
|
|
142
145
|
};
|
|
143
146
|
}
|
|
144
147
|
break;
|
|
145
|
-
case EnumToken.RuleNodeType:
|
|
146
|
-
case EnumToken.AtRuleNodeType:
|
|
147
|
-
case EnumToken.KeyframeAtRuleNodeType:
|
|
148
|
-
case EnumToken.KeyFrameRuleNodeType:
|
|
148
|
+
// case EnumToken.RuleNodeType:
|
|
149
|
+
// case EnumToken.AtRuleNodeType:
|
|
150
|
+
// case EnumToken.KeyframeAtRuleNodeType:
|
|
151
|
+
// case EnumToken.KeyFrameRuleNodeType:
|
|
149
152
|
// default:
|
|
150
153
|
//
|
|
151
154
|
// throw new Error(`Not implemented: ${node.typ}`);
|
|
@@ -178,7 +181,20 @@ function doEvaluateSyntax(syntaxes, context, options) {
|
|
|
178
181
|
let syntax;
|
|
179
182
|
let i = 0;
|
|
180
183
|
let result;
|
|
184
|
+
let tmpResult;
|
|
181
185
|
let token = null;
|
|
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'));
|
|
182
198
|
for (; i < syntaxes.length; i++) {
|
|
183
199
|
syntax = syntaxes[i];
|
|
184
200
|
if (context.done()) {
|
|
@@ -188,6 +204,10 @@ function doEvaluateSyntax(syntaxes, context, options) {
|
|
|
188
204
|
break;
|
|
189
205
|
}
|
|
190
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
|
+
}
|
|
191
211
|
if (syntax.typ == ValidationTokenEnum.Whitespace) {
|
|
192
212
|
if (context.peek()?.typ == EnumToken.WhitespaceTokenType) {
|
|
193
213
|
context.next();
|
|
@@ -198,7 +218,13 @@ function doEvaluateSyntax(syntaxes, context, options) {
|
|
|
198
218
|
result = matchList(syntax, context, options);
|
|
199
219
|
}
|
|
200
220
|
else if (options.isRepeatable !== false && syntax.isRepeatable) {
|
|
201
|
-
|
|
221
|
+
tmpResult = matchRepeatable(syntax, context, options);
|
|
222
|
+
if (tmpResult.valid == SyntaxValidationResult.Valid) {
|
|
223
|
+
result = tmpResult;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
202
228
|
}
|
|
203
229
|
else if (options.occurence !== false && syntax.occurence != null) {
|
|
204
230
|
result = matchOccurence(syntax, context, options);
|
|
@@ -252,9 +278,9 @@ function matchAtLeastOnce(syntax, context, options) {
|
|
|
252
278
|
}
|
|
253
279
|
return {
|
|
254
280
|
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
255
|
-
node: context.
|
|
281
|
+
node: context.peek(),
|
|
256
282
|
syntax,
|
|
257
|
-
error: success ? '' : `could not match
|
|
283
|
+
error: success ? '' : `could not match syntax: ${renderSyntax(syntax)}`,
|
|
258
284
|
context
|
|
259
285
|
};
|
|
260
286
|
}
|
|
@@ -277,7 +303,7 @@ function matchRepeatable(syntax, context, options) {
|
|
|
277
303
|
};
|
|
278
304
|
}
|
|
279
305
|
function matchList(syntax, context, options) {
|
|
280
|
-
let success
|
|
306
|
+
let success;
|
|
281
307
|
let result;
|
|
282
308
|
let count = 0;
|
|
283
309
|
let con = context.clone();
|
|
@@ -286,15 +312,16 @@ function matchList(syntax, context, options) {
|
|
|
286
312
|
while (!con.done() && con.peek()?.typ != EnumToken.CommaTokenType) {
|
|
287
313
|
tokens.push(con.next());
|
|
288
314
|
}
|
|
289
|
-
if (tokens.length == 0) {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
}
|
|
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
|
+
// }
|
|
298
325
|
result = doEvaluateSyntax([syntax], createContext(tokens), {
|
|
299
326
|
...options,
|
|
300
327
|
isList: false,
|
|
@@ -315,7 +342,7 @@ function matchList(syntax, context, options) {
|
|
|
315
342
|
}
|
|
316
343
|
}
|
|
317
344
|
success = count > 0;
|
|
318
|
-
if (
|
|
345
|
+
if (success && syntax.occurence != null) {
|
|
319
346
|
success = count >= syntax.occurence.min;
|
|
320
347
|
if (success && syntax.occurence.max != null) {
|
|
321
348
|
success = count <= syntax.occurence.max;
|
|
@@ -323,7 +350,7 @@ function matchList(syntax, context, options) {
|
|
|
323
350
|
}
|
|
324
351
|
return {
|
|
325
352
|
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
326
|
-
node: context.
|
|
353
|
+
node: context.peek(),
|
|
327
354
|
syntax,
|
|
328
355
|
error: '',
|
|
329
356
|
context
|
|
@@ -348,7 +375,7 @@ function matchOccurence(syntax, context, options) {
|
|
|
348
375
|
}
|
|
349
376
|
return {
|
|
350
377
|
valid: sucesss ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
351
|
-
node: context.
|
|
378
|
+
node: context.peek(),
|
|
352
379
|
syntax,
|
|
353
380
|
error: sucesss ? '' : `expected ${renderSyntax(syntax)} ${syntax.occurence.min} to ${syntax.occurence.max} occurences, got ${counter}`,
|
|
354
381
|
context
|
|
@@ -372,26 +399,29 @@ function match(syntax, context, options) {
|
|
|
372
399
|
}
|
|
373
400
|
return {
|
|
374
401
|
valid: SyntaxValidationResult.Drop,
|
|
375
|
-
node: context.
|
|
402
|
+
node: context.peek(),
|
|
376
403
|
syntax,
|
|
377
404
|
error: `expected '${ValidationTokenEnum[syntax.typ].toLowerCase()}', got '${context.done() ? null : renderToken(context.peek())}'`,
|
|
378
405
|
context
|
|
379
406
|
};
|
|
380
407
|
}
|
|
381
408
|
}
|
|
382
|
-
if (token.typ == EnumToken.WhitespaceTokenType) {
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
+
// }
|
|
395
425
|
if (syntax.typ != ValidationTokenEnum.PropertyType && (token?.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val))) {
|
|
396
426
|
const result = doEvaluateSyntax(getParsedSyntax("functions" /* ValidationSyntaxGroupEnum.Functions */, token.val)?.[0]?.chi ?? [], createContext(token.chi), {
|
|
397
427
|
...options,
|
|
@@ -427,7 +457,7 @@ function match(syntax, context, options) {
|
|
|
427
457
|
return matchPropertyType(syntax, context, options);
|
|
428
458
|
case ValidationTokenEnum.ValidationFunctionDefinition:
|
|
429
459
|
token = context.peek();
|
|
430
|
-
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))) {
|
|
431
461
|
return {
|
|
432
462
|
valid: SyntaxValidationResult.Drop,
|
|
433
463
|
node: context.next(),
|
|
@@ -437,6 +467,7 @@ function match(syntax, context, options) {
|
|
|
437
467
|
};
|
|
438
468
|
}
|
|
439
469
|
result = match(getParsedSyntax("syntaxes" /* ValidationSyntaxGroupEnum.Syntaxes */, syntax.val + '()')?.[0], context, options);
|
|
470
|
+
// console.error(`>>[]
|
|
440
471
|
if (result.valid == SyntaxValidationResult.Valid) {
|
|
441
472
|
context.next();
|
|
442
473
|
result.context = context;
|
|
@@ -451,37 +482,49 @@ function match(syntax, context, options) {
|
|
|
451
482
|
occurence: null,
|
|
452
483
|
atLeastOnce: null
|
|
453
484
|
});
|
|
454
|
-
case ValidationTokenEnum.Parens:
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
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;
|
|
467
501
|
case ValidationTokenEnum.Comma:
|
|
468
502
|
success = context.peek()?.typ == EnumToken.CommaTokenType;
|
|
469
503
|
if (success) {
|
|
470
504
|
context.next();
|
|
471
505
|
}
|
|
472
506
|
break;
|
|
473
|
-
case ValidationTokenEnum.Number:
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
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;
|
|
485
528
|
case ValidationTokenEnum.Separator:
|
|
486
529
|
{
|
|
487
530
|
const token = context.peek();
|
|
@@ -496,21 +539,22 @@ function match(syntax, context, options) {
|
|
|
496
539
|
if (!wildCardFuncs.includes(syntax.val) && syntax.val != token.val) {
|
|
497
540
|
break;
|
|
498
541
|
}
|
|
499
|
-
if (
|
|
500
|
-
success = doEvaluateSyntax(syntax.chi, createContext(token.chi),
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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)}`);
|
|
507
551
|
if (success) {
|
|
508
552
|
context.next();
|
|
509
553
|
}
|
|
510
554
|
break;
|
|
511
555
|
}
|
|
512
|
-
if (
|
|
513
|
-
success =
|
|
556
|
+
if (token.typ != EnumToken.ParensTokenType && funcLike.includes(token.typ)) {
|
|
557
|
+
success = doEvaluateSyntax(syntax.chi, createContext(token.chi), {
|
|
514
558
|
...options,
|
|
515
559
|
isRepeatable: null,
|
|
516
560
|
isList: null,
|
|
@@ -539,7 +583,9 @@ function match(syntax, context, options) {
|
|
|
539
583
|
function matchPropertyType(syntax, context, options) {
|
|
540
584
|
if (![
|
|
541
585
|
'bg-position',
|
|
542
|
-
'
|
|
586
|
+
'integer',
|
|
587
|
+
'length-percentage', 'flex', 'calc-sum', 'color',
|
|
588
|
+
'color-base', 'system-color', 'deprecated-system-color',
|
|
543
589
|
'pseudo-class-selector', 'pseudo-element-selector'
|
|
544
590
|
].includes(syntax.val)) {
|
|
545
591
|
if (syntax.val in config["syntaxes" /* ValidationSyntaxGroupEnum.Syntaxes */]) {
|
|
@@ -683,10 +729,10 @@ function matchPropertyType(syntax, context, options) {
|
|
|
683
729
|
success = token.typ == EnumToken.DashedIdenTokenType;
|
|
684
730
|
break;
|
|
685
731
|
case 'system-color':
|
|
686
|
-
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));
|
|
687
733
|
break;
|
|
688
734
|
case 'deprecated-system-color':
|
|
689
|
-
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));
|
|
690
736
|
break;
|
|
691
737
|
case 'color':
|
|
692
738
|
case 'color-base':
|
|
@@ -702,10 +748,10 @@ function matchPropertyType(syntax, context, options) {
|
|
|
702
748
|
}
|
|
703
749
|
break;
|
|
704
750
|
case 'hex-color':
|
|
705
|
-
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));
|
|
706
752
|
break;
|
|
707
753
|
case 'integer':
|
|
708
|
-
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)));
|
|
709
755
|
if ('range' in syntax) {
|
|
710
756
|
success = success && +token.val >= +syntax.range[0] && +token.val <= +syntax.range[1];
|
|
711
757
|
}
|
|
@@ -749,7 +795,7 @@ function matchPropertyType(syntax, context, options) {
|
|
|
749
795
|
success = token.typ == EnumToken.HashTokenType;
|
|
750
796
|
break;
|
|
751
797
|
case 'string':
|
|
752
|
-
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));
|
|
753
799
|
break;
|
|
754
800
|
case 'time':
|
|
755
801
|
success = token.typ == EnumToken.TimeTokenType || (token.typ == EnumToken.FunctionTokenType && token.val == 'calc');
|
|
@@ -813,10 +859,12 @@ function someOf(syntaxes, context, options) {
|
|
|
813
859
|
let success = false;
|
|
814
860
|
const matched = [];
|
|
815
861
|
for (i = 0; i < syntaxes.length; i++) {
|
|
816
|
-
if (context.peek()?.typ == EnumToken.WhitespaceTokenType) {
|
|
817
|
-
|
|
818
|
-
|
|
862
|
+
// if (context.peek<Token>()?.typ == EnumToken.WhitespaceTokenType) {
|
|
863
|
+
//
|
|
864
|
+
// context.next();
|
|
865
|
+
// }
|
|
819
866
|
result = doEvaluateSyntax(syntaxes[i], context.clone(), options);
|
|
867
|
+
// console.error(`>> someOf: ${JSON.stringify({result}, null, 1)}`);
|
|
820
868
|
if (result.valid == SyntaxValidationResult.Valid) {
|
|
821
869
|
success = true;
|
|
822
870
|
if (result.context.done()) {
|
|
@@ -829,11 +877,13 @@ function someOf(syntaxes, context, options) {
|
|
|
829
877
|
// pick the best match
|
|
830
878
|
matched.sort((a, b) => a.context.done() ? -1 : b.context.done() ? 1 : b.context.index - a.context.index);
|
|
831
879
|
}
|
|
880
|
+
// console.error(JSON.stringify({matched}, null, 1));
|
|
881
|
+
// console.error(new Error('someOf'));
|
|
832
882
|
return matched[0] ?? {
|
|
833
883
|
valid: SyntaxValidationResult.Drop,
|
|
834
|
-
node: context.
|
|
884
|
+
node: context.peek(),
|
|
835
885
|
syntax: null,
|
|
836
|
-
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), ''), '')}`,
|
|
837
887
|
context
|
|
838
888
|
};
|
|
839
889
|
}
|
|
@@ -855,9 +905,9 @@ function anyOf(syntaxes, context, options) {
|
|
|
855
905
|
}
|
|
856
906
|
return {
|
|
857
907
|
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
858
|
-
node: context.
|
|
908
|
+
node: context.peek(),
|
|
859
909
|
syntax: null,
|
|
860
|
-
error: success ? '' : `could not match
|
|
910
|
+
error: success ? '' : `could not match syntax: ${syntaxes.reduce((acc, curr) => acc + '[' + curr.reduce((acc, curr) => acc + renderSyntax(curr), '') + ']', '')}`,
|
|
861
911
|
context
|
|
862
912
|
};
|
|
863
913
|
}
|
|
@@ -884,9 +934,10 @@ function allOf(syntax, context, options) {
|
|
|
884
934
|
for (i = 0; i < slice.length; i++) {
|
|
885
935
|
if (slice[i].typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(slice[i].val.toLowerCase())) {
|
|
886
936
|
vars.push(slice[i]);
|
|
887
|
-
if (slice[i + 1]?.typ == EnumToken.WhitespaceTokenType) {
|
|
888
|
-
|
|
889
|
-
|
|
937
|
+
// if (slice[i + 1]?.typ == EnumToken.WhitespaceTokenType) {
|
|
938
|
+
//
|
|
939
|
+
// vars.push(slice[++i]);
|
|
940
|
+
// }
|
|
890
941
|
continue;
|
|
891
942
|
}
|
|
892
943
|
if (slice[i].typ == EnumToken.CommaTokenType || (slice[i].typ == EnumToken.LiteralTokenType && slice[i].val == '/')) {
|
|
@@ -903,18 +954,17 @@ function allOf(syntax, context, options) {
|
|
|
903
954
|
let j;
|
|
904
955
|
for (i = 0; i < syntax.length; i++) {
|
|
905
956
|
if (syntax[i].length == 1 && syntax[i][0].isOptional) {
|
|
906
|
-
syntax[i][0].isOptional = false;
|
|
907
957
|
j = 0;
|
|
908
958
|
cp = con.clone();
|
|
909
959
|
slice = cp.slice();
|
|
910
960
|
if (cp.done()) {
|
|
911
|
-
syntax[i][0].isOptional = true;
|
|
912
961
|
syntax.splice(i, 1);
|
|
913
962
|
i = -1;
|
|
914
963
|
continue;
|
|
915
964
|
}
|
|
916
965
|
while (!cp.done()) {
|
|
917
|
-
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)}`);
|
|
918
968
|
if (result.valid == SyntaxValidationResult.Valid) {
|
|
919
969
|
let end = slice.indexOf(cp.current());
|
|
920
970
|
if (end == -1) {
|
|
@@ -929,9 +979,8 @@ function allOf(syntax, context, options) {
|
|
|
929
979
|
cp.next();
|
|
930
980
|
j++;
|
|
931
981
|
}
|
|
932
|
-
syntax[i][0].isOptional = true;
|
|
933
982
|
// @ts-ignore
|
|
934
|
-
if (result?.valid == SyntaxValidationResult.Valid) {
|
|
983
|
+
if (result?.valid == SyntaxValidationResult.Valid || (syntax[i].length == 1 && syntax[i][0].isOptional)) {
|
|
935
984
|
syntax.splice(i, 1);
|
|
936
985
|
i = -1;
|
|
937
986
|
}
|
|
@@ -944,12 +993,13 @@ function allOf(syntax, context, options) {
|
|
|
944
993
|
i = -1;
|
|
945
994
|
}
|
|
946
995
|
}
|
|
996
|
+
// console.error()
|
|
947
997
|
const success = syntax.length == 0;
|
|
948
998
|
return {
|
|
949
999
|
valid: success ? SyntaxValidationResult.Valid : SyntaxValidationResult.Drop,
|
|
950
|
-
node: context.
|
|
1000
|
+
node: context.peek(),
|
|
951
1001
|
syntax: syntax?.[0]?.[0] ?? null,
|
|
952
|
-
error: `could not match
|
|
1002
|
+
error: `could not match syntax: ${syntax.reduce((acc, curr) => acc + '[' + curr.reduce((acc, curr) => acc + renderSyntax(curr), '') + ']', '')}`,
|
|
953
1003
|
context: success ? con : context
|
|
954
1004
|
};
|
|
955
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 };
|
|
@@ -6,7 +6,7 @@ import '../../ast/walk.js';
|
|
|
6
6
|
import '../../parser/parse.js';
|
|
7
7
|
import '../../parser/tokenize.js';
|
|
8
8
|
import '../../parser/utils/config.js';
|
|
9
|
-
import '../../
|
|
9
|
+
import '../../syntax/color/utils/constants.js';
|
|
10
10
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
11
11
|
import { validateCompoundSelector } from './compound-selector.js';
|
|
12
12
|
|