@tbela99/css-parser 1.4.2 → 1.4.4

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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -1,5 +1,5 @@
1
1
  /**
2
- * options for the walk function
2
+ * Options for the walk function
3
3
  */
4
4
  var WalkerOptionEnum;
5
5
  (function (WalkerOptionEnum) {
@@ -21,7 +21,7 @@ var WalkerOptionEnum;
21
21
  WalkerOptionEnum[WalkerOptionEnum["IgnoreChildren"] = 8] = "IgnoreChildren";
22
22
  })(WalkerOptionEnum || (WalkerOptionEnum = {}));
23
23
  /**
24
- * event types for the walkValues function
24
+ * Event types for the walkValues function
25
25
  */
26
26
  var WalkerEvent;
27
27
  (function (WalkerEvent) {
@@ -35,7 +35,7 @@ var WalkerEvent;
35
35
  WalkerEvent[WalkerEvent["Leave"] = 2] = "Leave";
36
36
  })(WalkerEvent || (WalkerEvent = {}));
37
37
  /**
38
- * walk ast nodes
38
+ * Walk ast nodes
39
39
  * @param node initial node
40
40
  * @param filter control the walk process
41
41
  * @param reverse walk in reverse order
@@ -110,29 +110,41 @@ var WalkerEvent;
110
110
  function* walk(node, filter, reverse) {
111
111
  const parents = [node];
112
112
  const root = node;
113
- const map = new Map;
113
+ const map = new Map();
114
114
  let isNumeric = false;
115
115
  let i = 0;
116
116
  while ((node = parents[i++])) {
117
117
  let option = null;
118
118
  if (filter != null) {
119
119
  option = filter(node);
120
- isNumeric = typeof option == 'number';
120
+ isNumeric = typeof option == "number";
121
121
  if (isNumeric) {
122
- if ((option & WalkerOptionEnum.Ignore)) {
122
+ if (option & WalkerOptionEnum.Ignore) {
123
123
  continue;
124
124
  }
125
- if ((option & WalkerOptionEnum.Stop)) {
125
+ if (option & WalkerOptionEnum.Stop) {
126
126
  break;
127
127
  }
128
128
  }
129
129
  }
130
130
  if (!isNumeric || (option & WalkerOptionEnum.Children) === 0) {
131
131
  // @ts-ignore
132
- yield { node, parent: map.get(node), root };
132
+ yield {
133
+ node,
134
+ parent: map.get(node),
135
+ root,
136
+ // @ts-expect-error
137
+ parents: function* () {
138
+ let parent = map.get(node);
139
+ while (parent != null) {
140
+ yield parent;
141
+ parent = map.get(parent);
142
+ }
143
+ },
144
+ };
133
145
  }
134
- if ('chi' in node && (!isNumeric || ((option & WalkerOptionEnum.IgnoreChildren) === 0))) {
135
- parents.splice(i, 0, ...node.chi[reverse ? 'reverse' : 'slice']());
146
+ if ("chi" in node && (!isNumeric || (option & WalkerOptionEnum.IgnoreChildren) === 0)) {
147
+ parents.splice(i, 0, ...node.chi[reverse ? "reverse" : "slice"]());
136
148
  for (const child of node.chi) {
137
149
  map.set(child, node);
138
150
  }
@@ -140,7 +152,7 @@ function* walk(node, filter, reverse) {
140
152
  }
141
153
  }
142
154
  /**
143
- * walk ast node value tokens
155
+ * Walk ast node value tokens
144
156
  * @param values
145
157
  * @param root
146
158
  * @param filter
@@ -188,44 +200,69 @@ function* walk(node, filter, reverse) {
188
200
  */
