keits 0.0.2 → 0.0.4

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 (63) hide show
  1. package/dist/dev/api.js +270 -0
  2. package/dist/dev/forms.js +328 -0
  3. package/dist/dev/index.js +290 -294
  4. package/dist/dev/router.js +445 -0
  5. package/dist/dev/shared/computed-3UO-KiH1.js +40 -0
  6. package/dist/dev/shared/element-CQPqagu1.js +222 -0
  7. package/dist/dev/shared/graph-D6-x_CWC.js +324 -0
  8. package/dist/dev/shared/owner-Dmge21OW.js +62 -0
  9. package/dist/dev/shared/render-effect-kOGnLNbL.js +26 -0
  10. package/dist/dev/shared/resource-CbXVATvq.js +154 -0
  11. package/dist/dev/shared/signal-BOXkY-Iv.js +24 -0
  12. package/dist/dev/shared/store-BzUN1qE5.js +148 -0
  13. package/dist/dev/svg.js +1 -1
  14. package/dist/prod/api.js +210 -0
  15. package/dist/prod/forms.js +237 -0
  16. package/dist/prod/index.js +209 -234
  17. package/dist/prod/router.js +345 -0
  18. package/dist/prod/shared/computed-CAPpOLCY.js +33 -0
  19. package/dist/prod/shared/element-DGyQ01Vs.js +164 -0
  20. package/dist/prod/shared/graph-C_aA46c6.js +240 -0
  21. package/dist/prod/shared/owner-6BFHIPR9.js +59 -0
  22. package/dist/prod/shared/render-effect-DjaUT3Cn.js +23 -0
  23. package/dist/prod/shared/resource-CNC2DBO3.js +109 -0
  24. package/dist/prod/shared/signal-DGrl3r9T.js +16 -0
  25. package/dist/prod/shared/store-Dcscpk_h.js +109 -0
  26. package/dist/prod/svg.js +1 -1
  27. package/dist/types/api/cache.d.ts +16 -0
  28. package/dist/types/api/client.d.ts +42 -0
  29. package/dist/types/api/error.d.ts +16 -0
  30. package/dist/types/api/index.d.ts +3 -0
  31. package/dist/types/api/url.d.ts +5 -0
  32. package/dist/types/async/index.d.ts +2 -0
  33. package/dist/types/async/lazy.d.ts +7 -0
  34. package/dist/types/async/resource.d.ts +23 -0
  35. package/dist/types/async/suspense-context.d.ts +4 -0
  36. package/dist/types/core/graph.d.ts +8 -0
  37. package/dist/types/dom/types.d.ts +1 -1
  38. package/dist/types/flow/catch.d.ts +2 -0
  39. package/dist/types/flow/dynamic.d.ts +6 -0
  40. package/dist/types/flow/for.d.ts +7 -0
  41. package/dist/types/flow/if.d.ts +4 -0
  42. package/dist/types/flow/index.d.ts +8 -0
  43. package/dist/types/flow/portal.d.ts +2 -0
  44. package/dist/types/flow/range.d.ts +11 -0
  45. package/dist/types/flow/show.d.ts +1 -0
  46. package/dist/types/flow/suspense.d.ts +2 -0
  47. package/dist/types/flow/switch.d.ts +9 -0
  48. package/dist/types/forms/form.d.ts +55 -0
  49. package/dist/types/forms/index.d.ts +2 -0
  50. package/dist/types/forms/paths.d.ts +6 -0
  51. package/dist/types/forms/types.d.ts +38 -0
  52. package/dist/types/index.d.ts +2 -0
  53. package/dist/types/router/history.d.ts +21 -0
  54. package/dist/types/router/index.d.ts +3 -0
  55. package/dist/types/router/links.d.ts +1 -0
  56. package/dist/types/router/match.d.ts +15 -0
  57. package/dist/types/router/router.d.ts +10 -0
  58. package/dist/types/router/scroll.d.ts +5 -0
  59. package/dist/types/router/types.d.ts +87 -0
  60. package/dist/types/svg/types.d.ts +1 -1
  61. package/package.json +37 -6
  62. package/dist/dev/shared/element-By0uLZli.js +0 -544
  63. package/dist/prod/shared/element-C8c5hIhH.js +0 -394
