@tbela99/css-parser 1.4.7 → 1.4.9

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 (54) hide show
  1. package/CHANGELOG.md +33 -2
  2. package/dist/index-umd-web.js +12953 -13248
  3. package/dist/index.cjs +10358 -10655
  4. package/dist/index.d.ts +820 -89
  5. package/dist/lib/ast/features/calc.js +3 -1
  6. package/dist/lib/ast/features/if.js +2 -96
  7. package/dist/lib/ast/features/prefix.js +7 -2
  8. package/dist/lib/ast/features/shorthand.js +1 -1
  9. package/dist/lib/ast/features/transform.js +1 -1
  10. package/dist/lib/ast/find.js +6 -6
  11. package/dist/lib/ast/math/expression.js +30 -12
  12. package/dist/lib/ast/minify.js +34 -5
  13. package/dist/lib/ast/transform/perspective.js +1 -1
  14. package/dist/lib/ast/transform/utils.js +29 -21
  15. package/dist/lib/ast/types.js +12 -0
  16. package/dist/lib/fs/resolve.js +2 -17
  17. package/dist/lib/parser/declaration/list.js +6 -1
  18. package/dist/lib/parser/linesmap.js +78 -0
  19. package/dist/lib/parser/parse.js +1264 -151
  20. package/dist/lib/parser/source.js +103 -0
  21. package/dist/lib/parser/tokenize.js +112 -133
  22. package/dist/lib/parser/utils/at-rule-container.js +12 -44
  23. package/dist/lib/parser/utils/at-rule-generic.js +12 -24
  24. package/dist/lib/parser/utils/at-rule-import.js +4 -172
  25. package/dist/lib/parser/utils/at-rule-media.js +27 -263
  26. package/dist/lib/parser/utils/at-rule-support.js +8 -258
  27. package/dist/lib/parser/utils/at-rule-when-else.js +4 -214
  28. package/dist/lib/parser/utils/declaration.js +5 -95
  29. package/dist/lib/parser/utils/hash.js +54 -1
  30. package/dist/lib/parser/utils/selector.js +1 -1
  31. package/dist/lib/parser/utils/token.js +1 -0
  32. package/dist/lib/renderer/render.js +79 -45
  33. package/dist/lib/renderer/sourcemap/sourcemap.js +47 -43
  34. package/dist/lib/syntax/color/cmyk.js +3 -3
  35. package/dist/lib/syntax/color/color-mix.js +15 -8
  36. package/dist/lib/syntax/color/color.js +19 -11
  37. package/dist/lib/syntax/color/hex.js +1 -1
  38. package/dist/lib/syntax/color/hsl.js +6 -6
  39. package/dist/lib/syntax/color/hwb.js +6 -6
  40. package/dist/lib/syntax/color/lab.js +5 -5
  41. package/dist/lib/syntax/color/lch.js +6 -6
  42. package/dist/lib/syntax/color/oklab.js +5 -5
  43. package/dist/lib/syntax/color/oklch.js +5 -5
  44. package/dist/lib/syntax/color/p3.js +9 -1
  45. package/dist/lib/syntax/color/{relativecolor.js → relative-color.js} +8 -1
  46. package/dist/lib/syntax/color/utils/distance.js +7 -2
  47. package/dist/lib/syntax/constants.js +5 -1
  48. package/dist/lib/syntax/syntax.js +12 -21
  49. package/dist/lib/validation/config.json.js +27 -6
  50. package/dist/lib/validation/match.js +42 -845
  51. package/dist/node.js +136 -11
  52. package/dist/web.js +135 -8
  53. package/llms.txt +73 -0
  54. package/package.json +14 -17
