@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.
Files changed (105) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27382 -24086
  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/features/calc.js +85 -76
  11. package/dist/src/lib/ast/features/if.js +155 -115
  12. package/dist/src/lib/ast/features/index.js +1 -1
  13. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  14. package/dist/src/lib/ast/features/prefix.js +184 -183
  15. package/dist/src/lib/ast/features/shorthand.js +40 -46
  16. package/dist/src/lib/ast/features/transform.js +24 -24
  17. package/dist/src/lib/ast/features/type.js +1 -1
  18. package/dist/src/lib/ast/fingerprint.js +333 -0
  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 -1662
  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 -35874
  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,61 +2,61 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.1
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
- import { EnumToken } from '../types.js';
9
- import { walkValues } from '../walk.js';
8
+ import { EnumToken, EnumAstNodeStatus } from '../types.js';
10
9
  import { renderValue } from '../../printer/render.js';
11
10
  import { splitRule } from '../minify.js';
12
11
  import { FeatureWalkMode } from './type.js';
13
- import { RAW, mathFuncs } from '../../syntax/constants.js';
12
+ import { mathFuncs } from '../../syntax/constants.js';
13
+ import { TokenField } from '../arena.js';
14
+ import { ValueWalker } from '../walk.js';
14
15
 
15
- function inlineExpression(token) {
16
+ /**
17
+ * inline a binary expression into a parens token: (l op r)
18
+ * @param arena
19
+ * @param token
20
+ */
21
+ function inlineExpression(arena, token) {
16
22
  const result = [];
17
- if (token.typ == EnumToken.BinaryExpressionTokenType) {
18
- const chi = inlineExpression(token.l);
19
- chi.push({ typ: token.op });
20
- for (const child of inlineExpression(token.r)) {
23
+ if (arena.dataTypes[token] == EnumToken.BinaryExpressionTokenType) {
24
+ const chi = inlineExpression(arena, arena.field(token, TokenField.L));
25
+ chi.push(arena.createToken(arena.numericValue[token]));
26
+ for (const child of inlineExpression(arena, arena.field(token, TokenField.R))) {
21
27
  chi.push(child);
22
28
  }
23
- result.push({
24
- typ: EnumToken.ParensTokenType,
25
- chi,
26
- });
29
+ result.push(arena.createList(EnumToken.ParensTokenType, chi));
27
30
  }
28
31
  else {
29
32
  result.push(token);
30
33
  }
31
34
  return result;
32
35
  }
33
- function replace(node, variableScope) {
34
- for (const { value, parent: parentValue } of walkValues(node.val)) {
35
- if (value.typ == EnumToken.BinaryExpressionTokenType && parentValue != null && "chi" in parentValue) {
36
- // @ts-ignore
37
- parentValue.chi.splice(parentValue.chi.indexOf(value), 1, ...inlineExpression(value));
36
+ function replace(arena, node, variableScope) {
37
+ // inlined: arena.walkValues(node)
38
+ for (const value of new ValueWalker(arena, node)) {
39
+ // the parent of a detached token is the declaration
40
+ const parentValue = arena.getValueParent(value) || node;
41
+ if (arena.dataTypes[value] == EnumToken.BinaryExpressionTokenType &&
42
+ parentValue != null &&
43
+ parentValue != node &&
44
+ // the token belongs to the parent token list (function arguments)
45
+ arena.parents[value] == parentValue) {
46
+ arena.replaceToken(value, ...inlineExpression(arena, value));
38
47
  }
39
48
  }
40
- for (const { value, parent: parentValue } of walkValues(node.val)) {
41
- if (value.typ == EnumToken.MathFunctionTokenType ||
42
- (value.typ == EnumToken.WildCardFunctionTokenType && value.val == "var")) {
43
- if (value.chi.length == 1 &&
44
- value.chi[0].typ == EnumToken.DashedIdenTokenType) {
45
- const info = (variableScope.get(value.chi[0].val));
46
- if (info?.replaceable) {
47
- if (parentValue != null) {
48
- let i = 0;
49
- const source = parentValue.typ == EnumToken.DeclarationNodeType ? parentValue.val : parentValue.chi;
50
- for (; i < source.length; i++) {
51
- if (source[i] == value) {
52
- source.splice(i, 1, ...info.node.val);
53
- break;
54
- }
55
- }
56
- }
57
- else {
58
- node.val = info.node.val.slice();
59
- }
49
+ // inlined: arena.walkValues(node)
50
+ for (const value of new ValueWalker(arena, node)) {
51
+ const typ = arena.dataTypes[value];
52
+ if (typ == EnumToken.MathFunctionTokenType ||
53
+ (typ == EnumToken.WildCardFunctionTokenType && arena.strings[arena.stringValue[value]] == "var")) {
54
+ const first = arena.firstToken[value];
55
+ if (first != 0 && arena.nextSibling[first] == 0 && arena.dataTypes[first] == EnumToken.DashedIdenTokenType) {
56
+ const info = (variableScope.get(arena.strings[arena.stringValue[first]]));
57
+ // the variable declaration may have been released
58
+ if (info?.replaceable && arena.name[info.node] != 0 && arena.parents[value] != 0) {
59
+ arena.replaceToken(value, ...arena.tokens(info.node).map((t) => arena.clone(t, true)));
60
60
  }
61
61
  }
62
62
  }
@@ -76,78 +76,71 @@ class InlineCssVariablesFeature {
76
76
  options.features.push(new InlineCssVariablesFeature());
77
77
  }
78
78
  }
79
- run(ast, options = {}, parent, context) {
80
- if (!("chi" in ast)) {
79
+ run(arena, ast, _options = {}, parent, context) {
80
+ if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren)) {
81
81
  return null;
82
82
  }
83
83
  if (!("variableScope" in context)) {
84
84
  context.variableScope = new Map();
85
85
  }
86
86
  // [':root', 'html']
87
- const isRoot = parent.typ == EnumToken.StyleSheetNodeType &&
88
- ast.typ == EnumToken.RuleNodeType &&
89
- (ast[RAW] ?? splitRule(ast.sel)).some((segment) => segment.some((s) => s == ":root" || s == "html"));
87
+ const isRoot = arena.dataTypes[parent] == EnumToken.StyleSheetNodeType &&
88
+ arena.dataTypes[ast] == EnumToken.RuleNodeType &&
89
+ (arena.raw[ast] ?? splitRule(arena.strings[arena.name[ast]])).some((segment) => segment.some((s) => s == ":root" || s == "html"));
90
90
  const variableScope = context.variableScope;
91
- // @ts-ignore
92
- for (const node of ast.chi) {
93
- if (node.typ !== EnumToken.DeclarationNodeType) {
91
+ for (let node = arena.firstChild[ast]; node != 0; node = arena.nextSibling[node]) {
92
+ if (arena.dataTypes[node] !== EnumToken.DeclarationNodeType) {
94
93
  continue;
95
94
  }
95
+ const name = arena.strings[arena.name[node]];
96
96
  // css variable
97
- if (node.nam.startsWith("--")) {
98
- if (!variableScope.has(node.val)) {
99
- const info = {
100
- globalScope: isRoot,
101
- // @ts-ignore
102
- parent: new Set(),
103
- declarationCount: 1,
104
- replaceable: isRoot,
105
- node: node,
106
- values: structuredClone(node.val),
107
- };
108
- info.parent.add(ast);
109
- variableScope.set(node.nam, info);
110
- let recursive = false;
111
- for (const { value } of walkValues(node.val)) {
112
- if (value?.typ == EnumToken.MathFunctionTokenDefType ||
113
- (value?.typ == EnumToken.WildCardFunctionTokenType &&
114
- (mathFuncs.includes(value.val) ||
115
- value.val === "var"))) {
116
- recursive = true;
117
- break;
118
- }
119
- }
120
- if (recursive) {
121
- replace(node, variableScope);
97
+ if (name.startsWith("--")) {
98
+ // the last declaration wins
99
+ const info = {
100
+ globalScope: isRoot,
101
+ // @ts-ignore
102
+ parent: new Set(),
103
+ declarationCount: 1,
104
+ replaceable: isRoot,
105
+ node: node,
106
+ values: arena.tokens(node).map((t) => arena.clone(t, true)),
107
+ };
108
+ info.parent.add(ast);
109
+ variableScope.set(name, info);
110
+ let recursive = false;
111
+ // inlined: arena.walkValues(node)
112
+ for (const value of new ValueWalker(arena, node)) {
113
+ if (arena.dataTypes[value] == EnumToken.MathFunctionTokenDefType ||
114
+ (arena.dataTypes[value] == EnumToken.WildCardFunctionTokenType &&
115
+ (mathFuncs.includes(arena.strings[arena.stringValue[value]]) ||
116
+ arena.strings[arena.stringValue[value]] === "var"))) {
117
+ recursive = true;
118
+ break;
122
119
  }
123
120
  }
121
+ if (recursive) {
122
+ replace(arena, node, variableScope);
123
+ }
124
124
  }
125
125
  else {
126
- replace(node, variableScope);
126
+ replace(arena, node, variableScope);
127
127
  }
128
128
  }
129
129
  return null;
130
130
  }
131
- cleanup(ast, options = {}, context) {
131
+ cleanup(arena, _ast, _options = {}, context) {
132
132
  const variableScope = context.variableScope;
133
133
  if (variableScope == null) {
134
134
  return;
135
135
  }
136
136
  for (const info of variableScope.values()) {
137
137
  if (info.replaceable) {
138
- let i;
139
138
  // drop declarations from :root{}
140
139
  for (const parent of info.parent) {
141
- i = parent.chi?.length ?? 0;
142
- while (i--) {
143
- if (parent.chi[i] == info.node) {
144
- // @ts-ignore
145
- parent.chi.splice(i, 1, {
146
- typ: EnumToken.CommentTokenType,
147
- val: `/* ${info.node.nam}: ${info.values.reduce((acc, curr) => acc + renderValue(curr, { convertColor: false }), "")} */`,
148
- });
149
- break;
150
- }
140
+ if (arena.parents[info.node] == parent) {
141
+ const comment = arena.createComment(EnumToken.CommentTokenType, `/* ${arena.strings[arena.name[info.node]]}: ${info.values.reduce((acc, curr) => acc + renderValue(arena, curr, { convertColor: false }), "")} */`);
142
+ arena.replaceChild(info.node, comment);
143
+ arena.detach(info.node);
151
144
  }
152
145
  }
153
146
  }