@tbela99/css-parser 1.8.2 → 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 +62 -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/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 -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,29 +2,38 @@
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.0
6
6
  License: MIT
7
7
  */
8
- import { eq } from '../utils/eq.js';
8
+ import { numericTokenTypes } from '../../ast/arena.js';
9
+ import { eqToken } from '../utils/eq.js';
9
10
  import { EnumToken } from '../../ast/types.js';
10
11
  import { isLength } from '../../syntax/syntax.js';
11
12
 
12
- function dedup(values) {
13
+ function dedup(arena, values) {
13
14
  for (const value of values) {
14
15
  let i = value.length;
15
16
  while (i-- > 1) {
16
17
  const t = value[i];
17
18
  const k = value[i == 1 ? 0 : i % 2];
18
- if (t.val == k.val && t.val == 0) {
19
- if ((t.typ == EnumToken.NumberTokenType && isLength(k)) ||
20
- (k.typ == EnumToken.NumberTokenType && isLength(t)) ||
21
- isLength(k) ||
22
- isLength(t)) {
19
+ if (
20
+ // inlined: arena.isNumeric(t)
21
+ numericTokenTypes.has(arena.dataTypes[t]) &&
22
+ // inlined: arena.isNumeric(k)
23
+ numericTokenTypes.has(arena.dataTypes[k]) &&
24
+ arena.numericValue[t] == 0 &&
25
+ arena.numericValue[k] == 0 &&
26
+ !arena.hasFraction(t) &&
27
+ !arena.hasFraction(k)) {
28
+ if ((arena.dataTypes[t] == EnumToken.NumberTokenType && isLength(arena, k)) ||
29
+ (arena.dataTypes[k] == EnumToken.NumberTokenType && isLength(arena, t)) ||
30
+ isLength(arena, k) ||
31
+ isLength(arena, t)) {
23
32
  value.splice(i, 1);
24
33
  continue;
25
34
  }
26
35
  }
27
- if (eq(t, k)) {
36
+ if (eqToken(arena, t, k)) {
28
37
  value.splice(i, 1);
29
38
  continue;
30
39
  }
@@ -33,15 +42,23 @@ function dedup(values) {
33
42
  }
34
43
  return values;
35
44
  }
45
+ /**
46
+ * set of declarations that can be merged into a single shorthand declaration.
47
+ * declarations are arena node ids
48
+ */
36
49
  class PropertySet {
37
50
  config;
51
+ arena;
38
52
  declarations;
39
- constructor(config) {
53
+ constructor(arena, config) {
54
+ this.arena = arena;
40
55
  this.config = config;
41
56
  this.declarations = new Map();
42
57
  }
43
58
  add(declaration) {
44
- if (declaration.nam == this.config.shorthand) {
59
+ const arena = this.arena;
60
+ const nam = arena.strings[arena.name[declaration]];
61
+ if (nam == this.config.shorthand) {
45
62
  this.declarations = new Map();
46
63
  }
47
64
  else {
@@ -50,13 +67,13 @@ class PropertySet {
50
67
  let isValid = true;
51
68
  let current = -1;
52
69
  const tokens = [];
53
- // @ts-ignore
54
- for (let token of this.declarations.get(this.config.shorthand).val) {
70
+ for (const token of arena.tokens(this.declarations.get(this.config.shorthand))) {
71
+ const typ = arena.dataTypes[token];
55
72
  if (
56
73
  // @ts-expect-error
57
- this.config.types.some((t) => token.typ == EnumToken[t]) ||
58
- (token.typ == EnumToken.NumberTokenType &&
59
- token.val == 0 &&
74
+ this.config.types.some((t) => typ == EnumToken[t]) ||
75
+ (typ == EnumToken.NumberTokenType &&
76
+ arena.numericValue[token] == 0 &&
60
77
  (this.config.types.includes("Length") ||
61
78
  this.config.types.includes("Angle") ||
62
79
  this.config.types.includes("Dimension")))) {
@@ -67,17 +84,17 @@ class PropertySet {
67
84
  tokens[current].push(token);
68
85
  continue;
69
86
  }
70
- if (token.typ != EnumToken.WhitespaceTokenType && token.typ != EnumToken.CommentTokenType) {
71
- if (token.typ == EnumToken.IdenTokenType &&
72
- this.config.keywords.includes(token.val)) {
87
+ if (typ != EnumToken.WhitespaceTokenType && typ != EnumToken.CommentTokenType) {
88
+ if (typ == EnumToken.IdenTokenType &&
89
+ this.config.keywords.includes(arena.strings[arena.stringValue[token]])) {
73
90
  if (tokens.length == 0) {
74
91
  tokens.push([]);
75
92
  current++;
76
93
  }
77
94
  tokens[current].push(token);
78
95
  }
79
- // @ts-ignore
80
- if (token.typ == EnumToken.LiteralTokenType && token.val == this.config.separator?.val) {
96
+ if (typ == EnumToken.LiteralTokenType &&
97
+ arena.strings[arena.stringValue[token]] == this.config.separator?.val) {
81
98
  tokens.push([]);
82
99
  current++;
83
100
  continue;
@@ -91,11 +108,7 @@ class PropertySet {
91
108
  for (const values of tokens) {
92
109
  this.config.properties.forEach((property, index) => {
93
110
  if (!this.declarations.has(property)) {
94
- this.declarations.set(property, {
95
- typ: EnumToken.DeclarationNodeType,
96
- nam: property,
97
- val: [],
98
- });
111
+ this.declarations.set(property, arena.createDeclaration(EnumToken.DeclarationNodeType, property, [], null, null));
99
112
  }
100
113
  while (index > 0 && index >= values.length) {
101
114
  if (index > 1) {
@@ -106,20 +119,19 @@ class PropertySet {
106
119
  break;
107
120
  }
108
121
  }
109
- // @ts-ignore
110
- const val = this.declarations.get(property).val;
111
- if (val.length > 0) {
112
- val.push({ typ: EnumToken.WhitespaceTokenType });
122
+ const node = this.declarations.get(property);
123
+ if (arena.firstToken[node] != 0) {
124
+ arena.appendToken(node, arena.createToken(EnumToken.WhitespaceTokenType));
113
125
  }
114
- val.push({ ...values[index] });
126
+ arena.appendToken(node, arena.clone(values[index], true));
115
127
  });
116
128
  }
117
129
  }
118
- this.declarations.set(declaration.nam, declaration);
130
+ this.declarations.set(nam, declaration);
119
131
  return this;
120
132
  }
121
133
  }
122
- this.declarations.set(declaration.nam, declaration);
134
+ this.declarations.set(nam, declaration);
123
135
  return this;
124
136
  }
125
137
  isShortHand() {
@@ -130,23 +142,26 @@ class PropertySet {
130
142
  }
131
143
  [Symbol.iterator]() {
132
144
  let iterator;
145
+ const arena = this.arena;
133
146
  const declarations = this.declarations;
134
147
  if (declarations.size < this.config.properties.length) {
135
148
  const values = [...declarations.values()];
136
149
  if (this.isShortHand()) {
137
- const val = values[0].val.reduce((acc, curr) => {
138
- if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(curr.typ)) {
150
+ const val = arena.tokens(values[0]).reduce((acc, curr) => {
151
+ if (arena.dataTypes[curr] != EnumToken.WhitespaceTokenType &&
152
+ arena.dataTypes[curr] != EnumToken.CommentTokenType) {
139
153
  acc.push(curr);
140
154
  }
141
155
  return acc;
142
156
  }, []);
143
- values[0].val = val.reduce((acc, curr) => {
157
+ // inlined: arena.setTokens(values[0], ...val.reduce((acc: Token[], curr: Token) => { if (acc.length > 0) { acc.push(arena.createToken(EnumToken.WhitespaceTokenType)); } acc.push(curr); return acc; }, <Token[]>[]))
158
+ arena.setTokenList(values[0], val.reduce((acc, curr) => {
144
159
  if (acc.length > 0) {
145
- acc.push({ typ: EnumToken.WhitespaceTokenType });
160
+ acc.push(arena.createToken(EnumToken.WhitespaceTokenType));
146
161
  }
147
162
  acc.push(curr);
148
163
  return acc;
149
- }, []);
164
+ }, []));
150
165
  }
151
166
  return values[Symbol.iterator]();
152
167
  }
@@ -154,12 +169,9 @@ class PropertySet {
154
169
  const values = [];
155
170
  this.config.properties.forEach((property) => {
156
171
  let index = 0;
157
- // @ts-ignore
158
- for (const token of this.declarations.get(property)?.val ?? []) {
159
- if (token == null) {
160
- return this.declarations.values()[Symbol.iterator]();
161
- }
162
- if (token.typ == EnumToken.WhitespaceTokenType) {
172
+ const id = this.declarations.get(property);
173
+ for (const token of id == null ? [] : arena.tokens(id)) {
174
+ if (arena.dataTypes[token] == EnumToken.WhitespaceTokenType) {
163
175
  continue;
164
176
  }
165
177
  if (values.length == index) {
@@ -170,31 +182,27 @@ class PropertySet {
170
182
  }
171
183
  });
172
184
  if (this.config.valueSeparator == null) {
173
- dedup(values);
185
+ dedup(arena, values);
174
186
  }
175
187
  iterator = [
176
- {
177
- typ: EnumToken.DeclarationNodeType,
178
- nam: this.config.shorthand,
179
- val: values.reduce((acc, curr) => {
180
- if (curr.length > 1) {
181
- const k = curr.length * 2 - 1;
182
- let i = 1;
183
- while (i < k) {
184
- curr.splice(i, 0, { typ: EnumToken.WhitespaceTokenType });
185
- i += 2;
186
- }
187
- }
188
- if (acc.length > 0) {
189
- // @ts-ignore
190
- acc.push({ ...this.config.separator, typ: EnumToken.LiteralTokenType });
191
- }
192
- for (const token of curr) {
193
- acc.push(token);
188
+ arena.createDeclaration(EnumToken.DeclarationNodeType, this.config.shorthand, values.reduce((acc, curr) => {
189
+ if (curr.length > 1) {
190
+ const k = curr.length * 2 - 1;
191
+ let i = 1;
192
+ while (i < k) {
193
+ curr.splice(i, 0, arena.createToken(EnumToken.WhitespaceTokenType));
194
+ i += 2;
194
195
  }
195
- return acc;
196
- }, []),
197
- },
196
+ }
197
+ if (acc.length > 0) {
198
+ acc.push(arena.createToken(EnumToken.LiteralTokenType, this.config.separator.val));
199
+ }
200
+ for (const token of curr) {
201
+ // tokens are shared with the original declarations
202
+ acc.push(arena.parents[token] != 0 ? arena.clone(token, true) : token);
203
+ }
204
+ return acc;
205
+ }, []), null, null),
198
206
  ][Symbol.iterator]();
199
207
  }
200
208
  return iterator;
@@ -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.0
6
6
  License: MIT
7
7
  */
8
8
  /**