@oscarpalmer/toretto 0.40.0 → 0.42.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.
Files changed (56) hide show
  1. package/dist/attribute/{get.d.mts → get.attribute.d.mts} +3 -2
  2. package/dist/attribute/{get.mjs → get.attribute.mjs} +4 -3
  3. package/dist/attribute/index.d.mts +3 -16
  4. package/dist/attribute/index.mjs +4 -7
  5. package/dist/attribute/{set.d.mts → set.attribute.d.mts} +4 -4
  6. package/dist/attribute/{set.mjs → set.attribute.mjs} +2 -2
  7. package/dist/create.d.mts +25 -0
  8. package/dist/create.mjs +17 -0
  9. package/dist/data.d.mts +1 -1
  10. package/dist/data.mjs +7 -7
  11. package/dist/event/delegation.mjs +8 -1
  12. package/dist/html/index.d.mts +23 -26
  13. package/dist/html/index.mjs +85 -18
  14. package/dist/html/sanitize.mjs +6 -5
  15. package/dist/index.d.mts +114 -53
  16. package/dist/index.mjs +553 -561
  17. package/dist/internal/attribute.d.mts +4 -3
  18. package/dist/internal/attribute.mjs +13 -23
  19. package/dist/internal/element-value.d.mts +2 -2
  20. package/dist/internal/element-value.mjs +4 -2
  21. package/dist/internal/get-value.mjs +1 -1
  22. package/dist/internal/property.d.mts +4 -0
  23. package/dist/internal/property.mjs +21 -0
  24. package/dist/property/get.property.d.mts +20 -0
  25. package/dist/property/get.property.mjs +35 -0
  26. package/dist/property/index.d.mts +3 -0
  27. package/dist/property/index.mjs +3 -0
  28. package/dist/property/set.property.d.mts +32 -0
  29. package/dist/property/set.property.mjs +34 -0
  30. package/dist/style.d.mts +12 -7
  31. package/dist/style.mjs +14 -18
  32. package/package.json +12 -5
  33. package/src/attribute/{get.ts → get.attribute.ts} +14 -3
  34. package/src/attribute/index.ts +10 -22
  35. package/src/attribute/{set.ts → set.attribute.ts} +9 -5
  36. package/src/create.ts +81 -0
  37. package/src/data.ts +17 -9
  38. package/src/event/delegation.ts +24 -3
  39. package/src/event/index.ts +9 -3
  40. package/src/find/index.ts +12 -4
  41. package/src/find/relative.ts +4 -0
  42. package/src/focusable.ts +10 -2
  43. package/src/html/index.ts +166 -58
  44. package/src/html/sanitize.ts +14 -11
  45. package/src/index.ts +2 -1
  46. package/src/internal/attribute.ts +23 -42
  47. package/src/internal/element-value.ts +11 -4
  48. package/src/internal/get-value.ts +8 -0
  49. package/src/internal/is.ts +4 -0
  50. package/src/internal/property.ts +42 -0
  51. package/src/is.ts +10 -2
  52. package/src/property/get.property.ts +73 -0
  53. package/src/property/index.ts +2 -0
  54. package/src/property/set.property.ts +102 -0
  55. package/src/style.ts +52 -27
  56. package/src/touch.ts +14 -2
package/dist/index.mjs CHANGED
@@ -108,203 +108,6 @@ function isNullableOrWhitespace(value) {
108
108
  }
109
109
  const EXPRESSION_WHITESPACE$1 = /^\s*$/;
110
110
  //#endregion
