@tbela99/css-parser 1.5.0 → 1.6.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 (73) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +7 -7
  3. package/dist/index-umd-web.js +5562 -4731
  4. package/dist/index.cjs +5565 -4736
  5. package/dist/index.d.ts +51 -37
  6. package/dist/lib/ast/expand.js +21 -7
  7. package/dist/lib/ast/features/calc.js +17 -55
  8. package/dist/lib/ast/features/if.js +7 -3
  9. package/dist/lib/ast/features/inlinecssvariables.js +6 -5
  10. package/dist/lib/ast/features/prefix.js +41 -44
  11. package/dist/lib/ast/features/shorthand.js +10 -5
  12. package/dist/lib/ast/features/transform.js +1 -1
  13. package/dist/lib/ast/math/expression.js +190 -32
  14. package/dist/lib/ast/minify.js +37 -14
  15. package/dist/lib/ast/node.js +5 -3
  16. package/dist/lib/ast/transform/compute.js +63 -4
  17. package/dist/lib/ast/transform/minify.js +6 -6
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +1 -1
  20. package/dist/lib/ast/transform/scale.js +1 -1
  21. package/dist/lib/ast/transform/skew.js +1 -1
  22. package/dist/lib/ast/transform/translate.js +1 -1
  23. package/dist/lib/ast/transform/utils.js +46 -18
  24. package/dist/lib/ast/walk.js +28 -4
  25. package/dist/lib/fs/resolve.js +11 -2
  26. package/dist/lib/parser/declaration/list.js +22 -7
  27. package/dist/lib/parser/declaration/map.js +26 -8
  28. package/dist/lib/parser/declaration/set.js +3 -1
  29. package/dist/lib/parser/linesmap.js +2 -4
  30. package/dist/lib/parser/parse.js +388 -227
  31. package/dist/lib/parser/tokenize.js +1478 -818
  32. package/dist/lib/parser/utils/at-rule-container.js +28 -196
  33. package/dist/lib/parser/utils/at-rule-generic.js +6 -7
  34. package/dist/lib/parser/utils/at-rule-import.js +17 -13
  35. package/dist/lib/parser/utils/at-rule-media.js +36 -19
  36. package/dist/lib/parser/utils/at-rule-support.js +19 -9
  37. package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
  38. package/dist/lib/parser/utils/at-rule.js +0 -18
  39. package/dist/lib/parser/utils/declaration.js +30 -95
  40. package/dist/lib/parser/utils/hash.js +4 -4
  41. package/dist/lib/parser/utils/selector.js +89 -144
  42. package/dist/lib/parser/utils/text.js +4 -2
  43. package/dist/lib/renderer/render.js +102 -72
  44. package/dist/lib/renderer/sourcemap/sourcemap.js +2 -5
  45. package/dist/lib/syntax/color/a98rgb.js +14 -4
  46. package/dist/lib/syntax/color/cmyk.js +10 -18
  47. package/dist/lib/syntax/color/color-mix.js +21 -34
  48. package/dist/lib/syntax/color/color.js +32 -49
  49. package/dist/lib/syntax/color/hsl.js +15 -15
  50. package/dist/lib/syntax/color/hwb.js +18 -17
  51. package/dist/lib/syntax/color/lab.js +17 -17
  52. package/dist/lib/syntax/color/lch.js +13 -13
  53. package/dist/lib/syntax/color/oklab.js +10 -11
  54. package/dist/lib/syntax/color/oklch.js +15 -18
  55. package/dist/lib/syntax/color/p3.js +18 -12
  56. package/dist/lib/syntax/color/prophotorgb.js +26 -28
  57. package/dist/lib/syntax/color/rec2020.js +8 -4
  58. package/dist/lib/syntax/color/relative-color.js +26 -10
  59. package/dist/lib/syntax/color/srgb.js +5 -4
  60. package/dist/lib/syntax/color/utils/distance.js +1 -1
  61. package/dist/lib/syntax/color/xyz.js +2 -2
  62. package/dist/lib/syntax/color/xyzd50.js +2 -2
  63. package/dist/lib/syntax/constants.js +12 -2
  64. package/dist/lib/syntax/syntax.js +32 -108
  65. package/dist/lib/validation/config.json.js +3 -0
  66. package/dist/lib/validation/match.js +29 -26
  67. package/dist/node.js +16 -9
  68. package/dist/types.d.ts +5 -1
  69. package/dist/types.js +4 -0
  70. package/dist/utils/sync.js +22 -1
  71. package/dist/web.js +13 -4
  72. package/package.json +1 -1
  73. package/llms.txt +0 -80
