@oscarpalmer/abydon 0.12.0 → 0.14.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 (48) hide show
  1. package/dist/abydon.full.js +1153 -0
  2. package/dist/constants.js +10 -0
  3. package/dist/fragment.js +60 -0
  4. package/dist/helpers/dom.js +28 -0
  5. package/dist/helpers/index.js +11 -0
  6. package/dist/index.js +6 -911
  7. package/dist/node/attribute/index.js +39 -0
  8. package/dist/node/attribute/value.js +56 -0
  9. package/dist/node/event.js +30 -0
  10. package/dist/node/index.js +52 -0
  11. package/dist/node/value.js +108 -0
  12. package/dist/parse.js +18 -0
  13. package/package.json +38 -27
  14. package/src/constants.ts +20 -0
  15. package/src/fragment.ts +63 -36
  16. package/src/helpers/dom.ts +5 -4
  17. package/src/helpers/index.ts +0 -9
  18. package/src/index.ts +13 -3
  19. package/src/models.ts +14 -11
  20. package/src/node/attribute/index.ts +28 -22
  21. package/src/node/attribute/value.ts +38 -42
  22. package/src/node/event.ts +12 -13
  23. package/src/node/index.ts +7 -7
  24. package/src/node/value.ts +169 -122
  25. package/src/{html.ts → parse.ts} +15 -17
  26. package/types/constants.d.ts +9 -0
  27. package/types/fragment.d.ts +3 -5
  28. package/types/helpers/index.d.ts +0 -3
  29. package/types/index.d.ts +4 -2
  30. package/types/models.d.ts +7 -9
  31. package/types/node/attribute/index.d.ts +2 -1
  32. package/types/node/attribute/value.d.ts +2 -1
  33. package/types/node/event.d.ts +1 -1
  34. package/types/node/value.d.ts +2 -2
  35. package/types/parse.d.ts +2 -0
  36. package/dist/fragment.mjs +0 -75
  37. package/dist/helpers/dom.mjs +0 -44
  38. package/dist/helpers/index.mjs +0 -25
  39. package/dist/html.mjs +0 -36
  40. package/dist/index.mjs +0 -6
  41. package/dist/node/attribute/index.mjs +0 -40
  42. package/dist/node/attribute/value.mjs +0 -89
  43. package/dist/node/event.mjs +0 -38
  44. package/dist/node/index.mjs +0 -60
  45. package/dist/node/value.mjs +0 -123
  46. package/types/html.d.ts +0 -4
  47. package/types/index.d.cts +0 -26
  48. /package/dist/{models.mjs → models.js} +0 -0
