@tbela99/css-parser 1.4.11 → 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 (80) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +18 -11
  3. package/dist/index-umd-web.js +6558 -5310
  4. package/dist/index.cjs +6576 -5313
  5. package/dist/index.d.ts +805 -279
  6. package/dist/lib/ast/clone.js +13 -8
  7. package/dist/lib/ast/expand.js +65 -15
  8. package/dist/lib/ast/features/calc.js +19 -56
  9. package/dist/lib/ast/features/if.js +28 -4
  10. package/dist/lib/ast/features/inlinecssvariables.js +6 -5
  11. package/dist/lib/ast/features/prefix.js +52 -73
  12. package/dist/lib/ast/features/shorthand.js +11 -6
  13. package/dist/lib/ast/features/transform.js +6 -3
  14. package/dist/lib/ast/find.js +7 -5
  15. package/dist/lib/ast/math/expression.js +190 -32
  16. package/dist/lib/ast/minify.js +117 -49
  17. package/dist/lib/ast/node.js +52 -0
  18. package/dist/lib/ast/transform/compute.js +63 -4
  19. package/dist/lib/ast/transform/minify.js +6 -6
  20. package/dist/lib/ast/transform/perspective.js +1 -1
  21. package/dist/lib/ast/transform/rotate.js +1 -1
  22. package/dist/lib/ast/transform/scale.js +1 -1
  23. package/dist/lib/ast/transform/skew.js +1 -1
  24. package/dist/lib/ast/transform/translate.js +1 -1
  25. package/dist/lib/ast/transform/utils.js +46 -18
  26. package/dist/lib/ast/types.js +1 -1
  27. package/dist/lib/ast/walk.js +55 -11
  28. package/dist/lib/fs/resolve.js +68 -27
  29. package/dist/lib/parser/declaration/list.js +22 -7
  30. package/dist/lib/parser/declaration/map.js +40 -13
  31. package/dist/lib/parser/declaration/set.js +3 -1
  32. package/dist/lib/parser/linesmap.js +2 -12
  33. package/dist/lib/parser/parse.js +843 -912
  34. package/dist/lib/parser/source.js +16 -2
  35. package/dist/lib/parser/tokenize.js +1489 -725
  36. package/dist/lib/parser/utils/at-rule-container.js +29 -196
  37. package/dist/lib/parser/utils/at-rule-generic.js +6 -7
  38. package/dist/lib/parser/utils/at-rule-import.js +17 -13
  39. package/dist/lib/parser/utils/at-rule-media.js +36 -19
  40. package/dist/lib/parser/utils/at-rule-support.js +19 -9
  41. package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
  42. package/dist/lib/parser/utils/at-rule.js +0 -18
  43. package/dist/lib/parser/utils/declaration.js +30 -95
  44. package/dist/lib/parser/utils/hash.js +4 -4
  45. package/dist/lib/parser/utils/selector.js +164 -141
  46. package/dist/lib/parser/utils/text.js +4 -2
  47. package/dist/lib/renderer/render.js +230 -125
  48. package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
  49. package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
  50. package/dist/lib/syntax/color/a98rgb.js +14 -4
  51. package/dist/lib/syntax/color/cmyk.js +10 -18
  52. package/dist/lib/syntax/color/color-mix.js +21 -34
  53. package/dist/lib/syntax/color/color.js +32 -49
  54. package/dist/lib/syntax/color/hsl.js +15 -15
  55. package/dist/lib/syntax/color/hwb.js +18 -17
  56. package/dist/lib/syntax/color/lab.js +17 -17
  57. package/dist/lib/syntax/color/lch.js +13 -13
  58. package/dist/lib/syntax/color/oklab.js +10 -11
  59. package/dist/lib/syntax/color/oklch.js +15 -18
  60. package/dist/lib/syntax/color/p3.js +18 -12
  61. package/dist/lib/syntax/color/prophotorgb.js +26 -28
  62. package/dist/lib/syntax/color/rec2020.js +8 -4
  63. package/dist/lib/syntax/color/relative-color.js +26 -10
  64. package/dist/lib/syntax/color/srgb.js +5 -4
  65. package/dist/lib/syntax/color/utils/distance.js +1 -1
  66. package/dist/lib/syntax/color/xyz.js +2 -2
  67. package/dist/lib/syntax/color/xyzd50.js +2 -2
  68. package/dist/lib/syntax/constants.js +12 -2
  69. package/dist/lib/syntax/syntax.js +45 -258
  70. package/dist/lib/validation/config.json.js +3 -0
  71. package/dist/lib/validation/match.js +110 -27
  72. package/dist/node.js +55 -33
  73. package/dist/types.d.ts +5 -1
  74. package/dist/types.js +4 -0
  75. package/dist/utils/sync.d.ts +16 -0
  76. package/dist/utils/sync.js +73 -0
  77. package/dist/web.js +37 -30
  78. package/package.json +1 -1
  79. package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
  80. package/llms.txt +0 -73