@@ -1,4 +1,4 @@
1
- import { multiply, identity } from './utils.js';
1
+ import { identity, multiply } from './utils.js';
2
2
 
3
3
  /**
4
4
  * angle in radian
@@ -1,4 +1,4 @@
1
- import { multiply, identity } from './utils.js';
1
+ import { identity, multiply } from './utils.js';
2
2
 
3
3
  function scaleX(x, from) {
4
4
  const matrix = identity();
@@ -1,4 +1,4 @@
1
- import { multiply, identity } from './utils.js';
1
+ import { identity, multiply } from './utils.js';
2
2
 
3
3
  function skewX(x, from) {
4
4
  const matrix = identity();
@@ -1,4 +1,4 @@
1
- import { multiply, identity } from './utils.js';
1
+ import { identity, multiply } from './utils.js';
2
2
 
3
3
  function translateX(x, from) {
4
4
  const matrix = identity();
@@ -1,7 +1,8 @@
1
1
  import { epsilon } from '../../syntax/constants.js';
2
2
 
3
+ const identityMatrix = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
3
4
  function identity() {
4
- return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
5
+ return identityMatrix.slice();
5
6
  }
6
7
  function normalize(point) {
7
8
  const [x, y, z] = point;
@@ -15,37 +16,64 @@ function dot(point1, point2) {
15
16
  return point1[0] * point2[0] + point1[1] * point2[1] + point1[2] * point2[2];
16
17
  }
17
18
  function multiply(matrixA, matrixB) {
18
- let result = new Array(16).fill(0);
19
- for (let i = 0; i < 4; i++) {
20
- for (let j = 0; j < 4; j++) {
21
- for (let k = 0; k < 4; k++) {
22
- // Utiliser l'indexation linéaire pour accéder aux éléments
23
- // Pour une matrice 4x4, l'index est (row * 4 + col)
24
- result[j * 4 + i] += matrixA[k * 4 + i] * matrixB[j * 4 + k];
25
- }
26
- }
27
- }
19
+ const result = new Float32Array(16);
20
+ result[0] = matrixA[0] * matrixB[0] + matrixA[4] * matrixB[1] + matrixA[8] * matrixB[2] + matrixA[12] * matrixB[3];
21
+ result[1] = matrixA[1] * matrixB[0] + matrixA[5] * matrixB[1] + matrixA[9] * matrixB[2] + matrixA[13] * matrixB[3];
22
+ result[2] = matrixA[2] * matrixB[0] + matrixA[6] * matrixB[1] + matrixA[10] * matrixB[2] + matrixA[14] * matrixB[3];
23
+ result[3] = matrixA[3] * matrixB[0] + matrixA[7] * matrixB[1] + matrixA[11] * matrixB[2] + matrixA[15] * matrixB[3];
24
+ result[4] = matrixA[0] * matrixB[4] + matrixA[4] * matrixB[5] + matrixA[8] * matrixB[6] + matrixA[12] * matrixB[7];
25
+ result[5] = matrixA[1] * matrixB[4] + matrixA[5] * matrixB[5] + matrixA[9] * matrixB[6] + matrixA[13] * matrixB[7];
26
+ result[6] = matrixA[2] * matrixB[4] + matrixA[6] * matrixB[5] + matrixA[10] * matrixB[6] + matrixA[14] * matrixB[7];
27
+ result[7] = matrixA[3] * matrixB[4] + matrixA[7] * matrixB[5] + matrixA[11] * matrixB[6] + matrixA[15] * matrixB[7];
28
+ result[8] =
29
+ matrixA[0] * matrixB[8] + matrixA[4] * matrixB[9] + matrixA[8] * matrixB[10] + matrixA[12] * matrixB[11];
30
+ result[9] =
31
+ matrixA[1] * matrixB[8] + matrixA[5] * matrixB[9] + matrixA[9] * matrixB[10] + matrixA[13] * matrixB[11];
32
+ result[10] =
33
+ matrixA[2] * matrixB[8] + matrixA[6] * matrixB[9] + matrixA[10] * matrixB[10] + matrixA[14] * matrixB[11];
34
+ result[11] =
35
+ matrixA[3] * matrixB[8] + matrixA[7] * matrixB[9] + matrixA[11] * matrixB[10] + matrixA[15] * matrixB[11];
36
+ result[12] =
37
+ matrixA[0] * matrixB[12] + matrixA[4] * matrixB[13] + matrixA[8] * matrixB[14] + matrixA[12] * matrixB[15];
38
+ result[13] =
39
+ matrixA[1] * matrixB[12] + matrixA[5] * matrixB[13] + matrixA[9] * matrixB[14] + matrixA[13] * matrixB[15];
40
+ result[14] =
41
+ matrixA[2] * matrixB[12] + matrixA[6] * matrixB[13] + matrixA[10] * matrixB[14] + matrixA[14] * matrixB[15];
42
+ result[15] =
43
+ matrixA[3] * matrixB[12] + matrixA[7] * matrixB[13] + matrixA[11] * matrixB[14] + matrixA[15] * matrixB[15];
28
44
  return result;
29
45
  }
30
46
  function inverse(matrix) {
31
47
  // Create augmented matrix [matrix | identity]
32
48
  let augmented = [
33
- ...matrix.slice(0, 4),
49
+ matrix[0],
50
+ matrix[1],
51
+ matrix[2],
52
+ matrix[3],
34
53
  1,
35
54
  0,
36
55
  0,
37
56
  0,
38
- ...matrix.slice(4, 8),
57
+ matrix[4],
58
+ matrix[5],
59
+ matrix[6],
60
+ matrix[7],
39
61
  0,
40
62
  1,
41
63
  0,
42
64
  0,
43
- ...matrix.slice(8, 12),
65
+ matrix[8],
66
+ matrix[9],
67
+ matrix[10],
68
+ matrix[11],
44
69
  0,
45
70
  0,
46
71
  1,
47
72
  0,
48
- ...matrix.slice(12, 16),
73
+ matrix[12],
74
+ matrix[13],
75
+ matrix[14],
76
+ matrix[15],
49
77
  0,
50
78
  0,
51
79
  0,
@@ -141,11 +169,11 @@ function decompose(original) {
141
169
  row1[0] * row2[1] - row1[1] * row2[0],
142
170
  ];
143
171
  // Compute scale
144
- const scaleX = Math.hypot(...row0);
172
+ const scaleX = Math.hypot(row0[0], row0[1], row0[2]);
145
173
  const row0Norm = normalize(row0);
146
174
  const skewXY = dot(row0Norm, row1);
147
175
  const row1Proj = [row1[0] - skewXY * row0Norm[0], row1[1] - skewXY * row0Norm[1], row1[2] - skewXY * row0Norm[2]];
148
- const scaleY = Math.hypot(...row1Proj);
176
+ const scaleY = Math.hypot(row1Proj[0], row1Proj[1], row1Proj[2]);
149
177
  const row1Norm = normalize(row1Proj);
150
178
  const skewXZ = dot(row0Norm, row2);
151
179
  const skewYZ = dot(row1Norm, row2);
@@ -156,7 +184,7 @@ function decompose(original) {
156
184
  ];
157
185
  const row2Norm = normalize(row2Proj);
158
186
  const determinant = row0[0] * cross[0] + row0[1] * cross[1] + row0[2] * cross[2];
159
- const scaleZ = Math.hypot(...row2Proj) * (determinant < 0 ? -1 : 1);
187
+ const scaleZ = Math.hypot(row2Proj[0], row2Proj[1], row2Proj[2]) * (determinant < 0 ? -1 : 1);
160
188
  // Build rotation matrix from orthonormalized vectors
161
189
  const r00 = row0Norm[0], r01 = row1Norm[0], r02 = row2Norm[0];
162
190
  const r10 = row0Norm[1], r11 = row1Norm[1], r12 = row2Norm[1];
@@ -259,6 +259,7 @@ function* walkValues(values, root = null, filter, reverse) {
259
259
  (Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
260
260
  (typeof filter.type == "function" && filter.type(value));
261
261
  if (isValid) {
262
+ // @ts-ignore
262
263
  option = filter.fn(value, map.get(value) ?? root, WalkerEvent.Enter,
263
264
  // @ts-expect-error
264
265
  function* () {
@@ -281,8 +282,13 @@ function* walkValues(values, root = null, filter, reverse) {
281
282
  const op = Array.isArray(option) ? option : [option];
282
283
  for (const o of op) {
283
284
  map.set(o, map.get(value) ?? root);
285
+ if (reverse) {
286
+ stack.unshift(o);
287
+ }
288
+ else {
289
+ stack.push(o);
290
+ }
284
291
  }
285
- stack[reverse ? "push" : "unshift"](...op);
286
292
  }
287
293
  }
288
294
  }
@@ -313,8 +319,13 @@ function* walkValues(values, root = null, filter, reverse) {
313
319
  const sliced = value.chi.slice();
314
320
  for (const child of sliced) {
315
321
  map.set(child, value);
322
+ if (reverse) {
323
+ stack.unshift(child);
324
+ }
325
+ else {
326
+ stack.push(child);
327
+ }
316
328
  }
317
- stack[reverse ? "push" : "unshift"](...sliced);
318
329
  }
319
330
  else {
320
331
  const values = [];
@@ -347,7 +358,14 @@ function* walkValues(values, root = null, filter, reverse) {
347
358
  }
348
359
  }
349
360
  if (values.length > 0) {
350
- stack[reverse ? "push" : "unshift"](...values);
361
+ for (const v of values) {
362
+ if (reverse) {
363
+ stack.unshift(v);
364
+ }
365
+ else {
366
+ stack.push(v);
367
+ }
368
+ }
351
369
  }
352
370
  }
353
371
  }
@@ -357,14 +375,20 @@ function* walkValues(values, root = null, filter, reverse) {
357
375
  (Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
358
376
  (typeof filter.type == "function" && filter.type(value));
359
377
  if (isValid) {
378
+ // @ts-ignore
360
379
  option = filter.fn(value, map.get(value), WalkerEvent.Leave);
361
380
  // @ts-ignore
362
381
  if (option != null && ("typ" in option || Array.isArray(option))) {
363
382
  const op = Array.isArray(option) ? option : [option];
364
383
  for (const o of op) {
365
384
  map.set(o, map.get(value) ?? root);
385
+ if (reverse) {
386
+ stack.unshift(o);
387
+ }
388
+ else {
389
+ stack.push(o);
390
+ }
366
391
  }
367
- stack[reverse ? "push" : "unshift"](...op);
368
392
  }
369
393
  }
370
394
  }
@@ -4,6 +4,7 @@ import { memoize } from '../parser/utils/cache.js';
4
4
  * match url
5
5
  */