package/dist/index.js CHANGED
@@ -1,912 +1,7 @@
1
- // node_modules/@oscarpalmer/sentinel/node_modules/@oscarpalmer/atoms/dist/js/queue.mjs
2
- function queue(callback) {
3
- _atomic_queued.add(callback);
4
- if (_atomic_queued.size > 0) {
5
- queueMicrotask(() => {
6
- const callbacks = [..._atomic_queued];
7
- const { length } = callbacks;
8
- _atomic_queued.clear();
9
- for (let index = 0;index < length; index += 1) {
10
- callbacks[index]();
11
- }
12
- });
13
- }
1
+ import { Fragment } from "./fragment.js";
2
+ import "@oscarpalmer/mora";
3
+ export * from "@oscarpalmer/mora";
4
+ function html(strings, ...values) {
5
+ return new Fragment(strings, values);
14
6
  }
15
- if (globalThis._atomic_queued == null) {
16
- const queued = new Set;
17
- Object.defineProperty(globalThis, "_atomic_queued", {
18
- get() {
19
- return queued;
20
- }
21
- });
22
- }
23
-
24
- // node_modules/@oscarpalmer/sentinel/dist/global.mjs
25
- if (globalThis._sentinels == null) {
26
- const effects = [];
27
- Object.defineProperty(globalThis, "_sentinels", {
28
- get() {
29
- return effects;
30
- }
31
- });
32
- }
33
-
34
- // node_modules/@oscarpalmer/sentinel/dist/effect.mjs
35
- function effect(callback) {
36
- return new Effect(callback);
37
- }
38
- function watch(reactive, key) {
39
- const effect2 = globalThis._sentinels[globalThis._sentinels.length - 1];
40
- if (effect2 != null) {
41
- if (key == null) {
42
- reactive.callbacks.any.add(effect2);
43
- } else {
44
- if (!reactive.callbacks.keys.has(key)) {
45
- reactive.callbacks.keys.add(key);
46
- reactive.callbacks.values.set(key, new Set);
47
- }
48
- reactive.callbacks.values.get(key)?.add(effect2);
49
- }
50
- effect2.reactives.add(reactive);
51
- }
52
- }
53
-
54
- class Effect {
55
- $sentinel = "effect";
56
- state;
57
- constructor(callback) {
58
- this.state = {
59
- callback,
60
- active: false,
61
- reactives: new Set
62
- };
63
- this.start();
64
- }
65
- start() {
66
- if (!this.state.active) {
67
- this.state.active = true;
68
- const index = globalThis._sentinels.push(this.state) - 1;
69
- this.state.callback();
70
- globalThis._sentinels.splice(index, 1);
71
- }
72
- }
73
- stop() {
74
- if (this.state.active) {
75
- this.state.active = false;
76
- for (const reactive of this.state.reactives) {
77
- reactive.callbacks.any.delete(this.state);
78
- for (const [key, keyed] of reactive.callbacks.values) {
79
- keyed.delete(this.state);
80
- if (keyed.size === 0) {
81
- reactive.callbacks.keys.delete(key);
82
- }
83
- }
84
- }
85
- this.state.reactives.clear();
86
- }
87
- }
88
- }
89
-
90
- // node_modules/@oscarpalmer/sentinel/dist/helpers/is.mjs
91
- function isReactive(value) {
92
- return isSentinel(value, /^array|computed|signal|store$/i);
93
- }
94
- function isSentinel(value, expression) {
95
- return expression.test(value?.$sentinel ?? "");
96
- }
97
-
98
- // node_modules/@oscarpalmer/sentinel/node_modules/@oscarpalmer/atoms/dist/js/function.mjs
99
- function noop() {
100
- }
101
-
102
- // node_modules/@oscarpalmer/sentinel/dist/helpers/subscription.mjs
103
- function subscribe(state, subscriber, key) {
104
- let set;
105
- if (key != null) {
106
- if (state.callbacks.keys.has(key)) {
107
- set = state.callbacks.values.get(key);
108
- } else {
109
- set = new Set;
110
- state.callbacks.keys.add(key);
111
- state.callbacks.values.set(key, set);
112
- }
113
- } else {
114
- set = state.callbacks.any;
115
- }
116
- if (set == null || set.has(subscriber)) {
117
- return noop;
118
- }
119
- set.add(subscriber);
120
- subscriber(state.value);
121
- return () => {
122
- unsubscribe(state, subscriber, key);
123
- };
124
- }
125
- function unsubscribe(state, subscriber, key) {
126
- if (key != null) {
127
- const set = state.callbacks.values.get(key);
128
- if (set != null) {
129
- if (subscriber == null) {
130
- set.clear();
131
- } else {
132
- set.delete(subscriber);
133
- }
134
- if (set.size === 0) {
135
- state.callbacks.keys.delete(key);
136
- state.callbacks.values.delete(key);
137
- }
138
- }
139
- } else if (subscriber != null) {
140
- state.callbacks.any.delete(subscriber);
141
- }
142
- }
143
-
144
- // node_modules/@oscarpalmer/sentinel/dist/helpers/event.mjs
145
- function disable(state) {
146
- if (state.active) {
147
- state.active = false;
148
- const effects = [...state.callbacks.any, ...state.callbacks.values.values()].flatMap((value) => value instanceof Set ? [...value.values()] : value).filter((value) => typeof value !== "function");
149
- for (const fx of effects) {
150
- if (typeof fx !== "function") {
151
- fx.reactives.delete(state);
152
- }
153
- }
154
- }
155
- }
156
- function emit(state, keys) {
157
- if (state.active) {
158
- const subscribers = [
159
- ...state.callbacks.any,
160
- ...[...state.callbacks.values.entries()].filter(([key]) => keys == null || keys.includes(key)).map(([, value]) => value)
161
- ].flatMap((value) => value instanceof Set ? [...value.values()] : value).map((value) => typeof value === "function" ? value : value.callback);
162
- for (const subsriber of subscribers) {
163
- if (typeof subsriber === "function") {
164
- queue(() => {
165
- subsriber(state.value);
166
- });
167
- }
168
- }
169
- }
170
- }
171
- function enable(state) {
172
- if (!state.active) {
173
- state.active = true;
174
- emit(state);
175
- }
176
- }
177
-
178
- // node_modules/@oscarpalmer/sentinel/dist/helpers/value.mjs
179
- function getValue(reactive, key) {
180
- watch(reactive, typeof key === "symbol" ? undefined : key);
181
- return key == null ? reactive.value : Array.isArray(reactive.value) ? reactive.value.at(key) : reactive.value[key];
182
- }
183
- function setValue(reactive, value) {
184
- if (!Object.is(reactive.value, value)) {
185
- reactive.value = value;
186
- emit(reactive);
187
- }
188
- }
189
- var arrayOperations = new Set([
190
- "copyWithin",
191
- "fill",
192
- "pop",
193
- "push",
194
- "reverse",
195
- "shift",
196
- "sort",
197
- "splice",
198
- "unshift"
199
- ]);
200
-
201
- // node_modules/@oscarpalmer/sentinel/dist/reactive/instance.mjs
202
- class ReactiveInstance {
203
- state;
204
- constructor(type, value2) {
205
- this.$sentinel = type;
206
- this.state = {
207
- value: value2,
208
- active: true,
209
- callbacks: {
210
- any: new Set,
211
- keys: new Set,
212
- values: new Map
213
- }
214
- };
215
- }
216
- get() {
217
- return getValue(this.state);
218
- }
219
- peek() {
220
- return this.state.value;
221
- }
222
- run() {
223
- enable(this.state);
224
- }
225
- stop() {
226
- disable(this.state);
227
- }
228
- toJSON() {
229
- return getValue(this.state);
230
- }
231
- toString() {
232
- return String(getValue(this.state));
233
- }
234
- }
235
-
236
- // node_modules/@oscarpalmer/sentinel/dist/reactive/value.mjs
237
- class ReactiveValue extends ReactiveInstance {
238
- subscribe(subscriber) {
239
- return subscribe(this.state, subscriber);
240
- }
241
- unsubscribe(subscriber) {
242
- if (subscriber == null) {
243
- this.state.callbacks.any.clear();
244
- } else {
245
- this.state.callbacks.any.delete(subscriber);
246
- }
247
- }
248
- }
249
-
250
- // node_modules/@oscarpalmer/sentinel/dist/reactive/computed.mjs
251
- function computed(value32) {
252
- return new Computed(value32);
253
- }
254
-
255
- class Computed extends ReactiveValue {
256
- fx;
257
- constructor(value32) {
258
- super("computed", undefined);
259
- this.fx = effect(() => setValue(this.state, value32()));
260
- }
261
- run() {
262
- this.fx.start();
263
- }
264
- stop() {
265
- this.fx.stop();
266
- }
267
- }
268
- // node_modules/@oscarpalmer/sentinel/dist/reactive/index.mjs
269
- var primitives = new Set(["boolean", "number", "string"]);
270
- // node_modules/@oscarpalmer/atoms/dist/js/string/index.mjs
271
- function getString2(value10) {
272
- if (typeof value10 === "string") {
273
- return value10;
274
- }
275
- if (typeof value10 !== "object" || value10 == null) {
276
- return String(value10);
277
- }
278
- const valueOff = value10.valueOf?.() ?? value10;
279
- const asString = valueOff?.toString?.() ?? String(valueOff);
280
- return asString.startsWith("[object ") ? JSON.stringify(value10) : asString;
281
- }
282
- // node_modules/@oscarpalmer/atoms/dist/js/is.mjs
283
- function isNullableOrWhitespace(value10) {
284
- return value10 == null || /^\s*$/.test(getString2(value10));
285
- }
286
- // node_modules/@oscarpalmer/toretto/node_modules/@oscarpalmer/atoms/dist/js/string/index.mjs
287
- function getString3(value11) {
288
- if (typeof value11 === "string") {
289
- return value11;
290
- }
291
- if (typeof value11 !== "object" || value11 == null) {
292
- return String(value11);
293
- }
294
- const valueOff = value11.valueOf?.() ?? value11;
295
- const asString = valueOff?.toString?.() ?? String(valueOff);
296
- return asString.startsWith("[object ") ? JSON.stringify(value11) : asString;
297
- }
298
- // node_modules/@oscarpalmer/toretto/node_modules/@oscarpalmer/atoms/dist/js/is.mjs
299
- function isPlainObject3(value11) {
300
- if (typeof value11 !== "object" || value11 === null) {
301
- return false;
302
- }
303
- const prototype = Object.getPrototypeOf(value11);
304
- return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value11) && !(Symbol.iterator in value11);
305
- }
306
-
307
- // node_modules/@oscarpalmer/toretto/dist/attribute.mjs
308
- function isBadAttribute(attribute) {
309
- return onPrefix.test(attribute.name) || sourcePrefix.test(attribute.name) && valuePrefix.test(attribute.value);
310
- }
311
- function isBooleanAttribute(name) {
312
- return booleanAttributes.includes(name.toLowerCase());
313
- }
314
- function isEmptyNonBooleanAttribute(attribute) {
315
- return !booleanAttributes.includes(attribute.name) && attribute.value.trim().length === 0;
316
- }
317
- function isInvalidBooleanAttribute(attribute) {
318
- if (!booleanAttributes.includes(attribute.name)) {
319
- return true;
320
- }
321
- const normalised = attribute.value.toLowerCase().trim();
322
- return !(normalised.length === 0 || normalised === attribute.name || attribute.name === "hidden" && normalised === "until-found");
323
- }
324
- function setAttribute(element, first, second) {
325
- if (isPlainObject3(first) && typeof first?.name === "string") {
326
- setAttributeValue(element, first.name, first.value);
327
- } else if (typeof first === "string") {
328
- setAttributeValue(element, first, second);
329
- }
330
- }
331
- function setAttributeValue(element, name, value11) {
332
- if (value11 == null) {
333
- element.removeAttribute(name);
334
- } else {
335
- element.setAttribute(name, typeof value11 === "string" ? value11 : getString3(value11));
336
- }
337
- }
338
- var booleanAttributes = Object.freeze([
339
- "async",
340
- "autofocus",
341
- "autoplay",
342
- "checked",
343
- "controls",
344
- "default",
345
- "defer",
346
- "disabled",
347
- "formnovalidate",
348
- "hidden",
349
- "inert",
350
- "ismap",
351
- "itemscope",
352
- "loop",
353
- "multiple",
354
- "muted",
355
- "nomodule",
356
- "novalidate",
357
- "open",
358
- "playsinline",
359
- "readonly",
360
- "required",
361
- "reversed",
362
- "selected"
363
- ]);
364
- var onPrefix = /^on/i;
365
- var sourcePrefix = /^(href|src|xlink:href)$/i;
366
- var valuePrefix = /(data:text\/html|javascript:)/i;
367
-
368
- // node_modules/@oscarpalmer/toretto/dist/sanitise.mjs
369
- function sanitise(value11, options) {
370
- return sanitiseNodes(Array.isArray(value11) ? value11 : [value11], {
371
- sanitiseBooleanAttributes: options?.sanitiseBooleanAttributes ?? true
372
- });
373
- }
374
- function sanitiseAttributes(element, attributes, options) {
375
- const { length } = attributes;
376
- for (let index = 0;index < length; index += 1) {
377
- const attribute2 = attributes[index];
378
- if (isBadAttribute(attribute2) || isEmptyNonBooleanAttribute(attribute2)) {
379
- element.removeAttribute(attribute2.name);
380
- } else if (options.sanitiseBooleanAttributes && isInvalidBooleanAttribute(attribute2)) {
381
- element.setAttribute(attribute2.name, "");
382
- }
383
- }
384
- }
385
- function sanitiseNodes(nodes, options) {
386
- const { length } = nodes;
387
- for (let index = 0;index < length; index += 1) {
388
- const node = nodes[index];
389
- if (node instanceof Element) {
390
- sanitiseAttributes(node, [...node.attributes], options);
391
- }
392
- sanitiseNodes([...node.childNodes], options);
393
- }
394
- return nodes;
395
- }
396
-
397
- // node_modules/@oscarpalmer/toretto/dist/html.mjs
398
- function createTemplate(html) {
399
- const template4 = document.createElement("template");
400
- template4.innerHTML = html;
401
- templates[html] = template4;
402
- return template4;
403
- }
404
- function getTemplate(value11) {
405
- if (value11.trim().length === 0) {
406
- return;
407
- }
408
- let template4;
409
- if (/^[\w-]+$/.test(value11)) {
410
- template4 = document.querySelector(`#${value11}`);
411
- }
412
- if (template4 instanceof HTMLTemplateElement) {
413
- return template4;
414
- }
415
- return templates[value11] ?? createTemplate(value11);
416
- }
417
- function html(value11, sanitisation) {
418
- const options = sanitisation == null || sanitisation === true ? {} : isPlainObject3(sanitisation) ? { ...sanitisation } : null;
419
- const template4 = value11 instanceof HTMLTemplateElement ? value11 : typeof value11 === "string" ? getTemplate(value11) : null;
420
- if (template4 == null) {
421
- return [];
422
- }
423
- const cloned = template4.content.cloneNode(true);
424
- const scripts = cloned.querySelectorAll("script");
425
- const { length } = scripts;
426
- for (let index = 0;index < length; index += 1) {
427
- scripts[index].remove();
428
- }
429
- cloned.normalize();
430
- return options != null ? sanitise([...cloned.childNodes], options) : [...cloned.childNodes];
431
- }
432
- var templates = {};
433
-
434
- // src/helpers/index.ts
435
- function compareArrays(first, second) {
436
- const firstIsLarger = first.length > second.length;
437
- const from = firstIsLarger ? first : second;
438
- const to = firstIsLarger ? second : first;
439
- if (!from.filter((key) => to.includes(key)).every((key, index) => to[index] === key)) {
440
- return "dissimilar";
441
- }
442
- return firstIsLarger ? "removed" : "added";
443
- }
444
- function isChildNode(value11) {
445
- return value11 instanceof CharacterData || value11 instanceof Element;
446
- }
447
- function isFragment(value11) {
448
- return typeof value11 === "object" && value11 != null && "$fragment" in value11 && value11.$fragment === true;
449
- }
450
- function isHTMLOrSVGElement(value11) {
451
- return value11 instanceof HTMLElement || value11 instanceof SVGElement;
452
- }
453
-
454
- // src/node/event.ts
455
- function getController(element) {
456
- let controller = controllers.get(element);
457
- if (controller == null) {
458
- controller = new AbortController;
459
- controllers.set(element, controller);
460
- }
461
- return controller;
462
- }
463
- function getOptions(options) {
464
- const parts = options.split(":");
465
- return {
466
- capture: parts.includes("c") || parts.includes("capture"),
467
- once: parts.includes("o") || parts.includes("once"),
468
- passive: !parts.includes("a") && !parts.includes("active")
469
- };
470
- }
471
- function mapEvent(element, name, value11) {
472
- element.removeAttribute(name);
473
- const [, type, options] = eventNamePattern.exec(name) ?? [];
474
- if (typeof value11 === "function" && type != null) {
475
- element.addEventListener(type, value11, {
476
- ...getOptions(options ?? ""),
477
- signal: getController(element).signal
478
- });
479
- }
480
- }
481
- function removeEvents(element) {
482
- controllers.get(element)?.abort(reason);
483
- controllers.delete(element);
484
- }
485
- var controllers = new WeakMap;
486
- var eventNamePattern = /^@([\w-]+)(?::([a-z:]+))?$/i;
487
- var reason = "Event removed as element was removed from document by Abydon";
488
-
489
- // src/helpers/dom.ts
490
- function createNodes(value11) {
491
- if (isFragment(value11)) {
492
- return value11.get();
493
- }
494
- if (isChildNode(value11)) {
495
- return [value11];
496
- }
497
- return [new Text(getString2(value11))];
498
- }
499
- function removeNodes(nodes) {
500
- sanitiseNodes2(nodes);
501
- const { length } = nodes;
502
- for (let index = 0;index < length; index += 1) {
503
- nodes[index].remove();
504
- }
505
- }
506
- function replaceNodes(from, to) {
507
- from[0]?.replaceWith(...to);
508
- const { length } = from;
509
- for (let index = 1;index < length; index += 1) {
510
- from[index].remove();
511
- }
512
- }
513
- function sanitiseNodes2(nodes) {
514
- const { length } = nodes;
515
- for (let index = 0;index < length; index += 1) {
516
- const node = nodes[index];
517
- if (isHTMLOrSVGElement(node)) {
518
- removeEvents(node);
519
- }
520
- if (node.hasChildNodes()) {
521
- sanitiseNodes2([...node.childNodes]);
522
- }
523
- }
524
- }
525
-
526
- // src/node/attribute/value.ts
527
- function setAttribute2(data, element, name, value11) {
528
- element.removeAttribute(name);
529
- switch (true) {
530
- case classExpression.test(name):
531
- setClasses(data, element, name, value11);
532
- return;
533
- case stylePrefixExpression.test(name):
534
- setStyle(data, element, name, value11);
535
- return;
536
- default:
537
- setValue5(data, element, name, value11);
538
- break;
539
- }
540
- }
541
- function setClasses(data, element, name, value11) {
542
- function update(value12) {
543
- if (value12 === true) {
544
- element.classList.add(...classes);
545
- } else {
546
- element.classList.remove(...classes);
547
- }
548
- }
549
- const classes = name.slice(6).split(".");
550
- if (isReactive(value11)) {
551
- data.sentinel.effects.add(effect(() => {
552
- update(value11.get());
553
- }));
554
- } else {
555
- update(value11);
556
- }
557
- }
558
- function setStyle(data, element, name, value11) {
559
- function update(value12) {
560
- if (value12 == null || value12 === false || value12 === true && unit == null) {
561
- element.style.removeProperty(property);
562
- } else {
563
- element.style.setProperty(property, value12 === true ? unit : getString2(value12));
564
- }
565
- }
566
- const [, property, unit] = styleFullExpression.exec(name) ?? [];
567
- if (property != null) {
568
- if (isReactive(value11)) {
569
- data.sentinel.effects.add(effect(() => {
570
- update(value11.get());
571
- }));
572
- } else {
573
- update(value11);
574
- }
575
- }
576
- }
577
- function setValue5(data, element, name, value11) {
578
- const callback = isBooleanAttribute(name) && name in element ? name === "selected" ? updateSelected : updateProperty : setAttribute;
579
- if (isReactive(value11)) {
580
- data.sentinel.effects.add(effect(() => {
581
- callback(element, name, value11.get());
582
- }));
583
- } else {
584
- callback(element, name, value11);
585
- }
586
- }
587
- function triggerSelectChange(select) {
588
- if (select != null) {
589
- select.dispatchEvent(new Event("change", { bubbles: true }));
590
- }
591
- }
592
- function updateProperty(element, name, value11) {
593
- element[name] = value11 === true;
594
- }
595
- function updateSelected(element, name, value11) {
596
- const select = element.closest("select");
597
- const options = [...select?.options ?? []];
598
- if (select != null && options.includes(element)) {
599
- triggerSelectChange(select);
600
- }
601
- updateProperty(element, name, value11);
602
- }
603
- var classExpression = /^class\./;
604
- var styleFullExpression = /^style\.([\w-]+)(?:\.([\w-]+))?$/;
605
- var stylePrefixExpression = /^style\./;
606
-
607
- // src/node/attribute/index.ts
608
- function getValue5(data, original) {
609
- const matches = commentExpression.exec(original ?? "");
610
- return matches == null ? original : data.values[+matches[1]];
611
- }
612
- function mapAttributes(data, element) {
613
- const attributes = [...element.attributes];
614
- const { length } = attributes;
615
- for (let index = 0;index < length; index += 1) {
616
- const { name, value: value12 } = attributes[index];
617
- const actual = getValue5(data, value12);
618
- if (name.startsWith("@")) {
619
- mapEvent(element, name, actual);
620
- } else if (name.includes(".") || typeof value12 === "function" || isReactive(actual)) {
621
- mapValue(data, element, name, actual);
622
- }
623
- }
624
- }
625
- function mapValue(data, element, name, value12) {
626
- switch (true) {
627
- case typeof value12 === "function":
628
- setComputedAttribute(data, element, name, value12);
629
- break;
630
- default:
631
- setAttribute2(data, element, name, value12);
632
- break;
633
- }
634
- }
635
- function setComputedAttribute(data, element, name, callback) {
636
- const value12 = computed(callback);
637
- data.sentinel.values.add(value12);
638
- setAttribute2(data, element, name, value12);
639
- }
640
- var commentExpression = /^<!--abydon\.(\d+)-->$/;
641
-
642
- // src/node/value.ts
643
- function removeFragments(fragments) {
644
- if (fragments != null) {
645
- const { length } = fragments;
646
- for (let index = 0;index < length; index += 1) {
647
- fragments[index].remove();
648
- }
649
- }
650
- }
651
- function setArray(fragments, nodes, comment, text, value12) {
652
- if (value12.length === 0) {
653
- return {
654
- nodes: setText(fragments, nodes, comment, text, value12)
655
- };
656
- }
657
- let templates2 = value12.filter((item) => isFragment(item) && item.identifier != null);
658
- const identifiers = templates2.map((fragment) => fragment.identifier);
659
- const oldIdentifiers = fragments?.map((fragment) => fragment.identifier) ?? [];
660
- if (new Set(identifiers).size !== templates2.length) {
661
- templates2 = [];
662
- }
663
- const noTemplates = templates2.length === 0;
664
- if (noTemplates || nodes == null || oldIdentifiers.some((identifier) => identifier == null)) {
665
- return {
666
- fragments: noTemplates ? undefined : templates2,
667
- nodes: setNodes(fragments, nodes, comment, text, noTemplates ? value12.flatMap((item) => createNodes(item)) : templates2.flatMap((template4) => template4.get()))
668
- };
669
- }
670
- const next = templates2.map((template4) => fragments?.find((fragment) => fragment.identifier === template4.identifier) ?? template4);
671
- const comparison = compareArrays(fragments ?? [], templates2);
672
- if (comparison !== "removed") {
673
- let position = nodes[0];
674
- const before = comparison === "added" && !oldIdentifiers.includes(templates2[0].identifier);
675
- const items = next.flatMap((fragment) => fragment.get().flatMap((node) => ({
676
- identifier: fragment.identifier,
677
- value: node
678
- })));
679
- const { length: length2 } = items;
680
- for (let index = 0;index < length2; index += 1) {
681
- const item = items[index];
682
- if (comparison === "dissimilar" || !oldIdentifiers.includes(item.identifier)) {
683
- if (index === 0 && before) {
684
- position.before(item.value);
685
- } else {
686
- position.after(item.value);
687
- }
688
- }
689
- position = item.value;
690
- }
691
- }
692
- const toRemove = fragments?.filter((fragment) => !identifiers.includes(fragment.identifier)) ?? [];
693
- const { length } = toRemove;
694
- for (let index = 0;index < length; index += 1) {
695
- toRemove[index].remove();
696
- }
697
- return {
698
- fragments: next,
699
- nodes: next.flatMap((fragment) => fragment.get())
700
- };
701
- }
702
- function setNodes(fragments, nodes, comment, text, next) {
703
- if (nodes == null) {
704
- if (comment.parentNode != null) {
705
- replaceNodes([comment], next);
706
- } else if (text.parentNode != null) {
707
- replaceNodes([text], next);
708
- }
709
- } else {
710
- replaceNodes(nodes, next);
711
- }
712
- removeFragments(fragments);
713
- return next;
714
- }
715
- function setReactiveValue(data, comment, reactive3) {
716
- const item = data.items.find((item2) => item2.nodes.includes(comment));
717
- const text = new Text;
718
- let fragments;
719
- let nodes;
720
- data.sentinel.effects.add(effect(() => {
721
- const value12 = reactive3.get();
722
- if (Array.isArray(value12)) {
723
- const result = setArray(fragments, nodes, comment, text, value12);
724
- fragments = typeof result === "boolean" ? undefined : result?.fragments;
725
- nodes = typeof result === "boolean" ? result ? [text] : undefined : result?.nodes;
726
- } else {
727
- const valueIsFragment = isFragment(value12);
728
- fragments = valueIsFragment ? [value12] : undefined;
729
- if (valueIsFragment || isChildNode(value12)) {
730
- nodes = setNodes(fragments, nodes, comment, text, createNodes(value12));
731
- } else {
732
- nodes = setText(fragments, nodes, comment, text, value12);
733
- }
734
- }
735
- if (item != null) {
736
- item.fragments = fragments;
737
- item.nodes = [...nodes ?? [comment]];
738
- }
739
- }));
740
- }
741
- function setText(fragments, nodes, comment, text, value12) {
742
- const isNullable = isNullableOrWhitespace(value12);
743
- text.textContent = isNullable ? "" : getString2(value12);
744
- let result = false;
745
- if (nodes != null) {
746
- replaceNodes(nodes, [isNullable ? comment : text]);
747
- result = !isNullable;
748
- } else if (isNullable && comment.parentNode == null) {
749
- text.replaceWith(comment);
750
- } else if (!isNullable && text.parentNode == null) {
751
- comment.replaceWith(text);
752
- result = true;
753
- }
754
- removeFragments(fragments);
755
- return result ? [text] : undefined;
756
- }
757
-
758
- // src/node/index.ts
759
- function mapNode(data, comment) {
760
- const matches = commentExpression2.exec(comment.textContent ?? "");
761
- const value13 = matches == null ? null : data.values[+matches[1]];
762
- if (value13 != null) {
763
- mapValue2(data, comment, value13);
764
- }
765
- }
766
- function mapNodes(data, nodes) {
767
- const { length } = nodes;
768
- for (let index = 0;index < length; index += 1) {
769
- const node = nodes[index];
770
- if (node instanceof Comment) {
771
- mapNode(data, node);
772
- continue;
773
- }
774
- if (isHTMLOrSVGElement(node)) {
775
- mapAttributes(data, node);
776
- }
777
- if (node.hasChildNodes()) {
778
- mapNodes(data, [...node.childNodes]);
779
- }
780
- }
781
- }
782
- function mapValue2(data, comment, value13) {
783
- switch (true) {
784
- case typeof value13 === "function":
785
- setComputedValue(data, comment, value13);
786
- break;
787
- case isReactive(value13):
788
- setReactiveValue(data, comment, value13);
789
- break;
790
- default:
791
- replaceComment(data, comment, value13);
792
- break;
793
- }
794
- }
795
- function replaceComment(data, comment, value13) {
796
- const item = data.items.find((item2) => item2.nodes.includes(comment));
797
- const nodes = createNodes(value13);
798
- if (item != null) {
799
- item.fragments = isFragment(value13) ? [value13] : undefined;
800
- item.nodes = nodes;
801
- }
802
- comment.replaceWith(...nodes);
803
- }
804
- function setComputedValue(data, comment, callback) {
805
- const value13 = computed(callback);
806
- data.sentinel.values.add(value13);
807
- setReactiveValue(data, comment, value13);
808
- }
809
- var commentExpression2 = /^abydon\.(\d+)$/;
810
-
811
- // src/fragment.ts
812
- function removeFragment(data) {
813
- removeSentinels(data);
814
- const { length } = data.items;
815
- for (let index = 0;index < length; index += 1) {
816
- const { fragments, nodes } = data.items[index];
817
- const { length: length2 } = fragments ?? [];
818
- for (let index2 = 0;index2 < length2; index2 += 1) {
819
- fragments?.[index2]?.remove();
820
- }
821
- removeNodes(nodes);
822
- }
823
- data.items.splice(0, length);
824
- }
825
- function removeSentinels(data) {
826
- const sentinels = [...data.sentinel.effects, ...data.sentinel.values];
827
- const { length } = sentinels;
828
- for (let index = 0;index < length; index += 1) {
829
- sentinels[index].stop();
830
- }
831
- data.sentinel.effects.clear();
832
- data.sentinel.values.clear();
833
- }
834
-
835
- class Fragment {
836
- $fragment = true;
837
- data;
838
- get identifier() {
839
- return this.data.identifier;
840
- }
841
- constructor(strings, expressions) {
842
- this.data = {
843
- expressions,
844
- strings,
845
- items: [],
846
- sentinel: {
847
- effects: new Set,
848
- values: new Set
849
- },
850
- values: []
851
- };
852
- }
853
- appendTo(element) {
854
- element.append(...this.get());
855
- }
856
- get() {
857
- if (this.data.items.length === 0) {
858
- const parsed = parse(this.data);
859
- const templated = html(parsed, {
860
- sanitiseBooleanAttributes: false
861
- });
862
- this.data.items.splice(0, this.data.items.length, ...templated.map((node2) => ({
863
- nodes: [node2]
864
- })));
865
- mapNodes(this.data, this.data.items.flatMap((item) => item.fragments?.flatMap((fragment) => fragment.get()) ?? item.nodes));
866
- }
867
- return [
868
- ...this.data.items.flatMap((item) => item.fragments?.flatMap((fragment) => fragment.get()) ?? item.nodes)
869
- ];
870
- }
871
- identify(identifier) {
872
- this.data.identifier = identifier;
873
- return this;
874
- }
875
- remove() {
876
- removeFragment(this.data);
877
- }
878
- }
879
-
880
- // src/html.ts
881
- function handleExpression(data, prefix, expression) {
882
- if (isNullableOrWhitespace(expression)) {
883
- return prefix;
884
- }
885
- if (Array.isArray(expression)) {
886
- const { length } = expression;
887
- let expressions = "";
888
- for (let index = 0;index < length; index += 1) {
889
- expressions += handleExpression(data, "", expression[index]);
890
- }
891
- return `${prefix}${expressions}`;
892
- }
893
- if (typeof expression === "function" || typeof expression === "object") {
894
- const index = data.values.push(expression) - 1;
895
- return `${prefix}<!--abydon.${index}-->`;
896
- }
897
- return `${prefix}${expression}`;
898
- }
899
- function html4(strings, ...values) {
900
- return new Fragment(strings, values);
901
- }
902
- function parse(data) {
903
- const { length } = data.strings;
904
- let template4 = "";
905
- for (let index = 0;index < length; index += 1) {
906
- template4 += handleExpression(data, data.strings[index], data.expressions[index]);
907
- }
908
- return template4;
909
- }
910
- export {
911
- html4 as html
912
- };
7
+ export { html };