@tbela99/css-parser 1.8.1 → 2.0.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 +66 -0
- package/README.md +129 -31
- package/dist/index-umd-web.js +27382 -24086
- package/dist/index.d.ts +2737 -2200
- package/dist/package.json.js +2 -2
- package/dist/src/data/properties.json.js +1 -1
- package/dist/src/data/validation.json.js +1 -1
- package/dist/src/lib/ast/arena-node.js +548 -0
- package/dist/src/lib/ast/arena.js +3198 -0
- package/dist/src/lib/ast/features/calc.js +85 -76
- package/dist/src/lib/ast/features/if.js +155 -115
- package/dist/src/lib/ast/features/index.js +1 -1
- package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
- package/dist/src/lib/ast/features/prefix.js +184 -183
- package/dist/src/lib/ast/features/shorthand.js +40 -46
- package/dist/src/lib/ast/features/transform.js +24 -24
- package/dist/src/lib/ast/features/type.js +1 -1
- package/dist/src/lib/ast/fingerprint.js +333 -0
- package/dist/src/lib/ast/math/expression.js +458 -543
- package/dist/src/lib/ast/math/math.js +19 -41
- package/dist/src/lib/ast/minify.js +590 -647
- package/dist/src/lib/ast/transform/compute.js +82 -85
- package/dist/src/lib/ast/transform/matrix.js +21 -38
- package/dist/src/lib/ast/transform/minify.js +82 -193
- package/dist/src/lib/ast/transform/perspective.js +2 -3
- package/dist/src/lib/ast/transform/rotate.js +1 -1
- package/dist/src/lib/ast/transform/scale.js +1 -1
- package/dist/src/lib/ast/transform/skew.js +1 -1
- package/dist/src/lib/ast/transform/translate.js +1 -1
- package/dist/src/lib/ast/transform/utils.js +2 -2
- package/dist/src/lib/ast/types.js +263 -258
- package/dist/src/lib/ast/walk.js +246 -610
- package/dist/src/lib/fs/resolve.js +1 -1
- package/dist/src/lib/parser/declaration/list.js +223 -139
- package/dist/src/lib/parser/declaration/map.js +251 -238
- package/dist/src/lib/parser/declaration/set.js +74 -66
- package/dist/src/lib/parser/linesmap.js +1 -1
- package/dist/src/lib/parser/parse.js +1379 -1662
- package/dist/src/lib/parser/source.js +7 -4
- package/dist/src/lib/parser/tokenize.js +406 -199
- package/dist/src/lib/parser/utils/angle.js +11 -13
- package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
- package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
- package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
- package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
- package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
- package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
- package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
- package/dist/src/lib/parser/utils/at-rule.js +5 -5
- package/dist/src/lib/parser/utils/base64.js +2 -2
- package/dist/src/lib/parser/utils/cache.js +1 -1
- package/dist/src/lib/parser/utils/config.js +1 -1
- package/dist/src/lib/parser/utils/declaration.js +233 -234
- package/dist/src/lib/parser/utils/eq.js +50 -3
- package/dist/src/lib/parser/utils/hash.js +2 -35
- package/dist/src/lib/parser/utils/selector.js +309 -387
- package/dist/src/lib/parser/utils/text.js +18 -4
- package/dist/src/lib/parser/utils/token.js +16 -69
- package/dist/src/lib/parser/utils/type.js +37 -24
- package/dist/src/lib/printer/render.js +965 -511
- package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
- package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
- package/dist/src/lib/syntax/color/a98rgb.js +1 -1
- package/dist/src/lib/syntax/color/alpha.js +36 -37
- package/dist/src/lib/syntax/color/cmyk.js +35 -52
- package/dist/src/lib/syntax/color/color-mix.js +96 -132
- package/dist/src/lib/syntax/color/color.js +222 -231
- package/dist/src/lib/syntax/color/hex.js +45 -55
- package/dist/src/lib/syntax/color/hsl.js +54 -62
- package/dist/src/lib/syntax/color/hwb.js +60 -68
- package/dist/src/lib/syntax/color/lab.js +59 -67
- package/dist/src/lib/syntax/color/lch.js +59 -67
- package/dist/src/lib/syntax/color/oklab.js +60 -68
- package/dist/src/lib/syntax/color/oklch.js +60 -67
- package/dist/src/lib/syntax/color/p3.js +1 -1
- package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
- package/dist/src/lib/syntax/color/rec2020.js +1 -1
- package/dist/src/lib/syntax/color/relative-color.js +94 -144
- package/dist/src/lib/syntax/color/rgb.js +54 -57
- package/dist/src/lib/syntax/color/srgb.js +55 -53
- package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
- package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
- package/dist/src/lib/syntax/color/utils/components.js +19 -20
- package/dist/src/lib/syntax/color/utils/distance.js +15 -13
- package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
- package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
- package/dist/src/lib/syntax/color/xyz.js +1 -1
- package/dist/src/lib/syntax/color/xyzd50.js +1 -1
- package/dist/src/lib/syntax/constants.js +2 -16
- package/dist/src/lib/syntax/syntax.js +301 -407
- package/dist/src/lib/validation/config.js +1 -1
- package/dist/src/lib/validation/match.js +959 -723
- package/dist/src/lib/validation/parser/parse.js +230 -112
- package/dist/src/lib/validation/parser/typedef.js +1 -1
- package/dist/src/lib/validation/utils/list.js +27 -12
- package/dist/src/node.js +12 -72
- package/dist/src/types.js +1 -1
- package/dist/src/utils/sync.js +9 -6
- package/dist/src/web.js +11 -73
- package/package.json +9 -10
- package/dist/index.cjs +0 -35874
- package/dist/src/lib/ast/clone.js +0 -51
- package/dist/src/lib/ast/expand.js +0 -311
- package/dist/src/lib/ast/find.js +0 -192
- package/dist/src/lib/ast/node.js +0 -59
|
@@ -2,21 +2,22 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
|
+
import { TokenField } from '../ast/arena.js';
|
|
8
9
|
import { isOkLabClose } from './color/utils/distance.js';
|
|
9
|
-
import { EnumToken, ColorType } from '../ast/types.js';
|
|
10
|
-
import { walkValues, WalkerOptionEnum } from '../ast/walk.js';
|
|
10
|
+
import { EnumToken, ColorType, WalkerOptionEnum } from '../ast/types.js';
|
|
11
11
|
import { toDegrees } from '../parser/utils/angle.js';
|
|
12
12
|
import { memoize } from '../parser/utils/cache.js';
|
|
13
|
-
import { equalsIgnoreCase } from '../parser/utils/text.js';
|
|
13
|
+
import { equalsIgnoreCase, lowercase } from '../parser/utils/text.js';
|
|
14
14
|
import { trimArray } from '../validation/match.js';
|
|
15
15
|
import { splitTokenList } from '../validation/utils/list.js';
|
|
16
16
|
import { getColorSpace } from './color/utils/colorspace.js';
|
|
17
17
|
import { getColorComponents } from './color/utils/components.js';
|
|
18
|
-
import { anglePrecision, nonStandardColors, systemColors, deprecatedSystemColors, COLORS_NAMES, colorsFunc
|
|
18
|
+
import { colorPrecision, epsilon, anglePrecision, colorFuncColorSpace, nonStandardColors, systemColors, deprecatedSystemColors, COLORS_NAMES, colorsFunc } from './constants.js';
|
|
19
19
|
import { getSyntaxConfig } from '../validation/config.js';
|
|
20
|
+
import { ValueWalker } from '../ast/walk.js';
|
|
20
21
|
|
|
21
22
|
// https://www.w3.org/TR/CSS21/syndata.html#syntax
|
|
22
23
|
// https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token
|
|
@@ -501,56 +502,59 @@ const pseudoAliasMap = {
|
|
|
501
502
|
// ]);
|
|
502
503
|
// renamed standard properties
|
|
503
504
|
const renamedStandardProperties = new Map([["color-adjust", "print-color-adjust"]]);
|
|
504
|
-
|
|
505
|
-
|
|
505
|
+
/**
|
|
506
|
+
* true if the unit is a length unit
|
|
507
|
+
* @param unit
|
|
508
|
+
*/
|
|
509
|
+
function isLengthUnit(unit) {
|
|
510
|
+
return dimensionUnits.includes(unit.toLowerCase());
|
|
506
511
|
}
|
|
507
|
-
|
|
508
|
-
|
|
512
|
+
/**
|
|
513
|
+
* true if the unit is a resolution unit
|
|
514
|
+
* @param unit
|
|
515
|
+
*/
|
|
516
|
+
function isResolutionUnit(unit) {
|
|
517
|
+
return resolutionUnits.includes(unit.toLowerCase());
|
|
509
518
|
}
|
|
510
|
-
|
|
511
|
-
|
|
519
|
+
/**
|
|
520
|
+
* true if the unit is an angle unit
|
|
521
|
+
* @param unit
|
|
522
|
+
*/
|
|
523
|
+
function isAngleUnit(unit) {
|
|
524
|
+
return angleUnits.includes(unit.toLowerCase());
|
|
512
525
|
}
|
|
513
|
-
|
|
514
|
-
|
|
526
|
+
/**
|
|
527
|
+
* true if the unit is a time unit
|
|
528
|
+
* @param unit
|
|
529
|
+
*/
|
|
530
|
+
function isTimeUnit(unit) {
|
|
531
|
+
return timeUnits.includes(unit.toLowerCase());
|
|
515
532
|
}
|
|
516
|
-
|
|
517
|
-
|
|
533
|
+
/**
|
|
534
|
+
* true if the unit is a frequency unit
|
|
535
|
+
* @param unit
|
|
536
|
+
*/
|
|
537
|
+
function isFrequencyUnit(unit) {
|
|
538
|
+
return frequencyUnits.includes(unit.toLowerCase());
|
|
518
539
|
}
|
|
519
540
|
/**
|
|
520
|
-
*
|
|
521
|
-
* @param
|
|
522
|
-
* @returns
|
|
541
|
+
* true if the unit is a flex unit
|
|
542
|
+
* @param unit
|
|
523
543
|
*/
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
// "lab",
|
|
531
|
-
// "oklab",
|
|
532
|
-
// "lch",
|
|
533
|
-
// "oklch",
|
|
534
|
-
// "xyz",
|
|
535
|
-
// "xyz-d50",
|
|
536
|
-
// "xyz-d65",
|
|
537
|
-
// "display-p3",
|
|
538
|
-
// "a98-rgb",
|
|
539
|
-
// "prophoto-rgb",
|
|
540
|
-
// "rec2020",
|
|
541
|
-
// "rgb",
|
|
542
|
-
// "hsl",
|
|
543
|
-
// "hwb",
|
|
544
|
-
// ].includes((token as IdentToken).val.toLowerCase())
|
|
545
|
-
// );
|
|
546
|
-
// }
|
|
544
|
+
function isFlexUnit(unit) {
|
|
545
|
+
return "fr" == unit.toLowerCase();
|
|
546
|
+
}
|
|
547
|
+
function isLength(arena, dimension) {
|
|
548
|
+
return arena.unit[dimension] != 0 && isLengthUnit(arena.strings[arena.unit[dimension]]);
|
|
549
|
+
}
|
|
547
550
|
/**
|
|
548
551
|
* Reduce color stops
|
|
552
|
+
* @param arena
|
|
549
553
|
* @param stops
|
|
550
554
|
* @returns
|
|
551
555
|
*/
|
|
552
|
-
function reduceColorStops(stops) {
|
|
553
|
-
const parts = splitTokenList(stops);
|
|
556
|
+
function reduceColorStops(arena, stops) {
|
|
557
|
+
const parts = splitTokenList(arena, stops);
|
|
554
558
|
const n = parts.length == 1 ? 1 : parts.length - 1;
|
|
555
559
|
let j;
|
|
556
560
|
let i;
|
|
@@ -561,9 +565,9 @@ function reduceColorStops(stops) {
|
|
|
561
565
|
if (parts[i].length != 3) {
|
|
562
566
|
continue;
|
|
563
567
|
}
|
|
564
|
-
if (i > 0 && isOkLabClose(parts[i - 1][0], parts[i][0])) {
|
|
568
|
+
if (i > 0 && isOkLabClose(arena, parts[i - 1][0], parts[i][0])) {
|
|
565
569
|
if (parts[i - 1].length == 1) {
|
|
566
|
-
parts[i - 1].push(
|
|
570
|
+
parts[i - 1].push(arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.PercentageTokenType, ((k - 1) * 100) / n));
|
|
567
571
|
}
|
|
568
572
|
for (let m = 1; m < parts[i].length; m++) {
|
|
569
573
|
parts[i - 1].push(parts[i][m]);
|
|
@@ -573,12 +577,13 @@ function reduceColorStops(stops) {
|
|
|
573
577
|
continue;
|
|
574
578
|
}
|
|
575
579
|
for (j = 0; j < parts[i].length; j++) {
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
+
const typ = arena.dataTypes[parts[i][j]];
|
|
581
|
+
if ((typ == EnumToken.LengthTokenType && 0 == arena.numericValue[parts[i][j]]) ||
|
|
582
|
+
typ == EnumToken.NumberTokenType ||
|
|
583
|
+
typ == EnumToken.PercentageTokenType) {
|
|
584
|
+
if (arena.numericValue[parts[i][j]] === (k * 100) / n) {
|
|
580
585
|
parts[i].length = j;
|
|
581
|
-
trimArray(parts[i]);
|
|
586
|
+
trimArray(arena, parts[i]);
|
|
582
587
|
updated = true;
|
|
583
588
|
break;
|
|
584
589
|
}
|
|
@@ -589,7 +594,7 @@ function reduceColorStops(stops) {
|
|
|
589
594
|
stops.length = 0;
|
|
590
595
|
for (j = 0; j < parts.length; j++) {
|
|
591
596
|
if (stops.length > 0) {
|
|
592
|
-
stops.push(
|
|
597
|
+
stops.push(arena.createToken(EnumToken.CommaTokenType));
|
|
593
598
|
}
|
|
594
599
|
for (let m = 0; m < parts[j].length; m++) {
|
|
595
600
|
stops.push(parts[j][m]);
|
|
@@ -600,10 +605,11 @@ function reduceColorStops(stops) {
|
|
|
600
605
|
}
|
|
601
606
|
/**
|
|
602
607
|
* Reduce background-position values.
|
|
608
|
+
* @param arena
|
|
603
609
|
* @param positions
|
|
604
610
|
* @param position
|
|
605
611
|
*/
|
|
606
|
-
function reducegradientBackgroundPosition(positions, position) {
|
|
612
|
+
function reducegradientBackgroundPosition(arena, positions, position) {
|
|
607
613
|
switch (position) {
|
|
608
614
|
case "50%":
|
|
609
615
|
case "50% 50%":
|
|
@@ -617,66 +623,59 @@ function reducegradientBackgroundPosition(positions, position) {
|
|
|
617
623
|
case "left center":
|
|
618
624
|
case "center left":
|
|
619
625
|
positions.length = 0;
|
|
620
|
-
positions.push(
|
|
626
|
+
positions.push(arena.createToken(EnumToken.PercentageTokenType, 0));
|
|
621
627
|
break;
|
|
622
628
|
case "50% 0%":
|
|
623
629
|
case "50% 0":
|
|
624
630
|
case "top center":
|
|
625
631
|
case "center top":
|
|
626
632
|
positions.length = 0;
|
|
627
|
-
positions.push(
|
|
633
|
+
positions.push(arena.createToken(EnumToken.IdenTokenType, "top"));
|
|
628
634
|
break;
|
|
629
635
|
case "bottom center":
|
|
630
636
|
case "center bottom":
|
|
631
637
|
case "bottom":
|
|
632
638
|
case "50% 100%":
|
|
633
639
|
positions.length = 0;
|
|
634
|
-
positions.push(
|
|
640
|
+
positions.push(arena.createToken(EnumToken.IdenTokenType, "bottom"));
|
|
635
641
|
break;
|
|
636
|
-
// case "left":
|
|
637
|
-
// case "0 50%":
|
|
638
|
-
// case "0% 50%":
|
|
639
|
-
// case "left center":
|
|
640
|
-
// case "center left":
|
|
641
|
-
// positions.length = 0;
|
|
642
|
-
// positions.push({ typ: EnumToken.PercentageTokenType, val: 0 });
|
|
643
|
-
// break;
|
|
644
642
|
case "right center":
|
|
645
643
|
case "center right":
|
|
646
644
|
case "100% 50%":
|
|
647
645
|
case "right":
|
|
648
646
|
positions.length = 0;
|
|
649
|
-
positions.push(
|
|
647
|
+
positions.push(arena.createToken(EnumToken.PercentageTokenType, 100));
|
|
650
648
|
break;
|
|
651
649
|
case "bottom left":
|
|
652
650
|
case "left bottom":
|
|
653
651
|
positions.length = 0;
|
|
654
|
-
positions.push(
|
|
652
|
+
positions.push(arena.createToken(EnumToken.PercentageTokenType, 0), arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.PercentageTokenType, 100));
|
|
655
653
|
break;
|
|
656
654
|
case "bottom right":
|
|
657
655
|
case "right bottom":
|
|
658
656
|
positions.length = 0;
|
|
659
|
-
positions.push(
|
|
657
|
+
positions.push(arena.createToken(EnumToken.PercentageTokenType, 100), arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.PercentageTokenType, 100));
|
|
660
658
|
break;
|
|
661
659
|
case "top left":
|
|
662
660
|
case "left top":
|
|
663
661
|
positions.length = 0;
|
|
664
|
-
positions.push(
|
|
662
|
+
positions.push(arena.createToken(EnumToken.PercentageTokenType, 0), arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.PercentageTokenType, 0));
|
|
665
663
|
break;
|
|
666
664
|
case "top right":
|
|
667
665
|
case "right top":
|
|
668
666
|
positions.length = 0;
|
|
669
|
-
positions.push(
|
|
667
|
+
positions.push(arena.createToken(EnumToken.PercentageTokenType, 100), arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.PercentageTokenType, 0));
|
|
670
668
|
break;
|
|
671
669
|
}
|
|
672
670
|
}
|
|
673
671
|
/**
|
|
674
672
|
* Reduce conic-gradient color stops
|
|
673
|
+
* @param arena
|
|
675
674
|
* @param stops
|
|
676
675
|
* @returns
|
|
677
676
|
*/
|
|
678
|
-
function reduceConicColorStops(stops) {
|
|
679
|
-
const parts = splitTokenList(stops);
|
|
677
|
+
function reduceConicColorStops(arena, stops) {
|
|
678
|
+
const parts = splitTokenList(arena, stops);
|
|
680
679
|
const n = parts.length == 1 ? 1 : parts.length - 1;
|
|
681
680
|
let j;
|
|
682
681
|
let i;
|
|
@@ -687,9 +686,9 @@ function reduceConicColorStops(stops) {
|
|
|
687
686
|
if (parts[i].length != 3) {
|
|
688
687
|
continue;
|
|
689
688
|
}
|
|
690
|
-
if (i > 0 && isOkLabClose(parts[i - 1][0], parts[i][0])) {
|
|
689
|
+
if (i > 0 && isOkLabClose(arena, parts[i - 1][0], parts[i][0])) {
|
|
691
690
|
if (parts[i - 1].length == 1) {
|
|
692
|
-
parts[i - 1].push(
|
|
691
|
+
parts[i - 1].push(arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.AngleTokenType, ((k - 1) * 100) / n, "deg"));
|
|
693
692
|
}
|
|
694
693
|
for (let m = 1; m < parts[i].length; m++) {
|
|
695
694
|
parts[i - 1].push(parts[i][m]);
|
|
@@ -699,11 +698,12 @@ function reduceConicColorStops(stops) {
|
|
|
699
698
|
continue;
|
|
700
699
|
}
|
|
701
700
|
for (j = 0; j < parts[i].length; j++) {
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
701
|
+
const typ = arena.dataTypes[parts[i][j]];
|
|
702
|
+
if ((typ == EnumToken.NumberTokenType && 0 == arena.numericValue[parts[i][j]]) ||
|
|
703
|
+
typ == EnumToken.AngleTokenType) {
|
|
704
|
+
if (arena.numericValue[toDegrees(arena, parts[i][j])] === (k * 360) / n) {
|
|
705
705
|
parts[i].length = j;
|
|
706
|
-
trimArray(parts[i]);
|
|
706
|
+
trimArray(arena, parts[i]);
|
|
707
707
|
updated = true;
|
|
708
708
|
break;
|
|
709
709
|
}
|
|
@@ -714,7 +714,7 @@ function reduceConicColorStops(stops) {
|
|
|
714
714
|
stops.length = 0;
|
|
715
715
|
for (j = 0; j < parts.length; j++) {
|
|
716
716
|
if (stops.length > 0) {
|
|
717
|
-
stops.push(
|
|
717
|
+
stops.push(arena.createToken(EnumToken.CommaTokenType));
|
|
718
718
|
}
|
|
719
719
|
for (const token of parts[j]) {
|
|
720
720
|
stops.push(token);
|
|
@@ -725,84 +725,85 @@ function reduceConicColorStops(stops) {
|
|
|
725
725
|
}
|
|
726
726
|
/**
|
|
727
727
|
* is rectangular orthogonal colorspace
|
|
728
|
+
* @param arena
|
|
728
729
|
* @param token
|
|
729
730
|
* @returns
|
|
730
731
|
*/
|
|
731
|
-
function isRectangularOrthogonalColorspace(token) {
|
|
732
|
-
return (token
|
|
733
|
-
colorFuncColorSpace.some((t) => equalsIgnoreCase(t, token
|
|
732
|
+
function isRectangularOrthogonalColorspace(arena, token) {
|
|
733
|
+
return (arena.dataTypes[token] === EnumToken.IdenTokenType &&
|
|
734
|
+
colorFuncColorSpace.some((t) => equalsIgnoreCase(t, arena.strings[arena.stringValue[token]])));
|
|
734
735
|
}
|
|
735
736
|
/**
|
|
736
737
|
* Is polar colorspace
|
|
738
|
+
* @param arena
|
|
737
739
|
* @param token
|
|
738
740
|
* @returns
|
|
739
741
|
*/
|
|
740
|
-
function isPolarColorspace(token) {
|
|
741
|
-
return (token
|
|
742
|
-
["hsl", "hwb", "lch", "oklch"].some((t) => equalsIgnoreCase(t, token
|
|
742
|
+
function isPolarColorspace(arena, token) {
|
|
743
|
+
return (arena.dataTypes[token] === EnumToken.IdenTokenType &&
|
|
744
|
+
["hsl", "hwb", "lch", "oklch"].some((t) => equalsIgnoreCase(t, arena.strings[arena.stringValue[token]])));
|
|
743
745
|
}
|
|
744
746
|
/**
|
|
745
|
-
* Is
|
|
747
|
+
* Is ident color
|
|
748
|
+
* @param arena
|
|
746
749
|
* @param token
|
|
747
750
|
* @returns
|
|
748
751
|
*/
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
// return false;
|
|
756
|
-
// }
|
|
757
|
-
//
|
|
758
|
-
// return (
|
|
759
|
-
// ["shorter", "longer", "increasing", "decreasing"].some((t) =>
|
|
760
|
-
// equalsIgnoreCase(t, (token[0] as IdentToken).val ?? ""),
|
|
761
|
-
// ) && "hue" === (token[1] as IdentToken).val?.toLowerCase?.()
|
|
762
|
-
// );
|
|
763
|
-
// }
|
|
752
|
+
function isIdentColor(arena, token) {
|
|
753
|
+
return (arena.dataTypes[token] == EnumToken.ColorTokenType &&
|
|
754
|
+
arena.colorType[token] != ColorType.NONE &&
|
|
755
|
+
[ColorType.SYS, ColorType.DPSYS, ColorType.LIT].includes(arena.colorType[token]) &&
|
|
756
|
+
isIdent(arena.strings[arena.stringValue[token]]));
|
|
757
|
+
}
|
|
764
758
|
/**
|
|
765
|
-
*
|
|
759
|
+
* color function arguments without whitespace and comments
|
|
760
|
+
* @param arena
|
|
766
761
|
* @param token
|
|
767
|
-
* @
|
|
762
|
+
* @param types accepted token types
|
|
768
763
|
*/
|
|
769
|
-
function
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
764
|
+
function filterChildren(arena, token, types) {
|
|
765
|
+
const children = [];
|
|
766
|
+
for (let t = arena.firstToken[token]; t != 0; t = arena.nextSibling[t]) {
|
|
767
|
+
if (types.includes(arena.dataTypes[t])) {
|
|
768
|
+
children.push(t);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
return children;
|
|
773
772
|
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
773
|
+
const colorKeywords = [
|
|
774
|
+
"in",
|
|
775
|
+
"hue",
|
|
776
|
+
"from",
|
|
777
|
+
"alpha",
|
|
778
|
+
"none",
|
|
779
|
+
"shorter",
|
|
780
|
+
"longer",
|
|
781
|
+
"increasing",
|
|
782
|
+
"decreasing",
|
|
783
|
+
];
|
|
784
|
+
function isColor(arena, token, errors) {
|
|
785
|
+
const typ = arena.dataTypes[token];
|
|
786
|
+
if (typ == EnumToken.WildCardFunctionTokenType) {
|
|
782
787
|
return true;
|
|
783
788
|
}
|
|
784
|
-
if (
|
|
785
|
-
if (
|
|
789
|
+
if (typ == EnumToken.ColorTokenType) {
|
|
790
|
+
if (arena.colorType[token] != ColorType.NONE && arena.firstToken[token] == 0) {
|
|
786
791
|
return true;
|
|
787
792
|
}
|
|
788
793
|
}
|
|
789
|
-
if (
|
|
790
|
-
const val =
|
|
794
|
+
if (typ == EnumToken.IdenTokenType) {
|
|
795
|
+
const val = lowercase(arena.strings[arena.stringValue[token]]);
|
|
791
796
|
if (systemColors.has(val) || deprecatedSystemColors.has(val) || nonStandardColors.has(val)) {
|
|
792
797
|
return true;
|
|
793
798
|
}
|
|
794
799
|
// named color
|
|
795
800
|
return val in COLORS_NAMES || "currentcolor" === val || "transparent" === val;
|
|
796
801
|
}
|
|
797
|
-
if (
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
// @ts-ignore
|
|
803
|
-
if (token.val === "light-dark") {
|
|
804
|
-
// @ts-ignore
|
|
805
|
-
const children = token.chi.filter((t) => [
|
|
802
|
+
if (typ === EnumToken.FunctionTokenType || typ === EnumToken.ColorTokenType) {
|
|
803
|
+
if (arena.firstToken[token] != 0) {
|
|
804
|
+
const name = arena.strings[arena.stringValue[token]];
|
|
805
|
+
if (name === "light-dark") {
|
|
806
|
+
const children = filterChildren(arena, token, [
|
|
806
807
|
EnumToken.IdenTokenType,
|
|
807
808
|
EnumToken.NumberTokenType,
|
|
808
809
|
EnumToken.LiteralTokenType,
|
|
@@ -810,7 +811,7 @@ function isColor(token, errors) {
|
|
|
810
811
|
EnumToken.FunctionTokenType,
|
|
811
812
|
EnumToken.PercentageTokenType,
|
|
812
813
|
EnumToken.WildCardFunctionTokenType,
|
|
813
|
-
]
|
|
814
|
+
]);
|
|
814
815
|
if (children.length != 2) {
|
|
815
816
|
errors?.push({
|
|
816
817
|
message: "light-dark function must have 2 arguments",
|
|
@@ -819,31 +820,23 @@ function isColor(token, errors) {
|
|
|
819
820
|
});
|
|
820
821
|
return false;
|
|
821
822
|
}
|
|
822
|
-
if (isColor(children[0]) && isColor(children[1])) {
|
|
823
|
+
if (isColor(arena, children[0]) && isColor(arena, children[1])) {
|
|
823
824
|
return true;
|
|
824
825
|
}
|
|
825
826
|
}
|
|
826
827
|
// adding numbers and percentages is disallowed
|
|
827
828
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/lch#defining_relative_color_output_channel_components:~:text=Adding%20a%20%3Cpercentage%3E%20to%20a%20%3Cnumber%3E%2C%20for%20example%2C%20doesn%27t%20work
|
|
828
|
-
const components = getColorComponents(token);
|
|
829
|
+
const components = getColorComponents(arena, token);
|
|
829
830
|
if (components !== null) {
|
|
830
|
-
const colorSpace = getColorSpace(token)?.split?.("");
|
|
831
|
+
const colorSpace = getColorSpace(arena, token)?.split?.("");
|
|
831
832
|
if (colorSpace != null) {
|
|
832
833
|
for (const value of components) {
|
|
833
|
-
if (value
|
|
834
|
-
const val = value.
|
|
834
|
+
if (arena.dataTypes[value] === EnumToken.IdenTokenType) {
|
|
835
|
+
const val = arena.strings[arena.stringValue[value]].toLowerCase();
|
|
835
836
|
if (
|
|
836
837
|
// @ts-expect-error
|
|
837
838
|
typeof Math[val.toUpperCase()] !== "number" &&
|
|
838
|
-
val
|
|
839
|
-
val != "hue" &&
|
|
840
|
-
val != "from" &&
|
|
841
|
-
val != "alpha" &&
|
|
842
|
-
val != "none" &&
|
|
843
|
-
val != "shorter" &&
|
|
844
|
-
val != "longer" &&
|
|
845
|
-
val != "increasing" &&
|
|
846
|
-
val != "decreasing" &&
|
|
839
|
+
!colorKeywords.includes(val) &&
|
|
847
840
|
!colorsFunc.includes(val) &&
|
|
848
841
|
!colorSpace.includes(val) &&
|
|
849
842
|
!colorFuncColorSpace.includes(val)) {
|
|
@@ -851,37 +844,28 @@ function isColor(token, errors) {
|
|
|
851
844
|
action: "drop",
|
|
852
845
|
message: `Unexpected constant '${val}'`,
|
|
853
846
|
node: value,
|
|
854
|
-
// location: options.source!.getSourLocation(value[LOC]!.sta),
|
|
855
847
|
});
|
|
856
848
|
return false;
|
|
857
849
|
}
|
|
858
850
|
}
|
|
859
|
-
else if (value
|
|
860
|
-
equalsIgnoreCase("calc", value
|
|
851
|
+
else if (arena.dataTypes[value] === EnumToken.MathFunctionTokenType &&
|
|
852
|
+
equalsIgnoreCase("calc", arena.strings[arena.stringValue[value]])) {
|
|
861
853
|
let val;
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
854
|
+
// inlined: arena.walkValues(arena.tokens(value))
|
|
855
|
+
for (const v of new ValueWalker(arena, arena.tokens(value))) {
|
|
856
|
+
if (arena.dataTypes[v] === EnumToken.IdenTokenType) {
|
|
857
|
+
val = arena.strings[arena.stringValue[v]].toLowerCase();
|
|
865
858
|
if (
|
|
866
859
|
// @ts-expect-error
|
|
867
860
|
typeof Math[val.toUpperCase()] !== "number" &&
|
|
868
|
-
val
|
|
869
|
-
val != "hue" &&
|
|
870
|
-
val != "from" &&
|
|
871
|
-
val != "alpha" &&
|
|
872
|
-
val != "none" &&
|
|
873
|
-
val != "shorter" &&
|
|
874
|
-
val != "longer" &&
|
|
875
|
-
val != "increasing" &&
|
|
876
|
-
val != "decreasing" &&
|
|
861
|
+
!colorKeywords.includes(val) &&
|
|
877
862
|
!colorsFunc.includes(val) &&
|
|
878
863
|
!colorSpace.includes(val) &&
|
|
879
864
|
!colorFuncColorSpace.includes(val)) {
|
|
880
865
|
errors?.push({
|
|
881
866
|
action: "drop",
|
|
882
867
|
message: `Unexpected constant '${val}'`,
|
|
883
|
-
node: v
|
|
884
|
-
// location: options.source!.getSourLocation(v.value[LOC]!.sta),
|
|
868
|
+
node: v,
|
|
885
869
|
});
|
|
886
870
|
return false;
|
|
887
871
|
}
|
|
@@ -891,54 +875,57 @@ function isColor(token, errors) {
|
|
|
891
875
|
}
|
|
892
876
|
}
|
|
893
877
|
}
|
|
894
|
-
//
|
|
895
|
-
for (const
|
|
878
|
+
// inlined: arena.walkValues(arena.tokens(token), filter)
|
|
879
|
+
for (const value of new ValueWalker(arena, arena.tokens(token), (arena, value) => arena.dataTypes[value] === EnumToken.WildCardFunctionTokenType
|
|
896
880
|
? WalkerOptionEnum.Ignore | WalkerOptionEnum.IgnoreChildren
|
|
897
881
|
: null)) {
|
|
882
|
+
// the parent of a detached token is the root token
|
|
883
|
+
const parent = arena.getValueParent(value) || token;
|
|
898
884
|
let k = 0;
|
|
899
885
|
let l;
|
|
900
|
-
let tk =
|
|
901
|
-
let tl =
|
|
902
|
-
if (value
|
|
903
|
-
tk = value.
|
|
904
|
-
tl = value.
|
|
886
|
+
let tk = 0;
|
|
887
|
+
let tl = 0;
|
|
888
|
+
if (arena.dataTypes[value] === EnumToken.BinaryExpressionTokenType) {
|
|
889
|
+
tk = arena.field(value, TokenField.L);
|
|
890
|
+
tl = arena.field(value, TokenField.R);
|
|
905
891
|
}
|
|
906
|
-
else if (parent
|
|
907
|
-
parent
|
|
892
|
+
else if (parent != null &&
|
|
893
|
+
parent != token &&
|
|
894
|
+
arena.dataTypes[parent] === EnumToken.MathFunctionTokenType &&
|
|
895
|
+
arena.strings[arena.stringValue[parent]] === "calc") {
|
|
896
|
+
const children = arena.tokens(parent);
|
|
908
897
|
l = k + 1;
|
|
909
|
-
while (l + 1 <
|
|
910
|
-
const tk =
|
|
911
|
-
if (tk
|
|
912
|
-
tk
|
|
913
|
-
tk
|
|
914
|
-
tk
|
|
915
|
-
tk
|
|
916
|
-
tk
|
|
898
|
+
while (l + 1 < children.length) {
|
|
899
|
+
const tk = arena.dataTypes[children[l]];
|
|
900
|
+
if (tk === EnumToken.WhitespaceTokenType ||
|
|
901
|
+
tk === EnumToken.CommentTokenType ||
|
|
902
|
+
tk === EnumToken.Add ||
|
|
903
|
+
tk === EnumToken.Sub ||
|
|
904
|
+
tk === EnumToken.Div ||
|
|
905
|
+
tk === EnumToken.Mul) {
|
|
917
906
|
l++;
|
|
918
907
|
continue;
|
|
919
908
|
}
|
|
920
909
|
break;
|
|
921
910
|
}
|
|
922
|
-
tk =
|
|
923
|
-
tl =
|
|
911
|
+
tk = children[k];
|
|
912
|
+
tl = children[l];
|
|
924
913
|
}
|
|
925
|
-
if (tk != null && tl != null) {
|
|
926
|
-
if ((tk
|
|
927
|
-
|
|
914
|
+
if (tk != 0 && tl != 0 && tk != null && tl != null) {
|
|
915
|
+
if ((arena.dataTypes[tk] === EnumToken.PercentageTokenType ||
|
|
916
|
+
arena.dataTypes[tl] === EnumToken.PercentageTokenType) &&
|
|
917
|
+
arena.dataTypes[tk] !== arena.dataTypes[tl]) {
|
|
928
918
|
errors?.push({
|
|
929
919
|
action: "drop",
|
|
930
920
|
message: "adding percentage and number is not allowed",
|
|
931
921
|
node: token,
|
|
932
|
-
// location: options.source!.getSourLocation(token[LOC]!.sta),
|
|
933
922
|
});
|
|
934
923
|
return false;
|
|
935
924
|
}
|
|
936
925
|
}
|
|
937
926
|
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
// @ts-ignore
|
|
941
|
-
const children = token.chi.filter((t) => [
|
|
927
|
+
if (name == "color") {
|
|
928
|
+
const children = filterChildren(arena, token, [
|
|
942
929
|
EnumToken.DashedIdenTokenType,
|
|
943
930
|
EnumToken.IdenTokenType,
|
|
944
931
|
EnumToken.NumberTokenType,
|
|
@@ -947,13 +934,14 @@ function isColor(token, errors) {
|
|
|
947
934
|
EnumToken.FunctionTokenType,
|
|
948
935
|
EnumToken.MathFunctionTokenType,
|
|
949
936
|
EnumToken.PercentageTokenType,
|
|
950
|
-
]
|
|
951
|
-
const isRelative = children[0]
|
|
937
|
+
]);
|
|
938
|
+
const isRelative = arena.dataTypes[children[0]] == EnumToken.IdenTokenType &&
|
|
939
|
+
arena.strings[arena.stringValue[children[0]]] == "from";
|
|
952
940
|
let offset = 0;
|
|
953
941
|
if (isRelative) {
|
|
954
942
|
offset = 2;
|
|
955
943
|
}
|
|
956
|
-
if (children[offset]
|
|
944
|
+
if (offset < children.length && arena.dataTypes[children[offset]] == EnumToken.DashedIdenTokenType) {
|
|
957
945
|
if (children.length <= offset + 1) {
|
|
958
946
|
errors?.push({
|
|
959
947
|
action: "drop",
|
|
@@ -963,15 +951,16 @@ function isColor(token, errors) {
|
|
|
963
951
|
return false;
|
|
964
952
|
}
|
|
965
953
|
for (let i = offset + 1; i < children.length; i++) {
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
(children[i]
|
|
974
|
-
|
|
954
|
+
const childType = arena.dataTypes[children[i]];
|
|
955
|
+
if (childType == EnumToken.NumberTokenType ||
|
|
956
|
+
childType == EnumToken.LiteralTokenType ||
|
|
957
|
+
childType == EnumToken.ColorTokenType ||
|
|
958
|
+
childType == EnumToken.FunctionTokenType ||
|
|
959
|
+
childType == EnumToken.MathFunctionTokenType ||
|
|
960
|
+
childType == EnumToken.PercentageTokenType ||
|
|
961
|
+
isColor(arena, children[i]) ||
|
|
962
|
+
(childType == EnumToken.IdenTokenType &&
|
|
963
|
+
equalsIgnoreCase("none", arena.strings[arena.stringValue[children[i]]]))) {
|
|
975
964
|
continue;
|
|
976
965
|
}
|
|
977
966
|
return false;
|
|
@@ -983,37 +972,34 @@ function isColor(token, errors) {
|
|
|
983
972
|
}
|
|
984
973
|
return true;
|
|
985
974
|
}
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
acc.push([]);
|
|
975
|
+
else if (name == "color-mix") {
|
|
976
|
+
const children = [[]];
|
|
977
|
+
for (let t = arena.firstToken[token]; t != 0; t = arena.nextSibling[t]) {
|
|
978
|
+
if (arena.dataTypes[t] == EnumToken.CommaTokenType) {
|
|
979
|
+
children.push([]);
|
|
992
980
|
}
|
|
993
|
-
else {
|
|
994
|
-
|
|
995
|
-
acc[acc.length - 1].push(t);
|
|
996
|
-
}
|
|
981
|
+
else if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(arena.dataTypes[t])) {
|
|
982
|
+
children[children.length - 1].push(t);
|
|
997
983
|
}
|
|
998
|
-
|
|
999
|
-
}, [[]]);
|
|
984
|
+
}
|
|
1000
985
|
let j = 0;
|
|
1001
986
|
let k = 0;
|
|
1002
|
-
if (children[j]
|
|
1003
|
-
|
|
987
|
+
if (children[j].length > 0 &&
|
|
988
|
+
arena.dataTypes[children[j][0]] === EnumToken.IdenTokenType &&
|
|
989
|
+
equalsIgnoreCase("in", arena.strings[arena.stringValue[children[j][k]]])) {
|
|
1004
990
|
k++;
|
|
1005
|
-
if (children[j][k]
|
|
1006
|
-
if (!isRectangularOrthogonalColorspace(children[j][k])) {
|
|
1007
|
-
if (isPolarColorspace(children[j][k++])) {
|
|
991
|
+
if (k < children[j].length && arena.dataTypes[children[j][k]] === EnumToken.IdenTokenType) {
|
|
992
|
+
if (!isRectangularOrthogonalColorspace(arena, children[j][k])) {
|
|
993
|
+
if (isPolarColorspace(arena, children[j][k++])) {
|
|
1008
994
|
if (k == children[j].length) ;
|
|
1009
|
-
else if (children[j][k]
|
|
995
|
+
else if (arena.dataTypes[children[j][k]] !== EnumToken.IdenTokenType) {
|
|
1010
996
|
return false;
|
|
1011
997
|
}
|
|
1012
|
-
else if (equalsIgnoreCase("hue", children[j][k]
|
|
998
|
+
else if (equalsIgnoreCase("hue", arena.strings[arena.stringValue[children[j][k]]])) {
|
|
1013
999
|
k++;
|
|
1014
1000
|
}
|
|
1015
1001
|
else {
|
|
1016
|
-
switch (children[j][k]
|
|
1002
|
+
switch (arena.strings[arena.stringValue[children[j][k]]]) {
|
|
1017
1003
|
case "increasing":
|
|
1018
1004
|
case "decreasing":
|
|
1019
1005
|
case "longer":
|
|
@@ -1023,8 +1009,9 @@ function isColor(token, errors) {
|
|
|
1023
1009
|
default:
|
|
1024
1010
|
return false;
|
|
1025
1011
|
}
|
|
1026
|
-
if (children[j]
|
|
1027
|
-
|
|
1012
|
+
if (k >= children[j].length ||
|
|
1013
|
+
arena.dataTypes[children[j][k]] !== EnumToken.IdenTokenType ||
|
|
1014
|
+
!equalsIgnoreCase("hue", arena.strings[arena.stringValue[children[j][k]]])) {
|
|
1028
1015
|
return false;
|
|
1029
1016
|
}
|
|
1030
1017
|
k++;
|
|
@@ -1039,91 +1026,71 @@ function isColor(token, errors) {
|
|
|
1039
1026
|
}
|
|
1040
1027
|
return true;
|
|
1041
1028
|
}
|
|
1042
|
-
else {
|
|
1043
|
-
// const keywords: string[] = ["from", "none"];
|
|
1044
|
-
//
|
|
1045
|
-
// // @ts-ignore
|
|
1046
|
-
// if (
|
|
1047
|
-
// ["rgb", "hsl", "hwb", "lab", "lch", "oklab", "oklch"].some((t) =>
|
|
1048
|
-
// equalsIgnoreCase(t, (token as ColorToken).val),
|
|
1049
|
-
// )
|
|
1050
|
-
// ) {
|
|
1051
|
-
// // @ts-ignore
|
|
1052
|
-
// keywords.push("alpha");
|
|
1053
|
-
//
|
|
1054
|
-
// for (const keyword of (token as ColorToken).val.slice(-3).split("")) {
|
|
1055
|
-
// keywords.push(keyword);
|
|
1056
|
-
// }
|
|
1057
|
-
// }
|
|
1058
|
-
// @ts-ignore
|
|
1059
|
-
for (const v of token.chi) {
|
|
1060
|
-
// if (v.typ == EnumToken.IdenTokenType) {
|
|
1061
|
-
// continue;
|
|
1062
|
-
// }
|
|
1063
|
-
// if (
|
|
1064
|
-
// v.typ === EnumToken.MathFunctionTokenType ||
|
|
1065
|
-
// v.typ === EnumToken.WildCardFunctionTokenType ||
|
|
1066
|
-
// colorsFunc.includes(v.val)
|
|
1067
|
-
// ) {
|
|
1068
|
-
// continue;
|
|
1069
|
-
// }
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
1029
|
return true;
|
|
1073
1030
|
}
|
|
1074
1031
|
}
|
|
1075
1032
|
return false;
|
|
1076
1033
|
}
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1034
|
+
/**
|
|
1035
|
+
* parse a color token: set the color type of identifiers and color functions
|
|
1036
|
+
* @param arena
|
|
1037
|
+
* @param token
|
|
1038
|
+
*/
|
|
1039
|
+
function parseColor(arena, token) {
|
|
1040
|
+
const typ = arena.dataTypes[token];
|
|
1041
|
+
if (typ === EnumToken.IdenTokenType) {
|
|
1042
|
+
const val = arena.strings[arena.stringValue[token]].toLowerCase();
|
|
1080
1043
|
if (nonStandardColors.has(val)) {
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
});
|
|
1044
|
+
arena.dataTypes[token] = EnumToken.ColorTokenType;
|
|
1045
|
+
// inlined: arena.setColorType(token, ColorType.NON_STD)
|
|
1046
|
+
arena.colorType[token] = ColorType.NON_STD;
|
|
1085
1047
|
return token;
|
|
1086
1048
|
}
|
|
1087
1049
|
if (systemColors.has(val)) {
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
});
|
|
1050
|
+
arena.dataTypes[token] = EnumToken.ColorTokenType;
|
|
1051
|
+
// inlined: arena.setColorType(token, ColorType.SYS)
|
|
1052
|
+
arena.colorType[token] = ColorType.SYS;
|
|
1092
1053
|
return token;
|
|
1093
1054
|
}
|
|
1094
1055
|
if (deprecatedSystemColors.has(val)) {
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
});
|
|
1056
|
+
arena.dataTypes[token] = EnumToken.ColorTokenType;
|
|
1057
|
+
// inlined: arena.setColorType(token, ColorType.DPSYS)
|
|
1058
|
+
arena.colorType[token] = ColorType.DPSYS;
|
|
1099
1059
|
return token;
|
|
1100
1060
|
}
|
|
1101
1061
|
if (val in COLORS_NAMES || val === "currentcolor") {
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1062
|
+
arena.dataTypes[token] = EnumToken.ColorTokenType;
|
|
1063
|
+
// inlined: arena.setStr(token, val)
|
|
1064
|
+
arena.stringValue[token] = arena.internString(val);
|
|
1065
|
+
// inlined: arena.setColorType(token, ColorType.LIT)
|
|
1066
|
+
arena.colorType[token] = ColorType.LIT;
|
|
1107
1067
|
return token;
|
|
1108
1068
|
}
|
|
1109
1069
|
}
|
|
1110
|
-
if (
|
|
1111
|
-
|
|
1070
|
+
if (typ === EnumToken.ColorTokenType) {
|
|
1071
|
+
const val = arena.strings[arena.stringValue[token]];
|
|
1072
|
+
if (arena.colorType[token] == ColorType.NONE && arena.stringValue[token] != 0) {
|
|
1073
|
+
// inlined: arena.setColorType(token, ColorType[val.replaceAll("-", "_").toUpperCase()] ?? 0)
|
|
1112
1074
|
// @ts-expect-error
|
|
1113
|
-
token
|
|
1075
|
+
arena.colorType[token] = (ColorType[val.replaceAll("-", "_").toUpperCase()] ?? 0);
|
|
1114
1076
|
}
|
|
1115
|
-
if (
|
|
1116
|
-
const tk =
|
|
1117
|
-
|
|
1118
|
-
|
|
1077
|
+
if (arena.firstToken[token] != 0) {
|
|
1078
|
+
const tk = arena.findToken(token, (arena, t) => arena.dataTypes[t] !== EnumToken.WhitespaceTokenType &&
|
|
1079
|
+
arena.dataTypes[t] !== EnumToken.CommentTokenType);
|
|
1080
|
+
if (tk != null &&
|
|
1081
|
+
arena.dataTypes[tk] === EnumToken.IdenTokenType &&
|
|
1082
|
+
arena.strings[arena.stringValue[tk]] === "from") {
|
|
1083
|
+
// inlined: arena.setUnitString(token, "rel")
|
|
1084
|
+
arena.unit[token] = arena.internString("rel");
|
|
1119
1085
|
}
|
|
1120
|
-
else if (
|
|
1121
|
-
token
|
|
1086
|
+
else if (val == "color-mix" && tk != null && arena.strings[arena.stringValue[tk]] == "in") {
|
|
1087
|
+
// inlined: arena.setUnitString(token, "mix")
|
|
1088
|
+
arena.unit[token] = arena.internString("mix");
|
|
1122
1089
|
}
|
|
1123
|
-
if (
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
token
|
|
1090
|
+
if (val == "color") {
|
|
1091
|
+
if (tk != null && EnumToken.DashedIdenTokenType == arena.dataTypes[tk]) {
|
|
1092
|
+
// inlined: arena.setColorType(token, ColorType.CUSTOM_COLOR)
|
|
1093
|
+
arena.colorType[token] = ColorType.CUSTOM_COLOR;
|
|
1127
1094
|
}
|
|
1128
1095
|
}
|
|
1129
1096
|
}
|
|
@@ -1258,97 +1225,6 @@ function isPseudo(name) {
|
|
|
1258
1225
|
function isHash(name) {
|
|
1259
1226
|
return name.charAt(0) == "#" && isIdentStart(name.charCodeAt(1));
|
|
1260
1227
|
}
|
|
1261
|
-
// export const isNumber = memoize(function (name: string): boolean {
|
|
1262
|
-
// let codepoint: number = name.charCodeAt(0) as number;
|
|
1263
|
-
// let i: number = 0;
|
|
1264
|
-
// const j: number = name.length;
|
|
1265
|
-
//
|
|
1266
|
-
// if (j == 1 && !isDigit(codepoint)) {
|
|
1267
|
-
// return false;
|
|
1268
|
-
// }
|
|
1269
|
-
//
|
|
1270
|
-
// // '+' '-'
|
|
1271
|
-
// if ([0x2b, 0x2d].includes(codepoint)) {
|
|
1272
|
-
// i++;
|
|
1273
|
-
// }
|
|
1274
|
-
//
|
|
1275
|
-
// // consume digits
|
|
1276
|
-
// while (i < j) {
|
|
1277
|
-
// codepoint = name.charCodeAt(i) as number;
|
|
1278
|
-
//
|
|
1279
|
-
// if (isDigit(codepoint)) {
|
|
1280
|
-
// i++;
|
|
1281
|
-
// continue;
|
|
1282
|
-
// }
|
|
1283
|
-
//
|
|
1284
|
-
// // '.' 'E' 'e'
|
|
1285
|
-
// if (codepoint == 0x2e || codepoint == 0x45 || codepoint == 0x65) {
|
|
1286
|
-
// break;
|
|
1287
|
-
// }
|
|
1288
|
-
//
|
|
1289
|
-
// return false;
|
|
1290
|
-
// }
|
|
1291
|
-
//
|
|
1292
|
-
// // '.'
|
|
1293
|
-
// if (codepoint == 0x2e) {
|
|
1294
|
-
// if (!isDigit(name.charCodeAt(++i) as number)) {
|
|
1295
|
-
// return false;
|
|
1296
|
-
// }
|
|
1297
|
-
// }
|
|
1298
|
-
//
|
|
1299
|
-
// while (i < j) {
|
|
1300
|
-
// codepoint = name.charCodeAt(i) as number;
|
|
1301
|
-
//
|
|
1302
|
-
// if (isDigit(codepoint)) {
|
|
1303
|
-
// i++;
|
|
1304
|
-
// continue;
|
|
1305
|
-
// }
|
|
1306
|
-
//
|
|
1307
|
-
// // 'E' 'e'
|
|
1308
|
-
// if (codepoint == 0x45 || codepoint == 0x65) {
|
|
1309
|
-
// i++;
|
|
1310
|
-
// break;
|
|
1311
|
-
// }
|
|
1312
|
-
//
|
|
1313
|
-
// return false;
|
|
1314
|
-
// }
|
|
1315
|
-
//
|
|
1316
|
-
// // 'E' 'e'
|
|
1317
|
-
// if (codepoint == 0x45 || codepoint == 0x65) {
|
|
1318
|
-
// // if (i == j) {
|
|
1319
|
-
// // return false;
|
|
1320
|
-
// // }
|
|
1321
|
-
//
|
|
1322
|
-
// codepoint = name.charCodeAt(i + 1) as number;
|
|
1323
|
-
//
|
|
1324
|
-
// // '+' '-'
|
|
1325
|
-
// // if ([0x2b, 0x2d].includes(codepoint)) {
|
|
1326
|
-
// // i++;
|
|
1327
|
-
// // }
|
|
1328
|
-
//
|
|
1329
|
-
// codepoint = name.charCodeAt(i + 1) as number;
|
|
1330
|
-
//
|
|
1331
|
-
// if (!isDigit(codepoint)) {
|
|
1332
|
-
// return false;
|
|
1333
|
-
// }
|
|
1334
|
-
// }
|
|
1335
|
-
//
|
|
1336
|
-
// // while (++i < j) {
|
|
1337
|
-
// // codepoint = name.charCodeAt(i) as number;
|
|
1338
|
-
//
|
|
1339
|
-
// // if (!isDigit(codepoint)) {
|
|
1340
|
-
// // return false;
|
|
1341
|
-
// // }
|
|
1342
|
-
// // }
|
|
1343
|
-
//
|
|
1344
|
-
// return true;
|
|
1345
|
-
// }) as (name: string) => boolean;
|
|
1346
|
-
// export function isPercentage(name: string) {
|
|
1347
|
-
// return name.endsWith("%") && isNumber(name.slice(0, -1));
|
|
1348
|
-
// }
|
|
1349
|
-
function isFlex(dimension) {
|
|
1350
|
-
return "unit" in dimension && "fr" == dimension.unit.toLowerCase();
|
|
1351
|
-
}
|
|
1352
1228
|
function parseDimension(name) {
|
|
1353
1229
|
let index = name.length;
|
|
1354
1230
|
while (index--) {
|
|
@@ -1371,31 +1247,22 @@ function parseDimension(name) {
|
|
|
1371
1247
|
if (Number.isNaN(dimension.val)) {
|
|
1372
1248
|
return null;
|
|
1373
1249
|
}
|
|
1374
|
-
if (
|
|
1375
|
-
// @ts-ignore
|
|
1250
|
+
if (isAngleUnit(dimension.unit)) {
|
|
1376
1251
|
dimension.typ = EnumToken.AngleTokenType;
|
|
1377
1252
|
}
|
|
1378
|
-
else if (
|
|
1379
|
-
// @ts-ignore
|
|
1253
|
+
else if (isLengthUnit(dimension.unit)) {
|
|
1380
1254
|
dimension.typ = EnumToken.LengthTokenType;
|
|
1381
1255
|
}
|
|
1382
|
-
else if (
|
|
1383
|
-
// @ts-ignore
|
|
1256
|
+
else if (isTimeUnit(dimension.unit)) {
|
|
1384
1257
|
dimension.typ = EnumToken.TimeTokenType;
|
|
1385
1258
|
}
|
|
1386
|
-
else if (
|
|
1387
|
-
// @ts-ignore
|
|
1259
|
+
else if (isResolutionUnit(dimension.unit)) {
|
|
1388
1260
|
dimension.typ = EnumToken.ResolutionTokenType;
|
|
1389
|
-
// if (dimension.unit == "dppx") {
|
|
1390
|
-
// dimension.unit = "x";
|
|
1391
|
-
// }
|
|
1392
1261
|
}
|
|
1393
|
-
else if (
|
|
1394
|
-
// @ts-ignore
|
|
1262
|
+
else if (isFrequencyUnit(dimension.unit)) {
|
|
1395
1263
|
dimension.typ = EnumToken.FrequencyTokenType;
|
|
1396
1264
|
}
|
|
1397
|
-
else if (
|
|
1398
|
-
// @ts-ignore
|
|
1265
|
+
else if (isFlexUnit(dimension.unit)) {
|
|
1399
1266
|
dimension.typ = EnumToken.FlexTokenType;
|
|
1400
1267
|
}
|
|
1401
1268
|
return dimension;
|
|
@@ -1479,39 +1346,34 @@ function isWhiteSpace(codepoint) {
|
|
|
1479
1346
|
* @param value
|
|
1480
1347
|
* @returns
|
|
1481
1348
|
*/
|
|
1482
|
-
function length2Px(value) {
|
|
1349
|
+
function length2Px(arena, value) {
|
|
1483
1350
|
let result = null;
|
|
1484
|
-
|
|
1485
|
-
|
|
1351
|
+
const val = arena.numericValue[value];
|
|
1352
|
+
if (arena.dataTypes[value] == EnumToken.NumberTokenType) {
|
|
1353
|
+
result = val;
|
|
1486
1354
|
}
|
|
1487
1355
|
else {
|
|
1488
|
-
switch (
|
|
1356
|
+
switch (arena.strings[arena.unit[value]]) {
|
|
1489
1357
|
case "cm":
|
|
1490
|
-
|
|
1491
|
-
result = value.val * 37.8;
|
|
1358
|
+
result = val * 37.8;
|
|
1492
1359
|
break;
|
|
1493
1360
|
case "mm":
|
|
1494
|
-
|
|
1495
|
-
result = value.val * 3.78;
|
|
1361
|
+
result = val * 3.78;
|
|
1496
1362
|
break;
|
|
1497
1363
|
case "Q":
|
|
1498
|
-
|
|
1499
|
-
result = (value.val * 37.8) / 40;
|
|
1364
|
+
result = (val * 37.8) / 40;
|
|
1500
1365
|
break;
|
|
1501
1366
|
case "in":
|
|
1502
|
-
|
|
1503
|
-
result = value.val / 96;
|
|
1367
|
+
result = val / 96;
|
|
1504
1368
|
break;
|
|
1505
1369
|
case "pc":
|
|
1506
|
-
|
|
1507
|
-
result = value.val / 16;
|
|
1370
|
+
result = val / 16;
|
|
1508
1371
|
break;
|
|
1509
1372
|
case "pt":
|
|
1510
|
-
|
|
1511
|
-
result = (value.val * 4) / 3;
|
|
1373
|
+
result = (val * 4) / 3;
|
|
1512
1374
|
break;
|
|
1513
1375
|
case "px":
|
|
1514
|
-
result =
|
|
1376
|
+
result = val;
|
|
1515
1377
|
break;
|
|
1516
1378
|
}
|
|
1517
1379
|
}
|
|
@@ -1522,6 +1384,38 @@ function length2Px(value) {
|
|
|
1522
1384
|
* @param val
|
|
1523
1385
|
*/
|
|
1524
1386
|
function minifyNumber(val) {
|
|
1387
|
+
if (typeof val != "number") {
|
|
1388
|
+
return minifyNumberValue(val);
|
|
1389
|
+
}
|
|
1390
|
+
// the same numbers are rendered many times (0, 1, .5, 100, ...): the result only depends on the number
|
|
1391
|
+
let result = minifiedNumbers.get(val);
|
|
1392
|
+
if (result === undefined) {
|
|
1393
|
+
result = minifyNumberValue(val);
|
|
1394
|
+
if (minifiedNumbers.size >= minifiedNumbersSize) {
|
|
1395
|
+
minifiedNumbers.clear();
|
|
1396
|
+
}
|
|
1397
|
+
// 0 and -0 share the same key: both are minified to "0"
|
|
1398
|
+
minifiedNumbers.set(val, result);
|
|
1399
|
+
}
|
|
1400
|
+
return result;
|
|
1401
|
+
}
|
|
1402
|
+
/**
|
|
1403
|
+
* minified numbers, see {@link minifyNumber}
|
|
1404
|
+
*/
|
|
1405
|
+
const minifiedNumbers = new Map();
|
|
1406
|
+
/**
|
|
1407
|
+
* maximum number of minified numbers kept
|
|
1408
|
+
*/
|
|
1409
|
+
const minifiedNumbersSize = 4096;
|
|
1410
|
+
/**
|
|
1411
|
+
* 10 ** colorPrecision, see {@link toPrecisionValue}
|
|
1412
|
+
*/
|
|
1413
|
+
const colorPrecisionFactor = Math.pow(10, colorPrecision);
|
|
1414
|
+
/**
|
|
1415
|
+
* minify a number, see {@link minifyNumber}
|
|
1416
|
+
* @param val
|
|
1417
|
+
*/
|
|
1418
|
+
function minifyNumberValue(val) {
|
|
1525
1419
|
val = String(toPrecisionValue(val));
|
|
1526
1420
|
if (val === "0") {
|
|
1527
1421
|
return "0";
|
|
@@ -1539,7 +1433,7 @@ function minifyNumber(val) {
|
|
|
1539
1433
|
return val;
|
|
1540
1434
|
}
|
|
1541
1435
|
function toPrecisionValue(value, precision = colorPrecision) {
|
|
1542
|
-
const div = Math.pow(10, precision);
|
|
1436
|
+
const div = precision === colorPrecision ? colorPrecisionFactor : Math.pow(10, precision);
|
|
1543
1437
|
// @ts-ignore
|
|
1544
1438
|
value = Math.round(value * div) / div;
|
|
1545
1439
|
return Math.abs(value) < epsilon ? 0 : value;
|
|
@@ -1555,4 +1449,4 @@ function toPrecisionAngle(angle, precision = anglePrecision, correctValue = true
|
|
|
1555
1449
|
return angle;
|
|
1556
1450
|
}
|
|
1557
1451
|
|
|
1558
|
-
export { angleUnits, dimensionUnits, flexUnits, frequencyUnits,
|
|
1452
|
+
export { angleUnits, dimensionUnits, flexUnits, frequencyUnits, isAngleUnit, isColor, isDigit, isFlexUnit, isFrequencyUnit, isFunction, isHash, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isLengthUnit, isLetter, isNewLine, isNonPrintable, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolutionUnit, isTimeUnit, isWhiteSpace, length2Px, minifyNumber, parseColor, parseDimension, pseudoAliasMap, reduceColorStops, reduceConicColorStops, reducegradientBackgroundPosition, renamedStandardProperties, resolutionUnits, timeUnits, toPrecisionAngle, toPrecisionValue };
|