@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/control.ts
ADDED
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/** Owned control flow for Moonshine lowering, not Solid's compiled ABI. */
|
|
2
|
+
import {
|
|
3
|
+
batch,
|
|
4
|
+
createMemo,
|
|
5
|
+
createRenderMemo,
|
|
6
|
+
getRenderScheduler,
|
|
7
|
+
getOwner,
|
|
8
|
+
type Owner,
|
|
9
|
+
createRoot,
|
|
10
|
+
createComponentScope,
|
|
11
|
+
createSignal,
|
|
12
|
+
onCleanup,
|
|
13
|
+
type Accessor,
|
|
14
|
+
type Setter,
|
|
15
|
+
} from "./reactive";
|
|
16
|
+
|
|
17
|
+
type Entry<T, U> = {
|
|
18
|
+
item: T;
|
|
19
|
+
value: U;
|
|
20
|
+
index: Setter<number>;
|
|
21
|
+
dispose: () => void;
|
|
22
|
+
scope?: Owner;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function owned<T>(
|
|
26
|
+
fn: () => T,
|
|
27
|
+
order?: () => number,
|
|
28
|
+
owner?: Owner | null,
|
|
29
|
+
): { value: T; dispose: () => void; scope?: Owner } {
|
|
30
|
+
let stop: (() => void) | undefined;
|
|
31
|
+
try {
|
|
32
|
+
return createRoot((dispose) => {
|
|
33
|
+
stop = dispose;
|
|
34
|
+
const scheduler = getRenderScheduler();
|
|
35
|
+
const value = scheduler ? scheduler.branch(fn, order) : fn();
|
|
36
|
+
if (owner && !owner.disposed) {
|
|
37
|
+
const scope = getOwner()!;
|
|
38
|
+
scope.parent = owner;
|
|
39
|
+
owner.children.add(scope);
|
|
40
|
+
}
|
|
41
|
+
return owner
|
|
42
|
+
? { value, dispose, scope: getOwner()! }
|
|
43
|
+
: { value, dispose };
|
|
44
|
+
});
|
|
45
|
+
} catch (error) {
|
|
46
|
+
try {
|
|
47
|
+
stop?.();
|
|
48
|
+
} catch {
|
|
49
|
+
// Preserve the mapper failure after all owned cleanups run.
|
|
50
|
+
}
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function disposeAll(entries: Iterable<{ dispose: () => void }>): void {
|
|
56
|
+
let failed = false;
|
|
57
|
+
let failure: unknown;
|
|
58
|
+
for (const entry of entries) {
|
|
59
|
+
try {
|
|
60
|
+
entry.dispose();
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (!failed) failure = error;
|
|
63
|
+
failed = true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (failed) throw failure;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Deferred removals transfer root ownership; immediate fallbacks own one empty branch per read. */
|
|
70
|
+
export function mapArray<T, U, F = never>(
|
|
71
|
+
list: Accessor<readonly T[]>,
|
|
72
|
+
map: (item: T, index: Accessor<number>) => U,
|
|
73
|
+
deferRemoved?: (dispose: () => void) => void,
|
|
74
|
+
schedule: (
|
|
75
|
+
read: () => U[] | NoInfer<F>,
|
|
76
|
+
) => Accessor<U[] | NoInfer<F>> = createRenderMemo,
|
|
77
|
+
removalOrder: "after-map" | "before-map" = "after-map",
|
|
78
|
+
fallback?: () => F,
|
|
79
|
+
): Accessor<U[] | NoInfer<F>> {
|
|
80
|
+
if (deferRemoved && removalOrder === "before-map")
|
|
81
|
+
throw new TypeError("Removal before mapping cannot be deferred");
|
|
82
|
+
if (deferRemoved && fallback)
|
|
83
|
+
throw new TypeError("Owned fallback removal cannot be deferred");
|
|
84
|
+
const owner = getRenderScheduler()
|
|
85
|
+
? createComponentScope(() => getOwner()!)
|
|
86
|
+
: undefined;
|
|
87
|
+
let entries: Entry<T, U>[] = [];
|
|
88
|
+
let fallbackRoot: ReturnType<typeof owned<F>> | undefined;
|
|
89
|
+
let disposed = false;
|
|
90
|
+
onCleanup(() => {
|
|
91
|
+
disposed = true;
|
|
92
|
+
const previous = entries,
|
|
93
|
+
previousFallback = fallbackRoot;
|
|
94
|
+
entries = [];
|
|
95
|
+
fallbackRoot = undefined;
|
|
96
|
+
disposeAll(previousFallback ? [...previous, previousFallback] : previous);
|
|
97
|
+
});
|
|
98
|
+
return schedule(() => {
|
|
99
|
+
const items = list();
|
|
100
|
+
const mappedLength =
|
|
101
|
+
removalOrder === "before-map" ? items.length : undefined;
|
|
102
|
+
const previousFallback = fallbackRoot;
|
|
103
|
+
fallbackRoot = undefined;
|
|
104
|
+
previousFallback?.dispose();
|
|
105
|
+
if (fallback && (mappedLength ?? items.length) === 0) {
|
|
106
|
+
const previous = entries;
|
|
107
|
+
entries = [];
|
|
108
|
+
disposeAll(previous);
|
|
109
|
+
if (disposed) return [];
|
|
110
|
+
const root = owned(fallback, undefined, owner);
|
|
111
|
+
if (disposed) {
|
|
112
|
+
root.dispose();
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
fallbackRoot = root;
|
|
116
|
+
return root.value;
|
|
117
|
+
}
|
|
118
|
+
const available = new Map<T, Entry<T, U>[]>();
|
|
119
|
+
let retainedPlan: Array<Entry<T, U> | undefined> | undefined;
|
|
120
|
+
let start = 0,
|
|
121
|
+
end = entries.length - 1,
|
|
122
|
+
newEnd = 0;
|
|
123
|
+
if (removalOrder === "before-map") {
|
|
124
|
+
newEnd = mappedLength! - 1;
|
|
125
|
+
retainedPlan = [];
|
|
126
|
+
while (
|
|
127
|
+
start <= Math.min(end, newEnd) &&
|
|
128
|
+
entries[start]!.item === items[start]
|
|
129
|
+
) {
|
|
130
|
+
retainedPlan[start] = entries[start];
|
|
131
|
+
start++;
|
|
132
|
+
}
|
|
133
|
+
while (
|
|
134
|
+
end >= start &&
|
|
135
|
+
newEnd >= start &&
|
|
136
|
+
entries[end]!.item === items[newEnd]
|
|
137
|
+
) {
|
|
138
|
+
retainedPlan[newEnd--] = entries[end--];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
for (let i = end; i >= start; i--) {
|
|
142
|
+
const entry = entries[i]!;
|
|
143
|
+
const bucket = available.get(entry.item);
|
|
144
|
+
if (bucket) bucket.push(entry);
|
|
145
|
+
else available.set(entry.item, [entry]);
|
|
146
|
+
}
|
|
147
|
+
if (retainedPlan) {
|
|
148
|
+
for (let position = start; position <= newEnd; position++)
|
|
149
|
+
retainedPlan[position] = available.get(items[position]!)?.pop();
|
|
150
|
+
const retained = new Set(retainedPlan);
|
|
151
|
+
const previous = entries;
|
|
152
|
+
entries = [];
|
|
153
|
+
const removed: Entry<T, U>[] = [];
|
|
154
|
+
for (const entry of previous)
|
|
155
|
+
(retained.has(entry) ? entries : removed).push(entry);
|
|
156
|
+
// Keep only live survivors if cleanup throws or a replacement mapper fails.
|
|
157
|
+
disposeAll(removed);
|
|
158
|
+
}
|
|
159
|
+
const created: Entry<T, U>[] = [];
|
|
160
|
+
let next: Entry<T, U>[];
|
|
161
|
+
try {
|
|
162
|
+
next = [];
|
|
163
|
+
for (
|
|
164
|
+
let position = 0;
|
|
165
|
+
position < (mappedLength ?? items.length);
|
|
166
|
+
position++
|
|
167
|
+
) {
|
|
168
|
+
if (disposed) break;
|
|
169
|
+
const item = items[position]!;
|
|
170
|
+
const retained = retainedPlan
|
|
171
|
+
? retainedPlan[position]
|
|
172
|
+
: available.get(item)?.pop();
|
|
173
|
+
if (retained) {
|
|
174
|
+
if (retainedPlan) retained.index(position);
|
|
175
|
+
next.push(retained);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const [index, setIndex] = createSignal(position);
|
|
179
|
+
const root = owned(() => map(item, index), index, owner);
|
|
180
|
+
const entry = { item, ...root, index: setIndex };
|
|
181
|
+
created.push(entry);
|
|
182
|
+
next.push(entry);
|
|
183
|
+
}
|
|
184
|
+
} catch (error) {
|
|
185
|
+
try {
|
|
186
|
+
disposeAll(created);
|
|
187
|
+
} catch {
|
|
188
|
+
// Preserve the mapper failure after all new entries are disposed.
|
|
189
|
+
}
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
if (disposed) {
|
|
193
|
+
disposeAll(created);
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
let removed: Entry<T, U>[] = [];
|
|
197
|
+
if (entries.length !== next.length - created.length) {
|
|
198
|
+
const retained = new Set(next);
|
|
199
|
+
removed = entries.filter((entry) => !retained.has(entry));
|
|
200
|
+
}
|
|
201
|
+
const reordered = next.some((entry, index) => entry !== entries[index]);
|
|
202
|
+
entries = next;
|
|
203
|
+
if (owner && reordered) {
|
|
204
|
+
const pending = [...owner.children];
|
|
205
|
+
owner.children.clear();
|
|
206
|
+
for (const entry of next)
|
|
207
|
+
if (entry.scope) owner.children.add(entry.scope);
|
|
208
|
+
// Deferred removals remain owned until their explicit commit/disposal.
|
|
209
|
+
for (const child of pending) owner.children.add(child);
|
|
210
|
+
}
|
|
211
|
+
batch(() => {
|
|
212
|
+
next.forEach((entry, index) => entry.index(index));
|
|
213
|
+
if (reordered) getRenderScheduler()?.reorder();
|
|
214
|
+
if (deferRemoved && removed.length) {
|
|
215
|
+
try {
|
|
216
|
+
deferRemoved(() => disposeAll(removed));
|
|
217
|
+
} catch (error) {
|
|
218
|
+
try {
|
|
219
|
+
disposeAll(removed);
|
|
220
|
+
} catch {}
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
} else disposeAll(removed);
|
|
224
|
+
});
|
|
225
|
+
return disposed ? [] : next.map((entry) => entry.value);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Retain positions; before-map publishes item values before creating later slots. */
|
|
230
|
+
export function indexArray<T, U, F = never>(
|
|
231
|
+
list: Accessor<readonly T[]>,
|
|
232
|
+
map: (item: Accessor<T>, index: number) => U,
|
|
233
|
+
schedule: (
|
|
234
|
+
read: () => U[] | NoInfer<F>,
|
|
235
|
+
) => Accessor<U[] | NoInfer<F>> = createRenderMemo,
|
|
236
|
+
updateOrder: "after-map" | "before-map" = "after-map",
|
|
237
|
+
fallback?: () => F,
|
|
238
|
+
): Accessor<U[] | NoInfer<F>> {
|
|
239
|
+
const owner = getRenderScheduler()
|
|
240
|
+
? createComponentScope(() => getOwner()!)
|
|
241
|
+
: undefined;
|
|
242
|
+
let entries: Array<{
|
|
243
|
+
value: U;
|
|
244
|
+
dispose: () => void;
|
|
245
|
+
write: Setter<T>;
|
|
246
|
+
scope?: Owner;
|
|
247
|
+
}> = [];
|
|
248
|
+
let fallbackRoot: ReturnType<typeof owned<F>> | undefined;
|
|
249
|
+
let disposed = false;
|
|
250
|
+
onCleanup(() => {
|
|
251
|
+
disposed = true;
|
|
252
|
+
const previous = entries,
|
|
253
|
+
previousFallback = fallbackRoot;
|
|
254
|
+
entries = [];
|
|
255
|
+
fallbackRoot = undefined;
|
|
256
|
+
disposeAll(previousFallback ? [...previous, previousFallback] : previous);
|
|
257
|
+
});
|
|
258
|
+
return schedule(() => {
|
|
259
|
+
const items = list();
|
|
260
|
+
const mappedLength =
|
|
261
|
+
updateOrder === "before-map" ? items.length : undefined;
|
|
262
|
+
const previousFallback = fallbackRoot;
|
|
263
|
+
fallbackRoot = undefined;
|
|
264
|
+
previousFallback?.dispose();
|
|
265
|
+
if (fallback && (mappedLength ?? items.length) === 0) {
|
|
266
|
+
const previous = entries;
|
|
267
|
+
entries = [];
|
|
268
|
+
disposeAll(previous);
|
|
269
|
+
if (disposed) return [];
|
|
270
|
+
const root = owned(fallback, undefined, owner);
|
|
271
|
+
if (disposed) {
|
|
272
|
+
root.dispose();
|
|
273
|
+
return [];
|
|
274
|
+
}
|
|
275
|
+
fallbackRoot = root;
|
|
276
|
+
return root.value;
|
|
277
|
+
}
|
|
278
|
+
const next = entries.slice(0, mappedLength ?? items.length);
|
|
279
|
+
if (mappedLength !== undefined)
|
|
280
|
+
next.forEach((entry, index) => entry.write(() => items[index]!));
|
|
281
|
+
const created: typeof entries = [];
|
|
282
|
+
try {
|
|
283
|
+
for (
|
|
284
|
+
let index = next.length;
|
|
285
|
+
index < (mappedLength ?? items.length);
|
|
286
|
+
index++
|
|
287
|
+
) {
|
|
288
|
+
if (disposed) break;
|
|
289
|
+
const [item, write] = createSignal<T>(items[index]!);
|
|
290
|
+
const entry = {
|
|
291
|
+
...owned(() => map(item, index), undefined, owner),
|
|
292
|
+
write,
|
|
293
|
+
};
|
|
294
|
+
created.push(entry);
|
|
295
|
+
next.push(entry);
|
|
296
|
+
}
|
|
297
|
+
} catch (error) {
|
|
298
|
+
try {
|
|
299
|
+
disposeAll(created);
|
|
300
|
+
} catch {
|
|
301
|
+
// Preserve the mapper failure after all new entries are disposed.
|
|
302
|
+
}
|
|
303
|
+
throw error;
|
|
304
|
+
}
|
|
305
|
+
if (disposed) {
|
|
306
|
+
disposeAll(created);
|
|
307
|
+
return [];
|
|
308
|
+
}
|
|
309
|
+
const removed = entries.slice(mappedLength ?? items.length);
|
|
310
|
+
entries = next;
|
|
311
|
+
batch(() => {
|
|
312
|
+
if (mappedLength === undefined)
|
|
313
|
+
next.forEach((entry, index) => entry.write(() => items[index]!));
|
|
314
|
+
disposeAll(removed);
|
|
315
|
+
});
|
|
316
|
+
return disposed ? [] : next.map((entry) => entry.value);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function Index<T, U>(props: {
|
|
321
|
+
each: Accessor<readonly T[]>;
|
|
322
|
+
children: (item: Accessor<T>, index: number) => U;
|
|
323
|
+
}): Accessor<U[]> {
|
|
324
|
+
return indexArray(props.each, props.children);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export function For<T, U>(
|
|
328
|
+
props: {
|
|
329
|
+
each: Accessor<readonly T[]>;
|
|
330
|
+
children: (item: T, index: Accessor<number>) => U;
|
|
331
|
+
},
|
|
332
|
+
schedule: (read: () => U[]) => Accessor<U[]> = createRenderMemo,
|
|
333
|
+
): Accessor<U[]> {
|
|
334
|
+
return mapArray(props.each, props.children, undefined, schedule);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function Show<T, U>(
|
|
338
|
+
props: {
|
|
339
|
+
when: Accessor<T>;
|
|
340
|
+
children: (value: Accessor<NonNullable<T>>) => U;
|
|
341
|
+
fallback?: () => U;
|
|
342
|
+
},
|
|
343
|
+
schedule: (
|
|
344
|
+
read: () => U | undefined,
|
|
345
|
+
) => Accessor<U | undefined> = createRenderMemo,
|
|
346
|
+
cache: <V>(read: () => V) => Accessor<V> = createMemo,
|
|
347
|
+
): Accessor<U | undefined> {
|
|
348
|
+
const owner = getRenderScheduler() ? getOwner() : undefined;
|
|
349
|
+
let branch: { value: U | undefined; dispose: () => void } | undefined;
|
|
350
|
+
let visible: boolean | undefined;
|
|
351
|
+
let disposed = false;
|
|
352
|
+
onCleanup(() => {
|
|
353
|
+
disposed = true;
|
|
354
|
+
branch?.dispose();
|
|
355
|
+
branch = undefined;
|
|
356
|
+
});
|
|
357
|
+
const value = cache(props.when);
|
|
358
|
+
if (disposed) return () => undefined;
|
|
359
|
+
const condition = cache(() => Boolean(value()));
|
|
360
|
+
if (disposed) return () => undefined;
|
|
361
|
+
return schedule(() => {
|
|
362
|
+
if (disposed) return undefined;
|
|
363
|
+
const nextVisible = condition();
|
|
364
|
+
if (visible !== nextVisible) {
|
|
365
|
+
branch?.dispose();
|
|
366
|
+
branch = undefined;
|
|
367
|
+
visible = undefined;
|
|
368
|
+
if (disposed) return undefined;
|
|
369
|
+
branch = owned(
|
|
370
|
+
() =>
|
|
371
|
+
nextVisible
|
|
372
|
+
? props.children(() => {
|
|
373
|
+
const current = value();
|
|
374
|
+
if (!current) throw new Error("Show branch is inactive");
|
|
375
|
+
return current as NonNullable<T>;
|
|
376
|
+
})
|
|
377
|
+
: props.fallback?.(),
|
|
378
|
+
undefined,
|
|
379
|
+
owner,
|
|
380
|
+
);
|
|
381
|
+
if (disposed) {
|
|
382
|
+
const cancelled = branch;
|
|
383
|
+
branch = undefined;
|
|
384
|
+
cancelled.dispose();
|
|
385
|
+
return undefined;
|
|
386
|
+
}
|
|
387
|
+
visible = nextVisible;
|
|
388
|
+
}
|
|
389
|
+
return branch?.value;
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const MATCH = /* @__PURE__ */ Symbol("moonshine-match");
|
|
394
|
+
export type MatchBranch<T, U> = {
|
|
395
|
+
readonly [MATCH]: true;
|
|
396
|
+
readonly when: Accessor<T>;
|
|
397
|
+
readonly children: (value: Accessor<NonNullable<T>>) => U;
|
|
398
|
+
};
|
|
399
|
+
/** Describe a branch without evaluating its condition or creating its contents. */
|
|
400
|
+
export function Match<T, U>(props: {
|
|
401
|
+
when: Accessor<T>;
|
|
402
|
+
children: (value: Accessor<NonNullable<T>>) => U;
|
|
403
|
+
}): MatchBranch<T, U> {
|
|
404
|
+
return { [MATCH]: true, when: props.when, children: props.children };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** First truthy branch wins; retained branches keep their owner and live value. */
|
|
408
|
+
export function Switch<U>(props: {
|
|
409
|
+
children: Accessor<readonly MatchBranch<any, U>[]>;
|
|
410
|
+
fallback?: () => U;
|
|
411
|
+
}): Accessor<U | undefined> {
|
|
412
|
+
const owner = getRenderScheduler() ? getOwner() : undefined;
|
|
413
|
+
let current: MatchBranch<any, U> | undefined;
|
|
414
|
+
let branch: { value: U | undefined; dispose: () => void } | undefined;
|
|
415
|
+
let initialized = false;
|
|
416
|
+
let disposed = false;
|
|
417
|
+
onCleanup(() => {
|
|
418
|
+
disposed = true;
|
|
419
|
+
branch?.dispose();
|
|
420
|
+
branch = undefined;
|
|
421
|
+
});
|
|
422
|
+
const branches = createMemo(props.children);
|
|
423
|
+
if (disposed) return () => undefined;
|
|
424
|
+
const selection = createMemo(() => {
|
|
425
|
+
for (const candidate of branches()) {
|
|
426
|
+
if (!candidate || candidate[MATCH] !== true)
|
|
427
|
+
throw new TypeError("Switch children must be Match branches");
|
|
428
|
+
const value = candidate.when();
|
|
429
|
+
if (value) return { candidate, value };
|
|
430
|
+
}
|
|
431
|
+
return undefined;
|
|
432
|
+
});
|
|
433
|
+
if (disposed) return () => undefined;
|
|
434
|
+
const selected = createMemo(() => selection()?.candidate);
|
|
435
|
+
return createRenderMemo(() => {
|
|
436
|
+
const next = selected();
|
|
437
|
+
if (!initialized || current !== next) {
|
|
438
|
+
branch?.dispose();
|
|
439
|
+
branch = undefined;
|
|
440
|
+
initialized = false;
|
|
441
|
+
current = undefined;
|
|
442
|
+
if (disposed) return undefined;
|
|
443
|
+
branch = owned(
|
|
444
|
+
() =>
|
|
445
|
+
next
|
|
446
|
+
? next.children(() => {
|
|
447
|
+
const chosen = selection();
|
|
448
|
+
if (chosen?.candidate !== next)
|
|
449
|
+
throw new Error("Match branch is inactive");
|
|
450
|
+
return chosen.value;
|
|
451
|
+
})
|
|
452
|
+
: props.fallback?.(),
|
|
453
|
+
undefined,
|
|
454
|
+
owner,
|
|
455
|
+
);
|
|
456
|
+
if (disposed) {
|
|
457
|
+
const cancelled = branch;
|
|
458
|
+
branch = undefined;
|
|
459
|
+
cancelled.dispose();
|
|
460
|
+
return undefined;
|
|
461
|
+
}
|
|
462
|
+
current = next;
|
|
463
|
+
initialized = true;
|
|
464
|
+
}
|
|
465
|
+
return branch?.value;
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export { ErrorBoundary } from "./error";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** Opt-in compiler writes for HTML custom elements; ordinary h props are unchanged. */
|
|
2
|
+
import { runWithOwner, untrack } from "./reactive";
|
|
3
|
+
|
|
4
|
+
export function setCustomElementValue(
|
|
5
|
+
node: HTMLElement,
|
|
6
|
+
name: string,
|
|
7
|
+
value: unknown,
|
|
8
|
+
): void {
|
|
9
|
+
if (
|
|
10
|
+
node.namespaceURI !== "http://www.w3.org/1999/xhtml" ||
|
|
11
|
+
!node.localName.includes("-")
|
|
12
|
+
)
|
|
13
|
+
throw new TypeError(
|
|
14
|
+
"Custom element values require a hyphenated HTML element",
|
|
15
|
+
);
|
|
16
|
+
if (typeof name !== "string" || /^on/i.test(name))
|
|
17
|
+
throw new TypeError("Custom element events must use event listeners");
|
|
18
|
+
// Let the DOM validate names, including names outside ASCII, before setters run.
|
|
19
|
+
node.ownerDocument.createAttribute(name);
|
|
20
|
+
runWithOwner(null, () =>
|
|
21
|
+
untrack(() => {
|
|
22
|
+
const lower = name.toLowerCase();
|
|
23
|
+
const attributeOnly =
|
|
24
|
+
lower === "style" ||
|
|
25
|
+
lower === "innerhtml" ||
|
|
26
|
+
lower === "outerhtml" ||
|
|
27
|
+
lower === "textcontent" ||
|
|
28
|
+
lower === "innertext" ||
|
|
29
|
+
lower === "__proto__";
|
|
30
|
+
let property = false;
|
|
31
|
+
if (!attributeOnly) {
|
|
32
|
+
const view = node.ownerDocument.defaultView;
|
|
33
|
+
if (view?.customElements.get(node.localName)) {
|
|
34
|
+
const boundary = view.Element.prototype;
|
|
35
|
+
for (
|
|
36
|
+
let proto: object | null = node;
|
|
37
|
+
proto && proto !== boundary;
|
|
38
|
+
proto = Object.getPrototypeOf(proto)
|
|
39
|
+
) {
|
|
40
|
+
if (Object.getOwnPropertyDescriptor(proto, name)?.set) {
|
|
41
|
+
property = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} else property = value !== null && typeof value === "object";
|
|
46
|
+
}
|
|
47
|
+
if (property) {
|
|
48
|
+
if (!Reflect.set(node, name, value))
|
|
49
|
+
throw new TypeError(`Cannot set custom element property ${name}`);
|
|
50
|
+
} else if (value == null) node.removeAttribute(name);
|
|
51
|
+
else node.setAttribute(name, String(value));
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
}
|