@tbela99/css-parser 1.0.0 → 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.
Files changed (86) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/README.md +16 -11
  3. package/dist/index-umd-web.js +3613 -1829
  4. package/dist/index.cjs +3611 -1827
  5. package/dist/index.d.ts +160 -50
  6. package/dist/lib/ast/expand.js +2 -1
  7. package/dist/lib/ast/features/calc.js +12 -1
  8. package/dist/lib/ast/features/inlinecssvariables.js +47 -24
  9. package/dist/lib/ast/features/prefix.js +117 -86
  10. package/dist/lib/ast/features/shorthand.js +29 -6
  11. package/dist/lib/ast/features/transform.js +10 -3
  12. package/dist/lib/ast/features/type.js +7 -0
  13. package/dist/lib/ast/math/expression.js +7 -1
  14. package/dist/lib/ast/math/math.js +6 -0
  15. package/dist/lib/ast/minify.js +165 -77
  16. package/dist/lib/ast/transform/compute.js +1 -0
  17. package/dist/lib/ast/transform/matrix.js +1 -0
  18. package/dist/lib/ast/types.js +17 -15
  19. package/dist/lib/ast/walk.js +33 -7
  20. package/dist/lib/fs/resolve.js +10 -0
  21. package/dist/lib/parser/declaration/list.js +48 -45
  22. package/dist/lib/parser/declaration/map.js +1 -0
  23. package/dist/lib/parser/declaration/set.js +2 -1
  24. package/dist/lib/parser/parse.js +364 -276
  25. package/dist/lib/parser/tokenize.js +147 -72
  26. package/dist/lib/parser/utils/declaration.js +4 -3
  27. package/dist/lib/parser/utils/type.js +2 -1
  28. package/dist/lib/renderer/color/a98rgb.js +2 -1
  29. package/dist/lib/renderer/color/color-mix.js +10 -7
  30. package/dist/lib/renderer/color/color.js +171 -153
  31. package/dist/lib/renderer/color/hex.js +2 -1
  32. package/dist/lib/renderer/color/hsl.js +2 -1
  33. package/dist/lib/renderer/color/hwb.js +2 -1
  34. package/dist/lib/renderer/color/lab.js +2 -1
  35. package/dist/lib/renderer/color/lch.js +2 -1
  36. package/dist/lib/renderer/color/oklab.js +2 -1
  37. package/dist/lib/renderer/color/oklch.js +2 -1
  38. package/dist/lib/renderer/color/p3.js +2 -1
  39. package/dist/lib/renderer/color/rec2020.js +2 -1
  40. package/dist/lib/renderer/color/relativecolor.js +17 -11
  41. package/dist/lib/renderer/color/rgb.js +4 -3
  42. package/dist/lib/renderer/color/srgb.js +18 -17
  43. package/dist/lib/renderer/color/utils/components.js +6 -5
  44. package/dist/lib/renderer/color/utils/constants.js +47 -3
  45. package/dist/lib/renderer/color/xyz.js +2 -1
  46. package/dist/lib/renderer/color/xyzd50.js +2 -1
  47. package/dist/lib/renderer/render.js +48 -20
  48. package/dist/lib/syntax/syntax.js +253 -140
  49. package/dist/lib/validation/at-rules/container.js +75 -97
  50. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  51. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  52. package/dist/lib/validation/at-rules/document.js +22 -27
  53. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  54. package/dist/lib/validation/at-rules/import.js +30 -81
  55. package/dist/lib/validation/at-rules/keyframes.js +18 -22
  56. package/dist/lib/validation/at-rules/layer.js +5 -5
  57. package/dist/lib/validation/at-rules/media.js +42 -52
  58. package/dist/lib/validation/at-rules/namespace.js +19 -23
  59. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  60. package/dist/lib/validation/at-rules/page.js +8 -7
  61. package/dist/lib/validation/at-rules/supports.js +73 -82
  62. package/dist/lib/validation/at-rules/when.js +32 -36
  63. package/dist/lib/validation/atrule.js +15 -14
  64. package/dist/lib/validation/config.js +24 -1
  65. package/dist/lib/validation/config.json.js +563 -63
  66. package/dist/lib/validation/parser/parse.js +196 -185
  67. package/dist/lib/validation/parser/types.js +1 -1
  68. package/dist/lib/validation/selector.js +3 -3
  69. package/dist/lib/validation/syntax.js +828 -0
  70. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  71. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  72. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  73. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  74. package/dist/lib/validation/syntaxes/keyframe-block-list.js +4 -3
  75. package/dist/lib/validation/syntaxes/keyframe-selector.js +15 -18
  76. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  77. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  78. package/dist/lib/validation/syntaxes/relative-selector.js +2 -1
  79. package/dist/lib/validation/syntaxes/url.js +18 -22
  80. package/dist/lib/validation/utils/list.js +2 -1
  81. package/dist/lib/validation/utils/whitespace.js +2 -1
  82. package/dist/node/index.js +4 -2
  83. package/dist/node/load.js +5 -0
  84. package/dist/web/index.js +4 -2
  85. package/dist/web/load.js +5 -0
  86. package/package.json +12 -11
