@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.
- package/CHANGELOG.md +354 -4
- package/README.md +69 -1077
- package/dist/config.json.js +76 -3
- package/dist/index-umd-web.js +26838 -18743
- package/dist/index.cjs +26819 -18731
- package/dist/index.d.ts +2471 -1884
- package/dist/lib/ast/clone.js +46 -0
- package/dist/lib/ast/expand.js +90 -65
- package/dist/lib/ast/features/calc.js +86 -43
- package/dist/lib/ast/features/if.js +221 -0
- package/dist/lib/ast/features/index.js +1 -0
- package/dist/lib/ast/features/inlinecssvariables.js +33 -28
- package/dist/lib/ast/features/prefix.js +406 -44
- package/dist/lib/ast/features/shorthand.js +6 -9
- package/dist/lib/ast/features/transform.js +14 -15
- package/dist/lib/ast/find.js +175 -0
- package/dist/lib/ast/math/expression.js +145 -99
- package/dist/lib/ast/math/math.js +30 -41
- package/dist/lib/ast/minify.js +551 -232
- package/dist/lib/ast/transform/compute.js +82 -69
- package/dist/lib/ast/transform/matrix.js +12 -26
- package/dist/lib/ast/transform/minify.js +154 -150
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +3 -2
- package/dist/lib/ast/types.js +345 -30
- package/dist/lib/ast/walk.js +119 -64
- package/dist/lib/fs/resolve.js +94 -88
- package/dist/lib/parser/declaration/list.js +117 -26
- package/dist/lib/parser/declaration/map.js +141 -96
- package/dist/lib/parser/declaration/set.js +30 -25
- package/dist/lib/parser/parse.js +1810 -1739
- package/dist/lib/parser/tokenize.js +662 -403
- package/dist/lib/parser/utils/angle.js +29 -0
- package/dist/lib/parser/utils/at-rule-container.js +486 -0
- package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
- package/dist/lib/parser/utils/at-rule-generic.js +118 -0
- package/dist/lib/parser/utils/at-rule-import.js +393 -0
- package/dist/lib/parser/utils/at-rule-media.js +603 -0
- package/dist/lib/parser/utils/at-rule-support.js +431 -0
- package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
- package/dist/lib/parser/utils/at-rule.js +31 -0
- package/dist/lib/parser/utils/cache.js +19 -0
- package/dist/lib/parser/utils/config.js +1 -0
- package/dist/lib/parser/utils/declaration.js +672 -91
- package/dist/lib/parser/utils/hash.js +1 -1
- package/dist/lib/parser/utils/selector.js +518 -0
- package/dist/lib/parser/utils/text.js +17 -1
- package/dist/lib/parser/utils/token.js +104 -0
- package/dist/lib/parser/utils/type.js +29 -18
- package/dist/lib/renderer/render.js +870 -269
- package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
- package/dist/lib/syntax/color/a98rgb.js +8 -12
- package/dist/lib/syntax/color/alpha.js +64 -0
- package/dist/lib/syntax/color/cmyk.js +23 -21
- package/dist/lib/syntax/color/color-mix.js +311 -237
- package/dist/lib/syntax/color/color.js +98 -92
- package/dist/lib/syntax/color/hex.js +29 -33
- package/dist/lib/syntax/color/hsl.js +7 -14
- package/dist/lib/syntax/color/hwb.js +14 -27
- package/dist/lib/syntax/color/lab.js +23 -24
- package/dist/lib/syntax/color/lch.js +16 -18
- package/dist/lib/syntax/color/oklab.js +21 -40
- package/dist/lib/syntax/color/oklch.js +16 -18
- package/dist/lib/syntax/color/p3.js +0 -8
- package/dist/lib/syntax/color/rec2020.js +9 -13
- package/dist/lib/syntax/color/relativecolor.js +160 -48
- package/dist/lib/syntax/color/rgb.js +14 -18
- package/dist/lib/syntax/color/srgb.js +30 -29
- package/dist/lib/syntax/color/utils/color-type.js +18 -0
- package/dist/lib/syntax/color/utils/colorspace.js +46 -0
- package/dist/lib/syntax/color/utils/components.js +36 -16
- package/dist/lib/syntax/color/utils/distance.js +21 -11
- package/dist/lib/syntax/color/utils/make-color.js +56 -0
- package/dist/lib/syntax/color/xyz.js +10 -22
- package/dist/lib/syntax/color/xyzd50.js +6 -20
- package/dist/lib/syntax/constants.js +464 -0
- package/dist/lib/syntax/syntax.js +825 -592
- package/dist/lib/validation/config.js +72 -33
- package/dist/lib/validation/config.json.js +1185 -85
- package/dist/lib/validation/match.js +3120 -0
- package/dist/lib/validation/parser/parse.js +683 -935
- package/dist/lib/validation/parser/typedef.js +106 -0
- package/dist/lib/validation/utils/list.js +4 -11
- package/dist/lib/validation/utils/whitespace.js +2 -8
- package/dist/node.js +102 -57
- package/dist/web.js +100 -48
- package/package.json +31 -17
- package/dist/lib/syntax/color/utils/constants.js +0 -214
- package/dist/lib/syntax/utils.js +0 -70
- package/dist/lib/validation/at-rules/container.js +0 -342
- package/dist/lib/validation/at-rules/counter-style.js +0 -90
- package/dist/lib/validation/at-rules/custom-media.js +0 -50
- package/dist/lib/validation/at-rules/document.js +0 -89
- package/dist/lib/validation/at-rules/else.js +0 -5
- package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
- package/dist/lib/validation/at-rules/import.js +0 -150
- package/dist/lib/validation/at-rules/keyframes.js +0 -67
- package/dist/lib/validation/at-rules/layer.js +0 -41
- package/dist/lib/validation/at-rules/media.js +0 -255
- package/dist/lib/validation/at-rules/namespace.js +0 -81
- package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
- package/dist/lib/validation/at-rules/page.js +0 -100
- package/dist/lib/validation/at-rules/supports.js +0 -295
- package/dist/lib/validation/at-rules/when.js +0 -185
- package/dist/lib/validation/atrule.js +0 -184
- package/dist/lib/validation/selector.js +0 -36
- package/dist/lib/validation/syntax.js +0 -1073
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
- package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
- package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
- package/dist/lib/validation/syntaxes/family-name.js +0 -57
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
- package/dist/lib/validation/syntaxes/layer-name.js +0 -57
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
- package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
- package/dist/lib/validation/syntaxes/selector-list.js +0 -5
- package/dist/lib/validation/syntaxes/selector.js +0 -5
- package/dist/lib/validation/syntaxes/url.js +0 -40
package/dist/lib/ast/walk.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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 ==
|
|
120
|
+
isNumeric = typeof option == "number";
|
|
121
121
|
if (isNumeric) {
|
|
122
|
-
if (
|
|
122
|
+
if (option & WalkerOptionEnum.Ignore) {
|
|
123
123
|
continue;
|
|
124
124
|
}
|
|
125
|
-
if (
|
|
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 {
|
|
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 (
|
|
135
|
-
parents.splice(i, 0, ...node.chi[reverse ?
|
|
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
|
-
*
|
|
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 ==
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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 ==
|
|
240
|
+
(typeof filter.type == "function" && filter.type(value));
|
|
213
241
|
if (isValid) {
|
|
214
|
-
option = filter.fn(value, map.get(value) ?? root, WalkerEvent.Enter
|
|
215
|
-
|
|
216
|
-
|
|
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 &&
|
|
256
|
+
if (isNumeric && option & WalkerOptionEnum.Ignore) {
|
|
220
257
|
continue;
|
|
221
258
|
}
|
|
222
259
|
// @ts-ignore
|
|
223
|
-
if (option != null && typeof 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 ?
|
|
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 (
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
-
|
|
270
|
-
|
|
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 (
|
|
281
|
-
const isValid = filter.type == null ||
|
|
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 ==
|
|
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 && (
|
|
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 ?
|
|
347
|
+
stack[reverse ? "push" : "unshift"](...op);
|
|
293
348
|
}
|
|
294
349
|
}
|
|
295
350
|
}
|
package/dist/lib/fs/resolve.js
CHANGED
|
@@ -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
|
-
|
|
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 ?
|
|
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: [
|
|
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 ==
|
|
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
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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 ===
|
|
141
|
+
if (currentDirectory === "" && cwd !== "") {
|
|
96
142
|
cwd = normalize(cwd);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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) +
|
|
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 ===
|
|
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 };
|