189
201
  function* walkValues(values, root = null, filter, reverse) {
190
202
  const stack = values.slice();
191
- const map = new Map;
203
+ const map = new Map();
204
+ const used = new Set();
192
205
  let previous = null;
193
- if (filter != null && typeof filter == 'function') {
206
+ if (filter != null && typeof filter == "function") {
194
207
  filter = {
195
208
  event: WalkerEvent.Enter,
196
- fn: filter
209
+ fn: filter,
197
210
  };
198
211
  }
199
212
  else if (filter == null) {
200
213
  filter = {
201
- event: WalkerEvent.Enter
214
+ event: WalkerEvent.Enter,
202
215
  };
203
216
  }
204
217
  let isNumeric = false;
218
+ let value;
219
+ let option;
220
+ let node;
221
+ // const parents: Token[] = [];
205
222
  const eventType = filter.event ?? WalkerEvent.Enter;
206
223
  while (stack.length > 0) {
207
- let value = reverse ? stack.pop() : stack.shift();
208
- let option = null;
209
- if (filter.fn != null && (eventType & WalkerEvent.Enter)) {
210
- const isValid = filter.type == null || value.typ == filter.type ||
224
+ value = reverse ? stack.pop() : stack.shift();
225
+ option = null;
226
+ node = map.get(value) ?? null;
227
+ if (used.has(value)) {
228
+ continue;
229
+ }
230
+ used.add(value);
231
+ // parents.length = 0;
232
+ // while (node != null) {
233
+ // parents.push(node);
234
+ // node = map.get(node) ?? null;
235
+ // }
236
+ if (filter.fn != null && eventType & WalkerEvent.Enter) {
237
+ const isValid = filter.type == null ||
238
+ value.typ == filter.type ||
211
239
  (Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
212
- (typeof filter.type == 'function' && filter.type(value));
240
+ (typeof filter.type == "function" && filter.type(value));
213
241
  if (isValid) {
214
- option = filter.fn(value, map.get(value) ?? root, WalkerEvent.Enter);
215
- isNumeric = typeof option == 'number';
216
- if (isNumeric && (option & WalkerOptionEnum.Stop)) {
242
+ option = filter.fn(value, map.get(value) ?? root, WalkerEvent.Enter,
243
+ // @ts-expect-error
244
+ function* () {
245
+ // @ts-expect-error
246
+ let parent = map.get(node);
247
+ while (parent != null) {
248
+ yield parent;
249
+ parent = map.get(parent);
250
+ }
251
+ });
252
+ isNumeric = typeof option == "number";
253
+ if (isNumeric && option & WalkerOptionEnum.Stop) {
217
254
  return;
218
255
  }
219
- if (isNumeric && (option & WalkerOptionEnum.Ignore)) {
256
+ if (isNumeric && option & WalkerOptionEnum.Ignore) {
220
257
  continue;
221
258
  }
222
259
  // @ts-ignore
223
- if (option != null && typeof option == 'object' && ('typ' in option || Array.isArray(option))) {
260
+ if (option != null && typeof option == "object" && ("typ" in option || Array.isArray(option))) {
224
261
  const op = Array.isArray(option) ? option : [option];
225
262
  for (const o of op) {
226
263
  map.set(o, map.get(value) ?? root);
227
264
  }
228
- stack[reverse ? 'push' : 'unshift'](...op);
265
+ stack[reverse ? "push" : "unshift"](...op);
229
266
  }
230
267
  }
231
268
  }
@@ -235,61 +272,79 @@ function* walkValues(values, root = null, filter, reverse) {
235
272
  previousValue: previous,
236
273
  nextValue: stack[0] ?? null,
237
274
  // @ts-ignore
238
- root: root ?? null
275
+ root: root ?? null,
276
+ // @ts-expect-error
277
+ parents: function* () {
278
+ // @ts-expect-error
279
+ let result = map.get(node) ?? root;
280
+ let next;
281
+ do {
282
+ yield result;
283
+ next = map.get(result) ?? root;
284
+ if (next == result) {
285
+ break;
286
+ }
287
+ result = next;
288
+ } while (result != null);
289
+ },
239
290
  };
240
- if ('chi' in value && (!isNumeric || (option & WalkerOptionEnum.IgnoreChildren) === 0)) {
241
- const sliced = value.chi.slice();
242
- for (const child of sliced) {
243
- map.set(child, value);
244
- }
245
- stack[reverse ? 'push' : 'unshift'](...sliced);
246
- }
247
- else {
248
- const values = [];
249
- if ('l' in value && value.l != null) {
250
- // @ts-ignore
251
- values.push(value.l);
252
- // @ts-ignore
253
- map.set(value.l, value);
254
- }
255
- if ('op' in value && typeof value.op == 'object') {
256
- values.push(value.op);
257
- // @ts-ignore
258
- map.set(value.op, value);
291
+ if (!isNumeric || (option & WalkerOptionEnum.IgnoreChildren) === 0) {
292
+ if ("chi" in value) {
293
+ const sliced = value.chi.slice();
294
+ for (const child of sliced) {
295
+ map.set(child, value);
296
+ }
297
+ stack[reverse ? "push" : "unshift"](...sliced);
259
298
  }
260
- if ('r' in value && value.r != null) {
261
- if (Array.isArray(value.r)) {
262
- for (const r of value.r) {
299
+ else {
300
+ const values = [];
301
+ if ("l" in value && value.l != null) {
302
+ // @ts-ignore
303
+ values.push(value.l);
304
+ // @ts-ignore
305
+ map.set(value.l, value);
306
+ }
307
+ if ("op" in value && typeof value.op == "object") {
308
+ // @ts-ignore
309
+ values.push(value.op);
310
+ // @ts-ignore
311
+ map.set(value.op, value);
312
+ }
313
+ if ("r" in value && value.r != null) {
314
+ if (Array.isArray(value.r)) {
315
+ for (const r of value.r) {
316
+ // @ts-ignore
317
+ values.push(r);
318
+ // @ts-ignore
319
+ map.set(r, value);
320
+ }
321
+ }
322
+ else {
263
323
  // @ts-ignore
264
- values.push(r);
324
+ values.push(value.r);
265
325
  // @ts-ignore
266
- map.set(r, value);
326
+ map.set(value.r, value);
267
327
  }
268
328
  }
269
- else {
270
- // @ts-ignore
271
- values.push(value.r);
272
- // @ts-ignore
273
- map.set(value.r, value);
329
+ if (values.length > 0) {
330
+ stack[reverse ? "push" : "unshift"](...values);
274
331
  }
275
332
  }
276
- if (values.length > 0) {
277
- stack[reverse ? 'push' : 'unshift'](...values);
278
- }
279
333
  }
280
- if ((eventType & WalkerEvent.Leave) && filter.fn != null) {
281
- const isValid = filter.type == null || value.typ == filter.type ||
334
+ if (eventType & WalkerEvent.Leave && filter.fn != null) {
335
+ const isValid = filter.type == null ||
336
+ value.typ == filter.type ||
282
337
  (Array.isArray(filter.type) && filter.type.includes(value.typ)) ||
283
- (typeof filter.type == 'function' && filter.type(value));
338
+ (typeof filter.type == "function" && filter.type(value));
284
339
  if (isValid) {
285
340
  option = filter.fn(value, map.get(value), WalkerEvent.Leave);
286
341
  // @ts-ignore
287
- if (option != null && ('typ' in option || Array.isArray(option))) {
342
+ if (option != null && ("typ" in option || Array.isArray(option))) {
288
343
  const op = Array.isArray(option) ? option : [option];
289
344
  for (const o of op) {
290
345
  map.set(o, map.get(value) ?? root);
291
346
  }
292
- stack[reverse ? 'push' : 'unshift'](...op);
347
+ stack[reverse ? "push" : "unshift"](...op);
293
348
  }
294
349
  }
295
350
  }
@@ -1,3 +1,5 @@
1
+ import { memoize } from '../parser/utils/cache.js';
2
+
1
3
  const matchUrl = /^(https?:)?\/\//;
2
4
  /**
3
5
  * return the directory name of a path
@@ -6,30 +8,22 @@ const matchUrl = /^(https?:)?\/\//;
6
8
  * @private
7
9
  */
8
10
  function dirname(path) {
9
- // if (path == '/' || path === '') {
10
- //
11
- // return path;
12
- // }
13
- if (path === '') {
14
- return '';
11
+ if (path === "") {
12
+ return "";
15
13
  }
16
14
  let i = 0;
17
- let parts = [''];
15
+ let parts = [""];
18
16
  for (; i < path.length; i++) {
19
17
  const chr = path.charAt(i);
20
- if (chr == '/') {
21
- parts.push('');
18
+ if (chr == "/") {
19
+ parts.push("");
22
20
  }
23
- // else if (chr == '?' || chr == '#') {
24
- //
25
- // break;
26
- // }
27
21
  else {
28
22
  parts[parts.length - 1] += chr;
29
23
  }
30
24
  }
31
25
  parts.pop();
32
- return parts.length == 0 ? '/' : parts.join('/');
26
+ return parts.length == 0 ? "/" : parts.join("/");
33
27
  }
34
28
  /**
35
29
  * split path
@@ -40,17 +34,17 @@ function splitPath(result) {
40
34
  if (result.length == 0) {
41
35
  return { parts: [], i: 0 };
42
36
  }
43
- if (result === '/') {
44
- return { parts: ['/'], i: 0 };
37
+ if (result === "/") {
38
+ return { parts: ["/"], i: 0 };
45
39
  }
46
- const parts = [''];
40
+ const parts = [""];
47
41
  let i = 0;
48
42
  for (; i < result.length; i++) {
49
43
  const chr = result.charAt(i);
50
- if (chr == '/') {
51
- parts.push('');
44
+ if (chr == "/") {
45
+ parts.push("");
52
46
  }
53
- else if (chr == '?' || chr == '#') {
47
+ else if (chr == "?" || chr == "#") {
54
48
  break;
55
49
  }
56
50
  else {
@@ -59,16 +53,64 @@ function splitPath(result) {
59
53
  }
60
54
  let k = -1;
61
55
  while (++k < parts.length) {
62
- if (parts[k] == '.') {
56
+ if (parts[k] == ".") {
63
57
  parts.splice(k--, 1);
64
58
  }
65
- else if (parts[k] == '..') {
59
+ else if (parts[k] == "..") {
66
60
  parts.splice(k - 1, 2);
67
61
  k -= 2;
68
62
  }
69
63
  }
70
64
  return { parts, i };
71
65
  }
66
+ const normalize = memoize(function (path) {
67
+ let parts = [];
68
+ let i = 0;
69
+ if (path.includes("\\")) {
70
+ path = path.replace(/(\\)/g, "/");
71
+ }
72
+ for (; i < path.length; i++) {
73
+ const chr = path.charAt(i);
74
+ if (chr == "/") {
75
+ if (parts.length == 0 || parts[parts.length - 1] !== "") {
76
+ parts.push("");
77
+ }
78
+ }
79
+ else if (chr == "?" || chr == "#") {
80
+ break;
81
+ }
82
+ else {
83
+ if (parts.length == 0) {
84
+ parts.push("");
85
+ }
86
+ parts[parts.length - 1] += chr;
87
+ }
88
+ }
89
+ let k = -1;
90
+ while (++k < parts.length) {
91
+ if (parts[k] == ".") {
92
+ parts.splice(k--, 1);
93
+ }
94
+ else if (parts[k] == "..") {
95
+ parts.splice(k - 1, 2);
96
+ k -= 2;
97
+ }
98
+ }
99
+ return (path.charAt(0) == "/" ? "/" : "") + parts.join("/");
100
+ });
101
+ const diff = memoize(function (path1, path2) {
102
+ let { parts } = splitPath(path1);
103
+ const { parts: dirs } = splitPath(path2);
104
+ for (const p of dirs) {
105
+ if (parts[0] == p) {
106
+ parts.shift();
107
+ }
108
+ else {
109
+ parts.unshift("..");
110
+ }
111
+ }
112
+ return parts.join("/");
113
+ });
72
114
  /**
73
115
  * resolve path
74
116
  * @param url url or path to resolve
@@ -77,91 +119,55 @@ function splitPath(result) {
77
119
  *
78
120
  * @private
79
121
  */
80
- function resolve(url, currentDirectory, cwd) {
122
+ const resolve = memoize(function (url, currentDirectory, cwd) {
81
123
  if (matchUrl.test(url)) {
82
124
  return {
83
125
  absolute: url,
84
- relative: url
126
+ relative: url,
85
127
  };
86
128
  }
87
- cwd ??= '';
88
- currentDirectory ??= '';
89
- if (currentDirectory !== '' && url.startsWith(currentDirectory + '/')) {
90
- return {
91
- absolute: url,
92
- relative: url.slice(currentDirectory.length + 1)
93
- };
129
+ cwd ??= "";
130
+ currentDirectory ??= "";
131
+ url = normalize(url);
132
+ if (currentDirectory !== "") {
133
+ currentDirectory = normalize(currentDirectory);
134
+ if (url.startsWith(currentDirectory + "/")) {
135
+ return {
136
+ absolute: url,
137
+ relative: url.slice(currentDirectory.length + 1),
138
+ };
139
+ }
94
140
  }
95
- if (currentDirectory === '' && cwd !== '' && url.startsWith(cwd == '/' ? cwd : cwd + '/')) {
141
+ if (currentDirectory === "" && cwd !== "") {
96
142
  cwd = normalize(cwd);
97
- const absolute = normalize(url);
98
- const prefix = cwd == '/' ? cwd : cwd + '/';
99
- return {
100
- absolute,
101
- relative: absolute.startsWith(prefix) ? absolute.slice(prefix.length) : diff(absolute, cwd)
102
- };
143
+ if (url.startsWith(cwd == "/" ? cwd : cwd + "/")) {
144
+ const absolute = url;
145
+ const prefix = cwd == "/" ? cwd : cwd + "/";
146
+ return {
147
+ absolute,
148
+ relative: absolute.startsWith(prefix) ? absolute.slice(prefix.length) : diff(absolute, cwd),
149
+ };
150
+ }
103
151
  }
104
152
  if (matchUrl.test(currentDirectory)) {
105
153
  const path = new URL(url, currentDirectory).href;
106
154
  return {
107
155
  absolute: path,
108
- relative: path
156
+ relative: path,
109
157
  };
110
158
  }
111
- let result = '';
112
- if (url.charAt(0) == '/') {
159
+ let result = "";
160
+ if (url.charAt(0) == "/") {
113
161
  result = url;
114
162
  }
115
- else if (currentDirectory.charAt(0) == '/') {
116
- result = dirname(currentDirectory) + '/' + url;
163
+ else if (currentDirectory.charAt(0) == "/") {
164
+ result = dirname(currentDirectory) + "/" + url;
117
165
  }
118
166
  const absolute = normalize(result);
119
167
  return {
120
168
  absolute,
121
- relative: absolute === '' ? '' : diff(absolute, cwd ?? currentDirectory),
169
+ relative: absolute === "" ? "" : diff(absolute, cwd ?? currentDirectory),
122
170
  };
123
- }
124
- function diff(path1, path2) {
125
- let { parts } = splitPath(path1);
126
- const { parts: dirs } = splitPath(path2);
127
- for (const p of dirs) {
128
- if (parts[0] == p) {
129
- parts.shift();
130
- }
131
- else {
132
- parts.unshift('..');
133
- }
134
- }
135
- return parts.join('/');
136
- }
137
- function normalize(path) {
138
- let parts = [];
139
- let i = 0;
140
- for (; i < path.length; i++) {
141
- const chr = path.charAt(i);
142
- if (chr == '/') {
143
- if (parts.length == 0 || parts[parts.length - 1] !== '') {
144
- parts.push('');
145
- }
146
- }
147
- else if (chr == '?' || chr == '#') {
148
- break;
149
- }
150
- else {
151
- parts[parts.length - 1] += chr;
152
- }
153
- }
154
- let k = -1;
155
- while (++k < parts.length) {
156
- if (parts[k] == '.') {
157
- parts.splice(k--, 1);
158
- }
159
- else if (parts[k] == '..') {
160
- parts.splice(k - 1, 2);
161
- k -= 2;
162
- }
163
- }
164
- return (path.charAt(0) == '/' ? '/' : '') + parts.join('/');
165
- }
171
+ });
166
172
 
167
- export { dirname, matchUrl, resolve };
173
+ export { diff, dirname, matchUrl, normalize, resolve };