@schukai/monster 4.77.1 → 4.77.2
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 +11 -0
- package/package.json +1 -1
- package/source/dom/util.mjs +188 -188
- package/test/cases/components/form/button-bar.mjs +1 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.4","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"4.77.
|
|
1
|
+
{"author":"Volker Schukai","dependencies":{"@floating-ui/dom":"^1.7.4","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"4.77.2"}
|
package/source/dom/util.mjs
CHANGED
|
@@ -16,14 +16,14 @@ import { getGlobal } from "../types/global.mjs";
|
|
|
16
16
|
import { validateString } from "../types/validate.mjs";
|
|
17
17
|
|
|
18
18
|
export {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
getDocument,
|
|
20
|
+
getWindow,
|
|
21
|
+
getDocumentFragmentFromString,
|
|
22
|
+
findElementWithIdUpwards,
|
|
23
|
+
getContainingDocument,
|
|
24
|
+
getRegisteredCustomElements,
|
|
25
|
+
findElementWithSelectorUpwards,
|
|
26
|
+
waitForCustomElement,
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
/**
|
|
@@ -63,12 +63,12 @@ export {
|
|
|
63
63
|
* @throws {Error} not supported environment
|
|
64
64
|
*/
|
|
65
65
|
function getDocument() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
const document = getGlobal()?.["document"];
|
|
67
|
+
if (typeof document !== "object") {
|
|
68
|
+
throw new Error("not supported environment");
|
|
69
|
+
}
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
return document;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
/**
|
|
@@ -110,12 +110,12 @@ function getDocument() {
|
|
|
110
110
|
* @throws {Error} not supported environment
|
|
111
111
|
*/
|
|
112
112
|
function getWindow() {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
const window = getGlobal()?.["window"];
|
|
114
|
+
if (typeof window !== "object") {
|
|
115
|
+
throw new Error("not supported environment");
|
|
116
|
+
}
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
return window;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
/**
|
|
@@ -156,13 +156,13 @@ function getWindow() {
|
|
|
156
156
|
* @throws {TypeError} value is not a string
|
|
157
157
|
*/
|
|
158
158
|
function getDocumentFragmentFromString(html) {
|
|
159
|
-
|
|
159
|
+
validateString(html);
|
|
160
160
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
161
|
+
const document = getDocument();
|
|
162
|
+
const template = document.createElement("template");
|
|
163
|
+
template.innerHTML = html;
|
|
164
164
|
|
|
165
|
-
|
|
165
|
+
return template.content;
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
/**
|
|
@@ -177,39 +177,39 @@ function getDocumentFragmentFromString(html) {
|
|
|
177
177
|
* @copyright Volker Schukai
|
|
178
178
|
*/
|
|
179
179
|
function findElementWithIdUpwards(element, targetId) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
180
|
+
if (!element) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Check if the current element has the target ID
|
|
185
|
+
if (element.id === targetId) {
|
|
186
|
+
return element;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Search within the current element's shadow root, if it exists
|
|
190
|
+
if (element.shadowRoot) {
|
|
191
|
+
const target = element.shadowRoot.getElementById(targetId);
|
|
192
|
+
if (target) {
|
|
193
|
+
return target;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// If the current element is the document.documentElement, search within the main document
|
|
198
|
+
if (element === document.documentElement || element === document) {
|
|
199
|
+
const target = document.getElementById(targetId);
|
|
200
|
+
if (target) {
|
|
201
|
+
return target;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// If the current element is inside a shadow root, search its host's ancestors
|
|
206
|
+
const rootNode = element.getRootNode();
|
|
207
|
+
if (rootNode && rootNode instanceof ShadowRoot) {
|
|
208
|
+
return findElementWithIdUpwards(rootNode.host, targetId);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Otherwise, search the current element's parent
|
|
212
|
+
return findElementWithIdUpwards(element.parentElement, targetId);
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
/**
|
|
@@ -223,33 +223,33 @@ function findElementWithIdUpwards(element, targetId) {
|
|
|
223
223
|
* @since 3.55.0
|
|
224
224
|
*/
|
|
225
225
|
function findElementWithSelectorUpwards(element, selector) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
226
|
+
if (!element || !selector) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Search within the current element's shadow root, if it exists
|
|
231
|
+
if (element.shadowRoot) {
|
|
232
|
+
const target = element.shadowRoot.querySelector(selector);
|
|
233
|
+
if (target) {
|
|
234
|
+
return target;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (element === document.documentElement) {
|
|
239
|
+
const target = document.querySelector(selector);
|
|
240
|
+
if (target) {
|
|
241
|
+
return target;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// If the current element is inside a shadow root, search its host's ancestors
|
|
246
|
+
const rootNode = element.getRootNode();
|
|
247
|
+
if (rootNode && rootNode instanceof ShadowRoot) {
|
|
248
|
+
return findElementWithSelectorUpwards(rootNode.host, selector);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Otherwise, search the current element's parent
|
|
252
|
+
return findElementWithSelectorUpwards(element.parentElement, selector);
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
/**
|
|
@@ -258,34 +258,34 @@ function findElementWithSelectorUpwards(element, selector) {
|
|
|
258
258
|
* @return {HTMLElement|null}
|
|
259
259
|
*/
|
|
260
260
|
function traverseShadowRoots(element) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
261
|
+
let currentRoot = element.shadowRoot;
|
|
262
|
+
let currentParent = element.parentNode;
|
|
263
|
+
|
|
264
|
+
while (
|
|
265
|
+
currentParent &&
|
|
266
|
+
currentParent.nodeType !== Node.DOCUMENT_NODE &&
|
|
267
|
+
currentParent.nodeType !== Node.DOCUMENT_FRAGMENT_NODE
|
|
268
|
+
) {
|
|
269
|
+
if (currentRoot && currentRoot.parentNode) {
|
|
270
|
+
currentParent = currentRoot.parentNode;
|
|
271
|
+
currentRoot = currentParent.shadowRoot;
|
|
272
|
+
} else if (currentParent.parentNode) {
|
|
273
|
+
currentParent = currentParent.parentNode;
|
|
274
|
+
currentRoot = null;
|
|
275
|
+
} else if (
|
|
276
|
+
currentRoot &&
|
|
277
|
+
currentRoot.host &&
|
|
278
|
+
currentRoot.host.nodeType === Node.DOCUMENT_NODE
|
|
279
|
+
) {
|
|
280
|
+
currentParent = currentRoot.host;
|
|
281
|
+
currentRoot = null;
|
|
282
|
+
} else {
|
|
283
|
+
currentParent = null;
|
|
284
|
+
currentRoot = null;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return currentParent;
|
|
289
289
|
}
|
|
290
290
|
|
|
291
291
|
/**
|
|
@@ -297,17 +297,17 @@ function traverseShadowRoots(element) {
|
|
|
297
297
|
* @since 3.36.0
|
|
298
298
|
*/
|
|
299
299
|
function getContainingDocument(element) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
300
|
+
if (
|
|
301
|
+
!element ||
|
|
302
|
+
!(
|
|
303
|
+
element instanceof HTMLElement ||
|
|
304
|
+
element instanceof element.ownerDocument.defaultView.HTMLElement
|
|
305
|
+
)
|
|
306
|
+
) {
|
|
307
|
+
throw new Error("Invalid argument. Expected an HTMLElement.");
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return traverseShadowRoots(element) || null;
|
|
311
311
|
}
|
|
312
312
|
|
|
313
313
|
/**
|
|
@@ -318,11 +318,11 @@ function getContainingDocument(element) {
|
|
|
318
318
|
* @return {string[]}
|
|
319
319
|
*/
|
|
320
320
|
function getRegisteredCustomElements() {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
321
|
+
const customElementTags = Array.from(document.querySelectorAll("*"))
|
|
322
|
+
.map((tag) => tag.tagName.toLowerCase())
|
|
323
|
+
.filter((tagName) => tagName.includes("-") && customElements.get(tagName));
|
|
324
324
|
|
|
325
|
-
|
|
325
|
+
return Array.from(new Set(customElementTags));
|
|
326
326
|
}
|
|
327
327
|
|
|
328
328
|
/**
|
|
@@ -337,67 +337,67 @@ function getRegisteredCustomElements() {
|
|
|
337
337
|
* @since 4.1.0
|
|
338
338
|
*/
|
|
339
339
|
function waitForCustomElement(
|
|
340
|
-
|
|
341
|
-
|
|
340
|
+
element,
|
|
341
|
+
{ method = null, tagName = null, timeout = 2000, readyCheck = null } = {},
|
|
342
342
|
) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
343
|
+
if (!(element instanceof HTMLElement)) {
|
|
344
|
+
return Promise.reject(
|
|
345
|
+
new Error("Invalid argument. Expected an HTMLElement."),
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const name = (tagName || element.tagName || "").toLowerCase();
|
|
350
|
+
if (!name.includes("-")) {
|
|
351
|
+
return Promise.reject(
|
|
352
|
+
new Error("Invalid argument. Expected a custom element tag name."),
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const window = getWindow();
|
|
357
|
+
const registry = window.customElements;
|
|
358
|
+
if (!registry) {
|
|
359
|
+
return Promise.reject(new Error("customElements is not supported."));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return registry.whenDefined(name).then(
|
|
363
|
+
() =>
|
|
364
|
+
new Promise((resolve, reject) => {
|
|
365
|
+
if (typeof registry.upgrade === "function") {
|
|
366
|
+
registry.upgrade(element);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const start = Date.now();
|
|
370
|
+
const isReady = () => {
|
|
371
|
+
if (method && typeof element[method] !== "function") {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
if (typeof readyCheck === "function") {
|
|
375
|
+
return readyCheck(element) === true;
|
|
376
|
+
}
|
|
377
|
+
return true;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const check = () => {
|
|
381
|
+
if (isReady()) {
|
|
382
|
+
resolve(element);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (
|
|
387
|
+
typeof timeout === "number" &&
|
|
388
|
+
timeout >= 0 &&
|
|
389
|
+
Date.now() - start > timeout
|
|
390
|
+
) {
|
|
391
|
+
reject(
|
|
392
|
+
new Error(`Timed out waiting for "${method}" on <${name}>.`),
|
|
393
|
+
);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
window.setTimeout(check, 10);
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
window.setTimeout(check, 10);
|
|
401
|
+
}),
|
|
402
|
+
);
|
|
403
403
|
}
|