jails-js 5.8.7 → 6.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1125 -0
- package/dist/index.js.map +1 -0
- package/dist/jails.js +1 -1
- package/dist/jails.js.map +1 -1
- package/{html.ts → html.js} +0 -1
- package/package.json +6 -6
- package/readme.md +1 -1
- package/src/component.ts +130 -102
- package/src/element.ts +21 -55
- package/src/index.ts +15 -30
- package/src/template-system.ts +162 -45
- package/src/utils/index.ts +11 -5
- package/src/utils/pubsub.ts +1 -1
- package/vite.config.ts +4 -2
- package/index.d.ts +0 -65
- package/logo.svg +0 -29
- package/src/transpile.ts +0 -79
- package/src/utils/events.ts +0 -86
- package/src/utils/hmr-register.ts +0 -41
package/dist/index.js
ADDED
@@ -0,0 +1,1125 @@
|
|
1
|
+
document.createElement("textarea");
|
2
|
+
const g = {
|
3
|
+
scope: {}
|
4
|
+
};
|
5
|
+
const rAF = (fn) => {
|
6
|
+
if (requestAnimationFrame)
|
7
|
+
return requestAnimationFrame(fn);
|
8
|
+
else
|
9
|
+
return setTimeout(fn, 1e3 / 60);
|
10
|
+
};
|
11
|
+
const uuid = () => {
|
12
|
+
return Math.random().toString(36).substring(2, 9);
|
13
|
+
};
|
14
|
+
const safe = (execute, val) => {
|
15
|
+
try {
|
16
|
+
return execute();
|
17
|
+
} catch (err) {
|
18
|
+
return val || "";
|
19
|
+
}
|
20
|
+
};
|
21
|
+
var Idiomorph = /* @__PURE__ */ function() {
|
22
|
+
let EMPTY_SET = /* @__PURE__ */ new Set();
|
23
|
+
let defaults = {
|
24
|
+
morphStyle: "outerHTML",
|
25
|
+
callbacks: {
|
26
|
+
beforeNodeAdded: noOp,
|
27
|
+
afterNodeAdded: noOp,
|
28
|
+
beforeNodeMorphed: noOp,
|
29
|
+
afterNodeMorphed: noOp,
|
30
|
+
beforeNodeRemoved: noOp,
|
31
|
+
afterNodeRemoved: noOp,
|
32
|
+
beforeAttributeUpdated: noOp,
|
33
|
+
beforeNodePantried: noOp
|
34
|
+
},
|
35
|
+
head: {
|
36
|
+
style: "merge",
|
37
|
+
shouldPreserve: function(elt) {
|
38
|
+
return elt.getAttribute("im-preserve") === "true";
|
39
|
+
},
|
40
|
+
shouldReAppend: function(elt) {
|
41
|
+
return elt.getAttribute("im-re-append") === "true";
|
42
|
+
},
|
43
|
+
shouldRemove: noOp,
|
44
|
+
afterHeadMorphed: noOp
|
45
|
+
}
|
46
|
+
};
|
47
|
+
function morph(oldNode, newContent, config2 = {}) {
|
48
|
+
if (oldNode instanceof Document) {
|
49
|
+
oldNode = oldNode.documentElement;
|
50
|
+
}
|
51
|
+
if (typeof newContent === "string") {
|
52
|
+
newContent = parseContent(newContent);
|
53
|
+
}
|
54
|
+
let normalizedContent = normalizeContent(newContent);
|
55
|
+
let ctx = createMorphContext(oldNode, normalizedContent, config2);
|
56
|
+
return morphNormalizedContent(oldNode, normalizedContent, ctx);
|
57
|
+
}
|
58
|
+
function morphNormalizedContent(oldNode, normalizedNewContent, ctx) {
|
59
|
+
var _a, _b;
|
60
|
+
if (ctx.head.block) {
|
61
|
+
let oldHead = oldNode.querySelector("head");
|
62
|
+
let newHead = normalizedNewContent.querySelector("head");
|
63
|
+
if (oldHead && newHead) {
|
64
|
+
let promises = handleHeadElement(newHead, oldHead, ctx);
|
65
|
+
Promise.all(promises).then(function() {
|
66
|
+
morphNormalizedContent(
|
67
|
+
oldNode,
|
68
|
+
normalizedNewContent,
|
69
|
+
Object.assign(ctx, {
|
70
|
+
head: {
|
71
|
+
block: false,
|
72
|
+
ignore: true
|
73
|
+
}
|
74
|
+
})
|
75
|
+
);
|
76
|
+
});
|
77
|
+
return;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
if (ctx.morphStyle === "innerHTML") {
|
81
|
+
morphChildren(normalizedNewContent, oldNode, ctx);
|
82
|
+
if (ctx.config.twoPass) {
|
83
|
+
restoreFromPantry(oldNode, ctx);
|
84
|
+
}
|
85
|
+
return Array.from(oldNode.children);
|
86
|
+
} else if (ctx.morphStyle === "outerHTML" || ctx.morphStyle == null) {
|
87
|
+
let bestMatch = findBestNodeMatch(normalizedNewContent, oldNode, ctx);
|
88
|
+
let previousSibling = (_a = bestMatch == null ? void 0 : bestMatch.previousSibling) != null ? _a : null;
|
89
|
+
let nextSibling = (_b = bestMatch == null ? void 0 : bestMatch.nextSibling) != null ? _b : null;
|
90
|
+
let morphedNode = morphOldNodeTo(oldNode, bestMatch, ctx);
|
91
|
+
if (bestMatch) {
|
92
|
+
if (morphedNode) {
|
93
|
+
const elements = insertSiblings(
|
94
|
+
previousSibling,
|
95
|
+
morphedNode,
|
96
|
+
nextSibling
|
97
|
+
);
|
98
|
+
if (ctx.config.twoPass) {
|
99
|
+
restoreFromPantry(morphedNode.parentNode, ctx);
|
100
|
+
}
|
101
|
+
return elements;
|
102
|
+
}
|
103
|
+
} else {
|
104
|
+
return [];
|
105
|
+
}
|
106
|
+
} else {
|
107
|
+
throw "Do not understand how to morph style " + ctx.morphStyle;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
function ignoreValueOfActiveElement(possibleActiveElement, ctx) {
|
111
|
+
return !!ctx.ignoreActiveValue && possibleActiveElement === document.activeElement && possibleActiveElement !== document.body;
|
112
|
+
}
|
113
|
+
function morphOldNodeTo(oldNode, newContent, ctx) {
|
114
|
+
var _a, _b;
|
115
|
+
if (ctx.ignoreActive && oldNode === document.activeElement) ;
|
116
|
+
else if (newContent == null) {
|
117
|
+
if (ctx.callbacks.beforeNodeRemoved(oldNode) === false) return oldNode;
|
118
|
+
(_a = oldNode.parentNode) == null ? void 0 : _a.removeChild(oldNode);
|
119
|
+
ctx.callbacks.afterNodeRemoved(oldNode);
|
120
|
+
return null;
|
121
|
+
} else if (!isSoftMatch(oldNode, newContent)) {
|
122
|
+
if (ctx.callbacks.beforeNodeRemoved(oldNode) === false) return oldNode;
|
123
|
+
if (ctx.callbacks.beforeNodeAdded(newContent) === false) return oldNode;
|
124
|
+
(_b = oldNode.parentNode) == null ? void 0 : _b.replaceChild(newContent, oldNode);
|
125
|
+
ctx.callbacks.afterNodeAdded(newContent);
|
126
|
+
ctx.callbacks.afterNodeRemoved(oldNode);
|
127
|
+
return newContent;
|
128
|
+
} else {
|
129
|
+
if (ctx.callbacks.beforeNodeMorphed(oldNode, newContent) === false)
|
130
|
+
return oldNode;
|
131
|
+
if (oldNode instanceof HTMLHeadElement && ctx.head.ignore) ;
|
132
|
+
else if (oldNode instanceof HTMLHeadElement && ctx.head.style !== "morph") {
|
133
|
+
handleHeadElement(
|
134
|
+
/** @type {HTMLHeadElement} */
|
135
|
+
newContent,
|
136
|
+
oldNode,
|
137
|
+
ctx
|
138
|
+
);
|
139
|
+
} else {
|
140
|
+
syncNodeFrom(newContent, oldNode, ctx);
|
141
|
+
if (!ignoreValueOfActiveElement(oldNode, ctx)) {
|
142
|
+
morphChildren(newContent, oldNode, ctx);
|
143
|
+
}
|
144
|
+
}
|
145
|
+
ctx.callbacks.afterNodeMorphed(oldNode, newContent);
|
146
|
+
return oldNode;
|
147
|
+
}
|
148
|
+
return null;
|
149
|
+
}
|
150
|
+
function morphChildren(newParent, oldParent, ctx) {
|
151
|
+
if (newParent instanceof HTMLTemplateElement && oldParent instanceof HTMLTemplateElement) {
|
152
|
+
newParent = newParent.content;
|
153
|
+
oldParent = oldParent.content;
|
154
|
+
}
|
155
|
+
let nextNewChild = newParent.firstChild;
|
156
|
+
let insertionPoint = oldParent.firstChild;
|
157
|
+
let newChild;
|
158
|
+
while (nextNewChild) {
|
159
|
+
newChild = nextNewChild;
|
160
|
+
nextNewChild = newChild.nextSibling;
|
161
|
+
if (insertionPoint == null) {
|
162
|
+
if (ctx.config.twoPass && ctx.persistentIds.has(
|
163
|
+
/** @type {Element} */
|
164
|
+
newChild.id
|
165
|
+
)) {
|
166
|
+
oldParent.appendChild(newChild);
|
167
|
+
} else {
|
168
|
+
if (ctx.callbacks.beforeNodeAdded(newChild) === false) continue;
|
169
|
+
oldParent.appendChild(newChild);
|
170
|
+
ctx.callbacks.afterNodeAdded(newChild);
|
171
|
+
}
|
172
|
+
removeIdsFromConsideration(ctx, newChild);
|
173
|
+
continue;
|
174
|
+
}
|
175
|
+
if (isIdSetMatch(newChild, insertionPoint, ctx)) {
|
176
|
+
morphOldNodeTo(insertionPoint, newChild, ctx);
|
177
|
+
insertionPoint = insertionPoint.nextSibling;
|
178
|
+
removeIdsFromConsideration(ctx, newChild);
|
179
|
+
continue;
|
180
|
+
}
|
181
|
+
let idSetMatch = findIdSetMatch(
|
182
|
+
newParent,
|
183
|
+
oldParent,
|
184
|
+
newChild,
|
185
|
+
insertionPoint,
|
186
|
+
ctx
|
187
|
+
);
|
188
|
+
if (idSetMatch) {
|
189
|
+
insertionPoint = removeNodesBetween(insertionPoint, idSetMatch, ctx);
|
190
|
+
morphOldNodeTo(idSetMatch, newChild, ctx);
|
191
|
+
removeIdsFromConsideration(ctx, newChild);
|
192
|
+
continue;
|
193
|
+
}
|
194
|
+
let softMatch = findSoftMatch(
|
195
|
+
newParent,
|
196
|
+
oldParent,
|
197
|
+
newChild,
|
198
|
+
insertionPoint,
|
199
|
+
ctx
|
200
|
+
);
|
201
|
+
if (softMatch) {
|
202
|
+
insertionPoint = removeNodesBetween(insertionPoint, softMatch, ctx);
|
203
|
+
morphOldNodeTo(softMatch, newChild, ctx);
|
204
|
+
removeIdsFromConsideration(ctx, newChild);
|
205
|
+
continue;
|
206
|
+
}
|
207
|
+
if (ctx.config.twoPass && ctx.persistentIds.has(
|
208
|
+
/** @type {Element} */
|
209
|
+
newChild.id
|
210
|
+
)) {
|
211
|
+
oldParent.insertBefore(newChild, insertionPoint);
|
212
|
+
} else {
|
213
|
+
if (ctx.callbacks.beforeNodeAdded(newChild) === false) continue;
|
214
|
+
oldParent.insertBefore(newChild, insertionPoint);
|
215
|
+
ctx.callbacks.afterNodeAdded(newChild);
|
216
|
+
}
|
217
|
+
removeIdsFromConsideration(ctx, newChild);
|
218
|
+
}
|
219
|
+
while (insertionPoint !== null) {
|
220
|
+
let tempNode = insertionPoint;
|
221
|
+
insertionPoint = insertionPoint.nextSibling;
|
222
|
+
removeNode(tempNode, ctx);
|
223
|
+
}
|
224
|
+
}
|
225
|
+
function ignoreAttribute(attr, to, updateType, ctx) {
|
226
|
+
if (attr === "value" && ctx.ignoreActiveValue && to === document.activeElement) {
|
227
|
+
return true;
|
228
|
+
}
|
229
|
+
return ctx.callbacks.beforeAttributeUpdated(attr, to, updateType) === false;
|
230
|
+
}
|
231
|
+
function syncNodeFrom(from, to, ctx) {
|
232
|
+
let type = from.nodeType;
|
233
|
+
if (type === 1) {
|
234
|
+
const fromEl = (
|
235
|
+
/** @type {Element} */
|
236
|
+
from
|
237
|
+
);
|
238
|
+
const toEl = (
|
239
|
+
/** @type {Element} */
|
240
|
+
to
|
241
|
+
);
|
242
|
+
const fromAttributes = fromEl.attributes;
|
243
|
+
const toAttributes = toEl.attributes;
|
244
|
+
for (const fromAttribute of fromAttributes) {
|
245
|
+
if (ignoreAttribute(fromAttribute.name, toEl, "update", ctx)) {
|
246
|
+
continue;
|
247
|
+
}
|
248
|
+
if (toEl.getAttribute(fromAttribute.name) !== fromAttribute.value) {
|
249
|
+
toEl.setAttribute(fromAttribute.name, fromAttribute.value);
|
250
|
+
}
|
251
|
+
}
|
252
|
+
for (let i = toAttributes.length - 1; 0 <= i; i--) {
|
253
|
+
const toAttribute = toAttributes[i];
|
254
|
+
if (!toAttribute) continue;
|
255
|
+
if (!fromEl.hasAttribute(toAttribute.name)) {
|
256
|
+
if (ignoreAttribute(toAttribute.name, toEl, "remove", ctx)) {
|
257
|
+
continue;
|
258
|
+
}
|
259
|
+
toEl.removeAttribute(toAttribute.name);
|
260
|
+
}
|
261
|
+
}
|
262
|
+
}
|
263
|
+
if (type === 8 || type === 3) {
|
264
|
+
if (to.nodeValue !== from.nodeValue) {
|
265
|
+
to.nodeValue = from.nodeValue;
|
266
|
+
}
|
267
|
+
}
|
268
|
+
if (!ignoreValueOfActiveElement(to, ctx)) {
|
269
|
+
syncInputValue(from, to, ctx);
|
270
|
+
}
|
271
|
+
}
|
272
|
+
function syncBooleanAttribute(from, to, attributeName, ctx) {
|
273
|
+
if (!(from instanceof Element && to instanceof Element)) return;
|
274
|
+
const fromLiveValue = from[attributeName], toLiveValue = to[attributeName];
|
275
|
+
if (fromLiveValue !== toLiveValue) {
|
276
|
+
let ignoreUpdate = ignoreAttribute(attributeName, to, "update", ctx);
|
277
|
+
if (!ignoreUpdate) {
|
278
|
+
to[attributeName] = from[attributeName];
|
279
|
+
}
|
280
|
+
if (fromLiveValue) {
|
281
|
+
if (!ignoreUpdate) {
|
282
|
+
to.setAttribute(attributeName, fromLiveValue);
|
283
|
+
}
|
284
|
+
} else {
|
285
|
+
if (!ignoreAttribute(attributeName, to, "remove", ctx)) {
|
286
|
+
to.removeAttribute(attributeName);
|
287
|
+
}
|
288
|
+
}
|
289
|
+
}
|
290
|
+
}
|
291
|
+
function syncInputValue(from, to, ctx) {
|
292
|
+
if (from instanceof HTMLInputElement && to instanceof HTMLInputElement && from.type !== "file") {
|
293
|
+
let fromValue = from.value;
|
294
|
+
let toValue = to.value;
|
295
|
+
syncBooleanAttribute(from, to, "checked", ctx);
|
296
|
+
syncBooleanAttribute(from, to, "disabled", ctx);
|
297
|
+
if (!from.hasAttribute("value")) {
|
298
|
+
if (!ignoreAttribute("value", to, "remove", ctx)) {
|
299
|
+
to.value = "";
|
300
|
+
to.removeAttribute("value");
|
301
|
+
}
|
302
|
+
} else if (fromValue !== toValue) {
|
303
|
+
if (!ignoreAttribute("value", to, "update", ctx)) {
|
304
|
+
to.setAttribute("value", fromValue);
|
305
|
+
to.value = fromValue;
|
306
|
+
}
|
307
|
+
}
|
308
|
+
} else if (from instanceof HTMLOptionElement && to instanceof HTMLOptionElement) {
|
309
|
+
syncBooleanAttribute(from, to, "selected", ctx);
|
310
|
+
} else if (from instanceof HTMLTextAreaElement && to instanceof HTMLTextAreaElement) {
|
311
|
+
let fromValue = from.value;
|
312
|
+
let toValue = to.value;
|
313
|
+
if (ignoreAttribute("value", to, "update", ctx)) {
|
314
|
+
return;
|
315
|
+
}
|
316
|
+
if (fromValue !== toValue) {
|
317
|
+
to.value = fromValue;
|
318
|
+
}
|
319
|
+
if (to.firstChild && to.firstChild.nodeValue !== fromValue) {
|
320
|
+
to.firstChild.nodeValue = fromValue;
|
321
|
+
}
|
322
|
+
}
|
323
|
+
}
|
324
|
+
function handleHeadElement(newHeadTag, currentHead, ctx) {
|
325
|
+
let added = [];
|
326
|
+
let removed = [];
|
327
|
+
let preserved = [];
|
328
|
+
let nodesToAppend = [];
|
329
|
+
let headMergeStyle = ctx.head.style;
|
330
|
+
let srcToNewHeadNodes = /* @__PURE__ */ new Map();
|
331
|
+
for (const newHeadChild of newHeadTag.children) {
|
332
|
+
srcToNewHeadNodes.set(newHeadChild.outerHTML, newHeadChild);
|
333
|
+
}
|
334
|
+
for (const currentHeadElt of currentHead.children) {
|
335
|
+
let inNewContent = srcToNewHeadNodes.has(currentHeadElt.outerHTML);
|
336
|
+
let isReAppended = ctx.head.shouldReAppend(currentHeadElt);
|
337
|
+
let isPreserved = ctx.head.shouldPreserve(currentHeadElt);
|
338
|
+
if (inNewContent || isPreserved) {
|
339
|
+
if (isReAppended) {
|
340
|
+
removed.push(currentHeadElt);
|
341
|
+
} else {
|
342
|
+
srcToNewHeadNodes.delete(currentHeadElt.outerHTML);
|
343
|
+
preserved.push(currentHeadElt);
|
344
|
+
}
|
345
|
+
} else {
|
346
|
+
if (headMergeStyle === "append") {
|
347
|
+
if (isReAppended) {
|
348
|
+
removed.push(currentHeadElt);
|
349
|
+
nodesToAppend.push(currentHeadElt);
|
350
|
+
}
|
351
|
+
} else {
|
352
|
+
if (ctx.head.shouldRemove(currentHeadElt) !== false) {
|
353
|
+
removed.push(currentHeadElt);
|
354
|
+
}
|
355
|
+
}
|
356
|
+
}
|
357
|
+
}
|
358
|
+
nodesToAppend.push(...srcToNewHeadNodes.values());
|
359
|
+
let promises = [];
|
360
|
+
for (const newNode of nodesToAppend) {
|
361
|
+
let newElt = (
|
362
|
+
/** @type {ChildNode} */
|
363
|
+
document.createRange().createContextualFragment(newNode.outerHTML).firstChild
|
364
|
+
);
|
365
|
+
if (ctx.callbacks.beforeNodeAdded(newElt) !== false) {
|
366
|
+
if ("href" in newElt && newElt.href || "src" in newElt && newElt.src) {
|
367
|
+
let resolve;
|
368
|
+
let promise = new Promise(function(_resolve) {
|
369
|
+
resolve = _resolve;
|
370
|
+
});
|
371
|
+
newElt.addEventListener("load", function() {
|
372
|
+
resolve();
|
373
|
+
});
|
374
|
+
promises.push(promise);
|
375
|
+
}
|
376
|
+
currentHead.appendChild(newElt);
|
377
|
+
ctx.callbacks.afterNodeAdded(newElt);
|
378
|
+
added.push(newElt);
|
379
|
+
}
|
380
|
+
}
|
381
|
+
for (const removedElement of removed) {
|
382
|
+
if (ctx.callbacks.beforeNodeRemoved(removedElement) !== false) {
|
383
|
+
currentHead.removeChild(removedElement);
|
384
|
+
ctx.callbacks.afterNodeRemoved(removedElement);
|
385
|
+
}
|
386
|
+
}
|
387
|
+
ctx.head.afterHeadMorphed(currentHead, {
|
388
|
+
added,
|
389
|
+
kept: preserved,
|
390
|
+
removed
|
391
|
+
});
|
392
|
+
return promises;
|
393
|
+
}
|
394
|
+
function noOp() {
|
395
|
+
}
|
396
|
+
function mergeDefaults(config2) {
|
397
|
+
let finalConfig = Object.assign({}, defaults);
|
398
|
+
Object.assign(finalConfig, config2);
|
399
|
+
finalConfig.callbacks = Object.assign(
|
400
|
+
{},
|
401
|
+
defaults.callbacks,
|
402
|
+
config2.callbacks
|
403
|
+
);
|
404
|
+
finalConfig.head = Object.assign({}, defaults.head, config2.head);
|
405
|
+
return finalConfig;
|
406
|
+
}
|
407
|
+
function createMorphContext(oldNode, newContent, config2) {
|
408
|
+
const mergedConfig = mergeDefaults(config2);
|
409
|
+
return {
|
410
|
+
target: oldNode,
|
411
|
+
newContent,
|
412
|
+
config: mergedConfig,
|
413
|
+
morphStyle: mergedConfig.morphStyle,
|
414
|
+
ignoreActive: mergedConfig.ignoreActive,
|
415
|
+
ignoreActiveValue: mergedConfig.ignoreActiveValue,
|
416
|
+
idMap: createIdMap(oldNode, newContent),
|
417
|
+
deadIds: /* @__PURE__ */ new Set(),
|
418
|
+
persistentIds: mergedConfig.twoPass ? createPersistentIds(oldNode, newContent) : /* @__PURE__ */ new Set(),
|
419
|
+
pantry: mergedConfig.twoPass ? createPantry() : document.createElement("div"),
|
420
|
+
callbacks: mergedConfig.callbacks,
|
421
|
+
head: mergedConfig.head
|
422
|
+
};
|
423
|
+
}
|
424
|
+
function createPantry() {
|
425
|
+
const pantry = document.createElement("div");
|
426
|
+
pantry.hidden = true;
|
427
|
+
document.body.insertAdjacentElement("afterend", pantry);
|
428
|
+
return pantry;
|
429
|
+
}
|
430
|
+
function isIdSetMatch(node1, node2, ctx) {
|
431
|
+
if (node1 == null || node2 == null) {
|
432
|
+
return false;
|
433
|
+
}
|
434
|
+
if (node1 instanceof Element && node2 instanceof Element && node1.tagName === node2.tagName) {
|
435
|
+
if (node1.id !== "" && node1.id === node2.id) {
|
436
|
+
return true;
|
437
|
+
} else {
|
438
|
+
return getIdIntersectionCount(ctx, node1, node2) > 0;
|
439
|
+
}
|
440
|
+
}
|
441
|
+
return false;
|
442
|
+
}
|
443
|
+
function isSoftMatch(oldNode, newNode) {
|
444
|
+
if (oldNode == null || newNode == null) {
|
445
|
+
return false;
|
446
|
+
}
|
447
|
+
if (
|
448
|
+
/** @type {Element} */
|
449
|
+
oldNode.id && /** @type {Element} */
|
450
|
+
oldNode.id !== /** @type {Element} */
|
451
|
+
newNode.id
|
452
|
+
) {
|
453
|
+
return false;
|
454
|
+
}
|
455
|
+
return oldNode.nodeType === newNode.nodeType && /** @type {Element} */
|
456
|
+
oldNode.tagName === /** @type {Element} */
|
457
|
+
newNode.tagName;
|
458
|
+
}
|
459
|
+
function removeNodesBetween(startInclusive, endExclusive, ctx) {
|
460
|
+
let cursor = startInclusive;
|
461
|
+
while (cursor !== endExclusive) {
|
462
|
+
let tempNode = (
|
463
|
+
/** @type {Node} */
|
464
|
+
cursor
|
465
|
+
);
|
466
|
+
cursor = tempNode.nextSibling;
|
467
|
+
removeNode(tempNode, ctx);
|
468
|
+
}
|
469
|
+
removeIdsFromConsideration(ctx, endExclusive);
|
470
|
+
return endExclusive.nextSibling;
|
471
|
+
}
|
472
|
+
function findIdSetMatch(newContent, oldParent, newChild, insertionPoint, ctx) {
|
473
|
+
let newChildPotentialIdCount = getIdIntersectionCount(
|
474
|
+
ctx,
|
475
|
+
newChild,
|
476
|
+
oldParent
|
477
|
+
);
|
478
|
+
let potentialMatch = null;
|
479
|
+
if (newChildPotentialIdCount > 0) {
|
480
|
+
potentialMatch = insertionPoint;
|
481
|
+
let otherMatchCount = 0;
|
482
|
+
while (potentialMatch != null) {
|
483
|
+
if (isIdSetMatch(newChild, potentialMatch, ctx)) {
|
484
|
+
return potentialMatch;
|
485
|
+
}
|
486
|
+
otherMatchCount += getIdIntersectionCount(
|
487
|
+
ctx,
|
488
|
+
potentialMatch,
|
489
|
+
newContent
|
490
|
+
);
|
491
|
+
if (otherMatchCount > newChildPotentialIdCount) {
|
492
|
+
return null;
|
493
|
+
}
|
494
|
+
potentialMatch = potentialMatch.nextSibling;
|
495
|
+
}
|
496
|
+
}
|
497
|
+
return potentialMatch;
|
498
|
+
}
|
499
|
+
function findSoftMatch(newContent, oldParent, newChild, insertionPoint, ctx) {
|
500
|
+
let potentialSoftMatch = insertionPoint;
|
501
|
+
let nextSibling = newChild.nextSibling;
|
502
|
+
let siblingSoftMatchCount = 0;
|
503
|
+
while (potentialSoftMatch != null) {
|
504
|
+
if (getIdIntersectionCount(ctx, potentialSoftMatch, newContent) > 0) {
|
505
|
+
return null;
|
506
|
+
}
|
507
|
+
if (isSoftMatch(potentialSoftMatch, newChild)) {
|
508
|
+
return potentialSoftMatch;
|
509
|
+
}
|
510
|
+
if (isSoftMatch(potentialSoftMatch, nextSibling)) {
|
511
|
+
siblingSoftMatchCount++;
|
512
|
+
nextSibling = /** @type {Node} */
|
513
|
+
nextSibling.nextSibling;
|
514
|
+
if (siblingSoftMatchCount >= 2) {
|
515
|
+
return null;
|
516
|
+
}
|
517
|
+
}
|
518
|
+
potentialSoftMatch = potentialSoftMatch.nextSibling;
|
519
|
+
}
|
520
|
+
return potentialSoftMatch;
|
521
|
+
}
|
522
|
+
const generatedByIdiomorph = /* @__PURE__ */ new WeakSet();
|
523
|
+
function parseContent(newContent) {
|
524
|
+
let parser = new DOMParser();
|
525
|
+
let contentWithSvgsRemoved = newContent.replace(
|
526
|
+
/<svg(\s[^>]*>|>)([\s\S]*?)<\/svg>/gim,
|
527
|
+
""
|
528
|
+
);
|
529
|
+
if (contentWithSvgsRemoved.match(/<\/html>/) || contentWithSvgsRemoved.match(/<\/head>/) || contentWithSvgsRemoved.match(/<\/body>/)) {
|
530
|
+
let content = parser.parseFromString(newContent, "text/html");
|
531
|
+
if (contentWithSvgsRemoved.match(/<\/html>/)) {
|
532
|
+
generatedByIdiomorph.add(content);
|
533
|
+
return content;
|
534
|
+
} else {
|
535
|
+
let htmlElement = content.firstChild;
|
536
|
+
if (htmlElement) {
|
537
|
+
generatedByIdiomorph.add(htmlElement);
|
538
|
+
return htmlElement;
|
539
|
+
} else {
|
540
|
+
return null;
|
541
|
+
}
|
542
|
+
}
|
543
|
+
} else {
|
544
|
+
let responseDoc = parser.parseFromString(
|
545
|
+
"<body><template>" + newContent + "</template></body>",
|
546
|
+
"text/html"
|
547
|
+
);
|
548
|
+
let content = (
|
549
|
+
/** @type {HTMLTemplateElement} */
|
550
|
+
responseDoc.body.querySelector("template").content
|
551
|
+
);
|
552
|
+
generatedByIdiomorph.add(content);
|
553
|
+
return content;
|
554
|
+
}
|
555
|
+
}
|
556
|
+
function normalizeContent(newContent) {
|
557
|
+
if (newContent == null) {
|
558
|
+
const dummyParent = document.createElement("div");
|
559
|
+
return dummyParent;
|
560
|
+
} else if (generatedByIdiomorph.has(
|
561
|
+
/** @type {Element} */
|
562
|
+
newContent
|
563
|
+
)) {
|
564
|
+
return (
|
565
|
+
/** @type {Element} */
|
566
|
+
newContent
|
567
|
+
);
|
568
|
+
} else if (newContent instanceof Node) {
|
569
|
+
const dummyParent = document.createElement("div");
|
570
|
+
dummyParent.append(newContent);
|
571
|
+
return dummyParent;
|
572
|
+
} else {
|
573
|
+
const dummyParent = document.createElement("div");
|
574
|
+
for (const elt of [...newContent]) {
|
575
|
+
dummyParent.append(elt);
|
576
|
+
}
|
577
|
+
return dummyParent;
|
578
|
+
}
|
579
|
+
}
|
580
|
+
function insertSiblings(previousSibling, morphedNode, nextSibling) {
|
581
|
+
var _a, _b;
|
582
|
+
let stack = [];
|
583
|
+
let added = [];
|
584
|
+
while (previousSibling != null) {
|
585
|
+
stack.push(previousSibling);
|
586
|
+
previousSibling = previousSibling.previousSibling;
|
587
|
+
}
|
588
|
+
let node = stack.pop();
|
589
|
+
while (node !== void 0) {
|
590
|
+
added.push(node);
|
591
|
+
(_a = morphedNode.parentElement) == null ? void 0 : _a.insertBefore(node, morphedNode);
|
592
|
+
node = stack.pop();
|
593
|
+
}
|
594
|
+
added.push(morphedNode);
|
595
|
+
while (nextSibling != null) {
|
596
|
+
stack.push(nextSibling);
|
597
|
+
added.push(nextSibling);
|
598
|
+
nextSibling = nextSibling.nextSibling;
|
599
|
+
}
|
600
|
+
while (stack.length > 0) {
|
601
|
+
const node2 = (
|
602
|
+
/** @type {Node} */
|
603
|
+
stack.pop()
|
604
|
+
);
|
605
|
+
(_b = morphedNode.parentElement) == null ? void 0 : _b.insertBefore(node2, morphedNode.nextSibling);
|
606
|
+
}
|
607
|
+
return added;
|
608
|
+
}
|
609
|
+
function findBestNodeMatch(newContent, oldNode, ctx) {
|
610
|
+
let currentElement;
|
611
|
+
currentElement = newContent.firstChild;
|
612
|
+
let bestElement = currentElement;
|
613
|
+
let score = 0;
|
614
|
+
while (currentElement) {
|
615
|
+
let newScore = scoreElement(currentElement, oldNode, ctx);
|
616
|
+
if (newScore > score) {
|
617
|
+
bestElement = currentElement;
|
618
|
+
score = newScore;
|
619
|
+
}
|
620
|
+
currentElement = currentElement.nextSibling;
|
621
|
+
}
|
622
|
+
return bestElement;
|
623
|
+
}
|
624
|
+
function scoreElement(node1, node2, ctx) {
|
625
|
+
if (isSoftMatch(node2, node1)) {
|
626
|
+
return 0.5 + getIdIntersectionCount(
|
627
|
+
ctx,
|
628
|
+
/** @type {Node} */
|
629
|
+
node1,
|
630
|
+
node2
|
631
|
+
);
|
632
|
+
}
|
633
|
+
return 0;
|
634
|
+
}
|
635
|
+
function removeNode(tempNode, ctx) {
|
636
|
+
var _a;
|
637
|
+
removeIdsFromConsideration(ctx, tempNode);
|
638
|
+
if (ctx.config.twoPass && hasPersistentIdNodes(ctx, tempNode) && tempNode instanceof Element) {
|
639
|
+
moveToPantry(tempNode, ctx);
|
640
|
+
} else {
|
641
|
+
if (ctx.callbacks.beforeNodeRemoved(tempNode) === false) return;
|
642
|
+
(_a = tempNode.parentNode) == null ? void 0 : _a.removeChild(tempNode);
|
643
|
+
ctx.callbacks.afterNodeRemoved(tempNode);
|
644
|
+
}
|
645
|
+
}
|
646
|
+
function moveToPantry(node, ctx) {
|
647
|
+
var _a;
|
648
|
+
if (ctx.callbacks.beforeNodePantried(node) === false) return;
|
649
|
+
Array.from(node.childNodes).forEach((child) => {
|
650
|
+
moveToPantry(child, ctx);
|
651
|
+
});
|
652
|
+
if (ctx.persistentIds.has(
|
653
|
+
/** @type {Element} */
|
654
|
+
node.id
|
655
|
+
)) {
|
656
|
+
if (ctx.pantry.moveBefore) {
|
657
|
+
ctx.pantry.moveBefore(node, null);
|
658
|
+
} else {
|
659
|
+
ctx.pantry.insertBefore(node, null);
|
660
|
+
}
|
661
|
+
} else {
|
662
|
+
if (ctx.callbacks.beforeNodeRemoved(node) === false) return;
|
663
|
+
(_a = node.parentNode) == null ? void 0 : _a.removeChild(node);
|
664
|
+
ctx.callbacks.afterNodeRemoved(node);
|
665
|
+
}
|
666
|
+
}
|
667
|
+
function restoreFromPantry(root, ctx) {
|
668
|
+
if (root instanceof Element) {
|
669
|
+
Array.from(ctx.pantry.children).reverse().forEach((element) => {
|
670
|
+
var _a;
|
671
|
+
const matchElement = root.querySelector(`#${element.id}`);
|
672
|
+
if (matchElement) {
|
673
|
+
if ((_a = matchElement.parentElement) == null ? void 0 : _a.moveBefore) {
|
674
|
+
matchElement.parentElement.moveBefore(element, matchElement);
|
675
|
+
while (matchElement.hasChildNodes()) {
|
676
|
+
element.moveBefore(matchElement.firstChild, null);
|
677
|
+
}
|
678
|
+
} else {
|
679
|
+
matchElement.before(element);
|
680
|
+
while (matchElement.firstChild) {
|
681
|
+
element.insertBefore(matchElement.firstChild, null);
|
682
|
+
}
|
683
|
+
}
|
684
|
+
if (ctx.callbacks.beforeNodeMorphed(element, matchElement) !== false) {
|
685
|
+
syncNodeFrom(matchElement, element, ctx);
|
686
|
+
ctx.callbacks.afterNodeMorphed(element, matchElement);
|
687
|
+
}
|
688
|
+
matchElement.remove();
|
689
|
+
}
|
690
|
+
});
|
691
|
+
ctx.pantry.remove();
|
692
|
+
}
|
693
|
+
}
|
694
|
+
function isIdInConsideration(ctx, id) {
|
695
|
+
return !ctx.deadIds.has(id);
|
696
|
+
}
|
697
|
+
function idIsWithinNode(ctx, id, targetNode) {
|
698
|
+
let idSet = ctx.idMap.get(targetNode) || EMPTY_SET;
|
699
|
+
return idSet.has(id);
|
700
|
+
}
|
701
|
+
function removeIdsFromConsideration(ctx, node) {
|
702
|
+
let idSet = ctx.idMap.get(node) || EMPTY_SET;
|
703
|
+
for (const id of idSet) {
|
704
|
+
ctx.deadIds.add(id);
|
705
|
+
}
|
706
|
+
}
|
707
|
+
function hasPersistentIdNodes(ctx, node) {
|
708
|
+
for (const id of ctx.idMap.get(node) || EMPTY_SET) {
|
709
|
+
if (ctx.persistentIds.has(id)) {
|
710
|
+
return true;
|
711
|
+
}
|
712
|
+
}
|
713
|
+
return false;
|
714
|
+
}
|
715
|
+
function getIdIntersectionCount(ctx, node1, node2) {
|
716
|
+
let sourceSet = ctx.idMap.get(node1) || EMPTY_SET;
|
717
|
+
let matchCount = 0;
|
718
|
+
for (const id of sourceSet) {
|
719
|
+
if (isIdInConsideration(ctx, id) && idIsWithinNode(ctx, id, node2)) {
|
720
|
+
++matchCount;
|
721
|
+
}
|
722
|
+
}
|
723
|
+
return matchCount;
|
724
|
+
}
|
725
|
+
function nodesWithIds(content) {
|
726
|
+
let nodes = Array.from(content.querySelectorAll("[id]"));
|
727
|
+
if (content.id) {
|
728
|
+
nodes.push(content);
|
729
|
+
}
|
730
|
+
return nodes;
|
731
|
+
}
|
732
|
+
function populateIdMapForNode(node, idMap) {
|
733
|
+
let nodeParent = node.parentElement;
|
734
|
+
for (const elt of nodesWithIds(node)) {
|
735
|
+
let current = elt;
|
736
|
+
while (current !== nodeParent && current != null) {
|
737
|
+
let idSet = idMap.get(current);
|
738
|
+
if (idSet == null) {
|
739
|
+
idSet = /* @__PURE__ */ new Set();
|
740
|
+
idMap.set(current, idSet);
|
741
|
+
}
|
742
|
+
idSet.add(elt.id);
|
743
|
+
current = current.parentElement;
|
744
|
+
}
|
745
|
+
}
|
746
|
+
}
|
747
|
+
function createIdMap(oldContent, newContent) {
|
748
|
+
let idMap = /* @__PURE__ */ new Map();
|
749
|
+
populateIdMapForNode(oldContent, idMap);
|
750
|
+
populateIdMapForNode(newContent, idMap);
|
751
|
+
return idMap;
|
752
|
+
}
|
753
|
+
function createPersistentIds(oldContent, newContent) {
|
754
|
+
const toIdTagName = (node) => node.tagName + "#" + node.id;
|
755
|
+
const oldIdSet = new Set(nodesWithIds(oldContent).map(toIdTagName));
|
756
|
+
let matchIdSet = /* @__PURE__ */ new Set();
|
757
|
+
for (const newNode of nodesWithIds(newContent)) {
|
758
|
+
if (oldIdSet.has(toIdTagName(newNode))) {
|
759
|
+
matchIdSet.add(newNode.id);
|
760
|
+
}
|
761
|
+
}
|
762
|
+
return matchIdSet;
|
763
|
+
}
|
764
|
+
return {
|
765
|
+
morph,
|
766
|
+
defaults
|
767
|
+
};
|
768
|
+
}();
|
769
|
+
const topics = {};
|
770
|
+
const _async = {};
|
771
|
+
const publish = (name, params) => {
|
772
|
+
_async[name] = Object.assign({}, _async[name], params);
|
773
|
+
if (topics[name])
|
774
|
+
topics[name].forEach((topic) => topic(params));
|
775
|
+
};
|
776
|
+
const subscribe = (name, method) => {
|
777
|
+
topics[name] = topics[name] || [];
|
778
|
+
topics[name].push(method);
|
779
|
+
if (name in _async) {
|
780
|
+
method(_async[name]);
|
781
|
+
}
|
782
|
+
return () => {
|
783
|
+
topics[name] = topics[name].filter((fn) => fn != method);
|
784
|
+
};
|
785
|
+
};
|
786
|
+
const Component = ({ name, module, dependencies, node, templates: templates2, signal }) => {
|
787
|
+
var _a;
|
788
|
+
const _model = module.model || {};
|
789
|
+
const initialState = new Function(`return ${node.getAttribute("html-model") || "{}"}`)();
|
790
|
+
const tplid = node.getAttribute("tplid");
|
791
|
+
const scopeid = node.getAttribute("html-scope-id");
|
792
|
+
const tpl = templates2[tplid];
|
793
|
+
const data = g.scope[scopeid];
|
794
|
+
const model = ((_a = module == null ? void 0 : module.model) == null ? void 0 : _a.apply) ? _model({ elm: node, initialState }) : _model;
|
795
|
+
const state = Object.assign({}, data, model, initialState);
|
796
|
+
const view = module.view ? module.view : (data2) => data2;
|
797
|
+
let updates = [];
|
798
|
+
const base = {
|
799
|
+
name,
|
800
|
+
model,
|
801
|
+
elm: node,
|
802
|
+
template: tpl.template,
|
803
|
+
dependencies,
|
804
|
+
publish,
|
805
|
+
subscribe,
|
806
|
+
main(fn) {
|
807
|
+
node.addEventListener(":mount", fn);
|
808
|
+
},
|
809
|
+
/**
|
810
|
+
* @State
|
811
|
+
*/
|
812
|
+
state: {
|
813
|
+
save(data2) {
|
814
|
+
if (data2.constructor === Function) {
|
815
|
+
data2(state);
|
816
|
+
} else {
|
817
|
+
Object.assign(state, data2);
|
818
|
+
}
|
819
|
+
},
|
820
|
+
set(data2) {
|
821
|
+
if (!document.body.contains(node)) {
|
822
|
+
return;
|
823
|
+
}
|
824
|
+
if (data2.constructor === Function) {
|
825
|
+
data2(state);
|
826
|
+
}
|
827
|
+
updates.push(data2);
|
828
|
+
return new Promise((resolve) => {
|
829
|
+
rAF(() => rAF(() => {
|
830
|
+
Object.assign.apply(null, [state, ...updates]);
|
831
|
+
if (updates.length) {
|
832
|
+
const newstate = Object.assign({}, state);
|
833
|
+
render(newstate);
|
834
|
+
resolve(newstate);
|
835
|
+
updates = [];
|
836
|
+
}
|
837
|
+
}));
|
838
|
+
});
|
839
|
+
},
|
840
|
+
get() {
|
841
|
+
return Object.assign({}, state);
|
842
|
+
}
|
843
|
+
},
|
844
|
+
/**
|
845
|
+
* @Events
|
846
|
+
*/
|
847
|
+
on(ev, selectorOrCallback, callback) {
|
848
|
+
if (callback) {
|
849
|
+
callback.handler = (e) => {
|
850
|
+
const detail = e.detail || {};
|
851
|
+
let parent = e.target;
|
852
|
+
while (parent) {
|
853
|
+
if (parent.matches(selectorOrCallback)) {
|
854
|
+
e.delegateTarget = parent;
|
855
|
+
callback.apply(node, [e].concat(detail.args));
|
856
|
+
}
|
857
|
+
if (parent === node) break;
|
858
|
+
parent = parent.parentNode;
|
859
|
+
}
|
860
|
+
};
|
861
|
+
node.addEventListener(ev, callback.handler, {
|
862
|
+
signal,
|
863
|
+
capture: ev == "focus" || ev == "blur" || ev == "mouseenter" || ev == "mouseleave"
|
864
|
+
});
|
865
|
+
} else {
|
866
|
+
selectorOrCallback.handler = (e) => {
|
867
|
+
e.delegateTarget = node;
|
868
|
+
selectorOrCallback.apply(node, [e].concat(e.detail.args));
|
869
|
+
};
|
870
|
+
node.addEventListener(ev, selectorOrCallback.handler, { signal });
|
871
|
+
}
|
872
|
+
},
|
873
|
+
off(ev, callback) {
|
874
|
+
if (callback.handler) {
|
875
|
+
node.removeEventListener(ev, callback.handler);
|
876
|
+
}
|
877
|
+
},
|
878
|
+
trigger(ev, selectorOrCallback, data2) {
|
879
|
+
if (selectorOrCallback.constructor === String) {
|
880
|
+
Array.from(node.querySelectorAll(selectorOrCallback)).forEach((children) => {
|
881
|
+
children.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
|
882
|
+
});
|
883
|
+
} else {
|
884
|
+
node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
|
885
|
+
}
|
886
|
+
},
|
887
|
+
emit(ev, data2) {
|
888
|
+
node.dispatchEvent(new CustomEvent(ev, { bubbles: true, detail: { args: data2 } }));
|
889
|
+
},
|
890
|
+
unmount(fn) {
|
891
|
+
node.addEventListener(":unmount", fn);
|
892
|
+
},
|
893
|
+
innerHTML(target, html_) {
|
894
|
+
const element = html_ ? target : elm;
|
895
|
+
const clone = element.cloneNode();
|
896
|
+
const html = html_ ? html_ : target;
|
897
|
+
clone.innerHTML = html;
|
898
|
+
rAF((_) => Idiomorph.morph(element, clone, IdiomorphOptions));
|
899
|
+
}
|
900
|
+
};
|
901
|
+
const render = (data2) => {
|
902
|
+
const html = tpl.render.call(view(data2), node, safe, g);
|
903
|
+
Idiomorph.morph(node, html, IdiomorphOptions(node));
|
904
|
+
rAF(() => {
|
905
|
+
node.querySelectorAll("[tplid]").forEach((element) => {
|
906
|
+
if (!element.base) return;
|
907
|
+
const base2 = element.base;
|
908
|
+
const props = Object.keys(base2.model).reduce((acc, key) => {
|
909
|
+
if (key in data2) {
|
910
|
+
if (!acc) acc = {};
|
911
|
+
acc[key] = data2[key];
|
912
|
+
}
|
913
|
+
return acc;
|
914
|
+
}, null);
|
915
|
+
if (props) {
|
916
|
+
base2.state.set(props);
|
917
|
+
}
|
918
|
+
});
|
919
|
+
rAF(() => g.scope = {});
|
920
|
+
});
|
921
|
+
};
|
922
|
+
node.base = base;
|
923
|
+
module.default(base);
|
924
|
+
};
|
925
|
+
const IdiomorphOptions = (parent) => ({
|
926
|
+
callbacks: {
|
927
|
+
beforeNodeMorphed(node) {
|
928
|
+
if (node.nodeType === 1) {
|
929
|
+
if ("html-static" in node.attributes) {
|
930
|
+
return false;
|
931
|
+
}
|
932
|
+
if (node.base && node !== parent) {
|
933
|
+
return false;
|
934
|
+
}
|
935
|
+
}
|
936
|
+
}
|
937
|
+
}
|
938
|
+
});
|
939
|
+
const Element$1 = ({ component, templates: templates2, start: start2 }) => {
|
940
|
+
const { name, module, dependencies } = component;
|
941
|
+
const abortController = new AbortController();
|
942
|
+
return class extends HTMLElement {
|
943
|
+
constructor() {
|
944
|
+
super();
|
945
|
+
}
|
946
|
+
connectedCallback() {
|
947
|
+
if (!this.getAttribute("tplid")) {
|
948
|
+
start2(this.parentNode);
|
949
|
+
}
|
950
|
+
Component({
|
951
|
+
node: this,
|
952
|
+
name,
|
953
|
+
module,
|
954
|
+
dependencies,
|
955
|
+
templates: templates2,
|
956
|
+
signal: abortController.signal
|
957
|
+
});
|
958
|
+
this.dispatchEvent(new CustomEvent(":mount"));
|
959
|
+
this.base.state.set({});
|
960
|
+
}
|
961
|
+
disconnectedCallback() {
|
962
|
+
this.dispatchEvent(new CustomEvent(":unmount"));
|
963
|
+
abortController.abort();
|
964
|
+
delete this.base;
|
965
|
+
}
|
966
|
+
};
|
967
|
+
};
|
968
|
+
const templates = {};
|
969
|
+
const config = {
|
970
|
+
tags: ["{{", "}}"]
|
971
|
+
};
|
972
|
+
const templateConfig$1 = (newconfig) => {
|
973
|
+
Object.assign(config, newconfig);
|
974
|
+
};
|
975
|
+
const template = (target, { components: components2 }) => {
|
976
|
+
tagElements(target, [...Object.keys(components2), "template"]);
|
977
|
+
const clone = target.cloneNode(true);
|
978
|
+
transformTemplate(clone);
|
979
|
+
removeTemplateTagsRecursively(clone);
|
980
|
+
transformAttributes(clone);
|
981
|
+
setTemplates(clone, components2);
|
982
|
+
return templates;
|
983
|
+
};
|
984
|
+
const compile = (html) => {
|
985
|
+
const parsedHtml = JSON.stringify(html);
|
986
|
+
return new Function("$element", "safe", "$g", `
|
987
|
+
var $data = this;
|
988
|
+
with( $data ){
|
989
|
+
var output=${parsedHtml.replace(/%%_=(.+?)_%%/g, function(_, variable) {
|
990
|
+
return '"+safe(function(){return ' + variable + ';})+"';
|
991
|
+
}).replace(/%%_(.+?)_%%/g, function(_, variable) {
|
992
|
+
return '";' + variable + '\noutput+="';
|
993
|
+
})};return output;
|
994
|
+
}
|
995
|
+
`);
|
996
|
+
};
|
997
|
+
const tagElements = (target, keys) => {
|
998
|
+
target.querySelectorAll(keys.toString()).forEach((node) => {
|
999
|
+
if (node.localName === "template") {
|
1000
|
+
return tagElements(node.content, keys);
|
1001
|
+
}
|
1002
|
+
node.setAttribute("tplid", uuid());
|
1003
|
+
});
|
1004
|
+
};
|
1005
|
+
const transformAttributes = (clone) => {
|
1006
|
+
const regexTags = new RegExp(`\\${config.tags[0]}(.+?)\\${config.tags[1]}`, "g");
|
1007
|
+
clone.innerHTML = clone.innerHTML.replace(regexTags, "%%_=$1_%%").replace(/html-(allowfullscreen|async|autofocus|autoplay|checked|controls|default|defer|disabled|formnovalidate|inert|ismap|itemscope|loop|multiple|muted|nomodule|novalidate|open|playsinline|readonly|required|reversed|selected)=\"(.*?)\"/g, `%%_if(safe(function(){ return $2 })){_%%$1%%_}_%%`).replace(/html-(.*?)=\"(.*?)\"/g, (all, key, value) => {
|
1008
|
+
if (key === "key" || key === "model") {
|
1009
|
+
return all;
|
1010
|
+
}
|
1011
|
+
if (value) {
|
1012
|
+
value = value.replace(/^{|}$/g, "");
|
1013
|
+
return `${key}="%%_=safe(function(){ return ${value} })_%%"`;
|
1014
|
+
} else {
|
1015
|
+
return all;
|
1016
|
+
}
|
1017
|
+
});
|
1018
|
+
};
|
1019
|
+
const transformTemplate = (clone) => {
|
1020
|
+
clone.querySelectorAll("template, [html-for], [html-if], [html-inner], [html-class]").forEach((element) => {
|
1021
|
+
const htmlFor = element.getAttribute("html-for");
|
1022
|
+
const htmlIf = element.getAttribute("html-if");
|
1023
|
+
const htmlInner = element.getAttribute("html-inner");
|
1024
|
+
const htmlClass = element.getAttribute("html-class");
|
1025
|
+
if (htmlFor) {
|
1026
|
+
element.removeAttribute("html-for");
|
1027
|
+
const split = htmlFor.match(/(.*)\sin\s(.*)/) || "";
|
1028
|
+
const varname = split[1];
|
1029
|
+
const object = split[2];
|
1030
|
+
const open = document.createTextNode(`%%_ ;(function(){ var $index = 0; for(var $key in safe(function(){ return ${object} }) ){ var $scopeid = Math.random().toString(36).substring(2, 9); var ${varname} = ${object}[$key]; $g.scope[$scopeid] = { ${varname} :${varname}, ${object}: ${object}, $index: $index, $key: $key }; _%%`);
|
1031
|
+
const close = document.createTextNode(`%%_ $index++; } })() _%%`);
|
1032
|
+
wrap(open, element, close);
|
1033
|
+
}
|
1034
|
+
if (htmlIf) {
|
1035
|
+
element.removeAttribute("html-if");
|
1036
|
+
const open = document.createTextNode(`%%_ if ( safe(function(){ return ${htmlIf} }) ){ _%%`);
|
1037
|
+
const close = document.createTextNode(`%%_ } _%%`);
|
1038
|
+
wrap(open, element, close);
|
1039
|
+
}
|
1040
|
+
if (htmlInner) {
|
1041
|
+
element.removeAttribute("html-inner");
|
1042
|
+
element.innerHTML = `%%_=${htmlInner}_%%`;
|
1043
|
+
}
|
1044
|
+
if (htmlClass) {
|
1045
|
+
element.removeAttribute("html-class");
|
1046
|
+
element.className = (element.className + ` %%_=${htmlClass}_%%`).trim();
|
1047
|
+
}
|
1048
|
+
if (element.localName === "template") {
|
1049
|
+
transformTemplate(element.content);
|
1050
|
+
}
|
1051
|
+
});
|
1052
|
+
};
|
1053
|
+
const setTemplates = (clone, components2) => {
|
1054
|
+
Array.from(clone.querySelectorAll("[tplid]")).reverse().forEach((node) => {
|
1055
|
+
const tplid = node.getAttribute("tplid");
|
1056
|
+
const name = node.localName;
|
1057
|
+
node.setAttribute("html-scope-id", "%%_=$scopeid_%%");
|
1058
|
+
if (name in components2 && components2[name].module.template) {
|
1059
|
+
const children = node.innerHTML;
|
1060
|
+
const html2 = components2[name].module.template({ elm: node, children });
|
1061
|
+
if (html2.constructor === Promise) {
|
1062
|
+
html2.then((htmlstring) => {
|
1063
|
+
node.innerHTML = htmlstring;
|
1064
|
+
const html3 = node.outerHTML;
|
1065
|
+
templates[tplid] = {
|
1066
|
+
template: html3,
|
1067
|
+
render: compile(html3)
|
1068
|
+
};
|
1069
|
+
});
|
1070
|
+
} else {
|
1071
|
+
node.innerHTML = html2;
|
1072
|
+
}
|
1073
|
+
}
|
1074
|
+
const html = node.outerHTML;
|
1075
|
+
templates[tplid] = {
|
1076
|
+
template: html,
|
1077
|
+
render: compile(html)
|
1078
|
+
};
|
1079
|
+
});
|
1080
|
+
};
|
1081
|
+
const removeTemplateTagsRecursively = (node) => {
|
1082
|
+
const templates2 = node.querySelectorAll("template");
|
1083
|
+
templates2.forEach((template2) => {
|
1084
|
+
if (template2.getAttribute("html-if") || template2.getAttribute("html-inner")) {
|
1085
|
+
return;
|
1086
|
+
}
|
1087
|
+
removeTemplateTagsRecursively(template2.content);
|
1088
|
+
const parent = template2.parentNode;
|
1089
|
+
if (parent) {
|
1090
|
+
const content = template2.content;
|
1091
|
+
while (content.firstChild) {
|
1092
|
+
parent.insertBefore(content.firstChild, template2);
|
1093
|
+
}
|
1094
|
+
parent.removeChild(template2);
|
1095
|
+
}
|
1096
|
+
});
|
1097
|
+
};
|
1098
|
+
const wrap = (open, node, close) => {
|
1099
|
+
var _a, _b;
|
1100
|
+
(_a = node.parentNode) == null ? void 0 : _a.insertBefore(open, node);
|
1101
|
+
(_b = node.parentNode) == null ? void 0 : _b.insertBefore(close, node.nextSibling);
|
1102
|
+
};
|
1103
|
+
const components = {};
|
1104
|
+
const templateConfig = (options) => {
|
1105
|
+
templateConfig$1(options);
|
1106
|
+
};
|
1107
|
+
const register = (name, module, dependencies) => {
|
1108
|
+
components[name] = { name, module, dependencies };
|
1109
|
+
};
|
1110
|
+
const start = (target = document.body) => {
|
1111
|
+
const templates2 = template(target, { components });
|
1112
|
+
Object.values(components).forEach(({ name, module, dependencies }) => {
|
1113
|
+
if (!customElements.get(name)) {
|
1114
|
+
customElements.define(name, Element$1({ component: { name, module, dependencies }, templates: templates2, start }));
|
1115
|
+
}
|
1116
|
+
});
|
1117
|
+
};
|
1118
|
+
export {
|
1119
|
+
publish,
|
1120
|
+
register,
|
1121
|
+
start,
|
1122
|
+
subscribe,
|
1123
|
+
templateConfig
|
1124
|
+
};
|
1125
|
+
//# sourceMappingURL=index.js.map
|