@tbela99/css-parser 1.1.1 → 1.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 (107) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +53 -6
  3. package/dist/index-umd-web.js +5503 -5037
  4. package/dist/index.cjs +5510 -5044
  5. package/dist/index.d.ts +148 -41
  6. package/dist/lib/ast/expand.js +81 -65
  7. package/dist/lib/ast/features/calc.js +14 -36
  8. package/dist/lib/ast/features/inlinecssvariables.js +6 -17
  9. package/dist/lib/ast/features/prefix.js +22 -19
  10. package/dist/lib/ast/features/shorthand.js +1 -1
  11. package/dist/lib/ast/features/transform.js +17 -2
  12. package/dist/lib/ast/math/expression.js +74 -172
  13. package/dist/lib/ast/math/math.js +24 -22
  14. package/dist/lib/ast/minify.js +249 -199
  15. package/dist/lib/ast/transform/compute.js +18 -41
  16. package/dist/lib/ast/transform/matrix.js +36 -36
  17. package/dist/lib/ast/transform/minify.js +37 -59
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +13 -13
  20. package/dist/lib/ast/transform/scale.js +8 -8
  21. package/dist/lib/ast/transform/skew.js +4 -4
  22. package/dist/lib/ast/transform/translate.js +8 -8
  23. package/dist/lib/ast/transform/utils.js +80 -28
  24. package/dist/lib/ast/types.js +122 -2
  25. package/dist/lib/fs/resolve.js +1 -14
  26. package/dist/lib/parser/declaration/list.js +1 -1
  27. package/dist/lib/parser/declaration/map.js +1 -1
  28. package/dist/lib/parser/declaration/set.js +3 -3
  29. package/dist/lib/parser/parse.js +53 -107
  30. package/dist/lib/parser/tokenize.js +29 -53
  31. package/dist/lib/parser/utils/declaration.js +1 -1
  32. package/dist/lib/parser/utils/type.js +2 -2
  33. package/dist/lib/renderer/render.js +79 -194
  34. package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
  35. package/dist/lib/syntax/color/cmyk.js +104 -0
  36. package/dist/lib/{renderer → syntax}/color/color-mix.js +31 -33
  37. package/dist/lib/syntax/color/color.js +581 -0
  38. package/dist/lib/syntax/color/hex.js +179 -0
  39. package/dist/lib/syntax/color/hsl.js +201 -0
  40. package/dist/lib/syntax/color/hwb.js +204 -0
  41. package/dist/lib/syntax/color/lab.js +262 -0
  42. package/dist/lib/syntax/color/lch.js +194 -0
  43. package/dist/lib/syntax/color/oklab.js +237 -0
  44. package/dist/lib/syntax/color/oklch.js +166 -0
  45. package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
  46. package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
  47. package/dist/lib/{renderer → syntax}/color/relativecolor.js +54 -41
  48. package/dist/lib/syntax/color/rgb.js +140 -0
  49. package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
  50. package/dist/lib/syntax/color/utils/components.js +46 -0
  51. package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
  52. package/dist/lib/syntax/color/utils/distance.js +40 -0
  53. package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
  54. package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
  55. package/dist/lib/syntax/syntax.js +78 -77
  56. package/dist/lib/syntax/utils.js +70 -0
  57. package/dist/lib/validation/at-rules/container.js +1 -1
  58. package/dist/lib/validation/at-rules/counter-style.js +1 -1
  59. package/dist/lib/validation/at-rules/custom-media.js +1 -1
  60. package/dist/lib/validation/at-rules/document.js +2 -2
  61. package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
  62. package/dist/lib/validation/at-rules/import.js +5 -5
  63. package/dist/lib/validation/at-rules/keyframes.js +3 -13
  64. package/dist/lib/validation/at-rules/layer.js +1 -1
  65. package/dist/lib/validation/at-rules/media.js +1 -1
  66. package/dist/lib/validation/at-rules/namespace.js +1 -1
  67. package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
  68. package/dist/lib/validation/at-rules/page.js +1 -1
  69. package/dist/lib/validation/at-rules/supports.js +7 -7
  70. package/dist/lib/validation/at-rules/when.js +1 -1
  71. package/dist/lib/validation/atrule.js +2 -2
  72. package/dist/lib/validation/config.js +0 -3
  73. package/dist/lib/validation/config.json.js +1 -1
  74. package/dist/lib/validation/parser/parse.js +8 -11
  75. package/dist/lib/validation/selector.js +1 -9
  76. package/dist/lib/validation/syntax.js +67 -137
  77. package/dist/lib/validation/syntaxes/complex-selector-list.js +2 -19
  78. package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
  79. package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
  80. package/dist/lib/validation/syntaxes/family-name.js +5 -40
  81. package/dist/lib/validation/syntaxes/keyframe-selector.js +3 -22
  82. package/dist/lib/validation/syntaxes/layer-name.js +1 -1
  83. package/dist/lib/validation/syntaxes/relative-selector-list.js +1 -25
  84. package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
  85. package/dist/lib/validation/syntaxes/url.js +3 -34
  86. package/dist/lib/validation/utils/list.js +2 -9
  87. package/dist/lib/validation/utils/whitespace.js +1 -1
  88. package/dist/node/index.js +4 -2
  89. package/dist/web/index.js +4 -2
  90. package/package.json +4 -4
  91. package/.editorconfig +0 -484
  92. package/dist/lib/ast/transform/convert.js +0 -33
  93. package/dist/lib/ast/utils/utils.js +0 -104
  94. package/dist/lib/renderer/color/color.js +0 -654
  95. package/dist/lib/renderer/color/hex.js +0 -105
  96. package/dist/lib/renderer/color/hsl.js +0 -125
  97. package/dist/lib/renderer/color/hwb.js +0 -103
  98. package/dist/lib/renderer/color/lab.js +0 -148
  99. package/dist/lib/renderer/color/lch.js +0 -90
  100. package/dist/lib/renderer/color/oklab.js +0 -131
  101. package/dist/lib/renderer/color/oklch.js +0 -75
  102. package/dist/lib/renderer/color/rgb.js +0 -50
  103. package/dist/lib/renderer/color/utils/components.js +0 -34
  104. package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
  105. package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
  106. package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
  107. /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