@@ -0,0 +1,445 @@
1
+ import { t as computed } from "./shared/computed-3UO-KiH1.js";
2
+ import { t as signal } from "./shared/signal-BOXkY-Iv.js";
3
+ import { c as handleError, h as untrack, i as batch, n as addMount, p as runWithOwner, s as getOwner } from "./shared/graph-D6-x_CWC.js";
4
+ import { i as onCleanup } from "./shared/owner-Dmge21OW.js";
5
+ // src/router/history.ts
6
+ var newKey = () => Math.random().toString(36).slice(2, 10);
7
+ var stateKey = () => window.history.state?.keitsKey ?? "start";
8
+ function browserHistory(base = "") {
9
+ const prefix = base.replace(/\/+$/, "");
10
+ return {
11
+ read() {
12
+ let path = window.location.pathname;
13
+ if (prefix !== "" && path.startsWith(prefix)) path = path.slice(prefix.length);
14
+ return {
15
+ path: path || "/",
16
+ search: window.location.search,
17
+ hash: window.location.hash
18
+ };
19
+ },
20
+ push: (url) => window.history.pushState({ keitsKey: newKey() }, "", prefix + url),
21
+ replace: (url) => window.history.replaceState(window.history.state, "", prefix + url),
22
+ listen(callback) {
23
+ window.addEventListener("popstate", callback);
24
+ return () => window.removeEventListener("popstate", callback);
25
+ },
26
+ href: (url) => prefix + url,
27
+ toAppUrl(url) {
28
+ let path = url.pathname;
29
+ if (prefix !== "") {
30
+ if (path !== prefix && !path.startsWith(`${prefix}/`)) return null;
31
+ path = path.slice(prefix.length) || "/";
32
+ }
33
+ return path + url.search + url.hash;
34
+ },
35
+ key: stateKey
36
+ };
37
+ }
38
+ function hashHistory() {
39
+ return {
40
+ read() {
41
+ const raw = window.location.hash.slice(1) || "/";
42
+ return splitUrl(raw.startsWith("/") ? raw : `/${raw}`);
43
+ },
44
+ push: (url) => window.history.pushState({ keitsKey: newKey() }, "", `#${url}`),
45
+ replace: (url) => window.history.replaceState(window.history.state, "", `#${url}`),
46
+ listen(callback) {
47
+ window.addEventListener("popstate", callback);
48
+ window.addEventListener("hashchange", callback);
49
+ return () => {
50
+ window.removeEventListener("popstate", callback);
51
+ window.removeEventListener("hashchange", callback);
52
+ };
53
+ },
54
+ href: (url) => `#${url}`,
55
+ toAppUrl(url) {
56
+ if (url.pathname !== window.location.pathname || url.search !== window.location.search) return null;
57
+ return url.hash.startsWith("#/") ? url.hash.slice(1) : null;
58
+ },
59
+ key: stateKey
60
+ };
61
+ }
62
+ function memoryHistory(initial = "/") {
63
+ const entries = [initial];
64
+ const keys = ["start"];
65
+ let index = 0;
66
+ const listeners = /* @__PURE__ */ new Set();
67
+ const notify = () => {
68
+ for (const listener of listeners) listener();
69
+ };
70
+ return {
71
+ read: () => splitUrl(entries[index]),
72
+ push(url) {
73
+ entries.splice(index + 1, entries.length, url);
74
+ keys.splice(index + 1, keys.length, newKey());
75
+ index++;
76
+ },
77
+ replace(url) {
78
+ entries[index] = url;
79
+ },
80
+ listen(callback) {
81
+ listeners.add(callback);
82
+ return () => listeners.delete(callback);
83
+ },
84
+ href: (url) => url,
85
+ toAppUrl: (url) => url.pathname + url.search + url.hash,
86
+ key: () => keys[index],
87
+ back() {
88
+ if (index > 0) index--;
89
+ notify();
90
+ },
91
+ forward() {
92
+ if (index < entries.length - 1) index++;
93
+ notify();
94
+ }
95
+ };
96
+ }
97
+ function splitUrl(url) {
98
+ const hashAt = url.indexOf("#");
99
+ const hash = hashAt >= 0 ? url.slice(hashAt) : "";
100
+ const rest = hashAt >= 0 ? url.slice(0, hashAt) : url;
101
+ const searchAt = rest.indexOf("?");
102
+ const search = searchAt >= 0 ? rest.slice(searchAt) : "";
103
+ return {
104
+ path: (searchAt >= 0 ? rest.slice(0, searchAt) : rest) || "/",
105
+ search: search === "?" ? "" : search,
106
+ hash: hash === "#" ? "" : hash
107
+ };
108
+ }
109
+ // src/router/links.ts
110
+ function linkTarget(event, toAppUrl) {
111
+ if (event.defaultPrevented || event.button !== 0) return null;
112
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return null;
113
+ const start = event.target;
114
+ if (!(start instanceof Element)) return null;
115
+ const link = start.closest("a");
116
+ if (link === null) return null;
117
+ const href = link.getAttribute("href");
118
+ if (href === null || link.hasAttribute("download") || link.hasAttribute("data-native")) return null;
119
+ const target = link.getAttribute("target");
120
+ if (target !== null && target !== "" && target !== "_self") return null;
121
+ if (/(^|\s)external(\s|$)/i.test(link.getAttribute("rel") ?? "")) return null;
122
+ let url;
123
+ try {
124
+ url = new URL(href, document.baseURI);
125
+ } catch {
126
+ return null;
127
+ }
128
+ if (url.origin !== window.location.origin) return null;
129
+ return toAppUrl(url);
130
+ }
131
+ // src/router/match.ts
132
+ function joinPath(parent, path) {
133
+ if (path.startsWith("/")) return path;
134
+ if (path === "") return parent === "" ? "/" : parent;
135
+ if (parent === "" || parent === "/") return `/${path}`;
136
+ return `${parent}/${path}`;
137
+ }
138
+ function compileRoutes(routes) {
139
+ const branches = [];
140
+ const walk = (list, parentPath, parents) => {
141
+ for (const route of list) {
142
+ const path = joinPath(parentPath, route.path);
143
+ const chain = [...parents, route];
144
+ if (route.children && route.children.length > 0) walk(route.children, path, chain);
145
+ if (route.component || !route.children) branches.push(compileBranch(path, chain));
146
+ }
147
+ };
148
+ walk(routes, "", []);
149
+ return branches.map((branch, order) => ({
150
+ branch,
151
+ order
152
+ })).sort((a, b) => b.branch.score - a.branch.score || a.order - b.order).map(({ branch }) => branch);
153
+ }
154
+ function compileBranch(path, routes) {
155
+ const keys = [];
156
+ let score = 0;
157
+ const source = path.split("/").filter((segment) => segment !== "").map((segment) => {
158
+ if (segment === "*") {
159
+ keys.push("*");
160
+ score -= 100;
161
+ return "(?:/(.*))?";
162
+ }
163
+ if (segment.startsWith(":")) {
164
+ keys.push(segment.slice(1));
165
+ score += 2;
166
+ return "/([^/]+)";
167
+ }
168
+ score += 3;
169
+ return `/${segment.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`;
170
+ }).join("");
171
+ return {
172
+ path,
173
+ routes,
174
+ keys,
175
+ score,
176
+ pattern: new RegExp(`^${source || ""}/?$`)
177
+ };
178
+ }
179
+ function matchPath(branches, pathname) {
180
+ for (const branch of branches) {
181
+ const result = branch.pattern.exec(pathname);
182
+ if (result === null) continue;
183
+ const params = {};
184
+ branch.keys.forEach((key, i) => {
185
+ params[key] = safeDecode(result[i + 1] ?? "");
186
+ });
187
+ return {
188
+ path: branch.path,
189
+ routes: branch.routes,
190
+ params
191
+ };
192
+ }
193
+ return null;
194
+ }
195
+ function safeDecode(value) {
196
+ try {
197
+ return decodeURIComponent(value);
198
+ } catch {
199
+ return value;
200
+ }
201
+ }
202
+ function branchSchema(match) {
203
+ const schema = {};
204
+ for (const route of match?.routes ?? []) Object.assign(schema, route.query);
205
+ return schema;
206
+ }
207
+ function parseQuery(schema, search) {
208
+ const raw = new URLSearchParams(search);
209
+ const values = {};
210
+ for (const [key, spec] of Object.entries(schema)) {
211
+ const text = raw.get(key);
212
+ if (text === null) continue;
213
+ if (spec === "string") values[key] = text;
214
+ else if (spec === "number") {
215
+ const n = Number(text);
216
+ if (text.trim() !== "" && Number.isFinite(n)) values[key] = n;
217
+ } else if (spec === "boolean") {
218
+ if (text === "true" || text === "1" || text === "") values[key] = true;
219
+ else if (text === "false" || text === "0") values[key] = false;
220
+ } else if (spec.includes(text)) values[key] = text;
221
+ }
222
+ return values;
223
+ }
224
+ function buildSearch(values) {
225
+ const search = new URLSearchParams();
226
+ for (const [key, value] of Object.entries(values)) if (value !== void 0) search.set(key, String(value));
227
+ const text = search.toString();
228
+ return text === "" ? "" : `?${text}`;
229
+ }
230
+ function fillParams(path, params) {
231
+ return path.replace(/:([^/]+)/g, (_, name) => {
232
+ const value = params?.[name];
233
+ if (value === void 0) throw new Error(`[keits/router] Missing param "${name}" for "${path}".`);
234
+ return encodeURIComponent(String(value));
235
+ });
236
+ }
237
+ // src/router/scroll.ts
238
+ function createScroller() {
239
+ const positions = /* @__PURE__ */ new Map();
240
+ if ("scrollRestoration" in window.history) window.history.scrollRestoration = "manual";
241
+ return {
242
+ save(key) {
243
+ positions.set(key, [window.scrollX, window.scrollY]);
244
+ },
245
+ restore(key, popped, pathChanged, hash, hashChanged) {
246
+ const saved = popped ? positions.get(key) : void 0;
247
+ if (saved !== void 0) {
248
+ window.scrollTo(saved[0], saved[1]);
249
+ return;
250
+ }
251
+ if (hash !== "" && (pathChanged || hashChanged)) {
252
+ const element = document.getElementById(decodeHash(hash));
253
+ if (element !== null) {
254
+ element.scrollIntoView();
255
+ return;
256
+ }
257
+ }
258
+ if (pathChanged) window.scrollTo(0, 0);
259
+ }
260
+ };
261
+ }
262
+ function decodeHash(hash) {
263
+ try {
264
+ return decodeURIComponent(hash.slice(1));
265
+ } catch {
266
+ return hash.slice(1);
267
+ }
268
+ }
269
+ // src/router/router.ts
270
+ function createRouter(config) {
271
+ const history = typeof config.history === "object" ? config.history : config.history === "hash" ? hashHistory() : browserHistory(config.base);
272
+ const owner = getOwner();
273
+ const branches = compileRoutes(config.routes);
274
+ const sameLocation = (a, b) => a.path === b.path && a.search === b.search && a.hash === b.hash;
275
+ const location = signal(history.read(), { equals: sameLocation });
276
+ const started = signal(false);
277
+ const pending = signal(false);
278
+ const leaveChecks = /* @__PURE__ */ new Set();
279
+ let navigationId = 0;
280
+ const scroller = config.scroll ?? typeof window !== "undefined" ? createScroller() : null;
281
+ let currentKey = history.key();
282
+ const match = computed(() => started() ? matchPath(branches, location().path) : null);
283
+ const params = computed(() => match()?.params ?? {}, { equals: shallowEqual });
284
+ const query = computed(() => parseQuery(branchSchema(match()), location().search), { equals: shallowEqual });
285
+ const toUrl = (path, options) => {
286
+ const filled = fillParams(path, options?.params);
287
+ const search = options?.query ? buildSearch(options.query) : "";
288
+ const hash = options?.hash ? `#${options.hash.replace(/^#/, "")}` : "";
289
+ return filled + search + hash;
290
+ };
291
+ function resolve(url, mode, redirects = 0) {
292
+ const id = ++navigationId;
293
+ const target = splitUrl(url);
294
+ const from = untrack(started) ? untrack(location) : null;
295
+ const commit = () => {
296
+ if (from !== null) scroller?.save(currentKey);
297
+ if (mode === "push") history.push(url);
298
+ else if (mode === "replace" || redirects > 0) history.replace(url);
299
+ currentKey = history.key();
300
+ const key = currentKey;
301
+ batch(() => {
302
+ pending.set(false);
303
+ started.set(true);
304
+ location.set(history.read());
305
+ if (scroller !== null && from !== null) {
306
+ const pathChanged = from.path !== target.path;
307
+ const hashChanged = from.hash !== target.hash;
308
+ runWithOwner(null, () => addMount(() => scroller.restore(key, mode === "pop", pathChanged, target.hash, hashChanged)));
309
+ }
310
+ });
311
+ return true;
312
+ };
313
+ if (from !== null && from.path === target.path) return Promise.resolve(commit());
314
+ const found = matchPath(branches, target.path);
315
+ const to = {
316
+ path: target.path,
317
+ route: found?.path ?? null,
318
+ params: found?.params ?? {},
319
+ search: target.search,
320
+ from: from?.path ?? null
321
+ };
322
+ const checks = [];
323
+ if (from !== null) for (const check of leaveChecks) checks.push(() => check(to));
324
+ for (const route of found?.routes ?? []) {
325
+ const guard = route.guard;
326
+ if (guard) checks.push(() => guard(to));
327
+ }
328
+ const finish = (result) => {
329
+ if (id !== navigationId) return false;
330
+ if (result === true) return commit();
331
+ pending.set(false);
332
+ if (typeof result === "string") {
333
+ if (redirects >= 10) throw new Error(`[keits/router] Too many redirects (last: "${result}"). Do two guards redirect to each other?`);
334
+ return resolve(result, mode === "pop" ? "replace" : mode, redirects + 1);
335
+ }
336
+ if (mode === "pop" && from !== null) {
337
+ history.push(from.path + from.search + from.hash);
338
+ currentKey = history.key();
339
+ }
340
+ return false;
341
+ };
342
+ const fail = (error) => {
343
+ if (id === navigationId) pending.set(false);
344
+ try {
345
+ handleError(error, owner);
346
+ return Promise.resolve(false);
347
+ } catch (unhandled) {
348
+ return Promise.reject(unhandled);
349
+ }
350
+ };
351
+ const safeFinish = (result) => {
352
+ try {
353
+ return Promise.resolve(finish(result));
354
+ } catch (error) {
355
+ return fail(error);
356
+ }
357
+ };
358
+ let outcome;
359
+ try {
360
+ outcome = runChecks(checks, 0);
361
+ } catch (error) {
362
+ return fail(error);
363
+ }
364
+ if (!isPromiseLike(outcome)) return safeFinish(outcome);
365
+ pending.set(true);
366
+ return outcome.then(safeFinish, fail);
367
+ }
368
+ const stopListening = history.listen(() => {
369
+ const now = history.read();
370
+ resolve(now.path + now.search + now.hash, "pop");
371
+ });
372
+ const initial = history.read();
373
+ resolve(initial.path + initial.search + initial.hash, "start");
374
+ const onClick = (event) => {
375
+ const url = linkTarget(event, (target) => history.toAppUrl(target));
376
+ if (url === null) return;
377
+ event.preventDefault();
378
+ const now = untrack(location);
379
+ resolve(url, url === now.path + now.search + now.hash ? "replace" : "push");
380
+ };
381
+ const listenToLinks = config.links !== false && typeof document !== "undefined";
382
+ if (listenToLinks) document.addEventListener("click", onClick);
383
+ return {
384
+ location: () => location(),
385
+ match: () => match(),
386
+ params: () => params(),
387
+ query: () => query(),
388
+ pending: () => pending(),
389
+ navigate: (path, options) => resolve(path.includes(":") || options?.query || options?.hash ? toUrl(path, options) : path, options?.replace ? "replace" : "push"),
390
+ href: (path, options) => history.href(toUrl(path, options)),
391
+ setQuery(values, options) {
392
+ const current = untrack(location);
393
+ const merged = {
394
+ ...Object.fromEntries(new URLSearchParams(current.search)),
395
+ ...values
396
+ };
397
+ resolve(current.path + buildSearch(merged) + current.hash, options?.push ? "push" : "replace");
398
+ },
399
+ onLeave(check) {
400
+ leaveChecks.add(check);
401
+ onCleanup(() => leaveChecks.delete(check));
402
+ },
403
+ view() {
404
+ const level = (depth, parent) => {
405
+ const route = computed(() => match()?.routes[depth] ?? null);
406
+ return () => {
407
+ const current = route();
408
+ return untrack(() => {
409
+ if (current === null) return parent?.component ? parent.component() : null;
410
+ const outlet = level(depth + 1, current);
411
+ return current.layout ? current.layout({ children: outlet }) : outlet;
412
+ });
413
+ };
414
+ };
415
+ return level(0, null);
416
+ },
417
+ isActive(path, exact = false) {
418
+ const current = location().path.replace(/\/$/, "") || "/";
419
+ const target = splitUrl(path).path.replace(/\/$/, "") || "/";
420
+ if (exact) return current === target;
421
+ return current === target || current.startsWith(`${target}/`);
422
+ },
423
+ dispose() {
424
+ stopListening();
425
+ if (listenToLinks) document.removeEventListener("click", onClick);
426
+ }
427
+ };
428
+ }
429
+ function runChecks(checks, from) {
430
+ for (let i = from; i < checks.length; i++) {
431
+ const result = checks[i]();
432
+ if (isPromiseLike(result)) return Promise.resolve(result).then((value) => value === true ? runChecks(checks, i + 1) : value);
433
+ if (result !== true) return result;
434
+ }
435
+ return true;
436
+ }
437
+ function isPromiseLike(value) {
438
+ return typeof value?.then === "function";
439
+ }
440
+ function shallowEqual(a, b) {
441
+ const keys = Object.keys(a);
442
+ if (keys.length !== Object.keys(b).length) return false;
443
+ return keys.every((key) => Object.is(a[key], b[key]));
444
+ }
445
+ export { browserHistory, createRouter, hashHistory, memoryHistory };
@@ -0,0 +1,40 @@
1
+ import { g as updateIfNeeded, m as track, r as adopt } from "./graph-D6-x_CWC.js";
2
+ // src/core/computed.ts
3
+ function computed(fn, options) {
4
+ const node = {
5
+ kind: 2,
6
+ fn,
7
+ value: void 0,
8
+ equals: options?.equals ?? Object.is,
9
+ hasValue: false,
10
+ hasError: false,
11
+ error: void 0,
12
+ computing: false,
13
+ state: 2,
14
+ sources: [],
15
+ observers: [],
16
+ seenBy: 0,
17
+ flushId: 0,
18
+ flushRuns: 0,
19
+ owner: null,
20
+ children: null,
21
+ cleanups: null,
22
+ disposed: false
23
+ };
24
+ if (options?.name !== void 0) node.name = options.name;
25
+ adopt(node);
26
+ return () => {
27
+ if (node.computing) throw cycleError(node);
28
+ if (node.state !== 0) updateIfNeeded(node);
29
+ track(node);
30
+ if (node.hasError) throw node.error;
31
+ return node.value;
32
+ };
33
+ }
34
+ function cycleError(node) {
35
+ {
36
+ const who = node.name ? `"${node.name}"` : "a computed";
37
+ return /* @__PURE__ */ new Error(`[keits] Cycle detected: ${who} reads itself (directly or through other computeds).`);
38
+ }
39
+ }
40
+ export { computed as t };
@@ -0,0 +1,222 @@
1
+ import { t as renderEffect } from "./render-effect-kOGnLNbL.js";
2
+ import { c as handleError, i as batch, l as isStaticRender, p as runWithOwner, s as getOwner } from "./graph-D6-x_CWC.js";
3
+ var hooks = {
4
+ element: (tag, namespace) => namespace === null ? document.createElement(tag) : document.createElementNS(namespace, tag),
5
+ text: (value) => document.createTextNode(value),
6
+ marker: () => document.createComment("")
7
+ };
8
+ // src/dom/insert.ts
9
+ function insert(parent, child, before = null) {
10
+ if (child === null || child === void 0 || typeof child === "boolean") return;
11
+ if (typeof child === "function") return insertDynamic(parent, child, before);
12
+ if (isChildArray(child)) {
13
+ for (const item of child) insert(parent, item, before);
14
+ return;
15
+ }
16
+ if (typeof child === "object") {
17
+ parent.insertBefore(child, before);
18
+ return;
19
+ }
20
+ parent.insertBefore(hooks.text(String(child)), before);
21
+ }
22
+ function isChildArray(value) {
23
+ return Array.isArray(value);
24
+ }
25
+ function isTextValue(value) {
26
+ return value === null || typeof value !== "object" && typeof value !== "function";
27
+ }
28
+ function toText(value) {
29
+ return value === null || value === void 0 || typeof value === "boolean" ? "" : String(value);
30
+ }
31
+ function insertDynamic(parent, fn, before) {
32
+ const text = hooks.text("");
33
+ parent.insertBefore(text, before);
34
+ let start = null;
35
+ let end = null;
36
+ renderEffect(() => {
37
+ const value = fn();
38
+ if (start === null || end === null) {
39
+ if (isTextValue(value)) {
40
+ const next = toText(value);
41
+ if (text.data !== next) text.data = next;
42
+ return;
43
+ }
44
+ start = hooks.marker();
45
+ end = hooks.marker();
46
+ text.before(start);
47
+ text.after(end);
48
+ text.remove();
49
+ }
50
+ clearRange(start, end);
51
+ insert(end.parentNode, value, end);
52
+ });
53
+ }
54
+ function clearRange(start, end) {
55
+ let node = start.nextSibling;
56
+ while (node !== null && node !== end) {
57
+ const next = node.nextSibling;
58
+ node.remove();
59
+ node = next;
60
+ }
61
+ }
62
+ // src/dom/events.ts
63
+ function applyEvent(element, key, value) {
64
+ let handler = value;
65
+ let options;
66
+ if (Array.isArray(value)) [handler, options] = value;
67
+ if (typeof handler !== "function") throw new Error(`[keits] "${key}" must be a function, not a ${typeof handler}.`);
68
+ if (isStaticRender()) return;
69
+ listen(element, key.slice(2), handler, options);
70
+ }
71
+ function listen(element, type, handler, options) {
72
+ const owner = getOwner();
73
+ element.addEventListener(type, (event) => {
74
+ batch(() => {
75
+ try {
76
+ const result = runWithOwner(owner, () => handler(event));
77
+ if (isPromiseLike(result)) result.then(void 0, (error) => batch(() => handleError(error, owner)));
78
+ } catch (error) {
79
+ handleError(error, owner);
80
+ }
81
+ });
82
+ }, options);
83
+ }
84
+ function isPromiseLike(value) {
85
+ return typeof value?.then === "function";
86
+ }
87
+ // src/dom/namespaces.ts
88
+ var SVG_NS = "http://www.w3.org/2000/svg";
89
+ // src/dom/props.ts
90
+ var UNSAFE_PROPS = /* @__PURE__ */ new Set([
91
+ "innerHTML",
92
+ "outerHTML",
93
+ "srcdoc"
94
+ ]);
95
+ var URL_PROPS = /* @__PURE__ */ new Set([
96
+ "href",
97
+ "src",
98
+ "action",
99
+ "formAction",
100
+ "formaction",
101
+ "poster",
102
+ "cite",
103
+ "data",
104
+ "xlink:href"
105
+ ]);
106
+ var BLOCKED_URL = "about:invalid#keits-blocked";
107
+ function applyProps(element, props) {
108
+ for (const key in props) {
109
+ const value = props[key];
110
+ if (key === "ref") continue;
111
+ if (key === "class") applyClass(element, value);
112
+ else if (key === "style") applyStyle(element, value);
113
+ else if (key.startsWith("on")) applyEvent(element, key, value);
114
+ else if (typeof value === "function") renderEffect(() => setProp(element, key, value()));
115
+ else setProp(element, key, value);
116
+ }
117
+ }
118
+ function setProp(element, key, value) {
119
+ if (UNSAFE_PROPS.has(key)) throw new Error(`[keits] "${key}" is not allowed as a prop: it would allow HTML injection.`);
120
+ if (URL_PROPS.has(key) && typeof value === "string" && isJavaScriptUrl(value)) {
121
+ console.warn(`[keits] Blocked a javascript: URL in "${key}".`);
122
+ value = BLOCKED_URL;
123
+ }
124
+ if (key in element && element.namespaceURI !== "http://www.w3.org/2000/svg") {
125
+ element[key] = value;
126
+ if (isStaticRender()) reflectForStatic(element, key, value);
127
+ } else if (value === null || value === void 0 || value === false) element.removeAttribute(key);
128
+ else element.setAttribute(key, value === true ? "" : String(value));
129
+ }
130
+ function isJavaScriptUrl(url) {
131
+ return url.replace(/[\u0000-\u0020]/g, "").toLowerCase().startsWith("javascript");
132
+ }
133
+ function reflectForStatic(element, key, value) {
134
+ if (key === "value") {
135
+ if (element.localName === "textarea") element.textContent = value == null ? "" : String(value);
136
+ else if (value == null) element.removeAttribute("value");
137
+ else element.setAttribute("value", String(value));
138
+ } else if (key === "checked" || key === "selected") element.toggleAttribute(key, value === true);
139
+ }
140
+ function applyClass(element, value) {
141
+ if (typeof value === "function") {
142
+ renderEffect(() => writeClass(element, toClassString(value())));
143
+ return;
144
+ }
145
+ const fixed = [];
146
+ collectClasses(value, fixed, (name, on) => {
147
+ renderEffect(() => element.classList.toggle(name, on()));
148
+ });
149
+ if (fixed.length > 0) element.classList.add(...fixed);
150
+ }
151
+ function writeClass(element, className) {
152
+ if (className === "") element.removeAttribute("class");
153
+ else element.setAttribute("class", className);
154
+ }
155
+ function collectClasses(value, out, onReactive) {
156
+ if (!value) return;
157
+ if (typeof value === "string") {
158
+ for (const name of value.split(/\s+/)) if (name) out.push(name);
159
+ } else if (Array.isArray(value)) for (const item of value) collectClasses(item, out, onReactive);
160
+ else for (const [name, on] of Object.entries(value)) if (typeof on === "function") {
161
+ if (onReactive) onReactive(name, on);
162
+ else if (on()) out.push(name);
163
+ } else if (on) out.push(name);
164
+ }
165
+ function toClassString(value) {
166
+ const names = [];
167
+ collectClasses(value, names);
168
+ return names.join(" ");
169
+ }
170
+ function applyStyle(element, value) {
171
+ const style = element.style;
172
+ if (typeof value === "function") {
173
+ let previous = [];
174
+ renderEffect(() => {
175
+ const next = value();
176
+ if (typeof next === "string" || next == null) {
177
+ style.cssText = next ?? "";
178
+ previous = [];
179
+ return;
180
+ }
181
+ const keys = Object.keys(next);
182
+ for (const name of previous) if (!(name in next)) style.removeProperty(name);
183
+ for (const name of keys) writeStyle(style, name, readStyle(next[name]));
184
+ previous = keys;
185
+ });
186
+ return;
187
+ }
188
+ if (typeof value === "string") {
189
+ style.cssText = value;
190
+ return;
191
+ }
192
+ if (value == null) return;
193
+ for (const [name, entry] of Object.entries(value)) if (typeof entry === "function") renderEffect(() => writeStyle(style, name, entry()));
194
+ else writeStyle(style, name, entry);
195
+ }
196
+ function readStyle(entry) {
197
+ return typeof entry === "function" ? entry() : entry;
198
+ }
199
+ function writeStyle(style, name, value) {
200
+ if (value === null || value === void 0 || value === "") style.removeProperty(name);
201
+ else style.setProperty(name, String(value));
202
+ }
203
+ // src/dom/element.ts
204
+ function html(tag) {
205
+ return createFactory(tag, null);
206
+ }
207
+ function createFactory(tag, namespace) {
208
+ return (first, children) => {
209
+ const element = hooks.element(tag, namespace);
210
+ if (isProps(first)) {
211
+ insert(element, children);
212
+ applyProps(element, first);
213
+ if (typeof first.ref === "function") first.ref(element);
214
+ } else if (first === null || first === void 0) insert(element, children);
215
+ else insert(element, first);
216
+ return element;
217
+ };
218
+ }
219
+ function isProps(value) {
220
+ return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.nodeType !== "number";
221
+ }
222
+ export { hooks as a, insert as i, html as n, SVG_NS as r, createFactory as t };