6
6
  const matchUrl = /^(https?:)?\/\//;
7
+ const windowsPathnameRegexp = /^\/?[a-zA-Z]:/;
7
8
  /**
8
9
  * return the directory name of a path
9
10
  * @param path
@@ -76,6 +77,9 @@ const normalize = memoize(function (path) {
76
77
  if (path.includes("\\")) {
77
78
  path = path.replace(/(\\)/g, "/");
78
79
  }
80
+ if (windowsPathnameRegexp.test(path)) {
81
+ path = path.replace(windowsPathnameRegexp, "");
82
+ }
79
83
  for (; i < path.length; i++) {
80
84
  const chr = path.charAt(i);
81
85
  if (chr == "/") {
@@ -148,8 +152,13 @@ const resolve = memoize(function (url, currentDirectory, cwd) {
148
152
  if (currentDirectory !== "") {
149
153
  currentDirectory = normalize(currentDirectory);
150
154
  }
151
- const dir = cwd || currentDirectory;
152
- const absolute = dir == "" || url.startsWith("/") || url.match(/^[a-zA-Z]:/) ? resolvePath(url) : resolvePath(dir, url);
155
+ let dir = cwd || currentDirectory;
156
+ if (windowsPathnameRegexp.test(dir)) {
157
+ dir = dir.replace(windowsPathnameRegexp, "");
158
+ }
159
+ const absolute = dir == "" || url.startsWith("/") || url.startsWith(dir) || windowsPathnameRegexp.test(url)
160
+ ? resolvePath(url)
161
+ : resolvePath(dir, url);
153
162
  return {
154
163
  absolute,
155
164
  relative: dir === "" ? absolute : diff(absolute, dir),
@@ -8,11 +8,13 @@ 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
10
  import { objectHash } from '../utils/hash.js';
11
+ import { equalsIgnoreCase } from '../utils/text.js';
11
12
 
12
13
  const config = getConfig();
13
14
  class PropertyList {
14
15
  options = { removeDuplicateDeclarations: true, computeShorthand: true };
15
16
  declarations;
17
+ // ketsey = new Map;
16
18
  constructor(options = {}) {
17
19
  this.options = options;
18
20
  this.declarations = new Map();
@@ -29,15 +31,12 @@ class PropertyList {
29
31
  let syntaxRules = null;
30
32
  let result;
31
33
  for (const declaration of declarations) {
32
- name =
33
- declaration.typ != EnumToken.DeclarationNodeType
34
- ? null
35
- : declaration.nam.toLowerCase();
34
+ name = declaration.typ != EnumToken.DeclarationNodeType ? null : declaration.nam;
36
35
  if (declaration[STATE] == EnumAstNodeStatus.Invalid ||
37
36
  declaration[STATE] == EnumAstNodeStatus.Unknown ||
38
37
  declaration[STATE] == EnumAstNodeStatus.ValidationFailed ||
39
38
  declaration.typ != EnumToken.DeclarationNodeType ||
40
- "composes" === name ||
39
+ equalsIgnoreCase("composes", name) ||
41
40
  (typeof this.options.removeDuplicateDeclarations === "string" &&
42
41
  this.options.removeDuplicateDeclarations === name) ||
43
42
  (Array.isArray(this.options.removeDuplicateDeclarations)
@@ -65,7 +64,21 @@ class PropertyList {
65
64
  }
66
65
  // do not compute shorthand for invalid declarations
67
66
  if (declaration[STATE] !== EnumAstNodeStatus.Validated) {
68
- this.declarations.set(declaration.nam, declaration);
67
+ // const key = objectHash(declaration);
68
+ // if (!this.ketsey.has(key)) {
69
+ // this.ketsey.set(key, [declaration.nam]);
70
+ // console.error(
71
+ // `Adding declaration : ${(<AstDeclaration>declaration).nam} with key : ${key}`
72
+ // )
73
+ // }
74
+ // else {
75
+ // console.error(
76
+ // `Duplicate declaration found: ${(<AstDeclaration>declaration).nam} with key : [ ${key} => ${this.ketsey.get(key)} ]`
77
+ // )
78
+ // console.error(JSON.stringify(toSortedString(declaration)))
79
+ // this.ketsey.get(key).push(declaration.nam);
80
+ // }
81
+ this.declarations.set(objectHash(declaration), declaration);
69
82
  return this;
70
83
  }
71
84
  let propertyName = declaration.nam;
@@ -181,7 +194,9 @@ class PropertyList {
181
194
  }
182
195
  if (values != declaration.val) {
183
196
  declaration.val.length = 0;
184
- declaration.val.push(...values);
197
+ for (const v of values) {
198
+ declaration.val.push(v);
199
+ }
185
200
  }
186
201
  }
187
202
  [Symbol.iterator]() {
@@ -123,10 +123,17 @@ class PropertyMap {
123
123
  else {
124
124
  if (current == tokens[property].length) {
125
125
  tokens[property].push([]);
126
- tokens[property][current].push(...defaults);
126
+ for (let i = 0; i < defaults.length; i++) {
127
+ tokens[property][current].push(defaults[i]);
128
+ }
127
129
  }
128
130
  else {
129
- tokens[property][current].push({ typ: EnumToken.WhitespaceTokenType }, ...defaults);
131
+ tokens[property][current].push({
132
+ typ: EnumToken.WhitespaceTokenType,
133
+ });
134
+ for (let i = 0; i < defaults.length; i++) {
135
+ tokens[property][current].push(defaults[i]);
136
+ }
130
137
  }
131
138
  }
132
139
  }
@@ -143,7 +150,9 @@ class PropertyMap {
143
150
  if (acc.length > 0) {
144
151
  acc.push({ ...separator });
145
152
  }
146
- acc.push(...curr);
153
+ for (let i = 0; i < curr.length; i++) {
154
+ acc.push(curr[i]);
155
+ }
147
156
  return acc;
148
157
  }, []),
149
158
  });
@@ -278,7 +287,9 @@ class PropertyMap {
278
287
  };
279
288
  const values = [...this.declarations.values()].reduce((acc, curr) => {
280
289
  if (curr instanceof PropertySet) {
281
- acc.push(...curr);
290
+ for (const declaration of curr) {
291
+ acc.push(declaration);
292
+ }
282
293
  }
283
294
  else {
284
295
  acc.push(curr);
@@ -500,7 +511,7 @@ class PropertyMap {
500
511
  else if (acc[i].length > 0) {
501
512
  acc[i].push({ typ: EnumToken.WhitespaceTokenType });
502
513
  }
503
- acc[i].push(...values.reduce((acc, curr) => {
514
+ for (const v of values.reduce((acc, curr) => {
504
515
  if (acc.length > 0) {
505
516
  // @ts-ignore
506
517
  acc.push({
@@ -514,7 +525,9 @@ class PropertyMap {
514
525
  // @ts-ignore
515
526
  acc.push(curr);
516
527
  return acc;
517
- }, []));
528
+ }, [])) {
529
+ acc[i].push(v);
530
+ }
518
531
  }
519
532
  }
520
533
  return acc;
@@ -532,7 +545,9 @@ class PropertyMap {
532
545
  return acc;
533
546
  }, []));
534
547
  }
535
- acc.push(...curr);
548
+ for (const c of curr) {
549
+ acc.push(c);
550
+ }
536
551
  return acc;
537
552
  }, []);
538
553
  if (this.config.mapping != null) {
@@ -600,10 +615,13 @@ class PropertyMap {
600
615
  }
601
616
  matchTypes(declaration) {
602
617
  const patterns = this.pattern.slice();
603
- const values = [...declaration.val];
618
+ const values = [];
604
619
  let i;
605
620
  let j;
606
621
  const map = new Map();
622
+ for (i = 0; i < declaration.val.length; i++) {
623
+ values.push(declaration.val[i]);
624
+ }
607
625
  for (i = 0; i < patterns.length; i++) {
608
626
  for (j = 0; j < values.length; j++) {
609
627
  if (!map.has(patterns[i])) {
@@ -182,7 +182,9 @@ class PropertySet {
182
182
  // @ts-ignore
183
183
  acc.push({ ...this.config.separator, typ: EnumToken.LiteralTokenType });
184
184
  }
185
- acc.push(...curr);
185
+ for (const token of curr) {
186
+ acc.push(token);
187
+ }
186
188
  return acc;
187
189
  }, []),
188
190
  },
@@ -23,11 +23,9 @@ class LineMap {
23
23
  */
24
24
  getOffsets(offset) {
25
25
  const line = this.search(offset);
26
- // if (offset < 0 || line < 0) {
27
- // return [1, 1];
28
- // }
26
+ const column = offset - this.lineStarts[line];
29
27
  // [line, column]
30
- return [line + 1, offset - this.lineStarts[line] + 1];
28
+ return [line + 1, line == 0 ? column + 1 : column];
31
29
  }
32
30
  /**
33
31
  * search the greatest index of the value less than or equal to offset