@tbela99/css-parser 1.4.11 → 1.5.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 (37) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +11 -4
  3. package/dist/index-umd-web.js +2116 -1699
  4. package/dist/index.cjs +2128 -1694
  5. package/dist/index.d.ts +781 -269
  6. package/dist/lib/ast/clone.js +13 -8
  7. package/dist/lib/ast/expand.js +45 -9
  8. package/dist/lib/ast/features/calc.js +2 -1
  9. package/dist/lib/ast/features/if.js +24 -4
  10. package/dist/lib/ast/features/prefix.js +13 -31
  11. package/dist/lib/ast/features/shorthand.js +1 -1
  12. package/dist/lib/ast/features/transform.js +5 -2
  13. package/dist/lib/ast/find.js +7 -5
  14. package/dist/lib/ast/minify.js +80 -35
  15. package/dist/lib/ast/node.js +50 -0
  16. package/dist/lib/ast/types.js +1 -1
  17. package/dist/lib/ast/walk.js +27 -7
  18. package/dist/lib/fs/resolve.js +60 -28
  19. package/dist/lib/parser/declaration/map.js +14 -5
  20. package/dist/lib/parser/linesmap.js +5 -13
  21. package/dist/lib/parser/parse.js +489 -719
  22. package/dist/lib/parser/source.js +16 -2
  23. package/dist/lib/parser/tokenize.js +382 -278
  24. package/dist/lib/parser/utils/at-rule-container.js +2 -1
  25. package/dist/lib/parser/utils/selector.js +88 -10
  26. package/dist/lib/renderer/render.js +174 -99
  27. package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
  28. package/dist/lib/renderer/sourcemap/sourcemap.js +190 -34
  29. package/dist/lib/syntax/syntax.js +14 -151
  30. package/dist/lib/validation/match.js +81 -1
  31. package/dist/node.js +41 -26
  32. package/dist/utils/sync.d.ts +16 -0
  33. package/dist/utils/sync.js +52 -0
  34. package/dist/web.js +26 -28
  35. package/llms.txt +12 -5
  36. package/package.json +1 -1
  37. package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
@@ -17,7 +17,7 @@ const rules = [
17
17
  EnumToken.AtRuleNodeType,
18
18
  EnumToken.RuleNodeType,
19
19
  EnumToken.AtRuleTokenType,
20
- EnumToken.KeyFramesRuleNodeType,
20
+ EnumToken.KeyframesRuleNodeType,
21
21
  ];
22
22
  // @ts-ignore
23
23
  const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
@@ -29,6 +29,7 @@ const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
29
29
  * @param errors
30
30
  * @param nestingContent
31
31
  *
32
+ * @param context
32
33
  * @private
33
34
  */
34
35
  function minify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
@@ -36,22 +37,22 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
36
37
  let postprocess = false;
37
38
  let parents;
38
39
  let replacement;
