-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathutils.js
91 lines (83 loc) · 2.46 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const path = require('path');
const fs = require('fs');
const url = require('url');
const mkdirp = require('mkdirp');
const parseIntStrict = require('parse-int');
const symbols = require('./symbols');
function _getBaseUrl(u, currentBaseUrl = '') {
let result;
if (/^file:\/\//.test(u)) {
result = u;
} else if (/^(?:data|blob):/.test(u)) {
result = currentBaseUrl;
} else {
const parsedUrl = url.parse(u);
result = url.format({
protocol: parsedUrl.protocol || 'http:',
host: parsedUrl.host || '127.0.0.1',
pathname: parsedUrl.pathname.replace(/\/[^\/]*\.[^\/]*$/, '') || '/',
});
}
if (!/\/$/.test(result) && !/\/[^\/]*?\.[^\/]*?$/.test(result)) {
result += '/';
}
return result;
}
module.exports._getBaseUrl = _getBaseUrl;
function _normalizeUrl(src, baseUrl) {
if (/^\/\//.test(src)) {
src = new URL(baseUrl).protocol + src;
}
/* if (!/^(?:\/|[a-z]+:)/.test(src)) {
src = baseUrl + (!/\/$/.test(baseUrl) ? '/' : '') + src;
} */
if (!/^(?:https?|data|blob):/.test(src)) {
return new URL(src, baseUrl).href
.replace(/^(file:\/\/)\/([a-z]:.*)$/i, '$1$2');
} else {
return src;
}
}
module.exports._normalizeUrl = _normalizeUrl;
const _makeHtmlCollectionProxy = (el, query) => new Proxy(el, {
get(target, prop) {
const propN = parseIntStrict(prop);
if (propN !== undefined) {
return el.querySelectorAll(query)[propN];
} else if (prop === 'length') {
return el.querySelectorAll(query).length;
} else {
return undefined;
}
},
set(target, prop, value) {
return true;
},
deleteProperty(target, prop) {
return true;
},
has(target, prop) {
if (typeof prop === 'number') {
return el.querySelectorAll(query)[prop] !== undefined;
} else if (prop === 'length') {
return true;
} else {
return false;
}
},
});
module.exports._makeHtmlCollectionProxy = _makeHtmlCollectionProxy;
const _elementGetter = (self, attribute) => self.listeners(attribute).filter(l => l[symbols.listenerSymbol])[0];
module.exports._elementGetter = _elementGetter;
const _elementSetter = (self, attribute, cb) => {
const listener = _elementGetter(self, attribute);
if (listener) {
self.removeEventListener(attribute, listener);
listener[symbols.listenerSymbol] = false;
}
if (typeof cb === 'function') {
self.addEventListener(attribute, cb);
cb[symbols.listenerSymbol] = true;
}
};
module.exports._elementSetter = _elementSetter;