@tbela99/css-parser 2.1.0 → 2.2.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 (125) hide show
  1. package/CHANGELOG.md +42 -1
  2. package/README.md +14 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +5 -2
  6. package/dist/index-umd-web.js +10738 -4359
  7. package/dist/index.d.ts +734 -100
  8. package/dist/{src/lib → lib}/ast/arena-node.js +10 -10
  9. package/dist/{src/lib → lib}/ast/arena.js +304 -139
  10. package/dist/{src/lib → lib}/ast/cache.js +2 -2
  11. package/dist/{src/lib → lib}/ast/features/calc.js +11 -9
  12. package/dist/{src/lib → lib}/ast/features/if.js +6 -6
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +12 -10
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +39 -39
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +24 -24
  21. package/dist/{src/lib → lib}/ast/math/math.js +2 -2
  22. package/dist/{src/lib → lib}/ast/minify.js +745 -201
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +2 -2
  37. package/dist/{src/lib → lib}/ast/walk.js +2 -2
  38. package/dist/{src/lib → lib}/fs/resolve.js +2 -2
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +25 -8
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +15 -15
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +5 -5
  42. package/dist/{src/lib → lib}/parser/input.js +18 -3
  43. package/dist/{src/lib → lib}/parser/linesmap.js +2 -2
  44. package/dist/{src/lib → lib}/parser/parse.js +425 -258
  45. package/dist/{src/lib → lib}/parser/source.js +2 -2
  46. package/dist/{src/lib → lib}/parser/tokenize.js +480 -312
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +12 -12
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +7 -7
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +10 -10
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +14 -14
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +9 -14
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +6 -6
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +31 -26
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +2 -2
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +2 -2
  64. package/dist/{src/lib → lib}/parser/utils/selector.js +305 -63
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +9 -7
  68. package/dist/{src/lib → lib}/printer/render.js +420 -152
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +2 -2
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +2 -2
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +2 -2
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +10 -10
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +2 -2
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +18 -18
  77. package/dist/{src/lib → lib}/syntax/color/color.js +18 -18
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +3 -3
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +2 -2
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +2 -2
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +2 -2
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +2 -2
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +2 -2
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +3 -3
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +2 -2
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +2 -2
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +14 -14
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +4 -4
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +5 -5
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +4 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +10 -10
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +5 -5
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +3 -3
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +2 -2
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +2 -2
  99. package/dist/{src/lib → lib}/syntax/constants.js +26 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +37 -29
  101. package/dist/{src/lib → lib}/validation/config.js +23 -10
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +292 -150
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +2 -2
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +2 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +52 -34
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +10 -5
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +47 -34
  113. package/package.json +12 -12
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -95
  116. package/dist/src/lib/ast/features/transform.js +0 -90
  117. package/dist/src/lib/ast/transform/compute.js +0 -362
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/utils/cache.js +0 -54
@@ -0,0 +1,390 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.2.0
6
+ License: MIT
7
+ */
8
+ import { epsilon } from '../../syntax/constants.js';
9
+
10
+ /**
11
+ * Matrix operations of the transform functions. The matrices are `Float64Array(16)` in column-major order.
12
+ *
13
+ * The operations write into matrices provided by the caller (no allocation): the computation of a transform list uses
14
+ * a few module level scratch matrices. The arithmetic is performed in the same order as the textbook formulas, the
15
+ * results do not depend on the buffers used.
16
+ *
17
+ * @module
18
+ * @internal
19
+ */
20
+ /**
21
+ * the identity matrix. must not be modified
22
+ */
23
+ const identityMatrix = new Float64Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
24
+ /**
25
+ * reset a matrix to the identity matrix
26
+ * @param matrix
27
+ */
28
+ function setIdentity(matrix) {
29
+ matrix.set(identityMatrix);
30
+ return matrix;
31
+ }
32
+ /**
33
+ * true if the values of the matrix are equal (`===`) to the values of the identity matrix
34
+ * @param matrix
35
+ */
36
+ function isIdentity(matrix) {
37
+ return (matrix[0] === 1 &&
38
+ matrix[1] === 0 &&
39
+ matrix[2] === 0 &&
40
+ matrix[3] === 0 &&
41
+ matrix[4] === 0 &&
42
+ matrix[5] === 1 &&
43
+ matrix[6] === 0 &&
44
+ matrix[7] === 0 &&
45
+ matrix[8] === 0 &&
46
+ matrix[9] === 0 &&
47
+ matrix[10] === 1 &&
48
+ matrix[11] === 0 &&
49
+ matrix[12] === 0 &&
50
+ matrix[13] === 0 &&
51
+ matrix[14] === 0 &&
52
+ matrix[15] === 1);
53
+ }
54
+ /**
55
+ * multiply two matrices: `result = matrixA × matrixB`. `result` can be one of the operands
56
+ * @param matrixA
57
+ * @param matrixB
58
+ * @param result
59
+ */
60
+ function multiplyInto(matrixA, matrixB, result) {
61
+ const a0 = matrixA[0];
62
+ const a1 = matrixA[1];
63
+ const a2 = matrixA[2];
64
+ const a3 = matrixA[3];
65
+ const a4 = matrixA[4];
66
+ const a5 = matrixA[5];
67
+ const a6 = matrixA[6];
68
+ const a7 = matrixA[7];
69
+ const a8 = matrixA[8];
70
+ const a9 = matrixA[9];
71
+ const a10 = matrixA[10];
72
+ const a11 = matrixA[11];
73
+ const a12 = matrixA[12];
74
+ const a13 = matrixA[13];
75
+ const a14 = matrixA[14];
76
+ const a15 = matrixA[15];
77
+ for (let column = 0; column < 16; column += 4) {
78
+ const b0 = matrixB[column];
79
+ const b1 = matrixB[column + 1];
80
+ const b2 = matrixB[column + 2];
81
+ const b3 = matrixB[column + 3];
82
+ result[column] = a0 * b0 + a4 * b1 + a8 * b2 + a12 * b3;
83
+ result[column + 1] = a1 * b0 + a5 * b1 + a9 * b2 + a13 * b3;
84
+ result[column + 2] = a2 * b0 + a6 * b1 + a10 * b2 + a14 * b3;
85
+ result[column + 3] = a3 * b0 + a7 * b1 + a11 * b2 + a15 * b3;
86
+ }
87
+ return result;
88
+ }
89
+ /**
90
+ * augmented matrix of {@link inverse}
91
+ */
92
+ const augmented = new Float64Array(32);
93
+ /**
94
+ * inverse of a matrix, used to extract the perspective. the result is a shared buffer (its first 16 values), valid
95
+ * until the next call
96
+ * @param matrix
97
+ */
98
+ function inverse(matrix) {
99
+ // Create augmented matrix [matrix | identity]
100
+ for (let row = 0; row < 4; row++) {
101
+ for (let j = 0; j < 4; j++) {
102
+ augmented[row * 8 + j] = matrix[row * 4 + j];
103
+ augmented[row * 8 + 4 + j] = row === j ? 1 : 0;
104
+ }
105
+ }
106
+ // Gaussian elimination with partial pivoting
107
+ for (let col = 0; col < 4; col++) {
108
+ // Find pivot row with maximum absolute value
109
+ let maxRow = col;
110
+ let maxVal = Math.abs(augmented[col * 4 + col]);
111
+ for (let row = col + 1; row < 4; row++) {
112
+ const val = Math.abs(augmented[row * 4 + col]);
113
+ if (val > maxVal) {
114
+ maxVal = val;
115
+ maxRow = row;
116
+ }
117
+ }
118
+ // Check for singularity
119
+ if (maxVal < epsilon) {
120
+ return null;
121
+ }
122
+ // Swap rows if necessary
123
+ if (maxRow !== col) {
124
+ const swap = augmented[col];
125
+ augmented[col] = augmented[maxRow];
126
+ augmented[maxRow] = swap;
127
+ }
128
+ // Scale pivot row to make pivot element 1
129
+ const pivot = augmented[col * 4 + col];
130
+ for (let j = 0; j < 8; j++) {
131
+ augmented[col * 4 + j] /= pivot;
132
+ }
133
+ // Eliminate column in other rows
134
+ for (let row = 0; row < 4; row++) {
135
+ if (row !== col) {
136
+ const factor = augmented[row * 4 + col];
137
+ for (let j = 0; j < 8; j++) {
138
+ augmented[row * 4 + j] -= factor * augmented[col * 4 + j];
139
+ }
140
+ }
141
+ }
142
+ }
143
+ // the inverse is read from the first 16 values
144
+ return augmented;
145
+ }
146
+ /**
147
+ * the powers of 10 that are exactly represented by a double
148
+ */
149
+ const powersOf10 = new Float64Array(23).map((_, i) => Number("1e" + i));
150
+ /**
151
+ * same value as `+value.toPrecision(6)`, computed without creating a string when the result does not depend on the
152
+ * rounding errors of the computation.
153
+ *
154
+ * `toPrecision(6)` rounds the exact value to the integer `n` (`10^5 <= n < 10^6`) times a power of 10 (`10^-k`),
155
+ * the decimal string is then parsed to the nearest double. `value × 10^k` is computed with an error below `1.2e-10`
156
+ * (`10^k` is exact, the product is correctly rounded): the rounding to `n` is exact unless the product is close to a
157
+ * tie. `n` and `10^k` are exact doubles: `n / 10^k` is the double nearest to the exact quotient, the value of the
158
+ * parsed string. the other cases (ties, very small or very large values, `NaN`, infinite values) use `toPrecision()`
159
+ * @param value
160
+ */
161
+ function toPrecision6(value) {
162
+ const abs = value < 0 ? -value : value;
163
+ if (abs >= 1e-15 && abs < 1e21) {
164
+ const k = 5 - Math.floor(Math.log10(abs));
165
+ const scaled = k >= 0 ? abs * powersOf10[k] : abs / powersOf10[-k];
166
+ // the exponent computed from the logarithm is exact
167
+ if (scaled >= 1e5 && scaled < 999999.5) {
168
+ let n = Math.floor(scaled);
169
+ const fraction = scaled - n;
170
+ // not close to a tie
171
+ if (fraction < 0.499999 || fraction > 0.500001) {
172
+ if (fraction > 0.5) {
173
+ n++;
174
+ }
175
+ const result = k >= 0 ? n / powersOf10[k] : n * powersOf10[-k];
176
+ return value < 0 ? -result : result;
177
+ }
178
+ }
179
+ }
180
+ return +value.toPrecision(6);
181
+ }
182
+ /**
183
+ * round a number to 6 significant digits, or to the nearest integer when it is closer than `epsilon`
184
+ * @param number
185
+ */
186
+ function round(number) {
187
+ const rounded = Math.round(number);
188
+ return Math.abs(rounded - number) <= epsilon ? rounded : toPrecision6(number);
189
+ }
190
+ /**
191
+ * working copy of the matrix being decomposed
192
+ */
193
+ const decomposeBuffer = new Float64Array(16);
194
+ /**
195
+ * decompose a matrix: translate → rotate → skew → scale. the matrix is not modified
196
+ *
197
+ * `translate3d(25.9808px, 0, 15px) rotateY(60deg) skewX(49.9999deg) scale(1, 1.2)`
198
+ * @param original
199
+ * @param result receives the decomposition
200
+ * @returns false if the matrix cannot be decomposed
201
+ */
202
+ function decompose(original, result) {
203
+ const matrix = decomposeBuffer;
204
+ // Normalize last row
205
+ if (original[15] === 0) {
206
+ return false;
207
+ }
208
+ matrix.set(original);
209
+ for (let i = 0; i < 16; i++)
210
+ matrix[i] /= matrix[15];
211
+ // Perspective extraction
212
+ result.perspectiveX = 0;
213
+ result.perspectiveY = 0;
214
+ result.perspectiveZ = 0;
215
+ result.perspectiveW = 1;
216
+ if (matrix[3] !== 0 || matrix[7] !== 0 || matrix[11] !== 0) {
217
+ const r0 = matrix[3];
218
+ const r1 = matrix[7];
219
+ const r2 = matrix[11];
220
+ const r3 = matrix[15];
221
+ const inverted = inverse(original);
222
+ if (inverted === null) {
223
+ return false;
224
+ }
225
+ // dot product of the right hand side and of the rows of the transposed inverse
226
+ result.perspectiveX = r0 * inverted[0] + r1 * inverted[4] + r2 * inverted[8] + r3 * inverted[12];
227
+ result.perspectiveY = r0 * inverted[1] + r1 * inverted[5] + r2 * inverted[9] + r3 * inverted[13];
228
+ result.perspectiveZ = r0 * inverted[2] + r1 * inverted[6] + r2 * inverted[10] + r3 * inverted[14];
229
+ result.perspectiveW = r0 * inverted[3] + r1 * inverted[7] + r2 * inverted[11] + r3 * inverted[15];
230
+ // Clear perspective from matrix
231
+ matrix[3] = 0;
232
+ matrix[7] = 0;
233
+ matrix[11] = 0;
234
+ matrix[15] = 1;
235
+ }
236
+ // Translation
237
+ result.translateX = matrix[12];
238
+ result.translateY = matrix[13];
239
+ result.translateZ = matrix[14];
240
+ // the 3x3 matrix
241
+ const row00 = matrix[0];
242
+ const row01 = matrix[1];
243
+ const row02 = matrix[2];
244
+ const row10 = matrix[4];
245
+ const row11 = matrix[5];
246
+ const row12 = matrix[6];
247
+ const row20 = matrix[8];
248
+ const row21 = matrix[9];
249
+ const row22 = matrix[10];
250
+ const cross0 = row11 * row22 - row12 * row21;
251
+ const cross1 = row12 * row20 - row10 * row22;
252
+ const cross2 = row10 * row21 - row11 * row20;
253
+ // Compute scale
254
+ const scaleX = Math.hypot(row00, row01, row02);
255
+ // normalized row 0
256
+ let norm = Math.sqrt(row00 * row00 + row01 * row01 + row02 * row02);
257
+ const row0Norm0 = norm === 0 ? 0 : row00 / norm;
258
+ const row0Norm1 = norm === 0 ? 0 : row01 / norm;
259
+ const row0Norm2 = norm === 0 ? 0 : row02 / norm;
260
+ const skewXY = row0Norm0 * row10 + row0Norm1 * row11 + row0Norm2 * row12;
261
+ const row1Proj0 = row10 - skewXY * row0Norm0;
262
+ const row1Proj1 = row11 - skewXY * row0Norm1;
263
+ const row1Proj2 = row12 - skewXY * row0Norm2;
264
+ const scaleY = Math.hypot(row1Proj0, row1Proj1, row1Proj2);
265
+ // normalized projection of row 1
266
+ norm = Math.sqrt(row1Proj0 * row1Proj0 + row1Proj1 * row1Proj1 + row1Proj2 * row1Proj2);
267
+ const row1Norm0 = norm === 0 ? 0 : row1Proj0 / norm;
268
+ const row1Norm1 = norm === 0 ? 0 : row1Proj1 / norm;
269
+ const row1Norm2 = norm === 0 ? 0 : row1Proj2 / norm;
270
+ const skewXZ = row0Norm0 * row20 + row0Norm1 * row21 + row0Norm2 * row22;
271
+ const skewYZ = row1Norm0 * row20 + row1Norm1 * row21 + row1Norm2 * row22;
272
+ const row2Proj0 = row20 - skewXZ * row0Norm0 - skewYZ * row1Norm0;
273
+ const row2Proj1 = row21 - skewXZ * row0Norm1 - skewYZ * row1Norm1;
274
+ const row2Proj2 = row22 - skewXZ * row0Norm2 - skewYZ * row1Norm2;
275
+ // normalized projection of row 2
276
+ norm = Math.sqrt(row2Proj0 * row2Proj0 + row2Proj1 * row2Proj1 + row2Proj2 * row2Proj2);
277
+ const row2Norm0 = norm === 0 ? 0 : row2Proj0 / norm;
278
+ const row2Norm1 = norm === 0 ? 0 : row2Proj1 / norm;
279
+ const row2Norm2 = norm === 0 ? 0 : row2Proj2 / norm;
280
+ const determinant = row00 * cross0 + row01 * cross1 + row02 * cross2;
281
+ const scaleZ = Math.hypot(row2Proj0, row2Proj1, row2Proj2) * (determinant < 0 ? -1 : 1);
282
+ // Build rotation matrix from orthonormalized vectors
283
+ const r00 = row0Norm0, r01 = row1Norm0, r02 = row2Norm0;
284
+ const r10 = row0Norm1, r11 = row1Norm1, r12 = row2Norm1;
285
+ const r20 = row0Norm2, r21 = row1Norm2, r22 = row2Norm2;
286
+ // Convert to quaternion
287
+ const trace = r00 + r11 + r22;
288
+ let qw, qx, qy, qz;
289
+ if (trace > 0) {
290
+ const s = 0.5 / Math.sqrt(trace + 1.0);
291
+ qw = 0.25 / s;
292
+ qx = (r21 - r12) * s;
293
+ qy = (r02 - r20) * s;
294
+ qz = (r10 - r01) * s;
295
+ }
296
+ else if (r00 > r11 && r00 > r22) {
297
+ const s = 2.0 * Math.sqrt(1.0 + r00 - r11 - r22);
298
+ qw = (r21 - r12) / s;
299
+ qx = 0.25 * s;
300
+ qy = (r01 + r10) / s;
301
+ qz = (r02 + r20) / s;
302
+ }
303
+ else if (r11 > r22) {
304
+ const s = 2.0 * Math.sqrt(1.0 + r11 - r00 - r22);
305
+ qw = (r02 - r20) / s;
306
+ qx = (r01 + r10) / s;
307
+ qy = 0.25 * s;
308
+ qz = (r12 + r21) / s;
309
+ }
310
+ else {
311
+ const s = 2.0 * Math.sqrt(1.0 + r22 - r00 - r11);
312
+ qw = (r10 - r01) / s;
313
+ qx = (r02 + r20) / s;
314
+ qy = (r12 + r21) / s;
315
+ qz = 0.25 * s;
316
+ }
317
+ qx = toZeroValue(qx);
318
+ qy = toZeroValue(qy);
319
+ qz = toZeroValue(qz);
320
+ // smallest non zero absolute value of the axis components
321
+ let q = 0;
322
+ let abs = Math.abs(qx);
323
+ if (q === 0 || (abs > 0 && abs < q)) {
324
+ q = abs;
325
+ }
326
+ abs = Math.abs(qy);
327
+ if (q === 0 || (abs > 0 && abs < q)) {
328
+ q = abs;
329
+ }
330
+ abs = Math.abs(qz);
331
+ if (q === 0 || (abs > 0 && abs < q)) {
332
+ q = abs;
333
+ }
334
+ if (q > 0) {
335
+ qx /= q;
336
+ qy /= q;
337
+ qz /= q;
338
+ }
339
+ result.rotateX = qx;
340
+ result.rotateY = qy;
341
+ result.rotateZ = qz;
342
+ result.rotateAngle = Object.is(qw, 0) ? 0 : (2 * Math.acos(qw) * 180) / Math.PI;
343
+ result.scaleX = scaleX;
344
+ result.scaleY = scaleY;
345
+ result.scaleZ = scaleZ;
346
+ result.skewXY = skewXY;
347
+ result.skewXZ = skewXZ;
348
+ result.skewYZ = skewYZ;
349
+ return true;
350
+ }
351
+ /**
352
+ * a value close to 0 (`epsilon`) is set to 0, the other values are rounded to 6 significant digits
353
+ * @param value
354
+ */
355
+ function toZeroValue(value) {
356
+ if (Math.abs(value) <= epsilon) {
357
+ return 0;
358
+ }
359
+ // the integers that have at most 6 digits are not changed by the rounding: no string is created
360
+ if (Number.isInteger(value) && value < 1e6 && value > -1e6) {
361
+ return value;
362
+ }
363
+ return toPrecision6(value);
364
+ }
365
+ /**
366
+ * apply {@link toZeroValue} to the values of a matrix
367
+ * @param matrix
368
+ */
369
+ function toZero(matrix) {
370
+ for (let i = 0; i < 16; i++) {
371
+ matrix[i] = toZeroValue(matrix[i]);
372
+ }
373
+ return matrix;
374
+ }
375
+ // https://drafts.csswg.org/css-transforms-1/#2d-matrix
376
+ function is2DMatrix(matrix) {
377
+ // m13,m14, m23, m24, m31, m32, m34, m43 are all 0
378
+ return (matrix[2] === 0 &&
379
+ matrix[3] === 0 &&
380
+ matrix[6] === 0 &&
381
+ matrix[7] === 0 &&
382
+ matrix[8] === 0 &&
383
+ matrix[9] === 0 &&
384
+ matrix[11] === 0 &&
385
+ matrix[14] === 0 &&
386
+ matrix[10] === 1 &&
387
+ matrix[15] === 1);
388
+ }
389
+
390
+ export { decompose, identityMatrix, is2DMatrix, isIdentity, multiplyInto, round, setIdentity, toPrecision6, toZero, toZeroValue };
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { WalkerOptionEnum, EnumAstNodeStatus, WalkerEvent, EnumToken } from './types.js';
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { memoize } from '../parser/utils/cache.js';
@@ -1,14 +1,14 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { PropertySet } from './set.js';
9
9
  import { getConfig } from '../utils/config.js';
