@verajs/renderer 0.2.1 → 0.3.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/README.md +729 -226
- package/dist/development/vera-renderer-elements.d.ts +39 -0
- package/dist/development/vera-renderer-elements.d.ts.map +1 -0
- package/dist/development/vera-renderer-elements.js +193 -0
- package/dist/development/vera-renderer-elements.js.map +1 -0
- package/dist/development/vera-renderer-hydrate-slots.d.ts +94 -0
- package/dist/development/vera-renderer-hydrate-slots.d.ts.map +1 -0
- package/dist/development/vera-renderer-hydrate-slots.js +325 -0
- package/dist/development/vera-renderer-hydrate-slots.js.map +1 -0
- package/dist/development/vera-renderer-hydration.d.ts +332 -0
- package/dist/development/vera-renderer-hydration.d.ts.map +1 -0
- package/dist/development/vera-renderer-hydration.js +864 -0
- package/dist/development/vera-renderer-hydration.js.map +1 -0
- package/dist/development/vera-renderer-keyed.d.ts +25 -8
- package/dist/development/vera-renderer-keyed.d.ts.map +1 -1
- package/dist/development/vera-renderer-keyed.js +843 -268
- package/dist/development/vera-renderer-keyed.js.map +1 -1
- package/dist/development/vera-renderer-multi-window.d.ts +5 -0
- package/dist/development/vera-renderer-multi-window.d.ts.map +1 -0
- package/dist/development/vera-renderer-multi-window.js +40 -0
- package/dist/development/vera-renderer-multi-window.js.map +1 -0
- package/dist/development/vera-renderer-namespaces.d.ts +64 -0
- package/dist/development/vera-renderer-namespaces.d.ts.map +1 -0
- package/dist/development/vera-renderer-namespaces.js +94 -0
- package/dist/development/vera-renderer-namespaces.js.map +1 -0
- package/dist/development/vera-renderer-native-elements.d.ts +13 -0
- package/dist/development/vera-renderer-native-elements.d.ts.map +1 -0
- package/dist/development/vera-renderer-native-elements.js +231 -0
- package/dist/development/vera-renderer-native-elements.js.map +1 -0
- package/dist/development/vera-renderer-portal.d.ts +29 -0
- package/dist/development/vera-renderer-portal.d.ts.map +1 -0
- package/dist/development/vera-renderer-portal.js +167 -0
- package/dist/development/vera-renderer-portal.js.map +1 -0
- package/dist/development/vera-renderer-profiler.d.ts +113 -138
- package/dist/development/vera-renderer-profiler.d.ts.map +1 -1
- package/dist/development/vera-renderer-profiler.js +2428 -2101
- package/dist/development/vera-renderer-profiler.js.map +1 -1
- package/dist/development/vera-renderer-slots.d.ts +88 -92
- package/dist/development/vera-renderer-slots.d.ts.map +1 -1
- package/dist/development/vera-renderer-slots.js +1834 -1237
- package/dist/development/vera-renderer-slots.js.map +1 -1
- package/dist/development/vera-renderer-spread.d.ts +8 -29
- package/dist/development/vera-renderer-spread.d.ts.map +1 -1
- package/dist/development/vera-renderer-spread.js +542 -461
- package/dist/development/vera-renderer-spread.js.map +1 -1
- package/dist/development/vera-renderer-tag.d.ts +12 -3
- package/dist/development/vera-renderer-tag.d.ts.map +1 -1
- package/dist/development/vera-renderer-tag.js +671 -494
- package/dist/development/vera-renderer-tag.js.map +1 -1
- package/dist/development/vera-renderer-until.d.ts +24 -0
- package/dist/development/vera-renderer-until.d.ts.map +1 -0
- package/dist/development/vera-renderer-until.js +83 -0
- package/dist/development/vera-renderer-until.js.map +1 -0
- package/dist/development/vera-renderer.d.ts +77 -96
- package/dist/development/vera-renderer.d.ts.map +1 -1
- package/dist/development/vera-renderer.js +2401 -2061
- package/dist/development/vera-renderer.js.map +1 -1
- package/dist/vera-renderer-elements.min.js +2 -0
- package/dist/vera-renderer-elements.min.js.map +1 -0
- package/dist/vera-renderer-hydrate-slots.min.js +2 -0
- package/dist/vera-renderer-hydrate-slots.min.js.map +1 -0
- package/dist/vera-renderer-hydration.min.js +3 -0
- package/dist/vera-renderer-hydration.min.js.map +1 -0
- package/dist/vera-renderer-keyed.min.js +1 -1
- package/dist/vera-renderer-keyed.min.js.map +1 -1
- package/dist/vera-renderer-multi-window.min.js +2 -0
- package/dist/vera-renderer-multi-window.min.js.map +1 -0
- package/dist/vera-renderer-namespaces.min.js +2 -0
- package/dist/vera-renderer-namespaces.min.js.map +1 -0
- package/dist/vera-renderer-native-elements.min.js +2 -0
- package/dist/vera-renderer-native-elements.min.js.map +1 -0
- package/dist/vera-renderer-portal.min.js +2 -0
- package/dist/vera-renderer-portal.min.js.map +1 -0
- package/dist/vera-renderer-slots.min.js +1 -1
- package/dist/vera-renderer-slots.min.js.map +1 -1
- package/dist/vera-renderer-spread.min.js +1 -1
- package/dist/vera-renderer-spread.min.js.map +1 -1
- package/dist/vera-renderer-tag.min.js +1 -1
- package/dist/vera-renderer-tag.min.js.map +1 -1
- package/dist/vera-renderer-until.min.js +2 -0
- package/dist/vera-renderer-until.min.js.map +1 -0
- package/dist/vera-renderer.min.js +1 -1
- package/dist/vera-renderer.min.js.map +1 -1
- package/package.json +52 -9
- package/dist/development/vera-renderer-hydrate.d.ts +0 -118
- package/dist/development/vera-renderer-hydrate.d.ts.map +0 -1
- package/dist/development/vera-renderer-hydrate.js +0 -3092
- package/dist/development/vera-renderer-hydrate.js.map +0 -1
- package/dist/vera-renderer-hydrate.min.js +0 -2
- package/dist/vera-renderer-hydrate.min.js.map +0 -1
|
@@ -0,0 +1,864 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* **Where every diagnostic code is explained.** The URL is permanent: a released bundle is immutable, so every version
|
|
3
|
+
* published points here for ever — the domain cannot lapse and `/e/` cannot be reorganized without breaking links in
|
|
4
|
+
* code nobody can edit; the docs site redirects instead. `scripts/sync-diagnostics.mjs` imports this constant, so the
|
|
5
|
+
* pages and the bundles can never disagree about where they are.
|
|
6
|
+
*/
|
|
7
|
+
const DOCS = 'https://verajs.dev/e/';
|
|
8
|
+
const diagnostic = (inArea, bySubject, withCode, andProse) =>
|
|
9
|
+
/** `true ` HERE as well as at the call: production folds this to the one-line form, branch and all. */
|
|
10
|
+
true && andProse
|
|
11
|
+
? `[vera] ${inArea}: ${bySubject} — ${andProse[0]}${andProse[1] ? ` ${andProse[1]}` : ''} (${withCode})`
|
|
12
|
+
: `[vera] ${inArea}: ${bySubject} — ${DOCS}${withCode}`;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* **The properties that replace an element's whole content** — a binding to one owns the element's children, so it
|
|
16
|
+
* cannot share the element with content of its own (markup, or a child binding the write would strand: a later commit
|
|
17
|
+
* into it then throws on a missing parent). Development refuses the pair in the template scanner and in `spread` alike.
|
|
18
|
+
*/
|
|
19
|
+
const CONTENT_PROPERTY = /^(?:textContent|innerHTML|innerText|outerHTML|outerText)$/;
|
|
20
|
+
|
|
21
|
+
const PROSE = {
|
|
22
|
+
'hydration-fallback': (template) => [
|
|
23
|
+
`fell back to a client render. This container's server markup was discarded and rebuilt (its template begins ` +
|
|
24
|
+
`${template}; its SSR <style> is kept), so the page is correct but the server's work on it was wasted.`,
|
|
25
|
+
`Its children are taken as server output of this template — if they were a client-side placeholder instead, empty ` +
|
|
26
|
+
`the container first (\`container.replaceChildren()\`) or render the placeholder with vera. Otherwise the two ` +
|
|
27
|
+
`renders have to agree exactly: check for markup the template does not describe, or state settled after the ` +
|
|
28
|
+
`server render. Other containers on the page hydrate independently and are unaffected.`,
|
|
29
|
+
],
|
|
30
|
+
'hydration-protocol': () => [
|
|
31
|
+
`this @verajs/renderer and this hydration are from different releases. Pages render fresh (correct, without ` +
|
|
32
|
+
`adopting the server's markup).`,
|
|
33
|
+
'Update both together.',
|
|
34
|
+
],
|
|
35
|
+
'hydration-slots': () => [
|
|
36
|
+
'this page was server-rendered with light-DOM slots, which hydration reads through `hydrateSlots`. The server markup is left as served.',
|
|
37
|
+
'Wire it: `wire([renderer, hydration, slots, hydrateSlots])`.',
|
|
38
|
+
],
|
|
39
|
+
'hydration-no-renderer': () => ['there is no renderer to hydrate.', 'Wire it after the renderer: `wire([renderer, hydration])`.'],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* **The renderer's binding kinds and child-part modes** — numbers both `renderer.ts` and the separately bundled
|
|
44
|
+
* `hydration.ts` read, from ONE source (each bundle inlines them; they are literals in the output). A change here
|
|
45
|
+
* changes the hand-off's protocol: bump `$V` in `renderer.ts`.
|
|
46
|
+
*/
|
|
47
|
+
/** A binding's kind, resolved once per template. */
|
|
48
|
+
const IGNORED = 0; // consumed, nothing rendered: inside a comment, the later values of a multi-part attribute
|
|
49
|
+
const SOLE = 2; // its element's only content: no anchor in the template, the first commit writes `textContent`
|
|
50
|
+
const ATTR = 3;
|
|
51
|
+
const PROPERTY = 4;
|
|
52
|
+
const BOOLEAN = 5;
|
|
53
|
+
/** `EVENT` through `ADOPT` hold a `Slot` record in their node slot — one range test, in `instantiate` and `commit`. */
|
|
54
|
+
const EVENT = 6;
|
|
55
|
+
/** An element-position expression: a ref, or a value that applies itself (`_$apply$`). */
|
|
56
|
+
const REF = 7;
|
|
57
|
+
/** An element-position expression ON a `<select>`: a value applying itself there (a spread) waits for `flush`. */
|
|
58
|
+
const SELECT_REF = 8;
|
|
59
|
+
/** `.name` on a custom element — see `adoptProperty` in shared-utils. */
|
|
60
|
+
const ADOPT = 9;
|
|
61
|
+
/**
|
|
62
|
+
* Kinds from here on re-assert on EVERY render, so the update loop never skips them as unchanged:
|
|
63
|
+
* `!name` writes from the live DOM's point of view (a sibling radio's click unchecks this one with no
|
|
64
|
+
* event on it), and a `<select>`'s selection is re-applied after its options exist — see `flush`.
|
|
65
|
+
*/
|
|
66
|
+
const LIVE = 10;
|
|
67
|
+
/** A `<select>`'s selection — `value` or `selectedIndex` (`isSelection`) — written when the pass ends: see `flush`. */
|
|
68
|
+
const SELECT = 11;
|
|
69
|
+
/** A `<select>`'s `selectedIndex` — the rare spelling of its selection, queued as `SELECT` is (see `flush`). */
|
|
70
|
+
const SELECT_INDEX = 14;
|
|
71
|
+
const TEXT = 1;
|
|
72
|
+
const TEMPLATE = 2;
|
|
73
|
+
const LIST = 3;
|
|
74
|
+
const NODE = 4;
|
|
75
|
+
/**
|
|
76
|
+
* **The hydration hand-off's positions** — the renderer sets `$H` to an array at `connect`, and `hydration.ts` reads
|
|
77
|
+
* it by these. An array, not an object of named fields: the names cost every app bytes (−35 B gzipped measured,
|
|
78
|
+
* 2026-10-02), and these constants inline to literals in both bundles. The first two are FROZEN across protocols — a
|
|
79
|
+
* hydration from another release reads the protocol at 0 and, on a mismatch, can still install its clearing hook at 1.
|
|
80
|
+
* Any other change here is a protocol change: bump `PROTOCOL`.
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* **The protocol of this package's first-party seams** — the renderer's hand-off (`$H`, at `HANDOFF_PROTOCOL`) and slots'
|
|
84
|
+
* capture (`_$capture$`), both read by hydration. One number for both: the three ship in one package, and a change to
|
|
85
|
+
* either seam bumps it. A hydration that meets another number declines (the page renders fresh) and says so.
|
|
86
|
+
*/
|
|
87
|
+
const PROTOCOL = 1;
|
|
88
|
+
/** The protocol number — FROZEN at 0. */
|
|
89
|
+
const HANDOFF_PROTOCOL = 0;
|
|
90
|
+
/** The first-render hook's setter — FROZEN at 1. */
|
|
91
|
+
const HANDOFF_ADOPTER = 1;
|
|
92
|
+
const HANDOFF_GET_TEMPLATE = 2;
|
|
93
|
+
const HANDOFF_CHILD_PART = 3;
|
|
94
|
+
const HANDOFF_INSTANCE = 4;
|
|
95
|
+
const HANDOFF_SLOT = 5;
|
|
96
|
+
const HANDOFF_UNSET = 6;
|
|
97
|
+
const HANDOFF_UPGRADED = 7;
|
|
98
|
+
const HANDOFF_COMMIT = 8;
|
|
99
|
+
const HANDOFF_COMMIT_AS = 9;
|
|
100
|
+
const HANDOFF_HOOK_UP = 10;
|
|
101
|
+
const HANDOFF_RESOLVE = 11;
|
|
102
|
+
const HANDOFF_ROOTS = 12;
|
|
103
|
+
const HANDOFF_TO_TEXT = 13;
|
|
104
|
+
const HANDOFF_REMOVAL_WORK = 14;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* **`@verajs/renderer/hydration` — adopt server-rendered DOM.** `wire([renderer, hydration])`.
|
|
108
|
+
*
|
|
109
|
+
* One renderer: this module plugs into it through the hand-off the renderer sets at `connect` (`$H` — a separately
|
|
110
|
+
* bundled module cannot import the renderer's internals, and production mangles their names), and asks to be told of
|
|
111
|
+
* every container's FIRST render that finds children already there. It adopts them as the server's output of the same
|
|
112
|
+
* template: node identity kept, listeners attached, updates mutating the adopted nodes. Server HTML carries no framework
|
|
113
|
+
* comments; the client puts in the anchors it needs.
|
|
114
|
+
*
|
|
115
|
+
* **Queue, then run.** ONE walk matches the container — statics byte for byte, element names, text boundaries, nested
|
|
116
|
+
* templates and lists — doing only what runs no user code as it goes: splitting text and putting in the renderer's
|
|
117
|
+
* comment anchors. Everything that runs user code — a binding's commit (a setter, a listener, an attribute write a
|
|
118
|
+
* component observes), an instance hook, an applier, a `'value'` handler, a node placed into the page — is QUEUED, in
|
|
119
|
+
* walk order, which is client order. Only when the whole container matches does the queue run. So a container that does
|
|
120
|
+
* not match is rendered fresh with NOTHING of the client's run against the server's nodes: no setter, no `create`, no
|
|
121
|
+
* ref, no listener, no applier. Two notes: each text value is converted once, during the walk, as a client render
|
|
122
|
+
* converts it (an object's `toString` is the one user code that runs before the decision); and a user's own
|
|
123
|
+
* MutationObserver on a container that then mismatches sees text splits and comments put in, then the clear. (The one
|
|
124
|
+
* exception is the platform's: a server custom element already defined upgrades on parse and runs its own setup before
|
|
125
|
+
* any hydration starts — `tests/hydration-walk.test.mjs` pins it.)
|
|
126
|
+
*
|
|
127
|
+
* User code that THROWS while the queue runs leaves the container partly committed — exactly as a client render that
|
|
128
|
+
* throws midway does; it is the render's error, not a hydration one.
|
|
129
|
+
*
|
|
130
|
+
* **The renderer's commit never asks "am I hydrating?".** Where the server's attribute already says what the client
|
|
131
|
+
* would write, the walk SEEDS the binding's committed value and calls the base commit — which runs every sink check as
|
|
132
|
+
* always (a `javascript:` URL is refused and removed exactly as on a fresh render) and then skips the write on its own
|
|
133
|
+
* `value === committed` fast path. Form state is recorded, never written: the server's default, and anything the user
|
|
134
|
+
* typed before the script arrived, stand.
|
|
135
|
+
*
|
|
136
|
+
* A mismatch — or a renderer speaking another hand-off protocol — never breaks the page: the container renders fresh,
|
|
137
|
+
* and the console says so once per cause.
|
|
138
|
+
*/
|
|
139
|
+
/** What a handler returns to keep the adopted nodes instead of claiming (`claimed`). */
|
|
140
|
+
const ADOPTED = 2;
|
|
141
|
+
let H;
|
|
142
|
+
let registry;
|
|
143
|
+
/** The piece, read per adoption (it may be wired after this module); the served host being adopted, else `null`. */
|
|
144
|
+
let piece;
|
|
145
|
+
let served = null;
|
|
146
|
+
/**
|
|
147
|
+
* Walking a FILLED slot's element: a detached copy of the canonical one, as the client keeps it — the server never wrote
|
|
148
|
+
* it, so its fallback's values are committed fresh into the copy rather than claimed (see `fresh*`).
|
|
149
|
+
*/
|
|
150
|
+
let fresh = false;
|
|
151
|
+
let untracked;
|
|
152
|
+
/* ── the warning: the only signal a fallback gives, once per cause ───────────────────────────────── */
|
|
153
|
+
/**
|
|
154
|
+
* Said once per KIND of cause per hydration pass, in every build — keyed by the kind (`text`, `element`…), not the
|
|
155
|
+
* detail: a page of 500 containers that each differ in their text says it once, naming the first. The record clears at
|
|
156
|
+
* the next microtask, so a later pass that meets the kind again says it. Not `warnOnce` (Brian's ruling, 2026-11-12): that memory
|
|
157
|
+
* is once per page and never forgets, and this one is once per PASS.
|
|
158
|
+
*/
|
|
159
|
+
const causesThisPass = new Set();
|
|
160
|
+
const warn = (cause, message, container) => {
|
|
161
|
+
if (causesThisPass.has(cause))
|
|
162
|
+
return;
|
|
163
|
+
if (causesThisPass.size === 0)
|
|
164
|
+
queueMicrotask(() => causesThisPass.clear());
|
|
165
|
+
causesThisPass.add(cause);
|
|
166
|
+
/** The container itself rides along: a live element in devtools — hover to highlight it, click to reveal it. */
|
|
167
|
+
if (container === undefined)
|
|
168
|
+
console.warn(message);
|
|
169
|
+
else
|
|
170
|
+
console.warn('%s', message, container);
|
|
171
|
+
};
|
|
172
|
+
/* ── the walk's state, per hydration ─────────────────────────────────────────────────────────────── */
|
|
173
|
+
/**
|
|
174
|
+
* **The work that runs user code, held until the whole container matches** — flat tuples of six, in walk order (client
|
|
175
|
+
* order): `[op, a, b, c, d, e]`. ONE array for every adoption, reused — allocating one per adoption cost a measured
|
|
176
|
+
* share of hydration in collection alone. Each adoption owns the range from where the queue stood when it began, and
|
|
177
|
+
* truncates back to that when it ends: a nested adoption (a queued setter rendering into its own element) pushes past
|
|
178
|
+
* the outer's range and is gone again before the outer goes on.
|
|
179
|
+
*/
|
|
180
|
+
const queue = [];
|
|
181
|
+
const COMMIT = 0;
|
|
182
|
+
const APPLY = 1;
|
|
183
|
+
const HOOK = 2;
|
|
184
|
+
const CHILD = 3;
|
|
185
|
+
const HANDLE = 4;
|
|
186
|
+
const INSERT = 5;
|
|
187
|
+
const later = (op, a, b, c, d, e) => {
|
|
188
|
+
queue.push(op, a, b, c, d, e);
|
|
189
|
+
};
|
|
190
|
+
/** The first place the two renders disagreed — for the warning. */
|
|
191
|
+
let why = '';
|
|
192
|
+
/** Why adoption stops: thrown, caught at the container, never escapes. */
|
|
193
|
+
const MISMATCH = {};
|
|
194
|
+
/** The KIND of the first disagreement — what the warning is said once per: `text`, `value`, `element`, `extra`. */
|
|
195
|
+
let kind = '';
|
|
196
|
+
const mismatch = (cause, at, reason) => {
|
|
197
|
+
kind = cause;
|
|
198
|
+
why = reason ? reason() : `${cause}: found ${describe(at)}`;
|
|
199
|
+
throw MISMATCH;
|
|
200
|
+
};
|
|
201
|
+
/** A live node as a person would name it. */
|
|
202
|
+
const describe = (node) => node === null
|
|
203
|
+
? 'nothing'
|
|
204
|
+
: node.nodeType === 1
|
|
205
|
+
? `<${node.localName}>`
|
|
206
|
+
: node.nodeType === 3
|
|
207
|
+
? `the text ${JSON.stringify(node.data.slice(0, 30))}`
|
|
208
|
+
: node.nodeType === 8
|
|
209
|
+
? 'a comment'
|
|
210
|
+
: `a ${node.nodeName} node`;
|
|
211
|
+
/** A value's text — the base's own conversion, once, as a client render converts it. */
|
|
212
|
+
const textOf = (value) => H[HANDOFF_TO_TEXT](value);
|
|
213
|
+
const isTemplateResult = (value) => value.strings !== undefined;
|
|
214
|
+
/** A template's opening markup, as its author wrote it — something to search the code for (development only). */
|
|
215
|
+
const opening = (result) => {
|
|
216
|
+
const text = result.strings.join('${…}').replace(/\s+/g, ' ').trim();
|
|
217
|
+
return `\`${text.length > 60 ? `${text.slice(0, 60)}…` : text}\``;
|
|
218
|
+
};
|
|
219
|
+
/* ── where the walk stands ───────────────────────────────────────────────────────────────────────── */
|
|
220
|
+
/**
|
|
221
|
+
* **Which bindings each canonical node carries**, built once per template the first time one is adopted: every
|
|
222
|
+
* binding's node located through its path on the pristine canonical content, as `instantiate` locates it on a clone.
|
|
223
|
+
*/
|
|
224
|
+
const plans = new WeakMap();
|
|
225
|
+
const planOf = (template) => {
|
|
226
|
+
let plan = plans.get(template);
|
|
227
|
+
if (plan === undefined) {
|
|
228
|
+
plans.set(template, (plan = new Map()));
|
|
229
|
+
const kinds = template.$K;
|
|
230
|
+
for (let i = 0; i < kinds.length; i++) {
|
|
231
|
+
if (kinds[i] === IGNORED)
|
|
232
|
+
continue;
|
|
233
|
+
let node = template.$R;
|
|
234
|
+
for (const step of template.$P[i]) {
|
|
235
|
+
node = node.firstChild;
|
|
236
|
+
for (let hops = step; hops > 0; hops--)
|
|
237
|
+
node = node.nextSibling;
|
|
238
|
+
}
|
|
239
|
+
const owned = plan.get(node);
|
|
240
|
+
if (owned === undefined)
|
|
241
|
+
plan.set(node, [i]);
|
|
242
|
+
else
|
|
243
|
+
owned.push(i);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return plan;
|
|
247
|
+
};
|
|
248
|
+
/** A comment carries no content: adoption neither matches nor requires one, at a node boundary. */
|
|
249
|
+
/**
|
|
250
|
+
* **The node after `node` on this walk** — its next sibling, until `hydrate-slots` is wired: then the piece's own step,
|
|
251
|
+
* which on a light host's walk gives the next light child. Only then can a light cursor exist, so a page without the
|
|
252
|
+
* piece walks exactly as before it (a light check on every step cost Chrome's hydrate phase ~2%, measured 2026-10-07,
|
|
253
|
+
* on pages with no light hosts at all) and carries none of its code.
|
|
254
|
+
*/
|
|
255
|
+
let next = (_cursor, node) => node.nextSibling;
|
|
256
|
+
const passComments = (cursor) => {
|
|
257
|
+
while (cursor.offset === 0 && cursor.node !== null && cursor.node.nodeType === 8)
|
|
258
|
+
cursor.node = next(cursor, cursor.node);
|
|
259
|
+
};
|
|
260
|
+
/** Puts the cursor on a node boundary (splitting the text it stands in) and returns the node there. */
|
|
261
|
+
const boundary = (cursor) => {
|
|
262
|
+
if (cursor.offset > 0) {
|
|
263
|
+
cursor.node = cursor.node.splitText(cursor.offset);
|
|
264
|
+
cursor.offset = 0;
|
|
265
|
+
}
|
|
266
|
+
return cursor.node;
|
|
267
|
+
};
|
|
268
|
+
/** Inserts at the cursor — the walk inserts only text and the renderer's comment anchors, which run no user code. */
|
|
269
|
+
const plainInsert = (cursor, node) => cursor.parent.insertBefore(node, boundary(cursor));
|
|
270
|
+
/** Like `next`: the plain insert until `hydrate-slots` is wired, then light-aware — a run's anchors go to the piece. */
|
|
271
|
+
let insertHere = plainInsert;
|
|
272
|
+
const lightInsert = (cursor, node) => (cursor.light === undefined ? plainInsert(cursor, node) : piece[7](cursor, node));
|
|
273
|
+
/** Consumes exactly `text`, a static; anything else is a mismatch. Reads only. */
|
|
274
|
+
const expectText = (cursor, text) => {
|
|
275
|
+
while (text !== '') {
|
|
276
|
+
passComments(cursor);
|
|
277
|
+
const node = cursor.node;
|
|
278
|
+
if (node === null || node.nodeType !== 3)
|
|
279
|
+
return mismatch('text', node, true && (() => `expected the text ${JSON.stringify(text)} and found ${describe(node)}`));
|
|
280
|
+
const data = node.data;
|
|
281
|
+
const take = Math.min(data.length - cursor.offset, text.length);
|
|
282
|
+
if (data.slice(cursor.offset, cursor.offset + take) !== text.slice(0, take))
|
|
283
|
+
return mismatch('text', node, true && (() => `expected the text ${JSON.stringify(text)} and found ${JSON.stringify(data.slice(cursor.offset, cursor.offset + 30))}`));
|
|
284
|
+
text = text.slice(take);
|
|
285
|
+
cursor.offset += take;
|
|
286
|
+
if (cursor.offset === data.length) {
|
|
287
|
+
cursor.node = next(cursor, node);
|
|
288
|
+
cursor.offset = 0;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
/**
|
|
293
|
+
* Claims a text node holding exactly `text`, a value — split out of the run it arrived in. `''` has no server
|
|
294
|
+
* counterpart: a fresh empty text node is put in as its anchor.
|
|
295
|
+
*/
|
|
296
|
+
const claimText = (cursor, text) => {
|
|
297
|
+
if (text === '') {
|
|
298
|
+
const anchor = cursor.parent.ownerDocument.createTextNode('');
|
|
299
|
+
insertHere(cursor, anchor);
|
|
300
|
+
return anchor;
|
|
301
|
+
}
|
|
302
|
+
passComments(cursor);
|
|
303
|
+
/** Split first: a text split runs no user code, so the walk may make it before the container is decided. */
|
|
304
|
+
const node = boundary(cursor);
|
|
305
|
+
if (node === null || node.nodeType !== 3)
|
|
306
|
+
return mismatch('value', node, true && (() => `expected a text node holding an interpolated value and found ${describe(node)}`));
|
|
307
|
+
const data = node.data;
|
|
308
|
+
if (!data.startsWith(text))
|
|
309
|
+
return mismatch('value', node, true && (() => `an interpolated value reads ${JSON.stringify(text)} here and the markup says ${JSON.stringify(data.slice(0, text.length))} ` +
|
|
310
|
+
`— a value that stringifies differently on the server (a Date? locale formatting?) disagrees here`));
|
|
311
|
+
/** The rest of a split run is the same light child's, so it stands next; otherwise the walk moves on. */
|
|
312
|
+
cursor.node = data.length > text.length ? node.splitText(text.length) : next(cursor, node);
|
|
313
|
+
return node;
|
|
314
|
+
};
|
|
315
|
+
/** Claims the element named `name` at the cursor. Reads only. */
|
|
316
|
+
const claimElement = (cursor, name) => {
|
|
317
|
+
passComments(cursor);
|
|
318
|
+
const node = cursor.offset > 0 ? null : cursor.node;
|
|
319
|
+
if (node === null || node.nodeType !== 1 || node.localName !== name)
|
|
320
|
+
return mismatch('element', cursor.offset > 0 ? cursor.node : node, true && (() => `expected <${name}> and found ${cursor.offset > 0 ? describe(cursor.node) : describe(node)}`));
|
|
321
|
+
cursor.node = next(cursor, node);
|
|
322
|
+
return node;
|
|
323
|
+
};
|
|
324
|
+
/** The walk has consumed everything the template describes here: whatever is left is a mismatch. */
|
|
325
|
+
const finish = (cursor, top = false) => {
|
|
326
|
+
passComments(cursor);
|
|
327
|
+
if (cursor.offset > 0 || (cursor.node !== null && (cursor.light === undefined || cursor.at < cursor.end)))
|
|
328
|
+
mismatch('extra', cursor.node, true && (() => !top && cursor.parent.nodeType === 1
|
|
329
|
+
? `<${cursor.parent.localName}> contains ${describe(cursor.node)}, which the template does not describe`
|
|
330
|
+
: `${describe(cursor.node)} follows everything the template describes`));
|
|
331
|
+
};
|
|
332
|
+
/** A filled slot's detached copy, from the piece — or `null`, and the element is claimed live. */
|
|
333
|
+
const copyAt = (canonical, cursor) => (served === null || fresh ? null : piece[2](canonical, cursor, served));
|
|
334
|
+
/** A filled slot's copy, adopted: everything inside it is committed fresh, as the client renders a slot it keeps. */
|
|
335
|
+
const adoptCopy = (canonical, copy, into, owned) => {
|
|
336
|
+
fresh = true;
|
|
337
|
+
try {
|
|
338
|
+
adoptElement(canonical, copy, into, owned);
|
|
339
|
+
}
|
|
340
|
+
finally {
|
|
341
|
+
fresh = false;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* **A binding the server never wrote** (inside a filled slot's detached copy): set up exactly as a client render's clone
|
|
346
|
+
* sets it — its node, and `''` for a child position (the empty text the template parsed it as) or UNSET — and queued, so
|
|
347
|
+
* the renderer's own first commit does the rest, text or template alike.
|
|
348
|
+
*/
|
|
349
|
+
const freshCommit = (into, i, node) => {
|
|
350
|
+
into.bindings[i * 2] = node;
|
|
351
|
+
into.bindings[i * 2 + 1] = into.template.$K[i] === SOLE ? H[HANDOFF_UNSET] : '';
|
|
352
|
+
later(COMMIT, into.template, into.bindings, i, into.template.$K[i], into.values);
|
|
353
|
+
};
|
|
354
|
+
/** Adopts the canonical siblings from `canonical` on, against the live cursor. */
|
|
355
|
+
const walk = (canonical, cursor, into) => {
|
|
356
|
+
for (let node = canonical; node !== null; node = node.nextSibling) {
|
|
357
|
+
const type = node.nodeType;
|
|
358
|
+
if (type === 8)
|
|
359
|
+
continue;
|
|
360
|
+
const owned = into.plan.get(node);
|
|
361
|
+
if (type === 3) {
|
|
362
|
+
if (owned !== undefined)
|
|
363
|
+
adoptChild(into, owned[0], cursor);
|
|
364
|
+
else
|
|
365
|
+
expectText(cursor, node.data);
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
const copy = copyAt(node, cursor);
|
|
369
|
+
if (copy === null)
|
|
370
|
+
adoptElement(node, claimElement(cursor, node.localName), into, owned);
|
|
371
|
+
else
|
|
372
|
+
adoptCopy(node, copy, into, owned);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
/**
|
|
377
|
+
* A control's state the user may have changed before the script arrived: recorded, never written — through `.name` or
|
|
378
|
+
* `!name` alike. Only this: any other `!name` on a plain element is the client's to write (`!indeterminate` has no
|
|
379
|
+
* markup at all), and on a COMPONENT `!name` is never recorded — it is delivered (the commit's `adoptProperty`). No
|
|
380
|
+
* first-party component holds such state before its script runs (`vera-select`'s value lives in its internals and its
|
|
381
|
+
* template has no native control); a choice made between a child's hydration and its parent's commit is overruled by
|
|
382
|
+
* the parent's `!name`, which is what `!` means on every client render.
|
|
383
|
+
*/
|
|
384
|
+
const FORM_STATE = /^(?:value|checked|selected|open)$/;
|
|
385
|
+
/** A value whose conversion to text runs no user code. */
|
|
386
|
+
const primitive = (value) => value === null || (typeof value !== 'object' && typeof value !== 'function');
|
|
387
|
+
/**
|
|
388
|
+
* **One element binding, adopted** — seeded now (DOM reads only), its commit QUEUED. The seedable kinds are ATTR and BOOLEAN — ONLY those: their server DOM can
|
|
389
|
+
* be compared to the client value without running user code, and seeding anything else (an EVENT's handler, a
|
|
390
|
+
* setter) would skip work the client must do. Seeded with the CLIENT value, and only on exact equality.
|
|
391
|
+
*/
|
|
392
|
+
const commitBinding = (into, i, kind, live) => {
|
|
393
|
+
const { template, bindings, values } = into;
|
|
394
|
+
const slot = i * 2;
|
|
395
|
+
const name = template.$N[i];
|
|
396
|
+
bindings[slot] = kind >= EVENT && kind <= ADOPT ? new H[HANDOFF_SLOT](live) : live;
|
|
397
|
+
bindings[slot + 1] = H[HANDOFF_UNSET];
|
|
398
|
+
const raw = values[i];
|
|
399
|
+
/** Recorded, never written: a selection, a `!name`, a form control's value. */
|
|
400
|
+
if (kind === SELECT || kind === SELECT_INDEX || ((kind === LIVE || kind === PROPERTY) && FORM_STATE.test(name))) {
|
|
401
|
+
bindings[slot + 1] = raw;
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
/** An element-position value applying itself is told it is adopting — a form control the user typed in stands. */
|
|
405
|
+
if ((kind === REF || kind === SELECT_REF) && raw != null && typeof raw._$apply$ === 'function') {
|
|
406
|
+
bindings[slot + 1] = raw;
|
|
407
|
+
later(APPLY, raw, live, bindings[slot]);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (kind === ATTR) {
|
|
411
|
+
const parts = template.$J[i];
|
|
412
|
+
let text;
|
|
413
|
+
if (parts === null)
|
|
414
|
+
text = primitive(raw) ? (raw == null ? null : H[HANDOFF_TO_TEXT](raw)) : undefined;
|
|
415
|
+
else {
|
|
416
|
+
text = parts[0];
|
|
417
|
+
for (let p = 1; p < parts.length; p++) {
|
|
418
|
+
const v = values[i + p - 1];
|
|
419
|
+
if (!primitive(v)) {
|
|
420
|
+
text = undefined;
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
text += H[HANDOFF_TO_TEXT](v) + parts[p];
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (text !== undefined) {
|
|
427
|
+
const server = live.getAttribute(name);
|
|
428
|
+
/**
|
|
429
|
+
* Equal — both texts, or both absent: the client value itself is seeded, exactly what the base's commit would
|
|
430
|
+
* have stored. A binding naming no URL then has NOTHING to commit (its commit would return at `value ===
|
|
431
|
+
* committed`), so nothing is queued; a URL-bearing one always commits, so a `javascript:` value the server wrote
|
|
432
|
+
* is refused through the base, as on a fresh render.
|
|
433
|
+
*/
|
|
434
|
+
if (server === text) {
|
|
435
|
+
bindings[slot + 1] = parts === null ? raw : text;
|
|
436
|
+
if (template.$L[i] === 0)
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
else if (text === null)
|
|
440
|
+
bindings[slot + 1] = server;
|
|
441
|
+
/** ↑ The client writes nothing and the server wrote something: a non-UNSET seed so the base REMOVES it. */
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
else if (kind === BOOLEAN && live.hasAttribute(name) === !!raw) {
|
|
445
|
+
bindings[slot + 1] = raw;
|
|
446
|
+
if (template.$L[i] === 0)
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
later(COMMIT, template, bindings, i, kind, values);
|
|
450
|
+
};
|
|
451
|
+
/**
|
|
452
|
+
* An element's own bindings commit when the walk reaches it — before its content, in document pre-order, as a client
|
|
453
|
+
* render commits them (queued) — then its content is adopted: as one SOLE value, or as canonical children.
|
|
454
|
+
*/
|
|
455
|
+
const adoptElement = (canonical, live, into, owned) => {
|
|
456
|
+
if (into.found !== null)
|
|
457
|
+
into.found.push(live);
|
|
458
|
+
let sole = -1;
|
|
459
|
+
/** A content property (`.innerHTML`, `.textContent`…) writes this element's children itself — see below. */
|
|
460
|
+
let content = false;
|
|
461
|
+
if (owned !== undefined)
|
|
462
|
+
for (const i of owned) {
|
|
463
|
+
const kind = into.template.$K[i];
|
|
464
|
+
if (kind === SOLE)
|
|
465
|
+
sole = i;
|
|
466
|
+
else {
|
|
467
|
+
if ((kind === PROPERTY || kind === LIVE) && CONTENT_PROPERTY.test(into.template.$N[i]))
|
|
468
|
+
content = true;
|
|
469
|
+
commitBinding(into, i, kind, live);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* A light host the template places content into: walked in light order (`hydrate-slots`), not as its DOM stands —
|
|
474
|
+
* unless a binding writes a RUN directly among those children (a value that is not text): its anchors would have to
|
|
475
|
+
* span the host's slots, so that host falls back as before until runs are adopted in the client's own shape.
|
|
476
|
+
*/
|
|
477
|
+
const inner = piece?.[5](live, canonical, into.plan, into.values) ?? { parent: live, node: live.firstChild, offset: 0 };
|
|
478
|
+
if (sole >= 0)
|
|
479
|
+
adoptSole(into, sole, live, inner);
|
|
480
|
+
/**
|
|
481
|
+
* Content the template does not describe that is not the template's to describe: a `<textarea>`'s server content is
|
|
482
|
+
* its default value, a custom element's children — when the template writes none — are that component's OWN render,
|
|
483
|
+
* which it adopts itself, and a content property's element holds what that binding writes.
|
|
484
|
+
*/ else if (canonical.firstChild === null &&
|
|
485
|
+
(content || live.localName === 'textarea' || live.localName.includes('-') || live.hasAttribute('is')))
|
|
486
|
+
return;
|
|
487
|
+
else
|
|
488
|
+
walk(canonical.firstChild, inner, into);
|
|
489
|
+
finish(inner);
|
|
490
|
+
};
|
|
491
|
+
/** A value is TEXT at a child position when the base would write it as text. */
|
|
492
|
+
const isText = (value) => value != null && typeof value !== 'object';
|
|
493
|
+
/** A CHILD binding: text split to the value, or a part between two markers put in around what it adopts. */
|
|
494
|
+
const adoptChild = (into, i, cursor) => {
|
|
495
|
+
if (fresh) {
|
|
496
|
+
const placeholder = cursor.node;
|
|
497
|
+
cursor.node = placeholder.nextSibling;
|
|
498
|
+
return freshCommit(into, i, placeholder);
|
|
499
|
+
}
|
|
500
|
+
const value = into.values[i];
|
|
501
|
+
if (isText(value)) {
|
|
502
|
+
into.bindings[i * 2] = claimText(cursor, textOf(value));
|
|
503
|
+
into.bindings[i * 2 + 1] = value;
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const part = new H[HANDOFF_CHILD_PART](cursor.parent.ownerDocument.createComment(''), cursor.parent.ownerDocument.createComment(''));
|
|
507
|
+
insertHere(cursor, part.$s);
|
|
508
|
+
adoptValue(part, value, cursor);
|
|
509
|
+
if (part.$e.parentNode === null)
|
|
510
|
+
insertHere(cursor, part.$e);
|
|
511
|
+
into.bindings[i * 2] = part;
|
|
512
|
+
into.bindings[i * 2 + 1] = H[HANDOFF_UPGRADED];
|
|
513
|
+
};
|
|
514
|
+
/** A SOLE binding: its element's one text node, or a part that owns the element (no markers). */
|
|
515
|
+
const adoptSole = (into, i, live, inner) => {
|
|
516
|
+
const value = into.values[i];
|
|
517
|
+
if (fresh)
|
|
518
|
+
return freshCommit(into, i, live);
|
|
519
|
+
if (isText(value)) {
|
|
520
|
+
into.bindings[i * 2] = claimText(inner, textOf(value));
|
|
521
|
+
into.bindings[i * 2 + 1] = value;
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
const part = new H[HANDOFF_CHILD_PART](null, null);
|
|
525
|
+
part.$w = live;
|
|
526
|
+
adoptValue(part, value, inner);
|
|
527
|
+
into.bindings[i * 2] = part;
|
|
528
|
+
into.bindings[i * 2 + 1] = H[HANDOFF_UPGRADED];
|
|
529
|
+
};
|
|
530
|
+
/** Adopts a template's instance at the cursor. */
|
|
531
|
+
const adoptInstance = (result, cursor) => {
|
|
532
|
+
let template = H[HANDOFF_GET_TEMPLATE](result);
|
|
533
|
+
/** Adoption is in place: an extension resolving the template (namespaces) is asked with the LIVE parent. */
|
|
534
|
+
if (template.$X)
|
|
535
|
+
template = H[HANDOFF_RESOLVE](template, cursor.parent);
|
|
536
|
+
const into = {
|
|
537
|
+
template,
|
|
538
|
+
bindings: new Array(template.$K.length * 2 + (template.$X ? 1 : 0)),
|
|
539
|
+
values: result.values,
|
|
540
|
+
plan: planOf(template),
|
|
541
|
+
found: template._$inst$ === undefined ? null : [],
|
|
542
|
+
};
|
|
543
|
+
const root = template.$R;
|
|
544
|
+
const single = root.nodeType === 1;
|
|
545
|
+
/** A single root is claimed here, not by the walk — so a `<slot>` at a template's root is asked here too. */
|
|
546
|
+
const copy = single ? copyAt(root, cursor) : null;
|
|
547
|
+
const adopted = copy ?? (single ? claimElement(cursor, root.localName) : cursor.parent.ownerDocument.createDocumentFragment());
|
|
548
|
+
const instance = new H[HANDOFF_INSTANCE](template, result.strings, adopted, into.bindings);
|
|
549
|
+
/**
|
|
550
|
+
* **Its instance hook meets it before its bindings commit**, as a client instance does — told it was adopted, and
|
|
551
|
+
* handed its elements in template order rather than a root to count from: an adopted root already holds what its
|
|
552
|
+
* nested parts rendered, so counting would land on the wrong element (or, for a fragment, on none). Queued first;
|
|
553
|
+
* the list is complete by the time the queue runs.
|
|
554
|
+
*/
|
|
555
|
+
if (into.found !== null)
|
|
556
|
+
later(HOOK, instance, into.found);
|
|
557
|
+
if (copy !== null)
|
|
558
|
+
adoptCopy(root, copy, into, into.plan.get(root));
|
|
559
|
+
else if (single)
|
|
560
|
+
adoptElement(root, adopted, into, into.plan.get(root));
|
|
561
|
+
else
|
|
562
|
+
walk(root.firstChild, cursor, into);
|
|
563
|
+
return instance;
|
|
564
|
+
};
|
|
565
|
+
/**
|
|
566
|
+
* Adopts `value` into `part` — the same decisions, in the same order, as the server's serializer made them (templates,
|
|
567
|
+
* lists, appliers, nodes, and every other object as its text), so a position adopts as exactly what was written.
|
|
568
|
+
*/
|
|
569
|
+
const adoptValue = (part, value, cursor) => {
|
|
570
|
+
if (value == null)
|
|
571
|
+
return;
|
|
572
|
+
if (typeof value !== 'object') {
|
|
573
|
+
part.$l = claimText(cursor, textOf(value));
|
|
574
|
+
part.$v = value;
|
|
575
|
+
part.$o = TEXT;
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const held = value.$h;
|
|
579
|
+
if (held !== undefined || isTemplateResult(value)) {
|
|
580
|
+
part.$n = adoptInstance(held ?? value, cursor);
|
|
581
|
+
part.$o = TEMPLATE;
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* **A `'value'` handler is asked when the queue runs, with base precedence** (templates first, then the handlers).
|
|
586
|
+
* The server never asks handlers — it wrote the value as the renderer's own types, or as its text — so the walk adopts
|
|
587
|
+
* it as written; a handler that then claims it commits over the part, which clears what was adopted, and everything
|
|
588
|
+
* queued for that content is skipped (`end`). Nothing claims: it stays adopted.
|
|
589
|
+
*/
|
|
590
|
+
const handlers = registry.get('value');
|
|
591
|
+
const at = handlers === undefined ? -1 : queue.length;
|
|
592
|
+
if (handlers !== undefined)
|
|
593
|
+
later(HANDLE, part, value, handlers, 0);
|
|
594
|
+
adoptObject(part, value, cursor);
|
|
595
|
+
if (at >= 0)
|
|
596
|
+
queue[at + 4] = queue.length;
|
|
597
|
+
};
|
|
598
|
+
/** An object value that is not a template: a list, an applier, a node, or its text. */
|
|
599
|
+
const adoptObject = (part, value, cursor) => {
|
|
600
|
+
/** What a LIST is, as the renderer's `$p` decides it: `tests/renderer-list-twins.test.mjs` keeps the two equal. */
|
|
601
|
+
if (Array.isArray(value) || (typeof value[Symbol.iterator] === 'function' && value.nodeType === undefined)) {
|
|
602
|
+
/** Read once: a generator iterates once. */
|
|
603
|
+
const list = Array.isArray(value) ? value : [...value];
|
|
604
|
+
const items = [];
|
|
605
|
+
for (let i = 0; i < list.length; i++)
|
|
606
|
+
items.push(adoptItem(list[i], cursor));
|
|
607
|
+
part.$i = items;
|
|
608
|
+
part.$o = LIST;
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
const applyChild = value._$child$;
|
|
612
|
+
if (applyChild !== undefined) {
|
|
613
|
+
/** What the server wrote for an applier cannot be delimited except as a SOLE element's whole content. */
|
|
614
|
+
if (part.$w != null && cursor.node !== null) {
|
|
615
|
+
part.$o = NODE;
|
|
616
|
+
cursor.node = null;
|
|
617
|
+
cursor.offset = 0;
|
|
618
|
+
}
|
|
619
|
+
if (part.$e !== null && part.$e.parentNode === null)
|
|
620
|
+
insertHere(cursor, part.$e);
|
|
621
|
+
part.$a = applyChild;
|
|
622
|
+
part.$R = cursor.parent;
|
|
623
|
+
later(CHILD, applyChild, value, part);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* A node the server could not have rendered: it goes where it belongs — QUEUED, since placing an element runs its
|
|
628
|
+
* connected callback. A comment holds its place until then: inserted later at a captured `null`, it would land after
|
|
629
|
+
* the anchors the walk appends meanwhile.
|
|
630
|
+
*/
|
|
631
|
+
if (value.nodeType !== undefined) {
|
|
632
|
+
const place = cursor.parent.ownerDocument.createComment('');
|
|
633
|
+
insertHere(cursor, place);
|
|
634
|
+
later(INSERT, cursor.parent, value, place);
|
|
635
|
+
part.$v = value;
|
|
636
|
+
part.$o = NODE;
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
/** Every other object, as the server wrote it: its text, converted once. */
|
|
640
|
+
part.$l = claimText(cursor, textOf(value));
|
|
641
|
+
part.$v = value;
|
|
642
|
+
part.$o = TEXT;
|
|
643
|
+
};
|
|
644
|
+
/** The root the template at this position will build — after an extension resolves it, as the base asks. */
|
|
645
|
+
const rootOf = (result, cursor) => {
|
|
646
|
+
const template = H[HANDOFF_GET_TEMPLATE](result);
|
|
647
|
+
return (template.$X ? H[HANDOFF_RESOLVE](template, cursor.parent) : template).$R;
|
|
648
|
+
};
|
|
649
|
+
/** Adopts one list item — the same shapes the base builds. */
|
|
650
|
+
const adoptItem = (value, cursor) => {
|
|
651
|
+
if (value !== null && typeof value === 'object' && isTemplateResult(value) && rootOf(value, cursor).nodeType === 1) {
|
|
652
|
+
const instance = adoptInstance(value, cursor);
|
|
653
|
+
instance.$k = value.key;
|
|
654
|
+
return instance;
|
|
655
|
+
}
|
|
656
|
+
const part = new H[HANDOFF_CHILD_PART](cursor.parent.ownerDocument.createComment(''), cursor.parent.ownerDocument.createComment(''));
|
|
657
|
+
insertHere(cursor, part.$s);
|
|
658
|
+
adoptValue(part, value, cursor);
|
|
659
|
+
if (part.$e.parentNode === null)
|
|
660
|
+
insertHere(cursor, part.$e);
|
|
661
|
+
part.$k = value?.key;
|
|
662
|
+
return part;
|
|
663
|
+
};
|
|
664
|
+
/**
|
|
665
|
+
* **A handler is asked with the part as a client render would hand it over — empty** — and the server's span stays
|
|
666
|
+
* where it is meanwhile: a claim commits into the empty part and the span is removed after; no claim puts the adopted
|
|
667
|
+
* state back. Copied whole (`{ ...part }`), so no field of the renderer's needs naming here — and so a handler that
|
|
668
|
+
* declines must not write to the part (a field it ADDED would stay).
|
|
669
|
+
*/
|
|
670
|
+
const claimed = (part, value, handlers) => {
|
|
671
|
+
const was = { ...part };
|
|
672
|
+
const span = [];
|
|
673
|
+
for (let node = part.$s === null ? part.$w.firstChild : part.$s.nextSibling; node !== null && node !== part.$e; node = node.nextSibling)
|
|
674
|
+
span.push(node);
|
|
675
|
+
Object.assign(part, new H[HANDOFF_CHILD_PART](part.$s, part.$e), { $w: was.$w, $k: was.$k });
|
|
676
|
+
for (let i = 0; i < handlers.length; i++) {
|
|
677
|
+
const claim = handlers[i](part, value, was);
|
|
678
|
+
/** Kept: the handler took ownership of what the base adopted — the adopted state comes back, its queue still runs. */
|
|
679
|
+
if (claim === ADOPTED)
|
|
680
|
+
break;
|
|
681
|
+
if (claim) {
|
|
682
|
+
for (const node of span)
|
|
683
|
+
node.parentNode?.removeChild(node);
|
|
684
|
+
return true;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
Object.assign(part, was);
|
|
688
|
+
return false;
|
|
689
|
+
};
|
|
690
|
+
/**
|
|
691
|
+
* **The queue runs** — once the whole container matched, inside the renderer's render bracket, in walk order. A handler
|
|
692
|
+
* that claims its value skips everything queued for the content it replaced.
|
|
693
|
+
*/
|
|
694
|
+
const run = (from, to) => {
|
|
695
|
+
const q = queue;
|
|
696
|
+
for (let k = from; k < to; k += 6) {
|
|
697
|
+
const op = q[k];
|
|
698
|
+
if (op === COMMIT)
|
|
699
|
+
H[HANDOFF_COMMIT](q[k + 1], q[k + 2], q[k + 3], q[k + 4], q[k + 5]);
|
|
700
|
+
else if (op === APPLY) {
|
|
701
|
+
H[HANDOFF_REMOVAL_WORK]();
|
|
702
|
+
q[k + 1]._$apply$(q[k + 2], q[k + 3], untracked, true);
|
|
703
|
+
}
|
|
704
|
+
else if (op === HOOK)
|
|
705
|
+
H[HANDOFF_HOOK_UP](q[k + 1], q[k + 2], true);
|
|
706
|
+
else if (op === CHILD) {
|
|
707
|
+
const applyChild = q[k + 1];
|
|
708
|
+
const part = q[k + 3];
|
|
709
|
+
if (applyChild._$detach$ !== undefined)
|
|
710
|
+
H[HANDOFF_REMOVAL_WORK]();
|
|
711
|
+
part.$z = applyChild.call(q[k + 2], part, undefined, true);
|
|
712
|
+
}
|
|
713
|
+
else if (op === HANDLE) {
|
|
714
|
+
if (claimed(q[k + 1], q[k + 2], q[k + 3]))
|
|
715
|
+
k = q[k + 4] - 6;
|
|
716
|
+
}
|
|
717
|
+
else {
|
|
718
|
+
q[k + 1].insertBefore(q[k + 2], q[k + 3]);
|
|
719
|
+
q[k + 3].remove();
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
/** A served light host — the piece reads the rest. */
|
|
724
|
+
const LIGHT_ATTR = 'data-vm-light';
|
|
725
|
+
/* ── the container ───────────────────────────────────────────────────────────────────────────────── */
|
|
726
|
+
/** A container's own SSR stylesheets lead its content and are not part of any template. */
|
|
727
|
+
const isSheet = (node) => node !== null && node.nodeType === 1 && node.hasAttribute('data-vm-sheet');
|
|
728
|
+
/** Clears a container after a mismatch — every child but its SSR stylesheets. */
|
|
729
|
+
const clearPreservingStyles = (container) => {
|
|
730
|
+
for (let node = container.firstChild; node !== null;) {
|
|
731
|
+
const next = node.nextSibling;
|
|
732
|
+
if (!isSheet(node))
|
|
733
|
+
container.removeChild(node);
|
|
734
|
+
node = next;
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
/**
|
|
738
|
+
* **A container's first render, with children already there** — the renderer asks (the hook set at `HANDOFF_ADOPTER`). Adopts them and answers
|
|
739
|
+
* true, or answers false having cleared them, and the renderer renders fresh.
|
|
740
|
+
*/
|
|
741
|
+
const adopt = (result, container) => {
|
|
742
|
+
if (result === null || typeof result !== 'object' || !isTemplateResult(result))
|
|
743
|
+
return false;
|
|
744
|
+
/**
|
|
745
|
+
* **One walk's state per adoption.** The queue runs user code — a setter that renders into its own element, a component
|
|
746
|
+
* set up synchronously — and that can adopt ANOTHER container in the middle of this one: its walk gets its own state,
|
|
747
|
+
* and this one's is restored when it returns. Shared, a nested walk reset this one's state underneath it.
|
|
748
|
+
*/
|
|
749
|
+
const from = queue.length;
|
|
750
|
+
const saved = [why, kind, served];
|
|
751
|
+
piece = registry._$hydrateSlots$;
|
|
752
|
+
if (piece !== undefined) {
|
|
753
|
+
next = piece[6];
|
|
754
|
+
insertHere = lightInsert;
|
|
755
|
+
}
|
|
756
|
+
try {
|
|
757
|
+
return adoptContainer(result, container, from);
|
|
758
|
+
}
|
|
759
|
+
finally {
|
|
760
|
+
/** Run or dropped, this adoption's range goes — a throw included. */
|
|
761
|
+
queue.length = from;
|
|
762
|
+
[why, kind, served] = saved;
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
const adoptContainer = (result, container, from) => {
|
|
766
|
+
const doc = container.ownerDocument ?? container;
|
|
767
|
+
served = null;
|
|
768
|
+
/** The root range is bounded before the walk: an insert at the root during it (a client-only node) needs the end. */
|
|
769
|
+
let start = null;
|
|
770
|
+
let end = null;
|
|
771
|
+
let part;
|
|
772
|
+
const host = container.nodeType === 1 ? container : null;
|
|
773
|
+
const spec = host === null ? null : host.getAttribute(LIGHT_ATTR);
|
|
774
|
+
/**
|
|
775
|
+
* **A light component the server did not render is rendered, not hydrated** — and keeps its children. With slots
|
|
776
|
+
* wired, the server states the light tree on EVERY component host it renders, so a custom element without that
|
|
777
|
+
* statement is client-made (by a template, or `createElement`): its children are light content, not server output, so
|
|
778
|
+
* the walk declines without clearing them, and slots distributes them. (So slots must be wired on both sides: a host a
|
|
779
|
+
* server rendered without it carries no statement, and is rendered on the client.)
|
|
780
|
+
*/
|
|
781
|
+
if (spec === null && registry._$capture$ !== undefined && host?.localName.includes('-'))
|
|
782
|
+
return false;
|
|
783
|
+
try {
|
|
784
|
+
/** A statement with runs needs the piece; one with none (`N:`) states no light content — its slots walk as elements. */
|
|
785
|
+
if (spec?.endsWith(':') && piece === undefined)
|
|
786
|
+
host.removeAttribute(LIGHT_ATTR);
|
|
787
|
+
else if (spec !== null) {
|
|
788
|
+
/** No piece to read it: thrown before anything is touched, so the server's markup stands exactly as served. */
|
|
789
|
+
if (piece === undefined)
|
|
790
|
+
throw new Error(diagnostic('hydration', 'no hydrateSlots', 'hydration-slots', true && PROSE['hydration-slots']()));
|
|
791
|
+
if (piece[0] !== PROTOCOL)
|
|
792
|
+
mismatch('protocol', container, () => `hydrateSlots protocol ${piece[0]}, expected ${PROTOCOL}`);
|
|
793
|
+
served = piece[1](container, mismatch);
|
|
794
|
+
}
|
|
795
|
+
let first = container.firstChild;
|
|
796
|
+
while (isSheet(first) || (first !== null && first === served?.carrier))
|
|
797
|
+
first = first.nextSibling;
|
|
798
|
+
/**
|
|
799
|
+
* **Stylesheets alone are not server output** — a client-made shadow root holds one before its first render whenever
|
|
800
|
+
* `styles` writes a `<style>` (string styles, or no `adoptedStyleSheets`). Nothing to adopt: rendered fresh after
|
|
801
|
+
* them, without a fallback warning that would blame the server for a page it never rendered.
|
|
802
|
+
*/
|
|
803
|
+
if (first === null && served === null)
|
|
804
|
+
return false;
|
|
805
|
+
start = doc.createComment('');
|
|
806
|
+
container.insertBefore(start, first);
|
|
807
|
+
end = container.appendChild(doc.createComment(''));
|
|
808
|
+
part = new H[HANDOFF_CHILD_PART](start, end);
|
|
809
|
+
const cursor = { parent: container, node: first, offset: 0 };
|
|
810
|
+
adoptValue(part, result, cursor);
|
|
811
|
+
finish(cursor, true);
|
|
812
|
+
piece?.[3](served);
|
|
813
|
+
}
|
|
814
|
+
catch (error) {
|
|
815
|
+
start?.remove();
|
|
816
|
+
end?.remove();
|
|
817
|
+
if (error !== MISMATCH)
|
|
818
|
+
throw error;
|
|
819
|
+
/** A served host keeps its light content: captured into holding before the clear, distributed by the fresh render. */
|
|
820
|
+
piece?.[4](served);
|
|
821
|
+
/** Every build names the kind and the first node that disagreed; development adds the whole story, production the link. */
|
|
822
|
+
warn(kind, diagnostic('hydration', why, 'hydration-fallback', true && PROSE['hydration-fallback'](opening(result))), container);
|
|
823
|
+
/** The queue is dropped unrun; the clear takes the walk's text splits and comments with the server's nodes. */
|
|
824
|
+
clearPreservingStyles(container);
|
|
825
|
+
return false;
|
|
826
|
+
}
|
|
827
|
+
/** Matched: the queue runs, inside the renderer's own render bracket (a stand-in part) — refs at its end, as always. */
|
|
828
|
+
const to = queue.length;
|
|
829
|
+
H[HANDOFF_COMMIT_AS](container, { $p: () => run(from, to) }, undefined, container);
|
|
830
|
+
H[HANDOFF_ROOTS].set(container, part);
|
|
831
|
+
return true;
|
|
832
|
+
};
|
|
833
|
+
/** `wire([renderer, hydration])` — after the renderer, whose `connect` sets the hand-off. */
|
|
834
|
+
const hydration = (given) => {
|
|
835
|
+
const handoff = given.$H;
|
|
836
|
+
if (handoff === undefined) {
|
|
837
|
+
console.warn(diagnostic('hydration', 'no renderer', 'hydration-no-renderer', true && PROSE['hydration-no-renderer']()));
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* **Another release's renderer: the page still works, rendered fresh** (Brian, 2026-10-02). The protocol and adopter
|
|
842
|
+
* positions are the hand-off's FROZEN pair — the same in every protocol — so even a mismatched renderer can be asked to clear a
|
|
843
|
+
* container's server markup before its first render; nothing else of the hand-off is read. Without that, the base
|
|
844
|
+
* renderer keeps what a container holds and the server's markup would stand beside the client's.
|
|
845
|
+
*/
|
|
846
|
+
if (handoff[HANDOFF_PROTOCOL] !== PROTOCOL) {
|
|
847
|
+
const subject = `protocol ${handoff[HANDOFF_PROTOCOL]}, expected ${PROTOCOL}`;
|
|
848
|
+
warn('protocol', diagnostic('hydration', subject, 'hydration-protocol', true && PROSE['hydration-protocol']()));
|
|
849
|
+
handoff[HANDOFF_ADOPTER]((_result, container) => {
|
|
850
|
+
clearPreservingStyles(container);
|
|
851
|
+
return false;
|
|
852
|
+
});
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
H = handoff;
|
|
856
|
+
registry = given;
|
|
857
|
+
/** Slots, wired after or before this module, defers a served host's capture to the adoption (`_$capture$`). */
|
|
858
|
+
given._$hydrating$ = true;
|
|
859
|
+
untracked = given.$t ?? ((fn, ...args) => fn(...args));
|
|
860
|
+
handoff[HANDOFF_ADOPTER](adopt);
|
|
861
|
+
};
|
|
862
|
+
|
|
863
|
+
export { hydration };
|
|
864
|
+
//# sourceMappingURL=vera-renderer-hydration.js.map
|