@wordpress/interactivity-router 2.32.0 → 2.32.1-next.47f435fc9.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
package/build/assets/styles.js
CHANGED
|
@@ -1,94 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var styles_exports = {};
|
|
19
|
+
__export(styles_exports, {
|
|
20
|
+
applyStyles: () => applyStyles,
|
|
21
|
+
normalizeMedia: () => normalizeMedia,
|
|
22
|
+
preloadStyles: () => preloadStyles,
|
|
23
|
+
updateStylesWithSCS: () => updateStylesWithSCS
|
|
5
24
|
});
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
*/
|
|
25
|
+
module.exports = __toCommonJS(styles_exports);
|
|
26
|
+
var import_scs = require("./scs");
|
|
21
27
|
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 => {
|
|
28
|
+
const normalizeMedia = (element) => {
|
|
41
29
|
element = element.cloneNode(true);
|
|
42
30
|
const media = element.media;
|
|
43
|
-
const {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
element.media = originalMedia || 'all';
|
|
48
|
-
element.removeAttribute('data-original-media');
|
|
31
|
+
const { originalMedia } = element.dataset;
|
|
32
|
+
if (media === "preload") {
|
|
33
|
+
element.media = originalMedia || "all";
|
|
34
|
+
element.removeAttribute("data-original-media");
|
|
49
35
|
} else if (!element.media) {
|
|
50
|
-
element.media =
|
|
36
|
+
element.media = "all";
|
|
51
37
|
}
|
|
52
38
|
return element;
|
|
53
39
|
};
|
|
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
40
|
function updateStylesWithSCS(X, Y, parent = window.document.head) {
|
|
78
41
|
if (X.length === 0) {
|
|
79
|
-
return Y.map(element => {
|
|
42
|
+
return Y.map((element) => {
|
|
80
43
|
const promise = prepareStylePromise(element);
|
|
81
44
|
parent.appendChild(element);
|
|
82
45
|
return promise;
|
|
83
46
|
});
|
|
84
47
|
}
|
|
85
|
-
|
|
86
|
-
// Create normalized arrays for comparison.
|
|
87
48
|
const xNormalized = X.map(normalizeMedia);
|
|
88
49
|
const yNormalized = Y.map(normalizeMedia);
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
50
|
+
const scs = (0, import_scs.shortestCommonSupersequence)(
|
|
51
|
+
xNormalized,
|
|
52
|
+
yNormalized,
|
|
53
|
+
areNodesEqual
|
|
54
|
+
);
|
|
92
55
|
const xLength = X.length;
|
|
93
56
|
const yLength = Y.length;
|
|
94
57
|
const promises = [];
|
|
@@ -96,10 +59,8 @@ function updateStylesWithSCS(X, Y, parent = window.document.head) {
|
|
|
96
59
|
let xIndex = 0;
|
|
97
60
|
let yIndex = 0;
|
|
98
61
|
for (const scsElement of scs) {
|
|
99
|
-
// Actual elements that will end up in the DOM.
|
|
100
62
|
const xElement = X[xIndex];
|
|
101
63
|
const yElement = Y[yIndex];
|
|
102
|
-
// Normalized elements for comparison.
|
|
103
64
|
const xNormEl = xNormalized[xIndex];
|
|
104
65
|
const yNormEl = yNormalized[yIndex];
|
|
105
66
|
if (xIndex < xLength && areNodesEqual(xNormEl, scsElement)) {
|
|
@@ -121,121 +82,64 @@ function updateStylesWithSCS(X, Y, parent = window.document.head) {
|
|
|
121
82
|
}
|
|
122
83
|
return promises;
|
|
123
84
|
}
|
|
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 => {
|
|
85
|
+
const stylePromiseCache = /* @__PURE__ */ new WeakMap();
|
|
86
|
+
const prepareStylePromise = (element) => {
|
|
148
87
|
if (stylePromiseCache.has(element)) {
|
|
149
88
|
return stylePromiseCache.get(element);
|
|
150
89
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
if (window.document.contains(element) && element.media !== 'preload') {
|
|
156
|
-
const promise = Promise.resolve(element);
|
|
157
|
-
stylePromiseCache.set(element, promise);
|
|
158
|
-
return promise;
|
|
90
|
+
if (window.document.contains(element) && element.media !== "preload") {
|
|
91
|
+
const promise2 = Promise.resolve(element);
|
|
92
|
+
stylePromiseCache.set(element, promise2);
|
|
93
|
+
return promise2;
|
|
159
94
|
}
|
|
160
|
-
if (element.hasAttribute(
|
|
95
|
+
if (element.hasAttribute("media") && element.media !== "all") {
|
|
161
96
|
element.dataset.originalMedia = element.media;
|
|
162
97
|
}
|
|
163
|
-
element.media =
|
|
98
|
+
element.media = "preload";
|
|
164
99
|
if (element instanceof HTMLStyleElement) {
|
|
165
|
-
const
|
|
166
|
-
stylePromiseCache.set(element,
|
|
167
|
-
return
|
|
100
|
+
const promise2 = Promise.resolve(element);
|
|
101
|
+
stylePromiseCache.set(element, promise2);
|
|
102
|
+
return promise2;
|
|
168
103
|
}
|
|
169
104
|
const promise = new Promise((resolve, reject) => {
|
|
170
|
-
element.addEventListener(
|
|
171
|
-
element.addEventListener(
|
|
172
|
-
const {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
105
|
+
element.addEventListener("load", () => resolve(element));
|
|
106
|
+
element.addEventListener("error", (event) => {
|
|
107
|
+
const { href } = event.target;
|
|
108
|
+
reject(
|
|
109
|
+
Error(
|
|
110
|
+
`The style sheet with the following URL failed to load: ${href}`
|
|
111
|
+
)
|
|
112
|
+
);
|
|
176
113
|
});
|
|
177
114
|
});
|
|
178
115
|
stylePromiseCache.set(element, promise);
|
|
179
116
|
return promise;
|
|
180
117
|
};
|
|
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
|
-
*/
|
|
118
|
+
const styleSheetCache = /* @__PURE__ */ new Map();
|
|
208
119
|
const preloadStyles = (doc, url) => {
|
|
209
120
|
if (!styleSheetCache.has(url)) {
|
|
210
|
-
const currentStyleElements = Array.from(
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
121
|
+
const currentStyleElements = Array.from(
|
|
122
|
+
window.document.querySelectorAll(
|
|
123
|
+
"style,link[rel=stylesheet]"
|
|
124
|
+
)
|
|
125
|
+
);
|
|
126
|
+
const newStyleElements = Array.from(
|
|
127
|
+
doc.querySelectorAll("style,link[rel=stylesheet]")
|
|
128
|
+
);
|
|
129
|
+
const stylePromises = updateStylesWithSCS(
|
|
130
|
+
currentStyleElements,
|
|
131
|
+
newStyleElements
|
|
132
|
+
);
|
|
215
133
|
styleSheetCache.set(url, stylePromises);
|
|
216
134
|
}
|
|
217
135
|
return styleSheetCache.get(url);
|
|
218
136
|
};
|
|
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 => {
|
|
137
|
+
const applyStyles = (styles) => {
|
|
138
|
+
window.document.querySelectorAll("style,link[rel=stylesheet]").forEach((el) => {
|
|
232
139
|
if (el.sheet) {
|
|
233
140
|
if (styles.includes(el)) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const {
|
|
237
|
-
originalMedia = 'all'
|
|
238
|
-
} = el.dataset;
|
|
141
|
+
if (el.sheet.media.mediaText === "preload") {
|
|
142
|
+
const { originalMedia = "all" } = el.dataset;
|
|
239
143
|
el.sheet.media.mediaText = originalMedia;
|
|
240
144
|
}
|
|
241
145
|
el.sheet.disabled = false;
|
|
@@ -245,5 +149,11 @@ const applyStyles = styles => {
|
|
|
245
149
|
}
|
|
246
150
|
});
|
|
247
151
|
};
|
|
248
|
-
|
|
249
|
-
|
|
152
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
153
|
+
0 && (module.exports = {
|
|
154
|
+
applyStyles,
|
|
155
|
+
normalizeMedia,
|
|
156
|
+
preloadStyles,
|
|
157
|
+
updateStylesWithSCS
|
|
158
|
+
});
|
|
159
|
+
//# sourceMappingURL=styles.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"names":["_scs","require","areNodesEqual","a","b","isEqualNode","normalizeMedia","element","cloneNode","media","originalMedia","dataset","removeAttribute","exports","updateStylesWithSCS","X","Y","parent","window","document","head","length","map","promise","prepareStylePromise","appendChild","xNormalized","yNormalized","scs","shortestCommonSupersequence","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":";;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AAKC,MAAsB,IAAM;EACxDtD,MAAM,CAACC,QAAQ,CACbiD,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,CAAClE,KAAK,CAACoE,SAAS,KAAK,SAAS,EAAG;UAC7C,MAAM;YAAEnE,aAAa,GAAG;UAAM,CAAC,GAAGgE,EAAE,CAAC/D,OAAO;UAC5C+D,EAAE,CAACC,KAAK,CAAClE,KAAK,CAACoE,SAAS,GAAGnE,aAAa;QACzC;QACAgE,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;AAACjE,OAAA,CAAA0D,WAAA,GAAAA,WAAA","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": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA4C;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;AAuBO,SAAS,oBACf,GACA,GACA,SAAkB,OAAO,SA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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
|
+
}
|
package/build/full-page.js
CHANGED
|
@@ -1,43 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
//
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __copyProps = (to, from, except, desc) => {
|
|
8
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
9
|
+
for (let key of __getOwnPropNames(from))
|
|
10
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
11
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
12
|
+
}
|
|
13
|
+
return to;
|
|
14
|
+
};
|
|
15
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
16
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
17
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
18
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
19
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
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");
|
|
24
|
+
const isValidEvent = (event) => event && event.button === 0 && // Left clicks only.
|
|
25
|
+
!event.metaKey && // Open in new tab (Mac).
|
|
26
|
+
!event.ctrlKey && // Open in new tab (Windows).
|
|
27
|
+
!event.altKey && // Download.
|
|
18
28
|
!event.shiftKey && !event.defaultPrevented;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
document.addEventListener('click', async event => {
|
|
22
|
-
const ref = event.target.closest('a');
|
|
29
|
+
document.addEventListener("click", async (event) => {
|
|
30
|
+
const ref = event.target.closest("a");
|
|
23
31
|
if (isValidLink(ref) && isValidEvent(event)) {
|
|
24
32
|
event.preventDefault();
|
|
25
|
-
const {
|
|
26
|
-
actions
|
|
27
|
-
} = await Promise.resolve().then(() => _interopRequireWildcard(require('@wordpress/interactivity-router')));
|
|
33
|
+
const { actions } = await import("@wordpress/interactivity-router");
|
|
28
34
|
actions.navigate(ref.href);
|
|
29
35
|
}
|
|
30
36
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
actions
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
document.addEventListener(
|
|
38
|
+
"mouseenter",
|
|
39
|
+
async (event) => {
|
|
40
|
+
if (event.target?.nodeName === "A") {
|
|
41
|
+
const ref = event.target.closest("a");
|
|
42
|
+
if (isValidLink(ref) && isValidEvent(event)) {
|
|
43
|
+
const { actions } = await import("@wordpress/interactivity-router");
|
|
44
|
+
actions.prefetch(ref.href);
|
|
45
|
+
}
|
|
40
46
|
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
},
|
|
48
|
+
true
|
|
49
|
+
);
|
|
50
|
+
//# sourceMappingURL=full-page.js.map
|
package/build/full-page.js.map
CHANGED
|
@@ -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\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