@@ -1,9 +1,8 @@
1
1
  const epsilon = 1e-5;
2
2
  function identity() {
3
- return [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
3
+ return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
4
4
  }
5
5
  function pLength(point) {
6
- // Calcul de la norme euclidienne
7
6
  return Math.sqrt(point[0] * point[0] + point[1] * point[1] + point[2] * point[2]);
8
7
  }
9
8
  function normalize(point) {
@@ -18,23 +17,85 @@ function dot(point1, point2) {
18
17
  return point1[0] * point2[0] + point1[1] * point2[1] + point1[2] * point2[2];
19
18
  }
20
19
  function multiply(matrixA, matrixB) {
21
- let result = Array(4).fill(0).map(() => Array(4).fill(0));
20
+ let result = new Array(16).fill(0);
22
21
  for (let i = 0; i < 4; i++) {
23
22
  for (let j = 0; j < 4; j++) {
24
23
  for (let k = 0; k < 4; k++) {
25
- result[j][i] += matrixA[k][i] * matrixB[j][k];
24
+ // Utiliser l'indexation linéaire pour accéder aux éléments
25
+ // Pour une matrice 4x4, l'index est (row * 4 + col)
26
+ result[j * 4 + i] += matrixA[k * 4 + i] * matrixB[j * 4 + k];
26
27
  }
27
28
  }
28
29
  }
29
30
  return result;
30
31
  }
32
+ function inverse(matrix) {
33
+ // Create augmented matrix [matrix | identity]
34
+ let augmented = [
35
+ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
36
+ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
37
+ ];
38
+ // Gaussian elimination with partial pivoting
39
+ for (let col = 0; col < 4; col++) {
40
+ // Find pivot row with maximum absolute value
41
+ let maxRow = col;
42
+ let maxVal = Math.abs(augmented[col * 4 + col]);
43
+ for (let row = col + 1; row < 4; row++) {
44
+ let val = Math.abs(augmented[row * 4 + col]);
45
+ if (val > maxVal) {
46
+ maxVal = val;
47
+ maxRow = row;
48
+ }
49
+ }
50
+ // Check for singularity
51
+ if (maxVal < 1e-5) {
52
+ return null;
53
+ }
54
+ // Swap rows if necessary
55
+ if (maxRow !== col) {
56
+ [augmented[col], augmented[maxRow]] = [augmented[maxRow], augmented[col]];
57
+ }
58
+ // Scale pivot row to make pivot element 1
59
+ let pivot = augmented[col * 4 + col];
60
+ for (let j = 0; j < 8; j++) {
61
+ augmented[col * 4 + j] /= pivot;
62
+ }
63
+ // Eliminate column in other rows
64
+ for (let row = 0; row < 4; row++) {
65
+ if (row !== col) {
66
+ let factor = augmented[row * 4 + col];
67
+ for (let j = 0; j < 8; j++) {
68
+ augmented[row * 4 + j] -= factor * augmented[col * 4 + j];
69
+ }
70
+ }
71
+ }
72
+ }
73
+ // Extract the inverse from the right side of the augmented matrix
74
+ return augmented.slice(0, 16);
75
+ }
76
+ // function transpose(matrix: Matrix): Matrix {
77
+ // // Crée une nouvelle matrice vide 4x4
78
+ // // @ts-ignore
79
+ // let transposed: Matrix = [[], [], [], []] as Matrix;
80
+ //
81
+ // // Parcourt chaque ligne et colonne pour transposer
82
+ // for (let i = 0; i < 4; i++) {
83
+ //
84
+ // for (let j = 0; j < 4; j++) {
85
+ //
86
+ // transposed[j][i] = matrix[i][j];
87
+ // }
88
+ // }
89
+ //
90
+ // return transposed;
91
+ // }
31
92
  function round(number) {
32
93
  return Math.abs(number) < epsilon ? 0 : +number.toPrecision(6);
33
94
  }
34
95
  // translate3d(25.9808px, 0, 15px ) rotateY(60deg) skewX(49.9999deg) scale(1, 1.2)
35
96
  // translate → rotate → skew → scale
36
97
  function decompose(original) {
37
- const matrix = original.flat();
98
+ const matrix = original.slice();
38
99
  // Normalize last row
39
100
  if (matrix[15] === 0) {
40
101
  return null;
@@ -50,11 +111,12 @@ function decompose(original) {
50
111
  perspectiveMatrix[7] = 0;
51
112
  perspectiveMatrix[11] = 0;
52
113
  perspectiveMatrix[15] = 1;
53
- const inverse = invertMatrix4(perspectiveMatrix);
54
- if (!inverse) {
114
+ // @ts-ignore
115
+ const inverted = inverse(original.slice());
116
+ if (!inverted) {
55
117
  return null;
56
118
  }
57
- const transposedInverse = transposeMatrix4(inverse);
119
+ const transposedInverse = transposeMatrix4(inverted);
58
120
  perspective[0] = dot(rightHandSide, transposedInverse.slice(0, 4));
59
121
  perspective[1] = dot(rightHandSide, transposedInverse.slice(4, 8));
60
122
  perspective[2] = dot(rightHandSide, transposedInverse.slice(8, 12));
@@ -159,16 +221,6 @@ function transposeMatrix4(m) {
159
221
  m[3], m[7], m[11], m[15],
160
222
  ];
161
223
  }
162
- function invertMatrix4(m) {
163
- new Array(16);
164
- const det = m[0] * m[5] * m[10] * m[15] + m[0] * m[9] * m[14] * m[7] + m[0] * m[13] * m[6] * m[11]
165
- - m[0] * m[13] * m[10] * m[7] - m[0] * m[9] * m[6] * m[15] - m[0] * m[5] * m[14] * m[11];
166
- if (det === 0)
167
- return null;
168
- // For brevity, not implementing full inverse here — you'd normally use gl-matrix or similar.
169
- // Just use a trusted library or expand this if needed.
170
- return null; // placeholder
171
- }
172
224
  function toZero(v) {
173
225
  for (let i = 0; i < v.length; i++) {
174
226
  if (Math.abs(v[i]) <= epsilon) {
@@ -183,16 +235,16 @@ function toZero(v) {
183
235
  // https://drafts.csswg.org/css-transforms-1/#2d-matrix
184
236
  function is2DMatrix(matrix) {
185
237
  // m13,m14, m23, m24, m31, m32, m34, m43 are all 0
186
- return matrix[0][2] === 0 &&
187
- matrix[0][3] === 0 &&
188
- matrix[1][2] === 0 &&
189
- matrix[1][3] === 0 &&
190
- matrix[2][0] === 0 &&
191
- matrix[2][1] === 0 &&
192
- matrix[2][3] === 0 &&
193
- matrix[3][2] === 0 &&
194
- matrix[2][2] === 1 &&
195
- matrix[3][3] === 1;
238
+ return matrix[0 * 4 + 2] === 0 &&
239
+ matrix[0 * 4 + 3] === 0 &&
240
+ matrix[1 * 4 + 2] === 0 &&
241
+ matrix[1 * 4 + 3] === 0 &&
242
+ matrix[2 * 4 + 0] === 0 &&
243
+ matrix[2 * 4 + 1] === 0 &&
244
+ matrix[2 * 4 + 3] === 0 &&
245
+ matrix[3 * 4 + 2] === 0 &&
246
+ matrix[2 * 4 + 2] === 1 &&
247
+ matrix[3 * 4 + 3] === 1;
196
248
  }
197
249
 
198
250
  export { decompose, epsilon, identity, is2DMatrix, multiply, round, toZero };
@@ -1,8 +1,14 @@
1
+ /**
2
+ * syntax validation enum
3
+ */
1
4
  var SyntaxValidationResult;
2
5
  (function (SyntaxValidationResult) {
6
+ /** valid syntax */
3
7
  SyntaxValidationResult[SyntaxValidationResult["Valid"] = 0] = "Valid";
8
+ /** drop invalid syntax */
4
9
  SyntaxValidationResult[SyntaxValidationResult["Drop"] = 1] = "Drop";
5
- SyntaxValidationResult[SyntaxValidationResult["Lenient"] = 2] = "Lenient"; /* preserve unknown at-rules, declarations and pseudo-classes */
10
+ /** preserve unknown at-rules, declarations and pseudo-classes */
11
+ SyntaxValidationResult[SyntaxValidationResult["Lenient"] = 2] = "Lenient";
6
12
  })(SyntaxValidationResult || (SyntaxValidationResult = {}));
7
13
  /**
8
14
  * validation level enum
@@ -154,5 +160,119 @@ var EnumToken;
154
160
  EnumToken[EnumToken["TimingFunction"] = 17] = "TimingFunction";
155
161
  EnumToken[EnumToken["TimelineFunction"] = 16] = "TimelineFunction";
156
162
  })(EnumToken || (EnumToken = {}));
163
+ /**
164
+ * color types enum
165
+ */
166
+ var ColorType;
167
+ (function (ColorType) {
168
+ /**
169
+ * system colors
170
+ */
171
+ ColorType[ColorType["SYS"] = 0] = "SYS";
172
+ /**
173
+ * deprecated system colors
174
+ */
175
+ ColorType[ColorType["DPSYS"] = 1] = "DPSYS";
176
+ /**
177
+ * colors as literals
178
+ */
179
+ ColorType[ColorType["LIT"] = 2] = "LIT";
180
+ /**
181
+ * colors as hex values
182
+ */
183
+ ColorType[ColorType["HEX"] = 3] = "HEX";
184
+ /**
185
+ * colors as rgb values
186
+ */
187
+ ColorType[ColorType["RGBA"] = 4] = "RGBA";
188
+ /**
189
+ * colors as hsl values
190
+ */
191
+ ColorType[ColorType["HSLA"] = 5] = "HSLA";
192
+ /**
193
+ * colors as hwb values
194
+ */
195
+ ColorType[ColorType["HWB"] = 6] = "HWB";
196
+ /**
197
+ * colors as cmyk values
198
+ */
199
+ ColorType[ColorType["CMYK"] = 7] = "CMYK";
200
+ /**
201
+ * colors as oklab values
202
+ * */
203
+ ColorType[ColorType["OKLAB"] = 8] = "OKLAB";
204
+ /**
205
+ * colors as oklch values
206
+ * */
207
+ ColorType[ColorType["OKLCH"] = 9] = "OKLCH";
208
+ /**
209
+ * colors as lab values
210
+ */
211
+ ColorType[ColorType["LAB"] = 10] = "LAB";
212
+ /**
213
+ * colors as lch values
214
+ */
215
+ ColorType[ColorType["LCH"] = 11] = "LCH";
216
+ /**
217
+ * colors using color() function
218
+ */
219
+ ColorType[ColorType["COLOR"] = 12] = "COLOR";
220
+ /**
221
+ * color using srgb values
222
+ */
223
+ ColorType[ColorType["SRGB"] = 13] = "SRGB";
224
+ /**
225
+ * color using prophoto-rgb values
226
+ */
227
+ ColorType[ColorType["PROPHOTO_RGB"] = 14] = "PROPHOTO_RGB";
228
+ /**
229
+ * color using a98-rgb values
230
+ */
231
+ ColorType[ColorType["A98_RGB"] = 15] = "A98_RGB";
232
+ /**
233
+ * color using rec2020 values
234
+ */
235
+ ColorType[ColorType["REC2020"] = 16] = "REC2020";
236
+ /**
237
+ * color using display-p3 values
238
+ */
239
+ ColorType[ColorType["DISPLAY_P3"] = 17] = "DISPLAY_P3";
240
+ /**
241
+ * color using srgb-linear values
242
+ */
243
+ ColorType[ColorType["SRGB_LINEAR"] = 18] = "SRGB_LINEAR";
244
+ /**
245
+ * color using xyz-d50 values
246
+ */
247
+ ColorType[ColorType["XYZ_D50"] = 19] = "XYZ_D50";
248
+ /**
249
+ * color using xyz-d65 values
250
+ */
251
+ ColorType[ColorType["XYZ_D65"] = 20] = "XYZ_D65";
252
+ /**
253
+ * light-dark() color function
254
+ */
255
+ ColorType[ColorType["LIGHT_DARK"] = 21] = "LIGHT_DARK";
256
+ /**
257
+ * color-mix() color function
258
+ */
259
+ ColorType[ColorType["COLOR_MIX"] = 22] = "COLOR_MIX";
260
+ /**
261
+ * alias for rgba
262
+ */
263
+ ColorType[ColorType["RGB"] = 4] = "RGB";
264
+ /**
265
+ * alias for hsl
266
+ */
267
+ ColorType[ColorType["HSL"] = 5] = "HSL";
268
+ /**
269
+ * alias for xyz-d65
270
+ */
271
+ ColorType[ColorType["XYZ"] = 20] = "XYZ";
272
+ /**
273
+ * alias for cmyk
274
+ */
275
+ ColorType[ColorType["DEVICE_CMYK"] = 7] = "DEVICE_CMYK";
276
+ })(ColorType || (ColorType = {}));
157
277
 
158
- export { EnumToken, SyntaxValidationResult, ValidationLevel };
278
+ export { ColorType, EnumToken, SyntaxValidationResult, ValidationLevel };
@@ -71,27 +71,14 @@ function resolve(url, currentDirectory, cwd) {
71
71
  relative: path
72
72
  };
73
73
  }
74
- let result;
74
+ let result = '';
75
75
  if (url.charAt(0) == '/') {
76
76
  result = url;
77
77
  }
78
78
  else if (currentDirectory.charAt(0) == '/') {
79
79
  result = dirname(currentDirectory) + '/' + url;
80
80
  }
81
- else if (currentDirectory === '' || dirname(currentDirectory) === '') {
82
- result = url;
83
- }
84
- else {
85
- result = dirname(currentDirectory) + '/' + url;
86
- }
87
81
  let { parts, i } = splitPath(result);
88
- if (parts.length == 0) {
89
- const path = result.charAt(0) == '/' ? '/' : '';
90
- return {
91
- absolute: path,
92
- relative: path
93
- };
94
- }
95
82
  const absolute = parts.join('/');
96
83
  const { parts: dirs } = splitPath(cwd ?? currentDirectory);
97
84
  for (const p of dirs) {
@@ -5,7 +5,7 @@ import '../../ast/minify.js';
5
5
  import '../../ast/walk.js';
6
6
  import { parseString } from '../parse.js';
7
7
  import '../tokenize.js';
8
- import '../../renderer/color/utils/constants.js';
8
+ import '../../syntax/color/utils/constants.js';
9
9
  import '../../renderer/sourcemap/lib/encode.js';
10
10
  import { PropertyMap } from './map.js';
11
11
 
@@ -7,7 +7,7 @@ import '../../ast/walk.js';
7
7
  import { parseString } from '../parse.js';
8
8
  import '../tokenize.js';
9
9
  import { renderToken } from '../../renderer/render.js';
10
- import '../../renderer/color/utils/constants.js';
10
+ import '../../syntax/color/utils/constants.js';
11
11
  import { PropertySet } from './set.js';
12
12
 
13
13
  const propertiesConfig = getConfig();
@@ -6,7 +6,7 @@ import '../parse.js';
6
6
  import '../tokenize.js';
7
7
  import '../utils/config.js';
8
8
  import { isLength } from '../../syntax/syntax.js';
9
- import '../../renderer/color/utils/constants.js';
9
+ import '../../syntax/color/utils/constants.js';
10
10
  import '../../renderer/sourcemap/lib/encode.js';
11
11
 
12
12
  function dedup(values) {
@@ -15,7 +15,7 @@ function dedup(values) {
15
15
  while (i-- > 1) {
16
16
  const t = value[i];
17
17
  const k = value[i == 1 ? 0 : i % 2];
18
- if (t.val == k.val && t.val == '0') {
18
+ if (t.val == k.val && t.val == 0) {
19
19
  if ((t.typ == EnumToken.NumberTokenType && isLength(k)) ||
20
20
  (k.typ == EnumToken.NumberTokenType && isLength(t)) ||
21
21
  (isLength(k) || isLength(t))) {
@@ -52,7 +52,7 @@ class PropertySet {
52
52
  // @ts-ignore
53
53
  for (let token of this.declarations.get(this.config.shorthand).val) {
54
54
  // @ts-ignore
55
- if (this.config.types.some(t => token.typ == EnumToken[t]) || (token.typ == EnumToken.NumberTokenType && token.val == '0' &&
55
+ if (this.config.types.some(t => token.typ == EnumToken[t]) || (token.typ == EnumToken.NumberTokenType && token.val == 0 &&
56
56
  (this.config.types.includes('Length') ||
57
57
  this.config.types.includes('Angle') ||
58
58
  this.config.types.includes('Dimension')))) {