@@ -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];
@@ -385,7 +385,7 @@ var EnumToken;
385
385
  /**
386
386
  * keyframe rule node type
387
387
  */
388
- EnumToken[EnumToken["KeyFramesRuleNodeType"] = 73] = "KeyFramesRuleNodeType";
388
+ EnumToken[EnumToken["KeyframesRuleNodeType"] = 73] = "KeyframesRuleNodeType";
389
389
  /**
390
390
  * class selector token type
391
391
  */
@@ -1,3 +1,5 @@
1
+ import { TOKENS } from '../syntax/constants.js';
2
+
1
3
  /**
2
4
  * Options for the walk function
3
5
  */
@@ -40,6 +42,8 @@ var WalkerEvent;
40
42
  * @param filter control the walk process
41
43
  * @param reverse walk in reverse order
42
44
  *
45
+ * @private
46
+ *
43
47
  * ```ts
44
48
  *
45
49
  * import {walk} from '@tbela99/css-parser';
@@ -111,11 +115,19 @@ function* walk(node, filter, reverse) {
111
115
  const parents = [node];
112
116
  const root = node;
113
117
  const map = new Map();
118
+ let options = filter;
114
119
  let isNumeric = false;
120
+ let includeValues = false;
115
121
  let i = 0;
122
+ if (options != null && typeof options == "object") {
123
+ filter = options.filter;
124
+ reverse = options.reverse;
125
+ includeValues = options.inludeValues;
126
+ }
116
127
  while ((node = parents[i++])) {
117
128
  let option = null;
118
129
  if (filter != null) {
130
+ // @ts-ignore
119
131
  option = filter(node);
120
132
  isNumeric = typeof option == "number";
121
133
  if (isNumeric) {
@@ -143,8 +155,21 @@ function* walk(node, filter, reverse) {
143
155
  },
144
156
  };
145
157
  }
146
- if ("chi" in node && (!isNumeric || (option & WalkerOptionEnum.IgnoreChildren) === 0)) {
147
- parents.splice(i, 0, ...node.chi[reverse ? "toReversed" : "slice"]());
158
+ if (includeValues) {
159
+ if (node[TOKENS] != null) {
160
+ // @ts-ignore
161
+ parents.splice(i, 0, ...(reverse ? node[TOKENS].toReversed() : node[TOKENS]));
162
+ // @ts-ignore
163
+ }
164
+ else if (Array.isArray(node.val)) {
165
+ // @ts-ignore
166
+ parents.splice(i, 0, ...(reverse ? node.val.toReversed() : node.val));
167
+ }
168
+ }
169
+ // @ts-ignore
170
+ if (node["chi"] != null && (!isNumeric || (option & WalkerOptionEnum.IgnoreChildren) === 0)) {
171
+ // @ts-ignore
172
+ parents.splice(i, 0, ...(reverse ? node.chi.toReversed() : node.chi));
148
173
  for (const child of node.chi) {
149
174
  map.set(child, node);
150
175
  }
@@ -228,17 +253,13 @@ function* walkValues(values, root = null, filter, reverse) {
228
253
  continue;
229
254
  }
230
255
  used.add(value);
231
- // parents.length = 0;
232
- // while (node != null) {
233
- // parents.push(node);
234
- // node = map.get(node) ?? null;
235
- // }
236
256
  if (filter.fn != null && eventType & WalkerEvent.Enter) {
237
257
  const isValid = filter.type == null ||
238
258
  value.typ == filter.type ||
239
259
  (Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
240
260
  (typeof filter.type == "function" && filter.type(value));
241
261
  if (isValid) {
262
+ // @ts-ignore
242
263
  option = filter.fn(value, map.get(value) ?? root, WalkerEvent.Enter,
243
264
  // @ts-expect-error
244
265
  function* () {
@@ -261,8 +282,13 @@ function* walkValues(values, root = null, filter, reverse) {
261
282
  const op = Array.isArray(option) ? option : [option];
262
283
  for (const o of op) {
263
284
  map.set(o, map.get(value) ?? root);
285
+ if (reverse) {
286
+ stack.unshift(o);
287
+ }
288
+ else {
289
+ stack.push(o);
290
+ }
264
291
  }
265
- stack[reverse ? "push" : "unshift"](...op);
266
292
  }
267
293
  }
268
294
  }
@@ -293,8 +319,13 @@ function* walkValues(values, root = null, filter, reverse) {
293
319
  const sliced = value.chi.slice();
294
320
  for (const child of sliced) {
295
321
  map.set(child, value);
322
+ if (reverse) {
323
+ stack.unshift(child);
324
+ }
325
+ else {
326
+ stack.push(child);
327
+ }
296
328
  }
297
- stack[reverse ? "push" : "unshift"](...sliced);
298
329
  }
299
330
  else {
300
331
  const values = [];
@@ -327,7 +358,14 @@ function* walkValues(values, root = null, filter, reverse) {
327
358
  }
328
359
  }
329
360
  if (values.length > 0) {
330
- 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
+ }
331
369
  }
332
370
  }
333
371
  }
@@ -337,14 +375,20 @@ function* walkValues(values, root = null, filter, reverse) {
337
375
  (Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
338
376
  (typeof filter.type == "function" && filter.type(value));
339
377
  if (isValid) {
378
+ // @ts-ignore
340
379
  option = filter.fn(value, map.get(value), WalkerEvent.Leave);
341
380
  // @ts-ignore
342
381
  if (option != null && ("typ" in option || Array.isArray(option))) {
343
382
  const op = Array.isArray(option) ? option : [option];
344
383
  for (const o of op) {
345
384
  map.set(o, map.get(value) ?? root);
385
+ if (reverse) {
386
+ stack.unshift(o);
387
+ }
388
+ else {
389
+ stack.push(o);
390
+ }
346
391
  }
347
- stack[reverse ? "push" : "unshift"](...op);
348
392
  }
349
393
  }
350
394
  }
@@ -1,6 +1,10 @@
1
1
  import { memoize } from '../parser/utils/cache.js';
2
2
 
3
+ /**
4
+ * match url
5
+ */
3
6
  const matchUrl = /^(https?:)?\/\//;
7
+ const windowsPathnameRegexp = /^\/?[a-zA-Z]:/;
4
8
  /**
5
9
  * return the directory name of a path
6
10
  * @param path
@@ -11,6 +15,9 @@ function dirname(path) {
11
15
  if (path === "") {
12
16
  return "";
13
17
  }
18
+ if (path.startsWith("data:")) {
19
+ return path;
20
+ }
14
21
  let i = 0;
15
22
  let parts = [""];
16
23
  for (; i < path.length; i++) {
@@ -34,10 +41,7 @@ function splitPath(result) {
34
41
  if (result.length == 0) {
35
42
  return { parts: [], i: 0 };
36
43
  }
37
- // if (result === "/") {
38
- // return { parts: ["/"], i: 0 };
39
- // }
40
- const parts = [""];
44
+ const parts = result == "/" ? [] : [""];
41
45
  let i = 0;
42
46
  for (; i < result.length; i++) {
43
47
  const chr = result.charAt(i);
@@ -46,7 +50,7 @@ function splitPath(result) {
46
50
  }
47
51
  // else if (chr == "?" || chr == "#") {
48
52
  // break;
49
- // }
53
+ // }
50
54
  else {
51
55
  parts[parts.length - 1] += chr;
52
56
  }
@@ -64,6 +68,8 @@ function splitPath(result) {
64
68
  }
65
69
  /**
66
70
  * Nomalize path
71
+ * @param path
72
+ * @private
67
73
  */
68
74
  const normalize = memoize(function (path) {
69
75
  let parts = [];
@@ -71,6 +77,9 @@ const normalize = memoize(function (path) {
71
77
  if (path.includes("\\")) {
72
78
  path = path.replace(/(\\)/g, "/");
73
79
  }
80
+ if (windowsPathnameRegexp.test(path)) {
81
+ path = path.replace(windowsPathnameRegexp, "");
82
+ }
74
83
  for (; i < path.length; i++) {
75
84
  const chr = path.charAt(i);
76
85
  if (chr == "/") {
@@ -92,14 +101,20 @@ const normalize = memoize(function (path) {
92
101
  while (++k < parts.length) {
93
102
  // if (parts[k] == ".") {
94
103
  // parts.splice(k--, 1);
95
- // } else
96
- if (parts[k] == "..") {
104
+ // } else
105
+ if (k > 0 && parts[k] == "..") {
97
106
  parts.splice(k - 1, 2);
98
107
  k -= 2;
99
108
  }
100
109
  }
101
110
  return (path.charAt(0) == "/" ? "/" : "") + parts.join("/");
102
111
  });
112
+ /**
113
+ * diff path
114
+ * @param path1
115
+ * @param path2
116
+ * @private
117
+ */
103
118
  const diff = memoize(function (path1, path2) {
104
119
  let { parts } = splitPath(path1);
105
120
  const { parts: dirs } = splitPath(path2);
@@ -122,39 +137,65 @@ const diff = memoize(function (path1, path2) {
122
137
  * @private
123
138
  */
124
139
  const resolve = memoize(function (url, currentDirectory, cwd) {
125
- cwd ??= "";
126
- currentDirectory ??= "";
127
140
  if (matchUrl.test(url)) {
128
141
  return {
129
142
  absolute: url,
130
143
  relative: url,
131
144
  };
132
145
  }
146
+ cwd ??= "";
147
+ currentDirectory ??= "";
133
148
  url = normalize(url);
149
+ if (cwd !== "") {
150
+ cwd = normalize(cwd);
151
+ }
134
152
  if (currentDirectory !== "") {
135
153
  currentDirectory = normalize(currentDirectory);
136
- if (url.startsWith(currentDirectory + "/")) {
137
- return {
138
- absolute: url,
139
- relative: url.slice(currentDirectory.length + 1),
140
- };
141
- }
142
154
  }
143
- if ((currentDirectory === "" || currentDirectory === ".") && cwd !== "") {
144
- cwd = normalize(cwd);
145
- if (url.startsWith(cwd == "/" ? cwd : cwd + "/")) {
146
- const absolute = url;
147
- const prefix = cwd == "/" ? cwd : cwd + "/";
148
- return {
149
- absolute,
150
- relative: absolute.startsWith(prefix) ? absolute.slice(prefix.length) : diff(absolute, cwd),
151
- };
152
- }
155
+ let dir = cwd || currentDirectory;
156
+ if (windowsPathnameRegexp.test(dir)) {
157
+ dir = dir.replace(windowsPathnameRegexp, "");
153
158
  }
159
+ const absolute = dir == "" || url.startsWith("/") || url.startsWith(dir) || windowsPathnameRegexp.test(url)
160
+ ? resolvePath(url)
161
+ : resolvePath(dir, url);
154
162
  return {
155
- absolute: url,
156
- relative: url === "" ? "" : diff(url, cwd || currentDirectory),
163
+ absolute,
164
+ relative: dir === "" ? absolute : diff(absolute, dir),
157
165
  };
158
166
  });
167
+ /**
168
+ *
169
+ * @param parts
170
+ * @returns
171
+ * @private
172
+ */
173
+ function resolvePath(...parts) {
174
+ const path = parts.filter(Boolean).join("/");
175
+ const isAbsolute = /^[\\/]/.test(path);
176
+ const segments = path.split(/[\\/]+/);
177
+ const resolved = [];
178
+ for (const segment of segments) {
179
+ if (!segment || segment === ".") {
180
+ continue;
181
+ }
182
+ if (segment === "..") {
183
+ if (resolved.length && resolved[resolved.length - 1] !== "..") {
184
+ resolved.pop();
185
+ }
186
+ else if (!isAbsolute) {
187
+ resolved.push("..");
188
+ }
189
+ }
190
+ else {
191
+ resolved.push(segment);
192
+ }
193
+ }
194
+ let result = resolved.join("/");
195
+ if (isAbsolute) {
196
+ result = "/" + result;
197
+ }
198
+ return result || (isAbsolute ? "/" : ".");
199
+ }
159
200
 
160
201
  export { diff, dirname, matchUrl, normalize, resolve };
@@ -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);
@@ -286,12 +297,21 @@ class PropertyMap {
286
297
  return acc;
287
298
  }, []);
288
299
  let isImportant = false;
289
- const filtered = values.map(removeDefaults).filter((x) => x.val.filter((t) => {
290
- if (t.typ == EnumToken.ImportantTokenType) {
291
- isImportant = true;
300
+ let dec;
301
+ const filtered = [];
302
+ for (const declaration of values) {
303
+ dec = removeDefaults(declaration);
304
+ for (const t of dec.val) {
305
+ if (t.typ == EnumToken.ImportantTokenType) {
306
+ isImportant = true;
307
+ }
308
+ if (filtered.length == 0 &&
309
+ t.typ != EnumToken.WhitespaceTokenType &&
310
+ t.typ != EnumToken.ImportantTokenType) {
311
+ filtered.push(dec);
312
+ }
292
313
  }
293
- return ![EnumToken.WhitespaceTokenType, EnumToken.ImportantTokenType].includes(t.typ);
294
- }).length > 0);
314
+ }
295
315
  if (filtered.length == 0 && this.config.default.length > 0) {
296
316
  filtered.push({
297
317
  typ: EnumToken.DeclarationNodeType,
@@ -491,7 +511,7 @@ class PropertyMap {
491
511
  else if (acc[i].length > 0) {
492
512
  acc[i].push({ typ: EnumToken.WhitespaceTokenType });
493
513
  }
494
- acc[i].push(...values.reduce((acc, curr) => {
514
+ for (const v of values.reduce((acc, curr) => {
495
515
  if (acc.length > 0) {
496
516
  // @ts-ignore
497
517
  acc.push({
@@ -505,7 +525,9 @@ class PropertyMap {
505
525
  // @ts-ignore
506
526
  acc.push(curr);
507
527
  return acc;
508
- }, []));
528
+ }, [])) {
529
+ acc[i].push(v);
530
+ }
509
531
  }
510
532
  }
511
533
  return acc;
@@ -523,7 +545,9 @@ class PropertyMap {
523
545
  return acc;
524
546
  }, []));
525
547
  }
526
- acc.push(...curr);
548
+ for (const c of curr) {
549
+ acc.push(c);
550
+ }
527
551
  return acc;
528
552
  }, []);
529
553
  if (this.config.mapping != null) {
@@ -591,10 +615,13 @@ class PropertyMap {
591
615
  }
592
616
  matchTypes(declaration) {
593
617
  const patterns = this.pattern.slice();
594
- const values = [...declaration.val];
618
+ const values = [];
595
619
  let i;
596
620
  let j;
597
621
  const map = new Map();
622
+ for (i = 0; i < declaration.val.length; i++) {
623
+ values.push(declaration.val[i]);
624
+ }
598
625
  for (i = 0; i < patterns.length; i++) {
599
626
  for (j = 0; j < values.length; j++) {
600
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
  },
@@ -10,7 +10,7 @@ class LineMap {
10
10
  * Constructor
11
11
  * @param lines
12
12
  */
13
- constructor(lines) {
13
+ constructor(lines = []) {
14
14
  if (lines.length === 0) {
15
15
  lines.push(0);
16
16
  }
@@ -23,12 +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
- }
29
26
  const column = offset - this.lineStarts[line];
30
27
  // [line, column]
31
- return [line + 1, column === 0 ? 1 : column];
28
+ return [line + 1, line == 0 ? column + 1 : column];
32
29
  }
33
30
  /**
34
31
  * search the greatest index of the value less than or equal to offset
@@ -66,13 +63,6 @@ class LineMap {
66
63
  addLineStart(lineStart) {
67
64
  this.lineStarts.push(lineStart);
68
65
  }
69
- /**
70
- * clone the linemap
71
- * @returns
72
- */
73
- clone() {
74
- return new LineMap(this.lineStarts.slice());
75
- }
76
66
  }
77
67
 
78
68
  export { LineMap };