111
- //#region src/internal/is.ts
112
- /**
113
- * Is the value an event target?
114
- * @param value Value to check
115
- * @returns `true` if it's an event target, otherwise `false`
116
- */
117
- function isEventTarget(value) {
118
- return typeof value === "object" && value != null && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function" && typeof value.dispatchEvent === "function";
119
- }
120
- /**
121
- * Is the value an HTML or SVG element?
122
- * @param value Value to check
123
- * @returns `true` if it's an HTML or SVG element, otherwise `false`
124
- */
125
- function isHTMLOrSVGElement(value) {
126
- return value instanceof HTMLElement || value instanceof SVGElement;
127
- }
128
- //#endregion
129
- //#region src/is.ts
130
- /**
131
- * Is the value a child node?
132
- * @param value Value to check
133
- * @returns `true` if it's a child node, otherwise `false`
134
- */
135
- function isChildNode(value) {
136
- return value instanceof Node && CHILD_NODE_TYPES.has(value.nodeType);
137
- }
138
- function isInDocument(node, doc) {
139
- if (!(node instanceof Node)) return false;
140
- if (!(doc instanceof Document)) return node.ownerDocument?.contains(node) ?? true;
141
- return node.ownerDocument == null ? node === doc : node.ownerDocument === doc && doc.contains(node);
142
- }
143
- const CHILD_NODE_TYPES = new Set([
144
- Node.ELEMENT_NODE,
145
- Node.TEXT_NODE,
146
- Node.PROCESSING_INSTRUCTION_NODE,
147
- Node.COMMENT_NODE,
148
- Node.DOCUMENT_TYPE_NODE
149
- ]);
150
- //#endregion
151
- //#region src/internal/element-value.ts
152
- function setElementValue(element, first, second, third, callback) {
153
- if (!isHTMLOrSVGElement(element)) return;
154
- if (typeof first === "string") setElementValues(element, first, second, third, callback);
155
- else if (isAttribute(first)) setElementValues(element, first.name, first.value, third, callback);
156
- }
157
- function setElementValues(element, first, second, third, callback) {
158
- if (!isHTMLOrSVGElement(element)) return;
159
- if (typeof first === "string") {
160
- callback(element, first, second, third);
161
- return;
162
- }
163
- const isArray = Array.isArray(first);
164
- if (!isArray && !(typeof first === "object" && first !== null)) return;
165
- const entries = isArray ? first : Object.entries(first).map(([name, value]) => ({
166
- name,
167
- value
168
- }));
169
- const { length } = entries;
170
- for (let index = 0; index < length; index += 1) {
171
- const entry = entries[index];
172
- if (typeof entry === "object" && typeof entry?.name === "string") callback(element, entry.name, entry.value, third);
173
- }
174
- }
175
- function updateElementValue(element, key, value, set, remove, isBoolean, json) {
176
- if (isBoolean ? value == null : isNullableOrWhitespace(value)) remove.call(element, key);
177
- else set.call(element, key, json ? JSON.stringify(value) : String(value));
178
- }
179
- //#endregion
180
- //#region src/internal/attribute.ts
181
- function badAttributeHandler(name, value) {
182
- if (typeof name !== "string" || name.trim().length === 0 || typeof value !== "string") return true;
183
- if (EXPRESSION_CLOBBERED_NAME.test(name) && (value in document || value in formElement) || EXPRESSION_EVENT_NAME.test(name)) return true;
184
- if (EXPRESSION_SKIP_NAME.test(name) || EXPRESSION_URI_VALUE.test(value) || isValidSourceAttribute(name, value)) return false;
185
- return EXPRESSION_DATA_OR_SCRIPT.test(value);
186
- }
187
- function booleanAttributeHandler(name, value) {
188
- if (typeof name !== "string" || name.trim().length === 0 || typeof value !== "string") return true;
189
- const normalizedName = name.toLowerCase();
190
- if (!booleanAttributesSet.has(normalizedName)) return false;
191
- const normalized = value.toLowerCase();
192
- return !(normalized.length === 0 || normalized === normalizedName);
193
- }
194
- function decodeAttribute(value) {
195
- textArea ??= document.createElement("textarea");
196
- textArea.innerHTML = value;
197
- return decodeURIComponent(textArea.value);
198
- }
199
- function handleAttribute(callback, decode, first, second) {
200
- let name;
201
- let value;
202
- if (isAttribute(first)) {
203
- name = first.name;
204
- value = String(first.value);
205
- } else if (typeof first === "string" && typeof second === "string") {
206
- name = first;
207
- value = second;
208
- }
209
- if (decode && value != null) value = decodeAttribute(value);
210
- return callback(name, value?.replace(EXPRESSION_WHITESPACE, ""));
211
- }
212
- function isAttribute(value) {
213
- return value instanceof Attr || isPlainObject(value) && typeof value.name === "string" && "value" in value;
214
- }
215
- function _isBadAttribute(first, second, decode) {
216
- return handleAttribute(badAttributeHandler, decode, first, second);
217
- }
218
- function _isBooleanAttribute(first, decode) {
219
- return handleAttribute((name) => booleanAttributesSet.has(name?.toLowerCase()), decode, first, "");
220
- }
221
- function _isEmptyNonBooleanAttribute(first, second, decode) {
222
- return handleAttribute((name, value) => name != null && value != null && !booleanAttributesSet.has(name) && value.trim().length === 0, decode, first, second);
223
- }
224
- function _isInvalidBooleanAttribute(first, second, decode) {
225
- return handleAttribute(booleanAttributeHandler, decode, first, second);
226
- }
227
- function isValidSourceAttribute(name, value) {
228
- return EXPRESSION_SOURCE_NAME.test(name) && EXPRESSION_SOURCE_VALUE.test(value);
229
- }
230
- function updateAttribute(element, name, value, dispatch) {
231
- const normalizedName = name.toLowerCase();
232
- const isBoolean = booleanAttributesSet.has(normalizedName);
233
- const next = isBoolean ? value === true || typeof value === "string" && (value === "" || value.toLowerCase() === normalizedName) : value == null ? "" : value;
234
- if (name in element) updateProperty(element, normalizedName, next, dispatch);
235
- updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, isBoolean, false);
236
- }
237
- function updateProperty(element, name, value, dispatch) {
238
- if (Object.is(element[name], value)) return;
239
- element[name] = value;
240
- const event = dispatch !== false && elementEvents[element.tagName]?.[name];
241
- if (typeof event === "string") element.dispatchEvent(new Event(event, { bubbles: true }));
242
- }
243
- const EXPRESSION_CLOBBERED_NAME = /^(id|name)$/i;
244
- const EXPRESSION_DATA_OR_SCRIPT = /^(?:data|\w+script):/i;
245
- const EXPRESSION_EVENT_NAME = /^on/i;
246
- const EXPRESSION_SKIP_NAME = /^(aria-[-\w]+|data-[-\w.\u00B7-\uFFFF]+)$/i;
247
- const EXPRESSION_SOURCE_NAME = /^src$/i;
248
- const EXPRESSION_SOURCE_VALUE = /^data:/i;
249
- const EXPRESSION_URI_VALUE = /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|matrix):|[^a-z]|[a-z+.-]+(?:[^a-z+.\-:]|$))/i;
250
- const EXPRESSION_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g;
251
- /**
252
- * List of boolean attributes
253
- */
254
- const booleanAttributes = Object.freeze([
255
- "async",
256
- "autofocus",
257
- "autoplay",
258
- "checked",
259
- "controls",
260
- "default",
261
- "defer",
262
- "disabled",
263
- "formnovalidate",
264
- "hidden",
265
- "inert",
266
- "ismap",
267
- "itemscope",
268
- "loop",
269
- "multiple",
270
- "muted",
271
- "nomodule",
272
- "novalidate",
273
- "open",
274
- "playsinline",
275
- "readonly",
276
- "required",
277
- "reversed",
278
- "selected"
279
- ]);
280
- const booleanAttributesSet = new Set(booleanAttributes);
281
- const elementEvents = {
282
- DETAILS: { open: "toggle" },
283
- INPUT: {
284
- checked: "change",
285
- value: "input"
286
- },
287
- SELECT: { value: "change" },
288
- TEXTAREA: { value: "input" }
289
- };
290
- const formElement = document.createElement("form");
291
- let textArea;
292
- //#endregion
293
- //#region node_modules/@oscarpalmer/atoms/dist/string/index.mjs
294
- /**
295
- * Parse a JSON string into its proper value _(or `undefined` if it fails)_
296
- * @param value JSON string to parse
297
- * @param reviver Reviver function to transform the parsed values
298
- * @returns Parsed value or `undefined` if parsing fails
299
- */
300
- function parse(value, reviver) {
301
- try {
302
- return JSON.parse(value, reviver);
303
- } catch {
304
- return;
305
- }
306
- }
307
- //#endregion
308
111
  //#region node_modules/@oscarpalmer/atoms/dist/internal/number.mjs
309
112
  /**
310
113
  * Clamp a number between a minimum and maximum value
@@ -349,7 +152,7 @@ var SizedMap = class extends Map {
349
152
  * Is the Map full?
350
153
  */
351
154
  get full() {
352
- return this.size >= this.#maximumSize;
155
+ return super.size >= this.#maximumSize;
353
156
  }