10
10
  import { PropertyMap } from './map.js';
11
- import { EnumAstNodeStatus, EnumToken } from '../../ast/types.js';
11
+ import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
12
12
  import { getParsedSyntax } from '../../validation/config.js';
13
13
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
14
14
  import { isValidDeclarationValue } from '../../validation/match.js';
@@ -108,17 +108,24 @@ class PropertyList {
108
108
  * keys shared by the lists that use the same arena
109
109
  */
110
110
  memo;
111
+ /**
112
+ * syntaxes of the descriptors of the at-rule that contains the declarations: `result` in `@function`. the
113
+ * declarations that are not validated are validated with them
114
+ */
115
+ descriptors;
111
116
  /**
112
117
  *
113
118
  * @param arena
114
119
  * @param options
115
120
  * @param memo keys shared by the lists that use the same arena
121
+ * @param descriptors syntaxes of the descriptors of the at-rule that contains the declarations
116
122
  */
117
- constructor(arena, options = {}, memo = null) {
123
+ constructor(arena, options = {}, memo = null, descriptors = null) {
118
124
  this.arena = arena;
119
125
  this.options = options;
120
126
  this.declarations = new Map();
121
127
  this.memo = memo ?? new DeclarationMemo();
128
+ this.descriptors = descriptors;
122
129
  }
123
130
  /**
124
131
  * add a declaration
@@ -131,7 +138,7 @@ class PropertyList {
131
138
  name =
132
139
  arena.dataTypes[declaration] !== EnumToken.DeclarationNodeType
133
140
  ? null
134
- : arena.strings[arena.name[declaration]];
141
+ : arena.resolveString(arena.name[declaration]);
135
142
  // do not compute shorthand for invalid declarations
136
143
  if (name == null) {
137
144
  this.declarations.set(this.memo.key(arena, declaration), declaration);
@@ -162,7 +169,9 @@ class PropertyList {
162
169
  return this;
163
170
  }
164
171
  if (arena.flags[declaration] & EnumAstNodeStatus.Unvalidated) {
165
- syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(name));
172
+ syntaxRules =
173
+ this.descriptors?.[lowercase(name)] ??
174
+ getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(name));
166
175
  if (syntaxRules != null) {
167
176
  arena.flags[declaration] |=
168
177
  (isValidDeclarationValue(arena, syntaxRules, declaration, this.options)
@@ -272,6 +281,14 @@ class PropertyList {
272
281
  if (shorthand != null) {
273
282
  this.mapValues(declaration, shorthand);
274
283
  }
284
+ const existing = this.declarations.get(propertyName);
285
+ // a previous declaration with the same name takes precedence when it is !important and the new one is not
286
+ if (existing != null &&
287
+ typeof existing === "number" &&
288
+ arena.flags[existing] & EnumAstNodeStatus.ImportantDeclaration &&
289
+ (arena.flags[declaration] & EnumAstNodeStatus.ImportantDeclaration) == 0) {
290
+ return this;
291
+ }
275
292
  this.declarations.set(propertyName, declaration);
276
293
  }
277
294
  return this;
@@ -305,7 +322,7 @@ class PropertyList {
305
322
  default: {
306
323
  for (const v of set) {
307
324
  if (arena.dataTypes[v] === EnumToken.IdenTokenType) {
308
- const value = arena.strings[arena.stringValue[v]].toLowerCase();
325
+ const value = arena.resolveString(arena.stringValue[v]).toLowerCase();
309
326
  if (value === pattern || new RegExp(`(^|\|)${pattern}(\||$)`).test(value)) {
310
327
  val.push(v);
311
328
  set.delete(v);
@@ -329,7 +346,7 @@ class PropertyList {
329
346
  }
330
347
  for (const val of declarationValues) {
331
348
  if (arena.dataTypes[val] === EnumToken.IdenTokenType) {
332
- name = arena.strings[arena.stringValue[val]].toLowerCase();
349
+ name = arena.resolveString(arena.stringValue[val]).toLowerCase();
333
350
  if (mapping.mapping[name] != null) {
334
351
  const typ = EnumToken[mapping.mapping[name].typ];
335
352
  arena.resetToken(val, typ, mapping.mapping[name].val);
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { eqToken } from '../utils/eq.js';
@@ -41,7 +41,7 @@ function configToken(arena, config) {
41
41
  function matchConfigToken(arena, token, config) {
42
42
  return (config != null &&
43
43
  arena.dataTypes[token] === configType(config) &&
44
- (config.val == null || arena.strings[arena.stringValue[token]] === config.val));
44
+ (config.val == null || arena.resolveString(arena.stringValue[token]) === config.val));
45
45
  }
46
46
  /**
47
47
  * map of declarations that can be merged into a single shorthand declaration.
@@ -83,7 +83,7 @@ class PropertyMap {
83
83
  */
84
84
  add(declaration) {
85
85
  const arena = this.arena;
86
- const nam = arena.strings[arena.name[declaration]];
86
+ const nam = arena.resolveString(arena.name[declaration]);
87
87
  if (nam === this.config.shorthand) {
88
88
  this.declarations.clear();
89
89
  this.declarations.set(nam, declaration);
@@ -270,7 +270,7 @@ class PropertyMap {
270
270
  else {
271
271
  // a property set has no name
272
272
  key =
273
- value instanceof PropertySet ? "undefined" : arena.strings[arena.name[value]];
273
+ value instanceof PropertySet ? "undefined" : arena.resolveString(arena.name[value]);
274
274
  mapped[key] = value;
275
275
  }
276
276
  }
@@ -413,7 +413,7 @@ class PropertyMap {
413
413
  if (declaration instanceof PropertySet || declaration instanceof PropertyMap) {
414
414
  continue;
415
415
  }
416
- const nam = arena.strings[arena.name[declaration]];
416
+ const nam = arena.resolveString(arena.name[declaration]);
417
417
  const config = nam === this.config.shorthand ? this.config : (propertiesMapping[nam] ?? this.config);
418
418
  if (!("mapping" in config)) {
419
419
  continue;
@@ -474,7 +474,7 @@ class PropertyMap {
474
474
  props.separator != null &&
475
475
  configType(props.separator) === valType &&
476
476
  (valType === EnumToken.CommaTokenType ||
477
- props.separator.val === arena.strings[arena.stringValue[val]])) {
477
+ props.separator.val === arena.resolveString(arena.stringValue[val]))) {
478
478
  continue;
479
479
  }
480
480
  match = matchType(arena, val, curr[1]);
@@ -621,7 +621,7 @@ class PropertyMap {
621
621
  EnumToken.IdenTokenType,
622
622
  ].includes(arena.dataTypes[v]) ||
623
623
  (arena.dataTypes[v] === EnumToken.IdenTokenType &&
624
- !propertiesMapping[curr[0]].default.includes(arena.strings[arena.stringValue[v]]))) {
624
+ !propertiesMapping[curr[0]].default.includes(arena.resolveString(arena.stringValue[v])))) {
625
625
  doFilterDefault = false;
626
626
  break;
627
627
  }
@@ -635,7 +635,7 @@ class PropertyMap {
635
635
  }
636
636
  return (!doFilterDefault ||
637
637
  !(arena.dataTypes[val] === EnumToken.IdenTokenType &&
638
- props.default.includes(arena.strings[arena.stringValue[val]])));
638
+ props.default.includes(arena.resolveString(arena.stringValue[val]))));
639
639
  });
