what-core 0.13.11 → 0.15.0
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 +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
package/src/dom.js
CHANGED
|
@@ -1,54 +1,17 @@
|
|
|
1
|
+
import { report, setNextBinding, _thrownMessage } from './diagnostics.js';
|
|
1
2
|
// What Framework - Fine-Grained DOM Runtime
|
|
2
3
|
// Components run ONCE. Signals create individual DOM effects.
|
|
3
4
|
// No VDOM reconciler, no diffing — direct DOM manipulation driven by signals.
|
|
4
5
|
|
|
5
|
-
import { effect, untrack,
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
const SVG_ELEMENTS = new Set([
|
|
10
|
-
'svg', 'path', 'circle', 'rect', 'line', 'polyline', 'polygon', 'ellipse',
|
|
11
|
-
'g', 'defs', 'use', 'symbol', 'clipPath', 'mask', 'pattern', 'image',
|
|
12
|
-
'text', 'tspan', 'textPath', 'foreignObject', 'linearGradient', 'radialGradient', 'stop',
|
|
13
|
-
'marker', 'animate', 'animateTransform', 'animateMotion', 'set', 'filter',
|
|
14
|
-
'feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix',
|
|
15
|
-
'feDiffuseLighting', 'feDisplacementMap', 'feFlood', 'feGaussianBlur', 'feImage',
|
|
16
|
-
'feMerge', 'feMergeNode', 'feMorphology', 'feOffset', 'feSpecularLighting',
|
|
17
|
-
'feTile', 'feTurbulence',
|
|
18
|
-
]);
|
|
6
|
+
import { _symbolChild, effect, untrack, createRoot, getOwner, onCleanup, _currentComponent, _serverComponentOwner, _enterFrame, _exitFrame, _isDisposed, _tracking, runWithOwner, _runOwned, __DEV__ } from './reactive.js';
|
|
7
|
+
import { _renderComponent, _region, _signalKeyed, _listFragment, _valuesToNodes, _toFunctionComponent, _adoptDetached, _isDomNode, _afterWalk, _installOptionsChanged, applyRef, setClassList, setStyle, _writeClass } from './render.js';
|
|
8
|
+
import { REFUSED_ATTRS, URL_ATTRS, ATTRIBUTE_VALUE_TYPES, isAriaAttr, isEnumeratedAttr, isUnsafeUrl, selectedOptions } from './markup-rules.js';
|
|
9
|
+
import { isVNode, SVG_TAG } from './vnode-renderer.js';
|
|
19
10
|
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
20
11
|
|
|
21
12
|
// --- Attribute sanitization (shared by both setProp implementations) ---
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
// executes it, and ping/object[data] are fetched by the browser.
|
|
25
|
-
const URL_ATTRS = new Set([
|
|
26
|
-
'href', 'src', 'action', 'formaction', 'formAction',
|
|
27
|
-
'data', 'ping', 'xlink:href', 'xlinkHref',
|
|
28
|
-
]);
|
|
29
|
-
|
|
30
|
-
// srcdoc is entity-decoded and parsed as a document by the browser, so HTML
|
|
31
|
-
// escaping is not a defense. Refuse it outright rather than trying to clean it.
|
|
32
|
-
const REFUSED_ATTRS = new Set(['srcdoc', 'srcDoc']);
|
|
33
|
-
|
|
34
|
-
function isSafeUrl(url) {
|
|
35
|
-
if (url == null) return true;
|
|
36
|
-
// A boxed String or an object with toString() still stringifies into a live
|
|
37
|
-
// href, so coerce before the protocol check rather than trusting the type.
|
|
38
|
-
// A value with no usable toString (Object.create(null), { toString: null },
|
|
39
|
-
// both reachable from JSON) throws here, so refuse it rather than letting the
|
|
40
|
-
// TypeError abort the render.
|
|
41
|
-
let normalized;
|
|
42
|
-
try {
|
|
43
|
-
normalized = String(url).trim().replace(/[\s\x00-\x1f]/g, '').toLowerCase();
|
|
44
|
-
} catch {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
if (normalized.startsWith('javascript:')) return false;
|
|
48
|
-
if (normalized.startsWith('data:')) return false;
|
|
49
|
-
if (normalized.startsWith('vbscript:')) return false;
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
13
|
+
// The refused props, the URL attributes and the scheme check are the markup
|
|
14
|
+
// rules what-server and what-compiler apply too (markup-rules.js).
|
|
52
15
|
|
|
53
16
|
// Event-handler prop test, case-insensitive. A case-sensitive `on` prefix lets
|
|
54
17
|
// `ONCLICK` fall through to setAttribute, where the browser honours it as a live
|
|
@@ -65,9 +28,7 @@ export function _isEventProp(key) {
|
|
|
65
28
|
// they enforce identical rules.
|
|
66
29
|
export function _isUnsafeAttr(key, value) {
|
|
67
30
|
const lower = key.toLowerCase();
|
|
68
|
-
|
|
69
|
-
if (!URL_ATTRS.has(key) && !URL_ATTRS.has(lower)) return false;
|
|
70
|
-
return !isSafeUrl(value);
|
|
31
|
+
return REFUSED_ATTRS.has(lower) || (URL_ATTRS.has(lower) && isUnsafeUrl(value));
|
|
71
32
|
}
|
|
72
33
|
|
|
73
34
|
// ARIA attributes and `role` take ENUMERATED string values, never HTML boolean
|
|
@@ -84,119 +45,8 @@ export function _isUnsafeAttr(key, value) {
|
|
|
84
45
|
// while `aria-*={false}` was wrong everywhere. Every widget built on the a11y
|
|
85
46
|
// module is affected, since useAriaExpanded/useAriaSelected/useAriaChecked all
|
|
86
47
|
// return booleans.
|
|
87
|
-
export
|
|
88
|
-
return key === 'role' || key.startsWith('aria-');
|
|
89
|
-
}
|
|
48
|
+
export { isAriaAttr as _isAriaAttr };
|
|
90
49
|
|
|
91
|
-
// Track all mounted component contexts for disposal
|
|
92
|
-
const mountedComponents = new Set();
|
|
93
|
-
|
|
94
|
-
// WeakMap: comment node → component context (for comment-node boundaries)
|
|
95
|
-
const _commentCtxMap = new WeakMap();
|
|
96
|
-
|
|
97
|
-
function isDomNode(value) {
|
|
98
|
-
if (!value || typeof value !== 'object') return false;
|
|
99
|
-
if (typeof Node !== 'undefined' && value instanceof Node) return true;
|
|
100
|
-
return typeof value.nodeType === 'number' && typeof value.nodeName === 'string';
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function isVNode(value) {
|
|
104
|
-
return !!value && typeof value === 'object' && (value._vnode === true || 'tag' in value);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Dispose a component: run effect cleanups, hook cleanups, onCleanup callbacks
|
|
108
|
-
function disposeComponent(ctx) {
|
|
109
|
-
if (ctx.disposed) return;
|
|
110
|
-
ctx.disposed = true;
|
|
111
|
-
|
|
112
|
-
// Run cleanup callbacks
|
|
113
|
-
if (ctx.cleanups) {
|
|
114
|
-
for (const cleanup of ctx.cleanups) {
|
|
115
|
-
try { cleanup(); } catch (e) { console.error('[what] cleanup error:', e); }
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Run effect disposals
|
|
120
|
-
if (ctx.effects) {
|
|
121
|
-
for (const dispose of ctx.effects) {
|
|
122
|
-
try { dispose(); } catch { /* already disposed */ }
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Run hook cleanups (useEffect return values)
|
|
127
|
-
if (ctx.hooks) {
|
|
128
|
-
for (const hook of ctx.hooks) {
|
|
129
|
-
if (hook && typeof hook.cleanup === 'function') {
|
|
130
|
-
try { hook.cleanup(); } catch (e) { console.error('[what] hook cleanup error:', e); }
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Run onCleanup callbacks
|
|
136
|
-
if (ctx._cleanupCallbacks) {
|
|
137
|
-
for (const fn of ctx._cleanupCallbacks) {
|
|
138
|
-
try { fn(); } catch (e) { console.error('[what] onCleanup error:', e); }
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (__DEV__ && __devtools?.onComponentUnmount) __devtools.onComponentUnmount?.(ctx);
|
|
143
|
-
mountedComponents.delete(ctx);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Hydration has no wrapper fragment and no comment markers to hang a component
|
|
147
|
-
// ctx or a reactive-child effect on, so it anchors them to the DOM node they
|
|
148
|
-
// produced. Without an anchor disposeTree cannot reach them and every hydrated
|
|
149
|
-
// component leaks its cleanups and effects.
|
|
150
|
-
export function addHydrationDisposer(node, fn) {
|
|
151
|
-
if (!node || typeof fn !== 'function') return;
|
|
152
|
-
if (node._hydrationDisposers) node._hydrationDisposers.push(fn);
|
|
153
|
-
else node._hydrationDisposers = [fn];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function addHydratedComponent(node, ctx) {
|
|
157
|
-
addHydrationDisposer(node, () => disposeComponent(ctx));
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Dispose all components and reactive effects attached to a DOM subtree.
|
|
161
|
-
// Performance: checks _componentCtx / _dispose / _propEffects before walking
|
|
162
|
-
// children, and only checks _commentCtxMap for comment nodes (nodeType 8).
|
|
163
|
-
export function disposeTree(node) {
|
|
164
|
-
if (!node) return;
|
|
165
|
-
if (node._componentCtx) {
|
|
166
|
-
disposeComponent(node._componentCtx);
|
|
167
|
-
}
|
|
168
|
-
if (node._hydrationDisposers) {
|
|
169
|
-
const disposers = node._hydrationDisposers;
|
|
170
|
-
node._hydrationDisposers = null;
|
|
171
|
-
for (let i = 0; i < disposers.length; i++) {
|
|
172
|
-
try { disposers[i](); } catch { /* already disposed */ }
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
// Check comment node WeakMap for component context — only for comment nodes
|
|
176
|
-
if (node.nodeType === 8) {
|
|
177
|
-
const commentCtx = _commentCtxMap.get(node);
|
|
178
|
-
if (commentCtx) {
|
|
179
|
-
disposeComponent(commentCtx);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
// Dispose reactive function child effects ({() => ...} wrappers)
|
|
183
|
-
if (node._dispose) {
|
|
184
|
-
try { node._dispose(); } catch { /* already disposed */ }
|
|
185
|
-
}
|
|
186
|
-
// Dispose reactive prop effects (value: () => ..., class: () => ..., etc.)
|
|
187
|
-
if (node._propEffects) {
|
|
188
|
-
for (const key in node._propEffects) {
|
|
189
|
-
try { node._propEffects[key](); } catch { /* already disposed */ }
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
// Recursively dispose children
|
|
193
|
-
const children = node.childNodes;
|
|
194
|
-
if (children && children.length > 0) {
|
|
195
|
-
for (let i = 0; i < children.length; i++) {
|
|
196
|
-
disposeTree(children[i]);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
50
|
|
|
201
51
|
// --- _liveRegionNodes(tracked) ---
|
|
202
52
|
//
|
|
@@ -261,26 +111,70 @@ export function _liveRegionNodes(tracked) {
|
|
|
261
111
|
return out || tracked;
|
|
262
112
|
}
|
|
263
113
|
|
|
264
|
-
// Mount a
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
114
|
+
// Mount a tree into a DOM container: compiled DOM, a vnode from h() or the JSX
|
|
115
|
+
// runtime, a thunk, or an array of them. Returns the unmount function.
|
|
116
|
+
//
|
|
117
|
+
// Realized through render.js, which hands vnodes to the renderer h.js installs,
|
|
118
|
+
// so an app that only mounts compiled JSX does not pull createDOM in through
|
|
119
|
+
// this function. The tree is built in a root that owns every component, effect
|
|
120
|
+
// and binding it creates, so unmounting is disposing that root. A container
|
|
121
|
+
// that already holds a mounted (or hydrated) root has that root disposed
|
|
122
|
+
// first.
|
|
123
|
+
export function mount(node, container) {
|
|
124
|
+
container = _mountTarget(container, 'mount');
|
|
125
|
+
return createRoot((dispose) => {
|
|
126
|
+
const unmount = _trackRoot(container, dispose);
|
|
275
127
|
container.textContent = '';
|
|
128
|
+
const nodes = _valuesToNodes(node, container, [], false);
|
|
129
|
+
_adoptDetached(nodes);
|
|
130
|
+
for (let i = 0; i < nodes.length; i++) container.appendChild(nodes[i]);
|
|
131
|
+
return unmount;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// The element mount() or hydrate() (`fn`) renders into: `container`, or the
|
|
136
|
+
// element its selector matches. None is a coded error, not a failure deeper
|
|
137
|
+
// in.
|
|
138
|
+
export function _mountTarget(container, fn) {
|
|
139
|
+
const el = typeof container === 'string' ? document.querySelector(container) : container;
|
|
140
|
+
if (el == null || typeof el.nodeType !== 'number') {
|
|
141
|
+
throw Object.assign(new Error(__DEV__
|
|
142
|
+
? _thrownMessage('ERR_MOUNT_TARGET_MISSING', `${fn}() got no container element: ${typeof container === 'string' ? `no element matches "${container}"` : `it was ${String(container)}`}`)
|
|
143
|
+
: `[what] ${fn}() got no container element`), { code: 'ERR_MOUNT_TARGET_MISSING' });
|
|
144
|
+
}
|
|
145
|
+
return el;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// The root mounted or hydrated into each container.
|
|
149
|
+
const mountedRoots = /* @__PURE__ */ new WeakMap();
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Record `dispose` as the root of `container`, disposing the root it held
|
|
153
|
+
* before. Returns the unmount function: it disposes the root and, while that
|
|
154
|
+
* root is still the container's, empties the container.
|
|
155
|
+
*/
|
|
156
|
+
export function _trackRoot(container, dispose) {
|
|
157
|
+
const previous = mountedRoots.get(container);
|
|
158
|
+
const unmount = () => {
|
|
159
|
+
if (mountedRoots.get(container) === unmount) {
|
|
160
|
+
mountedRoots.delete(container);
|
|
161
|
+
dispose();
|
|
162
|
+
container.textContent = '';
|
|
163
|
+
} else {
|
|
164
|
+
dispose();
|
|
165
|
+
}
|
|
276
166
|
};
|
|
167
|
+
mountedRoots.set(container, unmount);
|
|
168
|
+
if (previous !== undefined) previous();
|
|
169
|
+
return unmount;
|
|
277
170
|
}
|
|
278
171
|
|
|
279
172
|
// --- Create DOM from VNode ---
|
|
280
173
|
|
|
281
174
|
export function createDOM(vnode, parent, isSvg) {
|
|
282
175
|
// Null/false/true → placeholder comment (preserves child indices for reconciliation)
|
|
283
|
-
if (vnode == null || vnode ===
|
|
176
|
+
if (vnode == null || typeof vnode === 'boolean' || typeof vnode === 'symbol') {
|
|
177
|
+
if (__DEV__ && typeof vnode === 'symbol') _symbolChild(vnode, 'client');
|
|
284
178
|
return document.createComment('');
|
|
285
179
|
}
|
|
286
180
|
|
|
@@ -290,136 +184,17 @@ export function createDOM(vnode, parent, isSvg) {
|
|
|
290
184
|
}
|
|
291
185
|
|
|
292
186
|
// DOM node passthrough (fine-grained components return real nodes)
|
|
293
|
-
if (
|
|
187
|
+
if (_isDomNode(vnode)) {
|
|
294
188
|
return vnode;
|
|
295
189
|
}
|
|
296
190
|
|
|
297
|
-
//
|
|
298
|
-
//
|
|
299
|
-
// a
|
|
300
|
-
//
|
|
301
|
-
// lowers to a bare `_$mapArray(...)`. Without this special-case the generic
|
|
302
|
-
// function branch below would call vnode() with no parent and throw.
|
|
303
|
-
if (typeof vnode === 'function' && vnode._mapArray) {
|
|
304
|
-
const frag = document.createDocumentFragment();
|
|
305
|
-
// Open the list with a start marker, the same bracket shape the reactive
|
|
306
|
-
// region below uses, and for the same reason: whoever embeds this fragment
|
|
307
|
-
// has to be able to find the list's content LATER, not just now.
|
|
308
|
-
//
|
|
309
|
-
// A list reached through here is embedded in some other region's value —
|
|
310
|
-
// `{cond && <>{items.map(...)}<p/></>}` and every variation of it. That
|
|
311
|
-
// region records the nodes it inserted and reuses the record as its removal
|
|
312
|
-
// set on the next run. The record is a snapshot, and the list is not
|
|
313
|
-
// snapshot-shaped: it owns an effect and goes on inserting and removing rows
|
|
314
|
-
// for as long as it is mounted. Rows appended after mount were absent from
|
|
315
|
-
// the record, so switching the region off removed the mount-time rows and
|
|
316
|
-
// orphaned the rest — and switching it back on rendered a second, complete
|
|
317
|
-
// list beside the orphans.
|
|
318
|
-
//
|
|
319
|
-
// Two markers describe a moving target that a list of nodes cannot:
|
|
320
|
-
// everything the list ever inserts lands between them, so reconcileInsert
|
|
321
|
-
// (render.js) can sweep the live range at teardown instead of trusting the
|
|
322
|
-
// snapshot. `_rangeEnd` is the pairing it reads.
|
|
323
|
-
const startMarker = document.createComment('list');
|
|
324
|
-
frag.appendChild(startMarker);
|
|
325
|
-
// Passing a null marker makes the inserter APPEND its own `/list` end marker
|
|
326
|
-
// to `frag` (insertBefore(node, null) is an append) and insert every row
|
|
327
|
-
// before it, so the list closes the range itself and no second bookend is
|
|
328
|
-
// needed. Once `frag` is appended to the real DOM the markers and the rows
|
|
329
|
-
// between them carry over together.
|
|
330
|
-
/** @type {any} */ (startMarker)._rangeEnd = vnode(frag, null);
|
|
331
|
-
return frag;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// Deferred component children (compiled JSX). The value is static (the
|
|
335
|
-
// factory only exists so the owning component runs before its children are
|
|
336
|
-
// built), so realize it in place rather than through the reactive path below.
|
|
337
|
-
if (typeof vnode === 'function' && vnode._lazyChildren) {
|
|
338
|
-
return createDOM(vnode(), parent, isSvg);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
// Reactive function child — use comment markers (no wrapper element)
|
|
342
|
-
// to avoid polluting the DOM and breaking CSS selectors like :first-child.
|
|
191
|
+
// A mapArray inserter is called as (parent, marker), never as a thunk; a
|
|
192
|
+
// deferred children factory is static and realized in place; any other
|
|
193
|
+
// function is a reactive region between comment markers (no wrapper element,
|
|
194
|
+
// so CSS selectors like :first-child keep working).
|
|
343
195
|
if (typeof vnode === 'function') {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
let currentNodes = [];
|
|
347
|
-
// We need a parent to insert between markers. The caller (createElementFromVNode
|
|
348
|
-
// or createComponent) will appendChild both markers and the content. We return
|
|
349
|
-
// a document fragment containing start marker, then the effect will manage nodes
|
|
350
|
-
// between start and end markers once they're in the real DOM.
|
|
351
|
-
const frag = document.createDocumentFragment();
|
|
352
|
-
frag.appendChild(startMarker);
|
|
353
|
-
frag.appendChild(endMarker);
|
|
354
|
-
|
|
355
|
-
// Capture the owning component at CREATION time.
|
|
356
|
-
//
|
|
357
|
-
// This effect re-runs long after the synchronous render that created it,
|
|
358
|
-
// when the component stack is empty. Everything it builds on a re-run
|
|
359
|
-
// therefore got `parentCtx = null`, severing the owner chain, and the two
|
|
360
|
-
// things that walk that chain both went blind:
|
|
361
|
-
// - suspend() found no Suspense boundary, so a lazy() component reached by
|
|
362
|
-
// a signal update (any client-side navigation) threw its pending promise
|
|
363
|
-
// as an uncaught error and left the region permanently empty.
|
|
364
|
-
// - the ErrorBoundary lookup found nothing, so a throw from a component
|
|
365
|
-
// rendered after any state change escaped the boundary wrapping it.
|
|
366
|
-
// Both worked on first paint and only failed once the app was interactive.
|
|
367
|
-
const owner = componentStack[componentStack.length - 1] || null;
|
|
368
|
-
|
|
369
|
-
const dispose = effect(() => {
|
|
370
|
-
// Already on top during the initial synchronous run; only re-push when the
|
|
371
|
-
// stack has since unwound.
|
|
372
|
-
const restoreOwner = owner !== null && componentStack[componentStack.length - 1] !== owner;
|
|
373
|
-
if (restoreOwner) componentStack.push(owner);
|
|
374
|
-
try {
|
|
375
|
-
const val = vnode();
|
|
376
|
-
const vnodes = (val == null || val === false || val === true)
|
|
377
|
-
? []
|
|
378
|
-
: Array.isArray(val) ? val : [val];
|
|
379
|
-
|
|
380
|
-
const realParent = endMarker.parentNode;
|
|
381
|
-
if (!realParent) return; // not mounted yet — first run handled below
|
|
382
|
-
|
|
383
|
-
// Remove old nodes between markers. The removal set is the LIVE one: a
|
|
384
|
-
// list or a nested region inside this one has been replacing its own
|
|
385
|
-
// nodes since they were recorded, and what it produced late is just as
|
|
386
|
-
// much this region's to remove as what it produced at mount.
|
|
387
|
-
for (const old of _liveRegionNodes(currentNodes)) {
|
|
388
|
-
disposeTree(old);
|
|
389
|
-
if (old.parentNode === realParent) realParent.removeChild(old);
|
|
390
|
-
}
|
|
391
|
-
currentNodes = [];
|
|
392
|
-
|
|
393
|
-
// Add new nodes before endMarker
|
|
394
|
-
for (const v of vnodes) {
|
|
395
|
-
const node = createDOM(v, realParent, parent?._isSvg);
|
|
396
|
-
if (node) {
|
|
397
|
-
// If createDOM returned a DocumentFragment, track individual children
|
|
398
|
-
// since fragment nodes get absorbed into the DOM on insertion.
|
|
399
|
-
if (node.nodeType === 11 /* DOCUMENT_FRAGMENT_NODE */) {
|
|
400
|
-
const children = Array.from(node.childNodes);
|
|
401
|
-
realParent.insertBefore(node, endMarker);
|
|
402
|
-
for (const child of children) currentNodes.push(child);
|
|
403
|
-
} else {
|
|
404
|
-
realParent.insertBefore(node, endMarker);
|
|
405
|
-
currentNodes.push(node);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
} finally {
|
|
410
|
-
if (restoreOwner) componentStack.pop();
|
|
411
|
-
}
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
startMarker._dispose = dispose;
|
|
415
|
-
// Also store dispose on endMarker so disposeTree can find it from either marker
|
|
416
|
-
endMarker._dispose = dispose;
|
|
417
|
-
// This region manages itself, so an OUTER region that embeds it cannot
|
|
418
|
-
// describe it with a list of nodes: everything between the markers is
|
|
419
|
-
// replaced whenever this effect re-runs. Pairing them lets the outer
|
|
420
|
-
// teardown sweep the live range. See _liveRegionNodes.
|
|
421
|
-
/** @type {any} */ (startMarker)._rangeEnd = endMarker;
|
|
422
|
-
return frag;
|
|
196
|
+
if (vnode._lazyChildren) return createDOM(vnode(), parent, isSvg);
|
|
197
|
+
return vnode._mapArray ? _listFragment(vnode) : _region(vnode, isSvg);
|
|
423
198
|
}
|
|
424
199
|
|
|
425
200
|
// Array of vnodes
|
|
@@ -432,507 +207,450 @@ export function createDOM(vnode, parent, isSvg) {
|
|
|
432
207
|
return frag;
|
|
433
208
|
}
|
|
434
209
|
|
|
210
|
+
// Anything that is not a vnode (by its brand, vnode-renderer.js) is text,
|
|
211
|
+
// whatever properties it carries.
|
|
212
|
+
if (!isVNode(vnode)) return document.createTextNode(String(vnode));
|
|
213
|
+
|
|
214
|
+
// A key that is a signal builds the vnode again, in place, whenever its
|
|
215
|
+
// value changes (render.js _signalKeyed).
|
|
216
|
+
if (typeof vnode.key === 'function') return _region(_signalKeyed(vnode), isSvg);
|
|
217
|
+
|
|
435
218
|
// VNode with component tag — component runs ONCE
|
|
436
|
-
if (
|
|
219
|
+
if (typeof vnode.tag === 'function') {
|
|
437
220
|
return createComponent(vnode, parent, isSvg);
|
|
438
221
|
}
|
|
439
222
|
|
|
440
|
-
// VNode with special boundary
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
// Unknown — convert to text
|
|
449
|
-
return document.createTextNode(String(vnode));
|
|
223
|
+
// VNode with special tags. A boundary's or a portal's vnode carries its
|
|
224
|
+
// client renderer (components.js, createPortalDOM below), so a bundle that
|
|
225
|
+
// never uses one does not carry it.
|
|
226
|
+
if (vnode.tag === '__errorBoundary' || vnode.tag === '__portal') return vnode.props.mount(vnode, parent);
|
|
227
|
+
if (vnode.tag === '__suspense') return vnode.props.boundary.mount(vnode, parent);
|
|
228
|
+
return createElementFromVNode(vnode, parent, isSvg);
|
|
450
229
|
}
|
|
451
230
|
|
|
452
231
|
// --- Component Rendering ---
|
|
453
|
-
// Components run ONCE.
|
|
454
|
-
|
|
455
|
-
//
|
|
456
|
-
//
|
|
457
|
-
// invariants are satisfied. The propsSignal is stored as target._sig.
|
|
458
|
-
const _propsProxyHandler = {
|
|
459
|
-
get(target, key) {
|
|
460
|
-
if (key === '_sig') return undefined; // hide internal property
|
|
461
|
-
if (key === '__proto__' || key === 'constructor' || key === 'prototype') return undefined;
|
|
462
|
-
return target._sig()[key];
|
|
463
|
-
},
|
|
464
|
-
has(target, key) {
|
|
465
|
-
if (key === '_sig') return false;
|
|
466
|
-
return key in target._sig();
|
|
467
|
-
},
|
|
468
|
-
ownKeys(target) {
|
|
469
|
-
return Reflect.ownKeys(target._sig());
|
|
470
|
-
},
|
|
471
|
-
getOwnPropertyDescriptor(target, key) {
|
|
472
|
-
if (key === '_sig') return undefined;
|
|
473
|
-
const current = target._sig();
|
|
474
|
-
if (key in current) {
|
|
475
|
-
return { value: current[key], writable: false, enumerable: true, configurable: true };
|
|
476
|
-
}
|
|
477
|
-
return undefined;
|
|
478
|
-
},
|
|
479
|
-
set(_target, _key) {
|
|
480
|
-
// Props are read-only from the component's perspective.
|
|
481
|
-
// Reject all writes — especially dangerous prototype-chain keys.
|
|
482
|
-
return false;
|
|
483
|
-
},
|
|
484
|
-
};
|
|
485
|
-
|
|
486
|
-
const componentStack = [];
|
|
232
|
+
// Components run ONCE: render.js's _renderComponent holds the lifecycle that
|
|
233
|
+
// compiled JSX and vnodes share. A component is an owner (reactive.js
|
|
234
|
+
// _componentOwner); the component whose hooks run now is the nearest one above
|
|
235
|
+
// the current owner.
|
|
487
236
|
|
|
488
237
|
export function getCurrentComponent() {
|
|
489
|
-
return
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
// Inject into components.js and helpers.js to avoid circular imports
|
|
493
|
-
_injectGetCurrentComponent(getCurrentComponent);
|
|
494
|
-
_setComponentRef(getCurrentComponent);
|
|
495
|
-
|
|
496
|
-
export function getComponentStack() {
|
|
497
|
-
return componentStack;
|
|
238
|
+
return _currentComponent();
|
|
498
239
|
}
|
|
499
240
|
|
|
500
241
|
/**
|
|
501
|
-
* Run a component during SSR under a real component
|
|
242
|
+
* Run a component during SSR under a real component owner.
|
|
502
243
|
*
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
*
|
|
244
|
+
* Every hook that needs a component (useState, useSignal, useComputed,
|
|
245
|
+
* useMemo, useCallback, useRef, useReducer, onCleanup and Context.Provider)
|
|
246
|
+
* resolves it through the current owner, so the server renderers enter a
|
|
247
|
+
* frame for each component, under `parent` (the frame it renders in) or else
|
|
248
|
+
* the current owner, which during a server render is the render's own root
|
|
249
|
+
* (what-server's createRenderContext, entered by runWithServerContext): every
|
|
250
|
+
* frame, and everything its component creates, is disposed when the render is
|
|
251
|
+
* released. `_parentCtx` is the frame it renders in, for owner paths.
|
|
510
252
|
*
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
* has to stay on the stack while its children render. Hence the callback.
|
|
253
|
+
* Nothing here ever mounts, so nothing deferred may run: useEffect and onMount
|
|
254
|
+
* do nothing during a server render.
|
|
514
255
|
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
*
|
|
519
|
-
* is what makes an SSR render leave no live effects behind.
|
|
256
|
+
* A frame is current only for a synchronous call. renderToStream interleaves
|
|
257
|
+
* with other requests at every await; it passes `parent` explicitly and
|
|
258
|
+
* re-enters a frame with _runInComponentSSR whenever later work in that
|
|
259
|
+
* frame's subtree needs it.
|
|
520
260
|
*
|
|
521
|
-
*
|
|
522
|
-
* sites is a generator: renderToStream has to hold the frame open across yields
|
|
523
|
-
* until the subtree has finished streaming.
|
|
524
|
-
*
|
|
525
|
-
* Always pair these in a try/finally.
|
|
261
|
+
* Always pair begin and end in a try/finally.
|
|
526
262
|
*/
|
|
527
|
-
export function _beginComponentSSR(Component) {
|
|
528
|
-
const
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
effects: [],
|
|
533
|
-
cleanups: [],
|
|
534
|
-
mounted: false,
|
|
535
|
-
disposed: false,
|
|
536
|
-
Component,
|
|
537
|
-
_parentCtx: componentStack[componentStack.length - 1] || null,
|
|
538
|
-
_errorBoundary: null,
|
|
539
|
-
};
|
|
540
|
-
componentStack.push(ctx);
|
|
541
|
-
return ctx;
|
|
263
|
+
export function _beginComponentSSR(Component, parent) {
|
|
264
|
+
const frame = _serverComponentOwner(Component, parent);
|
|
265
|
+
frame._parentCtx = parent !== undefined ? parent : parentFrame(frame);
|
|
266
|
+
_enterFrame(frame);
|
|
267
|
+
return frame;
|
|
542
268
|
}
|
|
543
269
|
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
270
|
+
function parentFrame(frame) {
|
|
271
|
+
for (let o = frame.owner; o != null; o = o.owner) {
|
|
272
|
+
if (o.Component !== undefined) return o;
|
|
273
|
+
}
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Restores the owner that was current before the frame began.
|
|
278
|
+
export function _endComponentSSR(frame) {
|
|
279
|
+
_exitFrame(frame);
|
|
551
280
|
}
|
|
552
281
|
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
282
|
+
/** Run `fn` with an existing SSR frame as the current owner. */
|
|
283
|
+
export function _runInComponentSSR(frame, fn) {
|
|
284
|
+
return runWithOwner(frame, fn);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// --- _installLazyChildren(Component, target, lazyChildren, slots) ---
|
|
288
|
+
// JSX that compiled code hands a component, as its children or through any
|
|
289
|
+
// other prop (`slots`, a key -> factory map: render.js _$slots), arrives as a
|
|
290
|
+
// zero-arg factory instead of built DOM. This defines a getter over each
|
|
291
|
+
// factory on target and returns the function that ends the component's run
|
|
292
|
+
// for them, called with the component's owner (or null when there is none).
|
|
293
|
+
//
|
|
294
|
+
// Such a value is built where it is placed, once per placement, as an h()
|
|
295
|
+
// vnode is, and building it reads no signal into the scope around the place
|
|
296
|
+
// (a <Show>, a condition, an effect): the reactive reads of its JSX track
|
|
297
|
+
// inside it.
|
|
298
|
+
// - A read where the value is placed (compiled code reads it with _place)
|
|
299
|
+
// builds it there, under the owner of that place, so the same children
|
|
300
|
+
// placed twice are two copies, as h() and the server render them, and
|
|
301
|
+
// children a condition shows again are built again.
|
|
302
|
+
// - Any other read during the run (an inspection: `children.length`, a test,
|
|
303
|
+
// an alias) builds it once, and the first placement after it takes that
|
|
304
|
+
// build: a component that inspects its children and then renders them
|
|
305
|
+
// renders the nodes it inspected.
|
|
306
|
+
// - Once its JSX is placed, and after the run (a test in onMount or
|
|
307
|
+
// useEffect, a read in an event handler, a hand-written thunk), a read that
|
|
308
|
+
// does not place it gets a placeable: the same truthy value at every read,
|
|
309
|
+
// built where it is placed, as a vnode is. Its first placement takes a
|
|
310
|
+
// build still waiting for one; every later placement builds afresh (built
|
|
311
|
+
// DOM is single-use: a fragment is drained by its first insertion). A test
|
|
312
|
+
// in onMount is true without running the components again, and a handler
|
|
313
|
+
// that shows it in a dialog shows a copy owned by that place. A prop the
|
|
314
|
+
// run never read is built at the first such read, once, under the
|
|
315
|
+
// component's owner (its context, its error boundary), to know whether it
|
|
316
|
+
// is JSX.
|
|
317
|
+
// - A build with no owner current (JSX built in an event handler places the
|
|
318
|
+
// value) is the component's.
|
|
319
|
+
// A value that is not JSX (a prop handed on: data, a function) is given as
|
|
320
|
+
// it is.
|
|
557
321
|
//
|
|
558
|
-
//
|
|
559
|
-
//
|
|
560
|
-
//
|
|
561
|
-
//
|
|
562
|
-
// because realized children are single-use DOM: a DocumentFragment is drained
|
|
563
|
-
// by its first insertion and removed nodes have had their effects disposed, so
|
|
564
|
-
// a component that re-reads props.children from a reactive thunk must get a
|
|
565
|
-
// freshly built subtree rather than the corpse of the previous one.
|
|
322
|
+
// While hydrating, a placement builds where the walk stands, so its JSX
|
|
323
|
+
// claims the server's nodes for it. Any other read builds off the walk
|
|
324
|
+
// (outsideRead, render.js _$outside): the value is placed later, where it
|
|
325
|
+
// takes the place of the server's copy and is reported.
|
|
566
326
|
//
|
|
567
327
|
// A component that establishes a scope its children depend on (a context
|
|
568
|
-
// provider, an error or suspense boundary) cannot use the getter
|
|
569
|
-
// scope only exists after the component returns, and reading
|
|
570
|
-
// anywhere, including destructuring it in the parameter list,
|
|
571
|
-
// subtree first. Those set _deferChildren and receive the
|
|
572
|
-
// the render paths realize once the scope
|
|
573
|
-
// re-invokes to rebuild its subtree on a later
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
328
|
+
// provider, an error or suspense boundary) cannot use the getter for its
|
|
329
|
+
// children: the scope only exists after the component returns, and reading
|
|
330
|
+
// props.children anywhere, including destructuring it in the parameter list,
|
|
331
|
+
// would build the subtree first. Those set _deferChildren and receive the
|
|
332
|
+
// children factory itself, which the render paths realize once the scope
|
|
333
|
+
// exists and which a boundary re-invokes to rebuild its subtree on a later
|
|
334
|
+
// attempt. Their other props are getters like anyone's.
|
|
335
|
+
let placing = false;
|
|
336
|
+
|
|
337
|
+
/** @type {((build: () => any) => any) | null} */
|
|
338
|
+
let outsideRead = null;
|
|
339
|
+
|
|
340
|
+
/** Hydration's off-walk build, for reads that do not place what they read (render.js). */
|
|
341
|
+
export function _setOutsideRead(fn) {
|
|
342
|
+
outsideRead = fn;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Whether what is built now is placed where the server wrote its members in
|
|
346
|
+
// place, with no array markers of its own around them: compiled children in
|
|
347
|
+
// an element's hole or among a fragment's or an array's members, a boundary's
|
|
348
|
+
// or an h() element's children. A component's several children are an array
|
|
349
|
+
// the server brackets only where it is the whole value of a component or of
|
|
350
|
+
// a reactive child; their _$members (render.js) asks.
|
|
351
|
+
let flat = false;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Compiled code's read of `props[key]` where the value is placed. `inPlace`
|
|
355
|
+
* is 1 where the server writes the value with no markers of its own around it.
|
|
356
|
+
*/
|
|
357
|
+
export function _place(props, key, inPlace, fallback) {
|
|
358
|
+
// A live prop (`label={name()}` compiles to a getter, see _isLiveProp) is
|
|
359
|
+
// placed as a reactive child: the place reads the getter again whenever
|
|
360
|
+
// what it read changes. `fallback` is a destructured prop's default
|
|
361
|
+
// (`({ label = 'x' })`), taken where the value is undefined.
|
|
362
|
+
const fallsBack = arguments.length > 3;
|
|
363
|
+
if (_isLiveProp(props, key)) {
|
|
364
|
+
/** @type {(() => any) & { _forward?: boolean }} */
|
|
365
|
+
const read = fallsBack ? () => { const v = props[key]; return v === undefined ? fallback : v; } : () => props[key];
|
|
366
|
+
// A prop handed on to a component (`<Leaf counts={props.data} />`) is a
|
|
367
|
+
// lazy slot whose factory is this call: readLazy recognises the read and
|
|
368
|
+
// passes the live prop through, the value to a read and this to a place.
|
|
369
|
+
read._forward = true;
|
|
370
|
+
return read;
|
|
371
|
+
}
|
|
372
|
+
placing = true;
|
|
373
|
+
const outer = flat;
|
|
374
|
+
flat = inPlace === 1;
|
|
375
|
+
try {
|
|
376
|
+
const v = props[key];
|
|
377
|
+
return fallsBack && v === undefined ? fallback : v;
|
|
378
|
+
} finally {
|
|
379
|
+
placing = false;
|
|
380
|
+
flat = outer;
|
|
578
381
|
}
|
|
579
|
-
let realized;
|
|
580
|
-
let cached = false;
|
|
581
|
-
let inPass = true;
|
|
582
|
-
Object.defineProperty(target, 'children', {
|
|
583
|
-
get() {
|
|
584
|
-
if (!inPass) return lazyChildren();
|
|
585
|
-
if (!cached) {
|
|
586
|
-
cached = true;
|
|
587
|
-
realized = lazyChildren();
|
|
588
|
-
}
|
|
589
|
-
return realized;
|
|
590
|
-
},
|
|
591
|
-
enumerable: true,
|
|
592
|
-
configurable: true,
|
|
593
|
-
});
|
|
594
|
-
return () => { inPass = false; cached = false; realized = undefined; };
|
|
595
382
|
}
|
|
596
383
|
|
|
597
|
-
// ---
|
|
598
|
-
//
|
|
599
|
-
//
|
|
600
|
-
//
|
|
601
|
-
//
|
|
602
|
-
//
|
|
384
|
+
// --- Live props ---
|
|
385
|
+
// Compiled JSX passes a component a prop whose value reads a signal
|
|
386
|
+
// (`<Badge on={count() > 0} />`) as a getter over the expression, in an
|
|
387
|
+
// object it marks as What's (`_$marked({ get on() { … } })`), so the
|
|
388
|
+
// component sees the current value wherever it reads `props.on`: a JSX
|
|
389
|
+
// binding that reads it re-runs when `count` changes, as it would for
|
|
390
|
+
// `count()` written there. A getter on a props object What marked is kept by
|
|
391
|
+
// every copy and merge (render.js ownProps, _$merge, a rest element _$omit);
|
|
392
|
+
// a getter on any other object (a user's object spread into props) is read
|
|
393
|
+
// once, as Object.assign reads it. The lazy getters _installLazyChildren
|
|
394
|
+
// defines for children and slots are kept the same way, and are not live
|
|
395
|
+
// props: a read of one builds JSX (they carry _lazy).
|
|
396
|
+
//
|
|
397
|
+
// The props a component sees, a rest element and a merge are plain objects,
|
|
398
|
+
// as React's props are (`props.hasOwnProperty(k)`, `String(props)`,
|
|
399
|
+
// classnames and lodash's isPlainObject take them). One that holds a getter
|
|
400
|
+
// of either kind is marked as What's by a property no listing shows (PROPS:
|
|
401
|
+
// not enumerable, a symbol; Symbol.for, so another copy of what-core on the
|
|
402
|
+
// page takes them as its own too): compiled JSX's literal with a live prop, a
|
|
403
|
+
// copy or merge that kept a getter, and an object _installLazyChildren gave
|
|
404
|
+
// one. One with none is not marked, since every copy of it is the same marked
|
|
405
|
+
// or not: a copy of an object that is not marked reads its getters, and the
|
|
406
|
+
// copy holds none. An object with no prototype counts as marked as well: a
|
|
407
|
+
// source compiled JSX writes with a getter into a merge or an element's
|
|
408
|
+
// spread (`_$merge(props, { __proto__: null, get on() { … } })`), and a
|
|
409
|
+
// live() view. what-react's props for a native component are marked
|
|
410
|
+
// (react-compat mixed.js).
|
|
411
|
+
// Only own keys are copied or spread, so a key written onto Object.prototype
|
|
412
|
+
// never reaches a component or an element, and a key named `__proto__` is
|
|
413
|
+
// defined as a property like any other (_setOwnProp): no copy sets a
|
|
414
|
+
// prototype with it.
|
|
603
415
|
//
|
|
604
|
-
//
|
|
605
|
-
//
|
|
606
|
-
|
|
416
|
+
// Compiled JSX may write the mark into its literal (`{ [_$props]: true, get
|
|
417
|
+
// on() { … } }`, render.js _$props): the component gets that object with the
|
|
418
|
+
// mark hidden (_ownProps). Its object with no prototype is given
|
|
419
|
+
// Object.prototype in place instead, which V8 serves more slowly: each such
|
|
420
|
+
// object gets a hidden class of its own, so every read of it is a lookup.
|
|
421
|
+
export const PROPS = Symbol.for('what-core.props.v1');
|
|
422
|
+
const MARK = { value: true };
|
|
423
|
+
const HIDE = { enumerable: false };
|
|
424
|
+
|
|
425
|
+
/** `props`, a plain object, marked as a props object of What's (PROPS): its getters are kept by every copy. */
|
|
426
|
+
export function _markProps(props) {
|
|
427
|
+
return Object.defineProperty(props, PROPS, MARK);
|
|
428
|
+
}
|
|
607
429
|
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
if (
|
|
612
|
-
|
|
613
|
-
return true;
|
|
430
|
+
/** Compiled JSX's props object made the one its component sees, in place: Object.prototype, the mark hidden. */
|
|
431
|
+
export function _ownProps(props) {
|
|
432
|
+
if (Object.getPrototypeOf(props) === null) return Object.defineProperty(Object.setPrototypeOf(props, Object.prototype), PROPS, MARK);
|
|
433
|
+
if (Object.prototype.propertyIsEnumerable.call(props, PROPS)) Object.defineProperty(props, PROPS, HIDE);
|
|
434
|
+
return props;
|
|
614
435
|
}
|
|
615
436
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
if (typeof Component === 'function' &&
|
|
621
|
-
(Component.prototype?.isReactComponent || Component.prototype?.render)) {
|
|
622
|
-
const ClassComp = Component;
|
|
623
|
-
Component = function ClassComponentBridge(props) {
|
|
624
|
-
const instance = new ClassComp(props);
|
|
625
|
-
return instance.render();
|
|
626
|
-
};
|
|
627
|
-
Component.displayName = ClassComp.displayName || ClassComp.name || 'ClassComponent';
|
|
628
|
-
}
|
|
437
|
+
/** Whether the object `x` is a props object What built whose getters are What's (live props, lazy children): marked, or with no prototype. */
|
|
438
|
+
export function _isOwnProps(x) {
|
|
439
|
+
return x[PROPS] > 0 || Object.getPrototypeOf(x) === null;
|
|
440
|
+
}
|
|
629
441
|
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
442
|
+
// A props object marked 2 is one whose keys can arrive after its component
|
|
443
|
+
// ran: what-react's props for a native component a React parent renders
|
|
444
|
+
// (react-compat mixed.js), which the parent passes a prop on a later render.
|
|
445
|
+
// A key it does not hold yet is a live prop all the same, so the place that
|
|
446
|
+
// reads it shows it once it comes. It refuses writes itself: development
|
|
447
|
+
// does not freeze it (render.js _renderComponent).
|
|
448
|
+
|
|
449
|
+
/** Whether `props[key]` is a live prop: a getter, not a lazy JSX one, of a props object What built. */
|
|
450
|
+
export function _isLiveProp(props, key) {
|
|
451
|
+
if (props == null || typeof props !== 'object' || !_isOwnProps(props)) return false;
|
|
452
|
+
const d = Object.getOwnPropertyDescriptor(props, key);
|
|
453
|
+
return d === undefined ? props[PROPS] === 2 : d.get !== undefined && /** @type {any} */ (d.get)._lazy !== true;
|
|
454
|
+
}
|
|
640
455
|
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
456
|
+
/**
|
|
457
|
+
* A plain object with the own enumerable properties of `props`, without the
|
|
458
|
+
* keys in `omit` (a rest element, render.js _$omit). A props object What
|
|
459
|
+
* marked keeps its getters (live props, lazy children), and the copy is
|
|
460
|
+
* marked when it kept one; any other object is copied by value, as a spread
|
|
461
|
+
* copies it, and the copy holds no getter.
|
|
462
|
+
*/
|
|
463
|
+
export function _copyProps(props, omit) {
|
|
464
|
+
if (props == null) return {};
|
|
465
|
+
const own = _isOwnProps(props);
|
|
466
|
+
// A spread defines each key, `__proto__` too, where Object.assign would set
|
|
467
|
+
// the prototype with it.
|
|
468
|
+
if (!own && !omit) return { ...props };
|
|
469
|
+
const out = {};
|
|
470
|
+
let live = false;
|
|
471
|
+
for (const key in props) {
|
|
472
|
+
if (Object.hasOwn(props, key) && !(omit && omit.includes(key)) && _copyProp(out, props, key, own)) live = true;
|
|
655
473
|
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
hookIndex: 0,
|
|
659
|
-
/** @type {Array<() => void>} */
|
|
660
|
-
effects: [],
|
|
661
|
-
cleanups: [],
|
|
662
|
-
mounted: false,
|
|
663
|
-
disposed: false,
|
|
664
|
-
Component,
|
|
665
|
-
_parentCtx: parentCtx,
|
|
666
|
-
_errorBoundary: errorBoundary,
|
|
667
|
-
};
|
|
474
|
+
return live ? _markProps(out) : out;
|
|
475
|
+
}
|
|
668
476
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
const
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
ctx._endComment = endComment;
|
|
676
|
-
|
|
677
|
-
// Fragment to hold comment boundaries + component output
|
|
678
|
-
const container = document.createDocumentFragment();
|
|
679
|
-
container._componentCtx = ctx;
|
|
680
|
-
ctx._wrapper = startComment; // Reference for context lookup
|
|
681
|
-
|
|
682
|
-
// Track for disposal
|
|
683
|
-
mountedComponents.add(ctx);
|
|
684
|
-
if (__DEV__ && __devtools?.onComponentMount) __devtools.onComponentMount?.(ctx);
|
|
685
|
-
|
|
686
|
-
// Props signal for reactive updates from parent
|
|
687
|
-
const propsChildren = children.length === 0 ? undefined : children.length === 1 ? children[0] : children;
|
|
688
|
-
// Merge children into props without spreading when possible
|
|
689
|
-
let mergedProps;
|
|
690
|
-
if (propsChildren !== undefined) {
|
|
691
|
-
mergedProps = props ? Object.assign({}, props, { children: propsChildren }) : { children: propsChildren };
|
|
692
|
-
} else {
|
|
693
|
-
mergedProps = props ? Object.assign({}, props) : {};
|
|
694
|
-
}
|
|
695
|
-
const lazyChildren = props && props._$lazyChildren;
|
|
696
|
-
const endChildrenPass = lazyChildren ? _installLazyChildren(Component, mergedProps, lazyChildren) : null;
|
|
697
|
-
|
|
698
|
-
const propsSignal = signal(mergedProps);
|
|
699
|
-
ctx._propsSignal = propsSignal;
|
|
700
|
-
|
|
701
|
-
// Create a reactive props proxy: reading any prop inside an effect
|
|
702
|
-
// will auto-track the dependency on the propsSignal. This makes prop
|
|
703
|
-
// access reactive across re-renders without requiring the component
|
|
704
|
-
// to be re-executed.
|
|
705
|
-
// Reuse shared trap handlers to minimize per-component allocation.
|
|
706
|
-
// Store propsSignal on a plain object target (Proxy invariant: ownKeys must
|
|
707
|
-
// match non-configurable own properties of target; functions have 'prototype').
|
|
708
|
-
const reactiveProps = new Proxy({ _sig: propsSignal }, _propsProxyHandler);
|
|
709
|
-
|
|
710
|
-
// Component runs ONCE — not inside an effect.
|
|
711
|
-
// untrack() prevents the component's signal reads and effect creation
|
|
712
|
-
// from being captured by any parent effect (e.g., reconcileInsert).
|
|
713
|
-
// Without this, dynamically-rendered components leak their internal
|
|
714
|
-
// reactivity into the parent, causing infinite re-creation loops.
|
|
715
|
-
componentStack.push(ctx);
|
|
716
|
-
|
|
717
|
-
let result;
|
|
718
|
-
try {
|
|
719
|
-
result = untrack(() => Component(reactiveProps));
|
|
720
|
-
} catch (error) {
|
|
721
|
-
componentStack.pop();
|
|
722
|
-
// A thrown thenable is a suspension, not a failure: hand it to the nearest
|
|
723
|
-
// Suspense boundary, which swaps in its fallback and re-renders on resolve.
|
|
724
|
-
// A navigation signal is neither: it carries its own handler.
|
|
725
|
-
if (!_handleNavigationSignal(error)
|
|
726
|
-
&& !(error && typeof error.then === 'function' && suspend(error, ctx))
|
|
727
|
-
&& !reportError(error, ctx)) {
|
|
728
|
-
console.error('[what] Uncaught error in component:', Component.name || 'Anonymous', error);
|
|
729
|
-
throw error;
|
|
730
|
-
}
|
|
731
|
-
// Return fragment with just comment boundaries on error
|
|
732
|
-
container.appendChild(startComment);
|
|
733
|
-
container.appendChild(endComment);
|
|
734
|
-
return container;
|
|
735
|
-
}
|
|
736
|
-
// The component has run; anything that reads props.children from here on is a
|
|
737
|
-
// later render pass and must build its own children.
|
|
738
|
-
if (endChildrenPass) endChildrenPass();
|
|
739
|
-
|
|
740
|
-
ctx.mounted = true;
|
|
741
|
-
|
|
742
|
-
// Run onMount callbacks after DOM is ready
|
|
743
|
-
if (ctx._mountCallbacks) {
|
|
744
|
-
queueMicrotask(() => {
|
|
745
|
-
if (ctx.disposed) return;
|
|
746
|
-
for (const fn of ctx._mountCallbacks) {
|
|
747
|
-
try { fn(); } catch (e) { console.error('[what] onMount error:', e); }
|
|
748
|
-
}
|
|
749
|
-
});
|
|
477
|
+
/** `src[key]` onto `out`: a getter of a props object What built (`own`) as the getter, never read, and true; anything else as its value. */
|
|
478
|
+
export function _copyProp(out, src, key, own) {
|
|
479
|
+
const get = own ? /** @type {PropertyDescriptor} */ (Object.getOwnPropertyDescriptor(src, key)).get : undefined;
|
|
480
|
+
if (get) {
|
|
481
|
+
Object.defineProperty(out, key, { get, enumerable: true, configurable: true });
|
|
482
|
+
return true;
|
|
750
483
|
}
|
|
484
|
+
_setOwnProp(out, key, src[key]);
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* `props[key] = value` on a props object that may hold a getter under `key`
|
|
490
|
+
* (a forwarded live prop, lazy children), or inherit one: `__proto__` and
|
|
491
|
+
* Object.prototype's other names are defined as own properties, so a key
|
|
492
|
+
* named `__proto__` never sets the prototype.
|
|
493
|
+
*/
|
|
494
|
+
export function _setOwnProp(props, key, value) {
|
|
495
|
+
if (key in props) Object.defineProperty(props, key, { value, writable: true, enumerable: true, configurable: true });
|
|
496
|
+
else props[key] = value;
|
|
497
|
+
}
|
|
751
498
|
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
container.appendChild(startComment);
|
|
757
|
-
const vnodes = Array.isArray(result) ? result : [result];
|
|
499
|
+
/** `build()`, placed where the server wrote it with no markers of its own (_place). */
|
|
500
|
+
export function _placeFlat(build) {
|
|
501
|
+
const outer = flat;
|
|
502
|
+
flat = true;
|
|
758
503
|
try {
|
|
759
|
-
|
|
760
|
-
const node = createDOM(v, container, isSvg);
|
|
761
|
-
if (node) container.appendChild(node);
|
|
762
|
-
}
|
|
504
|
+
return build();
|
|
763
505
|
} finally {
|
|
764
|
-
|
|
506
|
+
flat = outer;
|
|
765
507
|
}
|
|
766
|
-
|
|
508
|
+
}
|
|
767
509
|
|
|
768
|
-
|
|
510
|
+
/** Whether the children built now are placed with no markers of their own (_place); asked once, by their _$members. */
|
|
511
|
+
export function _takeFlat() {
|
|
512
|
+
const was = flat;
|
|
513
|
+
flat = false;
|
|
514
|
+
return was;
|
|
769
515
|
}
|
|
770
516
|
|
|
771
|
-
|
|
772
|
-
//
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
517
|
+
export function _installLazyChildren(Component, target, lazyChildren, slots) {
|
|
518
|
+
// cache[0] is the component's owner once its run is over, null during it.
|
|
519
|
+
// Then, for each prop: its factory, what its reads got (PLACED once its JSX
|
|
520
|
+
// is placed), 0 while unread, 1 while that waits for its placement, 2 once
|
|
521
|
+
// placed, and its placeable.
|
|
522
|
+
/** @type {any[]} */
|
|
523
|
+
const cache = [null];
|
|
524
|
+
const define = (key, factory) => {
|
|
525
|
+
// Its first getter marks target, so every copy keeps them (_copyProps).
|
|
526
|
+
if (cache.length === 1 && !_isOwnProps(target)) _markProps(target);
|
|
527
|
+
const at = cache.push(factory, undefined, 0, null) - 4;
|
|
528
|
+
/** @type {(() => any) & { _lazy?: boolean }} */
|
|
529
|
+
const get = function () {
|
|
530
|
+
const place = placing;
|
|
531
|
+
placing = false;
|
|
532
|
+
return readLazy(cache, at, place);
|
|
533
|
+
};
|
|
534
|
+
// Not a live prop (_isLiveProp): a read of it builds JSX.
|
|
535
|
+
get._lazy = true;
|
|
536
|
+
Object.defineProperty(target, key, { get, enumerable: true, configurable: true });
|
|
537
|
+
};
|
|
538
|
+
if (lazyChildren) {
|
|
539
|
+
if (Component._deferChildren) target.children = lazyChildren;
|
|
540
|
+
else define('children', lazyChildren);
|
|
781
541
|
}
|
|
782
|
-
|
|
542
|
+
if (slots) for (const key in slots) define(key, slots[key]);
|
|
543
|
+
return cache.length === 1 ? null : (owner) => {
|
|
544
|
+
// Development: null before a hot update runs the component's new code
|
|
545
|
+
// (render.js rebuild), whose reads of these props are a first run's.
|
|
546
|
+
if (__DEV__ && owner === null) for (let at = 1; at < cache.length; at += 4) cache.splice(at + 1, 3, undefined, 0, null);
|
|
547
|
+
cache[0] = owner;
|
|
548
|
+
};
|
|
783
549
|
}
|
|
784
550
|
|
|
785
|
-
//
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
551
|
+
// What a prop's reads got once its JSX is placed: its nodes are not kept.
|
|
552
|
+
const PLACED = {};
|
|
553
|
+
// The state of a slot that forwards a live prop (_place's read, `_forward`):
|
|
554
|
+
// every read reads it through, a place gets the read as a reactive child.
|
|
555
|
+
const FORWARDS = 3;
|
|
556
|
+
|
|
557
|
+
// A placing read (and a placeable's placement) takes the build that waits
|
|
558
|
+
// for its placement, else builds anew, under the owner of the place (the
|
|
559
|
+
// component's when there is none). Any other read gets what a read got, else
|
|
560
|
+
// a build (the component's after its run, off the walk while hydrating) that
|
|
561
|
+
// waits for its placement; once its JSX is placed, and for any JSX after the
|
|
562
|
+
// run, the placeable.
|
|
563
|
+
function readLazy(cache, at, place) {
|
|
564
|
+
const home = cache[0];
|
|
565
|
+
let value = cache[at + 1];
|
|
566
|
+
const state = cache[at + 2];
|
|
567
|
+
// A forwarded live prop (`<Leaf counts={props.data} />`, data.counts a
|
|
568
|
+
// getter): the child reads the current value wherever it reads it, never
|
|
569
|
+
// the read function, and a place shows it as a reactive child.
|
|
570
|
+
if (state === FORWARDS) return place ? value : value();
|
|
571
|
+
if (place) {
|
|
572
|
+
if (state !== 1) value = buildLazy(cache[at], home !== null && getOwner() === null ? home : null);
|
|
573
|
+
cache[at + 1] = holdsNodes(value) ? PLACED : value;
|
|
574
|
+
cache[at + 2] = value?._forward ? FORWARDS : 2;
|
|
575
|
+
return value;
|
|
576
|
+
}
|
|
577
|
+
if (state === 0) {
|
|
578
|
+
const build = () => buildLazy(cache[at], home);
|
|
579
|
+
value = cache[at + 1] = outsideRead === null ? build() : outsideRead(build);
|
|
580
|
+
cache[at + 2] = 1;
|
|
581
|
+
if (value?._forward) return (cache[at + 2] = FORWARDS, value());
|
|
582
|
+
}
|
|
583
|
+
if (value !== PLACED && (home === null || !holdsNodes(value))) return value;
|
|
584
|
+
if (cache[at + 3] === null) {
|
|
585
|
+
// An array holding its build: a signal stores it as it is (a function
|
|
586
|
+
// would be called as an updater), and every render path builds it there.
|
|
587
|
+
const build = () => readLazy(cache, at, true);
|
|
588
|
+
build._lazyChildren = true;
|
|
589
|
+
cache[at + 3] = [build];
|
|
590
|
+
}
|
|
591
|
+
return cache[at + 3];
|
|
592
|
+
}
|
|
789
593
|
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
const endComment = document.createComment('eb:end');
|
|
794
|
-
|
|
795
|
-
const boundaryCtx = {
|
|
796
|
-
hooks: /** @type {any[]} */ ([]), hookIndex: 0,
|
|
797
|
-
effects: /** @type {Array<() => void>} */ ([]),
|
|
798
|
-
cleanups: /** @type {Array<() => void>} */ ([]),
|
|
799
|
-
mounted: false, disposed: false,
|
|
800
|
-
_parentCtx: componentStack[componentStack.length - 1] || null,
|
|
801
|
-
_errorBoundary: handleError,
|
|
802
|
-
_startComment: startComment,
|
|
803
|
-
_endComment: endComment,
|
|
804
|
-
};
|
|
805
|
-
_commentCtxMap.set(startComment, boundaryCtx);
|
|
806
|
-
|
|
807
|
-
const container = document.createDocumentFragment();
|
|
808
|
-
container._componentCtx = boundaryCtx;
|
|
809
|
-
container.appendChild(startComment);
|
|
810
|
-
container.appendChild(endComment);
|
|
811
|
-
|
|
812
|
-
const dispose = effect(() => {
|
|
813
|
-
const error = errorState();
|
|
814
|
-
|
|
815
|
-
componentStack.push(boundaryCtx);
|
|
816
|
-
try {
|
|
817
|
-
// Remove old content between comment boundaries
|
|
818
|
-
const openParent = startComment.parentNode;
|
|
819
|
-
if (openParent) {
|
|
820
|
-
while (startComment.nextSibling && startComment.nextSibling !== endComment) {
|
|
821
|
-
const old = startComment.nextSibling;
|
|
822
|
-
disposeTree(old);
|
|
823
|
-
openParent.removeChild(old);
|
|
824
|
-
}
|
|
825
|
-
}
|
|
594
|
+
function buildLazy(factory, owner) {
|
|
595
|
+
return owner !== null ? _runOwned(owner, factory) : untrack(factory);
|
|
596
|
+
}
|
|
826
597
|
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
598
|
+
// Whether a value holds built DOM (an element, a component's fragment, the
|
|
599
|
+
// array a fragment or a .map() gives), not data a prop hands on (an object, a
|
|
600
|
+
// function, a vnode, which is built where it is placed).
|
|
601
|
+
function holdsNodes(value) {
|
|
602
|
+
return _isDomNode(value) || (Array.isArray(value) && value.some(holdsNodes));
|
|
603
|
+
}
|
|
833
604
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
container.insertBefore(node, endComment);
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
} finally {
|
|
849
|
-
// A fallback may throw; later mounts must not inherit this boundary.
|
|
850
|
-
componentStack.pop();
|
|
851
|
-
}
|
|
852
|
-
});
|
|
605
|
+
// --- _handleNavigationSignal(error) ---
|
|
606
|
+
// A thrown value may carry its own handler under
|
|
607
|
+
// Symbol.for('what.navigation.signal.v1'). what-router's redirect() throws one, so
|
|
608
|
+
// a redirect from a component body runs the navigation instead of reaching an
|
|
609
|
+
// ErrorBoundary, which would render error UI for a value that is not an error.
|
|
610
|
+
// Returns true when the value was a signal and has been handled.
|
|
611
|
+
//
|
|
612
|
+
// Both component paths route through this (_renderComponent and the hydration
|
|
613
|
+
// branch, both in render.js) so the two cannot drift.
|
|
614
|
+
const NAV_SIGNAL = Symbol.for('what.navigation.signal.v1');
|
|
853
615
|
|
|
854
|
-
|
|
855
|
-
return
|
|
616
|
+
export function _handleNavigationSignal(error) {
|
|
617
|
+
if (error == null) return false;
|
|
618
|
+
const handler = error[NAV_SIGNAL];
|
|
619
|
+
if (typeof handler !== 'function') return false;
|
|
620
|
+
handler(error);
|
|
621
|
+
return true;
|
|
856
622
|
}
|
|
857
623
|
|
|
858
|
-
//
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
624
|
+
// Compiled JSX's lazy children and slots (_$lazyChildren, _$slots) reach a
|
|
625
|
+
// component the vnode renderer runs only on a vnode render.js componentVNode
|
|
626
|
+
// made (the root compiled JSX hands to hydrate()), which installs their
|
|
627
|
+
// reading here first: a program with no compiled JSX does not carry it.
|
|
628
|
+
/** @type {(...args: Parameters<typeof _installLazyChildren>) => ReturnType<typeof _installLazyChildren>} */
|
|
629
|
+
let vnodeLazyProps = () => null;
|
|
862
630
|
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
const boundaryCtx = {
|
|
869
|
-
hooks: /** @type {any[]} */ ([]), hookIndex: 0,
|
|
870
|
-
effects: /** @type {Array<() => void>} */ ([]),
|
|
871
|
-
cleanups: /** @type {Array<() => void>} */ ([]),
|
|
872
|
-
mounted: false, disposed: false,
|
|
873
|
-
_parentCtx: componentStack[componentStack.length - 1] || null,
|
|
874
|
-
_suspenseBoundary: boundary,
|
|
875
|
-
_startComment: startComment,
|
|
876
|
-
_endComment: endComment,
|
|
877
|
-
};
|
|
878
|
-
_commentCtxMap.set(startComment, boundaryCtx);
|
|
879
|
-
|
|
880
|
-
const container = document.createDocumentFragment();
|
|
881
|
-
container._componentCtx = boundaryCtx;
|
|
882
|
-
container.appendChild(startComment);
|
|
883
|
-
container.appendChild(endComment);
|
|
884
|
-
|
|
885
|
-
// A child suspending mid-render flips `loading` while this effect is still
|
|
886
|
-
// running, which can re-enter it. The generation counter lets the outer run
|
|
887
|
-
// detect that a newer run already replaced the content and bail out.
|
|
888
|
-
let generation = 0;
|
|
889
|
-
|
|
890
|
-
const dispose = effect(() => {
|
|
891
|
-
const isLoading = loading();
|
|
892
|
-
const vnodes = isLoading ? [fallback] : children;
|
|
893
|
-
const normalized = Array.isArray(vnodes) ? vnodes : [vnodes];
|
|
894
|
-
const gen = ++generation;
|
|
895
|
-
|
|
896
|
-
componentStack.push(boundaryCtx);
|
|
897
|
-
|
|
898
|
-
// Remove old content between comment boundaries
|
|
899
|
-
const openParent = startComment.parentNode;
|
|
900
|
-
if (openParent) {
|
|
901
|
-
while (startComment.nextSibling && startComment.nextSibling !== endComment) {
|
|
902
|
-
const old = startComment.nextSibling;
|
|
903
|
-
disposeTree(old);
|
|
904
|
-
openParent.removeChild(old);
|
|
905
|
-
}
|
|
906
|
-
}
|
|
631
|
+
/** @internal What makes a component vnode with compiled JSX's lazy children or slots calls this first. */
|
|
632
|
+
export function _installVNodeLazyProps() {
|
|
633
|
+
vnodeLazyProps = _installLazyChildren;
|
|
634
|
+
}
|
|
907
635
|
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
if (gen !== generation) {
|
|
912
|
-
if (node) disposeTree(node);
|
|
913
|
-
break;
|
|
914
|
-
}
|
|
915
|
-
if (node) {
|
|
916
|
-
// Insert before endComment
|
|
917
|
-
if (endComment.parentNode) {
|
|
918
|
-
endComment.parentNode.insertBefore(node, endComment);
|
|
919
|
-
} else {
|
|
920
|
-
// Still in fragment before first mount
|
|
921
|
-
container.insertBefore(node, endComment);
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
} finally {
|
|
926
|
-
componentStack.pop();
|
|
927
|
-
}
|
|
928
|
-
});
|
|
636
|
+
function createComponent(vnode, parent, isSvg) {
|
|
637
|
+
const { props, children } = vnode;
|
|
638
|
+
const Component = _toFunctionComponent(vnode.tag);
|
|
929
639
|
|
|
930
|
-
|
|
931
|
-
|
|
640
|
+
const mergedProps = _copyProps(props);
|
|
641
|
+
if (children.length > 0) _setOwnProp(mergedProps, 'children', children.length === 1 ? children[0] : children);
|
|
642
|
+
const lazyChildren = props && props._$lazyChildren;
|
|
643
|
+
const slots = props && props._$slots;
|
|
644
|
+
const endChildrenPass = lazyChildren || slots ? vnodeLazyProps(Component, mergedProps, lazyChildren, slots) : null;
|
|
645
|
+
if (__DEV__ && vnode._loc) return _renderComponent(Component, mergedProps, endChildrenPass, isSvg, vnode._loc);
|
|
646
|
+
return _renderComponent(Component, mergedProps, endChildrenPass, isSvg);
|
|
932
647
|
}
|
|
933
648
|
|
|
934
|
-
// Portal
|
|
935
|
-
|
|
649
|
+
// <Portal>: its children are built into `container` under the portal's owner,
|
|
650
|
+
// so context and boundaries reach them, and removed from it when that owner
|
|
651
|
+
// is disposed. A portal's vnode carries it (`mount`): helpers.js Portal and
|
|
652
|
+
// react-compat's createPortal (through what-core/internal).
|
|
653
|
+
export function createPortalDOM(vnode, _parent) {
|
|
936
654
|
const { container } = vnode.props;
|
|
937
655
|
const children = vnode.children;
|
|
938
656
|
|
|
@@ -941,34 +659,25 @@ function createPortalDOM(vnode, _parent) {
|
|
|
941
659
|
return document.createComment('portal:empty');
|
|
942
660
|
}
|
|
943
661
|
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
effects: /** @type {Array<() => void>} */ ([]),
|
|
947
|
-
cleanups: /** @type {Array<() => void>} */ ([]),
|
|
948
|
-
mounted: false, disposed: false,
|
|
949
|
-
_parentCtx: componentStack[componentStack.length - 1] || null,
|
|
950
|
-
};
|
|
951
|
-
|
|
952
|
-
const placeholder = document.createComment('portal');
|
|
953
|
-
placeholder._componentCtx = portalCtx;
|
|
954
|
-
|
|
662
|
+
// A component child arrives as a fragment, which inserting empties: track
|
|
663
|
+
// the nodes it carried, or the cleanup below finds nothing to remove.
|
|
955
664
|
const portalNodes = [];
|
|
956
665
|
for (const child of children) {
|
|
957
666
|
const node = createDOM(child, container);
|
|
958
|
-
if (node)
|
|
959
|
-
|
|
667
|
+
if (!node) continue;
|
|
668
|
+
if (node.nodeType === 11) {
|
|
669
|
+
for (let n = node.firstChild; n !== null; n = n.nextSibling) portalNodes.push(n);
|
|
670
|
+
} else {
|
|
960
671
|
portalNodes.push(node);
|
|
961
672
|
}
|
|
673
|
+
container.appendChild(node);
|
|
962
674
|
}
|
|
963
|
-
|
|
964
|
-
portalCtx._cleanupCallbacks = [() => {
|
|
675
|
+
onCleanup(() => {
|
|
965
676
|
for (const node of portalNodes) {
|
|
966
|
-
disposeTree(node);
|
|
967
677
|
if (node.parentNode) node.parentNode.removeChild(node);
|
|
968
678
|
}
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
return placeholder;
|
|
679
|
+
});
|
|
680
|
+
return document.createComment('portal');
|
|
972
681
|
}
|
|
973
682
|
|
|
974
683
|
// --- Create Element from VNode ---
|
|
@@ -977,81 +686,641 @@ function createPortalDOM(vnode, _parent) {
|
|
|
977
686
|
function createElementFromVNode(vnode, parent, isSvg) {
|
|
978
687
|
const { tag, props, children } = vnode;
|
|
979
688
|
|
|
980
|
-
|
|
689
|
+
// In SVG, or an element only SVG has (vnode-renderer.js SVG_TAG).
|
|
690
|
+
const svgContext = isSvg || SVG_TAG.test(tag);
|
|
981
691
|
const el = svgContext
|
|
982
692
|
? document.createElementNS(SVG_NS, tag)
|
|
983
693
|
: document.createElement(tag);
|
|
984
694
|
|
|
985
|
-
//
|
|
986
|
-
|
|
987
|
-
|
|
695
|
+
// Its props (an object, or null), written once with no diffing: a
|
|
696
|
+
// reactive prop updates through an effect of its own (setProp).
|
|
697
|
+
for (const key in props) {
|
|
698
|
+
if (key === 'key' || key === 'children') continue;
|
|
699
|
+
if (key === 'ref') applyRef(props.ref, el);
|
|
700
|
+
else setProp(el, key, props[key], svgContext);
|
|
988
701
|
}
|
|
989
702
|
|
|
990
|
-
// Append children
|
|
703
|
+
// Append children. A <noscript>'s are not rendered: with scripts on, the
|
|
704
|
+
// browser keeps its content as text, as React does.
|
|
705
|
+
if (tag === 'noscript') return el;
|
|
991
706
|
const isSvgChildren = svgContext && tag !== 'foreignObject';
|
|
992
707
|
for (let i = 0; i < children.length; i++) {
|
|
993
708
|
const node = createDOM(children[i], el, isSvgChildren);
|
|
994
709
|
if (node) el.appendChild(node);
|
|
995
710
|
}
|
|
996
711
|
|
|
997
|
-
el._vnode = vnode;
|
|
998
712
|
return el;
|
|
999
713
|
}
|
|
1000
714
|
|
|
1001
|
-
// ---
|
|
1002
|
-
//
|
|
715
|
+
// --- Form fields ---
|
|
716
|
+
//
|
|
717
|
+
// A form field's state, through every client path that writes it (h()
|
|
718
|
+
// setProp, compiled setProp, setValue and setChecked, a spread, hydration),
|
|
719
|
+
// as React 19 documents it for controlled and uncontrolled fields:
|
|
720
|
+
// - `value` (an <input> whose value is its own, a <textarea>, a <select>)
|
|
721
|
+
// and `checked` (an <input>) control what the field shows. They are
|
|
722
|
+
// written as properties, which mark the field changed, so a default
|
|
723
|
+
// written later never moves what it shows; a write of the value it wrote
|
|
724
|
+
// last is skipped, which leaves the caret where it is. The value a prop
|
|
725
|
+
// holds is what a form reset keeps (_resetForm), any value as its string
|
|
726
|
+
// (`true` is "true", as React writes it), checked as a boolean. A field
|
|
727
|
+
// whose value or checked compiled JSX binds to state shows the state:
|
|
728
|
+
// after each of its input and change events, once its handlers and the
|
|
729
|
+
// updates they caused are done, it shows what the prop holds again
|
|
730
|
+
// (showControlled), as React restores a controlled field whose handler
|
|
731
|
+
// did not take the edit. Any other field keeps the edit until the prop's
|
|
732
|
+
// next write: one whose value reads no state (a constant, a value a
|
|
733
|
+
// spread read once, as `{...register('name')}` gives), and one made with
|
|
734
|
+
// h().
|
|
735
|
+
// - Null or undefined releases a field the prop controlled: it shows
|
|
736
|
+
// nothing (an empty value, unchecked, no option selected), and a reset
|
|
737
|
+
// restores its default. A field it never controlled is left as it is.
|
|
738
|
+
// - `defaultValue`, `defaultChecked`, an <option>'s `selected` and
|
|
739
|
+
// `defaultSelected`, and a <select>'s `defaultValue` (the options it
|
|
740
|
+
// names) are the field's default: its content attribute (what a reset
|
|
741
|
+
// restores, what a server writes), which shows only while nothing has
|
|
742
|
+
// controlled or changed the field. An <option>'s `value` is what names
|
|
743
|
+
// it.
|
|
744
|
+
// A select's value selects the options markup-rules' selectedOptions names.
|
|
745
|
+
// Its options may not be in the DOM yet (children are appended after props),
|
|
746
|
+
// so a value or default they do not show is written once more after them;
|
|
747
|
+
// a later write, null included, supersedes that retry. A select a value
|
|
748
|
+
// controls follows it whenever its option values change: a region or list
|
|
749
|
+
// that adds or removes options (its own, or a group's), an option's `value`
|
|
750
|
+
// written, or the text of an option with no `value` (its value) written, has
|
|
751
|
+
// it select them again (_optionsChanged), as does an option's default
|
|
752
|
+
// changing (_keepSelection). A select with a default and no value follows its
|
|
753
|
+
// default the same way until the user changes what it shows.
|
|
1003
754
|
|
|
1004
|
-
|
|
1005
|
-
|
|
755
|
+
/**
|
|
756
|
+
* Write prop `key` of a form field `el` and return true, or return false
|
|
757
|
+
* when `key` is not a field prop of `el`.
|
|
758
|
+
*/
|
|
759
|
+
export function _setFieldProp(el, key, value) {
|
|
760
|
+
const tag = el.tagName;
|
|
761
|
+
if (tag === 'OPTION' || tag === 'SELECT') {
|
|
762
|
+
if (selects !== null) return selects(el, tag, key, value);
|
|
763
|
+
// Every path that writes one installs the code first (_installSelects).
|
|
764
|
+
if (__DEV__) console.error(`[what] internal: a <${el.localName}>'s ${key} was written before what-core installed its select code.`);
|
|
765
|
+
return false;
|
|
766
|
+
}
|
|
767
|
+
if (tag === 'INPUT' && key === 'checked') {
|
|
768
|
+
const on = value == null ? undefined : !!value;
|
|
769
|
+
if (el._$checked !== on) el.checked = (el._$checked = on) === true;
|
|
770
|
+
} else if (tag === 'INPUT' && key === 'defaultChecked') {
|
|
771
|
+
el.defaultChecked = !!value;
|
|
772
|
+
// A default checks a radio nothing has changed, which unchecks the others
|
|
773
|
+
// in its group, a radio a prop controls among them: that one is checked
|
|
774
|
+
// again.
|
|
775
|
+
if (value && el.type === 'radio' && el._$checked === undefined && el.form) {
|
|
776
|
+
for (const other of _formMember(el.form, 'elements')) if (other._$checked && other.name === el.name) other.checked = true;
|
|
777
|
+
}
|
|
778
|
+
} else if ((key !== 'value' && key !== 'defaultValue')
|
|
779
|
+
|| (tag === 'INPUT' ? ATTRIBUTE_VALUE_TYPES.has(el.type) : tag !== 'TEXTAREA')) {
|
|
780
|
+
return false;
|
|
781
|
+
} else if (key === 'defaultValue') {
|
|
782
|
+
el.defaultValue = value == null ? '' : String(value);
|
|
783
|
+
} else {
|
|
784
|
+
const str = value == null ? undefined : String(value);
|
|
785
|
+
if (el._$value !== str) {
|
|
786
|
+
el._$value = str;
|
|
787
|
+
// A file field holds what the user picked: only an empty value is
|
|
788
|
+
// written to it, which clears it (any other value throws). A number
|
|
789
|
+
// field that shows the number already ("04" typed for 4, "1.0" for 1)
|
|
790
|
+
// keeps what the user typed, as React's does.
|
|
791
|
+
if (el.type === 'file' ? !str : str === undefined || el.type !== 'number' || el.validity.badInput || (el.value || NaN) != +str) writeValue(el, str ?? '');
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
return true;
|
|
795
|
+
}
|
|
1006
796
|
|
|
1007
|
-
|
|
1008
|
-
|
|
797
|
+
// What writes a text field's value: what its onChange has seen (_onChange)
|
|
798
|
+
// when the field showed what onChange saw last. A field that holds an edit
|
|
799
|
+
// no onChange has heard yet (an onInput handler's state, written before the
|
|
800
|
+
// onChange handlers of the same `input` run) keeps it for onChange to hear,
|
|
801
|
+
// and one whose onChange saw nothing yet hears whatever comes next.
|
|
802
|
+
function writeValue(el, value) {
|
|
803
|
+
const was = el._$seen;
|
|
804
|
+
const seen = was === el.value;
|
|
805
|
+
el.value = value;
|
|
806
|
+
el._$seen = seen ? el.value : was;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
// The props of a <select> and its <option>s: their value, their default and
|
|
810
|
+
// the selection kept in step with them (below). Installed by what can write
|
|
811
|
+
// one, so a program that makes no select does not carry the code: compiled
|
|
812
|
+
// JSX whose module writes a prop of a <select> or an <option> (`_$selects()`,
|
|
813
|
+
// at module scope), the h() renderer and hydration writing a vnode's props
|
|
814
|
+
// (setProp below), and the setters written by hand given one (render.js
|
|
815
|
+
// setProp, setValue, spread).
|
|
816
|
+
// A select whose value a prop wrote carries the code that wrote it
|
|
817
|
+
// (`_$select`), which showControlled and _resetForm write it again with: the
|
|
818
|
+
// copy of what-core that does is not always the one that wrote it.
|
|
819
|
+
/** @type {typeof selectProp | null} */
|
|
820
|
+
let selects = null;
|
|
821
|
+
|
|
822
|
+
/** @internal Install what the props of a <select> and its <option>s need. */
|
|
823
|
+
export function _installSelects() {
|
|
824
|
+
selects = selectProp;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/** @internal Install the select code for `el` when it is a select or an option. */
|
|
828
|
+
export function _selectsFor(el) {
|
|
829
|
+
if (selects === null && (el.tagName === 'SELECT' || el.tagName === 'OPTION')) selects = selectProp;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
function selectProp(el, tag, key, value) {
|
|
833
|
+
if (tag === 'OPTION') {
|
|
834
|
+
// Its value names it: a select a value controls selects again when the
|
|
835
|
+
// value changed, and after its default changed (_keepSelection).
|
|
836
|
+
if (key === 'value') {
|
|
837
|
+
if (value == null) el.removeAttribute('value');
|
|
838
|
+
else el.value = value;
|
|
839
|
+
_optionsChanged(el);
|
|
840
|
+
} else if (key === 'selected' || key === 'defaultSelected') {
|
|
841
|
+
el.defaultSelected = !!value;
|
|
842
|
+
_keepSelection(el);
|
|
843
|
+
} else return false;
|
|
844
|
+
} else if (key === 'defaultValue') setSelectDefault(el, value);
|
|
845
|
+
else if (key === 'value') _setSelectValue(el, value);
|
|
846
|
+
else return false;
|
|
847
|
+
return true;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
// --- A controlled field shows what controls it ---
|
|
851
|
+
//
|
|
852
|
+
// The window of a field a prop controls hears every input, change and
|
|
853
|
+
// compositionend event, in both phases: nothing is added per field, and any
|
|
854
|
+
// other field is passed over at once. The bubble phase comes after
|
|
855
|
+
// every handler, the element's own and the delegated ones, and after the
|
|
856
|
+
// updates their writes caused when the browser dispatched the event (it runs
|
|
857
|
+
// microtasks after each listener); for an event a script dispatched those are
|
|
858
|
+
// still queued, ahead of the microtask taken here. A handler that stops the
|
|
859
|
+
// event keeps it from the bubble phase, so the capture phase, which nothing
|
|
860
|
+
// below the window can stop, schedules the same check as a task. While an
|
|
861
|
+
// input method composes, the field holds text the prop never will: it is
|
|
862
|
+
// left alone until the composition ends.
|
|
863
|
+
//
|
|
864
|
+
// A field is put back once every event of the user's action has been handled.
|
|
865
|
+
// The browser runs microtasks after each listener of an event it dispatches,
|
|
866
|
+
// so a check taken after `input` would run before a `change` of the same
|
|
867
|
+
// action is dispatched, and its handlers would read the field as it was
|
|
868
|
+
// (e.target.checked false on the click that checked it). Clicking or
|
|
869
|
+
// pressing a checkbox or radio, and picking in a select, fire `input` and then
|
|
870
|
+
// `change` in one task (the HTML standard's activation behaviour and select
|
|
871
|
+
// update notifications): their field is checked after the `change`. A text
|
|
872
|
+
// field is checked after each `input`, which its onChange handlers hear
|
|
873
|
+
// (_onChange below), and after a `change`.
|
|
874
|
+
//
|
|
875
|
+
// A form's reset gives each of its fields its default once the `reset` event
|
|
876
|
+
// is over: a field a prop controls shows what the prop holds again after it,
|
|
877
|
+
// as React's does (it keeps a controlled field's value attribute in step).
|
|
878
|
+
// The event comes before the reset: a reset a script calls (`form.reset()`)
|
|
879
|
+
// is done by the microtask the bubble phase takes, and one the user makes (a
|
|
880
|
+
// reset button) is done by the task the capture phase takes.
|
|
881
|
+
//
|
|
882
|
+
// Reached only from the bindings compiled JSX makes for a field's value and
|
|
883
|
+
// checked (render.js setValue, setChecked, and a spread on a field,
|
|
884
|
+
// _$controlled), so a program with no such binding does not carry it. A
|
|
885
|
+
// binding that read state marks its field (`_$kept`), and only a marked field
|
|
886
|
+
// is put back: the edit of any other field stands until its prop's next
|
|
887
|
+
// write, whatever else the page holds.
|
|
888
|
+
//
|
|
889
|
+
// A page can hold two copies of what-core (a library bundled with its own):
|
|
890
|
+
// the first to bind a field listens for the window, and puts back the fields
|
|
891
|
+
// of both. Everything it reads and writes is on the field, so it needs none
|
|
892
|
+
// of the other copy's code: a select is written again by the select code that
|
|
893
|
+
// wrote its value, which leaves itself on the select (`_$select`), so a copy
|
|
894
|
+
// without select code puts back the other copy's select.
|
|
895
|
+
|
|
896
|
+
/**
|
|
897
|
+
* @internal Called by a binding of `el`'s value or checked, in its run, with
|
|
898
|
+
* the value read: when the run read state, `el` shows what its props hold
|
|
899
|
+
* after each of its events.
|
|
900
|
+
*/
|
|
901
|
+
export function _controlFields(el) {
|
|
902
|
+
if (!_tracking()) return;
|
|
903
|
+
el._$kept = true;
|
|
904
|
+
// A field cloned from a template is in the template's document, which has
|
|
905
|
+
// no window, until it is placed: its events will reach this one's.
|
|
906
|
+
const win = el.ownerDocument.defaultView || globalThis.window;
|
|
907
|
+
if (!win || win._$controlled) return;
|
|
908
|
+
win._$controlled = true;
|
|
909
|
+
for (const type of ['input', 'change', 'compositionend', 'reset']) {
|
|
910
|
+
win.addEventListener(type, controlledChanged, true);
|
|
911
|
+
win.addEventListener(type, controlledChanged);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
function controlledChanged(e) {
|
|
916
|
+
const el = e.composedPath()[0];
|
|
917
|
+
// A form, on its reset (showControlled shows its fields, and passes over
|
|
918
|
+
// anything else a script dispatched one at); else a field, and before a
|
|
919
|
+
// change only a text field (not a select, the one field with options, a
|
|
920
|
+
// checkbox or a radio).
|
|
921
|
+
if (e.type !== 'reset' && (e.isComposing || !el?._$kept
|
|
922
|
+
|| (e.type !== 'change' && (el.options || el.type === 'checkbox' || el.type === 'radio')))) return;
|
|
923
|
+
if (e.eventPhase === 1) setTimeout(showControlled, 0, el);
|
|
924
|
+
else queueMicrotask(() => showControlled(el));
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
// --- onChange ---
|
|
928
|
+
//
|
|
929
|
+
// `onChange` is React's change event in every path that binds one: compiled
|
|
930
|
+
// JSX's delegated `$$change` (render.js _$changes) and its listener for an
|
|
931
|
+
// event with modifiers, a spread's (render.js listen), an h() element's props
|
|
932
|
+
// (setProp below). On a field or above one (a form's hears its
|
|
933
|
+
// fields), it fires for a text field (a <textarea>, an <input> of a type the
|
|
934
|
+
// user types in, the types React lists) as the user edits it, from its
|
|
935
|
+
// `input` events and from a `change` that reports an edit no `input` did; for
|
|
936
|
+
// a checkbox, a radio, a select and a file field on their `change`, as the
|
|
937
|
+
// DOM's does. A text field's event is an edit when the field holds what it
|
|
938
|
+
// did not when its onChange handlers last ran or What last wrote it
|
|
939
|
+
// (`_$seen`): the `change` it sends when it loses focus is none after the
|
|
940
|
+
// `input` that reported the edit, and an `input` that left its value as it
|
|
941
|
+
// was (`1.` typed on the way to `1.5`, which a number field holds as `1`)
|
|
942
|
+
// none, as React's value tracker decides. What a handler writes
|
|
943
|
+
// (`e.target.value = e.target.value.toUpperCase()`) is what it saw. A field
|
|
944
|
+
// forgets what it held at the user's `change` (not one a script dispatched),
|
|
945
|
+
// so a value a script wrote while the user was elsewhere (a button that
|
|
946
|
+
// clears it, a form reset) is changed by the user's next edit, whatever it
|
|
947
|
+
// holds. React sees a script's write itself
|
|
948
|
+
// (it shadows the field's `value`), so an event that follows one is no edit
|
|
949
|
+
// there; here it is one when the value differs, as in what-react. Decided once
|
|
950
|
+
// per event, for every handler on its path, with what it reads kept on the
|
|
951
|
+
// event and the field, so two copies of what-core on a page decide alike.
|
|
952
|
+
// `on(el, 'change', fn)` (render.js) is the DOM's change event, unchanged.
|
|
953
|
+
|
|
954
|
+
/** @internal Call `handler` with `e`, an `input` or `change` event, when it is React's change event. */
|
|
955
|
+
export function _onChange(e, handler) {
|
|
956
|
+
const el = e.target;
|
|
957
|
+
const change = e.type === 'change';
|
|
958
|
+
// An <input> or a <textarea> (`selectionStart` is theirs alone), of a type
|
|
959
|
+
// whose value is the user's: not a file field (`files`), a checkbox, a
|
|
960
|
+
// radio or a button.
|
|
961
|
+
const text = 'selectionStart' in el && !el.files && !ATTRIBUTE_VALUE_TYPES.has(el.type);
|
|
962
|
+
e._$change ??= text ? el.value !== el._$seen : change;
|
|
963
|
+
if (e._$change) handler(e);
|
|
964
|
+
if (text) {
|
|
965
|
+
el._$seen = change && e.isTrusted ? undefined : el.value;
|
|
966
|
+
hearScriptWrites(el);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
// A script's write to a text field it has heard (`el.value = ''` once a chat
|
|
971
|
+
// box has sent, while the field keeps the focus) makes the field forget what
|
|
972
|
+
// onChange saw, so the next edit is heard, one that types what the field held
|
|
973
|
+
// before the write included. The `value` setter of the field's prototype
|
|
974
|
+
// (HTMLInputElement's, HTMLTextAreaElement's) is wrapped on the field, as
|
|
975
|
+
// React's value tracker wraps it; What's writes (writeValue) keep what
|
|
976
|
+
// onChange saw. A field that already has its own `value` (a copy of
|
|
977
|
+
// what-core, or a library, wrapped it) is left as it is.
|
|
978
|
+
function hearScriptWrites(el) {
|
|
979
|
+
const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), 'value');
|
|
980
|
+
const set = desc?.set;
|
|
981
|
+
if (set === undefined || Object.hasOwn(el, 'value')) return;
|
|
982
|
+
Object.defineProperty(el, 'value', {
|
|
983
|
+
configurable: true,
|
|
984
|
+
get: /** @type {PropertyDescriptor} */ (desc).get,
|
|
985
|
+
set(next) {
|
|
986
|
+
set.call(this, next);
|
|
987
|
+
this._$seen = undefined;
|
|
988
|
+
},
|
|
989
|
+
});
|
|
990
|
+
}
|
|
1009
991
|
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
992
|
+
// Show what the props of `el` hold, where the field shows something else.
|
|
993
|
+
// Nothing is written to a field that already shows it, so the caret stays.
|
|
994
|
+
// A number field is compared as a number, and left alone while what it holds
|
|
995
|
+
// is not one yet ("1." on the way to "1.5", which it reports as bad input);
|
|
996
|
+
// an empty one is not the number 0, so a value of 0 is written to it, as
|
|
997
|
+
// React writes it. A file field holds only what the user picked: nothing is
|
|
998
|
+
// written to it (any value but '' throws), and it shows the value once it is
|
|
999
|
+
// another kind of field again. A radio is checked or unchecked with the rest
|
|
1000
|
+
// of its group: checking one unchecked the one a prop keeps checked. Also
|
|
1001
|
+
// called once an input's type or a select's multiple changed what the field
|
|
1002
|
+
// shows (render.js retype).
|
|
1003
|
+
function showControlled(el) {
|
|
1004
|
+
// A form after its reset: its fields a prop controls.
|
|
1005
|
+
if (el.localName === 'form') {
|
|
1006
|
+
for (const field of _formMember(el, 'elements')) if (field._$kept) showControlled(field);
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
const value = el._$value;
|
|
1010
|
+
if (value !== undefined) {
|
|
1011
|
+
// A select's value was written by the select code it carries (above).
|
|
1012
|
+
if (el._$select) el._$select(el, value);
|
|
1013
|
+
// `files` is a file field's alone (null on any other input).
|
|
1014
|
+
else if (!el.files && el.value !== value && (el.type !== 'number' || (!el.validity.badInput && (el.value || NaN) != +value))) writeValue(el, value);
|
|
1015
|
+
}
|
|
1016
|
+
if (el._$checked === undefined) return;
|
|
1017
|
+
const group = el.type !== 'radio' || !el.name ? [el]
|
|
1018
|
+
: el.form ? _formMember(el.form, 'elements') : el.getRootNode().querySelectorAll('input[type=radio]');
|
|
1019
|
+
for (const field of group) {
|
|
1020
|
+
if (field._$kept && field._$checked !== undefined && field.type === el.type && field.name === el.name) field.checked = field._$checked;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
export { showControlled as _showControlled };
|
|
1024
|
+
|
|
1025
|
+
function _setSelectValue(el, value) {
|
|
1026
|
+
if (value == null && el._$value === undefined) return;
|
|
1027
|
+
_selectionKeeper = _keepSelection;
|
|
1028
|
+
el._$value = value ?? undefined;
|
|
1029
|
+
// What writes it again (showControlled, _resetForm), from any copy.
|
|
1030
|
+
el._$select = _setSelectValue;
|
|
1031
|
+
_installOptionsChanged(_optionsChanged);
|
|
1032
|
+
const write = (el._$selectWrite = (el._$selectWrite || 0) + 1);
|
|
1033
|
+
if (!markOptions(el, value, 'selected')) {
|
|
1034
|
+
const retry = () => el._$selectWrite === write && markOptions(el, value, 'selected');
|
|
1035
|
+
if (!_afterWalk(retry)) queueMicrotask(retry);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// Write `prop` (`selected`, or `defaultSelected`) of every option of `el`:
|
|
1040
|
+
// true for the options `value` names, false for the rest (null names none;
|
|
1041
|
+
// a single select then shows none, as unselecting the last option of one
|
|
1042
|
+
// would select its first). Whether its options were there to name.
|
|
1043
|
+
function markOptions(el, value, prop) {
|
|
1044
|
+
const options = [...el.options];
|
|
1045
|
+
const values = options.map((option) => option.value);
|
|
1046
|
+
el._$marked = [options, values];
|
|
1047
|
+
const chosen = selectedOptions(values, value);
|
|
1048
|
+
// Only the options whose mark changes: each write runs the select's
|
|
1049
|
+
// selectedness algorithm over all of them.
|
|
1050
|
+
for (let i = 0; i < options.length; i++) if (options[i][prop] !== chosen[i]) options[i][prop] = chosen[i];
|
|
1051
|
+
if (prop === 'selected' && !el.multiple && !chosen.includes(true)) el.selectedIndex = -1;
|
|
1052
|
+
return values.length > 0 && [value].flat().every((v) => values.includes(String(v)));
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
// An option's default (its selected attribute) can move what its select
|
|
1056
|
+
// shows even after a script selected options (Chromium lets it; JSDOM, like
|
|
1057
|
+
// the HTML standard, does not once an option was written): a select a value
|
|
1058
|
+
// controls selects its options again. Called with an option after every
|
|
1059
|
+
// write of its default, by hydration after it rewrites an option's
|
|
1060
|
+
// attributes (_selectionKeeper), and with a select after a write of its
|
|
1061
|
+
// default.
|
|
1062
|
+
function _keepSelection(el) {
|
|
1063
|
+
const select = el.closest('select');
|
|
1064
|
+
if (select !== null) refollow(select, true);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
/**
|
|
1068
|
+
* A region, a list or a text write changed the options under `parent` (a
|
|
1069
|
+
* select, an optgroup or an option), or an option's value was written: its
|
|
1070
|
+
* select selects them again when its option values changed (other options,
|
|
1071
|
+
* or another value of one), and leaves what it shows alone otherwise, so a
|
|
1072
|
+
* label written in place of another keeps what the user chose.
|
|
1073
|
+
*/
|
|
1074
|
+
function _optionsChanged(parent, text) {
|
|
1075
|
+
const tag = parent.localName;
|
|
1076
|
+
// The text of an option with a value is its label.
|
|
1077
|
+
if (text === 1 && tag === 'option' && parent.hasAttribute('value')) return;
|
|
1078
|
+
const select = tag === 'select' ? parent : tag === 'optgroup' || tag === 'option' ? parent.closest('select') : null;
|
|
1079
|
+
if (select !== null) refollow(select, false);
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
// A select follows its options again (`always`, or when their values
|
|
1083
|
+
// changed): now, or, while a hydration walk claims them one by one, once
|
|
1084
|
+
// when the walk is done, so hydrating a select is linear in its options.
|
|
1085
|
+
function refollow(select, always) {
|
|
1086
|
+
if (select._$refollow !== undefined) {
|
|
1087
|
+
select._$refollow ||= always;
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
select._$refollow = always;
|
|
1091
|
+
const run = () => {
|
|
1092
|
+
const now = select._$refollow;
|
|
1093
|
+
select._$refollow = undefined;
|
|
1094
|
+
if (now || optionValuesChanged(select)) follow(select);
|
|
1095
|
+
};
|
|
1096
|
+
if (!_afterWalk(run)) run();
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
// Whether `select` has other options, or other values of them, than the ones
|
|
1100
|
+
// it marked last (markOptions).
|
|
1101
|
+
function optionValuesChanged(select) {
|
|
1102
|
+
const marked = select._$marked;
|
|
1103
|
+
if (marked === undefined) return true;
|
|
1104
|
+
const [options, values] = marked;
|
|
1105
|
+
const now = select.options;
|
|
1106
|
+
if (now.length !== options.length) return true;
|
|
1107
|
+
for (let i = 0; i < options.length; i++) {
|
|
1108
|
+
if (now[i] !== options[i] || now[i].value !== values[i]) return true;
|
|
1109
|
+
}
|
|
1110
|
+
return false;
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
// A select shows what its value names, else, until the user changes what it
|
|
1114
|
+
// shows (a change event, or an edit before hydration: _changedFields), what
|
|
1115
|
+
// its default names: an option set that changed under a select the user left
|
|
1116
|
+
// alone gets the default's marks again. A hydration rewrites its options one
|
|
1117
|
+
// by one, so it marks them once, when its walk is done.
|
|
1118
|
+
function follow(select) {
|
|
1119
|
+
if (select._$value !== undefined) markOptions(select, select._$value, 'selected');
|
|
1120
|
+
else if (select._$default !== undefined && !select._$chosen && !select._$following) {
|
|
1121
|
+
select._$following = true;
|
|
1122
|
+
const mark = () => {
|
|
1123
|
+
select._$following = false;
|
|
1124
|
+
if (select._$value === undefined && !select._$chosen) markOptions(select, select._$default, 'defaultSelected');
|
|
1125
|
+
};
|
|
1126
|
+
if (!_afterWalk(mark)) mark();
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
// What hydration calls with an option whose attributes it rewrote: the above,
|
|
1132
|
+
// installed by the first write of a select's value or default, so the
|
|
1133
|
+
// hydration walk itself does not reach the option marking. The select code
|
|
1134
|
+
// it belongs to is itself installed (_installSelects).
|
|
1135
|
+
/** @type {((option: Element) => void) | null} */
|
|
1136
|
+
export let _selectionKeeper = null;
|
|
1137
|
+
|
|
1138
|
+
// A select's defaultValue: the options it names are the default-selected
|
|
1139
|
+
// ones. Written now and once more after the options are in place: hydration
|
|
1140
|
+
// writes each option's own props after its select's, which would take the
|
|
1141
|
+
// mark away from an option the select named, so a hydration writes it again
|
|
1142
|
+
// when its walk is done.
|
|
1143
|
+
function setSelectDefault(el, value) {
|
|
1144
|
+
if (value == null) return;
|
|
1145
|
+
if (el._$default === undefined) el.addEventListener('change', () => { el._$chosen = true; });
|
|
1146
|
+
el._$default = value;
|
|
1147
|
+
_selectionKeeper = _keepSelection;
|
|
1148
|
+
_installOptionsChanged(_optionsChanged);
|
|
1149
|
+
const apply = () => {
|
|
1150
|
+
if (el._$default !== value) return;
|
|
1151
|
+
markOptions(el, value, 'defaultSelected');
|
|
1152
|
+
_keepSelection(el);
|
|
1153
|
+
};
|
|
1154
|
+
apply();
|
|
1155
|
+
if (!_afterWalk(apply)) queueMicrotask(apply);
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
// --- What the user changed before hydration ---
|
|
1159
|
+
//
|
|
1160
|
+
// A field the user changed between the first paint and hydration keeps what
|
|
1161
|
+
// they left in it, controlled or not, and no event is dispatched for it: the
|
|
1162
|
+
// handlers see the edits made after hydration. Released React does the same
|
|
1163
|
+
// for every field but a textarea (react-dom 19.3 leaves the live value alone
|
|
1164
|
+
// and dispatches nothing, and puts a textarea back to the server's text). The
|
|
1165
|
+
// fields show the user's values before the hydration's mount callbacks run
|
|
1166
|
+
// (render.js walkDone). A value the browser restored on a history navigation looks
|
|
1167
|
+
// exactly like an edit, so replaying one would run a handler the user never
|
|
1168
|
+
// triggered. A field is changed when what it shows is not what the same
|
|
1169
|
+
// markup shows untouched (the server wrote what it showed there). An input
|
|
1170
|
+
// or a textarea that shows exactly what its markup says (its value attribute
|
|
1171
|
+
// or text, its checked attribute) is untouched: what it shows is a value the
|
|
1172
|
+
// browser kept, and the same markup shows it untouched. Any other, and every
|
|
1173
|
+
// select, is compared with the browser's own reset algorithm run on a copy:
|
|
1174
|
+
// its value (a range at its midpoint, a color at #000000), its checkedness,
|
|
1175
|
+
// or the options a reset selects. A controlled field's prop still counts as
|
|
1176
|
+
// written, so a write of the same value leaves the user's change, and any
|
|
1177
|
+
// other value replaces it. A file input keeps the files chosen in it:
|
|
1178
|
+
// hydration writes none.
|
|
1179
|
+
|
|
1180
|
+
/**
|
|
1181
|
+
* The fields hydration is claiming with `el` that the user changed, each as
|
|
1182
|
+
* [el, what it shows], pushed to `out`: `el`, or every radio of its group,
|
|
1183
|
+
* which the props of the first one claimed would change (`seen` holds the
|
|
1184
|
+
* radios already read).
|
|
1185
|
+
*/
|
|
1186
|
+
export function _changedFields(el, out, seen) {
|
|
1187
|
+
if (el.type !== 'radio') {
|
|
1188
|
+
const changed = changedField(el);
|
|
1189
|
+
if (changed !== null) out.push(changed);
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
const group = el.form !== null ? _formMember(el.form, 'elements') : el.ownerDocument.getElementsByName(el.name);
|
|
1193
|
+
const radios = [];
|
|
1194
|
+
for (const radio of group) {
|
|
1195
|
+
// A native radio only: a form-associated custom element reporting
|
|
1196
|
+
// `type === 'radio'` is no member of the group's reset, and a copy of it
|
|
1197
|
+
// would run its constructor. It is left as it is.
|
|
1198
|
+
if (radio.localName !== 'input' || radio.type !== 'radio' || radio.name !== el.name || radio.form !== el.form || seen.has(radio)) continue;
|
|
1199
|
+
seen.add(radio);
|
|
1200
|
+
radios.push(radio);
|
|
1201
|
+
}
|
|
1202
|
+
if (radios.length === 0 || radios.every((radio) => radio.checked === radio.hasAttribute('checked'))) return;
|
|
1203
|
+
const copies = untouched(radios);
|
|
1204
|
+
radios.forEach((radio, i) => { if (radio.checked !== copies[i].checked) out.push([radio, radio.checked]); });
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
// What `els` show untouched: copies of them, reset by the browser in a form
|
|
1208
|
+
// of their own (a radio group resets together, so the last `checked` wins).
|
|
1209
|
+
// A copy is made from the field's markup alone (copyOf), so it runs no
|
|
1210
|
+
// customized element's constructor and loads no image: the reset needs a
|
|
1211
|
+
// form in a document with a browsing context, where a clone would do both.
|
|
1212
|
+
function untouched(els) {
|
|
1213
|
+
const doc = els[0].ownerDocument;
|
|
1214
|
+
const form = doc.createElement('form');
|
|
1215
|
+
const copies = els.map((el) => copyOf(el, doc));
|
|
1216
|
+
_formMember(form, 'append')(...copies);
|
|
1217
|
+
_formMember(form, 'reset')();
|
|
1218
|
+
return copies;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
// An element of `el`'s tag with its attributes but `is` (a customized
|
|
1222
|
+
// built-in is decided when the element is made); for a textarea its text,
|
|
1223
|
+
// for a select its options, each with its attributes and text, in their
|
|
1224
|
+
// groups.
|
|
1225
|
+
function copyOf(el, doc) {
|
|
1226
|
+
const copy = doc.createElement(el.localName);
|
|
1227
|
+
for (const name of el.getAttributeNames()) if (name !== 'is') copy.setAttribute(name, el.getAttribute(name));
|
|
1228
|
+
if (el.localName === 'textarea') copy.textContent = el.defaultValue;
|
|
1229
|
+
else if (el.localName === 'select') {
|
|
1230
|
+
let group = null;
|
|
1231
|
+
let groupCopy = null;
|
|
1232
|
+
for (const option of el.options) {
|
|
1233
|
+
const parent = option.parentElement;
|
|
1234
|
+
let into = copy;
|
|
1235
|
+
if (parent !== el && parent !== null && parent.localName === 'optgroup') {
|
|
1236
|
+
if (parent !== group) {
|
|
1237
|
+
group = parent;
|
|
1238
|
+
groupCopy = copy.appendChild(copyOf(parent, doc));
|
|
1239
|
+
}
|
|
1240
|
+
into = groupCopy;
|
|
1241
|
+
}
|
|
1242
|
+
into.appendChild(copyOf(option, doc)).textContent = option.text;
|
|
1016
1243
|
}
|
|
1244
|
+
}
|
|
1245
|
+
return copy;
|
|
1246
|
+
}
|
|
1017
1247
|
|
|
1018
|
-
|
|
1248
|
+
function changedField(el) {
|
|
1249
|
+
if (el.type === 'file' || el.type === 'image') return null;
|
|
1250
|
+
if (el.tagName === 'SELECT') {
|
|
1251
|
+
const [copy] = untouched([el]);
|
|
1252
|
+
const options = [...el.options];
|
|
1253
|
+
const live = options.map((option) => option.selected);
|
|
1254
|
+
const reset = copy.options;
|
|
1255
|
+
if (!live.some((selected, i) => selected !== reset[i].selected)) return null;
|
|
1256
|
+
el._$chosen = true;
|
|
1257
|
+
return [el, options, live];
|
|
1258
|
+
}
|
|
1259
|
+
if (el.type === 'checkbox') {
|
|
1260
|
+
if (el.checked === el.hasAttribute('checked')) return null;
|
|
1261
|
+
return el.checked !== untouched([el])[0].checked ? [el, el.checked] : null;
|
|
1019
1262
|
}
|
|
1263
|
+
if (el.value === (el.tagName === 'TEXTAREA' ? el.defaultValue : el.getAttribute('value') ?? '')) return null;
|
|
1264
|
+
return el.value !== untouched([el])[0].value ? [el, el.value] : null;
|
|
1020
1265
|
}
|
|
1021
1266
|
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1267
|
+
/** The fields a hydration claimed changed (_changedFields) show what they showed. */
|
|
1268
|
+
export function _restoreFields(fields) {
|
|
1269
|
+
for (const [el, shown, live] of fields) {
|
|
1270
|
+
if (live !== undefined) {
|
|
1271
|
+
shown.forEach((option, i) => { if (!live[i]) option.selected = false; });
|
|
1272
|
+
shown.forEach((option, i) => { if (live[i]) option.selected = true; });
|
|
1273
|
+
if (!el.multiple && !live.includes(true)) el.selectedIndex = -1;
|
|
1274
|
+
} else if (shown === false) el.checked = false;
|
|
1275
|
+
else if (shown !== true && el.value !== shown) el.value = shown;
|
|
1276
|
+
}
|
|
1277
|
+
// A radio checked last unchecks the rest of its group.
|
|
1278
|
+
for (const [el, shown] of fields) if (shown === true) el.checked = true;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
// Whether `el` takes prop `key` through a property it can write: one with a
|
|
1282
|
+
// setter (value, checked, a custom element's accessor) or a writable data
|
|
1283
|
+
// property. A getter-only reflection (an input's `form` owner, its `list`
|
|
1284
|
+
// datalist, a select's `type`) takes the attribute instead: assigning to it
|
|
1285
|
+
// throws in a module.
|
|
1286
|
+
export function _writableProp(el, key) {
|
|
1287
|
+
for (let o = el; o !== null; o = Object.getPrototypeOf(o)) {
|
|
1288
|
+
const d = Object.getOwnPropertyDescriptor(o, key);
|
|
1289
|
+
if (d !== undefined) return d.writable === true || d.set !== undefined;
|
|
1029
1290
|
}
|
|
1291
|
+
return false;
|
|
1030
1292
|
}
|
|
1031
1293
|
|
|
1032
1294
|
// NOTE: there are intentionally TWO `setProp` implementations in this codebase:
|
|
1033
|
-
// - dom.js setProp (this one) — h()/createDOM
|
|
1034
|
-
//
|
|
1035
|
-
//
|
|
1036
|
-
// legacy diff-driven update path.
|
|
1295
|
+
// - dom.js setProp (this one) — h()/createDOM path. Handles addEventListener
|
|
1296
|
+
// bookkeeping (el._events with capture variants and the untrack wrapper),
|
|
1297
|
+
// supports `isSvg` flag from the caller.
|
|
1037
1298
|
// - render.js setProp — fine-grained-compiler output path. No event-handler
|
|
1038
1299
|
// bookkeeping (events go through delegation / direct addEventListener at
|
|
1039
1300
|
// compile time), but adds the innerHTML `{__html}` enforcement that the
|
|
1040
1301
|
// compiler relies on.
|
|
1041
|
-
// Both
|
|
1042
|
-
//
|
|
1043
|
-
//
|
|
1302
|
+
// Both gate attributes through _isUnsafeAttr() so URL sanitization cannot
|
|
1303
|
+
// diverge. Do not merge without consolidating the event/listener model: they
|
|
1304
|
+
// have different callers. hydrate() writes a vnode's props onto the element it
|
|
1305
|
+
// claims through this one too (render.js, from the renderer slot), so a
|
|
1306
|
+
// hydrated element behaves as a built one.
|
|
1307
|
+
export { setProp as _setVNodeProp };
|
|
1308
|
+
|
|
1044
1309
|
function setProp(el, key, value, isSvg) {
|
|
1310
|
+
// A prop named `__proto__` (a user object's own key) would set the
|
|
1311
|
+
// element's prototype: it is never written.
|
|
1312
|
+
if (key === '__proto__') return;
|
|
1313
|
+
// A function classList, or an object with function entries, is reactive in
|
|
1314
|
+
// setClassList itself.
|
|
1315
|
+
if (key === 'classList') {
|
|
1316
|
+
setClassList(el, value);
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1045
1320
|
// Reactive function props — wrap in effect for fine-grained updates
|
|
1046
1321
|
if (typeof value === 'function' && !_isEventProp(key) && key !== 'ref') {
|
|
1047
|
-
if (
|
|
1048
|
-
|
|
1049
|
-
try { el._propEffects[key](); } catch { /* already disposed */ }
|
|
1050
|
-
}
|
|
1051
|
-
el._propEffects[key] = effect(() => {
|
|
1052
|
-
const resolved = value();
|
|
1053
|
-
setProp(el, key, resolved, isSvg);
|
|
1054
|
-
});
|
|
1322
|
+
if (__DEV__) setNextBinding(el, `${el.localName}.${key}`);
|
|
1323
|
+
effect(() => { setProp(el, key, value(), isSvg); });
|
|
1055
1324
|
return;
|
|
1056
1325
|
}
|
|
1057
1326
|
|
|
@@ -1060,33 +1329,50 @@ function setProp(el, key, value, isSvg) {
|
|
|
1060
1329
|
if (typeof value !== 'function' && value != null) return;
|
|
1061
1330
|
let eventName = key.slice(2);
|
|
1062
1331
|
let useCapture = false;
|
|
1063
|
-
|
|
1332
|
+
// onGotPointerCapture and onLostPointerCapture are events of their own.
|
|
1333
|
+
if (eventName.endsWith('Capture') && !eventName.endsWith('PointerCapture')) {
|
|
1064
1334
|
eventName = eventName.slice(0, -7);
|
|
1065
1335
|
useCapture = true;
|
|
1066
1336
|
}
|
|
1067
|
-
|
|
1337
|
+
// onDoubleClick is React's name for the DOM's `dblclick`.
|
|
1338
|
+
let event = eventName.toLowerCase();
|
|
1339
|
+
if (event === 'doubleclick') event = 'dblclick';
|
|
1068
1340
|
const storageKey = useCapture ? event + '_capture' : event;
|
|
1341
|
+
// onChange is React's change event (_onChange): it hears `input` too.
|
|
1342
|
+
const types = event === 'change' ? ['input', event] : [event];
|
|
1069
1343
|
const old = el._events?.[storageKey];
|
|
1070
1344
|
if (old && old._original === value) return;
|
|
1071
|
-
if (old) el.removeEventListener(
|
|
1345
|
+
if (old) for (const type of types) el.removeEventListener(type, old, useCapture);
|
|
1072
1346
|
if (value == null) return;
|
|
1073
1347
|
if (!el._events) el._events = {};
|
|
1074
1348
|
// Single closure per event listener. Uses untrack to prevent accidental
|
|
1075
|
-
// signal subscriptions inside event handlers.
|
|
1349
|
+
// signal subscriptions inside event handlers. The listener belongs to the
|
|
1350
|
+
// owner it was set under: once that owner is disposed it never runs again
|
|
1351
|
+
// and removes itself when an event reaches it (an element that outlived
|
|
1352
|
+
// its owner: a node the next hydrate() of the same container claims, one
|
|
1353
|
+
// the app keeps and inserts again).
|
|
1354
|
+
const owner = getOwner();
|
|
1355
|
+
const opts = value._eventOpts || useCapture || undefined;
|
|
1076
1356
|
const wrappedHandler = (e) => {
|
|
1357
|
+
if (owner !== null && _isDisposed(owner)) {
|
|
1358
|
+
for (const type of types) el.removeEventListener(type, wrappedHandler, opts);
|
|
1359
|
+
if (el._events[storageKey] === wrappedHandler) delete el._events[storageKey];
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1077
1362
|
if (!e.nativeEvent) e.nativeEvent = e;
|
|
1078
|
-
return untrack(() => wrappedHandler._handler(e));
|
|
1363
|
+
return untrack(() => event === 'change' ? _onChange(e, wrappedHandler._handler) : wrappedHandler._handler(e));
|
|
1079
1364
|
};
|
|
1080
1365
|
wrappedHandler._handler = value;
|
|
1081
1366
|
wrappedHandler._original = value;
|
|
1082
1367
|
el._events[storageKey] = wrappedHandler;
|
|
1083
|
-
const
|
|
1084
|
-
el.addEventListener(event, wrappedHandler, eventOpts || useCapture || undefined);
|
|
1368
|
+
for (const type of types) el.addEventListener(type, wrappedHandler, opts);
|
|
1085
1369
|
return;
|
|
1086
1370
|
}
|
|
1087
1371
|
|
|
1088
|
-
// Reject dangerous URL protocols and srcdoc
|
|
1372
|
+
// Reject dangerous URL protocols and srcdoc. A refused value leaves no
|
|
1373
|
+
// attribute: not one an earlier write set, not one the server wrote.
|
|
1089
1374
|
if (_isUnsafeAttr(key, value)) {
|
|
1375
|
+
el.removeAttribute(key);
|
|
1090
1376
|
if (typeof console !== 'undefined') {
|
|
1091
1377
|
console.warn(`[what] Blocked unsafe URL in "${key}" attribute:`, value);
|
|
1092
1378
|
}
|
|
@@ -1095,34 +1381,23 @@ function setProp(el, key, value, isSvg) {
|
|
|
1095
1381
|
|
|
1096
1382
|
// className / class
|
|
1097
1383
|
if (key === 'className' || key === 'class') {
|
|
1098
|
-
|
|
1099
|
-
el.setAttribute('class', value || '');
|
|
1100
|
-
} else {
|
|
1101
|
-
el.className = value || '';
|
|
1102
|
-
}
|
|
1384
|
+
_writeClass(el, value, isSvg);
|
|
1103
1385
|
return;
|
|
1104
1386
|
}
|
|
1105
1387
|
|
|
1106
|
-
// Style
|
|
1107
1388
|
if (key === 'style') {
|
|
1108
|
-
|
|
1109
|
-
el.style.cssText = value;
|
|
1110
|
-
el._prevStyle = null;
|
|
1111
|
-
} else if (typeof value === 'object') {
|
|
1112
|
-
const oldStyle = el._prevStyle || {};
|
|
1113
|
-
for (const prop in oldStyle) {
|
|
1114
|
-
if (!(prop in value)) el.style[prop] = '';
|
|
1115
|
-
}
|
|
1116
|
-
for (const prop in value) {
|
|
1117
|
-
el.style[prop] = value[prop] ?? '';
|
|
1118
|
-
}
|
|
1119
|
-
el._prevStyle = { ...value };
|
|
1120
|
-
}
|
|
1389
|
+
setStyle(el, value);
|
|
1121
1390
|
return;
|
|
1122
1391
|
}
|
|
1123
1392
|
|
|
1124
|
-
// dangerouslySetInnerHTML
|
|
1125
|
-
|
|
1393
|
+
// Raw inner HTML: dangerouslySetInnerHTML, or innerHTML given as
|
|
1394
|
+
// { __html } (null writes nothing; a string is refused, to prevent XSS:
|
|
1395
|
+
// { __html } marks markup someone trusted).
|
|
1396
|
+
if (key === 'dangerouslySetInnerHTML' || key === 'innerHTML') {
|
|
1397
|
+
if (key === 'innerHTML' && !(value && typeof value === 'object' && '__html' in value)) {
|
|
1398
|
+
if (__DEV__ && value != null) report('ERR_UNSAFE_INNERHTML', { subject: { kind: 'element', name: el.localName } });
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1126
1401
|
const html = value?.__html ?? '';
|
|
1127
1402
|
if (__DEV__ && typeof html === 'string' && /(<script|onerror\s*=|onload\s*=|javascript:)/i.test(html)) {
|
|
1128
1403
|
console.warn('[what] dangerouslySetInnerHTML contains potential XSS vectors. Ensure content is sanitized.');
|
|
@@ -1131,27 +1406,11 @@ function setProp(el, key, value, isSvg) {
|
|
|
1131
1406
|
return;
|
|
1132
1407
|
}
|
|
1133
1408
|
|
|
1134
|
-
//
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
if (__DEV__ && typeof html === 'string' && /(<script|onerror\s*=|onload\s*=|javascript:)/i.test(html)) {
|
|
1140
|
-
console.warn('[what] dangerouslySetInnerHTML contains potential XSS vectors. Ensure content is sanitized.');
|
|
1141
|
-
}
|
|
1142
|
-
el.innerHTML = html;
|
|
1143
|
-
} else {
|
|
1144
|
-
if (__DEV__) {
|
|
1145
|
-
console.warn(
|
|
1146
|
-
'[what] innerHTML received a raw string. This is a security risk (XSS). ' +
|
|
1147
|
-
'Use innerHTML={{ __html: trustedString }} or dangerouslySetInnerHTML={{ __html: trustedString }} instead.'
|
|
1148
|
-
);
|
|
1149
|
-
}
|
|
1150
|
-
// Refuse to set raw string innerHTML — prevent XSS
|
|
1151
|
-
return;
|
|
1152
|
-
}
|
|
1153
|
-
return;
|
|
1154
|
-
}
|
|
1409
|
+
// A form field's state, before the nullish and boolean branches below,
|
|
1410
|
+
// which would write its value as its default (Form fields above). The h()
|
|
1411
|
+
// renderer and hydration, which write props here, carry the select code.
|
|
1412
|
+
selects = selectProp;
|
|
1413
|
+
if (_setFieldProp(el, key, value)) return;
|
|
1155
1414
|
|
|
1156
1415
|
// null / undefined — attribute must be ABSENT (React/Solid semantics), not
|
|
1157
1416
|
// stamped as the literal string "undefined"/"null". Runs before the boolean,
|
|
@@ -1159,24 +1418,21 @@ function setProp(el, key, value, isSvg) {
|
|
|
1159
1418
|
// otherwise stringify a nullish value. Reflected props (e.g. el.title) are
|
|
1160
1419
|
// reset first so removeAttribute() clears both the attribute and the property.
|
|
1161
1420
|
if (value == null) {
|
|
1162
|
-
if (key
|
|
1163
|
-
try { el[key] = ''; } catch { /*
|
|
1421
|
+
if (_writableProp(el, key)) {
|
|
1422
|
+
try { el[key] = ''; } catch { /* a property '' does not fit (files) */ }
|
|
1164
1423
|
}
|
|
1165
1424
|
el.removeAttribute(key);
|
|
1166
1425
|
return;
|
|
1167
1426
|
}
|
|
1168
1427
|
|
|
1169
|
-
//
|
|
1170
|
-
//
|
|
1171
|
-
// boolean
|
|
1172
|
-
// data
|
|
1173
|
-
//
|
|
1174
|
-
//
|
|
1175
|
-
//
|
|
1176
|
-
|
|
1177
|
-
// generic boolean branch first is also what made an SSR page disagree with
|
|
1178
|
-
// its own compiled client on hydration.
|
|
1179
|
-
if (_isAriaAttr(key) || key.startsWith('data-')) {
|
|
1428
|
+
// Enumerated attributes BEFORE the boolean fast-path: aria-*, role, data-*,
|
|
1429
|
+
// draggable, spellcheck and contenteditable take string values, so a
|
|
1430
|
+
// boolean has to serialize as "true"/"false", never as HTML boolean syntax.
|
|
1431
|
+
// `data-open="false"` is a distinct state from an absent `data-open`, and
|
|
1432
|
+
// `draggable=""` leaves a <div> undraggable (markup-rules.js
|
|
1433
|
+
// isEnumeratedAttr). render.js and the server apply the same rule, so an SSR
|
|
1434
|
+
// page and its compiled client agree on hydration.
|
|
1435
|
+
if (isEnumeratedAttr(key)) {
|
|
1180
1436
|
el.setAttribute(key, typeof value === 'boolean' ? String(value) : value);
|
|
1181
1437
|
return;
|
|
1182
1438
|
}
|
|
@@ -1199,16 +1455,47 @@ function setProp(el, key, value, isSvg) {
|
|
|
1199
1455
|
return;
|
|
1200
1456
|
}
|
|
1201
1457
|
|
|
1202
|
-
//
|
|
1203
|
-
if (
|
|
1204
|
-
_setSelectValue(el, value);
|
|
1205
|
-
return;
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
// Default: property if exists, otherwise attribute
|
|
1209
|
-
if (key in el) {
|
|
1458
|
+
// Default: property if the element can write one, otherwise attribute
|
|
1459
|
+
if (_writableProp(el, key)) {
|
|
1210
1460
|
el[key] = value;
|
|
1211
1461
|
} else {
|
|
1212
1462
|
el.setAttribute(key, value);
|
|
1213
1463
|
}
|
|
1214
1464
|
}
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* @internal A member of a form, read from its prototype; a method comes back
|
|
1468
|
+
* bound to the form. HTMLFormElement is [LegacyOverrideBuiltIns]: a control
|
|
1469
|
+
* whose name or id is a member's (`reset`, `elements`, `action`, `append`,
|
|
1470
|
+
* `dispatchEvent`, any of them) stands in for that member on the form itself,
|
|
1471
|
+
* so the framework's reads of the members a form's own code uses (its
|
|
1472
|
+
* fields, reset, submit and events, in dom.js, form.js and islands.js) go
|
|
1473
|
+
* through here. The element reads every renderer makes of any element
|
|
1474
|
+
* (`getAttribute`, `addEventListener`, ...) do not: a control named after one
|
|
1475
|
+
* of those breaks its form's hydration (docs/GOTCHAS.md).
|
|
1476
|
+
*/
|
|
1477
|
+
export function _formMember(form, name) {
|
|
1478
|
+
const value = Reflect.get(Object.getPrototypeOf(form), name, form);
|
|
1479
|
+
return typeof value === 'function' ? value.bind(form) : value;
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
// A form's reset after its action succeeded (what-server's <Form>), as React 19
|
|
1483
|
+
// resets one: its uncontrolled fields go back to their defaults, and a
|
|
1484
|
+
// controlled field (a value or checked prop) keeps the value its prop holds.
|
|
1485
|
+
// A write that throws (a control's own setter, a custom element's
|
|
1486
|
+
// formResetCallback) goes to `onError`, and every other field is still
|
|
1487
|
+
// restored.
|
|
1488
|
+
export function _resetForm(form, onError) {
|
|
1489
|
+
const fields = [..._formMember(form, 'elements')];
|
|
1490
|
+
const held = fields.map((el) => [el._$value, el._$checked]);
|
|
1491
|
+
const attempt = (write) => {
|
|
1492
|
+
try { write(); } catch (error) { onError(error); }
|
|
1493
|
+
};
|
|
1494
|
+
attempt(_formMember(form, 'reset'));
|
|
1495
|
+
fields.forEach((el, i) => {
|
|
1496
|
+
const [value, checked] = held[i];
|
|
1497
|
+
// A select's value was written by the select code it carries (_setSelectValue).
|
|
1498
|
+
if (value !== undefined) attempt(() => (el._$select ? el._$select(el, value) : (el.value = value)));
|
|
1499
|
+
if (checked !== undefined) attempt(() => { el.checked = checked; });
|
|
1500
|
+
});
|
|
1501
|
+
}
|