@tbela99/css-parser 2.1.0 → 2.3.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 (130) hide show
  1. package/CHANGELOG.md +66 -1
  2. package/README.md +14 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/lib/ast → ast}/arena-node.js +10 -10
  5. package/dist/{src/lib/ast → ast}/arena.js +304 -139
  6. package/dist/{src/lib/ast → ast}/cache.js +40 -24
  7. package/dist/{src/lib/ast → ast}/features/calc.js +11 -9
  8. package/dist/{src/lib/ast → ast}/features/if.js +6 -6
  9. package/dist/ast/features/index.d.ts +7 -0
  10. package/dist/{src/lib/ast → ast}/features/index.js +3 -2
  11. package/dist/{src/lib/ast → ast}/features/inlinecssvariables.js +12 -10
  12. package/dist/ast/features/mixin.js +953 -0
  13. package/dist/{src/lib/ast → ast}/features/prefix.js +39 -39
  14. package/dist/ast/features/shorthand.js +129 -0
  15. package/dist/ast/features/transform.js +188 -0
  16. package/dist/{src/lib/ast → ast}/features/type.js +2 -2
  17. package/dist/{src/lib/ast → ast}/math/expression.js +24 -24
  18. package/dist/{src/lib/ast → ast}/math/math.js +2 -2
  19. package/dist/{src/lib/ast → ast}/minify.js +745 -201
  20. package/dist/ast/mixins/custom-function.js +393 -0
  21. package/dist/ast/mixins/utils.js +53 -0
  22. package/dist/ast/mixins/variables.js +678 -0
  23. package/dist/ast/strings.js +910 -0
  24. package/dist/ast/transform/compute.js +560 -0
  25. package/dist/ast/transform/matrix.js +67 -0
  26. package/dist/ast/transform/minify.js +325 -0
  27. package/dist/ast/transform/perspective.js +24 -0
  28. package/dist/{src/lib/ast → ast}/transform/rotate.js +16 -7
  29. package/dist/ast/transform/scale.js +43 -0
  30. package/dist/ast/transform/skew.js +38 -0
  31. package/dist/ast/transform/translate.js +56 -0
  32. package/dist/ast/transform/utils.js +390 -0
  33. package/dist/{src/lib/ast → ast}/types.js +2 -2
  34. package/dist/{src/lib/ast → ast}/walk.js +2 -2
  35. package/dist/{src/data → data}/properties.json.js +86 -2
  36. package/dist/{src/data → data}/validation.json.js +30 -21
  37. package/dist/{src/lib/fs → fs}/resolve.js +2 -2
  38. package/dist/index-umd-web.js +19727 -10719
  39. package/dist/index.d.ts +738 -100
  40. package/dist/{src/node.js → node.js} +70 -52
  41. package/dist/{src/lib/parser → parser}/declaration/list.js +25 -8
  42. package/dist/{src/lib/parser → parser}/declaration/map.js +15 -15
  43. package/dist/{src/lib/parser → parser}/declaration/set.js +5 -5
  44. package/dist/{src/lib/parser → parser}/input.js +18 -3
  45. package/dist/{src/lib/parser → parser}/linesmap.js +2 -2
  46. package/dist/{src/lib/parser → parser}/parse.js +444 -278
  47. package/dist/{src/lib/parser → parser}/source.js +3 -3
  48. package/dist/{src/lib/parser → parser}/tokenize.js +480 -312
  49. package/dist/{src/lib/parser → parser}/utils/angle.js +3 -3
  50. package/dist/{src/lib/parser → parser}/utils/at-rule-container.js +12 -12
  51. package/dist/{src/lib/parser → parser}/utils/at-rule-font-feature-values.js +3 -3
  52. package/dist/{src/lib/parser → parser}/utils/at-rule-generic.js +7 -7
  53. package/dist/{src/lib/parser → parser}/utils/at-rule-import.js +10 -10
  54. package/dist/{src/lib/parser → parser}/utils/at-rule-media.js +14 -14
  55. package/dist/{src/lib/parser → parser}/utils/at-rule-support.js +9 -14
  56. package/dist/{src/lib/parser → parser}/utils/at-rule-when-else.js +6 -6
  57. package/dist/{src/lib/parser → parser}/utils/at-rule.js +3 -3
  58. package/dist/{src/lib/parser → parser}/utils/base64.js +2 -2
  59. package/dist/parser/utils/cache.js +53 -0
  60. package/dist/parser/utils/canonical.js +78 -0
  61. package/dist/parser/utils/codes.js +93 -0
  62. package/dist/{src/lib/parser → parser}/utils/config.js +3 -3
  63. package/dist/{src/lib/parser → parser}/utils/declaration.js +31 -26
  64. package/dist/{src/lib/parser → parser}/utils/eq.js +2 -2
  65. package/dist/{src/lib/parser → parser}/utils/hash.js +2 -2
  66. package/dist/{src/lib/parser → parser}/utils/selector.js +339 -87
  67. package/dist/{src/lib/parser → parser}/utils/text.js +2 -2
  68. package/dist/{src/lib/parser → parser}/utils/token.js +2 -2
  69. package/dist/{src/lib/parser → parser}/utils/type.js +9 -7
  70. package/dist/{src/lib/printer/sourcemap → printer}/lib/codec.js +2 -2
  71. package/dist/{src/lib/printer → printer}/render.js +582 -153
  72. package/dist/printer/result.js +148 -0
  73. package/dist/{src/lib/printer/sourcemap → printer}/sourcemap.js +4 -3
  74. package/dist/printer/writer.js +252 -0
  75. package/dist/{src/lib/syntax → syntax}/color/a98rgb.js +2 -2
  76. package/dist/{src/lib/syntax → syntax}/color/alpha.js +10 -10
  77. package/dist/{src/lib/syntax → syntax}/color/cmyk.js +2 -2
  78. package/dist/{src/lib/syntax → syntax}/color/color-mix.js +18 -18
  79. package/dist/{src/lib/syntax → syntax}/color/color.js +18 -18
  80. package/dist/{src/lib/syntax → syntax}/color/hex.js +3 -3
  81. package/dist/{src/lib/syntax → syntax}/color/hsl.js +2 -2
  82. package/dist/{src/lib/syntax → syntax}/color/hwb.js +2 -2
  83. package/dist/{src/lib/syntax → syntax}/color/lab.js +2 -2
  84. package/dist/{src/lib/syntax → syntax}/color/lch.js +2 -2
  85. package/dist/{src/lib/syntax → syntax}/color/oklab.js +2 -2
  86. package/dist/{src/lib/syntax → syntax}/color/oklch.js +3 -3
  87. package/dist/{src/lib/syntax → syntax}/color/p3.js +2 -2
  88. package/dist/{src/lib/syntax → syntax}/color/prophotorgb.js +2 -2
  89. package/dist/{src/lib/syntax → syntax}/color/rec2020.js +2 -2
  90. package/dist/{src/lib/syntax → syntax}/color/relative-color.js +14 -14
  91. package/dist/{src/lib/syntax → syntax}/color/rgb.js +4 -4
  92. package/dist/{src/lib/syntax → syntax}/color/srgb.js +5 -5
  93. package/dist/{src/lib/syntax → syntax}/color/utils/color-type.js +4 -4
  94. package/dist/{src/lib/syntax → syntax}/color/utils/colorspace.js +10 -10
  95. package/dist/{src/lib/syntax → syntax}/color/utils/components.js +5 -5
  96. package/dist/{src/lib/syntax → syntax}/color/utils/distance.js +2 -2
  97. package/dist/{src/lib/syntax → syntax}/color/utils/make-color.js +3 -3
  98. package/dist/{src/lib/syntax → syntax}/color/utils/matrix.js +2 -2
  99. package/dist/{src/lib/syntax → syntax}/color/xyz.js +2 -2
  100. package/dist/{src/lib/syntax → syntax}/color/xyzd50.js +2 -2
  101. package/dist/{src/lib/syntax → syntax}/constants.js +27 -4
  102. package/dist/{src/lib/syntax → syntax}/syntax.js +37 -29
  103. package/dist/{src/types.js → types.js} +2 -2
  104. package/dist/{src/utils → utils}/sync.js +12 -7
  105. package/dist/validation/compiled/builtins.js +32 -0
  106. package/dist/validation/compiled/runtime.js +705 -0
  107. package/dist/validation/compiled/syntaxes.js +1620 -0
  108. package/dist/{src/lib/validation → validation}/config.js +63 -16
  109. package/dist/validation/first.js +574 -0
  110. package/dist/{src/lib/validation → validation}/match.js +376 -161
  111. package/dist/{src/lib/validation → validation}/parser/parse.js +2 -2
  112. package/dist/{src/lib/validation → validation}/parser/typedef.js +2 -2
  113. package/dist/{src/lib/validation → validation}/utils/list.js +3 -19
  114. package/dist/version.js +13 -0
  115. package/dist/{src/web.js → web.js} +68 -55
  116. package/package.json +15 -13
  117. package/dist/package.json.js +0 -10
  118. package/dist/src/lib/ast/features/shorthand.js +0 -95
  119. package/dist/src/lib/ast/features/transform.js +0 -90
  120. package/dist/src/lib/ast/transform/compute.js +0 -362
  121. package/dist/src/lib/ast/transform/matrix.js +0 -88
  122. package/dist/src/lib/ast/transform/minify.js +0 -267
  123. package/dist/src/lib/ast/transform/perspective.js +0 -16
  124. package/dist/src/lib/ast/transform/scale.js +0 -39
  125. package/dist/src/lib/ast/transform/skew.js +0 -30
  126. package/dist/src/lib/ast/transform/translate.js +0 -39
  127. package/dist/src/lib/ast/transform/utils.js +0 -287
  128. package/dist/src/lib/parser/utils/cache.js +0 -54
  129. package/dist/types.d.ts +0 -21
  130. package/dist/utils/sync.d.ts +0 -16
