@tbela99/css-parser 1.8.2 → 2.0.1

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 (105) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27383 -24083
  4. package/dist/index.d.ts +2737 -2200
  5. package/dist/package.json.js +2 -2
  6. package/dist/src/data/properties.json.js +1 -1
  7. package/dist/src/data/validation.json.js +1 -1
  8. package/dist/src/lib/ast/arena-node.js +548 -0
  9. package/dist/src/lib/ast/arena.js +3198 -0
  10. package/dist/src/lib/ast/cache.js +333 -0
  11. package/dist/src/lib/ast/features/calc.js +85 -76
  12. package/dist/src/lib/ast/features/if.js +155 -115
  13. package/dist/src/lib/ast/features/index.js +1 -1
  14. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  15. package/dist/src/lib/ast/features/prefix.js +184 -183
  16. package/dist/src/lib/ast/features/shorthand.js +40 -46
  17. package/dist/src/lib/ast/features/transform.js +24 -24
  18. package/dist/src/lib/ast/features/type.js +1 -1
  19. package/dist/src/lib/ast/math/expression.js +458 -543
  20. package/dist/src/lib/ast/math/math.js +19 -41
  21. package/dist/src/lib/ast/minify.js +590 -647
  22. package/dist/src/lib/ast/transform/compute.js +82 -85
  23. package/dist/src/lib/ast/transform/matrix.js +21 -38
  24. package/dist/src/lib/ast/transform/minify.js +82 -193
  25. package/dist/src/lib/ast/transform/perspective.js +2 -3
  26. package/dist/src/lib/ast/transform/rotate.js +1 -1
  27. package/dist/src/lib/ast/transform/scale.js +1 -1
  28. package/dist/src/lib/ast/transform/skew.js +1 -1
  29. package/dist/src/lib/ast/transform/translate.js +1 -1
  30. package/dist/src/lib/ast/transform/utils.js +2 -2
  31. package/dist/src/lib/ast/types.js +263 -258
  32. package/dist/src/lib/ast/walk.js +246 -610
  33. package/dist/src/lib/fs/resolve.js +1 -1
  34. package/dist/src/lib/parser/declaration/list.js +223 -139
  35. package/dist/src/lib/parser/declaration/map.js +251 -238
  36. package/dist/src/lib/parser/declaration/set.js +74 -66
  37. package/dist/src/lib/parser/linesmap.js +1 -1
  38. package/dist/src/lib/parser/parse.js +1379 -1658
  39. package/dist/src/lib/parser/source.js +7 -4
  40. package/dist/src/lib/parser/tokenize.js +406 -199
  41. package/dist/src/lib/parser/utils/angle.js +11 -13
  42. package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
  43. package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
  44. package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
  45. package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
  46. package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
  47. package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
  48. package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
  49. package/dist/src/lib/parser/utils/at-rule.js +5 -5
  50. package/dist/src/lib/parser/utils/base64.js +2 -2
  51. package/dist/src/lib/parser/utils/cache.js +1 -1
  52. package/dist/src/lib/parser/utils/config.js +1 -1
  53. package/dist/src/lib/parser/utils/declaration.js +233 -234
  54. package/dist/src/lib/parser/utils/eq.js +50 -3
  55. package/dist/src/lib/parser/utils/hash.js +2 -35
  56. package/dist/src/lib/parser/utils/selector.js +309 -387
  57. package/dist/src/lib/parser/utils/text.js +18 -4
  58. package/dist/src/lib/parser/utils/token.js +16 -69
  59. package/dist/src/lib/parser/utils/type.js +37 -24
  60. package/dist/src/lib/printer/render.js +965 -511
  61. package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
  62. package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
  63. package/dist/src/lib/syntax/color/a98rgb.js +1 -1
  64. package/dist/src/lib/syntax/color/alpha.js +36 -37
  65. package/dist/src/lib/syntax/color/cmyk.js +35 -52
  66. package/dist/src/lib/syntax/color/color-mix.js +96 -132
  67. package/dist/src/lib/syntax/color/color.js +222 -231
  68. package/dist/src/lib/syntax/color/hex.js +45 -55
  69. package/dist/src/lib/syntax/color/hsl.js +54 -62
  70. package/dist/src/lib/syntax/color/hwb.js +60 -68
  71. package/dist/src/lib/syntax/color/lab.js +59 -67
  72. package/dist/src/lib/syntax/color/lch.js +59 -67
  73. package/dist/src/lib/syntax/color/oklab.js +60 -68
  74. package/dist/src/lib/syntax/color/oklch.js +60 -67
  75. package/dist/src/lib/syntax/color/p3.js +1 -1
  76. package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
  77. package/dist/src/lib/syntax/color/rec2020.js +1 -1
  78. package/dist/src/lib/syntax/color/relative-color.js +94 -144
  79. package/dist/src/lib/syntax/color/rgb.js +54 -57
  80. package/dist/src/lib/syntax/color/srgb.js +55 -53
  81. package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
  82. package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
  83. package/dist/src/lib/syntax/color/utils/components.js +19 -20
  84. package/dist/src/lib/syntax/color/utils/distance.js +15 -13
  85. package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
  86. package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
  87. package/dist/src/lib/syntax/color/xyz.js +1 -1
  88. package/dist/src/lib/syntax/color/xyzd50.js +1 -1
  89. package/dist/src/lib/syntax/constants.js +2 -16
  90. package/dist/src/lib/syntax/syntax.js +301 -407
  91. package/dist/src/lib/validation/config.js +1 -1
  92. package/dist/src/lib/validation/match.js +959 -723
  93. package/dist/src/lib/validation/parser/parse.js +230 -112
  94. package/dist/src/lib/validation/parser/typedef.js +1 -1
  95. package/dist/src/lib/validation/utils/list.js +27 -12
  96. package/dist/src/node.js +12 -72
  97. package/dist/src/types.js +1 -1
  98. package/dist/src/utils/sync.js +9 -6
  99. package/dist/src/web.js +11 -73
  100. package/package.json +9 -10
  101. package/dist/index.cjs +0 -35870
  102. package/dist/src/lib/ast/clone.js +0 -51
  103. package/dist/src/lib/ast/expand.js +0 -311
  104. package/dist/src/lib/ast/find.js +0 -192
  105. package/dist/src/lib/ast/node.js +0 -59
