@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/ssr.ts
ADDED
|
@@ -0,0 +1,874 @@
|
|
|
1
|
+
import { cleanupOrderKey } from "./cleanup-order";
|
|
2
|
+
/**
|
|
3
|
+
* Independent HTML factory. Raw markup requires explicit rawHTML; ordinary children escape.
|
|
4
|
+
* No script/style element factories or hydration ABI.
|
|
5
|
+
* Ordinary nonempty single selects require a match: HTML cannot preserve selection -1.
|
|
6
|
+
* Explicit live props win over defaults; HTML cannot encode separate live/reset values.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
createRoot,
|
|
10
|
+
getOwner,
|
|
11
|
+
runWithOwner,
|
|
12
|
+
errorHandlerKey,
|
|
13
|
+
type Owner,
|
|
14
|
+
} from "./reactive";
|
|
15
|
+
import { styleEntries } from "./style";
|
|
16
|
+
import { attributeValue, sourceAttributeValue } from "./attributes";
|
|
17
|
+
import { createComponent } from "./component";
|
|
18
|
+
import {
|
|
19
|
+
createSSRRenderAttempt,
|
|
20
|
+
resetSSRRenderAttempt,
|
|
21
|
+
withSSRRenderAttempt,
|
|
22
|
+
type SSRRenderAttempt,
|
|
23
|
+
} from "./ssr-attempt";
|
|
24
|
+
import {
|
|
25
|
+
ssrOwner,
|
|
26
|
+
withSSRRendering,
|
|
27
|
+
isSSRRendering,
|
|
28
|
+
type OwnedSSRChild,
|
|
29
|
+
} from "./ssr-owner";
|
|
30
|
+
export { scopeSSRChild } from "./ssr-owner";
|
|
31
|
+
|
|
32
|
+
/** Compiler-only typed selection; payloads preserve literal function/object identity. */
|
|
33
|
+
export const selectBinding = Symbol("moonshine server select binding");
|
|
34
|
+
export const optionBinding = Symbol("moonshine server option binding");
|
|
35
|
+
type BindingValue = { value: unknown; preserveSelected?: boolean };
|
|
36
|
+
const ELEMENT_NAME = /^[a-zA-Z][a-zA-Z0-9:_-]*$/;
|
|
37
|
+
const ATTRIBUTE_NAME = /^[a-zA-Z_:][a-zA-Z0-9:_.-]*$/;
|
|
38
|
+
const EVENT_NAME = /^on/i;
|
|
39
|
+
|
|
40
|
+
function validateBindingBox(box: unknown): BindingValue {
|
|
41
|
+
if (box === null || typeof box !== "object")
|
|
42
|
+
throw new TypeError(
|
|
43
|
+
"Server binding metadata requires a plain { value } box",
|
|
44
|
+
);
|
|
45
|
+
const proto = Object.getPrototypeOf(box);
|
|
46
|
+
if (proto !== Object.prototype && proto !== null)
|
|
47
|
+
throw new TypeError(
|
|
48
|
+
"Server binding metadata requires a plain { value } box",
|
|
49
|
+
);
|
|
50
|
+
const descriptor = Object.getOwnPropertyDescriptor(box, "value");
|
|
51
|
+
if (!descriptor || !("value" in descriptor))
|
|
52
|
+
throw new TypeError("Server binding metadata requires an own data value");
|
|
53
|
+
let preserveSelected: boolean | undefined;
|
|
54
|
+
if (Object.hasOwn(box, "preserveSelected")) {
|
|
55
|
+
const policy = Object.getOwnPropertyDescriptor(box, "preserveSelected");
|
|
56
|
+
if (policy && (!("value" in policy) || typeof policy.value !== "boolean"))
|
|
57
|
+
throw new TypeError(
|
|
58
|
+
"Server selected policy requires an own boolean data value",
|
|
59
|
+
);
|
|
60
|
+
preserveSelected = policy?.value;
|
|
61
|
+
}
|
|
62
|
+
return { value: descriptor.value, preserveSelected };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function bindingValue(
|
|
66
|
+
props: Record<string, unknown> | null,
|
|
67
|
+
key: symbol,
|
|
68
|
+
): BindingValue | undefined {
|
|
69
|
+
if (!props || !Object.hasOwn(props, key)) return undefined;
|
|
70
|
+
return validateBindingBox(Reflect.get(props, key));
|
|
71
|
+
}
|
|
72
|
+
const html = Symbol("moonshine server HTML");
|
|
73
|
+
type Selection = {
|
|
74
|
+
values?: Set<string>;
|
|
75
|
+
binding?: BindingValue;
|
|
76
|
+
multiple: boolean;
|
|
77
|
+
matched: boolean;
|
|
78
|
+
options: number;
|
|
79
|
+
};
|
|
80
|
+
export type HTML = {
|
|
81
|
+
readonly [html]: (selection?: Selection, svg?: boolean) => string;
|
|
82
|
+
};
|
|
83
|
+
export type SSRChild =
|
|
84
|
+
| HTML
|
|
85
|
+
| OwnedSSRChild
|
|
86
|
+
| string
|
|
87
|
+
| number
|
|
88
|
+
| bigint
|
|
89
|
+
| boolean
|
|
90
|
+
| null
|
|
91
|
+
| undefined
|
|
92
|
+
| readonly SSRChild[]
|
|
93
|
+
| (() => SSRChild);
|
|
94
|
+
|
|
95
|
+
const VOID = new Set([
|
|
96
|
+
"area",
|
|
97
|
+
"base",
|
|
98
|
+
"br",
|
|
99
|
+
"col",
|
|
100
|
+
"embed",
|
|
101
|
+
"hr",
|
|
102
|
+
"img",
|
|
103
|
+
"input",
|
|
104
|
+
"link",
|
|
105
|
+
"meta",
|
|
106
|
+
"param",
|
|
107
|
+
"source",
|
|
108
|
+
"track",
|
|
109
|
+
"wbr",
|
|
110
|
+
]);
|
|
111
|
+
|
|
112
|
+
/** Explicit unsanitized server markup; ordinary children remain escaped. */
|
|
113
|
+
export function rawHTML(value: unknown): HTML {
|
|
114
|
+
const markup = String(value ?? "");
|
|
115
|
+
return { [html]: () => markup };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function escapeHtml(value: unknown): string {
|
|
119
|
+
const text = typeof value === "string" ? value : String(value);
|
|
120
|
+
if (text.length === 0) return "";
|
|
121
|
+
// Bun emits ' for apostrophes; retain this API's exact ' output.
|
|
122
|
+
if (typeof Bun !== "undefined" && !text.includes("'"))
|
|
123
|
+
return Bun.escapeHTML(text);
|
|
124
|
+
return text.replace(/[&<>"']/g, (character) => {
|
|
125
|
+
switch (character) {
|
|
126
|
+
case "&":
|
|
127
|
+
return "&";
|
|
128
|
+
case "<":
|
|
129
|
+
return "<";
|
|
130
|
+
case ">":
|
|
131
|
+
return ">";
|
|
132
|
+
case '"':
|
|
133
|
+
return """;
|
|
134
|
+
default:
|
|
135
|
+
return "'";
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function resolve(child: SSRChild): SSRChild {
|
|
141
|
+
while (typeof child === "function") child = child();
|
|
142
|
+
if (child == null || typeof child !== "object") return child;
|
|
143
|
+
if (Array.isArray(child)) {
|
|
144
|
+
const length = child.length;
|
|
145
|
+
if (length === 0) return "";
|
|
146
|
+
if (length === 1) return resolve(child[0]);
|
|
147
|
+
const resolved: SSRChild[] = [];
|
|
148
|
+
for (let index = 0; index < length; index++)
|
|
149
|
+
resolved.push(resolve(child[index]));
|
|
150
|
+
return resolved;
|
|
151
|
+
}
|
|
152
|
+
if (!(html in child) && !(ssrOwner in child)) {
|
|
153
|
+
throw new TypeError("Server children must be text or factory HTML");
|
|
154
|
+
}
|
|
155
|
+
return child;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function text(child: SSRChild): string {
|
|
159
|
+
while (typeof child === "function") child = child();
|
|
160
|
+
if (child == null || typeof child === "boolean") return "";
|
|
161
|
+
if (typeof child === "string") return child;
|
|
162
|
+
if (typeof child !== "object") return String(child);
|
|
163
|
+
if (ssrOwner in child) return inSSRScope(child, () => text(child.child));
|
|
164
|
+
if (Array.isArray(child)) {
|
|
165
|
+
let out = "";
|
|
166
|
+
for (let index = 0; index < child.length; index++)
|
|
167
|
+
out += text(child[index]);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
throw new TypeError("Server textarea/title/option children must be text");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function content(child: SSRChild, selection?: Selection, svg = false): string {
|
|
174
|
+
while (typeof child === "function") child = child();
|
|
175
|
+
if (child == null || typeof child === "boolean") return "";
|
|
176
|
+
if (typeof child === "object") {
|
|
177
|
+
if (html in child) return (child as HTML)[html](selection, svg);
|
|
178
|
+
if (ssrOwner in child)
|
|
179
|
+
return inSSRScope(child, () => content(child.child, selection, svg));
|
|
180
|
+
if (Array.isArray(child)) {
|
|
181
|
+
let out = "";
|
|
182
|
+
for (let index = 0; index < child.length; index++)
|
|
183
|
+
out += content(child[index], selection, svg);
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
throw new TypeError("Server children must be text or factory HTML");
|
|
187
|
+
}
|
|
188
|
+
return escapeHtml(child);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Serialize within the active request and owner without starting or disposing a root. */
|
|
192
|
+
export function serializeSSRChild(child: SSRChild): string {
|
|
193
|
+
const owner = getOwner();
|
|
194
|
+
if (!isSSRRendering() || !owner || owner.disposed)
|
|
195
|
+
throw new Error(
|
|
196
|
+
"SSR child serialization requires a live server render owner",
|
|
197
|
+
);
|
|
198
|
+
return content(child);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function inSSRScope<T>(child: OwnedSSRChild, render: () => T): T {
|
|
202
|
+
const owner = child[ssrOwner];
|
|
203
|
+
if (owner.disposed)
|
|
204
|
+
throw new Error("Cannot serialize a disposed SSR child scope");
|
|
205
|
+
return runWithOwner(owner, render);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Construct nested components during serialization in the containing owner's context. */
|
|
209
|
+
export function defer(fn: () => SSRChild): HTML {
|
|
210
|
+
return { [html]: (selection, svg) => content(fn(), selection, svg) };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Serialize the child successfully before publishing compiler binding values. */
|
|
214
|
+
export function afterRender(child: SSRChild, commit: () => void): HTML {
|
|
215
|
+
return {
|
|
216
|
+
[html]: (selection, svg) => {
|
|
217
|
+
const rendered = content(child, selection, svg);
|
|
218
|
+
commit();
|
|
219
|
+
return rendered;
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Synchronous buffered recovery. Arbitrary state writes are not rolled back. */
|
|
225
|
+
export function boundary(
|
|
226
|
+
render: () => SSRChild,
|
|
227
|
+
fallback: (error: unknown) => SSRChild,
|
|
228
|
+
): HTML {
|
|
229
|
+
return {
|
|
230
|
+
[html]: (selection, svg) => {
|
|
231
|
+
const owner = getOwner();
|
|
232
|
+
if (!owner || owner.disposed)
|
|
233
|
+
throw new Error(
|
|
234
|
+
"SSR boundary serialization requires a live native owner",
|
|
235
|
+
);
|
|
236
|
+
const branch = (renderBranch: () => SSRChild): string => {
|
|
237
|
+
let stop: (() => void) | undefined;
|
|
238
|
+
let attempt: SSRRenderAttempt | undefined;
|
|
239
|
+
let scope: Owner | undefined;
|
|
240
|
+
const localSelection = selection ? { ...selection } : undefined;
|
|
241
|
+
let rendered: string;
|
|
242
|
+
try {
|
|
243
|
+
rendered = runWithOwner(owner, () =>
|
|
244
|
+
createRoot((dispose) => {
|
|
245
|
+
stop = dispose;
|
|
246
|
+
scope = getOwner()!;
|
|
247
|
+
attempt = createSSRRenderAttempt();
|
|
248
|
+
// The attempt owns a fresh context map; setup errors belong to this buffer.
|
|
249
|
+
scope.context!.delete(errorHandlerKey);
|
|
250
|
+
try {
|
|
251
|
+
return withSSRRenderAttempt(attempt, () =>
|
|
252
|
+
content(renderBranch(), localSelection, svg),
|
|
253
|
+
);
|
|
254
|
+
} catch (error) {
|
|
255
|
+
// createRoot cleans failed setup before returning to the outer catch.
|
|
256
|
+
attempt.status = "discarded";
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
}),
|
|
260
|
+
);
|
|
261
|
+
} catch (error) {
|
|
262
|
+
if (attempt) attempt.status = "discarded";
|
|
263
|
+
try {
|
|
264
|
+
stop?.();
|
|
265
|
+
} catch {
|
|
266
|
+
/* Preserve the render failure. */
|
|
267
|
+
}
|
|
268
|
+
throw error;
|
|
269
|
+
}
|
|
270
|
+
if (owner.disposed) {
|
|
271
|
+
attempt!.status = "discarded";
|
|
272
|
+
try {
|
|
273
|
+
stop!();
|
|
274
|
+
} catch {
|
|
275
|
+
/* Preserve the ownership failure. */
|
|
276
|
+
}
|
|
277
|
+
throw new Error("SSR boundary owner was disposed during rendering");
|
|
278
|
+
}
|
|
279
|
+
attempt!.status = "accepted";
|
|
280
|
+
scope!.parent = owner;
|
|
281
|
+
owner.children.add(scope!);
|
|
282
|
+
if (selection && localSelection) {
|
|
283
|
+
selection.matched = localSelection.matched;
|
|
284
|
+
selection.options = localSelection.options;
|
|
285
|
+
}
|
|
286
|
+
return rendered;
|
|
287
|
+
};
|
|
288
|
+
try {
|
|
289
|
+
return branch(render);
|
|
290
|
+
} catch (error) {
|
|
291
|
+
if (owner.disposed) throw error;
|
|
292
|
+
return branch(() => fallback(error));
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** Buffer repeatable template output until compiler binding writes stop invalidating it. */
|
|
299
|
+
export function settle(
|
|
300
|
+
renderTemplate: (invalidate: () => void) => SSRChild,
|
|
301
|
+
): HTML {
|
|
302
|
+
const owner = getOwner();
|
|
303
|
+
if (!owner || owner.disposed)
|
|
304
|
+
throw new Error("SSR settlement requires a live native owner");
|
|
305
|
+
return {
|
|
306
|
+
[html]: (selection, svg) => {
|
|
307
|
+
if (owner.disposed)
|
|
308
|
+
throw new Error("Cannot serialize a disposed SSR settlement");
|
|
309
|
+
for (let iteration = 0; iteration < 1000; iteration++) {
|
|
310
|
+
let dirty = false;
|
|
311
|
+
let stop: (() => void) | undefined;
|
|
312
|
+
let attempt: SSRRenderAttempt | undefined;
|
|
313
|
+
let scope: Owner | undefined;
|
|
314
|
+
const localSelection = selection ? { ...selection } : undefined;
|
|
315
|
+
let rendered: string;
|
|
316
|
+
try {
|
|
317
|
+
rendered = runWithOwner(owner, () =>
|
|
318
|
+
createRoot((dispose) => {
|
|
319
|
+
stop = dispose;
|
|
320
|
+
scope = getOwner()!;
|
|
321
|
+
attempt = createSSRRenderAttempt();
|
|
322
|
+
return withSSRRenderAttempt(attempt, () =>
|
|
323
|
+
content(
|
|
324
|
+
renderTemplate(() => {
|
|
325
|
+
dirty = true;
|
|
326
|
+
}),
|
|
327
|
+
localSelection,
|
|
328
|
+
svg,
|
|
329
|
+
),
|
|
330
|
+
);
|
|
331
|
+
}),
|
|
332
|
+
);
|
|
333
|
+
} catch (error) {
|
|
334
|
+
if (attempt) attempt.status = "discarded";
|
|
335
|
+
try {
|
|
336
|
+
stop?.();
|
|
337
|
+
} catch {
|
|
338
|
+
/* Preserve the render failure. */
|
|
339
|
+
}
|
|
340
|
+
throw error;
|
|
341
|
+
}
|
|
342
|
+
if (dirty) {
|
|
343
|
+
attempt!.status = "discarded";
|
|
344
|
+
stop!();
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (owner.disposed) {
|
|
348
|
+
attempt!.status = "discarded";
|
|
349
|
+
try {
|
|
350
|
+
stop!();
|
|
351
|
+
} catch {
|
|
352
|
+
/* Preserve the ownership failure. */
|
|
353
|
+
}
|
|
354
|
+
throw new Error("SSR settlement owner was disposed during rendering");
|
|
355
|
+
}
|
|
356
|
+
attempt!.status = "accepted";
|
|
357
|
+
scope!.parent = owner;
|
|
358
|
+
owner.children.add(scope!);
|
|
359
|
+
if (selection && localSelection) {
|
|
360
|
+
selection.matched = localSelection.matched;
|
|
361
|
+
selection.options = localSelection.options;
|
|
362
|
+
}
|
|
363
|
+
return rendered;
|
|
364
|
+
}
|
|
365
|
+
throw new Error("SSR settlement update limit exceeded");
|
|
366
|
+
},
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function h<P extends object>(
|
|
371
|
+
component: (props: P & { children?: SSRChild }) => SSRChild,
|
|
372
|
+
props: P | null,
|
|
373
|
+
...children: SSRChild[]
|
|
374
|
+
): SSRChild;
|
|
375
|
+
export function h(
|
|
376
|
+
tag: string,
|
|
377
|
+
props?: Record<string, unknown> | null,
|
|
378
|
+
...children: SSRChild[]
|
|
379
|
+
): HTML;
|
|
380
|
+
export function h<P extends Record<string, unknown>>(
|
|
381
|
+
tag: string | ((props: P & { children?: SSRChild }) => SSRChild),
|
|
382
|
+
props: P | null = null,
|
|
383
|
+
...children: SSRChild[]
|
|
384
|
+
): SSRChild {
|
|
385
|
+
if (typeof tag === "function") {
|
|
386
|
+
const componentProps = children.length
|
|
387
|
+
? Object.defineProperties(
|
|
388
|
+
{},
|
|
389
|
+
{
|
|
390
|
+
...(props && Object.getOwnPropertyDescriptors(props)),
|
|
391
|
+
children: { value: children, enumerable: true, configurable: true },
|
|
392
|
+
},
|
|
393
|
+
)
|
|
394
|
+
: (props ?? {});
|
|
395
|
+
return createComponent(
|
|
396
|
+
tag,
|
|
397
|
+
componentProps as P & { children?: SSRChild },
|
|
398
|
+
"ssr",
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
return intrinsic(tag, props, children);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Compiler-only source attribute codec; children use the existing lazy contract. */
|
|
405
|
+
export function hSource(
|
|
406
|
+
tag: string,
|
|
407
|
+
props: Record<string, unknown> | null = null,
|
|
408
|
+
...children: SSRChild[]
|
|
409
|
+
): HTML {
|
|
410
|
+
return intrinsic(tag, props, children, sourceAttributeValue);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** Compiler-only ordered source attributes, including repeated authored names. */
|
|
414
|
+
export function hSourceEntries(
|
|
415
|
+
tag: string,
|
|
416
|
+
entries: ReadonlyArray<readonly [string, unknown]>,
|
|
417
|
+
...children: SSRChild[]
|
|
418
|
+
): HTML {
|
|
419
|
+
return intrinsic(tag, null, children, sourceAttributeValue, entries);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Explicit HTML attributes without DOM property aliases or form-binding inference. */
|
|
423
|
+
export function hAttributes(
|
|
424
|
+
tag: string,
|
|
425
|
+
attributes: Record<string, unknown> | null = null,
|
|
426
|
+
...children: SSRChild[]
|
|
427
|
+
): HTML {
|
|
428
|
+
return intrinsic(tag, attributes, children, undefined, undefined, true);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** Compiler-facing ordered explicit attributes; repeated names retain source order. */
|
|
432
|
+
export function hAttributeEntries(
|
|
433
|
+
tag: string,
|
|
434
|
+
entries: ReadonlyArray<readonly [string, unknown]>,
|
|
435
|
+
...children: SSRChild[]
|
|
436
|
+
): HTML {
|
|
437
|
+
return intrinsic(tag, null, children, undefined, entries, true);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** Compiler-only explicit attributes with independent typed select/option identity. */
|
|
441
|
+
export function hSelectionEntries(
|
|
442
|
+
tag: "select" | "option",
|
|
443
|
+
entries: ReadonlyArray<readonly [string, unknown]>,
|
|
444
|
+
selection: { value: unknown; preserveSelected?: boolean },
|
|
445
|
+
...children: SSRChild[]
|
|
446
|
+
): HTML {
|
|
447
|
+
if (tag !== "select" && tag !== "option")
|
|
448
|
+
throw new TypeError("Selection entries require select or option");
|
|
449
|
+
return intrinsic(
|
|
450
|
+
tag,
|
|
451
|
+
null,
|
|
452
|
+
children,
|
|
453
|
+
undefined,
|
|
454
|
+
entries,
|
|
455
|
+
true,
|
|
456
|
+
validateBindingBox(selection),
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function intrinsic(
|
|
461
|
+
tag: string,
|
|
462
|
+
props: Record<string, unknown> | null,
|
|
463
|
+
children: SSRChild[],
|
|
464
|
+
sourceCodec?: typeof sourceAttributeValue,
|
|
465
|
+
sourceEntries?: ReadonlyArray<readonly [string, unknown]>,
|
|
466
|
+
explicitAttributes = false,
|
|
467
|
+
selectionBox?: BindingValue,
|
|
468
|
+
): HTML {
|
|
469
|
+
if (!ELEMENT_NAME.test(tag)) {
|
|
470
|
+
throw new TypeError("Invalid server element name");
|
|
471
|
+
}
|
|
472
|
+
const owner = getOwner();
|
|
473
|
+
const lower = tag.toLowerCase();
|
|
474
|
+
const typedSelect =
|
|
475
|
+
lower === "select"
|
|
476
|
+
? (selectionBox ??
|
|
477
|
+
(!explicitAttributes ? bindingValue(props, selectBinding) : undefined))
|
|
478
|
+
: undefined;
|
|
479
|
+
const typedOption =
|
|
480
|
+
lower === "option"
|
|
481
|
+
? (selectionBox ??
|
|
482
|
+
(!explicitAttributes ? bindingValue(props, optionBinding) : undefined))
|
|
483
|
+
: undefined;
|
|
484
|
+
if (lower === "script" || lower === "style") {
|
|
485
|
+
throw new TypeError("Server script/style elements are unsupported");
|
|
486
|
+
}
|
|
487
|
+
const staticMode = explicitAttributes || Boolean(sourceCodec);
|
|
488
|
+
let staticAttributes = "";
|
|
489
|
+
const attributes: Array<[string, unknown]> | undefined = staticMode
|
|
490
|
+
? undefined
|
|
491
|
+
: [];
|
|
492
|
+
let textareaValue: unknown;
|
|
493
|
+
let hasTextareaValue = false;
|
|
494
|
+
let selectValue: unknown;
|
|
495
|
+
let hasSelectValue = false;
|
|
496
|
+
let multiple = false;
|
|
497
|
+
let optionValue: string | undefined;
|
|
498
|
+
let typedOptionValueSlot = false;
|
|
499
|
+
let selectedAttribute = "";
|
|
500
|
+
let sourceNamespace: unknown;
|
|
501
|
+
const names = sourceEntries
|
|
502
|
+
? undefined
|
|
503
|
+
: props
|
|
504
|
+
? Object.keys(props)
|
|
505
|
+
: undefined;
|
|
506
|
+
const attributeCount = sourceEntries
|
|
507
|
+
? sourceEntries.length
|
|
508
|
+
: (names?.length ?? 0);
|
|
509
|
+
for (let index = 0; index < attributeCount; index++) {
|
|
510
|
+
const name = sourceEntries ? sourceEntries[index]![0] : names![index]!;
|
|
511
|
+
if (!ATTRIBUTE_NAME.test(name)) {
|
|
512
|
+
throw new TypeError("Invalid server attribute name");
|
|
513
|
+
}
|
|
514
|
+
// Event and ref handlers are client-only and must never execute on the server.
|
|
515
|
+
if (
|
|
516
|
+
EVENT_NAME.test(name) ||
|
|
517
|
+
(!explicitAttributes && (name === "ref" || name === "children"))
|
|
518
|
+
)
|
|
519
|
+
continue;
|
|
520
|
+
if (
|
|
521
|
+
!explicitAttributes &&
|
|
522
|
+
(name === "innerHTML" ||
|
|
523
|
+
name === "outerHTML" ||
|
|
524
|
+
name === "dangerouslySetInnerHTML")
|
|
525
|
+
) {
|
|
526
|
+
throw new TypeError("Raw server HTML is unsupported");
|
|
527
|
+
}
|
|
528
|
+
if (
|
|
529
|
+
!explicitAttributes &&
|
|
530
|
+
name === "defaultValue" &&
|
|
531
|
+
lower !== "input" &&
|
|
532
|
+
lower !== "textarea"
|
|
533
|
+
)
|
|
534
|
+
throw new TypeError(
|
|
535
|
+
"defaultValue is supported only on input and textarea",
|
|
536
|
+
);
|
|
537
|
+
if (!explicitAttributes && name === "defaultChecked" && lower !== "input")
|
|
538
|
+
throw new TypeError("defaultChecked is supported only on input");
|
|
539
|
+
const property =
|
|
540
|
+
!explicitAttributes && name === "defaultValue"
|
|
541
|
+
? "value"
|
|
542
|
+
: !explicitAttributes && name === "defaultChecked"
|
|
543
|
+
? "checked"
|
|
544
|
+
: name;
|
|
545
|
+
if (
|
|
546
|
+
property !== name &&
|
|
547
|
+
(sourceEntries
|
|
548
|
+
? sourceEntries.some(([candidate]) => candidate === property)
|
|
549
|
+
: Object.hasOwn(props!, property))
|
|
550
|
+
)
|
|
551
|
+
continue;
|
|
552
|
+
// Typed options carry their serialized value as well as comparison identity.
|
|
553
|
+
// The ordinary value prop loses precedence without invoking its getter/accessor.
|
|
554
|
+
if (name === "value" && (typedOption || typedSelect)) {
|
|
555
|
+
if (typedOption && staticMode) {
|
|
556
|
+
typedOptionValueSlot = true;
|
|
557
|
+
if (
|
|
558
|
+
typedOption.value != null &&
|
|
559
|
+
typeof typedOption.value !== "function"
|
|
560
|
+
) {
|
|
561
|
+
optionValue = String(typedOption.value);
|
|
562
|
+
staticAttributes += ` value="${escapeHtml(optionValue)}"`;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
const supplied = sourceEntries ? sourceEntries[index]![1] : props![name];
|
|
568
|
+
let value = typeof supplied === "function" ? supplied() : supplied;
|
|
569
|
+
if (name === "xmlns") sourceNamespace = value;
|
|
570
|
+
if (!explicitAttributes && name === "style") {
|
|
571
|
+
if (value == null || value === false) value = undefined;
|
|
572
|
+
else if (typeof value === "object") {
|
|
573
|
+
const entries = styleEntries(value);
|
|
574
|
+
let styleText = "";
|
|
575
|
+
for (let styleIndex = 0; styleIndex < entries.length; styleIndex++) {
|
|
576
|
+
const [styleProp, entry] = entries[styleIndex]!;
|
|
577
|
+
if (entry != null && entry !== "")
|
|
578
|
+
styleText += `${styleProp}:${entry};`;
|
|
579
|
+
}
|
|
580
|
+
value = styleText;
|
|
581
|
+
} else if (typeof value !== "string") {
|
|
582
|
+
throw new TypeError(
|
|
583
|
+
"Style must be a CSS string, plain record, nullish or false",
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (
|
|
589
|
+
(!explicitAttributes || typedSelect) &&
|
|
590
|
+
lower === "select" &&
|
|
591
|
+
name === "multiple"
|
|
592
|
+
) {
|
|
593
|
+
multiple =
|
|
594
|
+
(sourceCodec
|
|
595
|
+
? sourceCodec(name, value, true)
|
|
596
|
+
: attributeValue(name, value, true)) !== undefined;
|
|
597
|
+
}
|
|
598
|
+
if (!explicitAttributes && lower === "select" && name === "value") {
|
|
599
|
+
selectValue = value;
|
|
600
|
+
hasSelectValue = true;
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
if (!explicitAttributes && lower === "option" && name === "selected") {
|
|
604
|
+
if (
|
|
605
|
+
(sourceCodec
|
|
606
|
+
? sourceCodec(name, value, true)
|
|
607
|
+
: attributeValue(name, value, true)) !== undefined
|
|
608
|
+
)
|
|
609
|
+
selectedAttribute = ` selected=""`;
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
if (!explicitAttributes && lower === "textarea" && property === "value") {
|
|
613
|
+
textareaValue = value;
|
|
614
|
+
hasTextareaValue = true;
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
const attr =
|
|
618
|
+
!explicitAttributes && property === "className"
|
|
619
|
+
? "class"
|
|
620
|
+
: !explicitAttributes && property === "htmlFor"
|
|
621
|
+
? "for"
|
|
622
|
+
: property;
|
|
623
|
+
if (!explicitAttributes && property === "value") {
|
|
624
|
+
if (sourceCodec) {
|
|
625
|
+
const serialized = sourceCodec(
|
|
626
|
+
"value",
|
|
627
|
+
value,
|
|
628
|
+
typeof supplied === "function",
|
|
629
|
+
);
|
|
630
|
+
if (lower === "option") optionValue = String(serialized ?? "");
|
|
631
|
+
if (serialized !== undefined)
|
|
632
|
+
staticAttributes += ` value="${escapeHtml(serialized)}"`;
|
|
633
|
+
} else {
|
|
634
|
+
if (lower === "option") optionValue = String(value ?? "");
|
|
635
|
+
attributes!.push(["value", value]);
|
|
636
|
+
}
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
if (explicitAttributes) {
|
|
640
|
+
if (value != null) staticAttributes += ` ${attr}="${escapeHtml(value)}"`;
|
|
641
|
+
} else if (sourceCodec) {
|
|
642
|
+
const serialized = sourceCodec(
|
|
643
|
+
attr,
|
|
644
|
+
value,
|
|
645
|
+
typeof supplied === "function",
|
|
646
|
+
);
|
|
647
|
+
if (serialized !== undefined)
|
|
648
|
+
staticAttributes += ` ${attr}="${escapeHtml(serialized)}"`;
|
|
649
|
+
} else {
|
|
650
|
+
attributes!.push([attr, value]);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (
|
|
654
|
+
typedOption &&
|
|
655
|
+
!typedOptionValueSlot &&
|
|
656
|
+
typedOption.value != null &&
|
|
657
|
+
typeof typedOption.value !== "function"
|
|
658
|
+
) {
|
|
659
|
+
optionValue = String(typedOption.value);
|
|
660
|
+
if (staticMode) {
|
|
661
|
+
staticAttributes += ` value="${escapeHtml(optionValue)}"`;
|
|
662
|
+
} else {
|
|
663
|
+
attributes!.push(["value", optionValue]);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (
|
|
667
|
+
lower === "select" &&
|
|
668
|
+
!typedSelect &&
|
|
669
|
+
Array.isArray(selectValue) &&
|
|
670
|
+
!multiple
|
|
671
|
+
) {
|
|
672
|
+
throw new TypeError("Server select array values require multiple");
|
|
673
|
+
}
|
|
674
|
+
const textOnly =
|
|
675
|
+
lower === "textarea" || lower === "title" || lower === "option";
|
|
676
|
+
const sourceChild =
|
|
677
|
+
lower === "textarea" && hasTextareaValue
|
|
678
|
+
? undefined
|
|
679
|
+
: children.length === 1
|
|
680
|
+
? children[0]
|
|
681
|
+
: children.length
|
|
682
|
+
? children
|
|
683
|
+
: explicitAttributes
|
|
684
|
+
? undefined
|
|
685
|
+
: (props?.children as SSRChild);
|
|
686
|
+
const bodyChild = resolve(sourceChild);
|
|
687
|
+
const bodyText = textOnly
|
|
688
|
+
? hasTextareaValue
|
|
689
|
+
? String(textareaValue ?? "")
|
|
690
|
+
: text(bodyChild)
|
|
691
|
+
: undefined;
|
|
692
|
+
let escapedBodyText =
|
|
693
|
+
bodyText === undefined ? undefined : escapeHtml(bodyText);
|
|
694
|
+
if (
|
|
695
|
+
escapedBodyText !== undefined &&
|
|
696
|
+
!explicitAttributes &&
|
|
697
|
+
lower === "textarea" &&
|
|
698
|
+
(escapedBodyText.startsWith("\n") || escapedBodyText.startsWith("\r"))
|
|
699
|
+
) {
|
|
700
|
+
escapedBodyText = `\n${escapedBodyText}`;
|
|
701
|
+
}
|
|
702
|
+
const isSvg = (inherited = false) =>
|
|
703
|
+
sourceNamespace != null
|
|
704
|
+
? sourceNamespace === "http://www.w3.org/2000/svg"
|
|
705
|
+
: lower === "svg" || inherited;
|
|
706
|
+
const renderAttributes = (svg: boolean) => {
|
|
707
|
+
if (staticMode) return staticAttributes;
|
|
708
|
+
let out = "";
|
|
709
|
+
for (let index = 0; index < attributes!.length; index++) {
|
|
710
|
+
const [attr, value] = attributes![index]!;
|
|
711
|
+
if (!svg && attr === "checked" && !value) continue;
|
|
712
|
+
const serialized = attributeValue(
|
|
713
|
+
attr,
|
|
714
|
+
!svg &&
|
|
715
|
+
attr === "value" &&
|
|
716
|
+
lower !== "progress" &&
|
|
717
|
+
lower !== "meter" &&
|
|
718
|
+
lower !== "li"
|
|
719
|
+
? (value ?? "")
|
|
720
|
+
: value,
|
|
721
|
+
!svg,
|
|
722
|
+
);
|
|
723
|
+
if (serialized !== undefined)
|
|
724
|
+
out += ` ${attr}="${escapeHtml(serialized)}"`;
|
|
725
|
+
}
|
|
726
|
+
return out;
|
|
727
|
+
};
|
|
728
|
+
if (VOID.has(lower)) {
|
|
729
|
+
if (bodyChild != null && bodyChild !== false && content(bodyChild))
|
|
730
|
+
throw new TypeError("Void server elements cannot have children");
|
|
731
|
+
if (staticMode) {
|
|
732
|
+
const markup = `<${tag}${staticAttributes}>`;
|
|
733
|
+
return { [html]: () => markup };
|
|
734
|
+
}
|
|
735
|
+
return {
|
|
736
|
+
[html]: (_selection, inheritedSvg) =>
|
|
737
|
+
`<${tag}${renderAttributes(isSvg(inheritedSvg))}>`,
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
if (staticMode && escapedBodyText !== undefined && lower !== "option") {
|
|
741
|
+
const markup = `<${tag}${staticAttributes}>${escapedBodyText}</${tag}>`;
|
|
742
|
+
return { [html]: () => markup };
|
|
743
|
+
}
|
|
744
|
+
return {
|
|
745
|
+
[html]: (inherited, inheritedSvg) => {
|
|
746
|
+
const svg = isSvg(inheritedSvg);
|
|
747
|
+
const selection =
|
|
748
|
+
lower === "select"
|
|
749
|
+
? typedSelect
|
|
750
|
+
? { binding: typedSelect, multiple, matched: false, options: 0 }
|
|
751
|
+
: hasSelectValue
|
|
752
|
+
? {
|
|
753
|
+
values: new Set(
|
|
754
|
+
(multiple
|
|
755
|
+
? Array.isArray(selectValue)
|
|
756
|
+
? selectValue
|
|
757
|
+
: selectValue == null
|
|
758
|
+
? []
|
|
759
|
+
: [selectValue]
|
|
760
|
+
: [selectValue ?? ""]
|
|
761
|
+
).map(String),
|
|
762
|
+
),
|
|
763
|
+
multiple,
|
|
764
|
+
matched: false,
|
|
765
|
+
options: 0,
|
|
766
|
+
}
|
|
767
|
+
: undefined
|
|
768
|
+
: inherited;
|
|
769
|
+
let selected = selectedAttribute;
|
|
770
|
+
if (lower === "option" && selection) {
|
|
771
|
+
selection.options++;
|
|
772
|
+
let matches: boolean;
|
|
773
|
+
if (selection.binding) {
|
|
774
|
+
const raw =
|
|
775
|
+
typedOption !== undefined
|
|
776
|
+
? typedOption.value
|
|
777
|
+
: (optionValue ??
|
|
778
|
+
bodyText!.replace(/[\t\n\f\r ]+/g, " ").replace(/^ | $/g, ""));
|
|
779
|
+
matches =
|
|
780
|
+
selection.multiple && Array.isArray(selection.binding.value)
|
|
781
|
+
? selection.binding.value.includes(raw)
|
|
782
|
+
: raw === selection.binding.value;
|
|
783
|
+
} else {
|
|
784
|
+
const value =
|
|
785
|
+
optionValue ??
|
|
786
|
+
bodyText!.replace(/[\t\n\f\r ]+/g, " ").replace(/^ | $/g, "");
|
|
787
|
+
matches =
|
|
788
|
+
selection.values!.has(value) &&
|
|
789
|
+
(selection.multiple || !selection.matched);
|
|
790
|
+
}
|
|
791
|
+
selected =
|
|
792
|
+
matches || (selection.binding?.preserveSelected && selectedAttribute)
|
|
793
|
+
? ` selected=""`
|
|
794
|
+
: "";
|
|
795
|
+
if (matches) selection.matched = true;
|
|
796
|
+
}
|
|
797
|
+
let body: string;
|
|
798
|
+
if (escapedBodyText !== undefined) {
|
|
799
|
+
body = escapedBodyText;
|
|
800
|
+
} else {
|
|
801
|
+
const activeOwner = getOwner();
|
|
802
|
+
const targetOwner = owner && !owner.disposed ? owner : activeOwner;
|
|
803
|
+
const nextSvg = svg && lower !== "foreignobject";
|
|
804
|
+
body =
|
|
805
|
+
targetOwner === activeOwner
|
|
806
|
+
? content(bodyChild, selection, nextSvg)
|
|
807
|
+
: runWithOwner(targetOwner, () =>
|
|
808
|
+
content(bodyChild, selection, nextSvg),
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
if (
|
|
812
|
+
lower === "select" &&
|
|
813
|
+
selection &&
|
|
814
|
+
!selection.binding &&
|
|
815
|
+
!selection.multiple &&
|
|
816
|
+
selection.options > 0 &&
|
|
817
|
+
!selection.matched
|
|
818
|
+
) {
|
|
819
|
+
throw new TypeError("Server select value has no matching option");
|
|
820
|
+
}
|
|
821
|
+
return `<${tag}${staticMode ? staticAttributes : renderAttributes(svg)}${selected}>${body}</${tag}>`;
|
|
822
|
+
},
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
export function renderToString(fn: () => SSRChild): string {
|
|
827
|
+
return withSSRRendering(() =>
|
|
828
|
+
withSSRRenderAttempt(undefined, () => {
|
|
829
|
+
let dispose = () => {};
|
|
830
|
+
let result: string;
|
|
831
|
+
try {
|
|
832
|
+
result = createRoot((stop) => {
|
|
833
|
+
dispose = stop;
|
|
834
|
+
resetSSRRenderAttempt();
|
|
835
|
+
const requestOwner = getOwner()!;
|
|
836
|
+
if (requestOwner.context?.has(cleanupOrderKey)) {
|
|
837
|
+
requestOwner.context = new Map(requestOwner.context);
|
|
838
|
+
requestOwner.context.delete(cleanupOrderKey);
|
|
839
|
+
}
|
|
840
|
+
return content(fn());
|
|
841
|
+
});
|
|
842
|
+
} catch (error) {
|
|
843
|
+
try {
|
|
844
|
+
dispose();
|
|
845
|
+
} catch {
|
|
846
|
+
/* Preserve the render error. */
|
|
847
|
+
}
|
|
848
|
+
throw error;
|
|
849
|
+
}
|
|
850
|
+
dispose();
|
|
851
|
+
return result;
|
|
852
|
+
}),
|
|
853
|
+
);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/** Server lowering must import lifecycle hooks here; browser effects never run. */
|
|
857
|
+
export function createEffect<T>(_fn: (previous: T) => T, _initial: T): void;
|
|
858
|
+
export function createEffect(_fn: (previous: undefined) => unknown): void;
|
|
859
|
+
export function createEffect<T>(_fn: (previous: T) => T, _initial?: T): void {}
|
|
860
|
+
export function onMount(_fn: () => void): void {}
|
|
861
|
+
|
|
862
|
+
export {
|
|
863
|
+
createSignal,
|
|
864
|
+
createMemo,
|
|
865
|
+
createRoot,
|
|
866
|
+
createComputed,
|
|
867
|
+
batch,
|
|
868
|
+
untrack,
|
|
869
|
+
onCleanup,
|
|
870
|
+
on,
|
|
871
|
+
getOwner,
|
|
872
|
+
runWithOwner,
|
|
873
|
+
} from "./reactive";
|
|
874
|
+
export type { Accessor, Setter, Owner } from "./reactive";
|