@wordpress/interactivity-router 2.25.0 → 2.26.1-next.719a03cbe.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.
- package/CHANGELOG.md +17 -0
- package/README.md +22 -0
- package/build/assets/dynamic-importmap/fetch.js +60 -0
- package/build/assets/dynamic-importmap/fetch.js.map +1 -0
- package/build/assets/dynamic-importmap/index.js +91 -0
- package/build/assets/dynamic-importmap/index.js.map +1 -0
- package/build/assets/dynamic-importmap/loader.js +286 -0
- package/build/assets/dynamic-importmap/loader.js.map +1 -0
- package/build/assets/dynamic-importmap/resolver.js +220 -0
- package/build/assets/dynamic-importmap/resolver.js.map +1 -0
- package/build/assets/script-modules.js +64 -0
- package/build/assets/script-modules.js.map +1 -0
- package/build/assets/scs.js +62 -0
- package/build/assets/scs.js.map +1 -0
- package/build/assets/styles.js +246 -0
- package/build/assets/styles.js.map +1 -0
- package/build/full-page.js +43 -0
- package/build/full-page.js.map +1 -0
- package/build/index.js +140 -38
- package/build/index.js.map +1 -1
- package/build-module/assets/dynamic-importmap/fetch.js +54 -0
- package/build-module/assets/dynamic-importmap/fetch.js.map +1 -0
- package/build-module/assets/dynamic-importmap/index.js +72 -0
- package/build-module/assets/dynamic-importmap/index.js.map +1 -0
- package/build-module/assets/dynamic-importmap/loader.js +279 -0
- package/build-module/assets/dynamic-importmap/loader.js.map +1 -0
- package/build-module/assets/dynamic-importmap/resolver.js +213 -0
- package/build-module/assets/dynamic-importmap/resolver.js.map +1 -0
- package/build-module/assets/script-modules.js +55 -0
- package/build-module/assets/script-modules.js.map +1 -0
- package/build-module/assets/scs.js +56 -0
- package/build-module/assets/scs.js.map +1 -0
- package/build-module/assets/styles.js +235 -0
- package/build-module/assets/styles.js.map +1 -0
- package/build-module/full-page.js +39 -0
- package/build-module/full-page.js.map +1 -0
- package/build-module/index.js +142 -38
- package/build-module/index.js.map +1 -1
- package/build-types/assets/dynamic-importmap/fetch.d.ts +30 -0
- package/build-types/assets/dynamic-importmap/fetch.d.ts.map +1 -0
- package/build-types/assets/dynamic-importmap/index.d.ts +42 -0
- package/build-types/assets/dynamic-importmap/index.d.ts.map +1 -0
- package/build-types/assets/dynamic-importmap/loader.d.ts +68 -0
- package/build-types/assets/dynamic-importmap/loader.d.ts.map +1 -0
- package/build-types/assets/dynamic-importmap/resolver.d.ts +35 -0
- package/build-types/assets/dynamic-importmap/resolver.d.ts.map +1 -0
- package/build-types/assets/script-modules.d.ts +26 -0
- package/build-types/assets/script-modules.d.ts.map +1 -0
- package/build-types/assets/scs.d.ts +24 -0
- package/build-types/assets/scs.d.ts.map +1 -0
- package/build-types/assets/styles.d.ts +69 -0
- package/build-types/assets/styles.d.ts.map +1 -0
- package/build-types/full-page.d.ts +3 -0
- package/build-types/full-page.d.ts.map +1 -0
- package/build-types/index.d.ts +4 -5
- package/build-types/index.d.ts.map +1 -1
- package/package.json +9 -5
- package/src/assets/dynamic-importmap/fetch.ts +58 -0
- package/src/assets/dynamic-importmap/index.ts +87 -0
- package/src/assets/dynamic-importmap/loader.ts +366 -0
- package/src/assets/dynamic-importmap/resolver.ts +292 -0
- package/src/assets/script-modules.ts +69 -0
- package/src/assets/scs.ts +61 -0
- package/src/assets/styles.ts +272 -0
- package/src/assets/test/scs.test.ts +367 -0
- package/src/assets/test/styles.test.ts +758 -0
- package/src/full-page.ts +47 -0
- package/src/index.ts +165 -45
- package/tsconfig.full-page.json +12 -0
- package/tsconfig.full-page.tsbuildinfo +1 -0
- package/tsconfig.json +9 -4
- package/tsconfig.main.json +21 -0
- package/tsconfig.main.tsbuildinfo +1 -0
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/* wp:polyfill */
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.addImportMap = addImportMap;
|
|
8
|
+
exports.resolve = resolve;
|
|
9
|
+
/**
|
|
10
|
+
* This code is derived from the following projects:
|
|
11
|
+
*
|
|
12
|
+
* 1. dynamic-importmap (https://github.com/keller-mark/dynamic-importmap)
|
|
13
|
+
* 2. es-module-shims (https://github.com/guybedford/es-module-shims)
|
|
14
|
+
*
|
|
15
|
+
* The original implementation was created by Guy Bedford in es-module-shims,
|
|
16
|
+
* then adapted by Mark Keller in dynamic-importmap, and further modified
|
|
17
|
+
* for use in this project.
|
|
18
|
+
*
|
|
19
|
+
* Both projects are licensed under the MIT license.
|
|
20
|
+
*
|
|
21
|
+
* MIT License: https://opensource.org/licenses/MIT
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const backslashRegEx = /\\/g;
|
|
25
|
+
function isURL(url) {
|
|
26
|
+
if (url.indexOf(':') === -1) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
new URL(url);
|
|
31
|
+
return true;
|
|
32
|
+
} catch (_) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
|
|
37
|
+
const hIdx = parentUrl.indexOf('#'),
|
|
38
|
+
qIdx = parentUrl.indexOf('?');
|
|
39
|
+
if (hIdx + qIdx > -2) {
|
|
40
|
+
parentUrl = parentUrl.slice(0,
|
|
41
|
+
// eslint-disable-next-line no-nested-ternary
|
|
42
|
+
hIdx === -1 ? qIdx : qIdx === -1 || qIdx > hIdx ? hIdx : qIdx);
|
|
43
|
+
}
|
|
44
|
+
if (relUrl.indexOf('\\') !== -1) {
|
|
45
|
+
relUrl = relUrl.replace(backslashRegEx, '/');
|
|
46
|
+
}
|
|
47
|
+
// protocol-relative
|
|
48
|
+
if (relUrl[0] === '/' && relUrl[1] === '/') {
|
|
49
|
+
return parentUrl.slice(0, parentUrl.indexOf(':') + 1) + relUrl;
|
|
50
|
+
}
|
|
51
|
+
// relative-url
|
|
52
|
+
else if (relUrl[0] === '.' && (relUrl[1] === '/' || relUrl[1] === '.' && (relUrl[2] === '/' || relUrl.length === 2 && (relUrl += '/')) || relUrl.length === 1 && (relUrl += '/')) || relUrl[0] === '/') {
|
|
53
|
+
const parentProtocol = parentUrl.slice(0, parentUrl.indexOf(':') + 1);
|
|
54
|
+
// Disabled, but these cases will give inconsistent results for deep backtracking
|
|
55
|
+
//if (parentUrl[parentProtocol.length] !== '/')
|
|
56
|
+
// throw new Error('Cannot resolve');
|
|
57
|
+
// read pathname from parent URL
|
|
58
|
+
// pathname taken to be part after leading "/"
|
|
59
|
+
let pathname;
|
|
60
|
+
if (parentUrl[parentProtocol.length + 1] === '/') {
|
|
61
|
+
// resolving to a :// so we need to read out the auth and host
|
|
62
|
+
if (parentProtocol !== 'file:') {
|
|
63
|
+
pathname = parentUrl.slice(parentProtocol.length + 2);
|
|
64
|
+
pathname = pathname.slice(pathname.indexOf('/') + 1);
|
|
65
|
+
} else {
|
|
66
|
+
pathname = parentUrl.slice(8);
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
// resolving to :/ so pathname is the /... part
|
|
70
|
+
pathname = parentUrl.slice(parentProtocol.length + (parentUrl[parentProtocol.length] === '/'));
|
|
71
|
+
}
|
|
72
|
+
if (relUrl[0] === '/') {
|
|
73
|
+
return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// join together and split for removal of .. and . segments
|
|
77
|
+
// looping the string instead of anything fancy for perf reasons
|
|
78
|
+
// '../../../../../z' resolved to 'x/y' is just 'z'
|
|
79
|
+
const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
|
|
80
|
+
const output = [];
|
|
81
|
+
let segmentIndex = -1;
|
|
82
|
+
for (let i = 0; i < segmented.length; i++) {
|
|
83
|
+
// busy reading a segment - only terminate on '/'
|
|
84
|
+
if (segmentIndex !== -1) {
|
|
85
|
+
if (segmented[i] === '/') {
|
|
86
|
+
output.push(segmented.slice(segmentIndex, i + 1));
|
|
87
|
+
segmentIndex = -1;
|
|
88
|
+
}
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
// new segment - check if it is relative
|
|
92
|
+
else if (segmented[i] === '.') {
|
|
93
|
+
// ../ segment
|
|
94
|
+
if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
|
|
95
|
+
output.pop();
|
|
96
|
+
i += 2;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
// ./ segment
|
|
100
|
+
else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
|
|
101
|
+
i += 1;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// it is the start of a new segment
|
|
106
|
+
while (segmented[i] === '/') {
|
|
107
|
+
i++;
|
|
108
|
+
}
|
|
109
|
+
segmentIndex = i;
|
|
110
|
+
}
|
|
111
|
+
// finish reading out the last segment
|
|
112
|
+
if (segmentIndex !== -1) {
|
|
113
|
+
output.push(segmented.slice(segmentIndex));
|
|
114
|
+
}
|
|
115
|
+
return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join('');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function resolveUrl(relUrl, parentUrl) {
|
|
119
|
+
return resolveIfNotPlainOrUrl(relUrl, parentUrl) || (isURL(relUrl) ? relUrl : resolveIfNotPlainOrUrl('./' + relUrl, parentUrl));
|
|
120
|
+
}
|
|
121
|
+
function getMatch(path, matchObj) {
|
|
122
|
+
if (matchObj[path]) {
|
|
123
|
+
return path;
|
|
124
|
+
}
|
|
125
|
+
let sepIndex = path.length;
|
|
126
|
+
do {
|
|
127
|
+
const segment = path.slice(0, sepIndex + 1);
|
|
128
|
+
if (segment in matchObj) {
|
|
129
|
+
return segment;
|
|
130
|
+
}
|
|
131
|
+
} while ((sepIndex = path.lastIndexOf('/', sepIndex - 1)) !== -1);
|
|
132
|
+
}
|
|
133
|
+
function applyPackages(id, packages) {
|
|
134
|
+
const pkgName = getMatch(id, packages);
|
|
135
|
+
if (pkgName) {
|
|
136
|
+
const pkg = packages[pkgName];
|
|
137
|
+
if (pkg === null) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
return pkg + id.slice(pkgName.length);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function resolveImportMap(importMap, resolvedOrPlain, parentUrl) {
|
|
144
|
+
let scopeUrl = parentUrl && getMatch(parentUrl, importMap.scopes);
|
|
145
|
+
while (scopeUrl) {
|
|
146
|
+
const packageResolution = applyPackages(resolvedOrPlain, importMap.scopes[scopeUrl]);
|
|
147
|
+
if (packageResolution) {
|
|
148
|
+
return packageResolution;
|
|
149
|
+
}
|
|
150
|
+
scopeUrl = getMatch(scopeUrl.slice(0, scopeUrl.lastIndexOf('/')), importMap.scopes);
|
|
151
|
+
}
|
|
152
|
+
return applyPackages(resolvedOrPlain, importMap.imports) || resolvedOrPlain.indexOf(':') !== -1 && resolvedOrPlain;
|
|
153
|
+
}
|
|
154
|
+
function resolveAndComposePackages(packages, outPackages, baseUrl, parentMap) {
|
|
155
|
+
for (const p in packages) {
|
|
156
|
+
const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl) || p;
|
|
157
|
+
const target = packages[p];
|
|
158
|
+
if (typeof target !== 'string') {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const mapped = resolveImportMap(parentMap, resolveIfNotPlainOrUrl(target, baseUrl) || target, baseUrl);
|
|
162
|
+
if (mapped) {
|
|
163
|
+
outPackages[resolvedLhs] = mapped;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
// console.warn(
|
|
167
|
+
// `Mapping "${ p }" -> "${ packages[ p ] }" does not resolve`
|
|
168
|
+
// );
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function resolveAndComposeImportMap(json, baseUrl, parentMap) {
|
|
172
|
+
const outMap = {
|
|
173
|
+
imports: Object.assign({}, parentMap.imports),
|
|
174
|
+
scopes: Object.assign({}, parentMap.scopes)
|
|
175
|
+
};
|
|
176
|
+
if (json.imports) {
|
|
177
|
+
resolveAndComposePackages(json.imports, outMap.imports, baseUrl, parentMap);
|
|
178
|
+
}
|
|
179
|
+
if (json.scopes) {
|
|
180
|
+
for (const s in json.scopes) {
|
|
181
|
+
const resolvedScope = resolveUrl(s, baseUrl);
|
|
182
|
+
resolveAndComposePackages(json.scopes[s], outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}), baseUrl, parentMap);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return outMap;
|
|
186
|
+
}
|
|
187
|
+
let importMap = {
|
|
188
|
+
imports: {},
|
|
189
|
+
scopes: {}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// TODO: check if this baseURI should change per document, and so
|
|
193
|
+
// it need to be passed as a parameter to methods like `resolve`.
|
|
194
|
+
const baseUrl = document.baseURI;
|
|
195
|
+
const pageBaseUrl = baseUrl;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Extends the internal dynamic import map with the passed one.
|
|
199
|
+
*
|
|
200
|
+
* @param importMapIn Import map.
|
|
201
|
+
* @param importMapIn.imports Imports declaration.
|
|
202
|
+
* @param importMapIn.scopes Scopes declaration.
|
|
203
|
+
*/
|
|
204
|
+
function addImportMap(importMapIn) {
|
|
205
|
+
importMap = resolveAndComposeImportMap(importMapIn, pageBaseUrl, importMap);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Resolves the URL of the passed module ID against the current internal
|
|
210
|
+
* dynamic import map.
|
|
211
|
+
*
|
|
212
|
+
* @param id Module ID.
|
|
213
|
+
* @param parentUrl Parent URL, in case the module ID is relative.
|
|
214
|
+
* @return Resolved module URL.
|
|
215
|
+
*/
|
|
216
|
+
function resolve(id, parentUrl) {
|
|
217
|
+
const urlResolved = resolveIfNotPlainOrUrl(id, parentUrl);
|
|
218
|
+
return resolveImportMap(importMap, urlResolved || id, parentUrl) || id;
|
|
219
|
+
}
|
|
220
|
+
//# sourceMappingURL=resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["backslashRegEx","isURL","url","indexOf","URL","_","resolveIfNotPlainOrUrl","relUrl","parentUrl","hIdx","qIdx","slice","replace","length","parentProtocol","pathname","segmented","lastIndexOf","output","segmentIndex","i","push","pop","join","resolveUrl","getMatch","path","matchObj","sepIndex","segment","applyPackages","id","packages","pkgName","pkg","resolveImportMap","importMap","resolvedOrPlain","scopeUrl","scopes","packageResolution","imports","resolveAndComposePackages","outPackages","baseUrl","parentMap","p","resolvedLhs","target","mapped","resolveAndComposeImportMap","json","outMap","Object","assign","s","resolvedScope","document","baseURI","pageBaseUrl","addImportMap","importMapIn","resolve","urlResolved"],"sources":["@wordpress/interactivity-router/src/assets/dynamic-importmap/resolver.ts"],"sourcesContent":["/**\n * This code is derived from the following projects:\n *\n * 1. dynamic-importmap (https://github.com/keller-mark/dynamic-importmap)\n * 2. es-module-shims (https://github.com/guybedford/es-module-shims)\n *\n * The original implementation was created by Guy Bedford in es-module-shims,\n * then adapted by Mark Keller in dynamic-importmap, and further modified\n * for use in this project.\n *\n * Both projects are licensed under the MIT license.\n *\n * MIT License: https://opensource.org/licenses/MIT\n */\n\nconst backslashRegEx = /\\\\/g;\n\nfunction isURL( url: string ) {\n\tif ( url.indexOf( ':' ) === -1 ) {\n\t\treturn false;\n\t}\n\ttry {\n\t\tnew URL( url );\n\t\treturn true;\n\t} catch ( _ ) {\n\t\treturn false;\n\t}\n}\n\nfunction resolveIfNotPlainOrUrl( relUrl, parentUrl ) {\n\tconst hIdx = parentUrl.indexOf( '#' ),\n\t\tqIdx = parentUrl.indexOf( '?' );\n\tif ( hIdx + qIdx > -2 ) {\n\t\tparentUrl = parentUrl.slice(\n\t\t\t0,\n\t\t\t// eslint-disable-next-line no-nested-ternary\n\t\t\thIdx === -1 ? qIdx : qIdx === -1 || qIdx > hIdx ? hIdx : qIdx\n\t\t);\n\t}\n\tif ( relUrl.indexOf( '\\\\' ) !== -1 ) {\n\t\trelUrl = relUrl.replace( backslashRegEx, '/' );\n\t}\n\t// protocol-relative\n\tif ( relUrl[ 0 ] === '/' && relUrl[ 1 ] === '/' ) {\n\t\treturn parentUrl.slice( 0, parentUrl.indexOf( ':' ) + 1 ) + relUrl;\n\t}\n\t// relative-url\n\telse if (\n\t\t( relUrl[ 0 ] === '.' &&\n\t\t\t( relUrl[ 1 ] === '/' ||\n\t\t\t\t( relUrl[ 1 ] === '.' &&\n\t\t\t\t\t( relUrl[ 2 ] === '/' ||\n\t\t\t\t\t\t( relUrl.length === 2 && ( relUrl += '/' ) ) ) ) ||\n\t\t\t\t( relUrl.length === 1 && ( relUrl += '/' ) ) ) ) ||\n\t\trelUrl[ 0 ] === '/'\n\t) {\n\t\tconst parentProtocol = parentUrl.slice(\n\t\t\t0,\n\t\t\tparentUrl.indexOf( ':' ) + 1\n\t\t);\n\t\t// Disabled, but these cases will give inconsistent results for deep backtracking\n\t\t//if (parentUrl[parentProtocol.length] !== '/')\n\t\t// throw new Error('Cannot resolve');\n\t\t// read pathname from parent URL\n\t\t// pathname taken to be part after leading \"/\"\n\t\tlet pathname;\n\t\tif ( parentUrl[ parentProtocol.length + 1 ] === '/' ) {\n\t\t\t// resolving to a :// so we need to read out the auth and host\n\t\t\tif ( parentProtocol !== 'file:' ) {\n\t\t\t\tpathname = parentUrl.slice( parentProtocol.length + 2 );\n\t\t\t\tpathname = pathname.slice( pathname.indexOf( '/' ) + 1 );\n\t\t\t} else {\n\t\t\t\tpathname = parentUrl.slice( 8 );\n\t\t\t}\n\t\t} else {\n\t\t\t// resolving to :/ so pathname is the /... part\n\t\t\tpathname = parentUrl.slice(\n\t\t\t\tparentProtocol.length +\n\t\t\t\t\t( parentUrl[ parentProtocol.length ] === '/' )\n\t\t\t);\n\t\t}\n\n\t\tif ( relUrl[ 0 ] === '/' ) {\n\t\t\treturn (\n\t\t\t\tparentUrl.slice( 0, parentUrl.length - pathname.length - 1 ) +\n\t\t\t\trelUrl\n\t\t\t);\n\t\t}\n\n\t\t// join together and split for removal of .. and . segments\n\t\t// looping the string instead of anything fancy for perf reasons\n\t\t// '../../../../../z' resolved to 'x/y' is just 'z'\n\t\tconst segmented =\n\t\t\tpathname.slice( 0, pathname.lastIndexOf( '/' ) + 1 ) + relUrl;\n\n\t\tconst output = [];\n\t\tlet segmentIndex = -1;\n\t\tfor ( let i = 0; i < segmented.length; i++ ) {\n\t\t\t// busy reading a segment - only terminate on '/'\n\t\t\tif ( segmentIndex !== -1 ) {\n\t\t\t\tif ( segmented[ i ] === '/' ) {\n\t\t\t\t\toutput.push( segmented.slice( segmentIndex, i + 1 ) );\n\t\t\t\t\tsegmentIndex = -1;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// new segment - check if it is relative\n\t\t\telse if ( segmented[ i ] === '.' ) {\n\t\t\t\t// ../ segment\n\t\t\t\tif (\n\t\t\t\t\tsegmented[ i + 1 ] === '.' &&\n\t\t\t\t\t( segmented[ i + 2 ] === '/' || i + 2 === segmented.length )\n\t\t\t\t) {\n\t\t\t\t\toutput.pop();\n\t\t\t\t\ti += 2;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// ./ segment\n\t\t\t\telse if (\n\t\t\t\t\tsegmented[ i + 1 ] === '/' ||\n\t\t\t\t\ti + 1 === segmented.length\n\t\t\t\t) {\n\t\t\t\t\ti += 1;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// it is the start of a new segment\n\t\t\twhile ( segmented[ i ] === '/' ) {\n\t\t\t\ti++;\n\t\t\t}\n\t\t\tsegmentIndex = i;\n\t\t}\n\t\t// finish reading out the last segment\n\t\tif ( segmentIndex !== -1 ) {\n\t\t\toutput.push( segmented.slice( segmentIndex ) );\n\t\t}\n\t\treturn (\n\t\t\tparentUrl.slice( 0, parentUrl.length - pathname.length ) +\n\t\t\toutput.join( '' )\n\t\t);\n\t}\n}\n\nfunction resolveUrl( relUrl, parentUrl ) {\n\treturn (\n\t\tresolveIfNotPlainOrUrl( relUrl, parentUrl ) ||\n\t\t( isURL( relUrl )\n\t\t\t? relUrl\n\t\t\t: resolveIfNotPlainOrUrl( './' + relUrl, parentUrl ) )\n\t);\n}\n\nfunction getMatch( path, matchObj ) {\n\tif ( matchObj[ path ] ) {\n\t\treturn path;\n\t}\n\tlet sepIndex = path.length;\n\tdo {\n\t\tconst segment = path.slice( 0, sepIndex + 1 );\n\t\tif ( segment in matchObj ) {\n\t\t\treturn segment;\n\t\t}\n\t} while ( ( sepIndex = path.lastIndexOf( '/', sepIndex - 1 ) ) !== -1 );\n}\n\nfunction applyPackages( id, packages ) {\n\tconst pkgName = getMatch( id, packages );\n\tif ( pkgName ) {\n\t\tconst pkg = packages[ pkgName ];\n\t\tif ( pkg === null ) {\n\t\t\treturn;\n\t\t}\n\t\treturn pkg + id.slice( pkgName.length );\n\t}\n}\n\nfunction resolveImportMap( importMap, resolvedOrPlain, parentUrl ) {\n\tlet scopeUrl = parentUrl && getMatch( parentUrl, importMap.scopes );\n\twhile ( scopeUrl ) {\n\t\tconst packageResolution = applyPackages(\n\t\t\tresolvedOrPlain,\n\t\t\timportMap.scopes[ scopeUrl ]\n\t\t);\n\t\tif ( packageResolution ) {\n\t\t\treturn packageResolution;\n\t\t}\n\t\tscopeUrl = getMatch(\n\t\t\tscopeUrl.slice( 0, scopeUrl.lastIndexOf( '/' ) ),\n\t\t\timportMap.scopes\n\t\t);\n\t}\n\treturn (\n\t\tapplyPackages( resolvedOrPlain, importMap.imports ) ||\n\t\t( resolvedOrPlain.indexOf( ':' ) !== -1 && resolvedOrPlain )\n\t);\n}\n\nfunction resolveAndComposePackages(\n\tpackages,\n\toutPackages,\n\tbaseUrl,\n\tparentMap\n) {\n\tfor ( const p in packages ) {\n\t\tconst resolvedLhs = resolveIfNotPlainOrUrl( p, baseUrl ) || p;\n\t\tconst target = packages[ p ];\n\t\tif ( typeof target !== 'string' ) {\n\t\t\tcontinue;\n\t\t}\n\t\tconst mapped = resolveImportMap(\n\t\t\tparentMap,\n\t\t\tresolveIfNotPlainOrUrl( target, baseUrl ) || target,\n\t\t\tbaseUrl\n\t\t);\n\t\tif ( mapped ) {\n\t\t\toutPackages[ resolvedLhs ] = mapped;\n\t\t\tcontinue;\n\t\t}\n\t\t// console.warn(\n\t\t// \t`Mapping \"${ p }\" -> \"${ packages[ p ] }\" does not resolve`\n\t\t// );\n\t}\n}\n\nfunction resolveAndComposeImportMap( json, baseUrl, parentMap ) {\n\tconst outMap = {\n\t\timports: Object.assign( {}, parentMap.imports ),\n\t\tscopes: Object.assign( {}, parentMap.scopes ),\n\t};\n\n\tif ( json.imports ) {\n\t\tresolveAndComposePackages(\n\t\t\tjson.imports,\n\t\t\toutMap.imports,\n\t\t\tbaseUrl,\n\t\t\tparentMap\n\t\t);\n\t}\n\n\tif ( json.scopes ) {\n\t\tfor ( const s in json.scopes ) {\n\t\t\tconst resolvedScope = resolveUrl( s, baseUrl );\n\t\t\tresolveAndComposePackages(\n\t\t\t\tjson.scopes[ s ],\n\t\t\t\toutMap.scopes[ resolvedScope ] ||\n\t\t\t\t\t( outMap.scopes[ resolvedScope ] = {} ),\n\t\t\t\tbaseUrl,\n\t\t\t\tparentMap\n\t\t\t);\n\t\t}\n\t}\n\n\treturn outMap;\n}\n\nlet importMap = { imports: {}, scopes: {} };\n\n// TODO: check if this baseURI should change per document, and so\n// it need to be passed as a parameter to methods like `resolve`.\nconst baseUrl = document.baseURI;\nconst pageBaseUrl = baseUrl;\n\n/**\n * Extends the internal dynamic import map with the passed one.\n *\n * @param importMapIn Import map.\n * @param importMapIn.imports Imports declaration.\n * @param importMapIn.scopes Scopes declaration.\n */\nexport function addImportMap( importMapIn: {\n\timports?: Record< string, string >;\n\tscopes?: Record< string, any >;\n} ) {\n\timportMap = resolveAndComposeImportMap(\n\t\timportMapIn,\n\t\tpageBaseUrl,\n\t\timportMap\n\t);\n}\n\n/**\n * Resolves the URL of the passed module ID against the current internal\n * dynamic import map.\n *\n * @param id Module ID.\n * @param parentUrl Parent URL, in case the module ID is relative.\n * @return Resolved module URL.\n */\nexport function resolve( id: string, parentUrl: string ): string {\n\tconst urlResolved = resolveIfNotPlainOrUrl( id, parentUrl );\n\treturn resolveImportMap( importMap, urlResolved || id, parentUrl ) || id;\n}\n"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,MAAMA,cAAc,GAAG,KAAK;AAE5B,SAASC,KAAKA,CAAEC,GAAW,EAAG;EAC7B,IAAKA,GAAG,CAACC,OAAO,CAAE,GAAI,CAAC,KAAK,CAAC,CAAC,EAAG;IAChC,OAAO,KAAK;EACb;EACA,IAAI;IACH,IAAIC,GAAG,CAAEF,GAAI,CAAC;IACd,OAAO,IAAI;EACZ,CAAC,CAAC,OAAQG,CAAC,EAAG;IACb,OAAO,KAAK;EACb;AACD;AAEA,SAASC,sBAAsBA,CAAEC,MAAM,EAAEC,SAAS,EAAG;EACpD,MAAMC,IAAI,GAAGD,SAAS,CAACL,OAAO,CAAE,GAAI,CAAC;IACpCO,IAAI,GAAGF,SAAS,CAACL,OAAO,CAAE,GAAI,CAAC;EAChC,IAAKM,IAAI,GAAGC,IAAI,GAAG,CAAC,CAAC,EAAG;IACvBF,SAAS,GAAGA,SAAS,CAACG,KAAK,CAC1B,CAAC;IACD;IACAF,IAAI,KAAK,CAAC,CAAC,GAAGC,IAAI,GAAGA,IAAI,KAAK,CAAC,CAAC,IAAIA,IAAI,GAAGD,IAAI,GAAGA,IAAI,GAAGC,IAC1D,CAAC;EACF;EACA,IAAKH,MAAM,CAACJ,OAAO,CAAE,IAAK,CAAC,KAAK,CAAC,CAAC,EAAG;IACpCI,MAAM,GAAGA,MAAM,CAACK,OAAO,CAAEZ,cAAc,EAAE,GAAI,CAAC;EAC/C;EACA;EACA,IAAKO,MA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,CAACC,OAAO;AAChC,MAAMC,WAAW,GAAGf,OAAO;;AAE3B;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASgB,YAAYA,CAAEC,WAG7B,EAAG;EACHzB,SAAS,GAAGc,0BAA0B,CACrCW,WAAW,EACXF,WAAW,EACXvB,SACD,CAAC;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS0B,OAAOA,CAAE/B,EAAU,EAAEvB,SAAiB,EAAW;EAChE,MAAMuD,WAAW,GAAGzD,sBAAsB,CAAEyB,EAAE,EAAEvB,SAAU,CAAC;EAC3D,OAAO2B,gBAAgB,CAAEC,SAAS,EAAE2B,WAAW,IAAIhC,EAAE,EAAEvB,SAAU,CAAC,IAAIuB,EAAE;AACzE","ignoreList":[]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.preloadScriptModules = exports.markScriptModuleAsResolved = exports.importScriptModules = void 0;
|
|
7
|
+
var _dynamicImportmap = require("./dynamic-importmap");
|
|
8
|
+
/**
|
|
9
|
+
* Internal dependencies
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* IDs of modules that should be resolved by the browser rather than
|
|
14
|
+
* processed internally.
|
|
15
|
+
*/
|
|
16
|
+
const resolvedScriptModules = new Set();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Marks the specified module as natively resolved.
|
|
20
|
+
* @param url Script module URL.
|
|
21
|
+
*/
|
|
22
|
+
const markScriptModuleAsResolved = url => {
|
|
23
|
+
resolvedScriptModules.add(url);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Resolves and fetches modules present in the passed document, using the
|
|
28
|
+
* document's import map to resolve them.
|
|
29
|
+
*
|
|
30
|
+
* @param doc Document containing the modules to preload.
|
|
31
|
+
* @return Array of promises that resolve to a `ScriptModuleLoad` instance.
|
|
32
|
+
*/
|
|
33
|
+
exports.markScriptModuleAsResolved = markScriptModuleAsResolved;
|
|
34
|
+
const preloadScriptModules = doc => {
|
|
35
|
+
// Extract the import map from the document.
|
|
36
|
+
const importMapElement = doc.querySelector('script#wp-importmap[type=importmap]');
|
|
37
|
+
const importMap = importMapElement ? JSON.parse(importMapElement.text) : {
|
|
38
|
+
imports: {},
|
|
39
|
+
scopes: {}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Remove imports also in the initial page's import map.
|
|
43
|
+
// Those should be handled natively.
|
|
44
|
+
for (const key in _dynamicImportmap.initialImportMap.imports) {
|
|
45
|
+
delete importMap.imports[key];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Get the URL of all modules contained in the document.
|
|
49
|
+
const moduleUrls = [...doc.querySelectorAll('script[type=module][src]')].map(s => s.src);
|
|
50
|
+
|
|
51
|
+
// Resolve and fetch those not resolved natively.
|
|
52
|
+
return moduleUrls.filter(url => !resolvedScriptModules.has(url)).map(url => (0, _dynamicImportmap.preloadWithMap)(url, importMap));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Imports modules respresented by the passed `ScriptModuleLoad` instances.
|
|
57
|
+
*
|
|
58
|
+
* @param modules Array of `MoudleLoad` instances.
|
|
59
|
+
* @return Promise that resolves once all modules are imported.
|
|
60
|
+
*/
|
|
61
|
+
exports.preloadScriptModules = preloadScriptModules;
|
|
62
|
+
const importScriptModules = modules => Promise.all(modules.map(m => (0, _dynamicImportmap.importPreloadedModule)(m)));
|
|
63
|
+
exports.importScriptModules = importScriptModules;
|
|
64
|
+
//# sourceMappingURL=script-modules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_dynamicImportmap","require","resolvedScriptModules","Set","markScriptModuleAsResolved","url","add","exports","preloadScriptModules","doc","importMapElement","querySelector","importMap","JSON","parse","text","imports","scopes","key","initialImportMap","moduleUrls","querySelectorAll","map","s","src","filter","has","preloadWithMap","importScriptModules","modules","Promise","all","m","importPreloadedModule"],"sources":["@wordpress/interactivity-router/src/assets/script-modules.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport {\n\tinitialImportMap,\n\timportPreloadedModule,\n\tpreloadWithMap,\n\ttype ModuleLoad,\n} from './dynamic-importmap';\n\n/**\n * IDs of modules that should be resolved by the browser rather than\n * processed internally.\n */\nconst resolvedScriptModules = new Set< string >();\n\n/**\n * Marks the specified module as natively resolved.\n * @param url Script module URL.\n */\nexport const markScriptModuleAsResolved = ( url: string ) => {\n\tresolvedScriptModules.add( url );\n};\n\n/**\n * Resolves and fetches modules present in the passed document, using the\n * document's import map to resolve them.\n *\n * @param doc Document containing the modules to preload.\n * @return Array of promises that resolve to a `ScriptModuleLoad` instance.\n */\nexport const preloadScriptModules = ( doc: Document ) => {\n\t// Extract the import map from the document.\n\tconst importMapElement = doc.querySelector< HTMLScriptElement >(\n\t\t'script#wp-importmap[type=importmap]'\n\t);\n\tconst importMap = importMapElement\n\t\t? JSON.parse( importMapElement.text )\n\t\t: { imports: {}, scopes: {} };\n\n\t// Remove imports also in the initial page's import map.\n\t// Those should be handled natively.\n\tfor ( const key in initialImportMap.imports ) {\n\t\tdelete importMap.imports[ key ];\n\t}\n\n\t// Get the URL of all modules contained in the document.\n\tconst moduleUrls = [\n\t\t...doc.querySelectorAll< HTMLScriptElement >(\n\t\t\t'script[type=module][src]'\n\t\t),\n\t].map( ( s ) => s.src );\n\n\t// Resolve and fetch those not resolved natively.\n\treturn moduleUrls\n\t\t.filter( ( url ) => ! resolvedScriptModules.has( url ) )\n\t\t.map( ( url ) => preloadWithMap( url, importMap ) );\n};\n\n/**\n * Imports modules respresented by the passed `ScriptModuleLoad` instances.\n *\n * @param modules Array of `MoudleLoad` instances.\n * @return Promise that resolves once all modules are imported.\n */\nexport const importScriptModules = ( modules: ScriptModuleLoad[] ) =>\n\tPromise.all( modules.map( ( m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AAMO,MAAMoB,mBAAmB,GAAKC,OAA2B,IAC/DC,OAAO,CAACC,GAAG,CAAEF,OAAO,CAACP,GAAG,CAAIU,CAAC,IAAM,IAAAC,uCAAqB,EAAED,CAAE,CAAE,CAAE,CAAC;AAACzB,OAAA,CAAAqB,mBAAA,GAAAA,mBAAA","ignoreList":[]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.shortestCommonSupersequence = shortestCommonSupersequence;
|
|
7
|
+
/**
|
|
8
|
+
* Calculates the Shortest Common Supersequence (SCS) of two sequences.
|
|
9
|
+
*
|
|
10
|
+
* A supersequence is a sequence that contains both input sequences as subsequences.
|
|
11
|
+
* The shortest common supersequence is the shortest possible such sequence.
|
|
12
|
+
*
|
|
13
|
+
* This implementation uses dynamic programming with a time complexity of O(mn)
|
|
14
|
+
* and space complexity of O(mn), where m and n are the lengths of sequences X and Y.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const seq1 = [1, 3, 5];
|
|
19
|
+
* const seq2 = [2, 3, 4];
|
|
20
|
+
* const scs = shortestCommonSupersequence(seq1, seq2); // [1, 2, 3, 4, 5]
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param X The first sequence.
|
|
24
|
+
* @param Y The second sequence.
|
|
25
|
+
* @param isEqual Optional equality function to compare elements.
|
|
26
|
+
* Defaults to strict equality (===).
|
|
27
|
+
* @return The shortest common supersequence of X and Y.
|
|
28
|
+
*/
|
|
29
|
+
function shortestCommonSupersequence(X, Y, isEqual = (a, b) => a === b) {
|
|
30
|
+
const m = X.length;
|
|
31
|
+
const n = Y.length;
|
|
32
|
+
|
|
33
|
+
// Create a 2D dp table where dp[i][j] is the SCS for X[0..i-1] and Y[0..j-1].
|
|
34
|
+
const dp = Array.from({
|
|
35
|
+
length: m + 1
|
|
36
|
+
}, () => Array(n + 1).fill(null));
|
|
37
|
+
|
|
38
|
+
// Base cases: one of the sequences is empty.
|
|
39
|
+
for (let i = 0; i <= m; i++) {
|
|
40
|
+
dp[i][0] = X.slice(0, i);
|
|
41
|
+
}
|
|
42
|
+
for (let j = 0; j <= n; j++) {
|
|
43
|
+
dp[0][j] = Y.slice(0, j);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Fill in the dp table.
|
|
47
|
+
for (let i = 1; i <= m; i++) {
|
|
48
|
+
for (let j = 1; j <= n; j++) {
|
|
49
|
+
if (isEqual(X[i - 1], Y[j - 1])) {
|
|
50
|
+
// When X[i-1] equals Y[j-1], use the reference from X.
|
|
51
|
+
dp[i][j] = dp[i - 1][j - 1].concat(X[i - 1]);
|
|
52
|
+
} else {
|
|
53
|
+
// Choose the shorter option between appending X[i-1] or Y[j-1].
|
|
54
|
+
const option1 = dp[i - 1][j].concat(X[i - 1]);
|
|
55
|
+
const option2 = dp[i][j - 1].concat(Y[j - 1]);
|
|
56
|
+
dp[i][j] = option1.length <= option2.length ? option1 : option2;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return dp[m][n];
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=scs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["shortestCommonSupersequence","X","Y","isEqual","a","b","m","length","n","dp","Array","from","fill","i","slice","j","concat","option1","option2"],"sources":["@wordpress/interactivity-router/src/assets/scs.ts"],"sourcesContent":["/**\n * Calculates the Shortest Common Supersequence (SCS) of two sequences.\n *\n * A supersequence is a sequence that contains both input sequences as subsequences.\n * The shortest common supersequence is the shortest possible such sequence.\n *\n * This implementation uses dynamic programming with a time complexity of O(mn)\n * and space complexity of O(mn), where m and n are the lengths of sequences X and Y.\n *\n * @example\n * ```ts\n * const seq1 = [1, 3, 5];\n * const seq2 = [2, 3, 4];\n * const scs = shortestCommonSupersequence(seq1, seq2); // [1, 2, 3, 4, 5]\n * ```\n *\n * @param X The first sequence.\n * @param Y The second sequence.\n * @param isEqual Optional equality function to compare elements.\n * Defaults to strict equality (===).\n * @return The shortest common supersequence of X and Y.\n */\nexport function shortestCommonSupersequence< E = unknown >(\n\tX: E[],\n\tY: E[],\n\tisEqual = ( a: E, b: E ) => a === b\n) {\n\tconst m = X.length;\n\tconst n = Y.length;\n\n\t// Create a 2D dp table where dp[i][j] is the SCS for X[0..i-1] and Y[0..j-1].\n\tconst dp: E[][][] = Array.from( { length: m + 1 }, () =>\n\t\tArray( n + 1 ).fill( null )\n\t);\n\n\t// Base cases: one of the sequences is empty.\n\tfor ( let i = 0; i <= m; i++ ) {\n\t\tdp[ i ][ 0 ] = X.slice( 0, i );\n\t}\n\tfor ( let j = 0; j <= n; j++ ) {\n\t\tdp[ 0 ][ j ] = Y.slice( 0, j );\n\t}\n\n\t// Fill in the dp table.\n\tfor ( let i = 1; i <= m; i++ ) {\n\t\tfor ( let j = 1; j <= n; j++ ) {\n\t\t\tif ( isEqual( X[ i - 1 ], Y[ j - 1 ] ) ) {\n\t\t\t\t// When X[i-1] equals Y[j-1], use the reference from X.\n\t\t\t\tdp[ i ][ j ] = dp[ i - 1 ][ j - 1 ].concat( X[ i - 1 ] );\n\t\t\t} else {\n\t\t\t\t// Choose the shorter option between appending X[i-1] or Y[j-1].\n\t\t\t\tconst option1 = dp[ i - 1 ][ j ].concat( X[ i - 1 ] );\n\t\t\t\tconst option2 = dp[ i ][ j - 1 ].concat( Y[ j - 1 ] );\n\t\t\t\tdp[ i ][ j ] =\n\t\t\t\t\toption1.length <= option2.length ? option1 : option2;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn dp[ m ][ n ];\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,2BAA2BA,CAC1CC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,MAAM,CAAEf,CAAC,CAAEY,CAAC,GAAG,CAAC,CAAG,CAAC;MACzD,CAAC,MAAM;QACN;QACA,MAAMI,OAAO,GAAGR,EAAE,CAAEI,CAAC,GAAG,CAAC,CAAE,CAAEE,CAAC,CAAE,CAACC,MAAM,CAAEf,CAAC,CAAEY,CAAC,GAAG,CAAC,CAAG,CAAC;QACrD,MAAMK,OAAO,GAAGT,EAAE,CAAEI,CAAC,CAAE,CAAEE,CAAC,GAAG,CAAC,CAAE,CAACC,MAAM,CAAEd,CAAC,CAAEa,CAAC,GAAG,CAAC,CAAG,CAAC;QACrDN,EAAE,CAAEI,CAAC,CAAE,CAAEE,CAAC,CAAE,GACXE,OAAO,CAACV,MAAM,IAAIW,OAAO,CAACX,MAAM,GAAGU,OAAO,GAAGC,OAAO;MACtD;IACD;EACD;EAEA,OAAOT,EAAE,CAAEH,CAAC,CAAE,CAAEE,CAAC,CAAE;AACpB","ignoreList":[]}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.preloadStyles = exports.normalizeMedia = exports.applyStyles = void 0;
|
|
7
|
+
exports.updateStylesWithSCS = updateStylesWithSCS;
|
|
8
|
+
var _scs = require("./scs");
|
|
9
|
+
/**
|
|
10
|
+
* Internal dependencies
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compares the passed style or link elements to check if they can be
|
|
15
|
+
* considered equal.
|
|
16
|
+
*
|
|
17
|
+
* @param a `<style>` or `<link>` element.
|
|
18
|
+
* @param b `<style>` or `<link>` element.
|
|
19
|
+
* @return Whether they are considered equal.
|
|
20
|
+
*/
|
|
21
|
+
const areNodesEqual = (a, b) => a.isEqualNode(b);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Normalizes the passed style or link element, reverting the changes
|
|
25
|
+
* made by {@link prepareStylePromise|`prepareStylePromise`} to the
|
|
26
|
+
* `data-original-media` and `media`.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* The following elements should be normalized to the same element:
|
|
30
|
+
* ```html
|
|
31
|
+
* <link rel="stylesheet" src="./assets/styles.css">
|
|
32
|
+
* <link rel="stylesheet" src="./assets/styles.css" media="all">
|
|
33
|
+
* <link rel="stylesheet" src="./assets/styles.css" media="preload">
|
|
34
|
+
* <link rel="stylesheet" src="./assets/styles.css" media="preload" data-original-media="all">
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param element `<style>` or `<link>` element.
|
|
38
|
+
* @return Normalized node.
|
|
39
|
+
*/
|
|
40
|
+
const normalizeMedia = element => {
|
|
41
|
+
element = element.cloneNode(true);
|
|
42
|
+
const media = element.media;
|
|
43
|
+
const {
|
|
44
|
+
originalMedia
|
|
45
|
+
} = element.dataset;
|
|
46
|
+
if (media === 'preload') {
|
|
47
|
+
element.media = originalMedia || 'all';
|
|
48
|
+
element.removeAttribute('data-original-media');
|
|
49
|
+
} else if (!element.media) {
|
|
50
|
+
element.media = 'all';
|
|
51
|
+
}
|
|
52
|
+
return element;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Adds the minimum style elements from Y around those in X using a
|
|
57
|
+
* shortest common supersequence algorithm, returning a list of
|
|
58
|
+
* promises for all the elements in Y.
|
|
59
|
+
*
|
|
60
|
+
* If X is empty, it appends all elements in Y to the passed parent
|
|
61
|
+
* element or to `document.head` instead.
|
|
62
|
+
*
|
|
63
|
+
* The returned promises resolve once the corresponding style element
|
|
64
|
+
* is loaded and ready. Those elements that are also in X return a
|
|
65
|
+
* cached promise.
|
|
66
|
+
*
|
|
67
|
+
* The algorithm ensures that the final style elements present in the
|
|
68
|
+
* document (or the passed `parent` element) are in the correct order
|
|
69
|
+
* and they are included in either X or Y.
|
|
70
|
+
*
|
|
71
|
+
* @param X Base list of style elements.
|
|
72
|
+
* @param Y List of style elements.
|
|
73
|
+
* @param parent Optional parent element to append to the new style elements.
|
|
74
|
+
* @return List of promises that resolve once the elements in Y are ready.
|
|
75
|
+
*/
|
|
76
|
+
exports.normalizeMedia = normalizeMedia;
|
|
77
|
+
function updateStylesWithSCS(X, Y, parent = window.document.head) {
|
|
78
|
+
if (X.length === 0) {
|
|
79
|
+
return Y.map(element => {
|
|
80
|
+
const promise = prepareStylePromise(element);
|
|
81
|
+
parent.appendChild(element);
|
|
82
|
+
return promise;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Create normalized arrays for comparison.
|
|
87
|
+
const xNormalized = X.map(normalizeMedia);
|
|
88
|
+
const yNormalized = Y.map(normalizeMedia);
|
|
89
|
+
|
|
90
|
+
// The `scs` array contains normalized elements.
|
|
91
|
+
const scs = (0, _scs.shortestCommonSupersequence)(xNormalized, yNormalized, areNodesEqual);
|
|
92
|
+
const xLength = X.length;
|
|
93
|
+
const yLength = Y.length;
|
|
94
|
+
const promises = [];
|
|
95
|
+
let last = X[xLength - 1];
|
|
96
|
+
let xIndex = 0;
|
|
97
|
+
let yIndex = 0;
|
|
98
|
+
for (const scsElement of scs) {
|
|
99
|
+
// Actual elements that will end up in the DOM.
|
|
100
|
+
const xElement = X[xIndex];
|
|
101
|
+
const yElement = Y[yIndex];
|
|
102
|
+
// Normalized elements for comparison.
|
|
103
|
+
const xNormEl = xNormalized[xIndex];
|
|
104
|
+
const yNormEl = yNormalized[yIndex];
|
|
105
|
+
if (xIndex < xLength && areNodesEqual(xNormEl, scsElement)) {
|
|
106
|
+
if (yIndex < yLength && areNodesEqual(yNormEl, scsElement)) {
|
|
107
|
+
promises.push(prepareStylePromise(xElement));
|
|
108
|
+
yIndex++;
|
|
109
|
+
}
|
|
110
|
+
xIndex++;
|
|
111
|
+
} else {
|
|
112
|
+
promises.push(prepareStylePromise(yElement));
|
|
113
|
+
if (xIndex < xLength) {
|
|
114
|
+
xElement.before(yElement);
|
|
115
|
+
} else {
|
|
116
|
+
last.after(yElement);
|
|
117
|
+
last = yElement;
|
|
118
|
+
}
|
|
119
|
+
yIndex++;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return promises;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Cache of promises per style elements.
|
|
127
|
+
*
|
|
128
|
+
* Each style element has their own associated `Promise` that resolves
|
|
129
|
+
* once the element has been loaded and is ready.
|
|
130
|
+
*/
|
|
131
|
+
const stylePromiseCache = new WeakMap();
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Prepares and returns the corresponding `Promise` for the passed style
|
|
135
|
+
* element.
|
|
136
|
+
*
|
|
137
|
+
* It returns the cached promise if it exists. Otherwise, constructs
|
|
138
|
+
* a `Promise` that resolves once the element has finished loading.
|
|
139
|
+
*
|
|
140
|
+
* For those elements that are not in the DOM yet, this function
|
|
141
|
+
* injects a `media="preload"` attribute to the passed element so the
|
|
142
|
+
* style is loaded without applying any styles to the document.
|
|
143
|
+
*
|
|
144
|
+
* @param element Style element.
|
|
145
|
+
* @return The associated `Promise` to the passed element.
|
|
146
|
+
*/
|
|
147
|
+
const prepareStylePromise = element => {
|
|
148
|
+
if (stylePromiseCache.has(element)) {
|
|
149
|
+
return stylePromiseCache.get(element);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// When the element exists in the main document and its media attribute
|
|
153
|
+
// is not "preload", that means the element comes from the initial page.
|
|
154
|
+
// The `media` attribute doesn't need to be handled in this case.
|
|
155
|
+
if (window.document.contains(element) && element.media !== 'preload') {
|
|
156
|
+
const promise = Promise.resolve(element);
|
|
157
|
+
stylePromiseCache.set(element, promise);
|
|
158
|
+
return promise;
|
|
159
|
+
}
|
|
160
|
+
if (element.hasAttribute('media') && element.media !== 'all') {
|
|
161
|
+
element.dataset.originalMedia = element.media;
|
|
162
|
+
}
|
|
163
|
+
element.media = 'preload';
|
|
164
|
+
if (element instanceof HTMLStyleElement) {
|
|
165
|
+
const promise = Promise.resolve(element);
|
|
166
|
+
stylePromiseCache.set(element, promise);
|
|
167
|
+
return promise;
|
|
168
|
+
}
|
|
169
|
+
const promise = new Promise((resolve, reject) => {
|
|
170
|
+
element.addEventListener('load', () => resolve(element));
|
|
171
|
+
element.addEventListener('error', event => {
|
|
172
|
+
const {
|
|
173
|
+
href
|
|
174
|
+
} = event.target;
|
|
175
|
+
reject(Error(`The style sheet with the following URL failed to load: ${href}`));
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
stylePromiseCache.set(element, promise);
|
|
179
|
+
return promise;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Cache of style promise lists per URL.
|
|
184
|
+
*
|
|
185
|
+
* It contains the list of style elements associated to the page with the
|
|
186
|
+
* passed URL. The original order is preserved to respect the CSS cascade.
|
|
187
|
+
*
|
|
188
|
+
* Each included promise resolves when the associated style element is ready.
|
|
189
|
+
*/
|
|
190
|
+
const styleSheetCache = new Map();
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Prepares all style elements contained in the passed document.
|
|
194
|
+
*
|
|
195
|
+
* This function calls {@link updateStylesWithSCS|`updateStylesWithSCS`}
|
|
196
|
+
* to insert only the minimum amount of style elements into the DOM, so
|
|
197
|
+
* those present in the passed document end up in the DOM while the order
|
|
198
|
+
* is respected.
|
|
199
|
+
*
|
|
200
|
+
* New appended style elements contain a `media=preload` attribute to
|
|
201
|
+
* make them effectively disabled until they are applied with the
|
|
202
|
+
* {@link applyStyles|`applyStyles`} function.
|
|
203
|
+
*
|
|
204
|
+
* @param doc Document instance.
|
|
205
|
+
* @param url URL for the passed document.
|
|
206
|
+
* @return A list of promises for each style element in the passed document.
|
|
207
|
+
*/
|
|
208
|
+
const preloadStyles = (doc, url) => {
|
|
209
|
+
if (!styleSheetCache.has(url)) {
|
|
210
|
+
const currentStyleElements = Array.from(window.document.querySelectorAll('style,link[rel=stylesheet]'));
|
|
211
|
+
const newStyleElements = Array.from(doc.querySelectorAll('style,link[rel=stylesheet]'));
|
|
212
|
+
|
|
213
|
+
// Set styles in order.
|
|
214
|
+
const stylePromises = updateStylesWithSCS(currentStyleElements, newStyleElements);
|
|
215
|
+
styleSheetCache.set(url, stylePromises);
|
|
216
|
+
}
|
|
217
|
+
return styleSheetCache.get(url);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Traverses all style elements in the DOM, enabling only those included
|
|
222
|
+
* in the passed list and disabling the others.
|
|
223
|
+
*
|
|
224
|
+
* If the style element has the `data-original-media` attribute, the
|
|
225
|
+
* original `media` value is restored.
|
|
226
|
+
*
|
|
227
|
+
* @param styles List of style elements to apply.
|
|
228
|
+
*/
|
|
229
|
+
exports.preloadStyles = preloadStyles;
|
|
230
|
+
const applyStyles = styles => {
|
|
231
|
+
window.document.querySelectorAll('style,link[rel=stylesheet]').forEach(el => {
|
|
232
|
+
if (el.sheet) {
|
|
233
|
+
if (styles.includes(el)) {
|
|
234
|
+
const {
|
|
235
|
+
originalMedia = 'all'
|
|
236
|
+
} = el.dataset;
|
|
237
|
+
el.sheet.media.mediaText = originalMedia;
|
|
238
|
+
el.sheet.disabled = false;
|
|
239
|
+
} else {
|
|
240
|
+
el.sheet.disabled = true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
exports.applyStyles = applyStyles;
|
|
246
|
+
//# sourceMappingURL=styles.js.map
|