39
- if (!("features" in options)) {
40
- // @ts-ignore
41
- options = {
40
+ let { sourcemap, module, ...options2 } = options;
41
+ if (!(options2.features != null)) {
42
+ options2 = {
42
43
  removeDuplicateDeclarations: true,
43
44
  computeShorthand: true,
44
45
  computeCalcExpression: true,
45
46
  removePrefix: false,
46
47
  features: [],
47
- ...options,
48
+ ...options2,
48
49
  };
49
50
  for (const feature of features) {
50
- feature.register(options);
51
+ feature.register(options2);
51
52
  }
52
- options.features.sort((a, b) => a.ordering - b.ordering);
53
+ options2.features.sort((a, b) => a.ordering - b.ordering);
53
54
  }
54
- for (const feature of options.features) {
55
+ for (const feature of options2.features) {
55
56
  if (feature.processMode & FeatureWalkMode.Pre) {
56
57
  preprocess = true;
57
58
  }
@@ -66,17 +67,20 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
66
67
  continue;
67
68
  }
68
69
  replacement = parent;
69
- for (const feature of options.features) {
70
+ for (const feature of options2.features) {
70
71
  if ((feature.processMode & FeatureWalkMode.Pre) === 0 ||
71
72
  (feature.accept != null && !feature.accept.has(parent.typ))) {
72
73
  continue;
73
74
  }
74
75
  if (rules.includes(replacement.typ) && !Array.isArray(replacement[TOKENS])) {
75
- replacement[TOKENS] = parseString(replacement.typ == EnumToken.RuleNodeType || replacement.typ === EnumToken.KeyFramesRuleNodeType
76
+ replacement[TOKENS] = parseString(replacement.typ == EnumToken.RuleNodeType || replacement.typ === EnumToken.KeyframesRuleNodeType
76
77
  ? replacement.sel
77
- : replacement.nam);
78
+ : // @ts-ignore
79
+ replacement.nam);
78
80
  }
79
- const result = feature.run(replacement, options, parent[PARENT] ?? ast, context, FeatureWalkMode.Pre);
81
+ const result = feature.run(replacement, options2,
82
+ // @ts-ignore
83
+ parent[PARENT] ?? ast, context, FeatureWalkMode.Pre);
80
84
  if (result != null) {
81
85
  replacement = result;
82
86
  }
@@ -85,24 +89,26 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
85
89
  (!Array.isArray(replacement) || replacement.length > 0) &&
86
90
  replacement != parent &&
87
91
  parent[PARENT] != null) {
92
+ // @ts-ignore
88
93
  replaceNodeOrValue(parent[PARENT], parent, replacement);
89
94
  }
90
- if ("chi" in replacement) {
95
+ // @ts-ignore
96
+ if (replacement.chi != null) {
91
97
  // @ts-ignore
92
98
  for (const node of replacement.chi) {
93
- // node[PARENT] = replacement;
99
+ node[PARENT] = replacement;
94
100
  parents.add(node);
95
101
  }
96
102
  }
97
103
  }
98
- for (const feature of options.features) {
104
+ for (const feature of options2.features) {
99
105
  if (feature.processMode & FeatureWalkMode.Pre && "cleanup" in feature) {
100
106
  // @ts-ignore
101
- feature.cleanup(ast, options, context, FeatureWalkMode.Pre);
107
+ feature.cleanup(ast, options2, context, FeatureWalkMode.Pre);
102
108
  }
103
109
  }
104
110
  }
105
- doMinify(ast, options, recursive, errors, nestingContent, context);
111
+ doMinify(ast, options2, recursive, errors, nestingContent, context);
106
112
  parents = new Set([ast]);
107
113
  for (const parent of parents) {
108
114
  if (parent.typ == EnumToken.CommentTokenType || parent.typ == EnumToken.CDOCOMMTokenType) {
@@ -110,12 +116,14 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
110
116
  }
111
117
  replacement = parent;
112
118
  if (postprocess) {
113
- for (const feature of options.features) {
119
+ for (const feature of options2.features) {
114
120
  if ((feature.processMode & FeatureWalkMode.Post) === 0 ||
115
121
  (feature.accept != null && !feature.accept.has(parent.typ))) {
116
122
  continue;
117
123
  }
118
- const result = feature.run(replacement, options, parent[PARENT] ?? ast, context, FeatureWalkMode.Post);
124
+ const result = feature.run(replacement, options2,
125
+ // @ts-ignore
126
+ parent[PARENT] ?? ast, context, FeatureWalkMode.Post);
119
127
  if (result != null) {
120
128
  replacement = result;
121
129
  }
@@ -128,18 +136,20 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
128
136
  // @ts-ignore
129
137
  replaceNodeOrValue(parent[PARENT], parent, replacement);
130
138
  }
131
- if ("chi" in replacement) {
139
+ // @ts-ignore
140
+ if (replacement.chi != null) {
141
+ // @ts-ignore
132
142
  for (const node of replacement.chi) {
133
- // node[PARENT] = replacement;
143
+ node[PARENT] = replacement;
134
144
  parents.add(node);
135
145
  }
136
146
  }
137
147
  }
138
148
  if (postprocess) {
139
- for (const feature of options.features) {
149
+ for (const feature of options2.features) {
140
150
  if (feature.processMode & FeatureWalkMode.Post && "cleanup" in feature) {
141
151
  // @ts-ignore
142
- feature.cleanup(ast, options, context, FeatureWalkMode.Post);
152
+ feature.cleanup(ast, options2, context, FeatureWalkMode.Post);
143
153
  }
144
154
  }
145
155
  }
@@ -159,6 +169,7 @@ function transformAtRuleMediaPrelude(values) {
159
169
  values[values.indexOf(value)] = value.l;
160
170
  }
161
171
  else {
172
+ // @ts-ignore
162
173
  replaceNodeOrValue(parent, value, value.l);
163
174
  // @ts-ignore
164
175
  value = value.l;
@@ -215,9 +226,11 @@ function transformAtRuleMediaPrelude(values) {
215
226
  // @ts-expect-error
216
227
  const p = parents?.[parents?.indexOf?.(parent) + 1];
217
228
  if (p != null) {
229
+ // @ts-ignore
218
230
  replaceNodeOrValue(p, parent, replacement);
219
231
  }
220
232
  else {
233
+ // @ts-ignore
221
234
  values.splice(values.indexOf(parent), 1, replacement);
222
235
  }
223
236
  hasUpdates = true;
@@ -232,9 +245,9 @@ function transformAtRuleMediaPrelude(values) {
232
245
  * Minify at-rule media
233
246
  * - remove redundant tokens
234
247
  * - generate range queries
235
- * @param ast
236
248
  *
237
249
  * @private
250
+ * @param tokens
238
251
  */
239
252
  function minifyAtRuleMedia(tokens) {
240
253
  let hasUpdates = false;
@@ -331,8 +344,8 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
331
344
  continue;
332
345
  }
333
346
  while (previous?.typ === EnumToken.CommentNodeType) {
347
+ // @ts-ignore
334
348
  previous = ast.chi[--nodeIndex];
335
- continue;
336
349
  }
337
350
  node = ast.chi[i];
338
351
  if (node.typ === EnumToken.AtRuleNodeType && node.nam === "font-face") {
@@ -348,12 +361,13 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
348
361
  continue;
349
362
  }
350
363
  }
351
- else if (node.typ === EnumToken.KeyFramesRuleNodeType) {
352
- if (previous?.typ === EnumToken.KeyFramesRuleNodeType &&
364
+ else if (node.typ === EnumToken.KeyframesRuleNodeType) {
365
+ if (previous?.typ === EnumToken.KeyframesRuleNodeType &&
353
366
  node.sel === previous.sel) {
354
367
  // do not merge keyframes
355
368
  // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@keyframes#resolving_duplicates
356
369
  previous.chi.push(...node.chi);
370
+ // @ts-ignore
357
371
  ast.chi.splice(i, 1);
358
372
  previous = ast?.chi?.[nodeIndex] ?? null;
359
373
  i = nodeIndex;
@@ -426,6 +440,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
426
440
  else if (ast.typ === node.typ &&
427
441
  ast.nam === node.nam &&
428
442
  ast.val === node.val) {
443
+ // @ts-ignore
429
444
  replaceNodeOrValue(ast, node, node.chi);
430
445
  i--;
431
446
  continue;
@@ -519,7 +534,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
519
534
  ":is(" +
520
535
  node[OPTIMIZED].selector.reduce(reducer, []).join(",") +
521
536
  ")";
522
- const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
537
+ const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) =>
538
+ // @ts-ignore
539
+ (acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
523
540
  node.sel = sel1.length < sel2.length ? sel1 : sel2;
524
541
  node[TOKENS] = null;
525
542
  }
@@ -599,7 +616,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
599
616
  ":is(" +
600
617
  node[OPTIMIZED].selector.reduce(reducer, []).join(",") +
601
618
  ")";
602
- const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
619
+ const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) =>
620
+ // @ts-ignore
621
+ (acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
603
622
  node.sel = sel1.length < sel2.length ? sel1 : sel2;
604
623
  node[TOKENS] = null;
605
624
  }
@@ -610,11 +629,14 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
610
629
  curr.join(""), "");
611
630
  node[TOKENS] = null;
612
631
  }
632
+ // @ts-ignore
613
633
  }
614
634
  else if (node[OPTIMIZED]?.optimized.length > 0) {
635
+ // @ts-ignore
615
636
  const sel = node[OPTIMIZED].optimized.join("");
616
637
  if (sel.length < node.sel.length) {
617
638
  node.sel = sel;
639
+ // @ts-ignore
618
640
  node[RAW] = [node[OPTIMIZED].optimized.slice()];
619
641
  node[TOKENS] = null;
620
642
  }
@@ -637,11 +659,14 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
637
659
  }
638
660
  if (shouldMerge) {
639
661
  if (((node.typ === EnumToken.RuleNodeType ||
640
- node.typ === EnumToken.KeyFramesRuleNodeType) &&
662
+ node.typ === EnumToken.KeyframesRuleNodeType) &&
641
663
  node.sel === previous.sel) ||
664
+ // @ts-ignore
642
665
  (node.typ == EnumToken.AtRuleNodeType &&
643
666
  node.nam !== "font-face" &&
667
+ // @ts-ignore
644
668
  node.nam === previous.nam)) {
669
+ // @ts-ignore
645
670
  node.chi.unshift(...previous.chi);
646
671
  doMinify(node, options, recursive, errors, nestingContent, context);
647
672
  ast.chi.splice(nodeIndex, 1);
@@ -650,7 +675,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
650
675
  continue;
651
676
  }
652
677
  else if (node.typ == previous?.typ &&
653
- [EnumToken.KeyFramesRuleNodeType, EnumToken.RuleNodeType].includes(node.typ)) {
678
+ [EnumToken.KeyframesRuleNodeType, EnumToken.RuleNodeType].includes(node.typ)) {
654
679
  const intersect = diff(previous, node, options);
655
680
  if (intersect != null) {
656
681
  if (intersect.node1.chi.length == 0) {
@@ -803,7 +828,9 @@ function optimizeSelector(selector) {
803
828
  }
804
829
  break;
805
830
  }
806
- selector.forEach((selector) => selector.splice(0, optimized.length));
831
+ for (let i1 = 0; i1 < selector.length; i1++) {
832
+ selector[i1].splice(0, optimized.length);
833
+ }
807
834
  let reducible = optimized.length == 1;
808
835
  if (optimized[0] == "&") {
809
836
  if (optimized[1] == " ") {
@@ -1185,7 +1212,6 @@ function wrapNodes(previous, node, match, ast, reducer, i, nodeIndex) {
1185
1212
  * Diff nodes
1186
1213
  * @param n1
1187
1214
  * @param n2
1188
- * @param reducer
1189
1215
  * @param options
1190
1216
  *
1191
1217
  * @private
@@ -1304,17 +1330,36 @@ function diff(n1, n2, options = {}) {
1304
1330
  // @ts-ignore
1305
1331
  chi: intersect.reverse(),
1306
1332
  };
1333
+ let op = { level: 0, ...options };
1307
1334
  if (result == null ||
1308
1335
  [n1, n2].reduce((acc, curr) => {
1309
1336
  let css = options.cache.get(curr);
1310
1337
  if (css == null) {
1311
- css = doRender(curr, options).code;
1338
+ let level = 0;
1339
+ let parent = curr[PARENT];
1340
+ while (parent != null && parent.typ != EnumToken.StyleSheetNodeType) {
1341
+ level++;
1342
+ parent = parent[PARENT];
1343
+ }
1344
+ op.level = level;
1345
+ css = doRender(curr, op).code;
1312
1346
  options.cache.set(curr, css);
1313
1347
  }
1314
1348
  return curr.chi.length == 0 ? acc : acc + css.length;
1315
1349
  }, 0) <=
1316
1350
  [node1, node2, result].reduce((acc, curr) => {
1317
- const css = doRender(curr, options).code;
1351
+ let css = options.cache.get(curr);
1352
+ if (css != null) {
1353
+ return curr.chi.length == 0 ? acc : acc + css.length;
1354
+ }
1355
+ let level = 0;
1356
+ let parent = curr[PARENT];
1357
+ while (parent != null && parent.typ != EnumToken.StyleSheetNodeType) {
1358
+ level++;
1359
+ parent = parent[PARENT];
1360
+ }
1361
+ op.level = level;
1362
+ css = doRender(curr, op).code;
1318
1363
  return curr.chi.length == 0 ? acc : acc + css.length;
1319
1364
  }, 0)) {
1320
1365
  if (node1.chi.length != 0 && node2.chi.length != 0) {
@@ -0,0 +1,50 @@
1
+ import { TOKENS, ERRORS, STATE, LOC, PARENT } from '../syntax/constants.js';
2
+
3
+ /**
4
+ *
5
+ * @param node
6
+ * @param key
7
+ * @returns
8
+ */
9
+ function getNodeProperty(node, key) {
10
+ switch (key) {
11
+ case "parent":
12
+ return node[PARENT];
13
+ case "location":
14
+ return node[LOC];
15
+ case "state":
16
+ return node[STATE];
17
+ case "errors":
18
+ return node[ERRORS];
19
+ case "tokens":
20
+ return node[TOKENS];
21
+ }
22
+ return undefined;
23
+ }
24
+ /**
25
+ *
26
+ * @param node
27
+ * @param key
28
+ * @param value
29
+ */
30
+ function setNodeProperty(node, key, value) {
31
+ switch (key) {
32
+ case "parent":
33
+ node[PARENT] = value;
34
+ break;
35
+ case "location":
36
+ node[LOC] = value;
37
+ break;
38
+ case "state":
39
+ node[STATE] = value;
40
+ break;
41
+ case "errors":
42
+ node[ERRORS] = value;
43
+ break;
44
+ case "tokens":
45
+ node[TOKENS] = value;
46
+ break;
47
+ }
48
+ }
49
+
50
+ export { getNodeProperty, setNodeProperty };
@@ -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,11 +253,6 @@ 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 ||
@@ -1,5 +1,8 @@
1
1
  import { memoize } from '../parser/utils/cache.js';
2
2
 
3
+ /**
4
+ * match url
5
+ */
3
6
  const matchUrl = /^(https?:)?\/\//;
4
7
  /**
5
8
  * return the directory name of a path
@@ -11,6 +14,9 @@ function dirname(path) {
11
14
  if (path === "") {
12
15
  return "";
13
16
  }
17
+ if (path.startsWith("data:")) {
18
+ return path;
19
+ }
14
20
  let i = 0;
15
21
  let parts = [""];
16
22
  for (; i < path.length; i++) {
@@ -34,10 +40,7 @@ function splitPath(result) {
34
40
  if (result.length == 0) {
35
41
  return { parts: [], i: 0 };
36
42
  }
37
- // if (result === "/") {
38
- // return { parts: ["/"], i: 0 };
39
- // }
40
- const parts = [""];
43
+ const parts = result == "/" ? [] : [""];
41
44
  let i = 0;
42
45
  for (; i < result.length; i++) {
43
46
  const chr = result.charAt(i);
@@ -46,7 +49,7 @@ function splitPath(result) {
46
49
  }
47
50
  // else if (chr == "?" || chr == "#") {
48
51
  // break;
49
- // }
52
+ // }
50
53
  else {
51
54
  parts[parts.length - 1] += chr;
52
55
  }
@@ -64,6 +67,8 @@ function splitPath(result) {
64
67
  }
65
68
  /**
66
69
  * Nomalize path
70
+ * @param path
71
+ * @private
67
72
  */
68
73
  const normalize = memoize(function (path) {
69
74
  let parts = [];
@@ -92,14 +97,20 @@ const normalize = memoize(function (path) {
92
97
  while (++k < parts.length) {
93
98
  // if (parts[k] == ".") {
94
99
  // parts.splice(k--, 1);
95
- // } else
96
- if (parts[k] == "..") {
100
+ // } else
101
+ if (k > 0 && parts[k] == "..") {
97
102
  parts.splice(k - 1, 2);
98
103
  k -= 2;
99
104
  }
100
105
  }
101
106
  return (path.charAt(0) == "/" ? "/" : "") + parts.join("/");
102
107
  });
108
+ /**
109
+ * diff path
110
+ * @param path1
111
+ * @param path2
112
+ * @private
113
+ */
103
114
  const diff = memoize(function (path1, path2) {
104
115
  let { parts } = splitPath(path1);
105
116
  const { parts: dirs } = splitPath(path2);
@@ -122,39 +133,60 @@ const diff = memoize(function (path1, path2) {
122
133
  * @private
123
134
  */
124
135
  const resolve = memoize(function (url, currentDirectory, cwd) {
125
- cwd ??= "";
126
- currentDirectory ??= "";
127
136
  if (matchUrl.test(url)) {
128
137
  return {
129
138
  absolute: url,
130
139
  relative: url,
131
140
  };
132
141
  }
142
+ cwd ??= "";
143
+ currentDirectory ??= "";
133
144
  url = normalize(url);
145
+ if (cwd !== "") {
146
+ cwd = normalize(cwd);
147
+ }
134
148
  if (currentDirectory !== "") {
135
149
  currentDirectory = normalize(currentDirectory);
136
- if (url.startsWith(currentDirectory + "/")) {
137
- return {
138
- absolute: url,
139
- relative: url.slice(currentDirectory.length + 1),
140
- };
141
- }
142
- }
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
- }
153
150
  }
151
+ const dir = cwd || currentDirectory;
152
+ const absolute = dir == "" || url.startsWith("/") || url.match(/^[a-zA-Z]:/) ? resolvePath(url) : resolvePath(dir, url);
154
153
  return {
155
- absolute: url,
156
- relative: url === "" ? "" : diff(url, cwd || currentDirectory),
154
+ absolute,
155
+ relative: dir === "" ? absolute : diff(absolute, dir),
157
156
  };
158
157
  });
158
+ /**
159
+ *
160
+ * @param parts
161
+ * @returns
162
+ * @private
163
+ */
164
+ function resolvePath(...parts) {
165
+ const path = parts.filter(Boolean).join("/");
166
+ const isAbsolute = /^[\\/]/.test(path);
167
+ const segments = path.split(/[\\/]+/);
168
+ const resolved = [];
169
+ for (const segment of segments) {
170
+ if (!segment || segment === ".") {
171
+ continue;
172
+ }
173
+ if (segment === "..") {
174
+ if (resolved.length && resolved[resolved.length - 1] !== "..") {
175
+ resolved.pop();
176
+ }
177
+ else if (!isAbsolute) {
178
+ resolved.push("..");
179
+ }
180
+ }
181
+ else {
182
+ resolved.push(segment);
183
+ }
184
+ }
185
+ let result = resolved.join("/");
186
+ if (isAbsolute) {
187
+ result = "/" + result;
188
+ }
189
+ return result || (isAbsolute ? "/" : ".");
190
+ }
159
191
 
160
192
  export { diff, dirname, matchUrl, normalize, resolve };
@@ -286,12 +286,21 @@ class PropertyMap {
286
286
  return acc;
287
287
  }, []);
288
288
  let isImportant = false;
289
- const filtered = values.map(removeDefaults).filter((x) => x.val.filter((t) => {
290
- if (t.typ == EnumToken.ImportantTokenType) {
291
- isImportant = true;
289
+ let dec;
290
+ const filtered = [];
291
+ for (const declaration of values) {
292
+ dec = removeDefaults(declaration);
293
+ for (const t of dec.val) {
294
+ if (t.typ == EnumToken.ImportantTokenType) {
295
+ isImportant = true;
296
+ }
297
+ if (filtered.length == 0 &&
298
+ t.typ != EnumToken.WhitespaceTokenType &&
299
+ t.typ != EnumToken.ImportantTokenType) {
300
+ filtered.push(dec);
301
+ }
292
302
  }
293
- return ![EnumToken.WhitespaceTokenType, EnumToken.ImportantTokenType].includes(t.typ);
294
- }).length > 0);
303
+ }
295
304
  if (filtered.length == 0 && this.config.default.length > 0) {
296
305
  filtered.push({
297
306
  typ: EnumToken.DeclarationNodeType,
@@ -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,11 @@ 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
- const column = offset - this.lineStarts[line];
26
+ // if (offset < 0 || line < 0) {
27
+ // return [1, 1];
28
+ // }
30
29
  // [line, column]
31
- return [line + 1, column === 0 ? 1 : column];
30
+ return [line + 1, offset - this.lineStarts[line] + 1];
32
31
  }
33
32
  /**
34
33
  * search the greatest index of the value less than or equal to offset
@@ -66,13 +65,6 @@ class LineMap {
66
65
  addLineStart(lineStart) {
67
66
  this.lineStarts.push(lineStart);
68
67
  }
69
- /**
70
- * clone the linemap
71
- * @returns
72
- */
73
- clone() {
74
- return new LineMap(this.lineStarts.slice());
75
- }
76
68
  }
77
69
 
78
70
  export { LineMap };