@@ -31,13 +31,25 @@ function inverse(matrix) {
31
31
  // Create augmented matrix [matrix | identity]
32
32
  let augmented = [
33
33
  ...matrix.slice(0, 4),
34
- 1, 0, 0, 0,
34
+ 1,
35
+ 0,
36
+ 0,
37
+ 0,
35
38
  ...matrix.slice(4, 8),
36
- 0, 1, 0, 0,
39
+ 0,
40
+ 1,
41
+ 0,
42
+ 0,
37
43
  ...matrix.slice(8, 12),
38
- 0, 0, 1, 0,
44
+ 0,
45
+ 0,
46
+ 1,
47
+ 0,
39
48
  ...matrix.slice(12, 16),
40
- 0, 0, 0, 1
49
+ 0,
50
+ 0,
51
+ 0,
52
+ 1,
41
53
  ];
42
54
  // Gaussian elimination with partial pivoting
43
55
  for (let col = 0; col < 4; col++) {
@@ -52,7 +64,7 @@ function inverse(matrix) {
52
64
  }
53
65
  }
54
66
  // Check for singularity
55
- if (maxVal < 1e-5) {
67
+ if (maxVal < epsilon) {
56
68
  return null;
57
69
  }
58
70
  // Swap rows if necessary
@@ -132,11 +144,7 @@ function decompose(original) {
132
144
  const scaleX = Math.hypot(...row0);
133
145
  const row0Norm = normalize(row0);
134
146
  const skewXY = dot(row0Norm, row1);
135
- const row1Proj = [
136
- row1[0] - skewXY * row0Norm[0],
137
- row1[1] - skewXY * row0Norm[1],
138
- row1[2] - skewXY * row0Norm[2]
139
- ];
147
+ const row1Proj = [row1[0] - skewXY * row0Norm[0], row1[1] - skewXY * row0Norm[1], row1[2] - skewXY * row0Norm[2]];
140
148
  const scaleY = Math.hypot(...row1Proj);
141
149
  const row1Norm = normalize(row1Proj);
142
150
  const skewXZ = dot(row0Norm, row2);
@@ -144,7 +152,7 @@ function decompose(original) {
144
152
  const row2Proj = [
145
153
  row2[0] - skewXZ * row0Norm[0] - skewYZ * row1Norm[0],
146
154
  row2[1] - skewXZ * row0Norm[1] - skewYZ * row1Norm[1],
147
- row2[2] - skewXZ * row0Norm[2] - skewYZ * row1Norm[2]
155
+ row2[2] - skewXZ * row0Norm[2] - skewYZ * row1Norm[2],
148
156
  ];
149
157
  const row2Norm = normalize(row2Proj);
150
158
  const determinant = row0[0] * cross[0] + row0[1] * cross[1] + row0[2] * cross[2];
@@ -196,7 +204,12 @@ function decompose(original) {
196
204
  qy /= q;
197
205
  qz /= q;
198
206
  }
199
- const rotate = [qx, qy, qz, Object.is(qw, 0) ? 0 : 2 * Math.acos(qw) * 180 / Math.PI];
207
+ const rotate = [
208
+ qx,
209
+ qy,
210
+ qz,
211
+ Object.is(qw, 0) ? 0 : (2 * Math.acos(qw) * 180) / Math.PI,
212
+ ];
200
213
  const scale = [scaleX, scaleY, scaleZ];
201
214
  const skew = [skewXY, skewXZ, skewYZ];
202
215
  return {
@@ -204,16 +217,11 @@ function decompose(original) {
204
217
  scale,
205
218
  rotate,
206
219
  skew,
207
- perspective
220
+ perspective,
208
221
  };
209
222
  }
210
223
  function transposeMatrix4(m) {
211
- return [
212
- m[0], m[4], m[8], m[12],
213
- m[1], m[5], m[9], m[13],
214
- m[2], m[6], m[10], m[14],
215
- m[3], m[7], m[11], m[15],
216
- ];
224
+ return [m[0], m[4], m[8], m[12], m[1], m[5], m[9], m[13], m[2], m[6], m[10], m[14], m[3], m[7], m[11], m[15]];
217
225
  }
218
226
  function toZero(v) {
219
227
  for (let i = 0; i < v.length; i++) {
@@ -229,7 +237,7 @@ function toZero(v) {
229
237
  // https://drafts.csswg.org/css-transforms-1/#2d-matrix
230
238
  function is2DMatrix(matrix) {
231
239
  // m13,m14, m23, m24, m31, m32, m34, m43 are all 0
232
- return matrix[2] === 0 &&
240
+ return (matrix[2] === 0 &&
233
241
  matrix[3] === 0 &&
234
242
  matrix[6] === 0 &&
235
243
  matrix[7] === 0 &&
@@ -238,7 +246,7 @@ function is2DMatrix(matrix) {
238
246
  matrix[11] === 0 &&
239
247
  matrix[14] === 0 &&
240
248
  matrix[10] === 1 &&
241
- matrix[15] === 1;
249
+ matrix[15] === 1);
242
250
  }
243
251
 
244
252
  export { decompose, identity, is2DMatrix, multiply, round, toZero };
@@ -879,6 +879,18 @@ var ColorType;
879
879
  * alpha() color function
880
880
  */
881
881
  ColorType[ColorType["ALPHA"] = 25] = "ALPHA";
882
+ /**
883
+ * color using display-p3-linear
884
+ */
885
+ ColorType[ColorType["DISPLAY_P3_LINEAR"] = 26] = "DISPLAY_P3_LINEAR";
886
+ /**
887
+ * Contrast color
888
+ */
889
+ ColorType[ColorType["CONTRAST_COLOR"] = 27] = "CONTRAST_COLOR";
890
+ /**
891
+ * color layers
892
+ */
893
+ ColorType[ColorType["COLOR_LAYERS"] = 28] = "COLOR_LAYERS";
882
894
  /**
883
895
  * alias for rgba
884
896
  */
@@ -140,7 +140,7 @@ const resolve = memoize(function (url, currentDirectory, cwd) {
140
140
  };
141
141
  }
142
142
  }
143
- if (currentDirectory === "" && cwd !== "") {
143
+ if ((currentDirectory === "" || currentDirectory === ".") && cwd !== "") {
144
144
  cwd = normalize(cwd);
145
145
  if (url.startsWith(cwd == "/" ? cwd : cwd + "/")) {
146
146
  const absolute = url;
@@ -151,24 +151,9 @@ const resolve = memoize(function (url, currentDirectory, cwd) {
151
151
  };
152
152
  }
153
153
  }
154
- // if (matchUrl.test(currentDirectory)) {
155
- // const path: string = new URL(url, currentDirectory).href;
156
- // return {
157
- // absolute: path,
158
- // relative: path,
159
- // };
160
- // }
161
- // let result: string = "";
162
- // if (url.charAt(0) == "/") {
163
- // result = url;
164
- // }
165
- // else if (currentDirectory.charAt(0) == "/") {
166
- // result = dirname(currentDirectory) + "/" + url;
167
- // }
168
- // const absolute = url; // normalize(result);
169
154
  return {
170
155
  absolute: url,
171
- relative: url === "" ? "" : diff(url, cwd ?? currentDirectory),
156
+ relative: url === "" ? "" : diff(url, cwd || currentDirectory),
172
157
  };
173
158
  });
174
159
 
@@ -7,6 +7,7 @@ import { getParsedSyntax } from '../../validation/config.js';
7
7
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
8
8
  import { matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
9
9
  import { STATE } from '../../syntax/constants.js';
10
+ import { objectHash } from '../utils/hash.js';
10
11
 
11
12
  const config = getConfig();
12
13
  class PropertyList {
@@ -42,7 +43,11 @@ class PropertyList {
42
43
  (Array.isArray(this.options.removeDuplicateDeclarations)
43
44
  ? this.options.removeDuplicateDeclarations.includes(declaration.nam)
44
45
  : !this.options.removeDuplicateDeclarations)) {
45
- this.declarations.set(Number(Math.random().toString().slice(2)).toString(36), declaration);
46
+ const key = objectHash(declaration);
47
+ if (this.declarations.has(key)) {
48
+ this.declarations.delete(key);
49
+ }
50
+ this.declarations.set(key, declaration);
46
51
  continue;
47
52
  }
48
53
  if (!this.options.computeShorthand) {
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Compute line and column of the offset
3
+ */
4
+ class LineMap {
5
+ /**
6
+ * line starts
7
+ */
8
+ lineStarts;
9
+ /**
10
+ * Constructor
11
+ * @param lines
12
+ */
13
+ constructor(lines) {
14
+ if (lines.length === 0) {
15
+ lines.push(0);
16
+ }
17
+ this.lineStarts = lines;
18
+ }
19
+ /**
20
+ * Compute line and column of the offset
21
+ * @param offset
22
+ * @returns
23
+ */
24
+ getOffsets(offset) {
25
+ const line = this.search(offset);
26
+ if (offset < 0 || line < 0) {
27
+ return [1, 1];
28
+ }
29
+ const column = offset - this.lineStarts[line];
30
+ // [line, column]
31
+ return [line + 1, column === 0 ? 1 : column];
32
+ }
33
+ /**
34
+ * search the greatest index of the value less than or equal to offset
35
+ * @param offset
36
+ * @returns
37
+ */
38
+ search(offset) {
39
+ // search lineStarts using binary search
40
+ let start = 0;
41
+ let end = this.lineStarts.length - 1;
42
+ let mid = 0;
43
+ let result = -1;
44
+ while (start <= end) {
45
+ mid = start + ((end - start) >>> 1);
46
+ if (this.lineStarts[mid] <= offset) {
47
+ result = mid;
48
+ start = mid + 1;
49
+ }
50
+ else if (this.lineStarts[mid] > offset) {
51
+ end = mid - 1;
52
+ }
53
+ }
54
+ return result;
55
+ }
56
+ /**
57
+ * get line starts
58
+ * @returns
59
+ */
60
+ getLineStarts() {
61
+ return this.lineStarts;
62
+ }
63
+ /**
64
+ * add line start
65
+ */
66
+ addLineStart(lineStart) {
67
+ this.lineStarts.push(lineStart);
68
+ }
69
+ /**
70
+ * clone the linemap
71
+ * @returns
72
+ */
73
+ clone() {
74
+ return new LineMap(this.lineStarts.slice());
75
+ }
76
+ }
77
+
78
+ export { LineMap };