640
640
  if (filtered.length > 0 ||
641
641
  !(this.requiredCount == requiredCount && propertiesMapping[curr[0]].required)) {
@@ -651,15 +651,15 @@ class PropertyMap {
651
651
  while (i--) {
652
652
  if (arena.dataTypes[values[i]] === EnumToken.IdenTokenType &&
653
653
  // @ts-expect-error
654
- arena.strings[arena.stringValue[values[i]]] in props.mapping) {
655
- // inlined: this.parse(props.mapping[arena.strings[arena.stringValue[values[i]]]])
654
+ arena.resolveString(arena.stringValue[values[i]]) in props.mapping) {
655
+ // inlined: this.parse(props.mapping[arena.resolveString(arena.stringValue[values[i]])])
656
656
  values.splice(i, 1, ...(this.memo == null
657
657
  ? parseString(this.arena,
658
658
  // @ts-ignore
659
- props.mapping[arena.strings[arena.stringValue[values[i]]]])
659
+ props.mapping[arena.resolveString(arena.stringValue[values[i]])])
660
660
  : this.memo.parse(this.arena,
661
661
  // @ts-ignore
662
- props.mapping[arena.strings[arena.stringValue[values[i]]]])));
662
+ props.mapping[arena.resolveString(arena.stringValue[values[i]])])));
663
663
  }