@@ -2,11 +2,11 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
- import { PropertyList } from '../../parser/declaration/list.js';
9
- import { EnumToken } from '../types.js';
8
+ import { DeclarationMemo, PropertyList } from '../../parser/declaration/list.js';
9
+ import { EnumToken, EnumAstNodeStatus } from '../types.js';
10
10
  import { FeatureWalkMode } from './type.js';
11
11
 
12
12
  class ComputeShorthandFeature {
@@ -27,65 +27,59 @@ class ComputeShorthandFeature {
27
27
  options.features.push(new ComputeShorthandFeature(options));
28
28
  }
29
29
  }
30
- run(ast, options) {
31
- if (!("chi" in ast || ast.chi?.length == 0)) {
30
+ run(arena, ast, options, _parent, context) {
31
+ if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren) || arena.firstChild[ast] == 0) {
32
32
  return null;
33
33
  }
34
- // @ts-ignore
35
- const j = ast.chi.length;
36
- let k = 0;
37
- let l;
38
- const rules = [];
39
- const declarations = [];
34
+ // the keys of the declarations are shared by the rules of the minification
35
+ const memo = context == null ? null : (context.declarations ??= new DeclarationMemo());
36
+ const list = new PropertyList(arena, options, memo);
40
37
  let doNotSort = false;
41
- if (ast.typ == EnumToken.AtRuleNodeType) {
42
- switch (ast.nam) {
38
+ let hasDeclarations = false;
39
+ if (arena.dataTypes[ast] == EnumToken.AtRuleNodeType) {
40
+ switch (arena.strings[arena.name[ast]]) {
43
41
  case "function":
44
42
  case "mixin":
45
43
  doNotSort = true;
46
44
  break;
47
45
  }
48
46
  }
49
- // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
50
- if (doNotSort) {
51
- for (const node of ast.chi) {
52
- declarations.push(node);
47
+ // the declarations (with their leading comments) are moved into the property list,
48
+ // the rules (with their leading comments) stay in place
49
+ let child = arena.firstChild[ast];
50
+ while (child != 0) {
51
+ let last = child;
52
+ if (!doNotSort) {
53
+ // capture comments with the next node
54
+ while (arena.nextSibling[last] != 0 && arena.dataTypes[last] == EnumToken.CommentNodeType) {
55
+ last = arena.nextSibling[last];
56
+ }
53
57
  }
54
- }
55
- else {
56
- // @ts-ignore
57
- for (; k < j; k++) {
58
- l = k;
59
- // capture comments with the next token
60
- while (l + 1 < j) {
61
- // @ts-ignore
62
- const node = ast.chi[l];
63
- if (node.typ == EnumToken.CommentNodeType) {
64
- l++;
65
- continue;
66
- }
67
- break;
58
+ const next = arena.nextSibling[last];
59
+ // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
60
+ if (doNotSort || arena.dataTypes[last] == EnumToken.DeclarationNodeType) {
61
+ hasDeclarations = true;
62
+ for (let node = child; node != next;) {
63
+ const nextNode = arena.nextSibling[node];
64
+ arena.detach(node);
65
+ list.addDeclaration(node);
66
+ node = nextNode;
68
67
  }
69
- // @ts-ignore
70
- const node = ast.chi[l];
71
- if (node.typ == EnumToken.DeclarationNodeType) {
72
- for (let m = k; m <= l; m++) {
73
- declarations.push(ast.chi[m]);
74
- }
68
+ }
69
+ child = next;
70
+ }
71
+ if (hasDeclarations) {
72
+ // the declarations are inserted before the rules
73
+ const first = arena.firstChild[ast];
74
+ for (const node of list) {
75
+ if (first == 0) {
76
+ arena.appendChild(ast, node);
75
77
  }
76
78
  else {
77
- for (let m = k; m <= l; m++) {
78
- rules.push(ast.chi[m]);
79
- }
79
+ arena.insertBefore(first, node);
80
80
  }
81
- k = l;
82
81
  }
83
82
  }
84
- if (declarations.length > 0) {
85
- ast.chi.length = 0;
86
- // @ts-expect-error
87
- ast.chi.push(...new PropertyList(options).add(declarations), ...rules);
88
- }
89
83
  return ast;
90
84
  }
91
85
  }
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken, EnumAstNodeStatus } from '../types.js';
@@ -10,8 +10,11 @@ import { compute } from '../transform/compute.js';
10
10
  import { filterValues, renderValue } from '../../printer/render.js';
11
11
  import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
12
12
  import { FeatureWalkMode } from './type.js';
13
- import { STATE } from '../../syntax/constants.js';
14
13
 
14
+ /**
15
+ * transform property and its prefixed versions
16
+ */
17
+ const transformProperty = /^(-[a-z]+-)?transform$/;
15
18
  class TransformCssFeature {
16
19
  accept = new Set([
17
20
  EnumToken.RuleNodeType,
@@ -31,52 +34,49 @@ class TransformCssFeature {
31
34
  options.features.push(new TransformCssFeature());
32
35
  }
33
36
  }
34
- run(ast) {
35
- if (ast.chi == null) {
37
+ run(arena, ast) {
38
+ if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
36
39
  return null;
37
40
  }
38
- let i = 0;
39
41
  let node;
40
- // @ts-ignore
41
- for (; i < ast.chi.length; i++) {
42
- // @ts-ignore
43
- node = ast.chi[i];
44
- if (node[STATE] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
42
+ for (node = arena.firstChild[ast]; node != 0; node = arena.nextSibling[node]) {
43
+ if (arena.flags[node] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.ValidationFailed)) {
45
44
  continue;
46
45
  }
47
- if (node.typ != EnumToken.DeclarationNodeType ||
48
- !node.nam.match(/^(-[a-z]+-)?transform$/)) {
46
+ if (arena.dataTypes[node] != EnumToken.DeclarationNodeType ||
47
+ !transformProperty.test(arena.strings[arena.name[node]])) {
49
48
  continue;
50
49
  }
51
50
  const children = [];
52
- for (const child of node.val) {
53
- children.push(child.typ == EnumToken.TransformFunctionTokenType
54
- ? minifyTransformFunctions(child)
51
+ for (const child of arena.tokens(node)) {
52
+ children.push(arena.dataTypes[child] == EnumToken.TransformFunctionTokenType
53
+ ? minifyTransformFunctions(arena, child)
55
54
  : child);
56
55
  }
57
- let { matrix, cumulative, minified } = compute(children) ?? {
56
+ let { matrix, cumulative, minified } = compute(arena, children) ?? {
58
57
  matrix: null,
59
58
  cumulative: null,
60
59
  minified: null,
61
60
  };
62
61
  if (matrix == null || cumulative == null || minified == null) {
63
- node.val = children.map((t) => t.typ == EnumToken.TransformFunctionTokenType ? minifyTransformFunctions(t) : t);
62
+ arena.setTokenList(node, children);
64
63
  continue;
65
64
  }
66
- let r = [filterValues(children)];
67
- if (eqMatrix(matrix, cumulative)) {
65
+ let r = [filterValues(arena, children)];
66
+ if (eqMatrix(arena, matrix, cumulative)) {
68
67
  r.push(cumulative);
69
68
  }
70
- if (eqMatrix(matrix, minified)) {
69
+ if (eqMatrix(arena, matrix, minified)) {
71
70
  r.push(minified);
72
71
  }
73
- const l = renderValue(matrix).length;
74
- node.val = r.reduce((acc, curr) => {
75
- if (curr.reduce((acc, t) => acc + renderValue(t), "").length < l) {
72
+ const l = renderValue(arena, matrix).length;
73
+ // inlined: arena.setTokens(node, ...r.reduce((acc: Token[], curr: Token[]): Token[] => { if (curr.reduce((acc: string, t: Token) => acc + renderValue(arena, t), "").length < l) { return curr; } return acc; }, [matrix]))
74
+ arena.setTokenList(node, r.reduce((acc, curr) => {
75
+ if (curr.reduce((acc, t) => acc + renderValue(arena, t), "").length < l) {
76
76
  return curr;
77
77
  }
78
78
  return acc;
79
- }, [matrix]);
79
+ }, [matrix]));
80
80
  }
81
81
  return null;
82
82
  }
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.1
6
6
  License: MIT
7
7
  */
8
8
  /**