keits 0.0.4 → 0.1.1

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 (71) hide show
  1. package/README.md +93 -79
  2. package/dist/dev/api.js +8 -3
  3. package/dist/dev/forms.js +10 -5
  4. package/dist/dev/index.js +28 -35
  5. package/dist/dev/router.js +24 -7
  6. package/dist/dev/shared/{computed-3UO-KiH1.js → computed-DrF8O8b4.js} +1 -1
  7. package/dist/dev/shared/element-Bgf6SNbW.js +21 -0
  8. package/dist/dev/shared/{graph-D6-x_CWC.js → graph-SIvYLXSv.js} +62 -3
  9. package/dist/dev/shared/hydrate-g_akkVyT.js +588 -0
  10. package/dist/dev/shared/{element-CQPqagu1.js → insert-DadcXJL8.js} +116 -86
  11. package/dist/dev/shared/mount-BtpMEhSZ.js +23 -0
  12. package/dist/dev/shared/{owner-Dmge21OW.js → owner-BuwIoI4j.js} +12 -2
  13. package/dist/dev/shared/render-context-k2uu_dM_.js +34 -0
  14. package/dist/dev/shared/render-effect-Uk9dDmuU.js +32 -0
  15. package/dist/dev/shared/{resource-CbXVATvq.js → resource-DlgzdtcG.js} +24 -33
  16. package/dist/dev/shared/{signal-BOXkY-Iv.js → signal-B3xOKbVm.js} +1 -1
  17. package/dist/dev/shared/{store-BzUN1qE5.js → store-0vEg2bQV.js} +7 -3
  18. package/dist/dev/shared/unsafe-keys-CB1P22No.js +8 -0
  19. package/dist/dev/static.js +76 -0
  20. package/dist/dev/svg.js +2 -1
  21. package/dist/dev/testing.js +236 -0
  22. package/dist/dev/vite.js +141 -0
  23. package/dist/prod/api.js +76 -71
  24. package/dist/prod/forms.js +89 -84
  25. package/dist/prod/index.js +58 -66
  26. package/dist/prod/router.js +86 -73
  27. package/dist/prod/shared/{computed-CAPpOLCY.js → computed-BjqJ6Pps.js} +1 -1
  28. package/dist/prod/shared/element-B-0tH1YW.js +15 -0
  29. package/dist/prod/shared/{graph-C_aA46c6.js → graph-CR2WKbwr.js} +107 -63
  30. package/dist/prod/shared/hydrate-DhHCtdOz.js +480 -0
  31. package/dist/prod/shared/insert-CbcOsYmu.js +192 -0
  32. package/dist/prod/shared/mount-BEAO35S_.js +18 -0
  33. package/dist/prod/shared/owner-DBi3odT4.js +67 -0
  34. package/dist/prod/shared/render-context-DF8lr1uu.js +30 -0
  35. package/dist/prod/shared/render-effect-UFDi2Pfh.js +25 -0
  36. package/dist/prod/shared/resource-Ch5tVseZ.js +91 -0
  37. package/dist/prod/shared/signal-BDMlfa0Z.js +16 -0
  38. package/dist/prod/shared/store-CvW0DbmK.js +110 -0
  39. package/dist/prod/shared/unsafe-keys-C5PMlerl.js +7 -0
  40. package/dist/prod/static.js +48 -0
  41. package/dist/prod/svg.js +3 -2
  42. package/dist/prod/testing.js +161 -0
  43. package/dist/prod/vite.js +106 -0
  44. package/dist/types/async/render-context.d.ts +10 -0
  45. package/dist/types/core/effect.d.ts +1 -0
  46. package/dist/types/core/graph.d.ts +11 -0
  47. package/dist/types/core/owner.d.ts +1 -0
  48. package/dist/types/core/unsafe-keys.d.ts +2 -0
  49. package/dist/types/dom/hooks.d.ts +4 -0
  50. package/dist/types/dom/props.d.ts +2 -0
  51. package/dist/types/dom/types.d.ts +5 -0
  52. package/dist/types/router/index.d.ts +1 -0
  53. package/dist/types/router/match.d.ts +1 -0
  54. package/dist/types/router/types.d.ts +1 -0
  55. package/dist/types/static/head.d.ts +36 -0
  56. package/dist/types/static/hydrate.d.ts +3 -0
  57. package/dist/types/static/index.d.ts +4 -0
  58. package/dist/types/static/island.d.ts +14 -0
  59. package/dist/types/static/json.d.ts +1 -0
  60. package/dist/types/static/render.d.ts +11 -0
  61. package/dist/types/static/server-dom.d.ts +100 -0
  62. package/dist/types/testing/index.d.ts +69 -0
  63. package/dist/types/vite/index.d.ts +24 -0
  64. package/package.json +35 -8
  65. package/dist/dev/shared/render-effect-kOGnLNbL.js +0 -26
  66. package/dist/prod/shared/element-DGyQ01Vs.js +0 -164
  67. package/dist/prod/shared/owner-6BFHIPR9.js +0 -59
  68. package/dist/prod/shared/render-effect-DjaUT3Cn.js +0 -23
  69. package/dist/prod/shared/resource-CNC2DBO3.js +0 -109
  70. package/dist/prod/shared/signal-DGrl3r9T.js +0 -16
  71. package/dist/prod/shared/store-Dcscpk_h.js +0 -109