354
157
  get maximum() {
355
158
  return this.#maximumSize;
@@ -368,17 +171,23 @@ var SizedMap = class extends Map {
368
171
  * @inheritdoc
369
172
  */
370
173
  get(key) {
371
- const value = super.get(key);
372
- if (value !== void 0 || this.has(key)) this.set(key, value);
373
- return value;
174
+ if (super.has(key)) {
175
+ const value = super.get(key);
176
+ this.#setValue(key, value, true);
177
+ return value;
178
+ }
374
179
  }
375
180
  /**
376
181
  * @inheritdoc
377
182
  */
378
183
  set(key, value) {
379
- if (this.has(key)) this.delete(key);
380
- else if (this.size >= this.#maximumSize) this.delete(this.keys().next().value);
381
- return super.set(key, value);
184
+ return this.#setValue(key, value, super.has(key));
185
+ }
186
+ #setValue(key, value, has) {
187
+ if (has) super.delete(key);
188
+ else if (super.size >= this.#maximumSize) super.delete(super.keys().next().value);
189
+ super.set(key, value);
190
+ return this;
382
191
  }
383
192
  };
384
193
  //#endregion
@@ -532,112 +341,480 @@ function toCaseCallback(value) {
532
341
  else partResult.push(capitalize(item));
533
342
  itemCount += 1;
534
343
  }
535
- cased.push(join(partResult, delimiters[type]));
344
+ cased.push(join(partResult, delimiters[type]));
345
+ }
346
+ return join(cased, delimiters[type]);
347
+ }
348
+ const CASE_CAMEL = "camel";
349
+ const CASE_KEBAB = "kebab";
350
+ const CASE_PASCAL = "pascal";
351
+ const CASE_SNAKE = "snake";
352
+ const DELIMTER_EMPTY = "";
353
+ const DELIMITER_HYPHEN = "-";
354
+ const DELIMITER_UNDERSCORE = "_";
355
+ const EXPRESSION_CAMEL_CASE = /(\p{Ll})(\p{Lu})/gu;
356
+ const EXPRESSION_ACRONYM = /(\p{Lu}*)(\p{Lu})(\p{Ll}+)/gu;
357
+ const REPLACEMENT_CAMEL_CASE = "$1-$2";
358
+ const S = "s";
359
+ const caseMemoizers = {};
360
+ const delimiters = {
361
+ [CASE_CAMEL]: DELIMTER_EMPTY,
362
+ [CASE_KEBAB]: DELIMITER_HYPHEN,
363
+ [CASE_PASCAL]: DELIMTER_EMPTY,
364
+ [CASE_SNAKE]: DELIMITER_UNDERSCORE
365
+ };
366
+ let memoizedCapitalize;
367
+ //#endregion
368
+ //#region src/internal/is.ts
369
+ /**
370
+ * Is the value an event target?
371
+ * @param value Value to check
372
+ * @returns `true` if it's an event target, otherwise `false`
373
+ */
374
+ function isEventTarget(value) {
375
+ return typeof value === "object" && value != null && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function" && typeof value.dispatchEvent === "function";
376
+ }
377
+ /**
378
+ * Is the value an HTML or SVG element?
379
+ * @param value Value to check
380
+ * @returns `true` if it's an HTML or SVG element, otherwise `false`
381
+ */
382
+ function isHTMLOrSVGElement(value) {
383
+ return value instanceof HTMLElement || value instanceof SVGElement;
384
+ }
385
+ //#endregion
386
+ //#region src/is.ts
387
+ /**
388
+ * Is the value a child node?
389
+ * @param value Value to check
390
+ * @returns `true` if it's a child node, otherwise `false`
391
+ */
392
+ function isChildNode(value) {
393
+ return value instanceof Node && CHILD_NODE_TYPES.has(value.nodeType);
394
+ }
395
+ function isInDocument(node, doc) {
396
+ if (!(node instanceof Node)) return false;
397
+ if (!(doc instanceof Document)) return node.ownerDocument?.contains(node) ?? true;
398
+ return node.ownerDocument == null ? node === doc : node.ownerDocument === doc && doc.contains(node);
399
+ }
400
+ const CHILD_NODE_TYPES = new Set([
401
+ Node.ELEMENT_NODE,
402
+ Node.TEXT_NODE,
403
+ Node.PROCESSING_INSTRUCTION_NODE,
404
+ Node.COMMENT_NODE,
405
+ Node.DOCUMENT_TYPE_NODE
406
+ ]);
407
+ //#endregion
408
+ //#region src/internal/element-value.ts
409
+ function setElementValue(element, first, second, third, callback) {
410
+ if (!isHTMLOrSVGElement(element)) return;
411
+ if (typeof first === "string") setElementValues(element, first, second, third, callback);
412
+ else if (isAttribute(first)) setElementValues(element, first.name, first.value, third, callback);
413
+ }
414
+ function setElementValues(element, first, second, third, callback) {
415
+ if (!isHTMLOrSVGElement(element)) return;
416
+ const dispatch = third !== false;
417
+ if (typeof first === "string") {
418
+ callback(element, kebabCase(first), second, dispatch);
419
+ return;
420
+ }
421
+ const isArray = Array.isArray(first);
422
+ if (!isArray && !(typeof first === "object" && first !== null)) return;
423
+ const entries = isArray ? first : Object.entries(first).map(([name, value]) => ({
424
+ name,
425
+ value
426
+ }));
427
+ const { length } = entries;
428
+ for (let index = 0; index < length; index += 1) {
429
+ const entry = entries[index];
430
+ if (typeof entry === "object" && typeof entry?.name === "string") callback(element, kebabCase(entry.name), entry.value, dispatch);
431
+ }
432
+ }
433
+ function updateElementValue(element, key, value, set, remove, isBoolean, json) {
434
+ if (isBoolean ? value == null : isNullableOrWhitespace(value)) remove.call(element, key);
435
+ else set.call(element, key, json ? JSON.stringify(value) : String(value));
436
+ }
437
+ //#endregion
438
+ //#region src/internal/property.ts
439
+ function updateProperty(element, name, value, dispatch) {
440
+ let property = name;
441
+ if (!(property in element)) property = camelCase(name);
442
+ if (!(property in element) || Object.is(element[property], value)) return;
443
+ element[property] = value;
444
+ const event = dispatch && elementEvents[element.tagName]?.[property];
445
+ if (typeof event === "string") element.dispatchEvent(new Event(event, { bubbles: true }));
446
+ }
447
+ const elementEvents = {
448
+ DETAILS: { open: "toggle" },
449
+ INPUT: {
450
+ checked: "change",
451
+ value: "input"
452
+ },
453
+ SELECT: { value: "change" },
454
+ TEXTAREA: { value: "input" }
455
+ };
456
+ //#endregion
457
+ //#region src/internal/attribute.ts
458
+ function badAttributeHandler(name, value) {
459
+ if (typeof name !== "string" || name.trim().length === 0 || typeof value !== "string") return true;
460
+ if (EXPRESSION_CLOBBERED_NAME.test(name) && (value in document || value in formElement) || EXPRESSION_EVENT_NAME.test(name)) return true;
461
+ if (EXPRESSION_SKIP_NAME.test(name) || EXPRESSION_URI_VALUE.test(value) || isValidSourceAttribute(name, value)) return false;
462
+ return EXPRESSION_DATA_OR_SCRIPT.test(value);
463
+ }
464
+ function booleanAttributeHandler(name, value) {
465
+ if (typeof name !== "string" || name.trim().length === 0 || typeof value !== "string") return true;
466
+ const normalizedName = name.toLowerCase();
467
+ if (!booleanAttributesSet.has(normalizedName)) return false;
468
+ const normalized = value.toLowerCase();
469
+ return !(normalized.length === 0 || normalized === normalizedName);
470
+ }
471
+ function decodeAttribute(value) {
472
+ textArea ??= document.createElement("textarea");
473
+ textArea.innerHTML = value;
474
+ return decodeURIComponent(textArea.value);
475
+ }
476
+ function handleAttribute(callback, decode, first, second) {
477
+ let name;
478
+ let value;
479
+ if (isAttribute(first)) {
480
+ name = first.name;
481
+ value = String(first.value);
482
+ } else if (typeof first === "string" && typeof second === "string") {
483
+ name = first;
484
+ value = second;
485
+ }
486
+ if (name != null) name = kebabCase(name);
487
+ if (decode && value != null) value = decodeAttribute(value);
488
+ return callback(name, value?.replace(EXPRESSION_WHITESPACE, ""));
489
+ }
490
+ function isAttribute(value) {
491
+ return value instanceof Attr || isPlainObject(value) && typeof value.name === "string" && "value" in value;
492
+ }
493
+ function _isBadAttribute(first, second, decode) {
494
+ return handleAttribute(badAttributeHandler, decode, first, second);
495
+ }
496
+ function _isBooleanAttribute(first, decode) {
497
+ return handleAttribute((name) => booleanAttributesSet.has(name?.toLowerCase()), decode, first, "");
498
+ }
499
+ function _isInvalidBooleanAttribute(first, second, decode) {
500
+ return handleAttribute(booleanAttributeHandler, decode, first, second);
501
+ }
502
+ function isValidSourceAttribute(name, value) {
503
+ return EXPRESSION_SOURCE_NAME.test(name) && EXPRESSION_SOURCE_VALUE.test(value);
504
+ }
505
+ function updateAttribute(element, name, value, dispatch) {
506
+ const lowerCaseName = name.toLowerCase();
507
+ const isBoolean = booleanAttributesSet.has(lowerCaseName);
508
+ const next = isBoolean ? value === true || typeof value === "string" && (value === "" || value.toLowerCase() === lowerCaseName) : value == null ? "" : value;
509
+ if (isBoolean || dispatchedAttributes.has(name)) updateProperty(element, name, next, dispatch);
510
+ updateElementValue(element, name, isBoolean ? next ? "" : null : value, element.setAttribute, element.removeAttribute, isBoolean, false);
511
+ }
512
+ const EXPRESSION_CLOBBERED_NAME = /^(id|name)$/i;
513
+ const EXPRESSION_DATA_OR_SCRIPT = /^(?:data|\w+script):/i;
514
+ const EXPRESSION_EVENT_NAME = /^on/i;
515
+ const EXPRESSION_SKIP_NAME = /^(aria-[-\w]+|data-[-\w.\u00B7-\uFFFF]+)$/i;
516
+ const EXPRESSION_SOURCE_NAME = /^src$/i;
517
+ const EXPRESSION_SOURCE_VALUE = /^data:/i;
518
+ const EXPRESSION_URI_VALUE = /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|matrix):|[^a-z]|[a-z+.-]+(?:[^a-z+.\-:]|$))/i;
519
+ const EXPRESSION_WHITESPACE = /[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g;
520
+ /**
521
+ * List of boolean attributes
522
+ */
523
+ const booleanAttributes = Object.freeze([
524
+ "async",
525
+ "autofocus",
526
+ "autoplay",
527
+ "checked",
528
+ "controls",
529
+ "default",
530
+ "defer",
531
+ "disabled",
532
+ "formnovalidate",
533
+ "hidden",
534
+ "inert",
535
+ "ismap",
536
+ "itemscope",
537
+ "loop",
538
+ "multiple",
539
+ "muted",
540
+ "nomodule",
541
+ "novalidate",
542
+ "open",
543
+ "playsinline",
544
+ "readonly",
545
+ "required",
546
+ "reversed",
547
+ "selected"
548
+ ]);
549
+ const booleanAttributesSet = new Set(booleanAttributes);
550
+ const dispatchedAttributes = new Set([
551
+ "checked",
552
+ "open",
553
+ "value"
554
+ ]);
555
+ const formElement = document.createElement("form");
556
+ let textArea;
557
+ //#endregion
558
+ //#region node_modules/@oscarpalmer/atoms/dist/string/index.mjs
559
+ /**
560
+ * Parse a JSON string into its proper value _(or `undefined` if it fails)_
561
+ * @param value JSON string to parse
562
+ * @param reviver Reviver function to transform the parsed values
563
+ * @returns Parsed value or `undefined` if parsing fails
564
+ */
565
+ function parse(value, reviver) {
566
+ try {
567
+ return JSON.parse(value, reviver);
568
+ } catch {
569
+ return;
570
+ }
571
+ }
572
+ //#endregion
573
+ //#region src/internal/get-value.ts
574
+ function getBoolean(value, defaultValue) {
575
+ return typeof value === "boolean" ? value : defaultValue ?? false;
576
+ }
577
+ function getAttributeValue(element, name, parseValue) {
578
+ const normalized = kebabCase(name);
579
+ const attribute = element.attributes[normalized];
580
+ const value = attribute instanceof Attr ? attribute.value : void 0;
581
+ return EXPRESSION_DATA_PREFIX.test(normalized) && typeof value === "string" && parseValue ? parse(value) ?? value : value;
582
+ }
583
+ function getStyleValue(element, property, computed) {
584
+ const name = camelCase(property);
585
+ return computed ? getComputedStyle(element)[name] : element.style[name];
586
+ }
587
+ const EXPRESSION_DATA_PREFIX = /^data-/i;
588
+ //#endregion
589
+ //#region src/attribute/get.attribute.ts
590
+ function getAttribute(element, name, parseValues) {
591
+ if (isHTMLOrSVGElement(element) && typeof name === "string") return getAttributeValue(element, kebabCase(name), parseValues !== false);
592
+ }
593
+ /**
594
+ * Get specific attributes from an element
595
+ * @param element Element to get attributes from
596
+ * @param names Attribute names
597
+ * @param parseData Parse data values? _(defaults to `true`)_
598
+ * @returns Object of named attributes
599
+ */
600
+ function getAttributes(element, names, parseData) {
601
+ const attributes = {};
602
+ if (!(isHTMLOrSVGElement(element) && Array.isArray(names))) return attributes;
603
+ const shouldParse = parseData !== false;
604
+ const { length } = names;
605
+ for (let index = 0; index < length; index += 1) {
606
+ const name = names[index];
607
+ if (typeof name === "string") attributes[name] = getAttributeValue(element, kebabCase(name), shouldParse);
608
+ }
609
+ return attributes;
610
+ }
611
+ //#endregion
612
+ //#region src/attribute/set.attribute.ts
613
+ function setAttribute(element, first, second, third) {
614
+ setElementValue(element, first, second, third, updateAttribute);
615
+ }
616
+ function setAttributes(element, attributes, dispatch) {
617
+ setElementValues(element, attributes, null, dispatch, updateAttribute);
618
+ }
619
+ //#endregion
620
+ //#region src/attribute/index.ts
621
+ function isBadAttribute(first, second) {
622
+ return _isBadAttribute(first, second, true);
623
+ }
624
+ function isBooleanAttribute(first) {
625
+ return _isBooleanAttribute(first, true);
626
+ }
627
+ function isInvalidBooleanAttribute(first, second) {
628
+ return _isInvalidBooleanAttribute(first, second, true);
629
+ }
630
+ //#endregion
631
+ //#region src/style.ts
632
+ /**
633
+ * Get a style from an element
634
+ * @param element Element to get the style from
635
+ * @param property Style name
636
+ * @param computed Get the computed style? _(defaults to `false`)_
637
+ * @returns Style value
638
+ */
639
+ function getStyle(element, property, computed) {
640
+ if (isHTMLOrSVGElement(element) && typeof property === "string") return getStyleValue(element, property, computed === true);
641
+ }
642
+ /**
643
+ * Get styles from an element
644
+ * @param element Element to get the styles from
645
+ * @param properties Styles to get
646
+ * @param computed Get the computed styles? _(defaults to `false`)_
647
+ * @returns Style values
648
+ */
649
+ function getStyles(element, properties, computed) {
650
+ const styles = {};
651
+ if (!(isHTMLOrSVGElement(element) && Array.isArray(properties))) return styles;
652
+ const { length } = properties;
653
+ for (let index = 0; index < length; index += 1) {
654
+ const property = properties[index];
655
+ if (typeof property === "string") styles[property] = getStyleValue(element, property, computed === true);
656
+ }
657
+ return styles;
658
+ }
659
+ function getTextDirection(node) {
660
+ let target;
661
+ if (isHTMLOrSVGElement(node)) target = node;
662
+ else target = node instanceof Node ? node.ownerDocument?.documentElement ?? document.documentElement : document.documentElement;
663
+ let { direction } = target.style;
664
+ if (direction === "") direction = getStyleValue(target, PROPETY_DIRECTION, true);
665
+ return direction === DIRECTION_RTL ? DIRECTION_RTL : DIRECTION_LTR;
666
+ }
667
+ /**
668
+ * Set a style on an element
669
+ * @param element Element to set the style on
670
+ * @param property Style name
671
+ * @param value Style value
672
+ */
673
+ function setStyle(element, property, value) {
674
+ setElementValues(element, property, value, null, updateStyleProperty);
675
+ }
676
+ /**
677
+ * Set styles on an element
678
+ * @param element Element to set the styles on
679
+ * @param styles Styles to set
680
+ */
681
+ function setStyles(element, styles) {
682
+ setElementValues(element, styles, null, null, updateStyleProperty);
683
+ }
684
+ /**
685
+ * Toggle styles for an element
686
+ * @param element Element to style
687
+ * @param styles Styles to be set or removed
688
+ * @returns Style toggler
689
+ */
690
+ function toggleStyles(element, styles) {
691
+ function toggle(set) {
692
+ hasSet = set;
693
+ let next;
694
+ if (set) {
695
+ values = getStyles(element, keys);
696
+ next = styles;
697
+ } else {
698
+ next = { ...values };
699
+ values = {};
700
+ for (let index = 0; index < length; index += 1) values[keys[index]] = void 0;
701
+ }
702
+ setStyles(element, next);
536
703
  }
537
- return join(cased, delimiters[type]);
538
- }
539
- const CASE_CAMEL = "camel";
540
- const CASE_KEBAB = "kebab";
541
- const CASE_PASCAL = "pascal";
542
- const CASE_SNAKE = "snake";
543
- const DELIMTER_EMPTY = "";
544
- const DELIMITER_HYPHEN = "-";
545
- const DELIMITER_UNDERSCORE = "_";
546
- const EXPRESSION_CAMEL_CASE = /(\p{Ll})(\p{Lu})/gu;
547
- const EXPRESSION_ACRONYM = /(\p{Lu}*)(\p{Lu})(\p{Ll}+)/gu;
548
- const REPLACEMENT_CAMEL_CASE = "$1-$2";
549
- const S = "s";
550
- const caseMemoizers = {};
551
- const delimiters = {
552
- [CASE_CAMEL]: DELIMTER_EMPTY,
553
- [CASE_KEBAB]: DELIMITER_HYPHEN,
554
- [CASE_PASCAL]: DELIMTER_EMPTY,
555
- [CASE_SNAKE]: DELIMITER_UNDERSCORE
556
- };
557
- let memoizedCapitalize;
558
- //#endregion
559
- //#region src/internal/get-value.ts
560
- function getBoolean(value, defaultValue) {
561
- return typeof value === "boolean" ? value : defaultValue ?? false;
562
- }
563
- function getAttributeValue(element, name, parseValue) {
564
- const normalized = kebabCase(name);
565
- const attribute = element.attributes[normalized];
566
- const value = attribute instanceof Attr ? attribute.value : void 0;
567
- return EXPRESSION_DATA_PREFIX.test(normalized) && typeof value === "string" && parseValue ? parse(value) ?? value : value;
704
+ const keys = Object.keys(styles);
705
+ const { length } = keys;
706
+ let hasSet = false;
707
+ let values = {};
708
+ return {
709
+ set() {
710
+ if (!hasSet) toggle(true);
711
+ },
712
+ remove() {
713
+ if (hasSet) toggle(false);
714
+ }
715
+ };
568
716
  }
569
- function getStyleValue(element, property, computed) {
570
- const name = camelCase(property);
571
- return computed ? getComputedStyle(element)[name] : element.style[name];
717
+ function updateStyleProperty(element, key, value) {
718
+ updateElementValue(element, key, value, function(property, style) {
719
+ this.style[property] = String(style);
720
+ }, function(property) {
721
+ this.style[property] = "";
722
+ }, false, false);
572
723
  }
573
- const EXPRESSION_DATA_PREFIX = /^data-/i;
724
+ const DIRECTION_LTR = "ltr";
725
+ const DIRECTION_RTL = "rtl";
726
+ const PROPETY_DIRECTION = "direction";
574
727
  //#endregion
575
- //#region src/attribute/get.ts
576
- function getAttribute(element, name, parseValues) {
577
- if (isHTMLOrSVGElement(element) && typeof name === "string") return getAttributeValue(element, name, parseValues !== false);
578
- }
728
+ //#region src/property/get.property.ts
579
729
  /**
580
- * Get specific attributes from an element
581
- * @param element Element to get attributes from
582
- * @param names Attribute names
583
- * @param parseData Parse data values? _(defaults to `true`)_
584
- * @returns Object of named attributes
730
+ * Get the values of one or more properties on an element
731
+ * @param target Target element
732
+ * @param properties Properties to get
733
+ * @returns Property values
585
734
  */
586
- function getAttributes(element, names, parseData) {
587
- const attributes = {};
588
- if (!(isHTMLOrSVGElement(element) && Array.isArray(names))) return attributes;
589
- const shouldParse = parseData !== false;
590
- const { length } = names;
735
+ function getProperties(target, properties) {
736
+ const values = {};
737
+ if (!isHTMLOrSVGElement(target) || !Array.isArray(properties)) return values;
738
+ const { length } = properties;
591
739
  for (let index = 0; index < length; index += 1) {
592
- const name = names[index];
593
- if (typeof name === "string") attributes[name] = getAttributeValue(element, name, shouldParse);
740
+ const property = properties[index];
741
+ if (typeof property === "string") values[property] = getPropertyValue(target, property);
594
742
  }
595
- return attributes;
743
+ return values;
596
744
  }
597
- //#endregion
598
- //#region src/attribute/set.ts
599
- function setAttribute(element, first, second, third) {
600
- setElementValue(element, first, second, third, updateAttribute);
745
+ /**
746
+ * Get the value of a property on an element
747
+ * @param target Target element
748
+ * @param property Property to get
749
+ * @returns Property value
750
+ */
751
+ function getProperty(target, property) {
752
+ if (isHTMLOrSVGElement(target) && typeof property === "string") return getPropertyValue(target, property);
601
753
  }
602
- function setAttributes(element, attributes, dispatch) {
603
- setElementValues(element, attributes, null, dispatch, updateAttribute);
754
+ function getPropertyValue(element, property) {
755
+ let actual = property;
756
+ if (!(actual in element)) actual = camelCase(actual);
757
+ if (actual in element) return element[actual];
604
758
  }
605
759
  //#endregion
606
- //#region src/attribute/index.ts
607
- function isBadAttribute(first, second) {
608
- return _isBadAttribute(first, second, true);
609
- }
610
- function isBooleanAttribute(first) {
611
- return _isBooleanAttribute(first, true);
612
- }
613
- function isEmptyNonBooleanAttribute(first, second) {
614
- return _isEmptyNonBooleanAttribute(first, second, true);
760
+ //#region src/property/set.property.ts
761
+ /**
762
+ * Set the values of one or more properties on an element
763
+ *
764
+ * Also updates attributes for boolean/dispatchable properties, and if `dispatch` is `true`, will dispatch events for dispatchable properties
765
+ * @param target Target element
766
+ * @param properties Properties to set
767
+ * @param dispatch Dispatch events for properties? _(defaults to `true`)_
768
+ */
769
+ function setProperties(target, properties, dispatch) {
770
+ if (!isHTMLOrSVGElement(target) || !isPlainObject(properties)) return;
771
+ const shouldDispatch = dispatch !== false;
772
+ const keys = Object.keys(properties);
773
+ const { length } = keys;
774
+ for (let index = 0; index < length; index += 1) {
775
+ const key = keys[index];
776
+ if (booleanAttributesSet.has(key.toLowerCase()) || dispatchedAttributes.has(key)) setAttribute(target, key, properties[key], shouldDispatch);
777
+ else updateProperty(target, key, properties[key], shouldDispatch);
778
+ }
615
779
  }
616
- function isInvalidBooleanAttribute(first, second) {
617
- return _isInvalidBooleanAttribute(first, second, true);
780
+ function setProperty(target, property, value, dispatch) {
781
+ if (!isHTMLOrSVGElement(target) || typeof property !== "string") return;
782
+ if (booleanAttributesSet.has(property.toLowerCase()) || dispatchedAttributes.has(property)) setAttribute(target, property, value, dispatch !== false);
783
+ else updateProperty(target, property, value, dispatch !== false);
618
784
  }
619
785
  //#endregion
786
+ //#region src/create.ts
787
+ function createElement(tag, properties, attributes, styles) {
788
+ if (typeof tag !== "string") throw new TypeError(MESSAGE);
789
+ const element = document.createElement(tag);
790
+ if (properties != null) setProperties(element, properties);
791
+ if (attributes != null) setAttributes(element, attributes);
792
+ if (styles != null) setStyles(element, styles);
793
+ return element;
794
+ }
795
+ const MESSAGE = "Tag name must be a string";
796
+ //#endregion
620
797
  //#region src/data.ts
621
798
  function getData(element, keys, parseValues) {
622
799
  if (!isHTMLOrSVGElement(element)) return;
623
- const shouldParse = parseValues !== false;
800
+ const noParse = parseValues === false;
624
801
  if (typeof keys === "string") {
625
- const value = element.dataset[keys];
802
+ const value = element.dataset[camelCase(keys)];
626
803
  if (value === void 0) return;
627
- return shouldParse ? parse(value) : value;
804
+ return noParse ? value : parse(value);
628
805
  }
629
806
  const { length } = keys;
630
807
  const data = {};
631
808
  for (let index = 0; index < length; index += 1) {
632
809
  const key = keys[index];
633
- const value = element.dataset[key];
810
+ const value = element.dataset[camelCase(key)];
634
811
  if (value == null) data[key] = void 0;
635
- else data[key] = shouldParse ? parse(value) : value;
812
+ else data[key] = noParse ? value : parse(value);
636
813
  }
637
814
  return data;
638
815
  }
639
816
  function getName(original) {
640
- return `${ATTRIBUTE_DATA_PREFIX}${kebabCase(original).replace(EXPRESSION_DATA_PREFIX, "")}`;
817
+ return `${ATTRIBUTE_DATA_PREFIX}${kebabCase(original.replace(EXPRESSION_DATA_PREFIX, ""))}`;
641
818
  }
642
819
  function setData(element, first, second) {
643
820
  setElementValues(element, first, second, null, updateDataAttribute);
@@ -653,163 +830,6 @@ const ATTRIBUTE_DATA_PREFIX = "data-";
653
830
  */
654
831
  function noop() {}
655
832
  //#endregion
656
- //#region node_modules/@oscarpalmer/atoms/dist/function/assert.mjs
657
- /**
658
- * Asserts that a condition is true, throwing an error if it is not
659
- * @param condition Condition to assert
660
- * @param message Error message
661
- * @param error Error constructor
662
- */
663
- function assert(condition, message, error) {
664
- if (!condition()) throw new (error ?? Error)(message);
665
- }
666
- assert.condition = assertCondition;
667
- assert.defined = assertDefined;
668
- assert.instanceOf = assertInstanceOf;
669
- assert.is = assertIs;
670
- /**
671
- * Creates an asserter that asserts a condition is true, throwing an error if it is not
672
- * @param condition Condition to assert
673
- * @param message Error message
674
- * @param error Error constructor
675
- * @returns Asserter
676
- */
677
- function assertCondition(condition, message, error) {
678
- return (value) => {
679
- assert(() => condition(value), message, error);
680
- };
681
- }
682
- /**
683
- * Asserts that a value is defined throwing an error if it is not
684
- * @param value Value to assert
685
- * @param message Error message
686
- */
687
- function assertDefined(value, message) {
688
- assert(() => value != null, message ?? MESSAGE_VALUE_DEFINED);
689
- }
690
- /**
691
- * Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
692
- * @param constructor Constructor to check against
693
- * @param message Error message
694
- * @param error Error constructor
695
- * @returns Asserter
696
- */
697
- function assertInstanceOf(constructor, message, error) {
698
- return (value) => {
699
- assert(() => value instanceof constructor, message, error);
700
- };
701
- }
702
- /**
703
- * Creates an asserter that asserts a value is of a specific type, throwing an error if it is not
704
- * @param condition Type guard function to check the value
705
- * @param message Error message
706
- * @param error Error constructor
707
- * @returns Asserter
708
- */
709
- function assertIs(condition, message, error) {
710
- return (value) => {
711
- assert(() => condition(value), message, error);
712
- };
713
- }
714
- const MESSAGE_VALUE_DEFINED = "Expected value to be defined";
715
- //#endregion
716
- //#region node_modules/@oscarpalmer/atoms/dist/function/once.mjs
717
- /**
718
- * Create an asynchronous function that can only be called once, rejecting or resolving the same result on subsequent calls
719
- * @param callback Callback to use once
720
- * @returns Once callback
721
- */
722
- function asyncOnce(callback) {
723
- assert(() => typeof callback === "function", MESSAGE_EXPECTATION);
724
- const state = {
725
- called: false,
726
- cleared: false,
727
- error: false,
728
- finished: false,
729
- items: [],
730
- value: void 0
731
- };
732
- const fn = (...parameters) => {
733
- if (state.cleared) return Promise.reject(new Error(MESSAGE_CLEARED));
734
- if (state.finished) return state.error ? Promise.reject(state.value) : Promise.resolve(state.value);
735
- if (state.called) return new Promise((resolve, reject) => {
736
- state.items.push({
737
- reject,
738
- resolve
739
- });
740
- });
741
- state.called = true;
742
- return new Promise((resolve, reject) => {
743
- state.items.push({
744
- reject,
745
- resolve
746
- });
747
- callback(...parameters).then((value) => {
748
- handleResult(state, value, false);
749
- }).catch((error) => {
750
- handleResult(state, error, true);
751
- });
752
- });
753
- };
754
- Object.defineProperties(fn, {
755
- called: { get: () => state.called },
756
- cleared: { get: () => state.cleared },
757
- error: { get: () => state.error },
758
- finished: { get: () => state.finished }
759
- });
760
- fn.clear = () => {
761
- if (!state.called || !state.finished || state.cleared) return;
762
- state.cleared = true;
763
- state.value = void 0;
764
- };
765
- return fn;
766
- }
767
- function handleResult(state, value, error) {
768
- state.error = error;
769
- state.finished = true;
770
- state.value = value;
771
- const items = state.items.splice(0);
772
- const { length } = items;
773
- for (let index = 0; index < length; index += 1) {
774
- const { reject, resolve } = items[index];
775
- if (error) reject(value);
776
- else resolve(value);
777
- }
778
- }
779
- /**
780
- * Create a function that can only be called once, returning the same value on subsequent calls
781
- * @param callback Callback to use once
782
- * @returns Once callback
783
- */
784
- function once(callback) {
785
- assert(() => typeof callback === "function", MESSAGE_EXPECTATION);
786
- const state = {
787
- called: false,
788
- cleared: false,
789
- value: void 0
790
- };
791
- const fn = (...parameters) => {
792
- if (state.cleared) throw new Error(MESSAGE_CLEARED);
793
- if (state.called) return state.value;
794
- state.called = true;
795
- state.value = callback(...parameters);
796
- return state.value;
797
- };
798
- Object.defineProperties(fn, {
799
- called: { get: () => state.called },
800
- cleared: { get: () => state.cleared }
801
- });
802
- fn.clear = () => {
803
- if (!state.called || state.cleared) return;
804
- state.cleared = true;
805
- state.value = void 0;
806
- };
807
- return fn;
808
- }
809
- once.async = asyncOnce;
810
- const MESSAGE_CLEARED = "Once has been cleared";
811
- const MESSAGE_EXPECTATION = "Once expected a function";
812
- //#endregion
813
833
  //#region src/event/delegation.ts
814
834
  function addDelegatedHandler(doc, type, name, passive) {
815
835
  if (DELEGATED.has(name)) return;
@@ -828,7 +848,14 @@ function delegatedEventHandler(event) {
828
848
  const key = `${EVENT_PREFIX}${event.type}${this ? EVENT_SUFFIX_PASSIVE : EVENT_SUFFIX_ACTIVE}`;
829
849
  const items = event.composedPath();
830
850
  const { length } = items;
851
+ let cancelled = false;
831
852
  let target = items[0];
853
+ const originalStopPropagation = event.stopPropagation;
854
+ event.stopPropagation = function() {
855
+ cancelled = true;
856
+ originalStopPropagation.call(event);
857
+ };
858
+ event.stopImmediatePropagation = event.stopPropagation.bind(event);
832
859
  Object.defineProperties(event, {
833
860
  currentTarget: {
834
861
  configurable: true,
@@ -848,7 +875,7 @@ function delegatedEventHandler(event) {
848
875
  target = item;
849
876
  for (const listener of listeners) {
850
877
  listener.call(item, event);
851
- if (event.cancelBubble) return;
878
+ if (cancelled) return;
852
879
  }
853
880
  }
854
881
  }
@@ -1282,8 +1309,9 @@ const TYPE_RADIO = "radio";
1282
1309
  //#region src/html/sanitize.ts
1283
1310
  function handleElement(element, depth) {
1284
1311
  if (depth === 0) {
1285
- const removable = element.querySelectorAll(REMOVE_SELECTOR);
1286
- for (const item of removable) item.remove();
1312
+ const removable = [...element.querySelectorAll(REMOVE_SELECTOR)];
1313
+ const { length } = removable;
1314
+ for (let index = 0; index < length; index += 1) removable[index].remove();
1287
1315
  }
1288
1316
  sanitizeAttributes(element, [...element.attributes]);
1289
1317
  }
@@ -1302,7 +1330,7 @@ function sanitizeAttributes(element, attributes) {
1302
1330
  const { length } = attributes;
1303
1331
  for (let index = 0; index < length; index += 1) {
1304
1332
  const { name, value } = attributes[index];
1305
- if (_isBadAttribute(name, value, false) || _isEmptyNonBooleanAttribute(name, value, false)) element.removeAttribute(name);
1333
+ if (_isBadAttribute(name, value, false)) element.removeAttribute(name);
1306
1334
  else if (_isInvalidBooleanAttribute(name, value, false)) setAttribute(element, name, true);
1307
1335
  }
1308
1336
  }
@@ -1348,16 +1376,22 @@ function createHtml(value) {
1348
1376
  function createTemplate(value, options) {
1349
1377
  const template = document.createElement(TEMPLATE_TAG);
1350
1378
  template.innerHTML = createHtml(value);
1351
- if (typeof value === "string" && options.cache) templates[value] = template;
1379
+ if (typeof value === "string" && options.cache) templates.set(value, template);
1352
1380
  return template;
1353
1381
  }
1382
+ function getComment(index) {
1383
+ return COMMENT_TEMPLATE.replace(COMMENT_INDEX, String(index));
1384
+ }
1354
1385
  function getHtml(value) {
1355
1386
  return `${TEMPORARY_ELEMENT}${typeof value === "string" ? value : value.innerHTML}${TEMPORARY_ELEMENT}`;
1356
1387
  }
1357
- function getNodes(value, options) {
1388
+ function getNodes(value, options, nodes) {
1358
1389
  if (typeof value !== "string" && !(value instanceof HTMLTemplateElement)) return [];
1359
1390
  const template = getTemplate(value, options);
1360
- return template == null ? [] : [...template.content.cloneNode(true).childNodes];
1391
+ if (template == null) return [];
1392
+ const cloned = [...template.content.cloneNode(true).childNodes];
1393
+ if (nodes != null) replaceComments(cloned, nodes);
1394
+ return cloned;
1361
1395
  }
1362
1396
  function getOptions(input) {
1363
1397
  const options = isPlainObject(input) ? input : {};
@@ -1368,31 +1402,86 @@ function getParser() {
1368
1402
  parser ??= new DOMParser();
1369
1403
  return parser;
1370
1404
  }
1405
+ function getTagged(strings, values) {
1406
+ const tagged = {
1407
+ nodes: [],
1408
+ template: ""
1409
+ };
1410
+ const stringsLength = strings.length;
1411
+ let nodeIndex = 0;
1412
+ for (let stringIndex = 0; stringIndex < stringsLength; stringIndex += 1) {
1413
+ const value = values[stringIndex];
1414
+ tagged.template += strings[stringIndex];
1415
+ if (value instanceof Node) {
1416
+ tagged.nodes.push(value);
1417
+ tagged.template += getComment(nodeIndex);
1418
+ nodeIndex += 1;
1419
+ } else if (hasNodes(value)) {
1420
+ const items = [...value];
1421
+ const itemsLength = items.length;
1422
+ for (let itemIndex = 0; itemIndex < itemsLength; itemIndex += 1) {
1423
+ const item = items[itemIndex];
1424
+ if (item instanceof Node) {
1425
+ tagged.nodes.push(item);
1426
+ tagged.template += getComment(nodeIndex);
1427
+ nodeIndex += 1;
1428
+ } else tagged.template += getString(item);
1429
+ }
1430
+ } else if (Array.isArray(value)) {
1431
+ const valueLength = value.length;
1432
+ for (let valueIndex = 0; valueIndex < valueLength; valueIndex += 1) tagged.template += getString(value[valueIndex]);
1433
+ } else tagged.template += getString(value);
1434
+ }
1435
+ return tagged;
1436
+ }
1371
1437
  function getTemplate(value, options) {
1372
1438
  if (value instanceof HTMLTemplateElement) return createTemplate(value, options);
1373
1439
  if (value.trim().length === 0) return;
1374
- let template = templates[value];
1440
+ let template = templates.get(value);
1375
1441
  if (template != null) return template;
1376
1442
  const element = EXPRESSION_ID.test(value) ? document.querySelector(`#${value}`) : null;
1377
1443
  return createTemplate(element instanceof HTMLTemplateElement ? element : value, options);
1378
1444
  }
1379
- const html = ((value, options) => {
1380
- return getNodes(value, getOptions(options));
1381
- });
1445
+ function hasNodes(value) {
1446
+ if (value instanceof HTMLCollection || value instanceof NodeList) return true;
1447
+ return Array.isArray(value) && value.some((item) => item instanceof Node);
1448
+ }
1449
+ function html(first, ...second) {
1450
+ if (isTagged(first)) {
1451
+ const tagged = getTagged(first, second);
1452
+ return getNodes(tagged.template, getOptions(), tagged.nodes);
1453
+ }
1454
+ return getNodes(first, getOptions(second[0]));
1455
+ }
1456
+ /**
1457
+ * Clear cache of template elements
1458
+ */
1382
1459
  html.clear = () => {
1383
- templates = {};
1460
+ templates.clear();
1384
1461
  };
1462
+ /**
1463
+ * Remove cached template element for an HTML string or id
1464
+ * @param template HTML string or id for a template element
1465
+ */
1385
1466
  html.remove = (template) => {
1386
- if (typeof template !== "string" || templates[template] == null) return;
1387
- const keys = Object.keys(templates);
1388
- const { length } = keys;
1389
- const updated = {};
1390
- for (let index = 0; index < length; index += 1) {
1391
- const key = keys[index];
1392
- if (key !== template) updated[key] = templates[key];
1393
- }
1394
- templates = updated;
1467
+ templates.delete(template);
1395
1468
  };
1469
+ function isTagged(value) {
1470
+ return Array.isArray(value) && Array.isArray(value.raw);
1471
+ }
1472
+ function replaceComments(origin, replacements) {
1473
+ const nodes = [...origin];
1474
+ const { length } = nodes;
1475
+ for (let nodeIndex = 0; nodeIndex < length; nodeIndex += 1) {
1476
+ const node = nodes[nodeIndex];
1477
+ if (node instanceof Comment) {
1478
+ const [, index] = EXPRESSION_COMMENT.exec(node.textContent) ?? [];
1479
+ if (index != null) node.replaceWith(replacements[Number(index)]);
1480
+ continue;
1481
+ }
1482
+ if (node.hasChildNodes()) replaceComments(node.childNodes, replacements);
1483
+ }
1484
+ }
1396
1485
  /**
1397
1486
  * Sanitize one or more nodes, recursively
1398
1487
  * @param value Node or nodes to sanitize
@@ -1402,112 +1491,15 @@ html.remove = (template) => {
1402
1491
  function sanitize(value) {
1403
1492
  return sanitizeNodes(Array.isArray(value) ? value : [value], 0);
1404
1493
  }
1494
+ const COMMENT_INDEX = "<index>";
1495
+ const COMMENT_TEMPLATE = `<!--toretto.node:${COMMENT_INDEX}-->`;
1496
+ const EXPRESSION_COMMENT = /^toretto\.node:(\d+)$/;
1405
1497
  const EXPRESSION_ID = /^[a-z][\w-]*$/i;
1406
1498
  const PARSE_TYPE_HTML = "text/html";
1407
1499
  const TEMPLATE_TAG = "template";
1408
1500
  const TEMPORARY_ELEMENT = "<toretto-temporary></toretto-temporary>";
1501
+ const templates = new SizedMap(128);
1409
1502
  let parser;
1410
- let templates = {};
1411
- //#endregion
1412
- //#region src/style.ts
1413
- /**
1414
- * Get a style from an element
1415
- * @param element Element to get the style from
1416
- * @param property Style name
1417
- * @param computed Get the computed style? _(defaults to `false`)_
1418
- * @returns Style value
1419
- */
1420
- function getStyle(element, property, computed) {
1421
- if (!isHTMLOrSVGElement(element) || typeof property !== "string") return;
1422
- return getStyleValue(element, property, computed === true);
1423
- }
1424
- /**
1425
- * Get styles from an element
1426
- * @param element Element to get the styles from
1427
- * @param properties Styles to get
1428
- * @param computed Get the computed styles? _(defaults to `false`)_
1429
- * @returns Style values
1430
- */
1431
- function getStyles(element, properties, computed) {
1432
- const styles = {};
1433
- if (!(isHTMLOrSVGElement(element) && Array.isArray(properties))) return styles;
1434
- const { length } = properties;
1435
- for (let index = 0; index < length; index += 1) {
1436
- const property = properties[index];
1437
- if (typeof property === "string") styles[property] = getStyleValue(element, property, computed === true);
1438
- }
1439
- return styles;
1440
- }
1441
- /**
1442
- * Get the text direction of an element
1443
- * @param element Element to get the text direction from
1444
- * @param computed Get the computed text direction? _(defaults to `false`)_
1445
- * @returns Text direction
1446
- */
1447
- function getTextDirection(element, computed) {
1448
- if (!(element instanceof Element)) return;
1449
- const direction = element.getAttribute(ATTRIBUTE_DIRECTION);
1450
- if (direction != null && EXPRESSION_DIRECTION.test(direction)) return direction.toLowerCase();
1451
- const value = getStyleValue(element, "direction", computed === true);
1452
- return value === "rtl" ? value : "ltr";
1453
- }
1454
- /**
1455
- * Set a style on an element
1456
- * @param element Element to set the style on
1457
- * @param property Style name
1458
- * @param value Style value
1459
- */
1460
- function setStyle(element, property, value) {
1461
- setElementValues(element, property, value, null, updateStyleProperty);
1462
- }
1463
- /**
1464
- * Set styles on an element
1465
- * @param element Element to set the styles on
1466
- * @param styles Styles to set
1467
- */
1468
- function setStyles(element, styles) {
1469
- setElementValues(element, styles, null, null, updateStyleProperty);
1470
- }
1471
- /**
1472
- * Toggle styles for an element
1473
- * @param element Element to style
1474
- * @param styles Styles to be set or removed
1475
- * @returns Style toggler
1476
- */
1477
- function toggleStyles(element, styles) {
1478
- function toggle(set) {
1479
- hasSet = set;
1480
- let next;
1481
- if (set) {
1482
- values = getStyles(element, keys);
1483
- next = styles;
1484
- } else {
1485
- next = { ...values };
1486
- values = {};
1487
- for (const key of keys) values[key] = void 0;
1488
- }
1489
- setStyles(element, next);
1490
- }
1491
- const keys = Object.keys(styles);
1492
- let hasSet = false;
1493
- let values = {};
1494
- return {
1495
- set() {
1496
- if (!hasSet) toggle(true);
1497
- },
1498
- remove() {
1499
- if (hasSet) toggle(false);
1500
- }
1501
- };
1502
- }
1503
- function updateStyleProperty(element, key, value) {
1504
- updateElementValue(element, key, value, function(property, style) {
1505
- this.style[property] = style;
1506
- }, function(property) {
1507
- this.style[property] = "";
1508
- }, false, false);
1509
- }
1510
- const ATTRIBUTE_DIRECTION = "dir";
1511
- const EXPRESSION_DIRECTION = /^(ltr|rtl)$/i;
1503
+ window.templates = templates;
1512
1504
  //#endregion
1513
- export { findElement as $, findElement, findElements as $$, findElements, booleanAttributes, dispatch, findAncestor, findRelatives, getAttribute, getAttributes, getData, getDistance, getElementUnderPointer, getFocusable, getPosition, getStyle, getStyles, getTabbable, getTextDirection, html, isBadAttribute, isBooleanAttribute, isChildNode, isEmptyNonBooleanAttribute, isEventTarget, isFocusable, isHTMLOrSVGElement, isInDocument, isInvalidBooleanAttribute, isTabbable, off, on, sanitize, setAttribute, setAttributes, setData, setStyle, setStyles, supportsTouch, toggleStyles };
1505
+ export { findElement as $, findElement, findElements as $$, findElements, booleanAttributes, createElement, dispatch, findAncestor, findRelatives, getAttribute, getAttributes, getData, getDistance, getElementUnderPointer, getFocusable, getPosition, getProperties, getProperty, getStyle, getStyles, getTabbable, getTextDirection, html, isBadAttribute, isBooleanAttribute, isChildNode, isEventTarget, isFocusable, isHTMLOrSVGElement, isInDocument, isInvalidBooleanAttribute, isTabbable, off, on, sanitize, setAttribute, setAttributes, setData, setProperties, setProperty, setStyle, setStyles, supportsTouch, toggleStyles };