@tbela99/css-parser 0.9.1 → 1.1.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 +265 -0
- package/LICENSE.md +1 -1
- package/README.md +29 -17
- package/dist/index-umd-web.js +7461 -4360
- package/dist/index.cjs +8608 -5507
- package/dist/index.d.ts +203 -61
- package/dist/lib/ast/expand.js +2 -1
- package/dist/lib/ast/features/calc.js +19 -11
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.js +47 -29
- package/dist/lib/ast/features/prefix.js +117 -91
- package/dist/lib/ast/features/shorthand.js +34 -14
- package/dist/lib/ast/features/transform.js +67 -0
- package/dist/lib/ast/features/type.js +7 -0
- package/dist/lib/ast/math/expression.js +20 -10
- package/dist/lib/ast/math/math.js +20 -2
- package/dist/lib/ast/minify.js +209 -80
- package/dist/lib/ast/transform/compute.js +337 -0
- package/dist/lib/ast/transform/convert.js +33 -0
- package/dist/lib/ast/transform/matrix.js +112 -0
- package/dist/lib/ast/transform/minify.js +296 -0
- package/dist/lib/ast/transform/perspective.js +10 -0
- package/dist/lib/ast/transform/rotate.js +40 -0
- package/dist/lib/ast/transform/scale.js +32 -0
- package/dist/lib/ast/transform/skew.js +23 -0
- package/dist/lib/ast/transform/translate.js +32 -0
- package/dist/lib/ast/transform/utils.js +198 -0
- package/dist/lib/ast/types.js +18 -15
- package/dist/lib/ast/walk.js +54 -22
- 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 +449 -340
- package/dist/lib/parser/tokenize.js +147 -72
- package/dist/lib/parser/utils/declaration.js +5 -4
- package/dist/lib/parser/utils/type.js +2 -1
- package/dist/lib/renderer/color/a98rgb.js +2 -1
- package/dist/lib/renderer/color/{colormix.js → color-mix.js} +16 -7
- package/dist/lib/renderer/color/color.js +264 -170
- package/dist/lib/renderer/color/hex.js +19 -8
- package/dist/lib/renderer/color/hsl.js +9 -3
- package/dist/lib/renderer/color/hwb.js +2 -1
- package/dist/lib/renderer/color/lab.js +10 -1
- package/dist/lib/renderer/color/lch.js +10 -1
- package/dist/lib/renderer/color/oklab.js +10 -1
- package/dist/lib/renderer/color/oklch.js +10 -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 +27 -32
- package/dist/lib/renderer/color/rgb.js +14 -10
- package/dist/lib/renderer/color/srgb.js +48 -23
- package/dist/lib/renderer/color/utils/components.js +18 -6
- 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 +108 -43
- package/dist/lib/syntax/syntax.js +267 -136
- package/dist/lib/validation/at-rules/container.js +81 -103
- 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 +19 -23
- package/dist/lib/validation/at-rules/layer.js +5 -5
- package/dist/lib/validation/at-rules/media.js +42 -53
- 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 -18
- package/dist/lib/validation/config.js +24 -1
- package/dist/lib/validation/config.json.js +563 -63
- package/dist/lib/validation/parser/parse.js +196 -185
- package/dist/lib/validation/parser/types.js +1 -1
- package/dist/lib/validation/selector.js +8 -5
- package/dist/lib/validation/syntax.js +724 -1405
- 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 +6 -5
- package/dist/lib/validation/syntaxes/keyframe-selector.js +23 -105
- 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 +17 -15
- package/dist/lib/validation/syntaxes/url.js +18 -22
- package/dist/lib/validation/utils/list.js +20 -2
- package/dist/lib/validation/utils/whitespace.js +2 -1
- package/dist/node/index.js +4 -2
- package/dist/node/load.js +6 -1
- package/dist/web/index.js +4 -2
- package/dist/web/load.js +5 -0
- package/package.json +16 -15
- package/dist/lib/renderer/color/prophotoRgb.js +0 -56
- package/dist/lib/validation/declaration.js +0 -94
- package/dist/lib/validation/syntaxes/image.js +0 -29
|
@@ -51,6 +51,13 @@ function* tokenize(syntax, position = { ind: 0, lin: 1, col: 0 }, currentPositio
|
|
|
51
51
|
let chr = syntax.charAt(i);
|
|
52
52
|
move(currentPosition, chr);
|
|
53
53
|
switch (chr) {
|
|
54
|
+
case '∞':
|
|
55
|
+
if (buffer.length > 0) {
|
|
56
|
+
yield getTokenType(buffer, position, currentPosition);
|
|
57
|
+
}
|
|
58
|
+
yield getTokenType(chr, position, currentPosition);
|
|
59
|
+
buffer = '';
|
|
60
|
+
break;
|
|
54
61
|
case '\\':
|
|
55
62
|
if (buffer.length > 0) {
|
|
56
63
|
yield getTokenType(buffer, position, currentPosition);
|
|
@@ -181,66 +188,13 @@ function* tokenize(syntax, position = { ind: 0, lin: 1, col: 0 }, currentPositio
|
|
|
181
188
|
yield getTokenType(buffer, position, currentPosition);
|
|
182
189
|
}
|
|
183
190
|
}
|
|
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
191
|
function parseSyntax(syntax) {
|
|
238
192
|
const root = Object.defineProperty({
|
|
239
193
|
typ: ValidationTokenEnum.Root,
|
|
240
194
|
chi: []
|
|
241
195
|
}, 'pos', { ...objectProperties, value: { ind: 0, lin: 1, col: 0 } });
|
|
242
196
|
// return minify(doParseSyntax(syntaxes, tokenize(syntaxes), root)) as ValidationRootToken;
|
|
243
|
-
return minify(
|
|
197
|
+
return minify(doParseSyntax(syntax, tokenize(syntax), root));
|
|
244
198
|
}
|
|
245
199
|
function matchParens(syntax, iterator) {
|
|
246
200
|
let item;
|
|
@@ -248,6 +202,7 @@ function matchParens(syntax, iterator) {
|
|
|
248
202
|
let items = [];
|
|
249
203
|
let match = 0;
|
|
250
204
|
while ((item = iterator.next()) && !item.done) {
|
|
205
|
+
// console.error(JSON.stringify({match, val: item.value,func}, null, 1));
|
|
251
206
|
switch (item.value.typ) {
|
|
252
207
|
case ValidationTokenEnum.OpenParenthesis:
|
|
253
208
|
if (match > 0) {
|
|
@@ -255,7 +210,7 @@ function matchParens(syntax, iterator) {
|
|
|
255
210
|
}
|
|
256
211
|
else if (match == 0) {
|
|
257
212
|
func = items.at(-1);
|
|
258
|
-
if (func == null) {
|
|
213
|
+
if (func == null || func.val == null || func.val === '') {
|
|
259
214
|
func = Object.defineProperty({
|
|
260
215
|
typ: ValidationTokenEnum.Parens,
|
|
261
216
|
val: '',
|
|
@@ -372,6 +327,10 @@ function matchCurlBraces(syntax, iterator) {
|
|
|
372
327
|
else {
|
|
373
328
|
items.push(item.value);
|
|
374
329
|
}
|
|
330
|
+
if ('chi' in item.value) {
|
|
331
|
+
// @ts-ignore
|
|
332
|
+
item.value.chi = matchCurlBraces(syntax, item.value.chi[Symbol.iterator]());
|
|
333
|
+
}
|
|
375
334
|
break;
|
|
376
335
|
}
|
|
377
336
|
}
|
|
@@ -382,7 +341,7 @@ function matchCurlBraces(syntax, iterator) {
|
|
|
382
341
|
if (i > 0 && it.typ == ValidationTokenEnum.Block && it.chi[0]?.typ == ValidationTokenEnum.Number) {
|
|
383
342
|
items[i - 1].occurence = {
|
|
384
343
|
min: +it.chi[0].val,
|
|
385
|
-
max: +(it.chi[2] ?? it.chi[0]).val
|
|
344
|
+
max: it.chi[2]?.typ == ValidationTokenEnum.InfinityToken ? Number.POSITIVE_INFINITY : +(it.chi[2] ?? it.chi[0]).val
|
|
386
345
|
};
|
|
387
346
|
items.splice(i--, 1);
|
|
388
347
|
continue;
|
|
@@ -506,8 +465,17 @@ function matchAtRule(syntax, iterator) {
|
|
|
506
465
|
return children;
|
|
507
466
|
}
|
|
508
467
|
function matchToken(syntax, iterator, validationToken) {
|
|
468
|
+
if (iterator.length == 0) {
|
|
469
|
+
return [];
|
|
470
|
+
}
|
|
471
|
+
if (Array.isArray(iterator[0])) {
|
|
472
|
+
const result = [];
|
|
473
|
+
for (let i = 0; i < iterator.length; i++) {
|
|
474
|
+
result.push(matchToken(syntax, iterator[i], validationToken));
|
|
475
|
+
}
|
|
476
|
+
return result;
|
|
477
|
+
}
|
|
509
478
|
let children = [];
|
|
510
|
-
let item;
|
|
511
479
|
let token = null;
|
|
512
480
|
let i;
|
|
513
481
|
if (validationToken == ValidationTokenEnum.Ampersand) {
|
|
@@ -538,42 +506,44 @@ function matchToken(syntax, iterator, validationToken) {
|
|
|
538
506
|
}
|
|
539
507
|
else if ('chi' in children[i]) {
|
|
540
508
|
// @ts-ignore
|
|
541
|
-
children[i].chi = matchToken(syntax, children[i].chi
|
|
509
|
+
children[i].chi = matchToken(syntax, children[i].chi, validationToken);
|
|
542
510
|
}
|
|
543
511
|
else if ('l' in children[i]) {
|
|
544
512
|
// @ts-ignore
|
|
545
|
-
children[i].l = matchToken(syntax, children[i].l
|
|
513
|
+
children[i].l = matchToken(syntax, children[i].l, validationToken);
|
|
546
514
|
// @ts-ignore
|
|
547
|
-
children[i].r = matchToken(syntax, children[i].r
|
|
515
|
+
children[i].r = matchToken(syntax, children[i].r, validationToken);
|
|
548
516
|
}
|
|
549
517
|
}
|
|
550
518
|
return children;
|
|
551
519
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
520
|
+
let item;
|
|
521
|
+
for (i = 0; i < iterator.length; i++) {
|
|
522
|
+
item = iterator[i];
|
|
523
|
+
if (item.typ == validationToken) {
|
|
524
|
+
if (item.typ == ValidationTokenEnum.Pipe) {
|
|
555
525
|
token = Object.defineProperty({
|
|
556
526
|
typ: ValidationTokenEnum.PipeToken,
|
|
557
|
-
chi: [matchToken(syntax, children.slice()
|
|
527
|
+
chi: [matchToken(syntax, children.slice(), validationToken)], //.concat(matchToken(syntaxes, children.slice()[Symbol.iterator]() as Iterator<ValidationTokenIteratorValue>, validationToken), matchToken(syntaxes, iterator, validationToken)),
|
|
558
528
|
// @ts-ignore
|
|
559
529
|
// l: matchToken(syntaxes, children.slice()[Symbol.iterator](), validationToken),
|
|
560
530
|
// r: matchToken(syntaxes, iterator, validationToken)
|
|
561
531
|
}, 'pos', {
|
|
562
532
|
...objectProperties,
|
|
563
|
-
value: item.
|
|
533
|
+
value: item.pos
|
|
564
534
|
});
|
|
565
535
|
children.length = 0;
|
|
566
|
-
while ((item = iterator
|
|
567
|
-
if (item.
|
|
568
|
-
token.chi.push(matchToken(syntax, children.slice()
|
|
536
|
+
while ((item = iterator[++i]) != null) {
|
|
537
|
+
if (item.typ == ValidationTokenEnum.Pipe) {
|
|
538
|
+
token.chi.push(matchToken(syntax, children.slice(), validationToken));
|
|
569
539
|
children.length = 0;
|
|
570
540
|
}
|
|
571
541
|
else {
|
|
572
|
-
children.push(item
|
|
542
|
+
children.push(item);
|
|
573
543
|
}
|
|
574
544
|
}
|
|
575
545
|
if (children.length > 0) {
|
|
576
|
-
token.chi.push(matchToken(syntax, children.slice()
|
|
546
|
+
token.chi.push(matchToken(syntax, children.slice(), validationToken));
|
|
577
547
|
}
|
|
578
548
|
token.chi.sort((a, b) => {
|
|
579
549
|
if (a.some((t) => t.isList)) {
|
|
@@ -613,55 +583,56 @@ function matchToken(syntax, iterator, validationToken) {
|
|
|
613
583
|
else {
|
|
614
584
|
token = Object.defineProperty({
|
|
615
585
|
typ: ValidationTokenEnum.ColumnToken,
|
|
616
|
-
l: matchToken(syntax, children.slice()
|
|
617
|
-
r: matchToken(syntax, iterator, validationToken)
|
|
586
|
+
l: matchToken(syntax, children.slice(), validationToken),
|
|
587
|
+
r: matchToken(syntax, iterator.slice(i + 1), validationToken)
|
|
618
588
|
}, 'pos', {
|
|
619
589
|
...objectProperties,
|
|
620
|
-
value: item.
|
|
590
|
+
value: item.pos
|
|
621
591
|
});
|
|
592
|
+
i = iterator.length;
|
|
622
593
|
}
|
|
623
594
|
children.length = 0;
|
|
624
595
|
children.push(token);
|
|
625
|
-
while ((item = iterator
|
|
626
|
-
children.push(item
|
|
627
|
-
if ('chi' in item
|
|
596
|
+
while ((item = iterator[++i]) != null) {
|
|
597
|
+
children.push(item);
|
|
598
|
+
if ('chi' in item) {
|
|
628
599
|
// @ts-ignore
|
|
629
|
-
item.
|
|
600
|
+
item.chi = matchToken(syntax, item.chi, validationToken);
|
|
630
601
|
}
|
|
631
|
-
else if ('l' in item
|
|
602
|
+
else if ('l' in item) {
|
|
632
603
|
// @ts-ignore
|
|
633
|
-
item.
|
|
604
|
+
item.l = matchToken(syntax, item.l, validationToken);
|
|
634
605
|
// @ts-ignore
|
|
635
|
-
item.
|
|
606
|
+
item.r = matchToken(syntax, item.r, validationToken);
|
|
636
607
|
}
|
|
637
608
|
}
|
|
638
609
|
// @ts-ignore
|
|
639
610
|
return children;
|
|
640
611
|
}
|
|
641
612
|
else {
|
|
642
|
-
|
|
643
|
-
|
|
613
|
+
// @ts-ignore
|
|
614
|
+
children.push(item);
|
|
615
|
+
if ('chi' in item) {
|
|
644
616
|
// @ts-ignore
|
|
645
|
-
item.
|
|
617
|
+
item.chi = matchToken(syntax, item.chi, validationToken);
|
|
646
618
|
}
|
|
647
|
-
else if ('l' in item
|
|
619
|
+
else if ('l' in item) {
|
|
620
|
+
item.l = matchToken(syntax, item.l, validationToken);
|
|
648
621
|
// @ts-ignore
|
|
649
|
-
item.
|
|
650
|
-
// @ts-ignore
|
|
651
|
-
item.value.r = matchToken(syntax, item.value.r[Symbol.iterator](), validationToken);
|
|
622
|
+
item.r = matchToken(syntax, item.r, validationToken);
|
|
652
623
|
}
|
|
653
624
|
}
|
|
654
625
|
}
|
|
655
626
|
return children;
|
|
656
627
|
}
|
|
657
|
-
function
|
|
628
|
+
function parseSyntaxTokens(syntax, iterator) {
|
|
658
629
|
const items = [];
|
|
659
630
|
let item;
|
|
660
631
|
let i;
|
|
661
632
|
while ((item = iterator.next()) && !item.done) {
|
|
662
633
|
if (Array.isArray(item.value)) {
|
|
663
634
|
// @ts-ignore
|
|
664
|
-
item.value =
|
|
635
|
+
item.value = parseSyntaxTokens(syntax, item.value[Symbol.iterator]());
|
|
665
636
|
}
|
|
666
637
|
switch (item.value.typ) {
|
|
667
638
|
case ValidationTokenEnum.Star:
|
|
@@ -694,14 +665,14 @@ function parseTokens(syntax, iterator) {
|
|
|
694
665
|
else if (item.value.typ == ValidationTokenEnum.OpenCurlyBrace) {
|
|
695
666
|
items[i].occurence = {
|
|
696
667
|
min: 0,
|
|
697
|
-
max:
|
|
668
|
+
max: null
|
|
698
669
|
};
|
|
699
670
|
while ((item = iterator.next()) && !item.done) {
|
|
700
671
|
if (item.value.typ == ValidationTokenEnum.Number) {
|
|
701
672
|
// @ts-ignore
|
|
702
673
|
if (items[i].occurence.min == 0) {
|
|
703
674
|
// @ts-ignore
|
|
704
|
-
items[i].occurence.min = +item.value.val;
|
|
675
|
+
items[i].occurence.min = items[i].occurence.max = +item.value.val;
|
|
705
676
|
}
|
|
706
677
|
else {
|
|
707
678
|
// @ts-ignore
|
|
@@ -715,7 +686,7 @@ function parseTokens(syntax, iterator) {
|
|
|
715
686
|
}
|
|
716
687
|
break;
|
|
717
688
|
case ValidationTokenEnum.Pipe:
|
|
718
|
-
item.value.chi = item.value.chi.map((t) =>
|
|
689
|
+
item.value.chi = item.value.chi.map((t) => parseSyntaxTokens(syntax, t[Symbol.iterator]()));
|
|
719
690
|
items.push(item.value);
|
|
720
691
|
break;
|
|
721
692
|
default:
|
|
@@ -726,46 +697,39 @@ function parseTokens(syntax, iterator) {
|
|
|
726
697
|
for (i = 0; i < items.length; i++) {
|
|
727
698
|
if ('chi' in items[i]) {
|
|
728
699
|
// @ts-ignore
|
|
729
|
-
items[i].chi =
|
|
700
|
+
items[i].chi = parseSyntaxTokens(syntax, items[i].chi[Symbol.iterator]());
|
|
730
701
|
}
|
|
731
702
|
else if ('l' in items[i]) {
|
|
732
703
|
// @ts-ignore
|
|
733
|
-
items[i].l =
|
|
704
|
+
items[i].l = parseSyntaxTokens(syntax, items[i].l[Symbol.iterator]());
|
|
734
705
|
// @ts-ignore
|
|
735
|
-
items[i].r =
|
|
706
|
+
items[i].r = parseSyntaxTokens(syntax, items[i].r[Symbol.iterator]());
|
|
736
707
|
}
|
|
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;
|
|
708
|
+
if (items[i].typ != ValidationTokenEnum.Bracket && (items[i].isOptional || items[i].isRepeatable)) {
|
|
709
|
+
let k = i;
|
|
710
|
+
while (--k > 0) {
|
|
711
|
+
if (items[k].typ == ValidationTokenEnum.Whitespace) {
|
|
712
|
+
continue;
|
|
751
713
|
}
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
714
|
+
if (items[k].typ == ValidationTokenEnum.Comma) {
|
|
715
|
+
const wrapper = Object.defineProperty({
|
|
716
|
+
typ: ValidationTokenEnum.Bracket,
|
|
717
|
+
chi: items.slice(k, i + 1)
|
|
718
|
+
}, 'pos', { ...objectProperties, value: items[k].pos });
|
|
719
|
+
if (items[i].isOptional) {
|
|
720
|
+
wrapper.isOptional = true;
|
|
721
|
+
delete items[i].isOptional;
|
|
758
722
|
}
|
|
759
|
-
if (items[
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
chi: items.slice(k, i + 1)
|
|
763
|
-
}, 'pos', { ...objectProperties, value: items[k].pos }));
|
|
764
|
-
i = k - 1;
|
|
723
|
+
if (items[i].isRepeatable) {
|
|
724
|
+
wrapper.isRepeatable = true;
|
|
725
|
+
delete items[i].isRepeatable;
|
|
765
726
|
}
|
|
766
|
-
|
|
727
|
+
items.splice(k, i - k + 1, wrapper);
|
|
728
|
+
i = k - 1;
|
|
767
729
|
}
|
|
730
|
+
break;
|
|
768
731
|
}
|
|
732
|
+
// }
|
|
769
733
|
}
|
|
770
734
|
}
|
|
771
735
|
return items;
|
|
@@ -779,18 +743,24 @@ function doParseSyntax(syntax, iterator, context) {
|
|
|
779
743
|
// @ts-ignore
|
|
780
744
|
context.chi = matchCurlBraces(syntax, context.chi[Symbol.iterator]());
|
|
781
745
|
// @ts-ignore
|
|
782
|
-
context.chi = matchToken(syntax, context.chi
|
|
746
|
+
context.chi = matchToken(syntax, context.chi, ValidationTokenEnum.Pipe);
|
|
783
747
|
// @ts-ignore
|
|
784
|
-
context.chi = matchToken(syntax, context.chi
|
|
748
|
+
context.chi = matchToken(syntax, context.chi, ValidationTokenEnum.Column);
|
|
785
749
|
// @ts-ignore
|
|
786
|
-
context.chi =
|
|
750
|
+
context.chi = matchToken(syntax, context.chi, ValidationTokenEnum.Ampersand);
|
|
787
751
|
// @ts-ignore
|
|
788
|
-
context.chi =
|
|
752
|
+
context.chi = parseSyntaxTokens(syntax, context.chi[Symbol.iterator]());
|
|
789
753
|
return context;
|
|
790
754
|
}
|
|
791
755
|
function getTokenType(token, position, currentPosition) {
|
|
792
756
|
const pos = { ...position };
|
|
793
757
|
Object.assign(position, currentPosition);
|
|
758
|
+
// '∞'
|
|
759
|
+
if (token == '\u221e') {
|
|
760
|
+
return Object.defineProperty({
|
|
761
|
+
typ: ValidationTokenEnum.InfinityToken,
|
|
762
|
+
}, 'pos', { ...objectProperties, value: pos });
|
|
763
|
+
}
|
|
794
764
|
if (token.charAt(0) == '"' || token.charAt(0) == "'") {
|
|
795
765
|
return Object.defineProperty({
|
|
796
766
|
typ: ValidationTokenEnum.StringToken,
|
|
@@ -923,7 +893,7 @@ function getTokenType(token, position, currentPosition) {
|
|
|
923
893
|
return Object.defineProperty({
|
|
924
894
|
typ: ValidationTokenEnum.PropertyType,
|
|
925
895
|
val: match[1],
|
|
926
|
-
range: [+match[3], match[4] == '
|
|
896
|
+
range: [+match[3], match[4] == '\u221e' ? Infinity : +match[4]]
|
|
927
897
|
}, 'pos', { ...objectProperties, value: pos });
|
|
928
898
|
}
|
|
929
899
|
return Object.defineProperty({
|
|
@@ -949,6 +919,104 @@ function move(position, chr) {
|
|
|
949
919
|
}
|
|
950
920
|
return position;
|
|
951
921
|
}
|
|
922
|
+
function renderSyntax(token, parent) {
|
|
923
|
+
let glue;
|
|
924
|
+
switch (token.typ) {
|
|
925
|
+
case ValidationTokenEnum.InfinityToken:
|
|
926
|
+
// '∞'
|
|
927
|
+
return '\u221e';
|
|
928
|
+
case ValidationTokenEnum.Root:
|
|
929
|
+
return token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '');
|
|
930
|
+
case ValidationTokenEnum.Whitespace:
|
|
931
|
+
return ' ';
|
|
932
|
+
case ValidationTokenEnum.ValidationFunctionDefinition:
|
|
933
|
+
return '<' + token.val + '()>';
|
|
934
|
+
case ValidationTokenEnum.HashMark:
|
|
935
|
+
return '#';
|
|
936
|
+
case ValidationTokenEnum.Pipe:
|
|
937
|
+
return '|';
|
|
938
|
+
case ValidationTokenEnum.Column:
|
|
939
|
+
return '||';
|
|
940
|
+
case ValidationTokenEnum.PipeToken:
|
|
941
|
+
return token.chi.reduce((acc, curr) => acc + (acc.trim().length > 0 ? '|' : '') + curr.reduce((acc, curr) => acc + renderSyntax(curr), ''), '');
|
|
942
|
+
case ValidationTokenEnum.ColumnToken:
|
|
943
|
+
case ValidationTokenEnum.AmpersandToken:
|
|
944
|
+
glue = token.typ == ValidationTokenEnum.ColumnToken ? '||' : '&&';
|
|
945
|
+
return token.l.reduce((acc, curr) => acc + renderSyntax(curr), '') +
|
|
946
|
+
glue +
|
|
947
|
+
token.r.reduce((acc, curr) => acc + renderSyntax(curr), '');
|
|
948
|
+
case ValidationTokenEnum.Function:
|
|
949
|
+
case ValidationTokenEnum.PseudoClassFunctionToken:
|
|
950
|
+
case ValidationTokenEnum.Parens:
|
|
951
|
+
return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '') + ')' + renderAttributes(token);
|
|
952
|
+
case ValidationTokenEnum.Comma:
|
|
953
|
+
return ',';
|
|
954
|
+
case ValidationTokenEnum.Keyword:
|
|
955
|
+
return token.val + renderAttributes(token);
|
|
956
|
+
case ValidationTokenEnum.OpenBracket:
|
|
957
|
+
return '[';
|
|
958
|
+
case ValidationTokenEnum.Ampersand:
|
|
959
|
+
return '&&';
|
|
960
|
+
case ValidationTokenEnum.QuestionMark:
|
|
961
|
+
return '?';
|
|
962
|
+
case ValidationTokenEnum.Separator:
|
|
963
|
+
return '/';
|
|
964
|
+
case ValidationTokenEnum.Bracket:
|
|
965
|
+
return '[' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '') + ']' + renderAttributes(token);
|
|
966
|
+
case ValidationTokenEnum.PropertyType:
|
|
967
|
+
return '<' + token.val + '>' + renderAttributes(token);
|
|
968
|
+
case ValidationTokenEnum.DeclarationType:
|
|
969
|
+
return "<'" + token.val + "'>" + renderAttributes(token);
|
|
970
|
+
case ValidationTokenEnum.Number:
|
|
971
|
+
case ValidationTokenEnum.PseudoClassToken:
|
|
972
|
+
case ValidationTokenEnum.StringToken:
|
|
973
|
+
return token.val + '';
|
|
974
|
+
case ValidationTokenEnum.SemiColon:
|
|
975
|
+
return ';';
|
|
976
|
+
case ValidationTokenEnum.AtRule:
|
|
977
|
+
return '@' + token.val;
|
|
978
|
+
case ValidationTokenEnum.AtRuleDefinition:
|
|
979
|
+
return '@' + token.val +
|
|
980
|
+
(token.prelude == null ? '' : ' ' + token.prelude.reduce((acc, curr) => acc + renderSyntax(curr), '')) +
|
|
981
|
+
(token.chi == null ? '' : ' {\n' + token.chi.reduce((acc, curr) => acc + renderSyntax(curr), '')).slice(1, -1) + '\n}';
|
|
982
|
+
case ValidationTokenEnum.Block:
|
|
983
|
+
return '{' + token.chi.reduce((acc, t) => acc + renderSyntax(t), '') + '}';
|
|
984
|
+
case ValidationTokenEnum.DeclarationDefinitionToken:
|
|
985
|
+
return token.nam + ': ' + renderSyntax(token.val);
|
|
986
|
+
// case ValidationTokenEnum.ColumnArrayToken:
|
|
987
|
+
//
|
|
988
|
+
// return (token as ValidationColumnArrayToken).chi.reduce((acc: string, curr: ValidationToken) => acc + (acc.trim().length > 0 ? '||' : '') + renderSyntax(curr), '');
|
|
989
|
+
default:
|
|
990
|
+
throw new Error('Unhandled token: ' + JSON.stringify({ token }, null, 1));
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
function renderAttributes(token) {
|
|
994
|
+
let result = '';
|
|
995
|
+
if (token.isList) {
|
|
996
|
+
result += '#';
|
|
997
|
+
}
|
|
998
|
+
if (token.isOptional) {
|
|
999
|
+
result += '?';
|
|
1000
|
+
}
|
|
1001
|
+
if (token.isRepeatableGroup) {
|
|
1002
|
+
result += '!';
|
|
1003
|
+
}
|
|
1004
|
+
if (token.isRepeatable) {
|
|
1005
|
+
result += '*';
|
|
1006
|
+
}
|
|
1007
|
+
if (token.atLeastOnce) {
|
|
1008
|
+
result += '+';
|
|
1009
|
+
}
|
|
1010
|
+
if (token.occurence != null) {
|
|
1011
|
+
if (token.occurence.max == null || token.occurence.max == token.occurence.min || Number.isNaN(token.occurence.max)) {
|
|
1012
|
+
result += '{' + token.occurence.min + '}';
|
|
1013
|
+
}
|
|
1014
|
+
else {
|
|
1015
|
+
result += '{' + token.occurence.min + ',' + (Number.isFinite(token.occurence.max) ? token.occurence.max : '\u221e') + '}';
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
return result;
|
|
1019
|
+
}
|
|
952
1020
|
function minify(ast) {
|
|
953
1021
|
if (Array.isArray(ast)) {
|
|
954
1022
|
// @ts-ignore
|
|
@@ -960,20 +1028,7 @@ function minify(ast) {
|
|
|
960
1028
|
ast.pop();
|
|
961
1029
|
}
|
|
962
1030
|
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
1031
|
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
1032
|
if (ast[i].typ == ValidationTokenEnum.Whitespace && ast[i + 1]?.typ == ValidationTokenEnum.Whitespace) {
|
|
978
1033
|
ast.splice(i + 1, 1);
|
|
979
1034
|
i--;
|
|
@@ -990,14 +1045,9 @@ function minify(ast) {
|
|
|
990
1045
|
}
|
|
991
1046
|
return ast;
|
|
992
1047
|
}
|
|
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
1048
|
if ('l' in ast) {
|
|
997
1049
|
minify(ast.l);
|
|
998
1050
|
}
|
|
999
|
-
// }
|
|
1000
|
-
// for (const j of (ast as ValidationPipeToken | ValidationColumnToken | ValidationAmpersandToken).r) {
|
|
1001
1051
|
if ('r' in ast) {
|
|
1002
1052
|
minify(ast.r);
|
|
1003
1053
|
}
|
|
@@ -1009,44 +1059,5 @@ function minify(ast) {
|
|
|
1009
1059
|
}
|
|
1010
1060
|
return ast;
|
|
1011
1061
|
}
|
|
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
1062
|
|
|
1052
|
-
export { WalkValidationTokenEnum, WalkValidationTokenKeyTypeEnum, parseSyntax,
|
|
1063
|
+
export { WalkValidationTokenEnum, WalkValidationTokenKeyTypeEnum, 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
|
|
|
@@ -19,7 +19,7 @@ function validateSelector(selector, options, root) {
|
|
|
19
19
|
}
|
|
20
20
|
// @ts-ignore
|
|
21
21
|
if (root.typ == EnumToken.AtRuleNodeType && root.nam.match(/^(-[a-z]+-)?keyframes$/)) {
|
|
22
|
-
return validateKeyframeBlockList(selector
|
|
22
|
+
return validateKeyframeBlockList(selector);
|
|
23
23
|
}
|
|
24
24
|
let isNested = root.typ == EnumToken.RuleNodeType ? 1 : 0;
|
|
25
25
|
let currentRoot = root.parent;
|
|
@@ -35,7 +35,10 @@ function validateSelector(selector, options, root) {
|
|
|
35
35
|
}
|
|
36
36
|
const nestedSelector = isNested > 0;
|
|
37
37
|
// @ts-ignore
|
|
38
|
-
return nestedSelector ? validateRelativeSelectorList(selector, root, {
|
|
38
|
+
return nestedSelector ? validateRelativeSelectorList(selector, root, {
|
|
39
|
+
...(options ?? {}),
|
|
40
|
+
nestedSelector
|
|
41
|
+
}) : validateSelectorList(selector, root, { ...(options ?? {}), nestedSelector });
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
export { validateSelector };
|