664
664
  }
665
665
  }
@@ -722,8 +722,8 @@ class PropertyMap {
722
722
  }
723
723
  if (values.length === 1 &&
724
724
  arena.stringValue[values[0]] !== 0 &&
725
- this.config.default.includes(arena.strings[arena.stringValue[values[0]]].toLowerCase()) &&
726
- this.config.default[0] !== arena.strings[arena.stringValue[values[0]]].toLowerCase()) {
725
+ this.config.default.includes(arena.resolveString(arena.stringValue[values[0]]).toLowerCase()) &&
726
+ this.config.default[0] !== arena.resolveString(arena.stringValue[values[0]]).toLowerCase()) {
727
727
  // inlined: this.parse(this.config.default[0])
728
728
  values =
729
729
  this.memo == null
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { numericTokenTypes } from '../../ast/arena.js';
@@ -57,7 +57,7 @@ class PropertySet {
57
57
  }
58
58
  add(declaration) {
59
59
  const arena = this.arena;
60
- const nam = arena.strings[arena.name[declaration]];
60
+ const nam = arena.resolveString(arena.name[declaration]);
61
61
  if (nam === this.config.shorthand) {
62
62
  this.declarations = new Map();
63
63
  }
@@ -86,7 +86,7 @@ class PropertySet {
86
86
  }
87
87
  if (typ !== EnumToken.WhitespaceTokenType && typ !== EnumToken.CommentTokenType) {
88
88
  if (typ === EnumToken.IdenTokenType &&
89
- this.config.keywords.includes(arena.strings[arena.stringValue[token]])) {
89
+ this.config.keywords.includes(arena.resolveString(arena.stringValue[token]))) {
90
90
  if (tokens.length === 0) {
91
91
  tokens.push([]);
92
92
  current++;
@@ -94,7 +94,7 @@ class PropertySet {
94
94
  tokens[current].push(token);
95
95
  }
96
96
  if (typ === EnumToken.LiteralTokenType &&
97
- arena.strings[arena.stringValue[token]] === this.config.separator?.val) {
97
+ arena.resolveString(arena.stringValue[token]) === this.config.separator?.val) {
98
98
  tokens.push([]);
99
99
  current++;
100
100
  continue;