@tschk/moonshine-dom 0.0.0-stage → 0.4.49
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/LICENSE +15 -0
- package/README.md +467 -2
- package/package.json +62 -4
- package/src/attributes.ts +56 -0
- package/src/binding.ts +430 -0
- package/src/cleanup-order.ts +16 -0
- package/src/component-instance.ts +291 -0
- package/src/component.ts +187 -0
- package/src/control.ts +469 -0
- package/src/custom-element.ts +54 -0
- package/src/dom.ts +870 -0
- package/src/error.ts +59 -0
- package/src/framework-lazy.ts +20 -0
- package/src/framework-native.ts +141 -0
- package/src/framework-source.ts +28 -0
- package/src/head-ssr.ts +108 -0
- package/src/head.ts +34 -0
- package/src/html.ts +54 -0
- package/src/jsx-types.d.ts +19 -0
- package/src/platform.d.ts +4 -0
- package/src/platform.js +4 -0
- package/src/reactive.ts +693 -0
- package/src/render-scheduler.ts +17 -0
- package/src/resource-ssr.ts +73 -0
- package/src/resource.ts +195 -0
- package/src/rotating-text.ts +38 -0
- package/src/ssr-attempt.ts +64 -0
- package/src/ssr-owner.ts +38 -0
- package/src/ssr.ts +874 -0
- package/src/store.ts +527 -0
- package/src/style.ts +30 -0
- package/src/template-retention.ts +4 -0
package/src/dom.ts
ADDED
|
@@ -0,0 +1,870 @@
|
|
|
1
|
+
import { cleanupOrderKey, type CleanupPolicy } from "./cleanup-order";
|
|
2
|
+
/** Internal one-shot callback after native range insertion. */
|
|
3
|
+
export const insertedNode = /* @__PURE__ */ Symbol(
|
|
4
|
+
"moonshine DOM inserted node",
|
|
5
|
+
);
|
|
6
|
+
/** Optional adapter event registration; absent for the native factory. */
|
|
7
|
+
export const eventRegistration = /* @__PURE__ */ Symbol(
|
|
8
|
+
"moonshine DOM event registration",
|
|
9
|
+
);
|
|
10
|
+
export type EventRegistration = (
|
|
11
|
+
element: Element,
|
|
12
|
+
name: string,
|
|
13
|
+
value: unknown,
|
|
14
|
+
) => () => void;
|
|
15
|
+
|
|
16
|
+
/** Independent DOM helpers for Moonshine lowering, not Solid's compiled ABI. */
|
|
17
|
+
import {
|
|
18
|
+
createRenderEffect,
|
|
19
|
+
createRoot,
|
|
20
|
+
createComponentScope,
|
|
21
|
+
getOwner,
|
|
22
|
+
runWithOwner,
|
|
23
|
+
onCleanup,
|
|
24
|
+
onBeforeDispose,
|
|
25
|
+
templateRetentionKey,
|
|
26
|
+
untrack,
|
|
27
|
+
withCleanupTransaction,
|
|
28
|
+
} from "./reactive";
|
|
29
|
+
import { attributeValue } from "./attributes";
|
|
30
|
+
import { styleEntries } from "./style";
|
|
31
|
+
import { createComponent } from "./component";
|
|
32
|
+
|
|
33
|
+
export type DOMChild =
|
|
34
|
+
| Node
|
|
35
|
+
| string
|
|
36
|
+
| number
|
|
37
|
+
| bigint
|
|
38
|
+
| boolean
|
|
39
|
+
| null
|
|
40
|
+
| undefined
|
|
41
|
+
| readonly DOMChild[]
|
|
42
|
+
| (() => DOMChild);
|
|
43
|
+
|
|
44
|
+
const SVG = "http://www.w3.org/2000/svg";
|
|
45
|
+
const HTML = "http://www.w3.org/1999/xhtml";
|
|
46
|
+
const SVG_TAGS = new Set(
|
|
47
|
+
"animate animateMotion animateTransform circle clipPath defs desc ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter foreignObject g image line linearGradient marker mask metadata mpath path pattern polygon polyline radialGradient rect set stop switch symbol text textPath tspan use view".split(
|
|
48
|
+
" ",
|
|
49
|
+
),
|
|
50
|
+
);
|
|
51
|
+
let namespace = HTML;
|
|
52
|
+
const selectUpdates = new WeakMap<Node, () => void>();
|
|
53
|
+
const styleNames = new WeakMap<Element, Set<string>>();
|
|
54
|
+
|
|
55
|
+
function childrenNamespace(parent: Node): string {
|
|
56
|
+
return parent instanceof Element &&
|
|
57
|
+
parent.namespaceURI === SVG &&
|
|
58
|
+
parent.localName !== "foreignObject"
|
|
59
|
+
? SVG
|
|
60
|
+
: HTML;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function dynamic(child: DOMChild): boolean {
|
|
64
|
+
return (
|
|
65
|
+
typeof child === "function" || (Array.isArray(child) && child.some(dynamic))
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function values(
|
|
70
|
+
child: DOMChild,
|
|
71
|
+
result: Array<Node | string> = [],
|
|
72
|
+
): Array<Node | string> {
|
|
73
|
+
if (typeof child === "function") return values(child(), result);
|
|
74
|
+
if (Array.isArray(child)) {
|
|
75
|
+
for (const value of child) values(value, result);
|
|
76
|
+
} else if (child instanceof Node) {
|
|
77
|
+
result.push(child);
|
|
78
|
+
} else if (child != null && typeof child !== "boolean") {
|
|
79
|
+
result.push(String(child));
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function nodes(child: DOMChild, previous: Node[]): Node[] {
|
|
85
|
+
while (
|
|
86
|
+
typeof child === "function" ||
|
|
87
|
+
(Array.isArray(child) && child.length === 1)
|
|
88
|
+
) {
|
|
89
|
+
child = typeof child === "function" ? child() : child[0];
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
(typeof child === "string" ||
|
|
93
|
+
typeof child === "number" ||
|
|
94
|
+
typeof child === "bigint") &&
|
|
95
|
+
previous.length === 1 &&
|
|
96
|
+
previous[0] instanceof Text
|
|
97
|
+
) {
|
|
98
|
+
const value = String(child);
|
|
99
|
+
if (previous[0].data !== value) previous[0].data = value;
|
|
100
|
+
return previous;
|
|
101
|
+
}
|
|
102
|
+
const resolved = values(child);
|
|
103
|
+
const reserved = new Set(
|
|
104
|
+
resolved.filter((value): value is Node => value instanceof Node),
|
|
105
|
+
);
|
|
106
|
+
return resolved.map((value, index) => {
|
|
107
|
+
if (value instanceof Node) return value;
|
|
108
|
+
const old = previous[index];
|
|
109
|
+
if (old instanceof Text && !reserved.has(old)) {
|
|
110
|
+
if (old.data !== value) old.data = value;
|
|
111
|
+
return old;
|
|
112
|
+
}
|
|
113
|
+
return document.createTextNode(value);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function refreshSelect(parent: Node): void {
|
|
118
|
+
for (let node: Node | null = parent; node; node = node.parentNode) {
|
|
119
|
+
const update = selectUpdates.get(node);
|
|
120
|
+
if (update) {
|
|
121
|
+
untrack(update);
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const nodeRange = /* @__PURE__ */ Symbol("native-node-range");
|
|
128
|
+
type NodeRange = {
|
|
129
|
+
readonly disposed: boolean;
|
|
130
|
+
schedule?: (update: () => void) => unknown;
|
|
131
|
+
retain?: (node: Node) => boolean;
|
|
132
|
+
end: Node;
|
|
133
|
+
contents: readonly Node[];
|
|
134
|
+
valid: () => boolean;
|
|
135
|
+
dispose: () => void;
|
|
136
|
+
move: (parent: Node, before: Node) => void;
|
|
137
|
+
};
|
|
138
|
+
function range(node: Node): NodeRange | undefined {
|
|
139
|
+
return (node as Node & { [nodeRange]?: NodeRange })[nodeRange];
|
|
140
|
+
}
|
|
141
|
+
function rangeOwned(node: Node): Set<Node> {
|
|
142
|
+
const owned = new Set<Node>();
|
|
143
|
+
const collect = (value: Node) => {
|
|
144
|
+
const nested = range(value);
|
|
145
|
+
// Retired child ranges have already removed their owned nodes.
|
|
146
|
+
if (owned.has(value) || (value !== node && nested?.disposed)) return;
|
|
147
|
+
owned.add(value);
|
|
148
|
+
if (nested) {
|
|
149
|
+
for (const child of nested.contents) collect(child);
|
|
150
|
+
collect(nested.end);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
collect(node);
|
|
154
|
+
return owned;
|
|
155
|
+
}
|
|
156
|
+
/** Validate the complete boundary before mutating any sibling. */
|
|
157
|
+
function rangeSpan(node: Node, end: Node): Node[] | undefined {
|
|
158
|
+
const parent = node.parentNode;
|
|
159
|
+
if (!parent || end.parentNode !== parent) return;
|
|
160
|
+
const metadata = range(node);
|
|
161
|
+
const owned = metadata ? rangeOwned(node) : undefined;
|
|
162
|
+
const result: Node[] = [];
|
|
163
|
+
for (
|
|
164
|
+
let current: Node | null = node;
|
|
165
|
+
current;
|
|
166
|
+
current = current.nextSibling
|
|
167
|
+
) {
|
|
168
|
+
if (owned && !owned.has(current)) return;
|
|
169
|
+
result.push(current);
|
|
170
|
+
if (current === end)
|
|
171
|
+
return !owned || result.length === owned.size ? result : undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function removeNode(node: Node, retain?: (node: Node) => boolean): void {
|
|
175
|
+
const metadata = range(node);
|
|
176
|
+
if (metadata) metadata.dispose();
|
|
177
|
+
else if (!retain?.(node)) node.parentNode?.removeChild(node);
|
|
178
|
+
}
|
|
179
|
+
function removeNodes(
|
|
180
|
+
ownedNodes: readonly Node[],
|
|
181
|
+
retained?: ReadonlySet<Node>,
|
|
182
|
+
retain?: (node: Node) => boolean,
|
|
183
|
+
): void {
|
|
184
|
+
let failed = false;
|
|
185
|
+
let failure: unknown;
|
|
186
|
+
for (const node of ownedNodes) {
|
|
187
|
+
if (retained?.has(node)) continue;
|
|
188
|
+
try {
|
|
189
|
+
removeNode(node, retain);
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (!failed) failure = error;
|
|
192
|
+
failed = true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (failed) throw failure;
|
|
196
|
+
}
|
|
197
|
+
function moveNode(parent: Node, node: Node, before: Node): void {
|
|
198
|
+
const metadata = range(node);
|
|
199
|
+
if (metadata) metadata.move(parent, before);
|
|
200
|
+
else if (node.nextSibling !== before) parent.insertBefore(node, before);
|
|
201
|
+
}
|
|
202
|
+
/** Internal range operations; adapters must not inspect marker fields. */
|
|
203
|
+
export function insertionRangeEnd(start: Node): Node {
|
|
204
|
+
const metadata = range(start);
|
|
205
|
+
if (!metadata) throw new Error("Not an insertion range");
|
|
206
|
+
return metadata.end;
|
|
207
|
+
}
|
|
208
|
+
export function prepareInsertionRange(start: Node): void {
|
|
209
|
+
const ready = (start as Node & { [insertedNode]?: () => void })[insertedNode];
|
|
210
|
+
if (ready) {
|
|
211
|
+
delete (start as Node & { [insertedNode]?: () => void })[insertedNode];
|
|
212
|
+
ready();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
export function moveInsertionRange(start: Node, before: Node): void {
|
|
216
|
+
if (!before.parentNode) throw new Error("Missing insertion range target");
|
|
217
|
+
moveNode(before.parentNode, start, before);
|
|
218
|
+
}
|
|
219
|
+
/** Create a live insertion range; schedule is an internal compiler-owned phase seam. */
|
|
220
|
+
export function createInsertionRange(
|
|
221
|
+
child: DOMChild,
|
|
222
|
+
schedule?: (update: () => void) => unknown,
|
|
223
|
+
retain?: (node: Node) => boolean,
|
|
224
|
+
): Node {
|
|
225
|
+
const owner = getOwner();
|
|
226
|
+
if (!owner || owner.disposed) throw new Error("Range requires a live owner");
|
|
227
|
+
retain ??= (
|
|
228
|
+
owner.context?.get(templateRetentionKey) as TemplateRetention | undefined
|
|
229
|
+
)?.retain;
|
|
230
|
+
const creationNamespace = namespace;
|
|
231
|
+
const start = document.createComment("range");
|
|
232
|
+
const end = document.createComment("/range");
|
|
233
|
+
const fragment = document.createDocumentFragment();
|
|
234
|
+
fragment.append(start, end);
|
|
235
|
+
let stop: (() => void) | undefined;
|
|
236
|
+
let disposed = false;
|
|
237
|
+
const cleanupPolicy = owner.context?.get(cleanupOrderKey) as
|
|
238
|
+
CleanupPolicy | undefined;
|
|
239
|
+
const cleanup = () => {
|
|
240
|
+
if (disposed) return;
|
|
241
|
+
disposed = true;
|
|
242
|
+
let failed = false;
|
|
243
|
+
let failure: unknown;
|
|
244
|
+
try {
|
|
245
|
+
const span = rangeSpan(start, end);
|
|
246
|
+
// Damaged or crossed boundaries remove only the owned row ledger.
|
|
247
|
+
const owned = span ?? rangeOwned(start);
|
|
248
|
+
if (cleanupPolicy)
|
|
249
|
+
cleanupPolicy.deferRemoval([...owned], end, retain, removeNodes);
|
|
250
|
+
else
|
|
251
|
+
for (const node of owned)
|
|
252
|
+
if (!retain?.(node)) node.parentNode?.removeChild(node);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
failed = true;
|
|
255
|
+
failure = error;
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
stop?.();
|
|
259
|
+
} catch (error) {
|
|
260
|
+
if (!failed) failure = error;
|
|
261
|
+
failed = true;
|
|
262
|
+
}
|
|
263
|
+
if (failed) throw failure;
|
|
264
|
+
};
|
|
265
|
+
const dispose = () => {
|
|
266
|
+
if (cleanupPolicy) withCleanupTransaction(cleanup);
|
|
267
|
+
else cleanup();
|
|
268
|
+
};
|
|
269
|
+
Object.defineProperty(start, nodeRange, {
|
|
270
|
+
value: {
|
|
271
|
+
get disposed() {
|
|
272
|
+
return disposed;
|
|
273
|
+
},
|
|
274
|
+
end,
|
|
275
|
+
contents: [],
|
|
276
|
+
valid: () => !disposed && !!rangeSpan(start, end),
|
|
277
|
+
dispose,
|
|
278
|
+
schedule,
|
|
279
|
+
retain,
|
|
280
|
+
move(parent: Node, before: Node) {
|
|
281
|
+
const span = rangeSpan(start, end);
|
|
282
|
+
if (
|
|
283
|
+
disposed ||
|
|
284
|
+
!span ||
|
|
285
|
+
before.parentNode !== parent ||
|
|
286
|
+
span.includes(before)
|
|
287
|
+
)
|
|
288
|
+
throw new Error("Invalid insertion range boundaries");
|
|
289
|
+
if (end.nextSibling === before && start.parentNode === parent) return;
|
|
290
|
+
for (const node of span) parent.insertBefore(node, before);
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
Object.defineProperty(start, insertedNode, {
|
|
295
|
+
configurable: true,
|
|
296
|
+
value: () => {
|
|
297
|
+
if (disposed || owner.disposed) return;
|
|
298
|
+
runWithOwner(owner, () =>
|
|
299
|
+
createComponentScope(() => {
|
|
300
|
+
createRoot((disposeRoot) => {
|
|
301
|
+
stop = disposeRoot;
|
|
302
|
+
if (disposed) {
|
|
303
|
+
disposeRoot();
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
insert(start.parentNode!, child, end, start, creationNamespace);
|
|
308
|
+
} catch (error) {
|
|
309
|
+
try {
|
|
310
|
+
disposeRoot();
|
|
311
|
+
} catch {}
|
|
312
|
+
throw error;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
onCleanup(dispose);
|
|
316
|
+
}),
|
|
317
|
+
);
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
onCleanup(dispose);
|
|
321
|
+
return start;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function insert(
|
|
325
|
+
parent: Node,
|
|
326
|
+
child: DOMChild,
|
|
327
|
+
suppliedAnchor?: Node,
|
|
328
|
+
rangeStart?: Node,
|
|
329
|
+
rangeNamespace?: string,
|
|
330
|
+
): void {
|
|
331
|
+
const retain =
|
|
332
|
+
(rangeStart ? range(rangeStart)?.retain : undefined) ??
|
|
333
|
+
(
|
|
334
|
+
getOwner()?.context?.get(templateRetentionKey) as
|
|
335
|
+
TemplateRetention | undefined
|
|
336
|
+
)?.retain;
|
|
337
|
+
const anchor = suppliedAnchor ?? document.createComment("");
|
|
338
|
+
if (!suppliedAnchor) parent.appendChild(anchor);
|
|
339
|
+
let current: Node[] = [];
|
|
340
|
+
let cancelled = false;
|
|
341
|
+
const update = () => {
|
|
342
|
+
if (
|
|
343
|
+
suppliedAnchor &&
|
|
344
|
+
(cancelled || !rangeStart || !range(rangeStart)?.valid())
|
|
345
|
+
) {
|
|
346
|
+
if (cancelled) return;
|
|
347
|
+
throw new Error("Invalid insertion range boundaries");
|
|
348
|
+
}
|
|
349
|
+
const target = suppliedAnchor ? (anchor.parentNode ?? parent) : parent;
|
|
350
|
+
const previousNamespace = namespace;
|
|
351
|
+
namespace =
|
|
352
|
+
suppliedAnchor && target.nodeType === 11
|
|
353
|
+
? (rangeNamespace ?? previousNamespace)
|
|
354
|
+
: childrenNamespace(target);
|
|
355
|
+
let next: Node[];
|
|
356
|
+
try {
|
|
357
|
+
next = nodes(child, current);
|
|
358
|
+
} finally {
|
|
359
|
+
namespace = previousNamespace;
|
|
360
|
+
}
|
|
361
|
+
if (cancelled) {
|
|
362
|
+
removeNodes(next, undefined, retain);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (
|
|
366
|
+
next !== current ||
|
|
367
|
+
next[0]?.nextSibling !== anchor ||
|
|
368
|
+
next[0]?.parentNode !== parent
|
|
369
|
+
) {
|
|
370
|
+
const retained = new Set(next);
|
|
371
|
+
removeNodes(current, retained, retain);
|
|
372
|
+
if (cancelled) {
|
|
373
|
+
removeNodes(next, undefined, retain);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (
|
|
377
|
+
next.length > 1 &&
|
|
378
|
+
next.length === current.length &&
|
|
379
|
+
retained.size === next.length &&
|
|
380
|
+
next[0] === current[current.length - 1] &&
|
|
381
|
+
current[0]!.parentNode === parent &&
|
|
382
|
+
next.every((node, index) => index === 0 || node === current[index - 1])
|
|
383
|
+
) {
|
|
384
|
+
// Move the last row once instead of moving every other retained row.
|
|
385
|
+
moveNode(target, next[0]!, current[0]!);
|
|
386
|
+
}
|
|
387
|
+
let cursor: Node = anchor;
|
|
388
|
+
for (let i = next.length - 1; i >= 0; i--) {
|
|
389
|
+
const node = next[i]!;
|
|
390
|
+
moveNode(target, node, cursor);
|
|
391
|
+
cursor = node;
|
|
392
|
+
}
|
|
393
|
+
current = next;
|
|
394
|
+
if (rangeStart) range(rangeStart)!.contents = current;
|
|
395
|
+
for (const node of next) {
|
|
396
|
+
const ready = (node as Node & { [insertedNode]?: () => void })[
|
|
397
|
+
insertedNode
|
|
398
|
+
];
|
|
399
|
+
if (ready) {
|
|
400
|
+
delete (node as Node & { [insertedNode]?: () => void })[insertedNode];
|
|
401
|
+
ready();
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
refreshSelect(target);
|
|
406
|
+
};
|
|
407
|
+
const cleanupPolicy = getOwner()?.context?.get(cleanupOrderKey) as
|
|
408
|
+
CleanupPolicy | undefined;
|
|
409
|
+
onCleanup(() => {
|
|
410
|
+
cancelled = true;
|
|
411
|
+
const owned = [...current, anchor];
|
|
412
|
+
if (!cleanupPolicy) {
|
|
413
|
+
removeNodes(owned, undefined, retain);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
cleanupPolicy.deferRemoval(owned, anchor, retain, removeNodes);
|
|
417
|
+
});
|
|
418
|
+
if (dynamic(child)) {
|
|
419
|
+
const schedule = rangeStart && range(rangeStart)?.schedule;
|
|
420
|
+
if (schedule) schedule(update);
|
|
421
|
+
else createRenderEffect(update);
|
|
422
|
+
} else update();
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function style(element: Element, value: unknown): void {
|
|
426
|
+
const declaration = (element as HTMLElement | SVGElement).style;
|
|
427
|
+
if (value != null && typeof value === "object") {
|
|
428
|
+
const entries = styleEntries(value);
|
|
429
|
+
const next = new Set(
|
|
430
|
+
entries.filter(([, value]) => value !== null).map(([name]) => name),
|
|
431
|
+
);
|
|
432
|
+
for (const name of styleNames.get(element) ?? []) {
|
|
433
|
+
if (!next.has(name)) declaration.removeProperty(name);
|
|
434
|
+
}
|
|
435
|
+
for (const [name, value] of entries) {
|
|
436
|
+
if (value === null) declaration.removeProperty(name);
|
|
437
|
+
else declaration.setProperty(name, value);
|
|
438
|
+
}
|
|
439
|
+
styleNames.set(element, next);
|
|
440
|
+
} else if (value == null || value === false) {
|
|
441
|
+
element.removeAttribute("style");
|
|
442
|
+
styleNames.delete(element);
|
|
443
|
+
} else {
|
|
444
|
+
if (typeof value !== "string")
|
|
445
|
+
throw new TypeError("style must be a CSS string, plain record or null");
|
|
446
|
+
element.setAttribute("style", value);
|
|
447
|
+
styleNames.set(element, new Set(Array.from(declaration)));
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function attribute(element: Element, name: string, value: unknown): void {
|
|
452
|
+
if (name === "defaultValue") {
|
|
453
|
+
(element as HTMLInputElement | HTMLTextAreaElement).defaultValue =
|
|
454
|
+
value == null ? "" : String(value);
|
|
455
|
+
return;
|
|
456
|
+
} else if (name === "defaultChecked") {
|
|
457
|
+
(element as HTMLInputElement).defaultChecked = Boolean(value);
|
|
458
|
+
return;
|
|
459
|
+
} else if (name === "style") {
|
|
460
|
+
style(element, value);
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (name === "value" && element instanceof HTMLSelectElement) {
|
|
464
|
+
if (!element.multiple && Array.isArray(value)) {
|
|
465
|
+
throw new TypeError("Single select value cannot be an array");
|
|
466
|
+
}
|
|
467
|
+
if (element.multiple) {
|
|
468
|
+
const selected = new Set(
|
|
469
|
+
(Array.isArray(value) ? value : value == null ? [] : [value]).map(
|
|
470
|
+
String,
|
|
471
|
+
),
|
|
472
|
+
);
|
|
473
|
+
for (const option of element.options)
|
|
474
|
+
option.selected = selected.has(option.value);
|
|
475
|
+
} else {
|
|
476
|
+
const next = value == null ? "" : String(value);
|
|
477
|
+
element.value = next;
|
|
478
|
+
}
|
|
479
|
+
} else if (
|
|
480
|
+
name === "value" &&
|
|
481
|
+
element.namespaceURI === HTML &&
|
|
482
|
+
(element.localName === "progress" ||
|
|
483
|
+
element.localName === "meter" ||
|
|
484
|
+
element.localName === "li")
|
|
485
|
+
) {
|
|
486
|
+
const target = element as
|
|
487
|
+
HTMLProgressElement | HTMLMeterElement | HTMLLIElement;
|
|
488
|
+
if (
|
|
489
|
+
target.value !== value ||
|
|
490
|
+
(element.localName === "progress" && value === 0)
|
|
491
|
+
)
|
|
492
|
+
target.value = (value ?? "") as number;
|
|
493
|
+
} else if (name === "value" && "value" in element) {
|
|
494
|
+
const input = element as HTMLInputElement;
|
|
495
|
+
const next = value == null ? "" : String(value);
|
|
496
|
+
if (
|
|
497
|
+
input.value !== next ||
|
|
498
|
+
(element instanceof HTMLOptionElement && !element.hasAttribute("value"))
|
|
499
|
+
)
|
|
500
|
+
input.value = next;
|
|
501
|
+
if (element instanceof HTMLOptionElement) refreshSelect(element);
|
|
502
|
+
} else if (name === "checked" && "checked" in element) {
|
|
503
|
+
(element as HTMLInputElement).checked = Boolean(value);
|
|
504
|
+
} else {
|
|
505
|
+
const attr =
|
|
506
|
+
name === "className" ? "class" : name === "htmlFor" ? "for" : name;
|
|
507
|
+
const attributeNamespace =
|
|
508
|
+
attr === "xmlns" || attr.startsWith("xmlns:")
|
|
509
|
+
? "http://www.w3.org/2000/xmlns/"
|
|
510
|
+
: attr.startsWith("xlink:")
|
|
511
|
+
? "http://www.w3.org/1999/xlink"
|
|
512
|
+
: attr.startsWith("xml:")
|
|
513
|
+
? "http://www.w3.org/XML/1998/namespace"
|
|
514
|
+
: null;
|
|
515
|
+
const text = attributeValue(attr, value, element.namespaceURI === HTML);
|
|
516
|
+
if (text === undefined) {
|
|
517
|
+
if (attributeNamespace)
|
|
518
|
+
element.removeAttributeNS(
|
|
519
|
+
attributeNamespace,
|
|
520
|
+
attr.slice(attr.indexOf(":") + 1),
|
|
521
|
+
);
|
|
522
|
+
else element.removeAttribute(attr);
|
|
523
|
+
} else {
|
|
524
|
+
if (attributeNamespace)
|
|
525
|
+
element.setAttributeNS(attributeNamespace, attr, text);
|
|
526
|
+
else element.setAttribute(attr, text);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export function h<K extends keyof HTMLElementTagNameMap>(
|
|
532
|
+
tag: K,
|
|
533
|
+
props?: Record<string, unknown> | null,
|
|
534
|
+
...children: DOMChild[]
|
|
535
|
+
): HTMLElementTagNameMap[K];
|
|
536
|
+
export function h<K extends keyof SVGElementTagNameMap>(
|
|
537
|
+
tag: K,
|
|
538
|
+
props?: Record<string, unknown> | null,
|
|
539
|
+
...children: DOMChild[]
|
|
540
|
+
): SVGElementTagNameMap[K];
|
|
541
|
+
export function h<P extends object>(
|
|
542
|
+
component: (props: P & { children?: DOMChild }) => DOMChild,
|
|
543
|
+
props: P | null,
|
|
544
|
+
...children: DOMChild[]
|
|
545
|
+
): DOMChild;
|
|
546
|
+
export function h(
|
|
547
|
+
tag: string,
|
|
548
|
+
props?: Record<string, unknown> | null,
|
|
549
|
+
...children: DOMChild[]
|
|
550
|
+
): HTMLElement | SVGElement;
|
|
551
|
+
export function h<P extends Record<string, unknown>>(
|
|
552
|
+
tag: string | ((props: P & { children?: DOMChild }) => DOMChild),
|
|
553
|
+
props: P | null = null,
|
|
554
|
+
...children: DOMChild[]
|
|
555
|
+
): DOMChild {
|
|
556
|
+
if (typeof tag === "function") {
|
|
557
|
+
const componentProps = children.length
|
|
558
|
+
? Object.defineProperties(
|
|
559
|
+
{},
|
|
560
|
+
{
|
|
561
|
+
...Object.getOwnPropertyDescriptors(props ?? {}),
|
|
562
|
+
children: {
|
|
563
|
+
value: children,
|
|
564
|
+
enumerable: true,
|
|
565
|
+
writable: true,
|
|
566
|
+
configurable: true,
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
)
|
|
570
|
+
: (props ?? {});
|
|
571
|
+
return createComponent(
|
|
572
|
+
tag,
|
|
573
|
+
componentProps as P & { children?: DOMChild },
|
|
574
|
+
"dom",
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
// shortcut: ambiguous SVG tags need accessor children or xmlns, until lowering supplies namespace context.
|
|
578
|
+
const elementNamespace =
|
|
579
|
+
props?.xmlns ??
|
|
580
|
+
(tag === "svg" || namespace === SVG || SVG_TAGS.has(tag) ? SVG : HTML);
|
|
581
|
+
return createIntrinsic(tag, props, children, String(elementNamespace));
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/** Compiler-owned lexical namespace; authored xmlns remains an ordinary attribute. */
|
|
585
|
+
export function hNS<K extends keyof SVGElementTagNameMap>(
|
|
586
|
+
elementNamespace: typeof SVG,
|
|
587
|
+
tag: K,
|
|
588
|
+
props?: Record<string, unknown> | null,
|
|
589
|
+
...children: DOMChild[]
|
|
590
|
+
): SVGElementTagNameMap[K];
|
|
591
|
+
export function hNS<K extends keyof HTMLElementTagNameMap>(
|
|
592
|
+
elementNamespace: typeof HTML,
|
|
593
|
+
tag: K,
|
|
594
|
+
props?: Record<string, unknown> | null,
|
|
595
|
+
...children: DOMChild[]
|
|
596
|
+
): HTMLElementTagNameMap[K];
|
|
597
|
+
export function hNS<K extends keyof MathMLElementTagNameMap>(
|
|
598
|
+
elementNamespace: "http://www.w3.org/1998/Math/MathML",
|
|
599
|
+
tag: K,
|
|
600
|
+
props?: Record<string, unknown> | null,
|
|
601
|
+
...children: DOMChild[]
|
|
602
|
+
): MathMLElementTagNameMap[K];
|
|
603
|
+
export function hNS(
|
|
604
|
+
elementNamespace: string,
|
|
605
|
+
tag: string,
|
|
606
|
+
props?: Record<string, unknown> | null,
|
|
607
|
+
...children: DOMChild[]
|
|
608
|
+
): Element;
|
|
609
|
+
export function hNS(
|
|
610
|
+
elementNamespace: string,
|
|
611
|
+
tag: string,
|
|
612
|
+
props: Record<string, unknown> | null = null,
|
|
613
|
+
...children: DOMChild[]
|
|
614
|
+
): Element {
|
|
615
|
+
if (typeof elementNamespace !== "string")
|
|
616
|
+
throw new TypeError("Unsupported element namespace");
|
|
617
|
+
return createIntrinsic(tag, props, children, elementNamespace);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** Compiler-owned namespace values retain browser coercion and errors. */
|
|
621
|
+
export function hSourceNS(
|
|
622
|
+
elementNamespace: unknown,
|
|
623
|
+
tag: string,
|
|
624
|
+
props: Record<string, unknown> | null = null,
|
|
625
|
+
...children: DOMChild[]
|
|
626
|
+
): Element {
|
|
627
|
+
return createIntrinsic(tag, props, children, elementNamespace ?? HTML);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function createIntrinsic(
|
|
631
|
+
tag: string,
|
|
632
|
+
props: Record<string, unknown> | null,
|
|
633
|
+
children: DOMChild[],
|
|
634
|
+
elementNamespace: unknown,
|
|
635
|
+
): Element {
|
|
636
|
+
const element = document.createElementNS(
|
|
637
|
+
elementNamespace as string | null,
|
|
638
|
+
tag,
|
|
639
|
+
);
|
|
640
|
+
const select = element instanceof HTMLSelectElement;
|
|
641
|
+
const input = element instanceof HTMLInputElement;
|
|
642
|
+
const textarea = element instanceof HTMLTextAreaElement;
|
|
643
|
+
const output =
|
|
644
|
+
element.namespaceURI === HTML && element.localName === "output";
|
|
645
|
+
const numericValue =
|
|
646
|
+
element.namespaceURI === HTML &&
|
|
647
|
+
(element.localName === "progress" ||
|
|
648
|
+
element.localName === "meter" ||
|
|
649
|
+
element.localName === "li");
|
|
650
|
+
let formProperties: Map<string, unknown> | undefined;
|
|
651
|
+
let selectValue: unknown;
|
|
652
|
+
let selectMultiple: unknown;
|
|
653
|
+
let hasSelectValue = false;
|
|
654
|
+
const registerEvents = (props as Record<PropertyKey, unknown> | null)?.[
|
|
655
|
+
eventRegistration
|
|
656
|
+
] as EventRegistration | undefined;
|
|
657
|
+
for (const name of Object.keys(props ?? {})) {
|
|
658
|
+
if (name === "children") continue;
|
|
659
|
+
const value = props![name];
|
|
660
|
+
if (name === "defaultValue" && !input && !textarea)
|
|
661
|
+
throw new TypeError(
|
|
662
|
+
"defaultValue is supported only on input and textarea",
|
|
663
|
+
);
|
|
664
|
+
if (name === "defaultChecked" && !input)
|
|
665
|
+
throw new TypeError("defaultChecked is supported only on input");
|
|
666
|
+
if (
|
|
667
|
+
name === "defaultValue" ||
|
|
668
|
+
name === "defaultChecked" ||
|
|
669
|
+
(name === "value" && (input || textarea || output || numericValue)) ||
|
|
670
|
+
(name === "checked" && input)
|
|
671
|
+
) {
|
|
672
|
+
(formProperties ??= new Map()).set(name, value);
|
|
673
|
+
continue;
|
|
674
|
+
}
|
|
675
|
+
if (select && name === "value") {
|
|
676
|
+
selectValue = value;
|
|
677
|
+
hasSelectValue = true;
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
if (select && name === "multiple") selectMultiple = value;
|
|
681
|
+
if (name === "ref") {
|
|
682
|
+
if (value == null) continue;
|
|
683
|
+
if (typeof value !== "function")
|
|
684
|
+
throw new TypeError("ref must be a callback");
|
|
685
|
+
untrack(() => value(element));
|
|
686
|
+
} else if (name.startsWith("on") && registerEvents) {
|
|
687
|
+
onCleanup(registerEvents(element, name, value));
|
|
688
|
+
} else if (name.startsWith("on")) {
|
|
689
|
+
if (value == null) continue;
|
|
690
|
+
const object =
|
|
691
|
+
typeof value === "object"
|
|
692
|
+
? (value as EventListenerObject & AddEventListenerOptions)
|
|
693
|
+
: undefined;
|
|
694
|
+
if (
|
|
695
|
+
typeof value !== "function" &&
|
|
696
|
+
typeof object?.handleEvent !== "function"
|
|
697
|
+
)
|
|
698
|
+
throw new TypeError(
|
|
699
|
+
`${name} must be an event handler or handleEvent object`,
|
|
700
|
+
);
|
|
701
|
+
const event = name.startsWith("on:")
|
|
702
|
+
? name.slice(3)
|
|
703
|
+
: name.slice(2).toLowerCase();
|
|
704
|
+
const listener = value as EventListenerOrEventListenerObject;
|
|
705
|
+
const options = object
|
|
706
|
+
? {
|
|
707
|
+
capture: Boolean(object.capture),
|
|
708
|
+
once: object.once,
|
|
709
|
+
passive: object.passive,
|
|
710
|
+
signal: object.signal,
|
|
711
|
+
}
|
|
712
|
+
: undefined;
|
|
713
|
+
element.addEventListener(event, listener, options);
|
|
714
|
+
onCleanup(() =>
|
|
715
|
+
element.removeEventListener(event, listener, options?.capture),
|
|
716
|
+
);
|
|
717
|
+
} else if (typeof value === "function") {
|
|
718
|
+
createRenderEffect(() => attribute(element, name, value()));
|
|
719
|
+
} else attribute(element, name, value);
|
|
720
|
+
}
|
|
721
|
+
if (
|
|
722
|
+
(children.length || (props && "children" in props)) &&
|
|
723
|
+
!(
|
|
724
|
+
textarea &&
|
|
725
|
+
(formProperties?.has("defaultValue") || formProperties?.has("value"))
|
|
726
|
+
)
|
|
727
|
+
)
|
|
728
|
+
insert(
|
|
729
|
+
element,
|
|
730
|
+
children.length ? children : () => props!.children as DOMChild,
|
|
731
|
+
);
|
|
732
|
+
// Defaults define reset state; live value/checked override it without controlled reset handling.
|
|
733
|
+
if (formProperties)
|
|
734
|
+
for (const name of ["defaultValue", "defaultChecked", "value", "checked"]) {
|
|
735
|
+
if (!formProperties.has(name)) continue;
|
|
736
|
+
const value = formProperties.get(name);
|
|
737
|
+
if ((output || numericValue) && name === "value") {
|
|
738
|
+
// Match the initial nullish cache: leave defaults untouched until a value changes.
|
|
739
|
+
let previous: unknown;
|
|
740
|
+
const apply = (next: unknown) => {
|
|
741
|
+
const normalized = next ?? undefined;
|
|
742
|
+
if (previous === normalized) return;
|
|
743
|
+
previous = normalized;
|
|
744
|
+
attribute(element, name, next);
|
|
745
|
+
};
|
|
746
|
+
if (typeof value === "function")
|
|
747
|
+
createRenderEffect(() => apply(value()));
|
|
748
|
+
else apply(value);
|
|
749
|
+
} else if (typeof value === "function")
|
|
750
|
+
createRenderEffect(() => attribute(element, name, value()));
|
|
751
|
+
else attribute(element, name, value);
|
|
752
|
+
}
|
|
753
|
+
if (select && hasSelectValue) {
|
|
754
|
+
const value = selectValue;
|
|
755
|
+
const multiple = selectMultiple;
|
|
756
|
+
const update = () => {
|
|
757
|
+
if (typeof multiple === "function") multiple();
|
|
758
|
+
attribute(
|
|
759
|
+
element,
|
|
760
|
+
"value",
|
|
761
|
+
typeof value === "function" ? value() : value,
|
|
762
|
+
);
|
|
763
|
+
};
|
|
764
|
+
selectUpdates.set(element, update);
|
|
765
|
+
onCleanup(() => selectUpdates.delete(element));
|
|
766
|
+
createRenderEffect(update);
|
|
767
|
+
}
|
|
768
|
+
return element;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
export function render(
|
|
772
|
+
fn: () => DOMChild,
|
|
773
|
+
container: Element | ShadowRoot,
|
|
774
|
+
): () => void {
|
|
775
|
+
let dispose = () => {};
|
|
776
|
+
try {
|
|
777
|
+
return createRoot((stop) => {
|
|
778
|
+
dispose = stop;
|
|
779
|
+
const previousNamespace = namespace;
|
|
780
|
+
namespace = childrenNamespace(container);
|
|
781
|
+
try {
|
|
782
|
+
insert(container, fn());
|
|
783
|
+
} finally {
|
|
784
|
+
namespace = previousNamespace;
|
|
785
|
+
}
|
|
786
|
+
return stop;
|
|
787
|
+
});
|
|
788
|
+
} catch (error) {
|
|
789
|
+
try {
|
|
790
|
+
dispose();
|
|
791
|
+
} catch {
|
|
792
|
+
// Cleanup failures must not hide the original mount error.
|
|
793
|
+
}
|
|
794
|
+
throw error;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
export { attribute as setAttributeValue };
|
|
799
|
+
|
|
800
|
+
export { templateRetentionKey };
|
|
801
|
+
type TemplateRetention = {
|
|
802
|
+
retain: (node: Node) => boolean;
|
|
803
|
+
active: boolean;
|
|
804
|
+
roots: readonly Node[];
|
|
805
|
+
parent?: TemplateRetention;
|
|
806
|
+
};
|
|
807
|
+
function retainedTemplateNode(
|
|
808
|
+
token: TemplateRetention | undefined,
|
|
809
|
+
node: Node,
|
|
810
|
+
): boolean {
|
|
811
|
+
for (let current = token; current; current = current.parent)
|
|
812
|
+
if (
|
|
813
|
+
current.active &&
|
|
814
|
+
current.roots.some((root) => root === node || root.contains(node))
|
|
815
|
+
)
|
|
816
|
+
return true;
|
|
817
|
+
return false;
|
|
818
|
+
}
|
|
819
|
+
/** Compiler-owned block teardown; nested insertions inherit its explicit retention policy. */
|
|
820
|
+
export function templateBlock<T extends Node | readonly Node[]>(
|
|
821
|
+
build: (retain: (node: Node) => boolean) => T,
|
|
822
|
+
): T {
|
|
823
|
+
const owner = getOwner();
|
|
824
|
+
if (!owner || owner.disposed || owner.disposing)
|
|
825
|
+
throw Error("Template block requires a live owner");
|
|
826
|
+
const previous = owner.context;
|
|
827
|
+
const token: TemplateRetention = {
|
|
828
|
+
retain: (node) => retainedTemplateNode(token, node),
|
|
829
|
+
active: false,
|
|
830
|
+
roots: [],
|
|
831
|
+
parent: previous?.get(templateRetentionKey) as
|
|
832
|
+
TemplateRetention | undefined,
|
|
833
|
+
};
|
|
834
|
+
let roots: Node | readonly Node[] = [];
|
|
835
|
+
onBeforeDispose(() => {
|
|
836
|
+
const captured = new Set<Node>();
|
|
837
|
+
for (const node of Array.isArray(roots) ? roots : [roots as Node]) {
|
|
838
|
+
const metadata = range(node);
|
|
839
|
+
for (const root of metadata
|
|
840
|
+
? (rangeSpan(node, metadata.end) ?? rangeOwned(node))
|
|
841
|
+
: [node])
|
|
842
|
+
captured.add(root);
|
|
843
|
+
}
|
|
844
|
+
token.roots = [...captured];
|
|
845
|
+
token.active = true;
|
|
846
|
+
let failed = false,
|
|
847
|
+
failure: unknown;
|
|
848
|
+
for (const node of token.roots) {
|
|
849
|
+
if (retainedTemplateNode(token.parent, node)) continue;
|
|
850
|
+
try {
|
|
851
|
+
node.parentNode?.removeChild(node);
|
|
852
|
+
} catch (error) {
|
|
853
|
+
if (!failed) failure = error;
|
|
854
|
+
failed = true;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
if (failed) throw failure;
|
|
858
|
+
});
|
|
859
|
+
owner.context = new Map(previous);
|
|
860
|
+
owner.context.set(templateRetentionKey, token);
|
|
861
|
+
try {
|
|
862
|
+
const result = build(token.retain);
|
|
863
|
+
if (owner.disposed)
|
|
864
|
+
throw Error("Template block disposed during construction");
|
|
865
|
+
roots = result;
|
|
866
|
+
return result;
|
|
867
|
+
} finally {
|
|
868
|
+
if (!owner.disposed) owner.context = previous;
|
|
869
|
+
}
|
|
870
|
+
}
|