@tbela99/css-parser 1.0.0 → 1.1.1-alpha4
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 +269 -0
- package/README.md +16 -11
- package/dist/index-umd-web.js +3805 -1894
- package/dist/index.cjs +3806 -1895
- package/dist/index.d.ts +184 -57
- package/dist/lib/ast/expand.js +2 -1
- package/dist/lib/ast/features/calc.js +12 -1
- package/dist/lib/ast/features/inlinecssvariables.js +47 -24
- package/dist/lib/ast/features/prefix.js +117 -86
- package/dist/lib/ast/features/shorthand.js +29 -6
- package/dist/lib/ast/features/transform.js +10 -3
- package/dist/lib/ast/features/type.js +7 -0
- package/dist/lib/ast/math/expression.js +7 -1
- package/dist/lib/ast/math/math.js +6 -0
- package/dist/lib/ast/minify.js +165 -77
- package/dist/lib/ast/transform/compute.js +1 -0
- package/dist/lib/ast/transform/matrix.js +1 -0
- package/dist/lib/ast/types.js +26 -15
- package/dist/lib/ast/utils/utils.js +104 -0
- package/dist/lib/ast/walk.js +33 -7
- package/dist/lib/fs/resolve.js +10 -0
- package/dist/lib/parser/declaration/list.js +48 -45
- package/dist/lib/parser/declaration/map.js +1 -0
- package/dist/lib/parser/declaration/set.js +2 -1
- package/dist/lib/parser/parse.js +350 -279
- package/dist/lib/parser/tokenize.js +147 -72
- package/dist/lib/parser/utils/declaration.js +4 -3
- package/dist/lib/parser/utils/type.js +2 -1
- package/dist/lib/renderer/color/a98rgb.js +2 -1
- package/dist/lib/renderer/color/color-mix.js +10 -7
- package/dist/lib/renderer/color/color.js +171 -153
- package/dist/lib/renderer/color/hex.js +2 -1
- package/dist/lib/renderer/color/hsl.js +2 -1
- package/dist/lib/renderer/color/hwb.js +2 -1
- package/dist/lib/renderer/color/lab.js +2 -1
- package/dist/lib/renderer/color/lch.js +2 -1
- package/dist/lib/renderer/color/oklab.js +2 -1
- package/dist/lib/renderer/color/oklch.js +2 -1
- package/dist/lib/renderer/color/p3.js +2 -1
- package/dist/lib/renderer/color/rec2020.js +2 -1
- package/dist/lib/renderer/color/relativecolor.js +17 -11
- package/dist/lib/renderer/color/rgb.js +4 -3
- package/dist/lib/renderer/color/srgb.js +18 -17
- package/dist/lib/renderer/color/utils/components.js +6 -5
- package/dist/lib/renderer/color/utils/constants.js +47 -3
- package/dist/lib/renderer/color/xyz.js +2 -1
- package/dist/lib/renderer/color/xyzd50.js +2 -1
- package/dist/lib/renderer/render.js +48 -20
- package/dist/lib/syntax/syntax.js +257 -140
- package/dist/lib/validation/at-rules/container.js +75 -97
- package/dist/lib/validation/at-rules/counter-style.js +9 -8
- package/dist/lib/validation/at-rules/custom-media.js +13 -15
- package/dist/lib/validation/at-rules/document.js +22 -27
- package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
- package/dist/lib/validation/at-rules/import.js +30 -81
- package/dist/lib/validation/at-rules/keyframes.js +18 -22
- package/dist/lib/validation/at-rules/layer.js +5 -5
- package/dist/lib/validation/at-rules/media.js +42 -52
- package/dist/lib/validation/at-rules/namespace.js +19 -23
- package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
- package/dist/lib/validation/at-rules/page.js +8 -7
- package/dist/lib/validation/at-rules/supports.js +73 -82
- package/dist/lib/validation/at-rules/when.js +32 -36
- package/dist/lib/validation/atrule.js +15 -14
- package/dist/lib/validation/config.js +24 -1
- package/dist/lib/validation/config.json.js +611 -111
- package/dist/lib/validation/parser/parse.js +206 -212
- package/dist/lib/validation/parser/types.js +1 -1
- package/dist/lib/validation/selector.js +3 -3
- package/dist/lib/validation/syntax.js +984 -0
- package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
- package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
- package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
- package/dist/lib/validation/syntaxes/family-name.js +9 -8
- package/dist/lib/validation/syntaxes/keyframe-block-list.js +4 -3
- package/dist/lib/validation/syntaxes/keyframe-selector.js +15 -18
- package/dist/lib/validation/syntaxes/layer-name.js +6 -5
- package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
- package/dist/lib/validation/syntaxes/relative-selector.js +2 -1
- package/dist/lib/validation/syntaxes/url.js +18 -22
- package/dist/lib/validation/utils/list.js +2 -1
- package/dist/lib/validation/utils/whitespace.js +2 -1
- package/dist/node/index.js +4 -2
- package/dist/node/load.js +5 -0
- package/dist/web/index.js +4 -2
- package/dist/web/load.js +5 -0
- package/package.json +14 -13
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { ValidationTokenEnum } from './types.js';
|
|
2
2
|
import { isIdent, isPseudo } from '../../syntax/syntax.js';
|
|
3
|
+
import { getTokenType as getTokenType$1 } from '../../parser/parse.js';
|
|
4
|
+
import '../../parser/tokenize.js';
|
|
5
|
+
import '../../parser/utils/config.js';
|
|
6
|
+
import { EnumToken } from '../../ast/types.js';
|
|
7
|
+
import '../../ast/minify.js';
|
|
8
|
+
import '../../ast/walk.js';
|
|
9
|
+
import '../../renderer/color/utils/constants.js';
|
|
10
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
3
11
|
|
|
4
|
-
var WalkValidationTokenEnum;
|
|
5
|
-
(function (WalkValidationTokenEnum) {
|
|
6
|
-
WalkValidationTokenEnum[WalkValidationTokenEnum["IgnoreChildren"] = 0] = "IgnoreChildren";
|
|
7
|
-
WalkValidationTokenEnum[WalkValidationTokenEnum["IgnoreNode"] = 1] = "IgnoreNode";
|
|
8
|
-
WalkValidationTokenEnum[WalkValidationTokenEnum["IgnoreAll"] = 2] = "IgnoreAll";
|
|
9
|
-
})(WalkValidationTokenEnum || (WalkValidationTokenEnum = {}));
|
|
10
|
-
var WalkValidationTokenKeyTypeEnum;
|
|
11
|
-
(function (WalkValidationTokenKeyTypeEnum) {
|
|
12
|
-
WalkValidationTokenKeyTypeEnum["Array"] = "array";
|
|
13
|
-
WalkValidationTokenKeyTypeEnum["Children"] = "chi";
|
|
14
|
-
WalkValidationTokenKeyTypeEnum["Left"] = "l";
|
|
15
|
-
WalkValidationTokenKeyTypeEnum["Right"] = "r";
|
|
16
|
-
WalkValidationTokenKeyTypeEnum["Prelude"] = "prelude";
|
|
17
|
-
})(WalkValidationTokenKeyTypeEnum || (WalkValidationTokenKeyTypeEnum = {}));
|
|
18
12
|
const skipped = [
|
|
19
13
|
ValidationTokenEnum.Star,
|
|
20
14
|
ValidationTokenEnum.HashMark,
|
|
@@ -28,18 +22,6 @@ const objectProperties = {
|
|
|
28
22
|
writable: true,
|
|
29
23
|
configurable: true
|
|
30
24
|
};
|
|
31
|
-
// syntaxes: keyword | <'property'> | <function>
|
|
32
|
-
// "none | [ [<dashed-ident> || <try-tactic>] | inset-area( <'inset-area'> ) ]#"
|
|
33
|
-
// ""
|
|
34
|
-
// : "<outline-radius>{1,4} [ / <outline-radius>{1,4} ]?
|
|
35
|
-
// ""
|
|
36
|
-
// false | true
|
|
37
|
-
// [ <mask-reference> || <position> [ / <bg-size> ]? || <repeat-style> || [ <box> | border | padding | content | text ] || [ <box> | border | padding | content ] ]#
|
|
38
|
-
// false | true | green | pipe
|
|
39
|
-
// keyword | <'property'> | <function>
|
|
40
|
-
// [<dashed-ident> || <try-tactic>]
|
|
41
|
-
// [ <mask-reference> || <position> [ / <bg-size> ]? || <repeat-style> || [ <box> | border | padding | content | text ] || [ <box> | border | padding | content ] ]#
|
|
42
|
-
// none | [ [<dashed-ident> || <try-tactic>] | inset-area( <'inset-area'> ) ]
|
|
43
25
|
function* tokenize(syntax, position = { ind: 0, lin: 1, col: 0 }, currentPosition = {
|
|
44
26
|
ind: -1,
|
|
45
27
|
lin: 1,
|
|
@@ -51,6 +33,13 @@ function* tokenize(syntax, position = { ind: 0, lin: 1, col: 0 }, currentPositio
|
|
|
51
33
|
let chr = syntax.charAt(i);
|
|
52
34
|
move(currentPosition, chr);
|
|
53
35
|
switch (chr) {
|
|
36
|
+
case '∞':
|
|
37
|
+
if (buffer.length > 0) {
|
|
38
|
+
yield getTokenType(buffer, position, currentPosition);
|
|
39
|
+
}
|
|
40
|
+
yield getTokenType(chr, position, currentPosition);
|
|
41
|
+
buffer = '';
|
|
42
|
+
break;
|
|
54
43
|
case '\\':
|
|
55
44
|
if (buffer.length > 0) {
|
|
56
45
|
yield getTokenType(buffer, position, currentPosition);
|
|
@@ -181,66 +170,13 @@ function* tokenize(syntax, position = { ind: 0, lin: 1, col: 0 }, currentPositio
|
|
|
181
170
|
yield getTokenType(buffer, position, currentPosition);
|
|
182
171
|
}
|
|
183
172
|
}
|
|
184
|
-
function columnCallback(token, parent, key) {
|
|
185
|
-
if (key == WalkValidationTokenKeyTypeEnum.Prelude) {
|
|
186
|
-
return WalkValidationTokenEnum.IgnoreAll;
|
|
187
|
-
}
|
|
188
|
-
if (token.typ == ValidationTokenEnum.ColumnToken || token.typ == ValidationTokenEnum.Whitespace) {
|
|
189
|
-
return WalkValidationTokenEnum.IgnoreNode;
|
|
190
|
-
}
|
|
191
|
-
return WalkValidationTokenEnum.IgnoreChildren;
|
|
192
|
-
}
|
|
193
|
-
function toColumnArray(ast, parent) {
|
|
194
|
-
const result = new Map;
|
|
195
|
-
// @ts-ignore
|
|
196
|
-
for (const { token } of walkValidationToken(ast, null, columnCallback)) {
|
|
197
|
-
result.set(JSON.stringify(token), token);
|
|
198
|
-
}
|
|
199
|
-
const node = {
|
|
200
|
-
typ: ValidationTokenEnum.ColumnArrayToken,
|
|
201
|
-
chi: [...result.values()]
|
|
202
|
-
};
|
|
203
|
-
if (parent != null) {
|
|
204
|
-
replaceNode(parent, ast, node);
|
|
205
|
-
}
|
|
206
|
-
return node;
|
|
207
|
-
}
|
|
208
|
-
function replaceNode(parent, target, node) {
|
|
209
|
-
if ('l' in parent && parent.l == target) {
|
|
210
|
-
parent.l = node;
|
|
211
|
-
}
|
|
212
|
-
if ('r' in parent && parent.r == target) {
|
|
213
|
-
parent.r = node;
|
|
214
|
-
}
|
|
215
|
-
if ('chi' in parent) {
|
|
216
|
-
// @ts-ignore
|
|
217
|
-
for (let i = 0; i < parent.chi.length; i++) {
|
|
218
|
-
// @ts-ignore
|
|
219
|
-
if (parent.chi[i] == target) {
|
|
220
|
-
// @ts-ignore
|
|
221
|
-
parent.chi[i] = node;
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
function transform(context) {
|
|
228
|
-
for (const { token, parent } of walkValidationToken(context)) {
|
|
229
|
-
switch (token.typ) {
|
|
230
|
-
case ValidationTokenEnum.ColumnToken:
|
|
231
|
-
toColumnArray(token, parent);
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return context;
|
|
236
|
-
}
|
|
237
173
|
function parseSyntax(syntax) {
|
|
238
174
|
const root = Object.defineProperty({
|
|
239
175
|
typ: ValidationTokenEnum.Root,
|
|
240
176
|
chi: []
|
|
241
177
|
}, 'pos', { ...objectProperties, value: { ind: 0, lin: 1, col: 0 } });
|
|
242
178
|
// return minify(doParseSyntax(syntaxes, tokenize(syntaxes), root)) as ValidationRootToken;
|
|
243
|
-
return minify(
|
|
179
|
+
return minify(doParseSyntax(syntax, tokenize(syntax), root));
|
|
244
180
|
}
|
|
245
181
|
function matchParens(syntax, iterator) {
|
|
246
182
|
let item;
|
|
@@ -255,7 +191,7 @@ function matchParens(syntax, iterator) {
|
|
|
255
191
|
}
|
|
256
192
|
else if (match == 0) {
|
|
257
193
|
func = items.at(-1);
|
|
258
|
-
if (func == null) {
|
|
194
|
+
if (func == null || func.val == null || func.val === '') {
|
|
259
195
|
func = Object.defineProperty({
|
|
260
196
|
typ: ValidationTokenEnum.Parens,
|
|
261
197
|
val: '',
|
|
@@ -372,6 +308,10 @@ function matchCurlBraces(syntax, iterator) {
|
|
|
372
308
|
else {
|
|
373
309
|
items.push(item.value);
|
|
374
310
|
}
|
|
311
|
+
if ('chi' in item.value) {
|
|
312
|
+
// @ts-ignore
|
|
313
|
+
item.value.chi = matchCurlBraces(syntax, item.value.chi[Symbol.iterator]());
|
|
314
|
+
}
|
|
375
315
|
break;
|
|
376
316
|
}
|
|
377
317
|
}
|
|
@@ -382,7 +322,7 @@ function matchCurlBraces(syntax, iterator) {
|
|
|
382
322
|
if (i > 0 && it.typ == ValidationTokenEnum.Block && it.chi[0]?.typ == ValidationTokenEnum.Number) {
|
|
383
323
|
items[i - 1].occurence = {
|
|
384
324
|
min: +it.chi[0].val,
|
|
385
|
-
max: +(it.chi[2] ?? it.chi[0]).val
|
|
325
|
+
max: it.chi[2]?.typ == ValidationTokenEnum.InfinityToken ? Number.POSITIVE_INFINITY : +(it.chi[2] ?? it.chi[0]).val
|
|
386
326
|
};
|
|
387
327
|
items.splice(i--, 1);
|
|
388
328
|
continue;
|
|
@@ -506,8 +446,17 @@ function matchAtRule(syntax, iterator) {
|
|
|
506
446
|
return children;
|
|
507
447
|
}
|
|
508
448
|
function matchToken(syntax, iterator, validationToken) {
|
|
449
|
+
if (iterator.length == 0) {
|
|
450
|
+
return [];
|
|
451
|
+
}
|
|
452
|
+
if (Array.isArray(iterator[0])) {
|
|
453
|
+
const result = [];
|
|
454
|
+
for (let i = 0; i < iterator.length; i++) {
|
|
455
|
+
result.push(matchToken(syntax, iterator[i], validationToken));
|
|
456
|
+
}
|
|
457
|
+
return result;
|
|
458
|
+
}
|
|
509
459
|
let children = [];
|
|
510
|
-
let item;
|
|
511
460
|
let token = null;
|
|
512
461
|
let i;
|
|
513
462
|
if (validationToken == ValidationTokenEnum.Ampersand) {
|
|
@@ -538,42 +487,44 @@ function matchToken(syntax, iterator, validationToken) {
|
|
|
538
487
|
}
|
|
539
488
|
else if ('chi' in children[i]) {
|
|
540
489
|
// @ts-ignore
|
|
541
|
-
children[i].chi = matchToken(syntax, children[i].chi
|
|
490
|
+
children[i].chi = matchToken(syntax, children[i].chi, validationToken);
|
|
542
491
|
}
|
|
543
492
|
else if ('l' in children[i]) {
|
|
544
493
|
// @ts-ignore
|
|
545
|
-
children[i].l = matchToken(syntax, children[i].l
|
|
494
|
+
children[i].l = matchToken(syntax, children[i].l, validationToken);
|
|
546
495
|
// @ts-ignore
|
|
547
|
-
children[i].r = matchToken(syntax, children[i].r
|
|
496
|
+
children[i].r = matchToken(syntax, children[i].r, validationToken);
|
|
548
497
|
}
|
|
549
498
|
}
|
|
550
499
|
return children;
|
|
551
500
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
501
|
+
let item;
|
|
502
|
+
for (i = 0; i < iterator.length; i++) {
|
|
503
|
+
item = iterator[i];
|
|
504
|
+
if (item.typ == validationToken) {
|
|
505
|
+
if (item.typ == ValidationTokenEnum.Pipe) {
|
|
555
506
|
token = Object.defineProperty({
|
|
556
507
|
typ: ValidationTokenEnum.PipeToken,
|
|
557
|
-
chi: [matchToken(syntax, children.slice()
|
|
508
|
+
chi: [matchToken(syntax, children.slice(), validationToken)], //.concat(matchToken(syntaxes, children.slice()[Symbol.iterator]() as Iterator<ValidationTokenIteratorValue>, validationToken), matchToken(syntaxes, iterator, validationToken)),
|
|
558
509
|
// @ts-ignore
|
|
559
510
|
// l: matchToken(syntaxes, children.slice()[Symbol.iterator](), validationToken),
|
|
560
511
|
// r: matchToken(syntaxes, iterator, validationToken)
|
|
561
512
|
}, 'pos', {
|
|
562
513
|
...objectProperties,
|
|
563
|
-
value: item.
|
|
514
|
+
value: item.pos
|
|
564
515
|
});
|
|
565
516
|
children.length = 0;
|
|
566
|
-
while ((item = iterator
|
|
567
|
-
if (item.
|
|
568
|
-
token.chi.push(matchToken(syntax, children.slice()
|
|
517
|
+
while ((item = iterator[++i]) != null) {
|
|
518
|
+
if (item.typ == ValidationTokenEnum.Pipe) {
|
|
519
|
+
token.chi.push(matchToken(syntax, children.slice(), validationToken));
|
|
569
520
|
children.length = 0;
|
|
570
521
|
}
|
|
571
522
|
else {
|
|
572
|
-
children.push(item
|
|
523
|
+
children.push(item);
|
|
573
524
|
}
|
|
574
525
|
}
|
|
575
526
|
if (children.length > 0) {
|
|
576
|
-
token.chi.push(matchToken(syntax, children.slice()
|
|
527
|
+
token.chi.push(matchToken(syntax, children.slice(), validationToken));
|
|
577
528
|
}
|
|
578
529
|
token.chi.sort((a, b) => {
|
|
579
530
|
if (a.some((t) => t.isList)) {
|
|
@@ -613,55 +564,56 @@ function matchToken(syntax, iterator, validationToken) {
|
|
|
613
564
|
else {
|
|
614
565
|
token = Object.defineProperty({
|
|
615
566
|
typ: ValidationTokenEnum.ColumnToken,
|
|
616
|
-
l: matchToken(syntax, children.slice()
|
|
617
|
-
r: matchToken(syntax, iterator, validationToken)
|
|
567
|
+
l: matchToken(syntax, children.slice(), validationToken),
|
|
568
|
+
r: matchToken(syntax, iterator.slice(i + 1), validationToken)
|
|
618
569
|
}, 'pos', {
|
|
619
570
|
...objectProperties,
|
|
620
|
-
value: item.
|
|
571
|
+
value: item.pos
|
|
621
572
|
});
|
|
573
|
+
i = iterator.length;
|
|
622
574
|
}
|
|
623
575
|
children.length = 0;
|
|
624
576
|
children.push(token);
|
|
625
|
-
while ((item = iterator
|
|
626
|
-
children.push(item
|
|
627
|
-
if ('chi' in item
|
|
577
|
+
while ((item = iterator[++i]) != null) {
|
|
578
|
+
children.push(item);
|
|
579
|
+
if ('chi' in item) {
|
|
628
580
|
// @ts-ignore
|
|
629
|
-
item.
|
|
581
|
+
item.chi = matchToken(syntax, item.chi, validationToken);
|
|
630
582
|
}
|
|
631
|
-
else if ('l' in item
|
|
583
|
+
else if ('l' in item) {
|
|
632
584
|
// @ts-ignore
|
|
633
|
-
item.
|
|
585
|
+
item.l = matchToken(syntax, item.l, validationToken);
|
|
634
586
|
// @ts-ignore
|
|
635
|
-
item.
|
|
587
|
+
item.r = matchToken(syntax, item.r, validationToken);
|
|
636
588
|
}
|
|
637
589
|
}
|
|
638
590
|
// @ts-ignore
|
|
639
591
|
return children;
|
|
640
592
|
}
|
|
641
593
|
else {
|
|
642
|
-
|
|
643
|
-
|
|
594
|
+
// @ts-ignore
|
|
595
|
+
children.push(item);
|
|
596
|
+
if ('chi' in item) {
|
|
644
597
|
// @ts-ignore
|
|
645
|
-
item.
|
|
598
|
+
item.chi = matchToken(syntax, item.chi, validationToken);
|
|
646
599
|
}
|
|
647
|
-
else if ('l' in item
|
|
600
|
+
else if ('l' in item) {
|
|
601
|
+
item.l = matchToken(syntax, item.l, validationToken);
|
|
648
602
|
// @ts-ignore
|
|
649
|
-
item.
|
|
650
|
-
// @ts-ignore
|
|
651
|
-
item.value.r = matchToken(syntax, item.value.r[Symbol.iterator](), validationToken);
|
|
603
|
+
item.r = matchToken(syntax, item.r, validationToken);
|
|
652
604
|
}
|
|
653
605
|
}
|
|
654
606
|
}
|
|
655
607
|
return children;
|
|
656
608
|
}
|
|
657
|
-
function
|
|
609
|
+
function parseSyntaxTokens(syntax, iterator) {
|
|
658
610
|
const items = [];
|
|
659
611
|
let item;
|
|
660
612
|
let i;
|
|
661
613
|
while ((item = iterator.next()) && !item.done) {
|
|
662
614
|
if (Array.isArray(item.value)) {
|
|
663
615
|
// @ts-ignore
|
|
664
|
-
item.value =
|
|
616
|
+
item.value = parseSyntaxTokens(syntax, item.value[Symbol.iterator]());
|
|
665
617
|
}
|
|
666
618
|
switch (item.value.typ) {
|
|
667
619
|
case ValidationTokenEnum.Star:
|
|
@@ -694,14 +646,14 @@ function parseTokens(syntax, iterator) {
|
|
|
694
646
|
else if (item.value.typ == ValidationTokenEnum.OpenCurlyBrace) {
|
|
695
647
|
items[i].occurence = {
|
|
696
648
|
min: 0,
|
|
697
|
-
max:
|
|
649
|
+
max: null
|
|
698
650
|
};
|
|
699
651
|
while ((item = iterator.next()) && !item.done) {
|
|
700
652
|
if (item.value.typ == ValidationTokenEnum.Number) {
|
|
701
653
|
// @ts-ignore
|
|
702
654
|
if (items[i].occurence.min == 0) {
|
|
703
655
|
// @ts-ignore
|
|
704
|
-
items[i].occurence.min = +item.value.val;
|
|
656
|
+
items[i].occurence.min = items[i].occurence.max = +item.value.val;
|
|
705
657
|
}
|
|
706
658
|
else {
|
|
707
659
|
// @ts-ignore
|
|
@@ -715,7 +667,7 @@ function parseTokens(syntax, iterator) {
|
|
|
715
667
|
}
|
|
716
668
|
break;
|
|
717
669
|
case ValidationTokenEnum.Pipe:
|
|
718
|
-
item.value.chi = item.value.chi.map((t) =>
|
|
670
|
+
item.value.chi = item.value.chi.map((t) => parseSyntaxTokens(syntax, t[Symbol.iterator]()));
|
|
719
671
|
items.push(item.value);
|
|
720
672
|
break;
|
|
721
673
|
default:
|
|
@@ -726,46 +678,39 @@ function parseTokens(syntax, iterator) {
|
|
|
726
678
|
for (i = 0; i < items.length; i++) {
|
|
727
679
|
if ('chi' in items[i]) {
|
|
728
680
|
// @ts-ignore
|
|
729
|
-
items[i].chi =
|
|
681
|
+
items[i].chi = parseSyntaxTokens(syntax, items[i].chi[Symbol.iterator]());
|
|
730
682
|
}
|
|
731
683
|
else if ('l' in items[i]) {
|
|
732
684
|
// @ts-ignore
|
|
733
|
-
items[i].l =
|
|
685
|
+
items[i].l = parseSyntaxTokens(syntax, items[i].l[Symbol.iterator]());
|
|
734
686
|
// @ts-ignore
|
|
735
|
-
items[i].r =
|
|
687
|
+
items[i].r = parseSyntaxTokens(syntax, items[i].r[Symbol.iterator]());
|
|
736
688
|
}
|
|
737
|
-
if (items[i].isOptional || items[i].isRepeatable) {
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
continue;
|
|
743
|
-
}
|
|
744
|
-
if (items[k].typ == ValidationTokenEnum.Comma) {
|
|
745
|
-
items.splice(i, k - i + 1, Object.defineProperty({
|
|
746
|
-
typ: ValidationTokenEnum.Bracket,
|
|
747
|
-
chi: items.slice(i, k + 1)
|
|
748
|
-
}, 'pos', { ...objectProperties, value: items[i].pos }));
|
|
749
|
-
}
|
|
750
|
-
break;
|
|
689
|
+
if (items[i].typ != ValidationTokenEnum.Bracket && (items[i].isOptional || items[i].isRepeatable)) {
|
|
690
|
+
let k = i;
|
|
691
|
+
while (--k > 0) {
|
|
692
|
+
if (items[k].typ == ValidationTokenEnum.Whitespace) {
|
|
693
|
+
continue;
|
|
751
694
|
}
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
695
|
+
if (items[k].typ == ValidationTokenEnum.Comma) {
|
|
696
|
+
const wrapper = Object.defineProperty({
|
|
697
|
+
typ: ValidationTokenEnum.Bracket,
|
|
698
|
+
chi: items.slice(k, i + 1)
|
|
699
|
+
}, 'pos', { ...objectProperties, value: items[k].pos });
|
|
700
|
+
if (items[i].isOptional) {
|
|
701
|
+
wrapper.isOptional = true;
|
|
702
|
+
delete items[i].isOptional;
|
|
758
703
|
}
|
|
759
|
-
if (items[
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
chi: items.slice(k, i + 1)
|
|
763
|
-
}, 'pos', { ...objectProperties, value: items[k].pos }));
|
|
764
|
-
i = k - 1;
|
|
704
|
+
if (items[i].isRepeatable) {
|
|
705
|
+
wrapper.isRepeatable = true;
|
|
706
|
+
delete items[i].isRepeatable;
|
|
765
707
|
}
|
|
766
|
-
|
|
708
|
+
items.splice(k, i - k + 1, wrapper);
|
|
709
|
+
i = k - 1;
|
|
767
710
|
}
|
|
711
|
+
break;
|
|
768
712
|
}
|
|
713
|
+
// }
|
|
769
714
|
}
|
|
770
715
|
}
|
|
771
716
|
return items;
|
|
@@ -779,18 +724,24 @@ function doParseSyntax(syntax, iterator, context) {
|
|
|
779
724
|
// @ts-ignore
|
|
780
725
|
context.chi = matchCurlBraces(syntax, context.chi[Symbol.iterator]());
|
|
781
726
|
// @ts-ignore
|
|
782
|
-
context.chi = matchToken(syntax, context.chi
|
|
727
|
+
context.chi = matchToken(syntax, context.chi, ValidationTokenEnum.Pipe);
|
|
783
728
|
// @ts-ignore
|
|
784
|
-
context.chi = matchToken(syntax, context.chi
|
|
729
|
+
context.chi = matchToken(syntax, context.chi, ValidationTokenEnum.Column);
|
|
785
730
|
// @ts-ignore
|
|
786
|
-
context.chi =
|
|
731
|
+
context.chi = matchToken(syntax, context.chi, ValidationTokenEnum.Ampersand);
|
|
787
732
|
// @ts-ignore
|
|
788
|
-
context.chi =
|
|
733
|
+
context.chi = parseSyntaxTokens(syntax, context.chi[Symbol.iterator]());
|
|
789
734
|
return context;
|
|
790
735
|
}
|
|
791
736
|
function getTokenType(token, position, currentPosition) {
|
|
792
737
|
const pos = { ...position };
|
|
793
738
|
Object.assign(position, currentPosition);
|
|
739
|
+
// '∞'
|
|
740
|
+
if (token == '\u221e') {
|
|
741
|
+
return Object.defineProperty({
|
|
742
|
+
typ: ValidationTokenEnum.InfinityToken,
|
|
743
|
+
}, 'pos', { ...objectProperties, value: pos });
|
|
744
|
+
}
|
|
794
745
|
if (token.charAt(0) == '"' || token.charAt(0) == "'") {
|
|
795
746
|
return Object.defineProperty({
|
|
796
747
|
typ: ValidationTokenEnum.StringToken,
|
|
@@ -908,7 +859,7 @@ function getTokenType(token, position, currentPosition) {
|
|
|
908
859
|
if (token.startsWith('<')) {
|
|
909
860
|
// <number [1,1000]>
|
|
910
861
|
// <length [0,∞]>
|
|
911
|
-
let match = token.match(/<([a-z0-9-]+)(\s+\[([0-9]+),(([0-9]+)|∞)\])?>/);
|
|
862
|
+
let match = token.match(/<([a-z0-9-]+)(\s+\[([0-9]+[a-zA-Z]*),(([0-9]+[a-zA-Z]*)|∞)\])?>/);
|
|
912
863
|
if (match == null) {
|
|
913
864
|
let match = token.match(/<([a-zA-Z0-9-]+)\(\)>/);
|
|
914
865
|
if (match != null) {
|
|
@@ -920,10 +871,12 @@ function getTokenType(token, position, currentPosition) {
|
|
|
920
871
|
throw new Error('invalid token at position: ' + position.lin + ':' + position.col + ' ' + token);
|
|
921
872
|
}
|
|
922
873
|
if (match[2] != null) {
|
|
874
|
+
const type = getTokenType$1(match[3]);
|
|
923
875
|
return Object.defineProperty({
|
|
924
876
|
typ: ValidationTokenEnum.PropertyType,
|
|
925
877
|
val: match[1],
|
|
926
|
-
|
|
878
|
+
unit: EnumToken[type.typ],
|
|
879
|
+
range: [+type.val, match[4] == '\u221e' ? null : +match[4]]
|
|
927
880
|
}, 'pos', { ...objectProperties, value: pos });
|
|
928
881
|
}
|
|
929
882
|
return Object.defineProperty({
|
|
@@ -949,6 +902,104 @@ function move(position, chr) {
|
|
|
949
902
|
}
|
|
950
903
|
return position;
|
|
951
904
|
}
|
|
905
|
+
function renderSyntax(token, parent) {
|
|
906
|
+
let glue;
|
|
907
|
+
switch (token.typ) {
|
|
908
|
+
case ValidationTokenEnum.InfinityToken:
|
|
909
|
+
// '∞'
|
|
910
|
+
return '\u221e';
|
|
911
|
+
case ValidationTokenEnum.Root:
|
|
912
|
+
return token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '');
|
|
913
|
+
case ValidationTokenEnum.Whitespace:
|
|
914
|
+
return ' ';
|
|
915
|
+
case ValidationTokenEnum.ValidationFunctionDefinition:
|
|
916
|
+
return '<' + token.val + '()>';
|
|
917
|
+
case ValidationTokenEnum.HashMark:
|
|
918
|
+
return '#';
|
|
919
|
+
case ValidationTokenEnum.Pipe:
|
|
920
|
+
return '|';
|
|
921
|
+
case ValidationTokenEnum.Column:
|
|
922
|
+
return '||';
|
|
923
|
+
case ValidationTokenEnum.PipeToken:
|
|
924
|
+
return token.chi.reduce((acc, curr) => acc + (acc.trim().length > 0 ? '|' : '') + curr.reduce((acc, curr) => acc + renderSyntax(curr), ''), '');
|
|
925
|
+
case ValidationTokenEnum.ColumnToken:
|
|
926
|
+
case ValidationTokenEnum.AmpersandToken:
|
|
927
|
+
glue = token.typ == ValidationTokenEnum.ColumnToken ? '||' : '&&';
|
|
928
|
+
return token.l.reduce((acc, curr) => acc + renderSyntax(curr), '') +
|
|
929
|
+
glue +
|
|
930
|
+
token.r.reduce((acc, curr) => acc + renderSyntax(curr), '');
|
|
931
|
+
case ValidationTokenEnum.Function:
|
|
932
|
+
case ValidationTokenEnum.PseudoClassFunctionToken:
|
|
933
|
+
case ValidationTokenEnum.Parens:
|
|
934
|
+
return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '') + ')' + renderAttributes(token);
|
|
935
|
+
case ValidationTokenEnum.Comma:
|
|
936
|
+
return ',';
|
|
937
|
+
case ValidationTokenEnum.Keyword:
|
|
938
|
+
return token.val + renderAttributes(token);
|
|
939
|
+
case ValidationTokenEnum.OpenBracket:
|
|
940
|
+
return '[';
|
|
941
|
+
case ValidationTokenEnum.Ampersand:
|
|
942
|
+
return '&&';
|
|
943
|
+
case ValidationTokenEnum.QuestionMark:
|
|
944
|
+
return '?';
|
|
945
|
+
case ValidationTokenEnum.Separator:
|
|
946
|
+
return '/';
|
|
947
|
+
case ValidationTokenEnum.Bracket:
|
|
948
|
+
return '[' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '') + ']' + renderAttributes(token);
|
|
949
|
+
case ValidationTokenEnum.PropertyType:
|
|
950
|
+
return '<' + token.val + (Array.isArray(token.range) ? `[${token?.range?.[0]}, ${token?.range?.[1] ?? '\u221e'}]` : '') + '>' + renderAttributes(token);
|
|
951
|
+
case ValidationTokenEnum.DeclarationType:
|
|
952
|
+
return "<'" + token.val + "'>" + renderAttributes(token);
|
|
953
|
+
case ValidationTokenEnum.Number:
|
|
954
|
+
case ValidationTokenEnum.PseudoClassToken:
|
|
955
|
+
case ValidationTokenEnum.StringToken:
|
|
956
|
+
return token.val + '';
|
|
957
|
+
case ValidationTokenEnum.SemiColon:
|
|
958
|
+
return ';';
|
|
959
|
+
case ValidationTokenEnum.AtRule:
|
|
960
|
+
return '@' + token.val;
|
|
961
|
+
case ValidationTokenEnum.AtRuleDefinition:
|
|
962
|
+
return '@' + token.val +
|
|
963
|
+
(token.prelude == null ? '' : ' ' + token.prelude.reduce((acc, curr) => acc + renderSyntax(curr), '')) +
|
|
964
|
+
(token.chi == null ? '' : ' {\n' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '')).slice(1, -1) + '\n}';
|
|
965
|
+
case ValidationTokenEnum.Block:
|
|
966
|
+
return '{' + token.chi.reduce((acc, t) => acc + renderSyntax(t), '') + '}';
|
|
967
|
+
case ValidationTokenEnum.DeclarationDefinitionToken:
|
|
968
|
+
return token.nam + ': ' + renderSyntax(token.val);
|
|
969
|
+
// case ValidationTokenEnum.ColumnArrayToken:
|
|
970
|
+
//
|
|
971
|
+
// return (token as ValidationColumnArrayToken).chi.reduce((acc: string, curr: ValidationToken) => acc + (acc.trim().length > 0 ? '||' : '') + renderSyntax(curr), '');
|
|
972
|
+
default:
|
|
973
|
+
throw new Error('Unhandled token: ' + JSON.stringify({ token }, null, 1));
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
function renderAttributes(token) {
|
|
977
|
+
let result = '';
|
|
978
|
+
if (token.isList) {
|
|
979
|
+
result += '#';
|
|
980
|
+
}
|
|
981
|
+
if (token.isOptional) {
|
|
982
|
+
result += '?';
|
|
983
|
+
}
|
|
984
|
+
if (token.isRepeatableGroup) {
|
|
985
|
+
result += '!';
|
|
986
|
+
}
|
|
987
|
+
if (token.isRepeatable) {
|
|
988
|
+
result += '*';
|
|
989
|
+
}
|
|
990
|
+
if (token.atLeastOnce) {
|
|
991
|
+
result += '+';
|
|
992
|
+
}
|
|
993
|
+
if (token.occurence != null) {
|
|
994
|
+
if (token.occurence.max == null || token.occurence.max == token.occurence.min || Number.isNaN(token.occurence.max)) {
|
|
995
|
+
result += '{' + token.occurence.min + '}';
|
|
996
|
+
}
|
|
997
|
+
else {
|
|
998
|
+
result += '{' + token.occurence.min + ',' + (Number.isFinite(token.occurence.max) ? token.occurence.max : '\u221e') + '}';
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
return result;
|
|
1002
|
+
}
|
|
952
1003
|
function minify(ast) {
|
|
953
1004
|
if (Array.isArray(ast)) {
|
|
954
1005
|
// @ts-ignore
|
|
@@ -960,20 +1011,7 @@ function minify(ast) {
|
|
|
960
1011
|
ast.pop();
|
|
961
1012
|
}
|
|
962
1013
|
for (let i = 0; i < ast.length; i++) {
|
|
963
|
-
// if ([ValidationTokenEnum.ColumnToken, ValidationTokenEnum.PipeToken, ValidationTokenEnum.AmpersandToken].includes(ast[i].typ)) {
|
|
964
|
-
// for (const j of (ast[i] as ValidationPipeToken | ValidationColumnToken | ValidationAmpersandToken).l) {
|
|
965
1014
|
minify(ast[i]);
|
|
966
|
-
// }
|
|
967
|
-
// for (const j of (ast[i] as ValidationPipeToken | ValidationColumnToken | ValidationAmpersandToken).r) {
|
|
968
|
-
//
|
|
969
|
-
// minify(j);
|
|
970
|
-
// }
|
|
971
|
-
// } else
|
|
972
|
-
// if ('chi' in ast[i]) {
|
|
973
|
-
//
|
|
974
|
-
// minify((ast[i] as ValidationFunctionToken | ValidationBracketToken).chi as ValidationToken[]);
|
|
975
|
-
//
|
|
976
|
-
// } else
|
|
977
1015
|
if (ast[i].typ == ValidationTokenEnum.Whitespace && ast[i + 1]?.typ == ValidationTokenEnum.Whitespace) {
|
|
978
1016
|
ast.splice(i + 1, 1);
|
|
979
1017
|
i--;
|
|
@@ -990,14 +1028,9 @@ function minify(ast) {
|
|
|
990
1028
|
}
|
|
991
1029
|
return ast;
|
|
992
1030
|
}
|
|
993
|
-
// else {
|
|
994
|
-
// if ([ValidationTokenEnum.ColumnToken, ValidationTokenEnum.PipeToken, ValidationTokenEnum.AmpersandToken].includes(ast.typ)) {
|
|
995
|
-
// for (const j of (ast as ValidationPipeToken | ValidationColumnToken | ValidationAmpersandToken).l) {
|
|
996
1031
|
if ('l' in ast) {
|
|
997
1032
|
minify(ast.l);
|
|
998
1033
|
}
|
|
999
|
-
// }
|
|
1000
|
-
// for (const j of (ast as ValidationPipeToken | ValidationColumnToken | ValidationAmpersandToken).r) {
|
|
1001
1034
|
if ('r' in ast) {
|
|
1002
1035
|
minify(ast.r);
|
|
1003
1036
|
}
|
|
@@ -1009,44 +1042,5 @@ function minify(ast) {
|
|
|
1009
1042
|
}
|
|
1010
1043
|
return ast;
|
|
1011
1044
|
}
|
|
1012
|
-
function* walkValidationToken(token, parent, callback, key) {
|
|
1013
|
-
if (Array.isArray(token)) {
|
|
1014
|
-
for (const child of token) {
|
|
1015
|
-
yield* walkValidationToken(child, parent, callback, WalkValidationTokenKeyTypeEnum.Array);
|
|
1016
|
-
}
|
|
1017
|
-
return;
|
|
1018
|
-
}
|
|
1019
|
-
let action = null;
|
|
1020
|
-
if (callback != null) {
|
|
1021
|
-
// @ts-ignore
|
|
1022
|
-
action = callback(token, parent, key);
|
|
1023
|
-
}
|
|
1024
|
-
if (action != WalkValidationTokenEnum.IgnoreNode && action != WalkValidationTokenEnum.IgnoreAll) {
|
|
1025
|
-
yield { token, parent };
|
|
1026
|
-
}
|
|
1027
|
-
if (action != WalkValidationTokenEnum.IgnoreChildren && action != WalkValidationTokenEnum.IgnoreAll && 'prelude' in token) {
|
|
1028
|
-
for (const child of token.prelude) {
|
|
1029
|
-
yield* walkValidationToken(child, token, callback, WalkValidationTokenKeyTypeEnum.Prelude);
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
if (action != WalkValidationTokenEnum.IgnoreChildren && 'chi' in token) {
|
|
1033
|
-
// @ts-ignore
|
|
1034
|
-
for (const child of token.chi) {
|
|
1035
|
-
yield* walkValidationToken(child, token, callback, WalkValidationTokenKeyTypeEnum.Children);
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
if (action != WalkValidationTokenEnum.IgnoreChildren && action != WalkValidationTokenEnum.IgnoreAll && 'l' in token) {
|
|
1039
|
-
// @ts-ignore
|
|
1040
|
-
for (const child of token.l) {
|
|
1041
|
-
yield* walkValidationToken(child, token, callback, WalkValidationTokenKeyTypeEnum.Left);
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
if (action != WalkValidationTokenEnum.IgnoreChildren && action != WalkValidationTokenEnum.IgnoreAll && 'r' in token) {
|
|
1045
|
-
// @ts-ignore
|
|
1046
|
-
for (const child of token.r) {
|
|
1047
|
-
yield* walkValidationToken(child, token, callback, WalkValidationTokenKeyTypeEnum.Right);
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
1045
|
|
|
1052
|
-
export {
|
|
1046
|
+
export { parseSyntax, renderSyntax };
|
|
@@ -40,7 +40,7 @@ var ValidationTokenEnum;
|
|
|
40
40
|
ValidationTokenEnum[ValidationTokenEnum["DeclarationDefinitionToken"] = 37] = "DeclarationDefinitionToken";
|
|
41
41
|
ValidationTokenEnum[ValidationTokenEnum["SemiColon"] = 38] = "SemiColon";
|
|
42
42
|
ValidationTokenEnum[ValidationTokenEnum["Character"] = 39] = "Character";
|
|
43
|
-
ValidationTokenEnum[ValidationTokenEnum["
|
|
43
|
+
ValidationTokenEnum[ValidationTokenEnum["InfinityToken"] = 40] = "InfinityToken";
|
|
44
44
|
})(ValidationTokenEnum || (ValidationTokenEnum = {}));
|
|
45
45
|
var ValidationSyntaxGroupEnum;
|
|
46
46
|
(function (ValidationSyntaxGroupEnum) {
|
|
@@ -2,14 +2,14 @@ import { EnumToken } from '../ast/types.js';
|
|
|
2
2
|
import '../ast/minify.js';
|
|
3
3
|
import '../ast/walk.js';
|
|
4
4
|
import '../parser/parse.js';
|
|
5
|
+
import '../parser/tokenize.js';
|
|
6
|
+
import '../parser/utils/config.js';
|
|
5
7
|
import '../renderer/color/utils/constants.js';
|
|
6
8
|
import '../renderer/sourcemap/lib/encode.js';
|
|
7
|
-
import '../parser/utils/config.js';
|
|
8
9
|
import { validateRelativeSelectorList } from './syntaxes/relative-selector-list.js';
|
|
9
10
|
import './syntaxes/complex-selector.js';
|
|
10
11
|
import { validateKeyframeBlockList } from './syntaxes/keyframe-block-list.js';
|
|
11
|
-
import './
|
|
12
|
-
import './parser/parse.js';
|
|
12
|
+
import './syntax.js';
|
|
13
13
|
import './config.js';
|
|
14
14
|
import { validateSelectorList } from './syntaxes/selector-list.js';
|
|
15
15
|
|