@wordpress/interactivity-router 2.32.0 → 2.32.1-next.b8c8708f3.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 +2 -0
- package/build/assets/dynamic-importmap/fetch.js +32 -43
- package/build/assets/dynamic-importmap/fetch.js.map +7 -1
- package/build/assets/dynamic-importmap/index.js +43 -73
- package/build/assets/dynamic-importmap/index.js.map +7 -1
- package/build/assets/dynamic-importmap/loader.js +139 -172
- package/build/assets/dynamic-importmap/loader.js.map +7 -1
- package/build/assets/dynamic-importmap/resolver.js +99 -112
- package/build/assets/dynamic-importmap/resolver.js.map +7 -1
- package/build/assets/script-modules.js +46 -58
- package/build/assets/script-modules.js.map +7 -1
- package/build/assets/scs.js +30 -39
- package/build/assets/scs.js.map +7 -1
- package/build/assets/styles.js +81 -171
- package/build/assets/styles.js.map +7 -1
- package/build/full-page.js +43 -36
- package/build/full-page.js.map +7 -1
- package/build/index.js +139 -265
- package/build/index.js.map +7 -1
- package/build-module/assets/dynamic-importmap/fetch.js +11 -39
- package/build-module/assets/dynamic-importmap/fetch.js.map +7 -1
- package/build-module/assets/dynamic-importmap/index.js +18 -52
- package/build-module/assets/dynamic-importmap/index.js.map +7 -1
- package/build-module/assets/dynamic-importmap/loader.js +101 -165
- package/build-module/assets/dynamic-importmap/loader.js.map +7 -1
- package/build-module/assets/dynamic-importmap/resolver.js +78 -108
- package/build-module/assets/dynamic-importmap/resolver.js.map +7 -1
- package/build-module/assets/script-modules.js +25 -49
- package/build-module/assets/script-modules.js.map +7 -1
- package/build-module/assets/scs.js +9 -35
- package/build-module/assets/scs.js.map +7 -1
- package/build-module/assets/styles.js +58 -163
- package/build-module/assets/styles.js.map +7 -1
- package/build-module/full-page.js +21 -32
- package/build-module/full-page.js.map +7 -1
- package/build-module/index.js +108 -257
- package/build-module/index.js.map +7 -1
- package/build-types/index.d.ts.map +1 -1
- package/package.json +12 -4
- package/src/index.ts +2 -3
- package/tsconfig.main.tsbuildinfo +1 -1
|
@@ -1,85 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { shortestCommonSupersequence } from './scs';
|
|
5
|
-
/**
|
|
6
|
-
* Compares the passed style or link elements to check if they can be
|
|
7
|
-
* considered equal.
|
|
8
|
-
*
|
|
9
|
-
* @param a `<style>` or `<link>` element.
|
|
10
|
-
* @param b `<style>` or `<link>` element.
|
|
11
|
-
* @return Whether they are considered equal.
|
|
12
|
-
*/
|
|
1
|
+
import { shortestCommonSupersequence } from "./scs";
|
|
13
2
|
const areNodesEqual = (a, b) => a.isEqualNode(b);
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Normalizes the passed style or link element, reverting the changes
|
|
17
|
-
* made by {@link prepareStylePromise|`prepareStylePromise`} to the
|
|
18
|
-
* `data-original-media` and `media`.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* The following elements should be normalized to the same element:
|
|
22
|
-
* ```html
|
|
23
|
-
* <link rel="stylesheet" src="./assets/styles.css">
|
|
24
|
-
* <link rel="stylesheet" src="./assets/styles.css" media="all">
|
|
25
|
-
* <link rel="stylesheet" src="./assets/styles.css" media="preload">
|
|
26
|
-
* <link rel="stylesheet" src="./assets/styles.css" media="preload" data-original-media="all">
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* @param element `<style>` or `<link>` element.
|
|
30
|
-
* @return Normalized node.
|
|
31
|
-
*/
|
|
32
|
-
export const normalizeMedia = element => {
|
|
3
|
+
const normalizeMedia = (element) => {
|
|
33
4
|
element = element.cloneNode(true);
|
|
34
5
|
const media = element.media;
|
|
35
|
-
const {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
element.media = originalMedia || 'all';
|
|
40
|
-
element.removeAttribute('data-original-media');
|
|
6
|
+
const { originalMedia } = element.dataset;
|
|
7
|
+
if (media === "preload") {
|
|
8
|
+
element.media = originalMedia || "all";
|
|
9
|
+
element.removeAttribute("data-original-media");
|
|
41
10
|
} else if (!element.media) {
|
|
42
|
-
element.media =
|
|
11
|
+
element.media = "all";
|
|
43
12
|
}
|
|
44
13
|
return element;
|
|
45
14
|
};
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Adds the minimum style elements from Y around those in X using a
|
|
49
|
-
* shortest common supersequence algorithm, returning a list of
|
|
50
|
-
* promises for all the elements in Y.
|
|
51
|
-
*
|
|
52
|
-
* If X is empty, it appends all elements in Y to the passed parent
|
|
53
|
-
* element or to `document.head` instead.
|
|
54
|
-
*
|
|
55
|
-
* The returned promises resolve once the corresponding style element
|
|
56
|
-
* is loaded and ready. Those elements that are also in X return a
|
|
57
|
-
* cached promise.
|
|
58
|
-
*
|
|
59
|
-
* The algorithm ensures that the final style elements present in the
|
|
60
|
-
* document (or the passed `parent` element) are in the correct order
|
|
61
|
-
* and they are included in either X or Y.
|
|
62
|
-
*
|
|
63
|
-
* @param X Base list of style elements.
|
|
64
|
-
* @param Y List of style elements.
|
|
65
|
-
* @param parent Optional parent element to append to the new style elements.
|
|
66
|
-
* @return List of promises that resolve once the elements in Y are ready.
|
|
67
|
-
*/
|
|
68
|
-
export function updateStylesWithSCS(X, Y, parent = window.document.head) {
|
|
15
|
+
function updateStylesWithSCS(X, Y, parent = window.document.head) {
|
|
69
16
|
if (X.length === 0) {
|
|
70
|
-
return Y.map(element => {
|
|
17
|
+
return Y.map((element) => {
|
|
71
18
|
const promise = prepareStylePromise(element);
|
|
72
19
|
parent.appendChild(element);
|
|
73
20
|
return promise;
|
|
74
21
|
});
|
|
75
22
|
}
|
|
76
|
-
|
|
77
|
-
// Create normalized arrays for comparison.
|
|
78
23
|
const xNormalized = X.map(normalizeMedia);
|
|
79
24
|
const yNormalized = Y.map(normalizeMedia);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
25
|
+
const scs = shortestCommonSupersequence(
|
|
26
|
+
xNormalized,
|
|
27
|
+
yNormalized,
|
|
28
|
+
areNodesEqual
|
|
29
|
+
);
|
|
83
30
|
const xLength = X.length;
|
|
84
31
|
const yLength = Y.length;
|
|
85
32
|
const promises = [];
|
|
@@ -87,10 +34,8 @@ export function updateStylesWithSCS(X, Y, parent = window.document.head) {
|
|
|
87
34
|
let xIndex = 0;
|
|
88
35
|
let yIndex = 0;
|
|
89
36
|
for (const scsElement of scs) {
|
|
90
|
-
// Actual elements that will end up in the DOM.
|
|
91
37
|
const xElement = X[xIndex];
|
|
92
38
|
const yElement = Y[yIndex];
|
|
93
|
-
// Normalized elements for comparison.
|
|
94
39
|
const xNormEl = xNormalized[xIndex];
|
|
95
40
|
const yNormEl = yNormalized[yIndex];
|
|
96
41
|
if (xIndex < xLength && areNodesEqual(xNormEl, scsElement)) {
|
|
@@ -112,120 +57,64 @@ export function updateStylesWithSCS(X, Y, parent = window.document.head) {
|
|
|
112
57
|
}
|
|
113
58
|
return promises;
|
|
114
59
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
* Cache of promises per style elements.
|
|
118
|
-
*
|
|
119
|
-
* Each style element has their own associated `Promise` that resolves
|
|
120
|
-
* once the element has been loaded and is ready.
|
|
121
|
-
*/
|
|
122
|
-
const stylePromiseCache = new WeakMap();
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Prepares and returns the corresponding `Promise` for the passed style
|
|
126
|
-
* element.
|
|
127
|
-
*
|
|
128
|
-
* It returns the cached promise if it exists. Otherwise, constructs
|
|
129
|
-
* a `Promise` that resolves once the element has finished loading.
|
|
130
|
-
*
|
|
131
|
-
* For those elements that are not in the DOM yet, this function
|
|
132
|
-
* injects a `media="preload"` attribute to the passed element so the
|
|
133
|
-
* style is loaded without applying any styles to the document.
|
|
134
|
-
*
|
|
135
|
-
* @param element Style element.
|
|
136
|
-
* @return The associated `Promise` to the passed element.
|
|
137
|
-
*/
|
|
138
|
-
const prepareStylePromise = element => {
|
|
60
|
+
const stylePromiseCache = /* @__PURE__ */ new WeakMap();
|
|
61
|
+
const prepareStylePromise = (element) => {
|
|
139
62
|
if (stylePromiseCache.has(element)) {
|
|
140
63
|
return stylePromiseCache.get(element);
|
|
141
64
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (window.document.contains(element) && element.media !== 'preload') {
|
|
147
|
-
const promise = Promise.resolve(element);
|
|
148
|
-
stylePromiseCache.set(element, promise);
|
|
149
|
-
return promise;
|
|
65
|
+
if (window.document.contains(element) && element.media !== "preload") {
|
|
66
|
+
const promise2 = Promise.resolve(element);
|
|
67
|
+
stylePromiseCache.set(element, promise2);
|
|
68
|
+
return promise2;
|
|
150
69
|
}
|
|
151
|
-
if (element.hasAttribute(
|
|
70
|
+
if (element.hasAttribute("media") && element.media !== "all") {
|
|
152
71
|
element.dataset.originalMedia = element.media;
|
|
153
72
|
}
|
|
154
|
-
element.media =
|
|
73
|
+
element.media = "preload";
|
|
155
74
|
if (element instanceof HTMLStyleElement) {
|
|
156
|
-
const
|
|
157
|
-
stylePromiseCache.set(element,
|
|
158
|
-
return
|
|
75
|
+
const promise2 = Promise.resolve(element);
|
|
76
|
+
stylePromiseCache.set(element, promise2);
|
|
77
|
+
return promise2;
|
|
159
78
|
}
|
|
160
79
|
const promise = new Promise((resolve, reject) => {
|
|
161
|
-
element.addEventListener(
|
|
162
|
-
element.addEventListener(
|
|
163
|
-
const {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
80
|
+
element.addEventListener("load", () => resolve(element));
|
|
81
|
+
element.addEventListener("error", (event) => {
|
|
82
|
+
const { href } = event.target;
|
|
83
|
+
reject(
|
|
84
|
+
Error(
|
|
85
|
+
`The style sheet with the following URL failed to load: ${href}`
|
|
86
|
+
)
|
|
87
|
+
);
|
|
167
88
|
});
|
|
168
89
|
});
|
|
169
90
|
stylePromiseCache.set(element, promise);
|
|
170
91
|
return promise;
|
|
171
92
|
};
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
* Cache of style promise lists per URL.
|
|
175
|
-
*
|
|
176
|
-
* It contains the list of style elements associated to the page with the
|
|
177
|
-
* passed URL. The original order is preserved to respect the CSS cascade.
|
|
178
|
-
*
|
|
179
|
-
* Each included promise resolves when the associated style element is ready.
|
|
180
|
-
*/
|
|
181
|
-
const styleSheetCache = new Map();
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Prepares all style elements contained in the passed document.
|
|
185
|
-
*
|
|
186
|
-
* This function calls {@link updateStylesWithSCS|`updateStylesWithSCS`}
|
|
187
|
-
* to insert only the minimum amount of style elements into the DOM, so
|
|
188
|
-
* those present in the passed document end up in the DOM while the order
|
|
189
|
-
* is respected.
|
|
190
|
-
*
|
|
191
|
-
* New appended style elements contain a `media=preload` attribute to
|
|
192
|
-
* make them effectively disabled until they are applied with the
|
|
193
|
-
* {@link applyStyles|`applyStyles`} function.
|
|
194
|
-
*
|
|
195
|
-
* @param doc Document instance.
|
|
196
|
-
* @param url URL for the passed document.
|
|
197
|
-
* @return A list of promises for each style element in the passed document.
|
|
198
|
-
*/
|
|
199
|
-
export const preloadStyles = (doc, url) => {
|
|
93
|
+
const styleSheetCache = /* @__PURE__ */ new Map();
|
|
94
|
+
const preloadStyles = (doc, url) => {
|
|
200
95
|
if (!styleSheetCache.has(url)) {
|
|
201
|
-
const currentStyleElements = Array.from(
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
96
|
+
const currentStyleElements = Array.from(
|
|
97
|
+
window.document.querySelectorAll(
|
|
98
|
+
"style,link[rel=stylesheet]"
|
|
99
|
+
)
|
|
100
|
+
);
|
|
101
|
+
const newStyleElements = Array.from(
|
|
102
|
+
doc.querySelectorAll("style,link[rel=stylesheet]")
|
|
103
|
+
);
|
|
104
|
+
const stylePromises = updateStylesWithSCS(
|
|
105
|
+
currentStyleElements,
|
|
106
|
+
newStyleElements
|
|
107
|
+
);
|
|
206
108
|
styleSheetCache.set(url, stylePromises);
|
|
207
109
|
}
|
|
208
110
|
return styleSheetCache.get(url);
|
|
209
111
|
};
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
* Traverses all style elements in the DOM, enabling only those included
|
|
213
|
-
* in the passed list and disabling the others.
|
|
214
|
-
*
|
|
215
|
-
* If the style element has the `data-original-media` attribute, the
|
|
216
|
-
* original `media` value is restored.
|
|
217
|
-
*
|
|
218
|
-
* @param styles List of style elements to apply.
|
|
219
|
-
*/
|
|
220
|
-
export const applyStyles = styles => {
|
|
221
|
-
window.document.querySelectorAll('style,link[rel=stylesheet]').forEach(el => {
|
|
112
|
+
const applyStyles = (styles) => {
|
|
113
|
+
window.document.querySelectorAll("style,link[rel=stylesheet]").forEach((el) => {
|
|
222
114
|
if (el.sheet) {
|
|
223
115
|
if (styles.includes(el)) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const {
|
|
227
|
-
originalMedia = 'all'
|
|
228
|
-
} = el.dataset;
|
|
116
|
+
if (el.sheet.media.mediaText === "preload") {
|
|
117
|
+
const { originalMedia = "all" } = el.dataset;
|
|
229
118
|
el.sheet.media.mediaText = originalMedia;
|
|
230
119
|
}
|
|
231
120
|
el.sheet.disabled = false;
|
|
@@ -235,4 +124,10 @@ export const applyStyles = styles => {
|
|
|
235
124
|
}
|
|
236
125
|
});
|
|
237
126
|
};
|
|
238
|
-
|
|
127
|
+
export {
|
|
128
|
+
applyStyles,
|
|
129
|
+
normalizeMedia,
|
|
130
|
+
preloadStyles,
|
|
131
|
+
updateStylesWithSCS
|
|
132
|
+
};
|
|
133
|
+
//# sourceMappingURL=styles.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"names":["shortestCommonSupersequence","areNodesEqual","a","b","isEqualNode","normalizeMedia","element","cloneNode","media","originalMedia","dataset","removeAttribute","updateStylesWithSCS","X","Y","parent","window","document","head","length","map","promise","prepareStylePromise","appendChild","xNormalized","yNormalized","scs","xLength","yLength","promises","last","xIndex","yIndex","scsElement","xElement","yElement","xNormEl","yNormEl","push","before","after","stylePromiseCache","WeakMap","has","get","contains","Promise","resolve","set","hasAttribute","HTMLStyleElement","reject","addEventListener","event","href","target","Error","styleSheetCache","Map","preloadStyles","doc","url","currentStyleElements","Array","from","querySelectorAll","newStyleElements","stylePromises","applyStyles","styles","forEach","el","sheet","includes","mediaText","disabled"],"sources":["@wordpress/interactivity-router/src/assets/styles.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { shortestCommonSupersequence } from './scs';\n\nexport type StyleElement = HTMLLinkElement | HTMLStyleElement;\n\n/**\n * Compares the passed style or link elements to check if they can be\n * considered equal.\n *\n * @param a `<style>` or `<link>` element.\n * @param b `<style>` or `<link>` element.\n * @return Whether they are considered equal.\n */\nconst areNodesEqual = ( a: StyleElement, b: StyleElement ): boolean =>\n\ta.isEqualNode( b );\n\n/**\n * Normalizes the passed style or link element, reverting the changes\n * made by {@link prepareStylePromise|`prepareStylePromise`} to the\n * `data-original-media` and `media`.\n *\n * @example\n * The following elements should be normalized to the same element:\n * ```html\n * <link rel=\"stylesheet\" src=\"./assets/styles.css\">\n * <link rel=\"stylesheet\" src=\"./assets/styles.css\" media=\"all\">\n * <link rel=\"stylesheet\" src=\"./assets/styles.css\" media=\"preload\">\n * <link rel=\"stylesheet\" src=\"./assets/styles.css\" media=\"preload\" data-original-media=\"all\">\n * ```\n *\n * @param element `<style>` or `<link>` element.\n * @return Normalized node.\n */\nexport const normalizeMedia = ( element: StyleElement ): StyleElement => {\n\telement = element.cloneNode( true ) as StyleElement;\n\tconst media = element.media;\n\tconst { originalMedia } = element.dataset;\n\n\tif ( media === 'preload' ) {\n\t\telement.media = originalMedia || 'all';\n\t\telement.removeAttribute( 'data-original-media' );\n\t} else if ( ! element.media ) {\n\t\telement.media = 'all';\n\t}\n\treturn element;\n};\n\n/**\n * Adds the minimum style elements from Y around those in X using a\n * shortest common supersequence algorithm, returning a list of\n * promises for all the elements in Y.\n *\n * If X is empty, it appends all elements in Y to the passed parent\n * element or to `document.head` instead.\n *\n * The returned promises resolve once the corresponding style element\n * is loaded and ready. Those elements that are also in X return a\n * cached promise.\n *\n * The algorithm ensures that the final style elements present in the\n * document (or the passed `parent` element) are in the correct order\n * and they are included in either X or Y.\n *\n * @param X Base list of style elements.\n * @param Y List of style elements.\n * @param parent Optional parent element to append to the new style elements.\n * @return List of promises that resolve once the elements in Y are ready.\n */\nexport function updateStylesWithSCS(\n\tX: StyleElement[],\n\tY: StyleElement[],\n\tparent: Element = window.document.head\n) {\n\tif ( X.length === 0 ) {\n\t\treturn Y.map( ( element ) => {\n\t\t\tconst promise = prepareStylePromise( element );\n\t\t\tparent.appendChild( element );\n\t\t\treturn promise;\n\t\t} );\n\t}\n\n\t// Create normalized arrays for comparison.\n\tconst xNormalized = X.map( normalizeMedia );\n\tconst yNormalized = Y.map( normalizeMedia );\n\n\t// The `scs` array contains normalized elements.\n\tconst scs = shortestCommonSupersequence(\n\t\txNormalized,\n\t\tyNormalized,\n\t\tareNodesEqual\n\t);\n\tconst xLength = X.length;\n\tconst yLength = Y.length;\n\tconst promises = [];\n\tlet last = X[ xLength - 1 ];\n\tlet xIndex = 0;\n\tlet yIndex = 0;\n\n\tfor ( const scsElement of scs ) {\n\t\t// Actual elements that will end up in the DOM.\n\t\tconst xElement = X[ xIndex ];\n\t\tconst yElement = Y[ yIndex ];\n\t\t// Normalized elements for comparison.\n\t\tconst xNormEl = xNormalized[ xIndex ];\n\t\tconst yNormEl = yNormalized[ yIndex ];\n\t\tif ( xIndex < xLength && areNodesEqual( xNormEl, scsElement ) ) {\n\t\t\tif ( yIndex < yLength && areNodesEqual( yNormEl, scsElement ) ) {\n\t\t\t\tpromises.push( prepareStylePromise( xElement ) );\n\t\t\t\tyIndex++;\n\t\t\t}\n\t\t\txIndex++;\n\t\t} else {\n\t\t\tpromises.push( prepareStylePromise( yElement ) );\n\t\t\tif ( xIndex < xLength ) {\n\t\t\t\txElement.before( yElement );\n\t\t\t} else {\n\t\t\t\tlast.after( yElement );\n\t\t\t\tlast = yElement;\n\t\t\t}\n\t\t\tyIndex++;\n\t\t}\n\t}\n\n\treturn promises;\n}\n\n/**\n * Cache of promises per style elements.\n *\n * Each style element has their own associated `Promise` that resolves\n * once the element has been loaded and is ready.\n */\nconst stylePromiseCache = new WeakMap<\n\tStyleElement,\n\tPromise< StyleElement >\n>();\n\n/**\n * Prepares and returns the corresponding `Promise` for the passed style\n * element.\n *\n * It returns the cached promise if it exists. Otherwise, constructs\n * a `Promise` that resolves once the element has finished loading.\n *\n * For those elements that are not in the DOM yet, this function\n * injects a `media=\"preload\"` attribute to the passed element so the\n * style is loaded without applying any styles to the document.\n *\n * @param element Style element.\n * @return The associated `Promise` to the passed element.\n */\nconst prepareStylePromise = (\n\telement: StyleElement\n): Promise< StyleElement > => {\n\tif ( stylePromiseCache.has( element ) ) {\n\t\treturn stylePromiseCache.get( element );\n\t}\n\n\t// When the element exists in the main document and its media attribute\n\t// is not \"preload\", that means the element comes from the initial page.\n\t// The `media` attribute doesn't need to be handled in this case.\n\tif ( window.document.contains( element ) && element.media !== 'preload' ) {\n\t\tconst promise = Promise.resolve( element );\n\t\tstylePromiseCache.set( element, promise );\n\t\treturn promise;\n\t}\n\n\tif ( element.hasAttribute( 'media' ) && element.media !== 'all' ) {\n\t\telement.dataset.originalMedia = element.media;\n\t}\n\n\telement.media = 'preload';\n\n\tif ( element instanceof HTMLStyleElement ) {\n\t\tconst promise = Promise.resolve( element );\n\t\tstylePromiseCache.set( element, promise );\n\t\treturn promise;\n\t}\n\n\tconst promise = new Promise< HTMLLinkElement >( ( resolve, reject ) => {\n\t\telement.addEventListener( 'load', () => resolve( element ) );\n\t\telement.addEventListener( 'error', ( event ) => {\n\t\t\tconst { href } = event.target as HTMLLinkElement;\n\t\t\treject(\n\t\t\t\tError(\n\t\t\t\t\t`The style sheet with the following URL failed to load: ${ href }`\n\t\t\t\t)\n\t\t\t);\n\t\t} );\n\t} );\n\n\tstylePromiseCache.set( element, promise );\n\treturn promise;\n};\n\n/**\n * Cache of style promise lists per URL.\n *\n * It contains the list of style elements associated to the page with the\n * passed URL. The original order is preserved to respect the CSS cascade.\n *\n * Each included promise resolves when the associated style element is ready.\n */\nconst styleSheetCache = new Map< string, Promise< StyleElement >[] >();\n\n/**\n * Prepares all style elements contained in the passed document.\n *\n * This function calls {@link updateStylesWithSCS|`updateStylesWithSCS`}\n * to insert only the minimum amount of style elements into the DOM, so\n * those present in the passed document end up in the DOM while the order\n * is respected.\n *\n * New appended style elements contain a `media=preload` attribute to\n * make them effectively disabled until they are applied with the\n * {@link applyStyles|`applyStyles`} function.\n *\n * @param doc Document instance.\n * @param url URL for the passed document.\n * @return A list of promises for each style element in the passed document.\n */\nexport const preloadStyles = (\n\tdoc: Document,\n\turl: string\n): Promise< StyleElement >[] => {\n\tif ( ! styleSheetCache.has( url ) ) {\n\t\tconst currentStyleElements = Array.from(\n\t\t\twindow.document.querySelectorAll< StyleElement >(\n\t\t\t\t'style,link[rel=stylesheet]'\n\t\t\t)\n\t\t);\n\t\tconst newStyleElements = Array.from(\n\t\t\tdoc.querySelectorAll< StyleElement >( 'style,link[rel=stylesheet]' )\n\t\t);\n\n\t\t// Set styles in order.\n\t\tconst stylePromises = updateStylesWithSCS(\n\t\t\tcurrentStyleElements,\n\t\t\tnewStyleElements\n\t\t);\n\n\t\tstyleSheetCache.set( url, stylePromises );\n\t}\n\treturn styleSheetCache.get( url );\n};\n\n/**\n * Traverses all style elements in the DOM, enabling only those included\n * in the passed list and disabling the others.\n *\n * If the style element has the `data-original-media` attribute, the\n * original `media` value is restored.\n *\n * @param styles List of style elements to apply.\n */\nexport const applyStyles = ( styles: StyleElement[] ) => {\n\twindow.document\n\t\t.querySelectorAll( 'style,link[rel=stylesheet]' )\n\t\t.forEach( ( el: HTMLLinkElement | HTMLStyleElement ) => {\n\t\t\tif ( el.sheet ) {\n\t\t\t\tif ( styles.includes( el ) ) {\n\t\t\t\t\t// Only update mediaText when necessary.\n\t\t\t\t\tif ( el.sheet.media.mediaText === 'preload' ) {\n\t\t\t\t\t\tconst { originalMedia = 'all' } = el.dataset;\n\t\t\t\t\t\tel.sheet.media.mediaText = originalMedia;\n\t\t\t\t\t}\n\t\t\t\t\tel.sheet.disabled = false;\n\t\t\t\t} else {\n\t\t\t\t\tel.sheet.disabled = true;\n\t\t\t\t}\n\t\t\t}\n\t\t} );\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,2BAA2B,QAAQ,OAAO;AAInD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,aAAa,GAAGA,CAAEC,CAAe,EAAEC,CAAe,KACvDD,CAAC,CAACE,WAAW,CAAED,CAAE,CAAC;;AAEnB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAME,cAAc,GAAKC,OAAqB,IAAoB;EACxEA,OAAO,GAAGA,OAAO,CAACC,SAAS,CAAE,IAAK,CAAiB;EACnD,MAAMC,KAAK,GAAGF,OAAO,CAACE,KAAK;EAC3B,MAAM;IAAEC;EAAc,CAAC,GAAGH,OAAO,CAACI,OAAO;EAEzC,IAAKF,KAAK,KAAK,SAAS,EAAG;IAC1BF,OAAO,CAACE,KAAK,GAAGC,aAAa,IAAI,KAAK;IACtCH,OAAO,CAACK,eAAe,CAAE,qBAAsB,CAAC;EACjD,CAAC,MAAM,IAAK,CAAEL,OAAO,CAACE,KAAK,EAAG;IAC7BF,OAAO,CAACE,KAAK,GAAG,KAAK;EACtB;EACA,OAAOF,OAAO;AACf,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASM,mBAAmBA,CAClCC,CAAiB,EACjBC,CAAiB,EACjBC,MAAe,GAAGC,MAAM,CAACC,QAAQ,CAACC,IAAI,EACrC;EACD,IAAKL,CAAC,CAACM,MAAM,KAAK,CAAC,EAAG;IACrB,OAA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AC;EAC1C;EACA,OAAOV,eAAe,CAACb,GAAG,CAAEiB,GAAI,CAAC;AAClC,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMO,WAAW,GAAKC,MAAsB,IAAM;EACxDrD,MAAM,CAACC,QAAQ,CACbgD,gBAAgB,CAAE,4BAA6B,CAAC,CAChDK,OAAO,CAAIC,EAAsC,IAAM;IACvD,IAAKA,EAAE,CAACC,KAAK,EAAG;MACf,IAAKH,MAAM,CAACI,QAAQ,CAAEF,EAAG,CAAC,EAAG;QAC5B;QACA,IAAKA,EAAE,CAACC,KAAK,CAAChE,KAAK,CAACkE,SAAS,KAAK,SAAS,EAAG;UAC7C,MAAM;YAAEjE,aAAa,GAAG;UAAM,CAAC,GAAG8D,EAAE,CAAC7D,OAAO;UAC5C6D,EAAE,CAACC,KAAK,CAAChE,KAAK,CAACkE,SAAS,GAAGjE,aAAa;QACzC;QACA8D,EAAE,CAACC,KAAK,CAACG,QAAQ,GAAG,KAAK;MAC1B,CAAC,MAAM;QACNJ,EAAE,CAACC,KAAK,CAACG,QAAQ,GAAG,IAAI;MACzB;IACD;EACD,CAAE,CAAC;AACL,CAAC","ignoreList":[]}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/assets/styles.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport { shortestCommonSupersequence } from './scs';\n\nexport type StyleElement = HTMLLinkElement | HTMLStyleElement;\n\n/**\n * Compares the passed style or link elements to check if they can be\n * considered equal.\n *\n * @param a `<style>` or `<link>` element.\n * @param b `<style>` or `<link>` element.\n * @return Whether they are considered equal.\n */\nconst areNodesEqual = ( a: StyleElement, b: StyleElement ): boolean =>\n\ta.isEqualNode( b );\n\n/**\n * Normalizes the passed style or link element, reverting the changes\n * made by {@link prepareStylePromise|`prepareStylePromise`} to the\n * `data-original-media` and `media`.\n *\n * @example\n * The following elements should be normalized to the same element:\n * ```html\n * <link rel=\"stylesheet\" src=\"./assets/styles.css\">\n * <link rel=\"stylesheet\" src=\"./assets/styles.css\" media=\"all\">\n * <link rel=\"stylesheet\" src=\"./assets/styles.css\" media=\"preload\">\n * <link rel=\"stylesheet\" src=\"./assets/styles.css\" media=\"preload\" data-original-media=\"all\">\n * ```\n *\n * @param element `<style>` or `<link>` element.\n * @return Normalized node.\n */\nexport const normalizeMedia = ( element: StyleElement ): StyleElement => {\n\telement = element.cloneNode( true ) as StyleElement;\n\tconst media = element.media;\n\tconst { originalMedia } = element.dataset;\n\n\tif ( media === 'preload' ) {\n\t\telement.media = originalMedia || 'all';\n\t\telement.removeAttribute( 'data-original-media' );\n\t} else if ( ! element.media ) {\n\t\telement.media = 'all';\n\t}\n\treturn element;\n};\n\n/**\n * Adds the minimum style elements from Y around those in X using a\n * shortest common supersequence algorithm, returning a list of\n * promises for all the elements in Y.\n *\n * If X is empty, it appends all elements in Y to the passed parent\n * element or to `document.head` instead.\n *\n * The returned promises resolve once the corresponding style element\n * is loaded and ready. Those elements that are also in X return a\n * cached promise.\n *\n * The algorithm ensures that the final style elements present in the\n * document (or the passed `parent` element) are in the correct order\n * and they are included in either X or Y.\n *\n * @param X Base list of style elements.\n * @param Y List of style elements.\n * @param parent Optional parent element to append to the new style elements.\n * @return List of promises that resolve once the elements in Y are ready.\n */\nexport function updateStylesWithSCS(\n\tX: StyleElement[],\n\tY: StyleElement[],\n\tparent: Element = window.document.head\n) {\n\tif ( X.length === 0 ) {\n\t\treturn Y.map( ( element ) => {\n\t\t\tconst promise = prepareStylePromise( element );\n\t\t\tparent.appendChild( element );\n\t\t\treturn promise;\n\t\t} );\n\t}\n\n\t// Create normalized arrays for comparison.\n\tconst xNormalized = X.map( normalizeMedia );\n\tconst yNormalized = Y.map( normalizeMedia );\n\n\t// The `scs` array contains normalized elements.\n\tconst scs = shortestCommonSupersequence(\n\t\txNormalized,\n\t\tyNormalized,\n\t\tareNodesEqual\n\t);\n\tconst xLength = X.length;\n\tconst yLength = Y.length;\n\tconst promises = [];\n\tlet last = X[ xLength - 1 ];\n\tlet xIndex = 0;\n\tlet yIndex = 0;\n\n\tfor ( const scsElement of scs ) {\n\t\t// Actual elements that will end up in the DOM.\n\t\tconst xElement = X[ xIndex ];\n\t\tconst yElement = Y[ yIndex ];\n\t\t// Normalized elements for comparison.\n\t\tconst xNormEl = xNormalized[ xIndex ];\n\t\tconst yNormEl = yNormalized[ yIndex ];\n\t\tif ( xIndex < xLength && areNodesEqual( xNormEl, scsElement ) ) {\n\t\t\tif ( yIndex < yLength && areNodesEqual( yNormEl, scsElement ) ) {\n\t\t\t\tpromises.push( prepareStylePromise( xElement ) );\n\t\t\t\tyIndex++;\n\t\t\t}\n\t\t\txIndex++;\n\t\t} else {\n\t\t\tpromises.push( prepareStylePromise( yElement ) );\n\t\t\tif ( xIndex < xLength ) {\n\t\t\t\txElement.before( yElement );\n\t\t\t} else {\n\t\t\t\tlast.after( yElement );\n\t\t\t\tlast = yElement;\n\t\t\t}\n\t\t\tyIndex++;\n\t\t}\n\t}\n\n\treturn promises;\n}\n\n/**\n * Cache of promises per style elements.\n *\n * Each style element has their own associated `Promise` that resolves\n * once the element has been loaded and is ready.\n */\nconst stylePromiseCache = new WeakMap<\n\tStyleElement,\n\tPromise< StyleElement >\n>();\n\n/**\n * Prepares and returns the corresponding `Promise` for the passed style\n * element.\n *\n * It returns the cached promise if it exists. Otherwise, constructs\n * a `Promise` that resolves once the element has finished loading.\n *\n * For those elements that are not in the DOM yet, this function\n * injects a `media=\"preload\"` attribute to the passed element so the\n * style is loaded without applying any styles to the document.\n *\n * @param element Style element.\n * @return The associated `Promise` to the passed element.\n */\nconst prepareStylePromise = (\n\telement: StyleElement\n): Promise< StyleElement > => {\n\tif ( stylePromiseCache.has( element ) ) {\n\t\treturn stylePromiseCache.get( element );\n\t}\n\n\t// When the element exists in the main document and its media attribute\n\t// is not \"preload\", that means the element comes from the initial page.\n\t// The `media` attribute doesn't need to be handled in this case.\n\tif ( window.document.contains( element ) && element.media !== 'preload' ) {\n\t\tconst promise = Promise.resolve( element );\n\t\tstylePromiseCache.set( element, promise );\n\t\treturn promise;\n\t}\n\n\tif ( element.hasAttribute( 'media' ) && element.media !== 'all' ) {\n\t\telement.dataset.originalMedia = element.media;\n\t}\n\n\telement.media = 'preload';\n\n\tif ( element instanceof HTMLStyleElement ) {\n\t\tconst promise = Promise.resolve( element );\n\t\tstylePromiseCache.set( element, promise );\n\t\treturn promise;\n\t}\n\n\tconst promise = new Promise< HTMLLinkElement >( ( resolve, reject ) => {\n\t\telement.addEventListener( 'load', () => resolve( element ) );\n\t\telement.addEventListener( 'error', ( event ) => {\n\t\t\tconst { href } = event.target as HTMLLinkElement;\n\t\t\treject(\n\t\t\t\tError(\n\t\t\t\t\t`The style sheet with the following URL failed to load: ${ href }`\n\t\t\t\t)\n\t\t\t);\n\t\t} );\n\t} );\n\n\tstylePromiseCache.set( element, promise );\n\treturn promise;\n};\n\n/**\n * Cache of style promise lists per URL.\n *\n * It contains the list of style elements associated to the page with the\n * passed URL. The original order is preserved to respect the CSS cascade.\n *\n * Each included promise resolves when the associated style element is ready.\n */\nconst styleSheetCache = new Map< string, Promise< StyleElement >[] >();\n\n/**\n * Prepares all style elements contained in the passed document.\n *\n * This function calls {@link updateStylesWithSCS|`updateStylesWithSCS`}\n * to insert only the minimum amount of style elements into the DOM, so\n * those present in the passed document end up in the DOM while the order\n * is respected.\n *\n * New appended style elements contain a `media=preload` attribute to\n * make them effectively disabled until they are applied with the\n * {@link applyStyles|`applyStyles`} function.\n *\n * @param doc Document instance.\n * @param url URL for the passed document.\n * @return A list of promises for each style element in the passed document.\n */\nexport const preloadStyles = (\n\tdoc: Document,\n\turl: string\n): Promise< StyleElement >[] => {\n\tif ( ! styleSheetCache.has( url ) ) {\n\t\tconst currentStyleElements = Array.from(\n\t\t\twindow.document.querySelectorAll< StyleElement >(\n\t\t\t\t'style,link[rel=stylesheet]'\n\t\t\t)\n\t\t);\n\t\tconst newStyleElements = Array.from(\n\t\t\tdoc.querySelectorAll< StyleElement >( 'style,link[rel=stylesheet]' )\n\t\t);\n\n\t\t// Set styles in order.\n\t\tconst stylePromises = updateStylesWithSCS(\n\t\t\tcurrentStyleElements,\n\t\t\tnewStyleElements\n\t\t);\n\n\t\tstyleSheetCache.set( url, stylePromises );\n\t}\n\treturn styleSheetCache.get( url );\n};\n\n/**\n * Traverses all style elements in the DOM, enabling only those included\n * in the passed list and disabling the others.\n *\n * If the style element has the `data-original-media` attribute, the\n * original `media` value is restored.\n *\n * @param styles List of style elements to apply.\n */\nexport const applyStyles = ( styles: StyleElement[] ) => {\n\twindow.document\n\t\t.querySelectorAll( 'style,link[rel=stylesheet]' )\n\t\t.forEach( ( el: HTMLLinkElement | HTMLStyleElement ) => {\n\t\t\tif ( el.sheet ) {\n\t\t\t\tif ( styles.includes( el ) ) {\n\t\t\t\t\t// Only update mediaText when necessary.\n\t\t\t\t\tif ( el.sheet.media.mediaText === 'preload' ) {\n\t\t\t\t\t\tconst { originalMedia = 'all' } = el.dataset;\n\t\t\t\t\t\tel.sheet.media.mediaText = originalMedia;\n\t\t\t\t\t}\n\t\t\t\t\tel.sheet.disabled = false;\n\t\t\t\t} else {\n\t\t\t\t\tel.sheet.disabled = true;\n\t\t\t\t}\n\t\t\t}\n\t\t} );\n};\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,mCAAmC;AAY5C,MAAM,gBAAgB,CAAE,GAAiB,MACxC,EAAE,YAAa,CAAE;AAmBX,MAAM,iBAAiB,CAAE,YAAyC;AACxE,YAAU,QAAQ,UAAW,IAAK;AAClC,QAAM,QAAQ,QAAQ;AACtB,QAAM,EAAE,cAAc,IAAI,QAAQ;AAElC,MAAK,UAAU,WAAY;AAC1B,YAAQ,QAAQ,iBAAiB;AACjC,YAAQ,gBAAiB,qBAAsB;AAAA,EAChD,WAAY,CAAE,QAAQ,OAAQ;AAC7B,YAAQ,QAAQ;AAAA,EACjB;AACA,SAAO;AACR;AAuB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K,OAAO,SAAU,EAAG,GAAI;AAE5B,YAAK,GAAG,MAAM,MAAM,cAAc,WAAY;AAC7C,gBAAM,EAAE,gBAAgB,MAAM,IAAI,GAAG;AACrC,aAAG,MAAM,MAAM,YAAY;AAAA,QAC5B;AACA,WAAG,MAAM,WAAW;AAAA,MACrB,OAAO;AACN,WAAG,MAAM,WAAW;AAAA,MACrB;AAAA,IACD;AAAA,EACD,CAAE;AACJ;",
|
|
6
|
+
"names": ["promise"]
|
|
7
|
+
}
|
|
@@ -1,39 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const isValidEvent = event => event && event.button === 0 &&
|
|
7
|
-
// Left clicks only.
|
|
8
|
-
!event.metaKey &&
|
|
9
|
-
// Open in new tab (Mac).
|
|
10
|
-
!event.ctrlKey &&
|
|
11
|
-
// Open in new tab (Windows).
|
|
12
|
-
!event.altKey &&
|
|
13
|
-
// Download.
|
|
1
|
+
const isValidLink = (ref) => ref && ref instanceof window.HTMLAnchorElement && ref.href && (!ref.target || ref.target === "_self") && ref.origin === window.location.origin && !ref.pathname.startsWith("/wp-admin") && !ref.pathname.startsWith("/wp-login.php") && !ref.getAttribute("href").startsWith("#") && !new URL(ref.href).searchParams.has("_wpnonce");
|
|
2
|
+
const isValidEvent = (event) => event && event.button === 0 && // Left clicks only.
|
|
3
|
+
!event.metaKey && // Open in new tab (Mac).
|
|
4
|
+
!event.ctrlKey && // Open in new tab (Windows).
|
|
5
|
+
!event.altKey && // Download.
|
|
14
6
|
!event.shiftKey && !event.defaultPrevented;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
document.addEventListener('click', async event => {
|
|
18
|
-
const ref = event.target.closest('a');
|
|
7
|
+
document.addEventListener("click", async (event) => {
|
|
8
|
+
const ref = event.target.closest("a");
|
|
19
9
|
if (isValidLink(ref) && isValidEvent(event)) {
|
|
20
10
|
event.preventDefault();
|
|
21
|
-
const {
|
|
22
|
-
actions
|
|
23
|
-
} = await import('@wordpress/interactivity-router');
|
|
11
|
+
const { actions } = await import("@wordpress/interactivity-router");
|
|
24
12
|
actions.navigate(ref.href);
|
|
25
13
|
}
|
|
26
14
|
});
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
actions
|
|
34
|
-
|
|
35
|
-
|
|
15
|
+
document.addEventListener(
|
|
16
|
+
"mouseenter",
|
|
17
|
+
async (event) => {
|
|
18
|
+
if (event.target?.nodeName === "A") {
|
|
19
|
+
const ref = event.target.closest("a");
|
|
20
|
+
if (isValidLink(ref) && isValidEvent(event)) {
|
|
21
|
+
const { actions } = await import("@wordpress/interactivity-router");
|
|
22
|
+
actions.prefetch(ref.href);
|
|
23
|
+
}
|
|
36
24
|
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
},
|
|
26
|
+
true
|
|
27
|
+
);
|
|
28
|
+
//# sourceMappingURL=full-page.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/full-page.ts"],
|
|
4
|
+
"sourcesContent": ["// Check if the link is valid for client-side navigation.\nconst isValidLink = ( ref: HTMLAnchorElement ) =>\n\tref &&\n\tref instanceof window.HTMLAnchorElement &&\n\tref.href &&\n\t( ! ref.target || ref.target === '_self' ) &&\n\tref.origin === window.location.origin &&\n\t! ref.pathname.startsWith( '/wp-admin' ) &&\n\t! ref.pathname.startsWith( '/wp-login.php' ) &&\n\t! ref.getAttribute( 'href' ).startsWith( '#' ) &&\n\t! new URL( ref.href ).searchParams.has( '_wpnonce' );\n\n// Check if the event is valid for client-side navigation.\nconst isValidEvent = ( event: MouseEvent ) =>\n\tevent &&\n\tevent.button === 0 && // Left clicks only.\n\t! event.metaKey && // Open in new tab (Mac).\n\t! event.ctrlKey && // Open in new tab (Windows).\n\t! event.altKey && // Download.\n\t! event.shiftKey &&\n\t! event.defaultPrevented;\n\n// Navigate on click.\ndocument.addEventListener( 'click', async ( event ) => {\n\tconst ref = ( event.target as Element ).closest( 'a' );\n\tif ( isValidLink( ref ) && isValidEvent( event ) ) {\n\t\tevent.preventDefault();\n\t\tconst { actions } = await import( '@wordpress/interactivity-router' );\n\t\tactions.navigate( ref.href );\n\t}\n} );\n// Prefetch on hover.\ndocument.addEventListener(\n\t'mouseenter',\n\tasync ( event ) => {\n\t\tif ( ( event.target as Element )?.nodeName === 'A' ) {\n\t\t\tconst ref = ( event.target as Element ).closest( 'a' );\n\t\t\tif ( isValidLink( ref ) && isValidEvent( event ) ) {\n\t\t\t\tconst { actions } = await import(\n\t\t\t\t\t'@wordpress/interactivity-router'\n\t\t\t\t);\n\t\t\t\tactions.prefetch( ref.href );\n\t\t\t}\n\t\t}\n\t},\n\ttrue\n);\n"],
|
|
5
|
+
"mappings": "AACA,MAAM,cAAc,CAAE,QACrB,OACA,eAAe,OAAO,qBACtB,IAAI,SACF,CAAE,IAAI,UAAU,IAAI,WAAW,YACjC,IAAI,WAAW,OAAO,SAAS,UAC/B,CAAE,IAAI,SAAS,WAAY,WAAY,KACvC,CAAE,IAAI,SAAS,WAAY,eAAgB,KAC3C,CAAE,IAAI,aAAc,MAAO,EAAE,WAAY,GAAI,KAC7C,CAAE,IAAI,IAAK,IAAI,IAAK,EAAE,aAAa,IAAK,UAAW;AAGpD,MAAM,eAAe,CAAE,UACtB,SACA,MAAM,WAAW;AACjB,CAAE,MAAM;AACR,CAAE,MAAM;AACR,CAAE,MAAM;AACR,CAAE,MAAM,YACR,CAAE,MAAM;AAGT,SAAS,iBAAkB,SAAS,OAAQ,UAAW;AACtD,QAAM,MAAQ,MAAM,OAAoB,QAAS,GAAI;AACrD,MAAK,YAAa,GAAI,KAAK,aAAc,KAAM,GAAI;AAClD,UAAM,eAAe;AACrB,UAAM,EAAE,QAAQ,IAAI,MAAM,OAAQ,iCAAkC;AACpE,YAAQ,SAAU,IAAI,IAAK;AAAA,EAC5B;AACD,CAAE;AAEF,SAAS;AAAA,EACR;AAAA,EACA,OAAQ,UAAW;AAClB,QAAO,MAAM,QAAqB,aAAa,KAAM;AACpD,YAAM,MAAQ,MAAM,OAAoB,QAAS,GAAI;AACrD,UAAK,YAAa,GAAI,KAAK,aAAc,KAAM,GAAI;AAClD,cAAM,EAAE,QAAQ,IAAI,MAAM,OACzB,iCACD;AACA,gBAAQ,SAAU,IAAI,IAAK;AAAA,MAC5B;AAAA,IACD;AAAA,EACD;AAAA,EACA;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|