@@ -0,0 +1,588 @@
1
+ import { b as stopKeys, c as getOwner, d as insideHydratable, m as nextKey, v as startKeys, y as staticRoot } from "./graph-SIvYLXSv.js";
2
+ import { t as renderEffect } from "./render-effect-Uk9dDmuU.js";
3
+ import { a as onCleanup, i as keyedRoot } from "./owner-BuwIoI4j.js";
4
+ import { a as provide, i as createContext, o as useContext, r as setHydratedValue, t as RenderContext } from "./render-context-k2uu_dM_.js";
5
+ import { a as claimsPaused, c as setHooks, i as SVG_NS, o as defaultHooks, t as insert } from "./insert-DadcXJL8.js";
6
+ // src/static/json.ts
7
+ function scriptJson(value) {
8
+ return JSON.stringify(value).replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
9
+ }
10
+ // src/static/head.ts
11
+ var HeadContext = createContext(null);
12
+ function head(tags) {
13
+ const registry = useContext(HeadContext) ?? (typeof document === "undefined" ? null : browserRegistry());
14
+ if (registry === null) return;
15
+ const token = {};
16
+ renderEffect(() => registry.set(token, collect(tags)));
17
+ onCleanup(() => registry.remove(token));
18
+ }
19
+ var read = (value) => typeof value === "function" ? value() : value;
20
+ var quote = (value) => `"${value.replace(/["\\]/g, "\\$&")}"`;
21
+ function collect(tags) {
22
+ const out = [];
23
+ const meta = (attribute, name, content) => {
24
+ if (content !== void 0) out.push({
25
+ key: `${attribute}:${name}`,
26
+ tag: "meta",
27
+ attrs: {
28
+ [attribute]: name,
29
+ content
30
+ },
31
+ selector: `meta[${attribute}=${quote(name)}]`
32
+ });
33
+ };
34
+ const title = read(tags.title);
35
+ if (title !== void 0) out.push({
36
+ key: "title",
37
+ tag: "title",
38
+ attrs: {},
39
+ text: title,
40
+ selector: "title"
41
+ });
42
+ meta("name", "description", read(tags.description));
43
+ meta("name", "robots", read(tags.robots));
44
+ const canonical = read(tags.canonical);
45
+ if (canonical !== void 0) out.push({
46
+ key: "link:canonical",
47
+ tag: "link",
48
+ attrs: {
49
+ rel: "canonical",
50
+ href: canonical
51
+ },
52
+ selector: "link[rel=\"canonical\"]"
53
+ });
54
+ for (const [name, value] of Object.entries(tags.og ?? {})) meta("property", `og:${name}`, read(value));
55
+ for (const [name, value] of Object.entries(tags.twitter ?? {})) meta("name", `twitter:${name}`, read(value));
56
+ for (const item of tags.meta ?? []) if (item.property !== void 0) meta("property", item.property, read(item.content));
57
+ else if (item.name !== void 0) meta("name", item.name, read(item.content));
58
+ for (const attrs of tags.link ?? []) out.push({
59
+ key: `link:${attrs.rel}:${attrs.href}`,
60
+ tag: "link",
61
+ attrs: { ...attrs },
62
+ selector: `link[rel=${quote(attrs.rel)}][href=${quote(attrs.href)}]`
63
+ });
64
+ const data = typeof tags.jsonLd === "function" ? tags.jsonLd() : tags.jsonLd;
65
+ if (data !== void 0) out.push({
66
+ key: "jsonld",
67
+ tag: "script",
68
+ attrs: { type: "application/ld+json" },
69
+ text: scriptJson(data),
70
+ selector: "script[type=\"application/ld+json\"]"
71
+ });
72
+ return out;
73
+ }
74
+ function createHeadRegistry(changed = () => {}) {
75
+ const tokens = /* @__PURE__ */ new Map();
76
+ const registry = {
77
+ set(token, tags) {
78
+ tokens.set(token, tags);
79
+ changed(registry.winners());
80
+ },
81
+ remove(token) {
82
+ tokens.delete(token);
83
+ changed(registry.winners());
84
+ },
85
+ winners() {
86
+ const result = /* @__PURE__ */ new Map();
87
+ for (const tags of tokens.values()) for (const tag of tags) result.set(tag.key, tag);
88
+ return result;
89
+ }
90
+ };
91
+ return registry;
92
+ }
93
+ var browser = null;
94
+ function browserRegistry() {
95
+ if (browser !== null) return browser;
96
+ const applied = /* @__PURE__ */ new Map();
97
+ const originalTitle = document.title;
98
+ browser = createHeadRegistry((winners) => {
99
+ for (const [key, element] of applied) {
100
+ if (winners.has(key)) continue;
101
+ if (element === null) document.title = originalTitle;
102
+ else element.remove();
103
+ applied.delete(key);
104
+ }
105
+ for (const [key, tag] of winners) {
106
+ if (tag.tag === "title") {
107
+ if (document.title !== tag.text) document.title = tag.text ?? "";
108
+ applied.set(key, null);
109
+ continue;
110
+ }
111
+ let previous = applied.get(key) ?? null;
112
+ if (previous !== null && !previous.isConnected) previous = null;
113
+ let element = previous ?? document.head.querySelector(tag.selector);
114
+ if (element === null) {
115
+ element = document.createElement(tag.tag);
116
+ document.head.append(element);
117
+ }
118
+ for (const [name, value] of Object.entries(tag.attrs)) if (element.getAttribute(name) !== value) element.setAttribute(name, value);
119
+ if (tag.text !== void 0 && element.textContent !== tag.text) element.textContent = tag.text;
120
+ applied.set(key, element);
121
+ }
122
+ });
123
+ return browser;
124
+ }
125
+ // src/static/server-dom.ts
126
+ var ServerNode = class {
127
+ parentNode = null;
128
+ previousSibling = null;
129
+ nextSibling = null;
130
+ remove() {
131
+ this.parentNode?.removeChild(this);
132
+ }
133
+ before(...nodes) {
134
+ const parent = this.parentNode;
135
+ if (parent !== null) for (const node of nodes) parent.insertBefore(toNode(node), this);
136
+ }
137
+ after(...nodes) {
138
+ const parent = this.parentNode;
139
+ const next = this.nextSibling;
140
+ if (parent !== null) for (const node of nodes) parent.insertBefore(toNode(node), next);
141
+ }
142
+ };
143
+ var ServerParent = class extends ServerNode {
144
+ firstChild = null;
145
+ lastChild = null;
146
+ insertBefore(node, before) {
147
+ if (node instanceof ServerFragment) {
148
+ while (node.firstChild !== null) this.insertBefore(node.firstChild, before);
149
+ return node;
150
+ }
151
+ node.remove();
152
+ node.parentNode = this;
153
+ node.nextSibling = before;
154
+ node.previousSibling = before === null ? this.lastChild : before.previousSibling;
155
+ if (node.previousSibling === null) this.firstChild = node;
156
+ else node.previousSibling.nextSibling = node;
157
+ if (before === null) this.lastChild = node;
158
+ else before.previousSibling = node;
159
+ return node;
160
+ }
161
+ appendChild(node) {
162
+ return this.insertBefore(node, null);
163
+ }
164
+ append(...nodes) {
165
+ for (const node of nodes) this.insertBefore(toNode(node), null);
166
+ }
167
+ removeChild(node) {
168
+ if (node.previousSibling === null) this.firstChild = node.nextSibling;
169
+ else node.previousSibling.nextSibling = node.nextSibling;
170
+ if (node.nextSibling === null) this.lastChild = node.previousSibling;
171
+ else node.nextSibling.previousSibling = node.previousSibling;
172
+ node.parentNode = node.previousSibling = node.nextSibling = null;
173
+ return node;
174
+ }
175
+ get textContent() {
176
+ let text = "";
177
+ for (let child = this.firstChild; child !== null; child = child.nextSibling) text += child.textContent;
178
+ return text;
179
+ }
180
+ set textContent(value) {
181
+ while (this.firstChild !== null) this.removeChild(this.firstChild);
182
+ if (value !== "") this.appendChild(new ServerText(value));
183
+ }
184
+ printChildren(out, rawText) {
185
+ for (let child = this.firstChild; child !== null; child = child.nextSibling) child.print(out, rawText);
186
+ }
187
+ };
188
+ var ServerFragment = class extends ServerParent {
189
+ nodeType = 11;
190
+ print(out, rawText) {
191
+ this.printChildren(out, rawText);
192
+ }
193
+ };
194
+ var ServerText = class extends ServerNode {
195
+ nodeType = 3;
196
+ data;
197
+ constructor(data) {
198
+ super();
199
+ this.data = data;
200
+ }
201
+ get textContent() {
202
+ return this.data;
203
+ }
204
+ print(out, rawText) {
205
+ out.push(rawText ? escapeRawText(this.data) : escapeText(this.data));
206
+ }
207
+ };
208
+ var ServerComment = class extends ServerNode {
209
+ nodeType = 8;
210
+ data;
211
+ constructor(data) {
212
+ super();
213
+ this.data = data;
214
+ }
215
+ get textContent() {
216
+ return "";
217
+ }
218
+ print() {}
219
+ };
220
+ var ServerRawHTML = class extends ServerNode {
221
+ nodeType = 3;
222
+ html;
223
+ constructor(html) {
224
+ super();
225
+ this.html = html;
226
+ }
227
+ get textContent() {
228
+ return "";
229
+ }
230
+ print(out) {
231
+ out.push(this.html);
232
+ }
233
+ };
234
+ var VOID = /* @__PURE__ */ new Set([
235
+ "area",
236
+ "base",
237
+ "br",
238
+ "col",
239
+ "embed",
240
+ "hr",
241
+ "img",
242
+ "input",
243
+ "link",
244
+ "meta",
245
+ "source",
246
+ "track",
247
+ "wbr"
248
+ ]);
249
+ var RAW_TEXT = /* @__PURE__ */ new Set(["script", "style"]);
250
+ var SAFE_NAME = /^[^\s"'<>/=\u0000-\u001f\u007f]+$/;
251
+ var RENAMED = {
252
+ className: "class",
253
+ htmlFor: "for",
254
+ httpEquiv: "http-equiv",
255
+ acceptCharset: "accept-charset"
256
+ };
257
+ var ServerElement = class extends ServerParent {
258
+ nodeType = 1;
259
+ localName;
260
+ namespaceURI;
261
+ classList;
262
+ style;
263
+ #attributes = /* @__PURE__ */ new Map();
264
+ #state = {};
265
+ constructor(tag, namespace) {
266
+ super();
267
+ if (!SAFE_NAME.test(tag)) throw new Error(`[keits/static] "${tag}" is not a valid tag name.`);
268
+ this.localName = namespace === null ? tag.toLowerCase() : tag;
269
+ this.namespaceURI = namespace;
270
+ this.classList = new ServerClassList(this);
271
+ this.style = new ServerStyle(this);
272
+ }
273
+ getAttribute(name) {
274
+ return this.#attributes.get(this.#name(name)) ?? null;
275
+ }
276
+ hasAttribute(name) {
277
+ return this.#attributes.has(this.#name(name));
278
+ }
279
+ setAttribute(name, value) {
280
+ if (!SAFE_NAME.test(name)) throw new Error(`[keits/static] "${name}" is not a valid attribute name.`);
281
+ this.#attributes.set(this.#name(name), String(value));
282
+ }
283
+ removeAttribute(name) {
284
+ this.#attributes.delete(this.#name(name));
285
+ }
286
+ toggleAttribute(name, force) {
287
+ const on = force ?? !this.hasAttribute(name);
288
+ if (on) this.setAttribute(name, "");
289
+ else this.removeAttribute(name);
290
+ return on;
291
+ }
292
+ #name(name) {
293
+ return this.namespaceURI === null ? name.toLowerCase() : name;
294
+ }
295
+ get value() {
296
+ return this.#state.value ?? "";
297
+ }
298
+ set value(value) {
299
+ this.#state.value = value;
300
+ }
301
+ get checked() {
302
+ return this.#state.checked === true;
303
+ }
304
+ set checked(value) {
305
+ this.#state.checked = value;
306
+ }
307
+ get selected() {
308
+ return this.#state.selected === true;
309
+ }
310
+ set selected(value) {
311
+ this.#state.selected = value;
312
+ }
313
+ get textContent() {
314
+ return super.textContent;
315
+ }
316
+ set textContent(value) {
317
+ super.textContent = value == null ? "" : String(value);
318
+ }
319
+ set className(value) {
320
+ this.setAttribute("class", value);
321
+ }
322
+ set htmlFor(value) {
323
+ this.setAttribute(RENAMED.htmlFor, value);
324
+ }
325
+ set draggable(value) {
326
+ this.setAttribute("draggable", String(value));
327
+ }
328
+ set spellcheck(value) {
329
+ this.setAttribute("spellcheck", String(value));
330
+ }
331
+ print(out) {
332
+ out.push(`<${this.localName}`);
333
+ for (const [name, value] of this.#attributes) out.push(value === "" ? ` ${name}` : ` ${name}="${escapeAttribute(value)}"`);
334
+ out.push(">");
335
+ if (this.namespaceURI === null && VOID.has(this.localName)) return;
336
+ this.printChildren(out, this.namespaceURI === null && RAW_TEXT.has(this.localName));
337
+ out.push(`</${this.localName}>`);
338
+ }
339
+ };
340
+ var ServerClassList = class {
341
+ #element;
342
+ constructor(element) {
343
+ this.#element = element;
344
+ }
345
+ #names() {
346
+ return (this.#element.getAttribute("class") ?? "").split(/\s+/).filter(Boolean);
347
+ }
348
+ #write(names) {
349
+ if (names.length === 0) this.#element.removeAttribute("class");
350
+ else this.#element.setAttribute("class", names.join(" "));
351
+ }
352
+ contains(name) {
353
+ return this.#names().includes(name);
354
+ }
355
+ add(...names) {
356
+ const current = this.#names();
357
+ for (const name of names) if (!current.includes(name)) current.push(name);
358
+ this.#write(current);
359
+ }
360
+ remove(...names) {
361
+ this.#write(this.#names().filter((name) => !names.includes(name)));
362
+ }
363
+ toggle(name, force) {
364
+ const on = force ?? !this.contains(name);
365
+ if (on) this.add(name);
366
+ else this.remove(name);
367
+ return on;
368
+ }
369
+ };
370
+ var ServerStyle = class {
371
+ #element;
372
+ #properties = /* @__PURE__ */ new Map();
373
+ constructor(element) {
374
+ this.#element = element;
375
+ }
376
+ getPropertyValue(name) {
377
+ return this.#properties.get(name) ?? "";
378
+ }
379
+ setProperty(name, value) {
380
+ if (value === "") this.#properties.delete(name);
381
+ else this.#properties.set(name, value);
382
+ this.#sync();
383
+ }
384
+ removeProperty(name) {
385
+ const previous = this.getPropertyValue(name);
386
+ this.#properties.delete(name);
387
+ this.#sync();
388
+ return previous;
389
+ }
390
+ get cssText() {
391
+ return [...this.#properties].map(([name, value]) => `${name}: ${value};`).join(" ");
392
+ }
393
+ set cssText(text) {
394
+ this.#properties.clear();
395
+ for (const part of text.split(";")) {
396
+ const colon = part.indexOf(":");
397
+ if (colon > 0) this.#properties.set(part.slice(0, colon).trim(), part.slice(colon + 1).trim());
398
+ }
399
+ this.#sync();
400
+ }
401
+ get display() {
402
+ return this.getPropertyValue("display");
403
+ }
404
+ set display(value) {
405
+ this.setProperty("display", value);
406
+ }
407
+ #sync() {
408
+ if (this.#properties.size === 0) this.#element.removeAttribute("style");
409
+ else this.#element.setAttribute("style", this.cssText);
410
+ }
411
+ };
412
+ function toNode(node) {
413
+ return typeof node === "string" ? new ServerText(node) : node;
414
+ }
415
+ function escapeText(text) {
416
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
417
+ }
418
+ function escapeAttribute(text) {
419
+ return escapeText(text).replace(/"/g, "&quot;");
420
+ }
421
+ function escapeRawText(text) {
422
+ return text.replace(/<\/(script|style)/gi, "<\\/$1");
423
+ }
424
+ var serverHooks = {
425
+ element(tag, namespace) {
426
+ const element = new ServerElement(tag, namespace === "http://www.w3.org/2000/svg" ? SVG_NS : namespace);
427
+ const key = nextKey();
428
+ if (key !== null && insideHydratable()) element.setAttribute("data-k", key);
429
+ return element;
430
+ },
431
+ text: (value) => new ServerText(value),
432
+ marker: () => new ServerComment(""),
433
+ fragment: () => new ServerFragment(),
434
+ html: (markup) => {
435
+ const fragment = new ServerFragment();
436
+ fragment.appendChild(new ServerRawHTML(markup));
437
+ return fragment;
438
+ }
439
+ };
440
+ function printNode(node) {
441
+ const out = [];
442
+ node.print(out, false);
443
+ return out.join("");
444
+ }
445
+ // src/static/render.ts
446
+ var active = 0;
447
+ var previousHooks = null;
448
+ async function renderToString(app, options = {}) {
449
+ if (active++ === 0) previousHooks = setHooks(serverHooks);
450
+ startKeys();
451
+ const loading = /* @__PURE__ */ new Set();
452
+ const data = {};
453
+ const tracker = {
454
+ wait(promise) {
455
+ loading.add(promise);
456
+ const done = () => loading.delete(promise);
457
+ promise.then(done, done);
458
+ },
459
+ store(key, value, owner) {
460
+ if (insideHydratable(owner)) data[key] = value;
461
+ }
462
+ };
463
+ const headTags = createHeadRegistry();
464
+ let dispose = () => {};
465
+ try {
466
+ const page = staticRoot((end) => {
467
+ dispose = end;
468
+ const root = serverHooks.fragment();
469
+ provide(RenderContext, tracker, () => provide(HeadContext, headTags, () => {
470
+ const owner = getOwner();
471
+ owner.key = "0";
472
+ owner.keyCount = 0;
473
+ if (options.hydrate) owner.hydratable = true;
474
+ insert(root, app());
475
+ }));
476
+ return root;
477
+ });
478
+ const timeout = options.timeout ?? 1e4;
479
+ const deadline = Date.now() + timeout;
480
+ while (loading.size > 0) await within(Promise.all(loading), deadline - Date.now(), timeout);
481
+ return {
482
+ html: printNode(page),
483
+ head: printHead(headTags.winners()),
484
+ data: printData(data)
485
+ };
486
+ } finally {
487
+ dispose();
488
+ stopKeys();
489
+ if (--active === 0) setHooks(previousHooks);
490
+ }
491
+ }
492
+ function within(promise, ms, timeout) {
493
+ return new Promise((resolve, reject) => {
494
+ const timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`[keits/static] Data was still loading after ${timeout} ms: the page could not be rendered.`)), Math.max(0, ms));
495
+ promise.then((value) => (clearTimeout(timer), resolve(value)), (error) => (clearTimeout(timer), reject(error)));
496
+ });
497
+ }
498
+ function printHead(tags) {
499
+ let html = "";
500
+ for (const tag of tags.values()) {
501
+ const attrs = Object.entries(tag.attrs).map(([name, value]) => {
502
+ if (!SAFE_NAME.test(name)) throw new Error(`[keits/static] "${name}" is not a valid attribute name.`);
503
+ return ` ${name}="${escapeAttribute(value)}"`;
504
+ }).join("");
505
+ if (tag.tag === "title") html += `<title>${escapeText(tag.text ?? "")}</title>`;
506
+ else if (tag.tag === "script") html += `<script${attrs}>${tag.text ?? ""}<\/script>`;
507
+ else html += `<${tag.tag}${attrs}>`;
508
+ }
509
+ return html;
510
+ }
511
+ function printData(data) {
512
+ if (Object.keys(data).length === 0) return "";
513
+ return `<script type="application/json" id="keits-data">${scriptJson(data)}<\/script>`;
514
+ }
515
+ // src/static/hydrate.ts
516
+ var claims = null;
517
+ var sent = null;
518
+ function lookup(key) {
519
+ if (sent === null) {
520
+ const script = [...document.querySelectorAll("script[type=\"application/json\"]#keits-data")].pop() ?? null;
521
+ sent = script === null ? {} : JSON.parse(script.textContent ?? "{}");
522
+ }
523
+ return Object.hasOwn(sent, key) ? { value: sent[key] } : void 0;
524
+ }
525
+ function resetHydrationData() {
526
+ sent = null;
527
+ }
528
+ var claimHooks = {
529
+ ...defaultHooks,
530
+ element(tag, namespace) {
531
+ const key = nextKey();
532
+ if (claims !== null && key !== null && !claimsPaused()) {
533
+ const found = claims.byKey.get(key);
534
+ if (found !== void 0 && found.localName === (namespace === null ? tag.toLowerCase() : tag)) {
535
+ claims.byKey.delete(key);
536
+ found.removeAttribute("data-k");
537
+ clearUnclaimable(found);
538
+ return found;
539
+ }
540
+ claims.mismatch ??= found === void 0 ? `no <${tag}> with key ${key}` : `<${found.localName}> instead of <${tag}> at key ${key}`;
541
+ }
542
+ return defaultHooks.element(tag, namespace);
543
+ }
544
+ };
545
+ function clearUnclaimable(parent) {
546
+ for (let child = parent.firstChild; child !== null;) {
547
+ const next = child.nextSibling;
548
+ if (!(child.nodeType === 1 && child.hasAttribute("data-k"))) parent.removeChild(child);
549
+ child = next;
550
+ }
551
+ }
552
+ function hydrate(app, container, key = "0") {
553
+ const byKey = /* @__PURE__ */ new Map();
554
+ for (const element of container.querySelectorAll("[data-k]")) byKey.set(element.getAttribute("data-k"), element);
555
+ const current = {
556
+ byKey,
557
+ mismatch: null
558
+ };
559
+ setHydratedValue(lookup);
560
+ const previousClaims = claims;
561
+ const previousHooks = setHooks(claimHooks);
562
+ claims = current;
563
+ startKeys();
564
+ let dispose;
565
+ try {
566
+ clearUnclaimable(container);
567
+ dispose = keyedRoot(key, false, (end) => (insert(container, app()), end));
568
+ } finally {
569
+ stopKeys();
570
+ claims = previousClaims;
571
+ setHooks(previousHooks);
572
+ }
573
+ const leftover = container.querySelector("[data-k]");
574
+ if (current.mismatch === null && leftover === null) return dispose;
575
+ {
576
+ const reason = current.mismatch ?? `the server rendered <${leftover.localName}> (key ${leftover.getAttribute("data-k")}) but the app didn't`;
577
+ console.warn(`[keits] Hydration mismatch: ${reason}. Rendering it again in the browser instead.`);
578
+ }
579
+ dispose();
580
+ container.textContent = "";
581
+ startKeys();
582
+ try {
583
+ return keyedRoot(key, false, (end) => (insert(container, app()), end));
584
+ } finally {
585
+ stopKeys();
586
+ }
587
+ }
588
+ export { head as i, resetHydrationData as n, renderToString as r, hydrate as t };