@@ -0,0 +1,560 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.3.0
6
+ License: MIT
7
+ */
8
+ import { setIdentity, multiplyInto, toZero } from './utils.js';
9
+ import { EnumToken } from '../types.js';
10
+ import { translateX, translateY, translateZ, translate, translate3d } from './translate.js';
11
+ import { getAngle, getNumber } from '../../syntax/color/color.js';
12
+ import { rotate, rotate3D } from './rotate.js';
13
+ import { scale3d, scale, scaleX, scaleY, scaleZ } from './scale.js';
14
+ import { minify } from './minify.js';
15
+ import { skew, skewX, skewY } from './skew.js';
16
+ import { serialize, setMatrixValues } from './matrix.js';
17
+ import { perspective } from './perspective.js';
18
+ import { length2Px } from '../../syntax/syntax.js';
19
+
20
+ /**
21
+ * kind of a transform function name, and family of the name (see {@link transformKind})
22
+ */
23
+ var TransformKind;
24
+ (function (TransformKind) {
25
+ TransformKind[TransformKind["None"] = 0] = "None";
26
+ TransformKind[TransformKind["Translate"] = 1] = "Translate";
27
+ TransformKind[TransformKind["TranslateX"] = 2] = "TranslateX";
28
+ TransformKind[TransformKind["TranslateY"] = 3] = "TranslateY";
29
+ TransformKind[TransformKind["TranslateZ"] = 4] = "TranslateZ";
30
+ TransformKind[TransformKind["Translate3d"] = 5] = "Translate3d";
31
+ TransformKind[TransformKind["Rotate"] = 6] = "Rotate";
32
+ TransformKind[TransformKind["RotateX"] = 7] = "RotateX";
33
+ TransformKind[TransformKind["RotateY"] = 8] = "RotateY";
34
+ TransformKind[TransformKind["RotateZ"] = 9] = "RotateZ";
35
+ TransformKind[TransformKind["Rotate3d"] = 10] = "Rotate3d";
36
+ TransformKind[TransformKind["Scale"] = 11] = "Scale";
37
+ TransformKind[TransformKind["ScaleX"] = 12] = "ScaleX";
38
+ TransformKind[TransformKind["ScaleY"] = 13] = "ScaleY";
39
+ TransformKind[TransformKind["ScaleZ"] = 14] = "ScaleZ";
40
+ TransformKind[TransformKind["Scale3d"] = 15] = "Scale3d";
41
+ TransformKind[TransformKind["Skew"] = 16] = "Skew";
42
+ TransformKind[TransformKind["SkewX"] = 17] = "SkewX";
43
+ TransformKind[TransformKind["SkewY"] = 18] = "SkewY";
44
+ TransformKind[TransformKind["Perspective"] = 19] = "Perspective";
45
+ TransformKind[TransformKind["Matrix"] = 20] = "Matrix";
46
+ TransformKind[TransformKind["Matrix3d"] = 21] = "Matrix3d";
47
+ /**
48
+ * the name of the `transform` property or of a prefixed version (`-webkit-transform`)
49
+ */
50
+ TransformKind[TransformKind["Property"] = 22] = "Property";
51
+ /**
52
+ * mask of the kind
53
+ */
54
+ TransformKind[TransformKind["KindMask"] = 255] = "KindMask";
55
+ /**
56
+ * the name starts with `translate`
57
+ */
58
+ TransformKind[TransformKind["TranslateFamily"] = 256] = "TranslateFamily";
59
+ /**
60
+ * the name starts with `rotate`
61
+ */
62
+ TransformKind[TransformKind["RotateFamily"] = 512] = "RotateFamily";
63
+ /**
64
+ * the name starts with `scale`
65
+ */
66
+ TransformKind[TransformKind["ScaleFamily"] = 768] = "ScaleFamily";
67
+ /**
68
+ * mask of the family
69
+ */
70
+ TransformKind[TransformKind["FamilyMask"] = 768] = "FamilyMask";
71
+ })(TransformKind || (TransformKind = {}));
72
+ /**
73
+ * the transform functions, by name (case-sensitive)
74
+ */
75
+ const transformKinds = new Map([
76
+ ["translate", 1 /* TransformKind.Translate */],
77
+ ["translateX", 2 /* TransformKind.TranslateX */],
78
+ ["translateY", 3 /* TransformKind.TranslateY */],
79
+ ["translateZ", 4 /* TransformKind.TranslateZ */],
80
+ ["translate3d", 5 /* TransformKind.Translate3d */],
81
+ ["rotate", 6 /* TransformKind.Rotate */],
82
+ ["rotateX", 7 /* TransformKind.RotateX */],
83
+ ["rotateY", 8 /* TransformKind.RotateY */],
84
+ ["rotateZ", 9 /* TransformKind.RotateZ */],
85
+ ["rotate3d", 10 /* TransformKind.Rotate3d */],
86
+ ["scale", 11 /* TransformKind.Scale */],
87
+ ["scaleX", 12 /* TransformKind.ScaleX */],
88
+ ["scaleY", 13 /* TransformKind.ScaleY */],
89
+ ["scaleZ", 14 /* TransformKind.ScaleZ */],
90
+ ["scale3d", 15 /* TransformKind.Scale3d */],
91
+ ["skew", 16 /* TransformKind.Skew */],
92
+ ["skewX", 17 /* TransformKind.SkewX */],
93
+ ["skewY", 18 /* TransformKind.SkewY */],
94
+ ["perspective", 19 /* TransformKind.Perspective */],
95
+ ["matrix", 20 /* TransformKind.Matrix */],
96
+ ["matrix3d", 21 /* TransformKind.Matrix3d */],
97
+ ]);
98
+ /**
99
+ * the transform property and its prefixed versions
100
+ */
101
+ const transformProperty = /^(-[a-z]+-)?transform$/;
102
+ /**
103
+ * the arena of the strings read by {@link computeTransformKind}: the function passed to the string table is not a
104
+ * closure (no allocation per call)
105
+ */
106
+ let kindArena = null;
107
+ /**
108
+ * see {@link transformKind}
109
+ * @param id
110
+ */
111
+ function computeTransformKind(id) {
112
+ const name = kindArena.resolveString(id);
113
+ let kind = transformKinds.get(name) ?? 0 /* TransformKind.None */;
114
+ if (kind === 0 /* TransformKind.None */ && transformProperty.test(name)) {
115
+ return 22 /* TransformKind.Property */;
116
+ }
117
+ if (name.startsWith("translate")) {
118
+ kind |= 256 /* TransformKind.TranslateFamily */;
119
+ }
120
+ else if (name.startsWith("rotate")) {
121
+ kind |= 512 /* TransformKind.RotateFamily */;
122
+ }
123
+ else if (name.startsWith("scale")) {
124
+ kind |= 768 /* TransformKind.ScaleFamily */;
125
+ }
126
+ return kind;
127
+ }
128
+ /**
129
+ * the kind of a string ({@link TransformKind.KindMask}: transform function or transform property) and the family of a
130
+ * transform function name ({@link TransformKind.FamilyMask}). computed once per interned string
131
+ * @param arena
132
+ * @param id interned string id
133
+ */
134
+ function transformKind(arena, id) {
135
+ kindArena = arena;
136
+ return arena.stringDerived(id, 2 /* StringDerived.TransformKind */, computeTransformKind);
137
+ }
138
+ /**
139
+ * numeric arguments of a transform function (the first 16 arguments), reused
140
+ */
141
+ const args = new Float64Array(16);
142
+ /**
143
+ * the matrix of the values of `matrix()` and `matrix3d()`
144
+ */
145
+ const valuesMatrix = new Float64Array(16);
146
+ /**
147
+ * multiply a matrix in place by the matrix of a transform function
148
+ * @param arena
149
+ * @param token
150
+ * @param matrixVar
151
+ * @returns false if the matrix cannot be computed. the matrix is then partially updated
152
+ */
153
+ function computeTransform(arena, token, matrixVar) {
154
+ if (arena.dataTypes[token] !== EnumToken.TransformFunctionTokenType) {
155
+ return false;
156
+ }
157
+ const kind = transformKind(arena, arena.stringValue[token]) & 255 /* TransformKind.KindMask */;
158
+ // number of arguments
159
+ let count = 0;
160
+ let child;
161
+ let typ;
162
+ switch (kind) {
163
+ case 1 /* TransformKind.Translate */:
164
+ case 2 /* TransformKind.TranslateX */:
165
+ case 3 /* TransformKind.TranslateY */:
166
+ case 4 /* TransformKind.TranslateZ */:
167
+ case 5 /* TransformKind.Translate3d */: {
168
+ let val;
169
+ for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
170
+ typ = arena.dataTypes[child];
171
+ if (typ === EnumToken.CommentTokenType ||
172
+ typ === EnumToken.CommaTokenType ||
173
+ typ === EnumToken.WhitespaceTokenType) {
174
+ continue;
175
+ }
176
+ val = length2Px(arena, child);
177
+ if (val == null) {
178
+ return false;
179
+ }
180
+ if (count < 16) {
181
+ args[count] = val;
182
+ }
183
+ count++;
184
+ }
185
+ if (count === 0 ||
186
+ count > (kind === 5 /* TransformKind.Translate3d */ ? 3 : kind === 1 /* TransformKind.Translate */ ? 2 : 1)) {
187
+ return false;
188
+ }
189
+ if (kind === 2 /* TransformKind.TranslateX */) {
190
+ translateX(args[0], matrixVar);
191
+ }
192
+ else if (kind === 3 /* TransformKind.TranslateY */) {
193
+ translateY(args[0], matrixVar);
194
+ }
195
+ else if (kind === 4 /* TransformKind.TranslateZ */) {
196
+ translateZ(args[0], matrixVar);
197
+ }
198
+ else if (kind === 1 /* TransformKind.Translate */) {
199
+ translate(args[0], count > 1 ? args[1] : undefined, matrixVar);
200
+ }
201
+ else {
202
+ translate3d(args[0], count > 1 ? args[1] : undefined, count > 2 ? args[2] : undefined, matrixVar);
203
+ }
204
+ return true;
205
+ }
206
+ case 6 /* TransformKind.Rotate */:
207
+ case 7 /* TransformKind.RotateX */:
208
+ case 8 /* TransformKind.RotateY */:
209
+ case 9 /* TransformKind.RotateZ */:
210
+ case 10 /* TransformKind.Rotate3d */: {
211
+ let x = 0;
212
+ let y = 0;
213
+ let z = 0;
214
+ // the first three arguments and the last argument
215
+ let first = 0;
216
+ let second = 0;
217
+ let third = 0;
218
+ let last = 0;
219
+ // the whitespace tokens are arguments
220
+ for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
221
+ typ = arena.dataTypes[child];
222
+ if (typ === EnumToken.CommentTokenType || typ === EnumToken.CommaTokenType) {
223
+ continue;
224
+ }
225
+ if (count === 0) {
226
+ first = child;
227
+ }
228
+ else if (count === 1) {
229
+ second = child;
230
+ }
231
+ else if (count === 2) {
232
+ third = child;
233
+ }
234
+ last = child;
235
+ count++;
236
+ }
237
+ if (count !== (kind === 10 /* TransformKind.Rotate3d */ ? 4 : 1)) {
238
+ return false;
239
+ }
240
+ if (kind === 7 /* TransformKind.RotateX */) {
241
+ x = 1;
242
+ }
243
+ else if (kind === 8 /* TransformKind.RotateY */) {
244
+ y = 1;
245
+ }
246
+ else if (kind === 6 /* TransformKind.Rotate */ || kind === 9 /* TransformKind.RotateZ */) {
247
+ z = 1;
248
+ }
249
+ else {
250
+ x = getNumber(arena, first);
251
+ y = getNumber(arena, second);
252
+ z = getNumber(arena, third);
253
+ }
254
+ const angle = getAngle(arena, last);
255
+ if (Number.isNaN(x) || Number.isNaN(y) || Number.isNaN(z) || Number.isNaN(angle)) {
256
+ return false;
257
+ }
258
+ if (kind === 6 /* TransformKind.Rotate */ || kind === 9 /* TransformKind.RotateZ */) {
259
+ rotate(angle * 2 * Math.PI, matrixVar);
260
+ }
261
+ else {
262
+ rotate3D(angle * 2 * Math.PI, x, y, z, matrixVar);
263
+ }
264
+ return true;
265
+ }
266
+ case 11 /* TransformKind.Scale */:
267
+ case 12 /* TransformKind.ScaleX */:
268
+ case 13 /* TransformKind.ScaleY */:
269
+ case 14 /* TransformKind.ScaleZ */:
270
+ case 15 /* TransformKind.Scale3d */:
271
+ for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
272
+ typ = arena.dataTypes[child];
273
+ if (typ === EnumToken.CommentTokenType || typ === EnumToken.CommaTokenType) {
274
+ continue;
275
+ }
276
+ // whitespace tokens are not allowed
277
+ if (typ !== EnumToken.NumberTokenType && typ !== EnumToken.PercentageTokenType) {
278
+ return false;
279
+ }
280
+ if (count < 16) {
281
+ args[count] = getNumber(arena, child);
282
+ }
283
+ count++;
284
+ }
285
+ if (kind === 15 /* TransformKind.Scale3d */) {
286
+ if (count !== 3) {
287
+ return false;
288
+ }
289
+ scale3d(args[0], args[1], args[2], matrixVar);
290
+ }
291
+ else if (kind === 11 /* TransformKind.Scale */) {
292
+ if (count !== 1 && count !== 2) {
293
+ return false;
294
+ }
295
+ scale(args[0], count > 1 ? args[1] : args[0], matrixVar);
296
+ }
297
+ else if (count !== 1) {
298
+ return false;
299
+ }
300
+ else if (kind === 12 /* TransformKind.ScaleX */) {
301
+ scaleX(args[0], matrixVar);
302
+ }
303
+ else if (kind === 13 /* TransformKind.ScaleY */) {
304
+ scaleY(args[0], matrixVar);
305
+ }
306
+ else {
307
+ scaleZ(args[0], matrixVar);
308
+ }
309
+ return true;
310
+ case 16 /* TransformKind.Skew */:
311
+ case 17 /* TransformKind.SkewX */:
312
+ case 18 /* TransformKind.SkewY */:
313
+ for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
314
+ typ = arena.dataTypes[child];
315
+ if (typ === EnumToken.CommentTokenType ||
316
+ typ === EnumToken.CommaTokenType ||
317
+ typ === EnumToken.WhitespaceTokenType) {
318
+ continue;
319
+ }
320
+ if (count < 16) {
321
+ args[count] = getAngle(arena, child) * 2 * Math.PI;
322
+ }
323
+ count++;
324
+ }
325
+ if (count === 0 || count > (kind === 16 /* TransformKind.Skew */ ? 2 : 1)) {
326
+ return false;
327
+ }
328
+ if (kind === 16 /* TransformKind.Skew */) {
329
+ skew(args[0], count > 1 ? args[1] : undefined, matrixVar);
330
+ }
331
+ else if (kind === 17 /* TransformKind.SkewX */) {
332
+ skewX(args[0], matrixVar);
333
+ }
334
+ else {
335
+ skewY(args[0], matrixVar);
336
+ }
337
+ return true;
338
+ case 19 /* TransformKind.Perspective */: {
339
+ // null for `none`
340
+ let distance = null;
341
+ let val;
342
+ for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
343
+ typ = arena.dataTypes[child];
344
+ if (typ === EnumToken.CommentTokenType || typ === EnumToken.WhitespaceTokenType) {
345
+ continue;
346
+ }
347
+ count++;
348
+ if (typ === EnumToken.IdenTokenType && arena.resolveString(arena.stringValue[child]) === "none") {
349
+ distance = null;
350
+ continue;
351
+ }
352
+ val = length2Px(arena, child);
353
+ if (val == null) {
354
+ return false;
355
+ }
356
+ distance = val;
357
+ }
358
+ if (count !== 1) {
359
+ return false;
360
+ }
361
+ perspective(distance, matrixVar);
362
+ return true;
363
+ }
364
+ case 20 /* TransformKind.Matrix */:
365
+ case 21 /* TransformKind.Matrix3d */:
366
+ for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
367
+ typ = arena.dataTypes[child];
368
+ if (typ === EnumToken.WhitespaceTokenType ||
369
+ typ === EnumToken.CommentTokenType ||
370
+ typ === EnumToken.CommaTokenType) {
371
+ continue;
372
+ }
373
+ if (count < 16) {
374
+ args[count] = getNumber(arena, child);
375
+ }
376
+ count++;
377
+ }
378
+ if (count !== (kind === 20 /* TransformKind.Matrix */ ? 6 : 16)) {
379
+ return false;
380
+ }
381
+ multiplyInto(matrixVar, setMatrixValues(args, count, valuesMatrix), matrixVar);
382
+ return true;
383
+ }
384
+ return false;
385
+ }
386
+ /**
387
+ * multiply a matrix in place by the matrices of a list of transform functions
388
+ * @param arena
389
+ * @param tokens
390
+ * @param start index of the first function
391
+ * @param end index after the last function
392
+ * @param matrixVar
393
+ * @returns false if the matrix cannot be computed. the matrix is then partially updated
394
+ */
395
+ function computeMatrix(arena, tokens, start, end, matrixVar) {
396
+ for (let i = start; i < end; i++) {
397
+ if (!computeTransform(arena, tokens[i], matrixVar)) {
398
+ return false;
399
+ }
400
+ }
401
+ return true;
402
+ }
403
+ /**
404
+ * the matrix of the transform list
405
+ */
406
+ const transformMatrix = new Float64Array(16);
407
+ /**
408
+ * the matrix of a run of functions of the transform list
409
+ */
410
+ const runMatrix = new Float64Array(16);
411
+ /**
412
+ * the transform functions of the runs: index of the first token of each run, then the end of the last run. the arrays
413
+ * are reused: their length is not reset (see {@link runCount} and {@link tokenCount})
414
+ */
415
+ const runs = [];
416
+ /**
417
+ * the tokens of the transform list without the comments and the whitespace tokens, see {@link splitTransformList}
418
+ */
419
+ const tokens = [];
420
+ /**
421
+ * number of values of {@link runs}
422
+ */
423
+ let runCount = 0;
424
+ /**
425
+ * number of values of {@link tokens}
426
+ */
427
+ let tokenCount = 0;
428
+ /**
429
+ * the result of the computation, reused
430
+ */
431
+ const computed = {
432
+ values: transformMatrix,
433
+ matrix: 0,
434
+ cumulative: [],
435
+ minified: [],
436
+ created: true,
437
+ };
438
+ /**
439
+ * compute the matrix of a transform list
440
+ * @param arena
441
+ * @param transformList the tokens of the value
442
+ * @returns null if the matrix cannot be computed
443
+ */
444
+ function compute(arena, transformList) {
445
+ let token;
446
+ let child;
447
+ let kind;
448
+ let typ;
449
+ // all transform function arguments must be 0 or scale(1)
450
+ for (let i = 0; i < transformList.length; i++) {
451
+ token = transformList[i];
452
+ kind = transformKind(arena, arena.stringValue[token]) & 255 /* TransformKind.KindMask */;
453
+ if (kind === 6 /* TransformKind.Rotate */ ||
454
+ kind === 7 /* TransformKind.RotateX */ ||
455
+ kind === 8 /* TransformKind.RotateY */ ||
456
+ kind === 9 /* TransformKind.RotateZ */ ||
457
+ kind === 10 /* TransformKind.Rotate3d */) {
458
+ for (child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
459
+ typ = arena.dataTypes[child];
460
+ if (typ === EnumToken.WhitespaceTokenType || typ === EnumToken.CommaTokenType) {
461
+ continue;
462
+ }
463
+ if ((typ !== EnumToken.AngleTokenType &&
464
+ typ !== EnumToken.NumberTokenType &&
465
+ typ !== EnumToken.PercentageTokenType) ||
466
+ // angle >= 360deg, do not transform
467
+ Math.abs(getAngle(arena, child)) >= 1) {
468
+ return null;
469
+ }
470
+ }
471
+ }
472
+ }
473
+ splitTransformList(arena, transformList);
474
+ // a new array: it may become the value of the declaration
475
+ const cumulative = [];
476
+ const matrix = setIdentity(transformMatrix);
477
+ let minified;
478
+ // the runs produced tokens: `none` is the value of an empty list
479
+ let empty = true;
480
+ computed.created = true;
481
+ for (let run = 0; run < runCount - 1; run++) {
482
+ const start = runs[run];
483
+ const end = runs[run + 1];
484
+ if (!computeMatrix(arena, tokens, start, end, setIdentity(runMatrix))) {
485
+ return null;
486
+ }
487
+ multiplyInto(matrix, runMatrix, matrix);
488
+ minified = minify(arena, runMatrix);
489
+ if (minified == null) {
490
+ computed.created = false;
491
+ empty = false;
492
+ for (let i = start; i < end; i++) {
493
+ pushTransform(arena, cumulative, tokens[i]);
494
+ }
495
+ }
496
+ else {
497
+ empty &&= minified.length === 0;
498
+ for (let i = 0; i < minified.length; i++) {
499
+ pushTransform(arena, cumulative, minified[i]);
500
+ }
501
+ }
502
+ }
503
+ const serialized = serialize(arena, matrix);
504
+ if (!empty && cumulative.length === 0) {
505
+ cumulative.push(arena.createToken(EnumToken.IdenTokenType, "none"));
506
+ }
507
+ computed.cumulative = cumulative;
508
+ computed.matrix = serialize(arena, toZero(matrix));
509
+ computed.minified = minify(arena, matrix) ?? [serialized];
510
+ return computed;
511
+ }
512
+ /**
513
+ * append a token to a transform list. `none` is omitted
514
+ * @param arena
515
+ * @param list
516
+ * @param token
517
+ */
518
+ function pushTransform(arena, list, token) {
519
+ if (arena.dataTypes[token] !== EnumToken.IdenTokenType ||
520
+ arena.resolveString(arena.stringValue[token]) !== "none") {
521
+ list.push(token);
522
+ }
523
+ }
524
+ /**
525
+ * split a transform list into runs (see {@link runs} and {@link tokens}): the consecutive translations, rotations and
526
+ * scales are grouped, the other tokens are alone. the comments and the whitespace tokens are ignored
527
+ * @param arena
528
+ * @param transformList
529
+ */
530
+ function splitTransformList(arena, transformList) {
531
+ // family of the current run
532
+ let pattern = 0;
533
+ let family;
534
+ let typ;
535
+ let token;
536
+ runCount = 0;
537
+ // the runs index the list without the ignored tokens
538
+ tokenCount = 0;
539
+ for (let i = 0; i < transformList.length; i++) {
540
+ token = transformList[i];
541
+ typ = arena.dataTypes[token];
542
+ if (typ === EnumToken.CommentTokenType || typ === EnumToken.WhitespaceTokenType) {
543
+ continue;
544
+ }
545
+ if (typ === EnumToken.TransformFunctionTokenType) {
546
+ family = transformKind(arena, arena.stringValue[token]) & 768 /* TransformKind.FamilyMask */;
547
+ if (pattern !== 0 && family === pattern) {
548
+ // continue the current run
549
+ tokens[tokenCount++] = token;
550
+ continue;
551
+ }
552
+ pattern = family;
553
+ }
554
+ runs[runCount++] = tokenCount;
555
+ tokens[tokenCount++] = token;
556
+ }
557
+ runs[runCount++] = tokenCount;
558
+ }
559
+
560
+ export { TransformKind, compute, computeMatrix, computeTransform, transformKind };
@@ -0,0 +1,67 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.3.0
6
+ License: MIT
7
+ */
8
+ import { isIdentity, is2DMatrix } from './utils.js';
9
+ import { EnumToken } from '../types.js';
10
+
11
+ /**
12
+ * set the values of a `matrix()` (6 values) or of a `matrix3d()` (16 values) function, column-major order. the other
13
+ * values of a `matrix()` are the values of the identity matrix
14
+ * @param values
15
+ * @param count number of values: 6 or 16
16
+ * @param matrix the matrix that receives the values
17
+ */
18
+ function setMatrixValues(values, count, matrix) {
19
+ if (count === 6) {
20
+ matrix[0] = values[0];
21
+ matrix[1] = values[1];
22
+ matrix[2] = 0;
23
+ matrix[3] = 0;
24
+ matrix[4] = values[2];
25
+ matrix[4 + 1] = values[3];
26
+ matrix[4 + 2] = 0;
27
+ matrix[4 + 3] = 0;
28
+ matrix[2 * 4] = 0;
29
+ matrix[2 * 4 + 1] = 0;
30
+ matrix[2 * 4 + 2] = 1;
31
+ matrix[2 * 4 + 3] = 0;
32
+ matrix[3 * 4] = values[4];
33
+ matrix[3 * 4 + 1] = values[5];
34
+ matrix[3 * 4 + 2] = 0;
35
+ matrix[3 * 4 + 3] = 1;
36
+ }
37
+ else {
38
+ for (let i = 0; i < 16; i++) {
39
+ matrix[i] = values[i];
40
+ }
41
+ }
42
+ return matrix;
43
+ }
44
+ /**
45
+ * the tokens of a matrix: `none`, `matrix()` if it is a 2d matrix, `matrix3d()` otherwise
46
+ * @param arena
47
+ * @param matrix
48
+ */
49
+ function serialize(arena, matrix) {
50
+ if (isIdentity(matrix)) {
51
+ return arena.createToken(EnumToken.IdenTokenType, "none");
52
+ }
53
+ // https://drafts.csswg.org/css-transforms-2/#two-dimensional-subset
54
+ const is2D = is2DMatrix(matrix);
55
+ const count = is2D ? 6 : 16;
56
+ const tokens = new Array(count * 2 - 1);
57
+ for (let i = 0; i < count; i++) {
58
+ if (i > 0) {
59
+ tokens[i * 2 - 1] = arena.createToken(EnumToken.CommaTokenType);
60
+ }
61
+ // matrix(a, b, c, d, e, f): values 0, 1, 4, 5, 12, 13
62
+ tokens[i * 2] = arena.createToken(EnumToken.NumberTokenType, matrix[is2D ? (i < 2 ? i : i < 4 ? i + 2 : i + 8) : i]);
63
+ }
64
+ return arena.createFunction(EnumToken.TransformFunctionTokenType, is2D ? "matrix" : "matrix3d", tokens);
65
+ }
66
+
67
+ export { serialize, setMatrixValues };