@@ -1,10 +1,12 @@
1
- import { colorsFunc } from '../renderer/render.js';
2
1
  import { EnumToken } from '../ast/types.js';
3
2
  import '../ast/minify.js';
4
- import '../ast/walk.js';
3
+ import { walkValues, WalkerOptionEnum } from '../ast/walk.js';
5
4
  import '../parser/parse.js';
5
+ import '../parser/tokenize.js';
6
6
  import '../parser/utils/config.js';
7
- import { COLORS_NAMES } from '../renderer/color/utils/constants.js';
7
+ import { COLORS_NAMES, colorsFunc, funcLike, ColorKind } from '../renderer/color/utils/constants.js';
8
+ import { buildExpression } from '../ast/math/expression.js';
9
+ import '../renderer/sourcemap/lib/encode.js';
8
10
 
9
11
  // https://www.w3.org/TR/CSS21/syndata.html#syntax
10
12
  // https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token
@@ -32,12 +34,33 @@ const mediaTypes = ['all', 'print', 'screen',
32
34
  /* deprecated */
33
35
  'aural', 'braille', 'embossed', 'handheld', 'projection', 'tty', 'tv', 'speech'];
34
36
  // https://www.w3.org/TR/css-values-4/#math-function
35
- const mathFuncs = ['calc', 'clamp', 'min', 'max', 'round', 'mod', 'rem', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2', 'pow', 'sqrt', 'hypot', 'log', 'exp', 'abs', 'sign'];
37
+ const mathFuncs = ['minmax', 'repeat', 'fit-content', 'calc', 'clamp', 'min', 'max', 'round', 'mod', 'rem', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2', 'pow', 'sqrt', 'hypot', 'log', 'exp', 'abs', 'sign'];
38
+ const wildCardFuncs = ['var', 'env'];
36
39
  const pseudoElements = [':before', ':after', ':first-line', ':first-letter'];
37
- const webkitPseudoAliasMap = {
38
- '-webkit-autofill': 'autofill',
39
- '-webkit-any': 'is',
40
- '-moz-any': 'is',
40
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/WebKit_Extensions
41
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/Mozilla_Extensions
42
+ const pseudoAliasMap = {
43
+ '-ms-grid-columns': 'grid-template-columns',
44
+ '-ms-grid-rows': 'grid-template-rows',
45
+ '-ms-grid-row': 'grid-row-start',
46
+ '-ms-grid-column': 'grid-column-start',
47
+ '-ms-grid-row-align': 'align-self',
48
+ '-ms-grid-row-span': 'grid-row-end',
49
+ '-ms-grid-column-span': 'grid-column-end',
50
+ '-ms-grid-column-align': 'justify-self',
51
+ ':-ms-input-placeholder': '::placeholder',
52
+ '::-ms-input-placeholder': '::placeholder',
53
+ ':-moz-any()': ':is',
54
+ '-moz-user-modify': 'user-modify',
55
+ '-moz-background-clip': 'background-clip',
56
+ '-moz-background-origin': 'background-origin',
57
+ '-ms-input-placeholder': 'placeholder',
58
+ ':-webkit-autofill': ':autofill',
59
+ ':-webkit-any()': ':is',
60
+ '::-webkit-input-placeholder': '::placeholder',
61
+ '::-webkit-file-upload-button': '::file-selector-button',
62
+ '::-moz-placeholder': '::placeholder',
63
+ ':-webkit-any-link': ':any-link',
41
64
  '-webkit-border-after': 'border-block-end',
42
65
  '-webkit-border-after-color': 'border-block-end-color',
43
66
  '-webkit-border-after-style': 'border-block-end-style',
@@ -74,7 +97,75 @@ const webkitPseudoAliasMap = {
74
97
  '-webkit-padding-end': 'padding-inline-end',
75
98
  '-webkit-padding-start': 'padding-inline-start',
76
99
  '-webkit-min-device-pixel-ratio': 'min-resolution',
77
- '-webkit-max-device-pixel-ratio': 'max-resolution'
100
+ '-webkit-max-device-pixel-ratio': 'max-resolution',
101
+ '-webkit-font-smoothing': 'font-smooth',
102
+ '-webkit-line-clamp': 'line-clamp',
103
+ ':-webkit-autofill-strong-password': ':autofill',
104
+ ':-webkit-full-page-media': ':fullscreen',
105
+ ':-webkit-full-screen': ':fullscreen',
106
+ ':-webkit-full-screen-ancestor': ':fullscreen',
107
+ ':-webkit-full-screen-document': ':fullscreen',
108
+ ':-webkit-full-screen-controls-hidden': ':fullscreen',
109
+ '-moz-background-inline-policy': 'box-decoration-break',
110
+ '-moz-background-size': 'background-size',
111
+ '-moz-border-end': 'border-inline-end',
112
+ '-moz-border-end-color': 'border-inline-end-color',
113
+ '-moz-border-end-style': 'border-inline-end-style',
114
+ '-moz-border-end-width': 'border-inline-end-width',
115
+ '-moz-border-image': 'border-inline-end-width',
116
+ '-moz-border-start': 'border-inline-start',
117
+ '-moz-border-start-color': 'border-inline-start-color',
118
+ '-moz-border-start-style': 'border-inline-start-style',
119
+ '-moz-border-start-width': 'border-inline-start-width',
120
+ '-moz-column-count': 'column-count',
121
+ '-moz-column-fill': 'column-fill',
122
+ '-moz-column-gap': 'column-gap',
123
+ '-moz-column-width': 'column-width',
124
+ '-moz-column-rule': 'column-rule',
125
+ '-moz-column-rule-width': 'column-rule-width',
126
+ '-moz-column-rule-style': 'column-rule-style',
127
+ '-moz-column-rule-color': 'column-rule-color',
128
+ '-moz-margin-end': 'margin-inline-end',
129
+ '-moz-margin-start': 'margin-inline-start',
130
+ '-moz-opacity': 'opacity',
131
+ '-moz-outline': 'outline',
132
+ '-moz-outline-color': 'outline-color',
133
+ '-moz-outline-offset': 'outline-offset',
134
+ '-moz-outline-style': 'outline-style',
135
+ '-moz-outline-width': 'outline-width',
136
+ '-moz-padding-end': 'padding-inline-end',
137
+ '-moz-padding-start': 'padding-inline-start',
138
+ '-moz-tab-size': 'tab-size',
139
+ '-moz-text-align-last': 'text-align-last',
140
+ '-moz-text-decoration-color': 'text-decoration-color',
141
+ '-moz-text-decoration-line': 'text-decoration-line',
142
+ '-moz-text-decoration-style': 'text-decoration-style',
143
+ '-moz-transition': 'transition',
144
+ '-moz-transition-delay': 'transition-delay',
145
+ '-moz-transition-duration': 'transition-duration',
146
+ '-moz-transition-property': 'transition-property',
147
+ '-moz-transition-timing-function': 'transition-timing-function',
148
+ '-moz-user-select': 'user-select',
149
+ '-moz-initial': 'initial',
150
+ '-moz-linear-gradient()': 'linear-gradient',
151
+ '-moz-radial-gradient()': 'radial-gradient',
152
+ '-moz-element()': 'element',
153
+ '-moz-crisp-edges': 'crisp-edges',
154
+ '-moz-calc()': 'calc',
155
+ '-moz-min-content': 'min-content',
156
+ '-moz-fit-content': 'fit-content',
157
+ '-moz-max-content': 'max-content',
158
+ '-moz-available': 'stretch',
159
+ ':-moz-any-link': ':any-link',
160
+ ':-moz-full-screen': ':fullscreen',
161
+ ':-moz-full-screen-ancestor': ':fullscreen',
162
+ ':-moz-placeholder': ':placeholder-shown',
163
+ ':-moz-read-only': ':read-only',
164
+ ':-moz-read-write': ':read-write',
165
+ ':-moz-submit-invalid': ':invalid',
166
+ ':-moz-ui-invalid': ':user-invalid',
167
+ ':-moz-ui-valid': ':user-valid',
168
+ '::-moz-selection': '::selection',
78
169
  };
79
170
  // https://developer.mozilla.org/en-US/docs/Web/CSS/WebKit_Extensions
80
171
  // https://developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-scrollbar
@@ -408,158 +499,205 @@ function isHueInterpolationMethod(token) {
408
499
  }
409
500
  return ['shorter', 'longer', 'increasing', 'decreasing'].includes(token.val);
410
501
  }
502
+ function isIdentColor(token) {
503
+ return token.typ == EnumToken.ColorTokenType && [ColorKind.SYS, ColorKind.DPSYS, ColorKind.LIT].includes(token.kin) && isIdent(token.val);
504
+ }
411
505
  function isColor(token) {
412
- if (token.typ == EnumToken.ColorTokenType) {
413
- return true;
414
- }
415
506
  if (token.typ == EnumToken.IdenTokenType) {
416
507
  // named color
417
508
  return token.val.toLowerCase() in COLORS_NAMES;
418
509
  }
419
510
  let isLegacySyntax = false;
420
- if (token.typ == EnumToken.FunctionTokenType && token.chi.length > 0 && colorsFunc.includes(token.val)) {
421
- // @ts-ignore
422
- if (token.val == 'light-dark') {
423
- // @ts-ignore
424
- const children = token.chi.filter((t) => [EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.LiteralTokenType, EnumToken.ColorTokenType, EnumToken.FunctionTokenType, EnumToken.PercentageTokenType].includes(t.typ));
425
- if (children.length != 2) {
426
- return false;
427
- }
428
- if (isColor(children[0]) && isColor(children[1])) {
429
- return true;
430
- }
511
+ if (token.typ == EnumToken.FunctionTokenType) {
512
+ if (!colorsFunc.includes(token.val.toLowerCase())) {
513
+ return false;
431
514
  }
432
- // @ts-ignore
433
- if (token.val == 'color') {
515
+ if (token.chi.length > 0) {
434
516
  // @ts-ignore
435
- const children = token.chi.filter((t) => [EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.LiteralTokenType, EnumToken.ColorTokenType, EnumToken.FunctionTokenType, EnumToken.PercentageTokenType].includes(t.typ));
436
- const isRelative = children[0].typ == EnumToken.IdenTokenType && children[0].val == 'from';
437
- if (children.length < 4 || children.length > 8) {
438
- return false;
439
- }
440
- if (!isRelative && !isColorspace(children[0])) {
441
- return false;
442
- }
443
- for (let i = 1; i < children.length - 2; i++) {
444
- if (children[i].typ == EnumToken.IdenTokenType) {
445
- if (children[i].val != 'none' &&
446
- !(isRelative && ['alpha', 'r', 'g', 'b', 'x', 'y', 'z'].includes(children[i].val) || isColorspace(children[i]))) {
447
- return false;
448
- }
449
- }
450
- if (children[i].typ == EnumToken.FunctionTokenType && !mathFuncs.includes(children[i].val)) {
517
+ if (token.val == 'light-dark') {
518
+ // @ts-ignore
519
+ const children = token.chi.filter((t) => [EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.LiteralTokenType, EnumToken.ColorTokenType, EnumToken.FunctionTokenType, EnumToken.PercentageTokenType].includes(t.typ));
520
+ if (children.length != 2) {
451
521
  return false;
452
522
  }
523
+ if (isColor(children[0]) && isColor(children[1])) {
524
+ return true;
525
+ }
453
526
  }
454
- if (children.length == 4 || (isRelative && children.length == 6)) {
455
- return true;
527
+ // adding numbers and percentages is disallowed
528
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lch#defining_relative_color_output_channel_components
529
+ // @ts-ignore
530
+ for (const { value } of walkValues(token.chi, null, (node) => funcLike.includes(node.typ) ? WalkerOptionEnum.IgnoreChildren : null)) {
531
+ if (funcLike.includes(value.typ)) {
532
+ for (const { value: val } of walkValues([buildExpression(value.chi)])) {
533
+ if (val.typ == EnumToken.BinaryExpressionTokenType &&
534
+ (val.l.typ == EnumToken.PercentageTokenType || val.r.typ == EnumToken.PercentageTokenType) &&
535
+ ((val.r.typ == EnumToken.PercentageTokenType && val.op == EnumToken.Div) ||
536
+ ((val.op == EnumToken.Add || val.op == EnumToken.Sub) &&
537
+ val.l.typ != val.r.typ))) {
538
+ return false;
539
+ }
540
+ }
541
+ }
456
542
  }
457
- if (children.length == 8 || children.length == 6) {
458
- const sep = children.at(-2);
459
- const alpha = children.at(-1);
543
+ // @ts-ignore
544
+ if (token.val == 'color') {
460
545
  // @ts-ignore
461
- if ((children.length > 6 || !isRelative) && sep.typ != EnumToken.LiteralTokenType || sep.val != '/') {
546
+ const children = token.chi.filter((t) => [EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.LiteralTokenType, EnumToken.ColorTokenType, EnumToken.FunctionTokenType, EnumToken.PercentageTokenType].includes(t.typ));
547
+ const isRelative = children[0].typ == EnumToken.IdenTokenType && children[0].val == 'from';
548
+ if (children.length < 4 || children.length > 8) {
462
549
  return false;
463
550
  }
464
- if (alpha.typ == EnumToken.IdenTokenType && alpha.val != 'none') {
551
+ if (!isRelative && !isColorspace(children[0])) {
465
552
  return false;
466
553
  }
467
- else {
468
- // @ts-ignore
469
- if (alpha.typ == EnumToken.PercentageTokenType) {
470
- if (+alpha.val < 0 || +alpha.val > 100) {
554
+ for (let i = 1; i < children.length - 2; i++) {
555
+ if (children[i].typ == EnumToken.IdenTokenType) {
556
+ if (children[i].val != 'none' &&
557
+ !(isRelative && ['alpha', 'r', 'g', 'b', 'x', 'y', 'z'].includes(children[i].val) || isColorspace(children[i]))) {
471
558
  return false;
472
559
  }
473
560
  }
474
- else if (alpha.typ == EnumToken.NumberTokenType) {
475
- if (+alpha.val < 0 || +alpha.val > 1) {
476
- return false;
477
- }
561
+ if (children[i].typ == EnumToken.FunctionTokenType && !mathFuncs.includes(children[i].val)) {
562
+ return false;
478
563
  }
479
564
  }
480
- }
481
- return true;
482
- }
483
- else { // @ts-ignore
484
- if (token.val == 'color-mix') {
485
- // @ts-ignore
486
- const children = token.chi.reduce((acc, t) => {
487
- if (t.typ == EnumToken.CommaTokenType) {
488
- acc.push([]);
489
- }
490
- else {
491
- if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
492
- acc[acc.length - 1].push(t);
493
- }
565
+ if (children.length == 4 || (isRelative && children.length == 6)) {
566
+ return true;
567
+ }
568
+ if (children.length == 8 || children.length == 6) {
569
+ const sep = children.at(-2);
570
+ const alpha = children.at(-1);
571
+ // @ts-ignore
572
+ if ((children.length > 6 || !isRelative) && sep.typ != EnumToken.LiteralTokenType || sep.val != '/') {
573
+ return false;
494
574
  }
495
- return acc;
496
- }, [[]]);
497
- if (children.length == 3) {
498
- if (children[0].length > 3 ||
499
- children[0][0].typ != EnumToken.IdenTokenType ||
500
- children[0][0].val != 'in' ||
501
- !isColorspace(children[0][1]) ||
502
- (children[0].length == 3 && !isHueInterpolationMethod(children[0][2])) ||
503
- children[1].length > 2 ||
504
- children[1][0].typ != EnumToken.ColorTokenType ||
505
- children[2].length > 2 ||
506
- children[2][0].typ != EnumToken.ColorTokenType) {
575
+ if (alpha.typ == EnumToken.IdenTokenType && alpha.val != 'none') {
507
576
  return false;
508
577
  }
509
- if (children[1].length == 2) {
510
- if (!(children[1][1].typ == EnumToken.PercentageTokenType || (children[1][1].typ == EnumToken.NumberTokenType && children[1][1].val == '0'))) {
511
- return false;
578
+ else {
579
+ // @ts-ignore
580
+ if (alpha.typ == EnumToken.PercentageTokenType) {
581
+ if (+alpha.val < 0 || +alpha.val > 100) {
582
+ return false;
583
+ }
512
584
  }
513
- }
514
- if (children[2].length == 2) {
515
- if (!(children[2][1].typ == EnumToken.PercentageTokenType || (children[2][1].typ == EnumToken.NumberTokenType && children[2][1].val == '0'))) {
516
- return false;
585
+ else if (alpha.typ == EnumToken.NumberTokenType) {
586
+ if (+alpha.val < 0 || +alpha.val > 1) {
587
+ return false;
588
+ }
517
589
  }
518
590
  }
519
- return true;
520
591
  }
521
- return false;
592
+ return true;
522
593
  }
523
- else {
524
- const keywords = ['from', 'none'];
525
- // @ts-ignore
526
- if (['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch'].includes(token.val)) {
594
+ else { // @ts-ignore
595
+ if (token.val == 'color-mix') {
527
596
  // @ts-ignore
528
- keywords.push('alpha', ...token.val.slice(-3).split(''));
529
- }
530
- // @ts-ignore
531
- for (const v of token.chi) {
532
- if (v.typ == EnumToken.CommaTokenType) {
533
- isLegacySyntax = true;
534
- }
535
- if (v.typ == EnumToken.IdenTokenType) {
536
- if (!(keywords.includes(v.val) || v.val.toLowerCase() in COLORS_NAMES)) {
597
+ const children = token.chi.reduce((acc, t) => {
598
+ if (t.typ == EnumToken.CommaTokenType) {
599
+ acc.push([]);
600
+ }
601
+ else {
602
+ if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
603
+ acc[acc.length - 1].push(t);
604
+ }
605
+ }
606
+ return acc;
607
+ }, [[]]);
608
+ if (children.length == 3) {
609
+ if (children[0].length > 3 ||
610
+ children[0][0].typ != EnumToken.IdenTokenType ||
611
+ children[0][0].val != 'in' ||
612
+ !isColorspace(children[0][1]) ||
613
+ (children[0].length == 3 && !isHueInterpolationMethod(children[0][2])) ||
614
+ children[1].length > 2 ||
615
+ children[1][0].typ != EnumToken.ColorTokenType ||
616
+ children[2].length > 2 ||
617
+ children[2][0].typ != EnumToken.ColorTokenType) {
537
618
  return false;
538
619
  }
539
- if (keywords.includes(v.val)) {
540
- if (isLegacySyntax) {
620
+ if (children[1].length == 2) {
621
+ if (!(children[1][1].typ == EnumToken.PercentageTokenType || (children[1][1].typ == EnumToken.NumberTokenType && children[1][1].val == '0'))) {
541
622
  return false;
542
623
  }
543
- // @ts-ignore
544
- if (v.val == 'from' && ['rgba', 'hsla'].includes(token.val)) {
624
+ }
625
+ if (children[2].length == 2) {
626
+ if (!(children[2][1].typ == EnumToken.PercentageTokenType || (children[2][1].typ == EnumToken.NumberTokenType && children[2][1].val == '0'))) {
545
627
  return false;
546
628
  }
547
629
  }
548
- continue;
630
+ return true;
549
631
  }
550
- if (v.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(v.val) || v.val == 'var' || colorsFunc.includes(v.val))) {
551
- continue;
632
+ return false;
633
+ }
634
+ else {
635
+ const keywords = ['from', 'none'];
636
+ // @ts-ignore
637
+ if (['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch'].includes(token.val)) {
638
+ // @ts-ignore
639
+ keywords.push('alpha', ...token.val.slice(-3).split(''));
552
640
  }
553
- if (![EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
554
- return false;
641
+ // @ts-ignore
642
+ for (const v of token.chi) {
643
+ if (v.typ == EnumToken.CommaTokenType) {
644
+ isLegacySyntax = true;
645
+ }
646
+ if (v.typ == EnumToken.IdenTokenType) {
647
+ if (!(keywords.includes(v.val) || v.val.toLowerCase() in COLORS_NAMES)) {
648
+ return false;
649
+ }
650
+ if (keywords.includes(v.val)) {
651
+ if (isLegacySyntax) {
652
+ return false;
653
+ }
654
+ // @ts-ignore
655
+ if (v.val == 'from' && ['rgba', 'hsla'].includes(token.val)) {
656
+ return false;
657
+ }
658
+ }
659
+ continue;
660
+ }
661
+ if (v.typ == EnumToken.FunctionTokenType && (mathFuncs.includes(v.val) || v.val == 'var' || colorsFunc.includes(v.val))) {
662
+ continue;
663
+ }
664
+ if (![EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType].includes(v.typ)) {
665
+ return false;
666
+ }
555
667
  }
556
668
  }
557
669
  }
670
+ return true;
558
671
  }
559
- return true;
560
672
  }
561
673
  return false;
562
674
  }
675
+ function parseColor(token) {
676
+ // @ts-ignore
677
+ token.typ = EnumToken.ColorTokenType;
678
+ // @ts-ignore
679
+ token.kin = ColorKind[token.val.replaceAll('-', '_').toUpperCase()];
680
+ // @ts-ignore
681
+ if (token.chi[0].typ == EnumToken.IdenTokenType) {
682
+ // @ts-ignore
683
+ if (token.chi[0].val == 'from') {
684
+ // @ts-ignore
685
+ token.cal = 'rel';
686
+ }
687
+ // @ts-ignore
688
+ else if (token.val == 'color-mix' && token.chi[0].val == 'in') {
689
+ // @ts-ignore
690
+ token.cal = 'mix';
691
+ }
692
+ else { // @ts-ignore
693
+ if (token.val == 'color') {
694
+ // @ts-ignore
695
+ token.cal = 'col';
696
+ }
697
+ }
698
+ }
699
+ return token;
700
+ }
563
701
  function isLetter(codepoint) {
564
702
  // lowercase
565
703
  return (codepoint >= 0x61 && codepoint <= 0x7a) ||
@@ -789,31 +927,6 @@ function isHexColor(name) {
789
927
  }
790
928
  return true;
791
929
  }
792
- /*
793
- export function isHexDigit(name: string): boolean {
794
-
795
- if (name.length || name.length > 6) {
796
-
797
- return false;
798
- }
799
-
800
- for (let chr of name) {
801
-
802
- let codepoint = <number>chr.charCodeAt(0);
803
-
804
- if (!isDigit(codepoint) &&
805
- // A F
806
- !(codepoint >= 0x41 && codepoint <= 0x46) &&
807
- // a f
808
- !(codepoint >= 0x61 && codepoint <= 0x66)) {
809
-
810
- return false;
811
- }
812
- }
813
-
814
- return true;
815
- }
816
- */
817
930
  function isFunction(name) {
818
931
  return name.endsWith('(') && isIdent(name.slice(0, -1));
819
932
  }
@@ -830,4 +943,4 @@ function isWhiteSpace(codepoint) {
830
943
  codepoint == 0xa || codepoint == 0xc || codepoint == 0xd;
831
944
  }
832
945
 
833
- export { colorFontTech, fontFeaturesTech, fontFormat, isAngle, isAtKeyword, isColor, isColorspace, isDigit, isDimension, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, mathFuncs, mediaTypes, mozExtensions, parseDimension, pseudoElements, transformFunctions, webkitExtensions, webkitPseudoAliasMap };
946
+ export { colorFontTech, fontFeaturesTech, fontFormat, isAngle, isAtKeyword, isColor, isColorspace, isDigit, isDimension, isFlex, isFrequency, isFunction, isHash, isHexColor, isHueInterpolationMethod, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isNewLine, isNonPrintable, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, mathFuncs, mediaTypes, mozExtensions, parseColor, parseDimension, pseudoAliasMap, pseudoElements, transformFunctions, webkitExtensions, wildCardFuncs };