@verajs/renderer 0.1.5 → 0.2.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 +975 -27
- package/dist/development/vera-renderer-hydrate.d.ts +32 -13
- package/dist/development/vera-renderer-hydrate.d.ts.map +1 -1
- package/dist/development/vera-renderer-hydrate.js +1786 -231
- package/dist/development/vera-renderer-hydrate.js.map +1 -1
- package/dist/development/vera-renderer-keyed.d.ts +17 -0
- package/dist/development/vera-renderer-keyed.d.ts.map +1 -0
- package/dist/development/vera-renderer-keyed.js +322 -0
- package/dist/development/vera-renderer-keyed.js.map +1 -0
- package/dist/development/vera-renderer-profiler.d.ts +57 -17
- package/dist/development/vera-renderer-profiler.d.ts.map +1 -1
- package/dist/development/vera-renderer-profiler.js +1419 -206
- package/dist/development/vera-renderer-profiler.js.map +1 -1
- package/dist/development/vera-renderer-slots.d.ts +103 -0
- package/dist/development/vera-renderer-slots.d.ts.map +1 -0
- package/dist/development/vera-renderer-slots.js +1387 -0
- package/dist/development/vera-renderer-slots.js.map +1 -0
- package/dist/development/vera-renderer-spread.d.ts +51 -0
- package/dist/development/vera-renderer-spread.d.ts.map +1 -0
- package/dist/development/vera-renderer-spread.js +499 -0
- package/dist/development/vera-renderer-spread.js.map +1 -0
- package/dist/development/vera-renderer-tag.d.ts +61 -0
- package/dist/development/vera-renderer-tag.d.ts.map +1 -0
- package/dist/development/vera-renderer-tag.js +727 -0
- package/dist/development/vera-renderer-tag.js.map +1 -0
- package/dist/development/vera-renderer.d.ts +41 -16
- package/dist/development/vera-renderer.d.ts.map +1 -1
- package/dist/development/vera-renderer.js +1349 -197
- package/dist/development/vera-renderer.js.map +1 -1
- package/dist/vera-renderer-hydrate.min.js +1 -1
- package/dist/vera-renderer-hydrate.min.js.map +1 -1
- package/dist/vera-renderer-keyed.min.js +2 -0
- package/dist/vera-renderer-keyed.min.js.map +1 -0
- package/dist/vera-renderer-slots.min.js +2 -0
- package/dist/vera-renderer-slots.min.js.map +1 -0
- package/dist/vera-renderer-spread.min.js +2 -0
- package/dist/vera-renderer-spread.min.js.map +1 -0
- package/dist/vera-renderer-tag.min.js +2 -0
- package/dist/vera-renderer-tag.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 +24 -1
|
@@ -0,0 +1,1387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Light-DOM slots — the `'slot'` insert the renderer's seam consults. Wire it beside the renderer
|
|
3
|
+
* and every light-rendered template's `<slot name="…">` distributes the host's own children,
|
|
4
|
+
* exactly as shadow DOM would have:
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* import { renderer } from '@verajs/renderer';
|
|
8
|
+
* import { slots } from '@verajs/renderer/slots';
|
|
9
|
+
* wire([renderer, slots]);
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* ```html
|
|
13
|
+
* <my-card><h2 slot="header">Hello</h2>Body text</my-card>
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* **Additive entry** (the `keyed`/`spread` family): imports nothing, reaches the renderer only
|
|
17
|
+
* through the wired seam, safe alongside any renderer entry on a CDN page.
|
|
18
|
+
*
|
|
19
|
+
* **Wire it at the app entry, before anything renders.** The renderer resolves this seam once per
|
|
20
|
+
* TEMPLATE, and templates are cached per call site for the life of the page — so a component that
|
|
21
|
+
* rendered before the wiring keeps a slotless template forever. That is the same contract every
|
|
22
|
+
* insert carries; the alternative (re-checking the registry per instance) would put a lookup on
|
|
23
|
+
* the hot path of every app, slots or not.
|
|
24
|
+
*
|
|
25
|
+
* The semantics contract is the platform's own assignment algorithm: elements go to the slot
|
|
26
|
+
* their `slot` attribute names, text nodes (whitespace included) to the default slot, comments
|
|
27
|
+
* are never slottables (which also keeps the renderer's root marker out of capture), duplicate
|
|
28
|
+
* slot names — first in mount order wins and later ones show fallback, fallback renders only
|
|
29
|
+
* while nothing is assigned and comes back when a slot empties, and capture takes the host's
|
|
30
|
+
* DIRECT children only, so nested components compose.
|
|
31
|
+
*
|
|
32
|
+
* **Post-render additions are NATIVE, and the mechanism is ownership, not position.** The host's
|
|
33
|
+
* childList holds both the user's content and the component's output, and the old rules told them
|
|
34
|
+
* apart by where a node sat — which made bare text unreachable at the tail and a late insertion
|
|
35
|
+
* join its slot out of order. Now the renderer stamps everything it emits at a captured host's
|
|
36
|
+
* top level (a non-enumerable `_$own$` property: `true` for a root part's own output — the
|
|
37
|
+
* structural `_end === null` test, so async commits are covered — and the placing part for
|
|
38
|
+
* content from an outer template). An unstamped top-level addition is therefore knowably the
|
|
39
|
+
* user's: captured with full native semantics, text included, no `slot` attribute required
|
|
40
|
+
* (`slot=""`/`slot="name"` still route). Ordering reads the same fact — placed content extends
|
|
41
|
+
* its part's own group; an unstamped node before the boundary takes its document position ahead
|
|
42
|
+
* of content distributed away, and past it, appends. **Re-slotting keeps that answer**: a node
|
|
43
|
+
* whose `slot` attribute changes has not moved in the light tree, so it rejoins by a rank taken
|
|
44
|
+
* when it was first captured rather than by arrival — otherwise an item toggled into a "pinned"
|
|
45
|
+
* slot jumped to the end of the pinned list instead of holding its place.
|
|
46
|
+
*
|
|
47
|
+
* **What remains is reach, not order** — the earlier note here said hand-edits among several
|
|
48
|
+
* `${…}` parts' content ordered "approximately", which measurement does not support: every
|
|
49
|
+
* position a user can actually reach orders exactly as the platform does, and every order light
|
|
50
|
+
* produces is one a shadow root also produces. What light has FEWER of is positions. A distributed
|
|
51
|
+
* child is no longer a direct child of the host, so `host.insertBefore(node, thatChild)` throws
|
|
52
|
+
* `NotFoundError` where a shadow host accepts it, and positions among an already-distributed
|
|
53
|
+
* group cannot be named at the host's top level at all. `child.before(node)` / `child.after(node)`
|
|
54
|
+
* go through the node's current parent and work identically in both modes; that is what the README
|
|
55
|
+
* teaches, and `tests/renderer-slots.test.mjs` pins both the throw and the answer. And one parity note: whitespace appended to a
|
|
56
|
+
* light host now suppresses the default fallback, because it does exactly that in a shadow root
|
|
57
|
+
* (measured). `tests/slots-transition-parity.test.mjs` holds the matrix.
|
|
58
|
+
*
|
|
59
|
+
* **A `<slot>` cannot sit inside table markup**, in either mode: the parser's table insertion mode
|
|
60
|
+
* rejects the element and foster-parents it out, so the slot lands before the `<table>` and takes
|
|
61
|
+
* its distributed content with it. Measured against a shadow root given the same markup — it does
|
|
62
|
+
* the same thing, so the modes agree and this is the platform's rule rather than this module's.
|
|
63
|
+
* Ordinary bindings are fine (`<tbody>${rows}</tbody>` renders correctly) because the renderer's
|
|
64
|
+
* anchor is a COMMENT, which table parsing permits where it rejects elements.
|
|
65
|
+
*
|
|
66
|
+
* **Pop-out rule.** Every *document* touch derives from the node itself (`ownerDocument`), so a
|
|
67
|
+
* component rendered into a second window creates its nodes in that window's document.
|
|
68
|
+
*
|
|
69
|
+
* The one global read is `MutationObserver`, deliberately: an observer is not bound to the realm
|
|
70
|
+
* of the nodes it watches, so deriving the constructor from `ownerDocument.defaultView` would buy
|
|
71
|
+
* nothing and cost bytes on every capture. **Measured, not assumed** — `tests/browser/
|
|
72
|
+
* slots-realm.test.js` renders a host in a second same-origin document and asserts that live
|
|
73
|
+
* additions, `slot` changes and removals all reach their slots, on Chromium, Firefox and WebKit.
|
|
74
|
+
* Behaviour the platform decides is not settled under jsdom.
|
|
75
|
+
*/
|
|
76
|
+
const HOSTS = new WeakMap();
|
|
77
|
+
/** The host mark's shared descriptor — non-enumerable, for the same invisibility the stamps get. */
|
|
78
|
+
const HOSTED = { value: true, enumerable: false, configurable: true };
|
|
79
|
+
/** Every captured node → its host's sentinel, for `_$home$`. Entries die with their nodes. */
|
|
80
|
+
const HOMES = new WeakMap();
|
|
81
|
+
/**
|
|
82
|
+
* **The light tree's surviving skeleton, indexed.** Distribution moves the CONTENT out of the
|
|
83
|
+
* host, but the part markers never move — so a comment's neighbours at capture time are exactly
|
|
84
|
+
* the positional record a late hand-edit needs. Each comment in the initial walk maps to the
|
|
85
|
+
* member captured immediately after it; placement walks forward from an inserted node to the
|
|
86
|
+
* first recorded comment whose member is still in the right bucket, and goes before that member.
|
|
87
|
+
* A WeakMap and a walk over LIVE nodes only, so a torn-down part's markers take their entries
|
|
88
|
+
* with them and staleness needs no bookkeeping. (Verified before building: a part keeps the SAME
|
|
89
|
+
* marker nodes across template-identity rebuilds, emptying included, so landmarks cannot churn.)
|
|
90
|
+
*/
|
|
91
|
+
const LANDMARKS = new WeakMap();
|
|
92
|
+
/**
|
|
93
|
+
* What the observer watches, wherever it watches. Hoisted because captured nodes rest in more than
|
|
94
|
+
* one place and every one of them needs the SAME watch — see the `observe` calls in `capture` and
|
|
95
|
+
* the park fragment in `takeOverSlot`.
|
|
96
|
+
*/
|
|
97
|
+
const WATCHING = { childList: true, subtree: true, attributes: true, attributeFilter: ['slot'] };
|
|
98
|
+
/**
|
|
99
|
+
* Slottables are elements and text nodes — comments and the rest are never assigned (`null`).
|
|
100
|
+
*
|
|
101
|
+
* **A deliberate twin of `slotNameOf` in `@verajs/ssr`'s `vera/nodes.js`**, which must spell the
|
|
102
|
+
* same rule: independent packages, ssr does not import this one at runtime, so the rule is copied
|
|
103
|
+
* rather than shared. A deliberate duplication is a fix's second address — change one and the
|
|
104
|
+
* other needs the same change. `tests/ssr-slot-assignment-parity.test.mjs` fails if they diverge.
|
|
105
|
+
*
|
|
106
|
+
* `null` here means "not slottable", and callers must decide what that implies for THEM: the read
|
|
107
|
+
* path filters those nodes out of `assignedNodes()`, while `serverDistribute` skips them, which
|
|
108
|
+
* leaves them where they are on the client and drops them on a server that has already re-rendered
|
|
109
|
+
* the host. Two different consequences from one answer — see `serverDistribute`.
|
|
110
|
+
*/
|
|
111
|
+
const slotNameOf = (node) => node.nodeType === 3 ? '' : node.nodeType === 1 ? (node.getAttribute('slot') ?? '') : null;
|
|
112
|
+
const bucketOf = (state, name) => {
|
|
113
|
+
let bucket = state._map.get(name);
|
|
114
|
+
if (bucket === undefined)
|
|
115
|
+
state._map.set(name, (bucket = []));
|
|
116
|
+
return bucket;
|
|
117
|
+
};
|
|
118
|
+
/** Take one node into the slot system: bucket it, remember it, and physically hold it. */
|
|
119
|
+
/** `ordered`: the caller vouches the node arrives in its final relative order (the initial
|
|
120
|
+
* capture walk and the server-park recovery — both iterate host order), so placement is a plain
|
|
121
|
+
* append: O(1), and immune to the fact that earlier-taken siblings are already in holding and
|
|
122
|
+
* can no longer be position-compared. */
|
|
123
|
+
const take = (state, node, ordered = false, atTail = false) => {
|
|
124
|
+
const name = slotNameOf(node);
|
|
125
|
+
if (name === null)
|
|
126
|
+
return null;
|
|
127
|
+
const bucket = bucketOf(state, name);
|
|
128
|
+
const binding = activeFor(state, name);
|
|
129
|
+
/**
|
|
130
|
+
* **A node captured where it already belongs stays there.** Whoever put it inside the run put it
|
|
131
|
+
* in a POSITION, and evacuating it to the holding fragment throws that away — it comes back at
|
|
132
|
+
* the end, because the fragment has no idea where it was. A keyed list inserting a row into a
|
|
133
|
+
* light host's children is the case: the row is created under the host, moved among its
|
|
134
|
+
* neighbours, and captured in the same batch, and the position is the only thing that says where
|
|
135
|
+
* it goes.
|
|
136
|
+
*
|
|
137
|
+
* Everything else — a node the user appended, one arriving from the holding fragment — is held
|
|
138
|
+
* as before, and `fill` places it.
|
|
139
|
+
*/
|
|
140
|
+
const settled = binding !== undefined && node.parentNode === binding._start.parentNode;
|
|
141
|
+
/**
|
|
142
|
+
* **Where a captured node goes in the bucket — one branch per way of knowing.**
|
|
143
|
+
*
|
|
144
|
+
* SETTLED (already inside its slot's run): document order — whoever moved it there meant that
|
|
145
|
+
* position; a keyed mid-insert lands here. PLACED (stamped with the part that put it in the
|
|
146
|
+
* host): after that part's last member, so a grown row extends its own list and two parts'
|
|
147
|
+
* content in one host cannot interleave; no member yet means append, where a first row belongs.
|
|
148
|
+
* UNSTAMPED (a human's): its position speaks — before the boundary it precedes everything
|
|
149
|
+
* distributed away (document order among nodes still beside it); at or past the boundary it
|
|
150
|
+
* was appended, and appends. The last-member fast path keeps the initial capture walk O(1) per
|
|
151
|
+
* node — each child follows the one before it — instead of O(n²) over a big light list.
|
|
152
|
+
*/
|
|
153
|
+
const own = node._$own$;
|
|
154
|
+
let at = bucket.length;
|
|
155
|
+
/**
|
|
156
|
+
* Set when this node sits in the light region ahead of everything already distributed — the rule
|
|
157
|
+
* the unstamped branch below states in words. It is tracked separately from `at` because `at` is
|
|
158
|
+
* a position in ONE bucket, and a node can land at the end of its own bucket while still
|
|
159
|
+
* preceding every node in every other one. That is exactly the case a prepend into an empty
|
|
160
|
+
* bucket makes: `at` is 0 and means nothing, and ranking from bucket neighbours put the node
|
|
161
|
+
* last when it belonged first.
|
|
162
|
+
*/
|
|
163
|
+
let front = false;
|
|
164
|
+
if (ordered) ;
|
|
165
|
+
else if (settled) {
|
|
166
|
+
for (let i = 0; i < bucket.length; i++)
|
|
167
|
+
|
|
168
|
+
if ((node.compareDocumentPosition(bucket[i]) & 4) !== 0) {
|
|
169
|
+
at = i;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else if (own !== undefined) {
|
|
174
|
+
for (let i = bucket.length - 1; i >= 0; i--)
|
|
175
|
+
if (bucket[i]._$own$ === own) {
|
|
176
|
+
at = i + 1;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
const home = node.parentNode;
|
|
182
|
+
const last = bucket[bucket.length - 1];
|
|
183
|
+
const sentinel = state._sentinel;
|
|
184
|
+
/**
|
|
185
|
+
* **The record is the light-tree truth; the sentinel is only a proxy for it.**
|
|
186
|
+
*
|
|
187
|
+
* `front` is "this node precedes everything already distributed" — inferred here from the
|
|
188
|
+
* node preceding the `_sentinel` in the LIVE DOM. That inference holds when the host is
|
|
189
|
+
* settled, and BREAKS mid-storm on an adopted seam: `flushPending` can process a genuine
|
|
190
|
+
* tail-append while the sentinel is transiently positioned AFTER it, so the append reads as
|
|
191
|
+
* front and takes a negative rank, sorting before the adopted nodes (run-18 residual, root
|
|
192
|
+
* cause). The observer path knows better: a childList record whose `nextSibling` is null was
|
|
193
|
+
* an APPEND at the light-tree tail, immune to any later churn — so `atTail` overrides the
|
|
194
|
+
* proxy outright. A non-null `nextSibling` (prepend, insertBefore) is left to the existing
|
|
195
|
+
* inference, which the settled and client-storm paths already exercise.
|
|
196
|
+
*/
|
|
197
|
+
front =
|
|
198
|
+
!atTail &&
|
|
199
|
+
sentinel.parentNode === home &&
|
|
200
|
+
|
|
201
|
+
(node.compareDocumentPosition(sentinel) & 4) !== 0;
|
|
202
|
+
if (!atTail &&
|
|
203
|
+
sentinel.parentNode === home &&
|
|
204
|
+
|
|
205
|
+
(node.compareDocumentPosition(sentinel) & 4) !== 0 &&
|
|
206
|
+
last !== undefined &&
|
|
207
|
+
|
|
208
|
+
!(last.parentNode === home && (last.compareDocumentPosition(node) & 4) !== 0)) {
|
|
209
|
+
/**
|
|
210
|
+
* LANDMARKS first — exact where available: the nearest recorded comment after the node
|
|
211
|
+
* names the member it belongs before, which places a hand-edit BETWEEN two parts' groups
|
|
212
|
+
* where the scan below can only say "ahead of everything distributed". Skips members that
|
|
213
|
+
* left this bucket (pulled, or another slot's) by walking on; bounded by the sentinel.
|
|
214
|
+
*/
|
|
215
|
+
let placed = false;
|
|
216
|
+
for (let next = node.nextSibling; next !== null && next !== sentinel; next = next.nextSibling) {
|
|
217
|
+
const member = next.nodeType === 8 ? LANDMARKS.get(next) : undefined;
|
|
218
|
+
if (member !== undefined) {
|
|
219
|
+
const found = bucket.indexOf(member);
|
|
220
|
+
if (found !== -1) {
|
|
221
|
+
at = found;
|
|
222
|
+
front = false;
|
|
223
|
+
placed = true;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* **Backward too, because the skeleton has landmarks on both sides.** Looking only forward
|
|
230
|
+
* meant that when every following landmark named a member that had since been removed or
|
|
231
|
+
* re-slotted elsewhere, placement fell through to the scan below — which knows nothing
|
|
232
|
+
* finer than "ahead of everything distributed" and so put the edit FIRST. Measured against a
|
|
233
|
+
* shadow root given the same disturbance: it answers `A,U` and this answered `U,A`.
|
|
234
|
+
*
|
|
235
|
+
* A preceding landmark names the member captured just after it, so a node inserted after
|
|
236
|
+
* that comment belongs after that member — the mirror of the forward rule, and it resolves
|
|
237
|
+
* exactly when the forward walk cannot.
|
|
238
|
+
*/
|
|
239
|
+
if (!placed)
|
|
240
|
+
for (let prev = node.previousSibling; prev !== null; prev = prev.previousSibling) {
|
|
241
|
+
const member = prev.nodeType === 8 ? LANDMARKS.get(prev) : undefined;
|
|
242
|
+
if (member !== undefined) {
|
|
243
|
+
const found = bucket.indexOf(member);
|
|
244
|
+
if (found !== -1) {
|
|
245
|
+
at = found + 1;
|
|
246
|
+
front = false;
|
|
247
|
+
placed = true;
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (!placed)
|
|
253
|
+
for (let i = 0; i < bucket.length; i++) {
|
|
254
|
+
const member = bucket[i];
|
|
255
|
+
|
|
256
|
+
if (member.parentNode !== home || (node.compareDocumentPosition(member) & 4) !== 0) {
|
|
257
|
+
at = i;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* **A first sighting takes its light-tree rank from the position just computed.**
|
|
265
|
+
*
|
|
266
|
+
* `at` is this module's best answer to "where does this node belong", worked out from whatever
|
|
267
|
+
* evidence exists — document position, the part that placed it, landmarks, the sentinel. That
|
|
268
|
+
* answer is about ONE bucket, and re-slotting needs the same fact about the light tree, so the
|
|
269
|
+
* rank is interpolated between the neighbours it landed among rather than invented separately.
|
|
270
|
+
* Deriving it here is what makes the two agree by construction; a counter bumped on arrival
|
|
271
|
+
* instead gave a node inserted at the FRONT of the light tree a tail rank, and re-slotting it
|
|
272
|
+
* then sent it to the end — right where it was, wrong where it went.
|
|
273
|
+
*
|
|
274
|
+
* Midpoints, so an insertion never has to renumber. Doubles run out after about fifty
|
|
275
|
+
* insertions between one adjacent pair, which no light host reaches; a re-capture (removed and
|
|
276
|
+
* re-added) starts fresh at the tail, which is also where the platform puts it.
|
|
277
|
+
*/
|
|
278
|
+
if (!state._rank.has(node)) {
|
|
279
|
+
const before = at > 0 ? state._rank.get(bucket[at - 1]) : undefined;
|
|
280
|
+
const after = at < bucket.length ? state._rank.get(bucket[at]) : undefined;
|
|
281
|
+
state._rank.set(node, front
|
|
282
|
+
? --state._min
|
|
283
|
+
: after === undefined
|
|
284
|
+
? state._next++
|
|
285
|
+
: before === undefined
|
|
286
|
+
? after - 1
|
|
287
|
+
: (before + after) / 2);
|
|
288
|
+
}
|
|
289
|
+
bucket.splice(at, 0, node);
|
|
290
|
+
state._names.set(node, name);
|
|
291
|
+
HOMES.set(node, state._sentinel);
|
|
292
|
+
if (!settled)
|
|
293
|
+
state._holding.appendChild(node);
|
|
294
|
+
return name;
|
|
295
|
+
};
|
|
296
|
+
const pull = (state, node, name) => {
|
|
297
|
+
const bucket = state._map.get(name);
|
|
298
|
+
if (bucket === undefined)
|
|
299
|
+
return;
|
|
300
|
+
const at = bucket.indexOf(node);
|
|
301
|
+
if (at !== -1)
|
|
302
|
+
bucket.splice(at, 1);
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* Discard the records the module's own DOM moves just produced. Wholesale discard is CORRECT here
|
|
306
|
+
* for exactly one reason, and `flushPending` is that reason: every mutating operation flushes the
|
|
307
|
+
* user's queued records BEFORE it moves anything, so by the time this runs the queue holds only
|
|
308
|
+
* our own. (The first fix for the run-17 storms re-fed the taken records through the handler
|
|
309
|
+
* instead — and our holding-fragment churn then flowed through arms never designed for it,
|
|
310
|
+
* breaking real re-fallback. Process-then-move is the design that holds: user records are
|
|
311
|
+
* processed while the queue is provably pure-user, ours are discarded while it is provably
|
|
312
|
+
* pure-ours.)
|
|
313
|
+
*/
|
|
314
|
+
const drain = (state) => {
|
|
315
|
+
state._observer.takeRecords();
|
|
316
|
+
};
|
|
317
|
+
/**
|
|
318
|
+
* Process the USER's queued records before the module mutates anything — the run-17 fix.
|
|
319
|
+
*
|
|
320
|
+
* Observer delivery is asynchronous, so a user mutation made earlier in the same task (an append,
|
|
321
|
+
* a `slot` rename, a removal) is still queued when a mount, fill or park starts moving nodes.
|
|
322
|
+
* The old code drained that queue wholesale afterwards, under a comment claiming nothing of the
|
|
323
|
+
* user's could be in it — wrong direction of time: seven deterministic storm divergences, one
|
|
324
|
+
* root cause. At operation ENTRY the queue holds only user records (every previous operation
|
|
325
|
+
* ended by draining its own), so processing here is exactly the observer callback running early.
|
|
326
|
+
* Re-entrant-safe: processing refills, refills fill, and the inner fill's flush must not recurse.
|
|
327
|
+
*/
|
|
328
|
+
const flushPending = (state) => {
|
|
329
|
+
if (state._flushing)
|
|
330
|
+
return;
|
|
331
|
+
const records = state._observer.takeRecords();
|
|
332
|
+
if (records.length === 0)
|
|
333
|
+
return;
|
|
334
|
+
state._flushing = true;
|
|
335
|
+
try {
|
|
336
|
+
processRecords(state._host, state, records);
|
|
337
|
+
}
|
|
338
|
+
finally {
|
|
339
|
+
state._flushing = false;
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
/** The binding that OWNS a name — the first in tree order, as the platform picks the first
|
|
343
|
+
* matching slot and leaves any duplicate showing its fallback. */
|
|
344
|
+
const activeFor = (state, name) => {
|
|
345
|
+
const bindings = state._bindings;
|
|
346
|
+
for (let i = 0; i < bindings.length; i++)
|
|
347
|
+
if (bindings[i]._name === name)
|
|
348
|
+
return bindings[i];
|
|
349
|
+
return undefined;
|
|
350
|
+
};
|
|
351
|
+
const NOTHING = [];
|
|
352
|
+
/**
|
|
353
|
+
* **`slotchange`, on the element the author bound it to.** Queued rather than dispatched inline:
|
|
354
|
+
* the platform fires it at the microtask checkpoint, and firing synchronously from inside a render
|
|
355
|
+
* would re-enter rendering from a handler. The queue also means the very first dispatch lands
|
|
356
|
+
* after the slot's own `AttrPart`s have attached their listeners, whatever order mounting took.
|
|
357
|
+
*
|
|
358
|
+
* `Event` comes from the HOST's realm, not the module's — a component rendered into a popped-out
|
|
359
|
+
* window must dispatch an event that window's code recognises, or a handler's `instanceof` is
|
|
360
|
+
* false and, measured, a strict DOM refuses the foreign object outright.
|
|
361
|
+
*
|
|
362
|
+
* **From the host, and deliberately not from the slot**, which is the trap: a `<template>`'s
|
|
363
|
+
* content belongs to an inert document that has no browsing context, so the slot element's
|
|
364
|
+
* `ownerDocument.defaultView` is `null` — every clone of it, in every realm. The host is a live
|
|
365
|
+
* element in the real document and is the only one of the two that knows which window this is.
|
|
366
|
+
*
|
|
367
|
+
* (`queueMicrotask` is not realm-bound; the job queue is shared.)
|
|
368
|
+
*/
|
|
369
|
+
const signal = (state, binding) => {
|
|
370
|
+
const slot = binding._slot;
|
|
371
|
+
if (slot === undefined || binding._queued)
|
|
372
|
+
return;
|
|
373
|
+
binding._queued = true;
|
|
374
|
+
queueMicrotask(() => {
|
|
375
|
+
binding._queued = false;
|
|
376
|
+
slot.dispatchEvent(new state._event('slotchange', { bubbles: true, composed: false }));
|
|
377
|
+
});
|
|
378
|
+
};
|
|
379
|
+
const same = (a, b) => {
|
|
380
|
+
if (a.length !== b.length)
|
|
381
|
+
return false;
|
|
382
|
+
for (let i = 0; i < a.length; i++)
|
|
383
|
+
if (a[i] !== b[i])
|
|
384
|
+
return false;
|
|
385
|
+
return true;
|
|
386
|
+
};
|
|
387
|
+
/**
|
|
388
|
+
* Make one binding show what it should: the assignment (when it is the active binding for its
|
|
389
|
+
* name and the bucket has nodes) or its fallback. Current occupants are evacuated first —
|
|
390
|
+
* assigned nodes to holding (they remain captured), an unassigned region into `_fallback`, which
|
|
391
|
+
* is a fragment rather than a list precisely because that region goes on living while it is away.
|
|
392
|
+
* A bucket entry the USER spirited away while it was held (removed from holding, or adopted into
|
|
393
|
+
* their own DOM) is purged rather than stolen back.
|
|
394
|
+
*
|
|
395
|
+
* **What the fragment fixes, stated because it cost real content.** Detaching the region node by
|
|
396
|
+
* node left every anchor inside it parentless, so a binding nested in this fallback hit the guard
|
|
397
|
+
* at the top of this function and returned without placing anything. A child the user added for
|
|
398
|
+
* that inner slot while the outer one was assigned therefore went into its bucket and nowhere
|
|
399
|
+
* else — and when the outer slot later fell back, the old list restored the inner slot's ORIGINAL
|
|
400
|
+
* fallback over the top. The user's node was detached, invisible, and unrecoverable, while the
|
|
401
|
+
* inner slot's own `assignedNodes()` still named it. In a shadow root the same sequence simply
|
|
402
|
+
* shows the node, because there the inner slot never leaves the tree.
|
|
403
|
+
*
|
|
404
|
+
* Parked in a fragment, the anchors keep a parent, the inner binding places into the fragment as
|
|
405
|
+
* it always would, and restoring carries the result back. One call, no list, no re-fill pass.
|
|
406
|
+
*/
|
|
407
|
+
const fill = (state, binding) => {
|
|
408
|
+
/** The user's queued mutations first — see `flushPending`. Re-entrant fills skip via the flag. */
|
|
409
|
+
flushPending(state);
|
|
410
|
+
const parent = binding._start.parentNode;
|
|
411
|
+
if (parent === null)
|
|
412
|
+
return; // anchors already discarded mid-teardown — nothing to show
|
|
413
|
+
/**
|
|
414
|
+
* **The run's current order is remembered before it is taken apart, because it is the intent.**
|
|
415
|
+
*
|
|
416
|
+
* The bucket records what is assigned; its order is the order things were CAPTURED. Re-placing
|
|
417
|
+
* from it therefore undoes any reordering that happened after distribution — and a keyed list
|
|
418
|
+
* rendering a host's children reorders exactly those nodes, in place, where they now live. The
|
|
419
|
+
* symptom was a list that reordered correctly and then snapped back to its original order the
|
|
420
|
+
* next time anything caused a refill.
|
|
421
|
+
*
|
|
422
|
+
* Only the case that is currently wrong changes: when nothing has reordered the run, this order
|
|
423
|
+
* IS the bucket's order and the sort below is the identity.
|
|
424
|
+
*/
|
|
425
|
+
const wasShowing = new Map();
|
|
426
|
+
let node = binding._start.nextSibling;
|
|
427
|
+
while (node !== null && node !== binding._end) {
|
|
428
|
+
const next = node.nextSibling;
|
|
429
|
+
wasShowing.set(node, wasShowing.size);
|
|
430
|
+
if (binding._assigned)
|
|
431
|
+
state._holding.appendChild(node);
|
|
432
|
+
else
|
|
433
|
+
binding._fallback.appendChild(node);
|
|
434
|
+
node = next;
|
|
435
|
+
}
|
|
436
|
+
const bucket = activeFor(state, binding._name) === binding ? bucketOf(state, binding._name) : NOTHING;
|
|
437
|
+
/**
|
|
438
|
+
* **A stable MERGE, not a re-sort.** Run members keep the run's order (a keyed reorder happens
|
|
439
|
+
* in place, invisible to the observer — the run is its only record). Everything else keeps its
|
|
440
|
+
* BUCKET position, ranked beside the run member it follows: a prepend `take` placed at index 0
|
|
441
|
+
* stays ahead of the run instead of being shoved to the end — which is exactly how the first
|
|
442
|
+
* ordering attempt broke two suites. Equal ranks fall back to bucket order (sort stability is
|
|
443
|
+
* guaranteed), and `NOTHING` is shared and never written to.
|
|
444
|
+
*/
|
|
445
|
+
if (wasShowing.size > 0 && bucket !== NOTHING) {
|
|
446
|
+
const rank = new Map();
|
|
447
|
+
let carried = -1;
|
|
448
|
+
for (const member of bucket) {
|
|
449
|
+
const shown = wasShowing.get(member);
|
|
450
|
+
if (shown !== undefined)
|
|
451
|
+
carried = shown;
|
|
452
|
+
rank.set(member, shown ?? carried + 0.5);
|
|
453
|
+
}
|
|
454
|
+
bucket.sort((a, b) => rank.get(a) - rank.get(b));
|
|
455
|
+
}
|
|
456
|
+
const shown = [];
|
|
457
|
+
for (let i = 0; i < bucket.length; i++) {
|
|
458
|
+
const candidate = bucket[i];
|
|
459
|
+
const home = candidate.parentNode;
|
|
460
|
+
if (home !== state._holding &&
|
|
461
|
+
home !== null &&
|
|
462
|
+
home !== parent &&
|
|
463
|
+
!state._parks.has(home) &&
|
|
464
|
+
!inAnyRun(state, candidate)) {
|
|
465
|
+
/**
|
|
466
|
+
* The user took this node for themselves while it was unassigned — respect that.
|
|
467
|
+
*
|
|
468
|
+
* `_parks` is the exception and it is not decoration: a node distributed by a slot nested in
|
|
469
|
+
* a displaced fallback rests in that fallback's park fragment, which is a home of OURS. Read
|
|
470
|
+
* as a user adoption it was purged from the bucket instead of being placed, so re-slotting
|
|
471
|
+
* such a node to an outer slot dropped it — visible content, gone, in a sequence a shadow
|
|
472
|
+
* root handles without comment.
|
|
473
|
+
*
|
|
474
|
+
* `!inAnyRun` is the run-27 completion of that same idea: a node RESLOTTED from one slot to
|
|
475
|
+
* another has its bucket moved by the attribute arm but is still PHYSICALLY in its old
|
|
476
|
+
* slot's run when this fill for the NEW slot runs — its parent is that other run's region,
|
|
477
|
+
* which is a location WE control, not a user adoption. Without this the reslot's own fill
|
|
478
|
+
* purged the node it was about to place, and the node vanished (run-27 adopted-seam storm
|
|
479
|
+
* node-loss: concurrent DOM churn shifted delivery so the new-slot fill ran before the node
|
|
480
|
+
* was physically relocated). Another run is the last of our regions the guard did not name.
|
|
481
|
+
*/
|
|
482
|
+
bucket.splice(i--, 1);
|
|
483
|
+
state._names.delete(candidate);
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
parent.insertBefore(candidate, binding._end);
|
|
487
|
+
shown.push(candidate);
|
|
488
|
+
}
|
|
489
|
+
binding._assigned = shown.length > 0;
|
|
490
|
+
if (shown.length === 0)
|
|
491
|
+
parent.insertBefore(binding._fallback, binding._end);
|
|
492
|
+
if (!same(binding._shown, shown)) {
|
|
493
|
+
binding._shown = shown;
|
|
494
|
+
signal(state, binding);
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
const refill = (state, name) => {
|
|
498
|
+
const binding = activeFor(state, name);
|
|
499
|
+
if (binding !== undefined)
|
|
500
|
+
fill(state, binding);
|
|
501
|
+
};
|
|
502
|
+
/**
|
|
503
|
+
* Register one binding for `name` and hand back its park closure — the single source for both the
|
|
504
|
+
* client seam and hydration's adopt, which previously spelled the same registration and the same
|
|
505
|
+
* park body twice (the house's most-repeated defect class). Park rescues assigned user nodes into
|
|
506
|
+
* holding before the instance's DOM is bulk-discarded, unregisters, and promotes the next
|
|
507
|
+
* duplicate slot to the assignment — native's next-in-tree-order.
|
|
508
|
+
*/
|
|
509
|
+
const bind = (state, binding) => {
|
|
510
|
+
state._bindings.push(binding);
|
|
511
|
+
const slot = binding._slot;
|
|
512
|
+
if (slot !== undefined) {
|
|
513
|
+
state._ghosts.set(slot, binding);
|
|
514
|
+
expose(state, binding);
|
|
515
|
+
/** A `name` that is a binding can change between renders, and the element it changes on is
|
|
516
|
+
* not in the host's subtree — so it is watched directly. */
|
|
517
|
+
state._observer.observe(slot, { attributes: true, attributeFilter: ['name'] });
|
|
518
|
+
if (true )
|
|
519
|
+
warnInert(slot);
|
|
520
|
+
}
|
|
521
|
+
return {
|
|
522
|
+
_$park$: () => {
|
|
523
|
+
/** A rename or removal queued in this same task decides WHAT gets parked — process it first. */
|
|
524
|
+
flushPending(state);
|
|
525
|
+
if (binding._assigned) {
|
|
526
|
+
let node = binding._start.nextSibling;
|
|
527
|
+
while (node !== null && node !== binding._end) {
|
|
528
|
+
const next = node.nextSibling;
|
|
529
|
+
state._holding.appendChild(node);
|
|
530
|
+
node = next;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
const wasActive = activeFor(state, binding._name) === binding;
|
|
534
|
+
const at = state._bindings.indexOf(binding);
|
|
535
|
+
if (at !== -1)
|
|
536
|
+
state._bindings.splice(at, 1);
|
|
537
|
+
/** Whoever is now first for that name inherits the assignment — native's next in tree order. */
|
|
538
|
+
if (wasActive)
|
|
539
|
+
refill(state, binding._name);
|
|
540
|
+
drain(state);
|
|
541
|
+
},
|
|
542
|
+
};
|
|
543
|
+
};
|
|
544
|
+
/**
|
|
545
|
+
* Answer `assignedNodes()`/`assignedElements()` from the live assignment, so a handler written for
|
|
546
|
+
* a shadow slot — `event.target.assignedElements()`, or the same through `&ref` — reads the same
|
|
547
|
+
* thing here. A detached element's own implementations return nothing, which is what made the
|
|
548
|
+
* ordinary way of reading a slot come back empty in light mode.
|
|
549
|
+
*
|
|
550
|
+
* `flatten` means what it means on the platform for a slot with nothing assigned: the fallback
|
|
551
|
+
* **that is actually on screen right now**. Both halves of that sentence were wrong.
|
|
552
|
+
*
|
|
553
|
+
* It answered from `_fallback`, which is the restore list — a snapshot of the slot's template
|
|
554
|
+
* children, taken once. That is the right list to re-insert from and the wrong list to report,
|
|
555
|
+
* because the region it stands for is live: a NESTED slot inside fallback content redistributes
|
|
556
|
+
* as its own assignment changes, and a binding inside fallback content re-renders. Reported from
|
|
557
|
+
* the snapshot, a nested slot's flatten kept naming a node the user had already removed from the
|
|
558
|
+
* document, forever.
|
|
559
|
+
*
|
|
560
|
+
* So read from wherever the fallback IS. Rendered, that is the region between the anchors, and
|
|
561
|
+
* reading it there is live by construction rather than by maintenance — nothing has to remember to
|
|
562
|
+
* update it — which also makes the recursion the platform specifies fall out for free, since an
|
|
563
|
+
* inner slot's distributed content is physically in that region. Displaced, there is no region to
|
|
564
|
+
* walk: `fill` detaches the old fallback node by node, anchors included, so `_start` has no parent
|
|
565
|
+
* and the restore list is the only record of it. That is the one honest discriminator here and it
|
|
566
|
+
* needs no new state — a slot nested in another slot's fallback is exactly the case that reaches
|
|
567
|
+
* it, and the platform still answers for such a slot because in a shadow tree it never left.
|
|
568
|
+
*
|
|
569
|
+
* The filter is the second half: the platform's word is *slottables*, so `slotNameOf` — the same
|
|
570
|
+
* predicate that decides assignment everywhere else in this file — is what says which nodes count.
|
|
571
|
+
* Without it the walk hands back a user's own `<!-- -->` written into fallback content, and this
|
|
572
|
+
* module's own markers around a nested slot's content: an implementation detail escaping through
|
|
573
|
+
* a documented API. One predicate closes both.
|
|
574
|
+
*/
|
|
575
|
+
const expose = (state, binding) => {
|
|
576
|
+
const slot = binding._slot;
|
|
577
|
+
const read = (flatten) => {
|
|
578
|
+
const bucket = activeFor(state, binding._name) === binding ? (state._map.get(binding._name) ?? NOTHING) : NOTHING;
|
|
579
|
+
if (bucket.length > 0)
|
|
580
|
+
return [...bucket];
|
|
581
|
+
if (flatten !== true)
|
|
582
|
+
return [];
|
|
583
|
+
const shown = [];
|
|
584
|
+
for (let n = binding._start.nextSibling; n !== null && n !== binding._end; n = n.nextSibling)
|
|
585
|
+
shown.push(n);
|
|
586
|
+
return shown.filter((node) => slotNameOf(node) !== null);
|
|
587
|
+
};
|
|
588
|
+
slot.assignedNodes = (options) => read(options?.flatten);
|
|
589
|
+
slot.assignedElements = (options) => read(options?.flatten).filter((node) => node.nodeType === 1);
|
|
590
|
+
};
|
|
591
|
+
/**
|
|
592
|
+
* **What a `<slot>` cannot carry in a light component, said out loud.** A light host has no second
|
|
593
|
+
* tree, so the slot element is not rendered and never can be: `class`, `style`, `id` and the rest
|
|
594
|
+
* have nowhere to apply, whether they were written statically or bound. In a shadow root they do
|
|
595
|
+
* apply — a `<slot>` is a real element there — so this is the one asymmetry left, and silence
|
|
596
|
+
* about it is what made it expensive to find.
|
|
597
|
+
*
|
|
598
|
+
* `name` is excluded because it is the slot's meaning, and events and `&ref` never appear as
|
|
599
|
+
* attributes at all, so they do not trip this.
|
|
600
|
+
*/
|
|
601
|
+
const warnInert = (slot) => {
|
|
602
|
+
const inert = [];
|
|
603
|
+
for (const attribute of slot.attributes)
|
|
604
|
+
if (attribute.name !== 'name')
|
|
605
|
+
inert.push(attribute.name);
|
|
606
|
+
if (inert.length === 0)
|
|
607
|
+
return;
|
|
608
|
+
const label = slot.getAttribute('name');
|
|
609
|
+
const key = `${label ?? ''}|${inert.join(',')}`;
|
|
610
|
+
if (warnedInert.has(key))
|
|
611
|
+
return;
|
|
612
|
+
warnedInert.add(key);
|
|
613
|
+
console.warn(`[vera] <slot${label === null ? '' : ` name="${label}"`}> carries ${inert.map((n) => `\`${n}\``).join(', ')}, ` +
|
|
614
|
+
`which does nothing in a light-DOM component: the slot element is not rendered, so there is ` +
|
|
615
|
+
`nothing for it to apply to. In a shadow root it would apply. Put it on a real element around ` +
|
|
616
|
+
`the slot instead. (\`name\`, \`@event\` bindings and \`&ref\` all work here.)`);
|
|
617
|
+
};
|
|
618
|
+
const warnedInert = new Set();
|
|
619
|
+
/**
|
|
620
|
+
* The user's mutations, batched. Additions join only with an explicit `slot` attribute (the
|
|
621
|
+
* documented rule above); removals and re-slottings of CAPTURED nodes are recognized anywhere by
|
|
622
|
+
* identity. Template-caused records match nothing here: its nodes were never captured, and our
|
|
623
|
+
* own moves were drained before they could arrive.
|
|
624
|
+
*/
|
|
625
|
+
/**
|
|
626
|
+
* Is this node currently sitting inside one of OUR runs — between some binding's anchors, in the
|
|
627
|
+
* host or in a not-yet-inserted instance fragment alike? The user-took removal arm needs it: a
|
|
628
|
+
* mount's `fill` moves a captured node into the instance's DETACHED fragment before the fragment
|
|
629
|
+
* enters the host, so at drain time the node is in neither the host nor the holding — exactly the
|
|
630
|
+
* signature the arm reads as "the user took it". Position between anchors is the fact that holds
|
|
631
|
+
* in both worlds, and it is the same test the nested-addition arm already trusts.
|
|
632
|
+
*/
|
|
633
|
+
const inAnyRun = (state, node) => {
|
|
634
|
+
for (const binding of state._bindings) {
|
|
635
|
+
const parent = binding._start.parentNode;
|
|
636
|
+
if (parent === null || parent !== node.parentNode)
|
|
637
|
+
continue;
|
|
638
|
+
|
|
639
|
+
if ((binding._start.compareDocumentPosition(node) & 4) !== 0 && (node.compareDocumentPosition(binding._end) & 4) !== 0)
|
|
640
|
+
return true;
|
|
641
|
+
}
|
|
642
|
+
return false;
|
|
643
|
+
};
|
|
644
|
+
const processRecords = (host, state, records) => {
|
|
645
|
+
const touched = new Set();
|
|
646
|
+
const moved = [];
|
|
647
|
+
for (const record of records) {
|
|
648
|
+
if (record.type === 'attributes') {
|
|
649
|
+
const node = record.target;
|
|
650
|
+
/**
|
|
651
|
+
* A kept `<slot>` element renaming itself — `<slot name=${section}>` with a new value. Both
|
|
652
|
+
* names are refilled: the one it left (whose next duplicate, if any, inherits) and the one it
|
|
653
|
+
* joined. The binding itself is filled either way, since it may now be a duplicate and owe
|
|
654
|
+
* its fallback.
|
|
655
|
+
*/
|
|
656
|
+
const ghost = state._ghosts.get(node);
|
|
657
|
+
if (ghost !== undefined) {
|
|
658
|
+
const next = node.getAttribute('name') ?? '';
|
|
659
|
+
if (next !== ghost._name) {
|
|
660
|
+
touched.add(ghost._name);
|
|
661
|
+
ghost._name = next;
|
|
662
|
+
touched.add(next);
|
|
663
|
+
moved.push(ghost);
|
|
664
|
+
}
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
667
|
+
const previous = state._names.get(node);
|
|
668
|
+
if (previous !== undefined) {
|
|
669
|
+
const next = slotNameOf(node);
|
|
670
|
+
if (next !== previous) {
|
|
671
|
+
pull(state, node, previous);
|
|
672
|
+
/**
|
|
673
|
+
* By LIGHT ORDER, not arrival. The node has not moved in the light tree — only its
|
|
674
|
+
* `slot` attribute changed — so its rank still says where it belongs among whatever the
|
|
675
|
+
* new bucket already holds. Appending here was the whole of the ordering divergence.
|
|
676
|
+
* `fill`'s stable merge preserves this: a member ahead of the current run ranks before
|
|
677
|
+
* it, which is the same rule that keeps a prepend ahead.
|
|
678
|
+
*/
|
|
679
|
+
const joining = bucketOf(state, next);
|
|
680
|
+
const rank = state._rank.get(node);
|
|
681
|
+
let at = joining.length;
|
|
682
|
+
for (let i = 0; i < joining.length; i++) {
|
|
683
|
+
const other = state._rank.get(joining[i]);
|
|
684
|
+
if (other !== undefined && other > rank) {
|
|
685
|
+
at = i;
|
|
686
|
+
break;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
joining.splice(at, 0, node);
|
|
690
|
+
state._names.set(node, next);
|
|
691
|
+
touched.add(previous);
|
|
692
|
+
touched.add(next);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
continue;
|
|
696
|
+
}
|
|
697
|
+
for (const node of record.addedNodes) {
|
|
698
|
+
/**
|
|
699
|
+
* **Ownership answers capture outright — the heuristic stack this replaces is gone.**
|
|
700
|
+
*
|
|
701
|
+
* Every node the renderer puts at a captured host's top level is stamped: `true` when it
|
|
702
|
+
* is the render's own output, the placing part when it is content from an outer template.
|
|
703
|
+
* So "may I capture this?" is one property read:
|
|
704
|
+
*
|
|
705
|
+
* `true` — the component's own output. Never content. (Without this, a component whose
|
|
706
|
+
* rendered root carries a `slot` attribute had its output eaten.)
|
|
707
|
+
* a part — placed content: captured, ordered by its part (see `take`).
|
|
708
|
+
* absent — a human put it here: captured with full native semantics, TEXT INCLUDED,
|
|
709
|
+
* which retires the `slot=`-after-first-render rule and the tail-text hole in
|
|
710
|
+
* one move. Safe for exactly one reason: the renderer stamps 100% of its own
|
|
711
|
+
* top-level output — measured across every template shape, and structurally
|
|
712
|
+
* guaranteed by the `_end === null` root test, async commits included — so
|
|
713
|
+
* unstamped genuinely means "not the renderer's".
|
|
714
|
+
*
|
|
715
|
+
* Deleted here: the insertion-site reconstruction (`record.nextSibling`, the region helper)
|
|
716
|
+
* — it existed because created-then-moved keyed rows were positionally indistinguishable
|
|
717
|
+
* from user content, and they are stamped now — and the tail attribute arm, the fail-closed
|
|
718
|
+
* stand-in for ownership nobody could know. `record.target === host` still bounds this to
|
|
719
|
+
* TOP-LEVEL additions (current parent alone misses same-batch moves); a component's
|
|
720
|
+
* internal renders mutate deeper parents and are never considered.
|
|
721
|
+
*/
|
|
722
|
+
if ((record.target === host || node.parentNode === host) &&
|
|
723
|
+
node._$own$ !== true &&
|
|
724
|
+
!state._names.has(node)) {
|
|
725
|
+
/** `nextSibling === null` in the record means an APPEND at the host's end — the
|
|
726
|
+
* light-tree tail, snapshotted at mutation time. `take` uses it to override the
|
|
727
|
+
* sentinel proxy that misfires mid-storm. See the front-detection note there. */
|
|
728
|
+
const name = take(state, node, false, record.nextSibling === null);
|
|
729
|
+
if (name !== null)
|
|
730
|
+
touched.add(name);
|
|
731
|
+
}
|
|
732
|
+
else if (!state._names.has(node) && node._$own$ !== true) {
|
|
733
|
+
/**
|
|
734
|
+
* **A slottable that appeared INSIDE a run, never passing the host's top level.**
|
|
735
|
+
*
|
|
736
|
+
* `splitText` on distributed text is the real case — a highlighting library's core
|
|
737
|
+
* gesture — and the tail is created as a sibling in the COMPONENT's tree, where the rule
|
|
738
|
+
* above does not look. Left uncaptured it was missing from the bucket, so the next refill
|
|
739
|
+
* evacuated it to holding and never brought it back: the text silently lost half of
|
|
740
|
+
* itself, while a shadow root reports both halves assigned and shows them.
|
|
741
|
+
*
|
|
742
|
+
* Captured here, from the record, rather than by having `fill` sweep its run for
|
|
743
|
+
* surprises — the observer holds the exact node, and re-deriving it downstream would be
|
|
744
|
+
* the same inference-instead-of-ownership this module spent its history removing, at the
|
|
745
|
+
* cost of an O(run) scan on every fill of every host forever.
|
|
746
|
+
*
|
|
747
|
+
* Three conditions make it safe, and each is a fact rather than a proxy: the run must be
|
|
748
|
+
* ASSIGNED (a run showing FALLBACK holds the component's own nodes, and adopting those
|
|
749
|
+
* would make a component's fallback into the user's content); the node must lie BETWEEN
|
|
750
|
+
* the anchors, not merely share their parent, or anything dropped elsewhere in that
|
|
751
|
+
* element would be pulled into the slot; and the node's own slot name must MATCH the
|
|
752
|
+
* binding it landed in, so a stray element in a named slot's run is left alone rather
|
|
753
|
+
* than filed under a name it never claimed.
|
|
754
|
+
*
|
|
755
|
+
* Measured before keeping the scan over bindings: with 500 rows rendered inside a host,
|
|
756
|
+
* every one of them an unstamped nested addition that reaches this loop, four bindings
|
|
757
|
+
* cost the same as one (44.5–51.5 ms against 45.1–53.9). DOM work dominates completely.
|
|
758
|
+
*/
|
|
759
|
+
for (const binding of state._bindings)
|
|
760
|
+
if (binding._assigned &&
|
|
761
|
+
binding._start.parentNode === record.target &&
|
|
762
|
+
|
|
763
|
+
(binding._start.compareDocumentPosition(node) & 4) !== 0 &&
|
|
764
|
+
|
|
765
|
+
(node.compareDocumentPosition(binding._end) & 4) !== 0 &&
|
|
766
|
+
slotNameOf(node) === binding._name) {
|
|
767
|
+
take(state, node);
|
|
768
|
+
touched.add(binding._name);
|
|
769
|
+
break;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
for (const node of record.removedNodes) {
|
|
774
|
+
const name = state._names.get(node);
|
|
775
|
+
/**
|
|
776
|
+
* Our evacuations were drained, so an undrained removal of a captured node is the user's.
|
|
777
|
+
* The question is only whether it is still OURS to hold, and there are three answers:
|
|
778
|
+
* detached entirely (`parentNode === null`) is gone; the holding fragment or anywhere in
|
|
779
|
+
* the host's own subtree is a MOVE the add/attribute handling covers — a keyed row created
|
|
780
|
+
* under the host and positioned inside the component in one batch is exactly that; and
|
|
781
|
+
* anywhere else is the user taking the node for themselves.
|
|
782
|
+
*
|
|
783
|
+
* That third case used to read as a move and kept the node captured forever: it was no
|
|
784
|
+
* longer in the run, so the slot showed nothing — not the node, and not its fallback — where
|
|
785
|
+
* a shadow root un-assigns and falls back. `fill` already had the corresponding guard
|
|
786
|
+
* ("the user took this node") but nothing brought it a reason to run.
|
|
787
|
+
*/
|
|
788
|
+
if (name !== undefined &&
|
|
789
|
+
node.parentNode !== state._holding &&
|
|
790
|
+
!host.contains(node) &&
|
|
791
|
+
!inAnyRun(state, node)) {
|
|
792
|
+
pull(state, node, name);
|
|
793
|
+
state._names.delete(node);
|
|
794
|
+
touched.add(name);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
for (const name of touched)
|
|
799
|
+
refill(state, name);
|
|
800
|
+
for (const binding of moved)
|
|
801
|
+
fill(state, binding);
|
|
802
|
+
};
|
|
803
|
+
const onMutations = (host, records) => {
|
|
804
|
+
const state = HOSTS.get(host);
|
|
805
|
+
processRecords(host, state, records);
|
|
806
|
+
drain(state);
|
|
807
|
+
};
|
|
808
|
+
/** Capture the host's children — once, at the first slot the seam hands us for it. */
|
|
809
|
+
const capture = (host, skipChildren = false, boundary) => {
|
|
810
|
+
let state = HOSTS.get(host);
|
|
811
|
+
if (state !== undefined)
|
|
812
|
+
return state;
|
|
813
|
+
const doc = host.ownerDocument;
|
|
814
|
+
const created = (state = {
|
|
815
|
+
_host: host,
|
|
816
|
+
_flushing: false,
|
|
817
|
+
_map: new Map(),
|
|
818
|
+
_bindings: [],
|
|
819
|
+
_holding: doc.createDocumentFragment(),
|
|
820
|
+
_parks: new WeakSet(),
|
|
821
|
+
_rank: new WeakMap(),
|
|
822
|
+
_next: 0,
|
|
823
|
+
_min: 0,
|
|
824
|
+
_names: new WeakMap(),
|
|
825
|
+
_ghosts: new WeakMap(),
|
|
826
|
+
_event: (doc.defaultView?.Event ?? Event),
|
|
827
|
+
_observer: new MutationObserver((records) => onMutations(host, records)),
|
|
828
|
+
/**
|
|
829
|
+
* **The renderer's own root marker, when it has one** — it already delimits where the render's
|
|
830
|
+
* output begins, which is exactly this boundary, so a sentinel of our own was a second comment
|
|
831
|
+
* saying the same thing one position to the left. Only the paths that reach `capture` WITHOUT
|
|
832
|
+
* one (hydration adopts per slot, and its render's marker is not in hand there) still mint it.
|
|
833
|
+
*/
|
|
834
|
+
_sentinel: boundary ?? doc.createComment(''),
|
|
835
|
+
});
|
|
836
|
+
HOSTS.set(host, created);
|
|
837
|
+
/**
|
|
838
|
+
* The host is MARKED as captured, on itself — this is what the renderer's stamp gate reads in
|
|
839
|
+
* `_insert`/`$c`: one own-property read instead of a seam call, and it cannot go stale in the
|
|
840
|
+
* dangerous direction (a part committing into a not-yet-captured host reads undefined, stamps
|
|
841
|
+
* nothing, and that content is exactly what the initial walk below lifts). Symmetric with the
|
|
842
|
+
* node stamps: ownership facts live on the objects they describe.
|
|
843
|
+
*/
|
|
844
|
+
Object.defineProperty(host, '_$hosted$', HOSTED);
|
|
845
|
+
/** Ours to place only if ours to make; the renderer's is already in the document. */
|
|
846
|
+
if (boundary === undefined)
|
|
847
|
+
host.appendChild(created._sentinel);
|
|
848
|
+
/**
|
|
849
|
+
* A server render parks content no slot claimed in an inert `<template>` — recover it into
|
|
850
|
+
* holding (captured, unrendered, ready if its slot ever mounts) and drop the carrier, so the
|
|
851
|
+
* round trip matches CSR exactly. Done before the children walk so the carrier is never itself
|
|
852
|
+
* mistaken for slot content.
|
|
853
|
+
*/
|
|
854
|
+
for (const child of [...host.children])
|
|
855
|
+
if (child.localName === 'template' && child.hasAttribute(UNASSIGNED_MARK)) {
|
|
856
|
+
const held = child.content;
|
|
857
|
+
for (const node of [...held.childNodes])
|
|
858
|
+
take(created, node, true);
|
|
859
|
+
host.removeChild(child);
|
|
860
|
+
}
|
|
861
|
+
/** Hydration already has the children distributed and registers them itself; a fresh CSR
|
|
862
|
+
* capture lifts them from the host. */
|
|
863
|
+
if (!skipChildren) {
|
|
864
|
+
/** Comments seen since the last member become its landmarks — see LANDMARKS. */
|
|
865
|
+
let pending = null;
|
|
866
|
+
for (const node of [...host.childNodes]) {
|
|
867
|
+
if (node.nodeType === 8)
|
|
868
|
+
(pending ??= []).push(node);
|
|
869
|
+
else if (take(created, node, true) !== null && pending !== null) {
|
|
870
|
+
for (const mark of pending)
|
|
871
|
+
LANDMARKS.set(mark, node);
|
|
872
|
+
pending = null;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
created._observer.observe(host, WATCHING);
|
|
877
|
+
/**
|
|
878
|
+
* HOLDING IS WATCHED TOO. Unassigned nodes wait in a detached fragment, which is not in the
|
|
879
|
+
* host's subtree — so re-slotting one (`slot="a"` → `"b"`) went unseen and the node never moved
|
|
880
|
+
* to its new slot, while native re-assigns a light child whether or not it is currently
|
|
881
|
+
* assigned (measured: displayed nodes re-slotted, held ones silently did not).
|
|
882
|
+
*/
|
|
883
|
+
created._observer.observe(created._holding, WATCHING);
|
|
884
|
+
return created;
|
|
885
|
+
};
|
|
886
|
+
/** The seam function — called by the renderer once per `<slot>` per instance (see the seam). */
|
|
887
|
+
const takeOverSlot = (slot, root, name) => {
|
|
888
|
+
/**
|
|
889
|
+
* The SERVER declines the client path entirely: SSR renders once and distributes through
|
|
890
|
+
* `_$server$` (markerless, no observer, no anchors). If this ran under the shim it would insert
|
|
891
|
+
* anchors and capture into a fragment, fighting the server pass. `__veraSsrShimmed` is the flag
|
|
892
|
+
* SSR sets when it installs the DOM.
|
|
893
|
+
*/
|
|
894
|
+
if (globalThis.__veraSsrShimmed)
|
|
895
|
+
return null;
|
|
896
|
+
/** Only an element host is ours: a shadow root keeps native slotting, and a fragment or
|
|
897
|
+
* document container is not a light component. Duck-typed — realm-safe for pop-outs. */
|
|
898
|
+
if (root.nodeType !== 1)
|
|
899
|
+
return null;
|
|
900
|
+
const host = root;
|
|
901
|
+
/**
|
|
902
|
+
* **A `<slot>` nested inside ANOTHER slot's fallback has already been detached by the time we
|
|
903
|
+
* reach it.** Slots mount in document order, and taking over the outer one lifts its fallback
|
|
904
|
+
* children out of the tree and into `_fallback` — the inner slot among them. Reaching for
|
|
905
|
+
* `parentNode.insertBefore` then threw a TypeError straight out of `renderInto`, taking the whole
|
|
906
|
+
* render with it.
|
|
907
|
+
*
|
|
908
|
+
* Declining leaves it a literal `<slot>` inside that fallback: inert, showing its own fallback
|
|
909
|
+
* children if the outer slot ever falls back. That is a documented divergence from the platform,
|
|
910
|
+
* where an inner slot DOES participate while the outer one shows its fallback — supporting that
|
|
911
|
+
* means taking slots over at the moment a fallback is inserted, which is a feature rather than
|
|
912
|
+
* this fix. What matters here is that markup the platform accepts cannot crash the renderer.
|
|
913
|
+
*/
|
|
914
|
+
if (slot.parentNode === null)
|
|
915
|
+
return null;
|
|
916
|
+
const nestedStates = [];
|
|
917
|
+
const state = capture(host);
|
|
918
|
+
const doc = slot.ownerDocument;
|
|
919
|
+
const parent = slot.parentNode;
|
|
920
|
+
const start = doc.createComment('');
|
|
921
|
+
const end = doc.createComment('');
|
|
922
|
+
parent.insertBefore(start, slot);
|
|
923
|
+
parent.insertBefore(end, slot);
|
|
924
|
+
/**
|
|
925
|
+
* **Slots inside this one's FALLBACK are taken over first, while they still have a parent.**
|
|
926
|
+
*
|
|
927
|
+
* The renderer hands slots over in document order, so the outer one arrives first — and the first
|
|
928
|
+
* thing it used to do was detach its fallback children, taking any nested slot out of the tree
|
|
929
|
+
* with them. By the time that inner slot's turn came it had no parent to anchor into: it threw
|
|
930
|
+
* until a guard declined it, and declining left it a literal `<slot>` that showed its own
|
|
931
|
+
* fallback instead of what it was assigned, whenever the outer one later fell back. Measured
|
|
932
|
+
* against a shadow root, which re-slots it: three arrangements, three different wrong answers,
|
|
933
|
+
* one of them showing nothing at all.
|
|
934
|
+
*
|
|
935
|
+
* Doing it here rather than reordering the renderer's mount keeps binding order in TREE order,
|
|
936
|
+
* which is what decides duplicate names. Deepest first, so a slot nested two levels down is live
|
|
937
|
+
* before the one containing it captures it. The renderer still visits these afterwards and the
|
|
938
|
+
* parentless guard declines them, so nothing is taken over twice.
|
|
939
|
+
*/
|
|
940
|
+
const nested = [...slot.querySelectorAll('slot')].reverse();
|
|
941
|
+
for (const inner of nested) {
|
|
942
|
+
const innerState = takeOverSlot(inner, root, inner.getAttribute('name') ?? '');
|
|
943
|
+
if (innerState !== null)
|
|
944
|
+
nestedStates.push(innerState);
|
|
945
|
+
}
|
|
946
|
+
const fallback = doc.createDocumentFragment();
|
|
947
|
+
state._parks.add(fallback);
|
|
948
|
+
/**
|
|
949
|
+
* **And a park is watched too, for exactly the reason holding is.** A displaced fallback is a
|
|
950
|
+
* second detached place captured nodes rest in: a slot nested here goes on distributing while the
|
|
951
|
+
* outer slot is assigned, so its content sits in this fragment, outside the host's subtree. Left
|
|
952
|
+
* unwatched, re-slotting one of those nodes (`slot="i"` → `""`) was invisible and it never moved
|
|
953
|
+
* — the same defect `_holding` already carries a comment about, one level further out.
|
|
954
|
+
*/
|
|
955
|
+
state._observer.observe(fallback, WATCHING);
|
|
956
|
+
while (slot.firstChild !== null)
|
|
957
|
+
fallback.appendChild(slot.firstChild);
|
|
958
|
+
/** Out of the document, but KEPT — it is the component's handle on this slot. */
|
|
959
|
+
parent.removeChild(slot);
|
|
960
|
+
const binding = {
|
|
961
|
+
_start: start,
|
|
962
|
+
_end: end,
|
|
963
|
+
_name: name,
|
|
964
|
+
_fallback: fallback,
|
|
965
|
+
_assigned: false,
|
|
966
|
+
_slot: slot,
|
|
967
|
+
_shown: [],
|
|
968
|
+
_queued: false,
|
|
969
|
+
};
|
|
970
|
+
const seam = bind(state, binding);
|
|
971
|
+
fill(state, binding);
|
|
972
|
+
drain(state);
|
|
973
|
+
/** Parking this slot has to park the ones living in its fallback, or a branch-away would strand
|
|
974
|
+
* their bindings while their nodes go with the discarded DOM. */
|
|
975
|
+
if (nestedStates.length > 0) {
|
|
976
|
+
const own = seam._$park$;
|
|
977
|
+
const inner = nestedStates;
|
|
978
|
+
seam._$park$ = () => {
|
|
979
|
+
for (const state of inner)
|
|
980
|
+
state._$park$();
|
|
981
|
+
own();
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
return seam;
|
|
985
|
+
};
|
|
986
|
+
/**
|
|
987
|
+
* **The capture map records membership, not order.** Native `assignedNodes()` answers in flat-tree
|
|
988
|
+
* order, so this has to as well, and the bucket's own order is the order things were CAPTURED —
|
|
989
|
+
* which stops being the document's the moment anything reorders the distributed nodes. A keyed list
|
|
990
|
+
* rendering a host's children is exactly that: the DOM came out `cab` and this still said `abc`,
|
|
991
|
+
* while the same component in shadow mode said `cab`. The observer cannot fix it either — it
|
|
992
|
+
* deliberately ignores moves inside the component's own tree, or it would react to every render.
|
|
993
|
+
*
|
|
994
|
+
* The common case is a handful of nodes sharing one parent, so that is the fast path: one walk of
|
|
995
|
+
* that parent's children. Members split across parents — some distributed, some still parked in the
|
|
996
|
+
* holding template — fall back to comparing positions, which is exact wherever they are.
|
|
997
|
+
*/
|
|
998
|
+
const inDocumentOrder = (bucket) => {
|
|
999
|
+
if (bucket === undefined || bucket.length < 2)
|
|
1000
|
+
return bucket === undefined ? [] : [...bucket];
|
|
1001
|
+
const parent = bucket[0].parentNode;
|
|
1002
|
+
let shared = parent !== null;
|
|
1003
|
+
for (let i = 1; shared && i < bucket.length; i++)
|
|
1004
|
+
if (bucket[i].parentNode !== parent)
|
|
1005
|
+
shared = false;
|
|
1006
|
+
if (!shared)
|
|
1007
|
+
return [...bucket].sort((a, b) =>
|
|
1008
|
+
|
|
1009
|
+
a === b ? 0 : a.compareDocumentPosition(b) & 4 ? -1 : 1);
|
|
1010
|
+
const members = new Set(bucket);
|
|
1011
|
+
const ordered = [];
|
|
1012
|
+
for (let node = parent.firstChild; node !== null; node = node.nextSibling)
|
|
1013
|
+
if (members.has(node))
|
|
1014
|
+
ordered.push(node);
|
|
1015
|
+
return ordered;
|
|
1016
|
+
};
|
|
1017
|
+
/**
|
|
1018
|
+
* What the user slotted, by name — the component-internal accessor that answers identically in
|
|
1019
|
+
* both modes. Shadow: the native assignment. Light: the capture map, in document order (a fresh
|
|
1020
|
+
* array; membership is live, so ask again after mutations). `''`/omitted is the default slot.
|
|
1021
|
+
*/
|
|
1022
|
+
const slotted = (host, name = '') => {
|
|
1023
|
+
const state = HOSTS.get(host);
|
|
1024
|
+
if (state !== undefined)
|
|
1025
|
+
return inDocumentOrder(state._map.get(name));
|
|
1026
|
+
/**
|
|
1027
|
+
* **`_root` before `shadowRoot`, because a CLOSED root is not reachable through `shadowRoot`** —
|
|
1028
|
+
* it is null there, and reading only that made this return `[]` for a closed component: a silent
|
|
1029
|
+
* wrong answer from an accessor documented as answering in either mode. Core keeps the root it
|
|
1030
|
+
* attached, in both modes, and exempts `_root` from property mangling precisely so other bundles
|
|
1031
|
+
* can read it; `@verajs/styles` already does, for this same reason.
|
|
1032
|
+
*
|
|
1033
|
+
* Written as a QUOTED access because this bundle mangles `_[a-z]` properties and core's does not
|
|
1034
|
+
* mangle this one — `keep_quoted` is what keeps the two spellings the same name in production.
|
|
1035
|
+
*/
|
|
1036
|
+
const root = host['_root'] ?? host.shadowRoot;
|
|
1037
|
+
if (root != null) {
|
|
1038
|
+
/**
|
|
1039
|
+
* Matched by READING each slot's name, never by interpolating one into a selector: a name
|
|
1040
|
+
* carrying a quote made `slot[name="…"]` an invalid selector and threw a DOMException out of
|
|
1041
|
+
* a public accessor, and `slot:not([name])` missed `<slot name="">` — which the platform
|
|
1042
|
+
* counts as the default slot (measured against native `assignedNodes`). One rule,
|
|
1043
|
+
* `(getAttribute('name') ?? '') === name`, is the platform's own and covers both.
|
|
1044
|
+
*/
|
|
1045
|
+
for (const slot of root.querySelectorAll('slot'))
|
|
1046
|
+
if ((slot.getAttribute('name') ?? '') === name)
|
|
1047
|
+
return slot.assignedNodes();
|
|
1048
|
+
}
|
|
1049
|
+
return [];
|
|
1050
|
+
};
|
|
1051
|
+
/**
|
|
1052
|
+
* SERVER distribution — a self-contained, observer-free, anchor-free pass for SSR. The client
|
|
1053
|
+
* path (capture + anchors + a live observer) is meaningless on a server that renders once and
|
|
1054
|
+
* flattens templates through its own serializer, so `@verajs/ssr` calls THIS instead, reached
|
|
1055
|
+
* off the insert as `_$server$` (like `_$capture$`) so ssr needs no import of this module.
|
|
1056
|
+
*
|
|
1057
|
+
* Input: the host after its template rendered — `source` are the user's original children
|
|
1058
|
+
* (snapshotted before the template ran), and the host also contains the rendered template with
|
|
1059
|
+
* literal `<slot>` elements. Output: markerless distributed light DOM — each `<slot>` UNWRAPPED
|
|
1060
|
+
* to its assigned nodes (or its own fallback children), the source consumed, and one attribute
|
|
1061
|
+
* (`data-vm-slotted="offset,count"`) when the DEFAULT slot received content, which is all
|
|
1062
|
+
* hydration needs to tell assigned-from-fallback there.
|
|
1063
|
+
*/
|
|
1064
|
+
/**
|
|
1065
|
+
* **`"offset,count"`, on the default slot's PARENT — position, not just extent.** A named slot's
|
|
1066
|
+
* content self-identifies (the user's nodes carry their own `slot="x"`); the default slot's does
|
|
1067
|
+
* not, because bare text is common there and cannot carry an attribute, so the server states it.
|
|
1068
|
+
*
|
|
1069
|
+
* The count alone is enough to ADOPT — the adoption walk arrives already standing at the right
|
|
1070
|
+
* place. It is not enough to RECOVER, and recovery is the case that matters: when hydration hits
|
|
1071
|
+
* a mismatch it discards the container and clean-renders, and for a light host the user's slotted
|
|
1072
|
+
* content is *inside* what gets discarded. Marked with only a count on the host, content whose
|
|
1073
|
+
* slot the walk never reached could not be found again and was destroyed — silently, under a
|
|
1074
|
+
* warning that promised the page was still correct. With the position stated, `_$rescue$` lifts
|
|
1075
|
+
* the user's nodes back out before the discard, no walk required, and the clean render
|
|
1076
|
+
* redistributes them exactly as it would on a first client render.
|
|
1077
|
+
*/
|
|
1078
|
+
const SLOTTED_ATTR = 'data-vm-slotted';
|
|
1079
|
+
/**
|
|
1080
|
+
* Unassigned slot content is PRESERVED, not dropped — native leaves an unassigned light child in
|
|
1081
|
+
* the DOM (present, unrendered), and a light host has no second tree to hide it in, so the server
|
|
1082
|
+
* parks it in an inert `<template>` (exactly what the element is for: parsed, never rendered).
|
|
1083
|
+
* Hydration drains it back into holding, so content for a slot that only appears in another state
|
|
1084
|
+
* survives the round trip instead of vanishing from the HTML forever.
|
|
1085
|
+
*/
|
|
1086
|
+
const UNASSIGNED_MARK = 'data-vm-unassigned';
|
|
1087
|
+
const serverDistribute = (host, source) => {
|
|
1088
|
+
const buckets = new Map();
|
|
1089
|
+
for (const node of source) {
|
|
1090
|
+
const name = slotNameOf(node);
|
|
1091
|
+
if (name === null)
|
|
1092
|
+
continue;
|
|
1093
|
+
let bucket = buckets.get(name);
|
|
1094
|
+
if (bucket === undefined)
|
|
1095
|
+
buckets.set(name, (bucket = []));
|
|
1096
|
+
bucket.push(node);
|
|
1097
|
+
if (node.parentNode !== null)
|
|
1098
|
+
node.parentNode.removeChild(node);
|
|
1099
|
+
}
|
|
1100
|
+
const filled = new Set();
|
|
1101
|
+
/** Parents carrying a mark, with the user's first and last node — see the separator pass. */
|
|
1102
|
+
const marked = [];
|
|
1103
|
+
/** Collect first (the live list mutates as slots are unwrapped). Any nesting order is fine —
|
|
1104
|
+
* a slot is replaced by its content, and a slot inside assigned content was itself resolved. */
|
|
1105
|
+
const slotEls = [...host.querySelectorAll('slot')];
|
|
1106
|
+
for (const slot of slotEls) {
|
|
1107
|
+
const parent = slot.parentNode;
|
|
1108
|
+
if (parent === null)
|
|
1109
|
+
continue; // already unwrapped as another slot's assigned content
|
|
1110
|
+
const name = slot.getAttribute('name') ?? '';
|
|
1111
|
+
const assigned = !filled.has(name) ? buckets.get(name) : undefined;
|
|
1112
|
+
if (assigned !== undefined && assigned.length > 0) {
|
|
1113
|
+
filled.add(name);
|
|
1114
|
+
for (const node of assigned)
|
|
1115
|
+
parent.insertBefore(node, slot);
|
|
1116
|
+
/** The DEFAULT slot's content is unmarkable in the body (it may be bare text), so its
|
|
1117
|
+
* PARENT states where it is and how much of it there is. Named slots self-delimit by
|
|
1118
|
+
* their own `slot` attribute and need nothing. Offset is stable: slots are unwrapped in
|
|
1119
|
+
* document order, so everything before this one is already final. */
|
|
1120
|
+
/** The mark is written after the separator pass — see it for why position is computed once,
|
|
1121
|
+
* at the end, rather than here where the nodes are still moving. */
|
|
1122
|
+
if (name === '')
|
|
1123
|
+
marked.push({
|
|
1124
|
+
parent: parent,
|
|
1125
|
+
first: assigned[0],
|
|
1126
|
+
last: assigned[assigned.length - 1],
|
|
1127
|
+
count: assigned.length,
|
|
1128
|
+
});
|
|
1129
|
+
}
|
|
1130
|
+
else {
|
|
1131
|
+
/** Fallback: the slot's own children, unwrapped in place. */
|
|
1132
|
+
let child = slot.firstChild;
|
|
1133
|
+
while (child !== null) {
|
|
1134
|
+
const next = child.nextSibling;
|
|
1135
|
+
parent.insertBefore(child, slot);
|
|
1136
|
+
child = next;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
parent.removeChild(slot);
|
|
1140
|
+
}
|
|
1141
|
+
/** Whatever no slot claimed goes into the inert carrier, in its original order per name. */
|
|
1142
|
+
/**
|
|
1143
|
+
* **Separators where two text runs would MERGE, because serialisation is where node identity
|
|
1144
|
+
* dies.** The `offset,count` mark counts nodes as they are HERE; the client's parser joins
|
|
1145
|
+
* adjacent text into one node, and the mark then addresses a node spanning a boundary it cannot
|
|
1146
|
+
* see. Both edges of the user's content are at risk and each corrupts a different reader:
|
|
1147
|
+
*
|
|
1148
|
+
* - the TRAILING edge breaks adoption's count — measured, `<main><slot>fb</slot> TAIL</main>`
|
|
1149
|
+
* served "BODY TAIL" and hydrated to "BODY TAIL TAIL", the static text adopted twice.
|
|
1150
|
+
* - the LEADING edge breaks the offset, which only `rescue` reads — so a hydration bail would
|
|
1151
|
+
* slice the wrong range and keep the component's markup instead of the user's.
|
|
1152
|
+
*
|
|
1153
|
+
* Run AFTER the slot loop, because until every slot is unwrapped the neighbour of a boundary is
|
|
1154
|
+
* still a `<slot>` element and the merge is not yet visible. Emitted by the side that KNOWS: the
|
|
1155
|
+
* alternative was to have hydration infer the boundary from the canonical template, which works
|
|
1156
|
+
* for the shape in front of you and needs a new case for each thing that can follow a slot
|
|
1157
|
+
* (static text, another default slot's fallback, a named slot's fallback, and whether that named
|
|
1158
|
+
* slot receives content at all) — one rule here removes the class instead of handling members of
|
|
1159
|
+
* it. React emits the same 7 bytes for the same reason.
|
|
1160
|
+
*/
|
|
1161
|
+
for (const { first, last } of marked) {
|
|
1162
|
+
const doc = host.ownerDocument;
|
|
1163
|
+
const ahead = first.previousSibling;
|
|
1164
|
+
if (ahead !== null && ahead.nodeType === 3 && first.nodeType === 3)
|
|
1165
|
+
first.parentNode.insertBefore(doc.createComment(''), first);
|
|
1166
|
+
const behind = last.nextSibling;
|
|
1167
|
+
if (behind !== null && behind.nodeType === 3 && last.nodeType === 3)
|
|
1168
|
+
last.parentNode.insertBefore(doc.createComment(''), behind);
|
|
1169
|
+
}
|
|
1170
|
+
let carrier = null;
|
|
1171
|
+
for (const [name, bucket] of buckets) {
|
|
1172
|
+
if (filled.has(name) || bucket.length === 0)
|
|
1173
|
+
continue;
|
|
1174
|
+
if (carrier === null) {
|
|
1175
|
+
carrier = host.ownerDocument.createElement('template');
|
|
1176
|
+
carrier.setAttribute(UNASSIGNED_MARK, '');
|
|
1177
|
+
}
|
|
1178
|
+
for (const node of bucket)
|
|
1179
|
+
carrier.appendChild(node);
|
|
1180
|
+
}
|
|
1181
|
+
if (carrier !== null)
|
|
1182
|
+
host.appendChild(carrier);
|
|
1183
|
+
/**
|
|
1184
|
+
* **The mark is written LAST, because it records a POSITION and position is only true once
|
|
1185
|
+
* nothing else will move.** Computing it in the slot loop and then inserting separators put the
|
|
1186
|
+
* two out of step by exactly one node: the recorded offset addressed the separator rather than
|
|
1187
|
+
* the content, and the rescue — the only reader of the offset — kept nothing, so a hydration
|
|
1188
|
+
* bail showed the component's own fallback with the user's content gone. Every hydration test
|
|
1189
|
+
* still passed, because adoption reads only the count.
|
|
1190
|
+
*
|
|
1191
|
+
* Writing it here rather than merely after the separators is the difference between a fix and a
|
|
1192
|
+
* rule: the carrier append above, and anything added below it later, cannot silently reintroduce
|
|
1193
|
+
* the same defect. One place computes position, and it is downstream of every pass that moves a
|
|
1194
|
+
* node — which is a property of the ORDER, not of remembering to check.
|
|
1195
|
+
*/
|
|
1196
|
+
for (const { parent, first, count } of marked) {
|
|
1197
|
+
let offset = 0;
|
|
1198
|
+
for (let n = parent.firstChild; n !== null && n !== first; n = n.nextSibling)
|
|
1199
|
+
offset++;
|
|
1200
|
+
parent.setAttribute(SLOTTED_ATTR, `${offset},${count}`);
|
|
1201
|
+
}
|
|
1202
|
+
};
|
|
1203
|
+
/**
|
|
1204
|
+
* Capture a light host's children at its FIRST render, before any slot has necessarily mounted —
|
|
1205
|
+
* so content destined for a slot that only appears later (a conditional `<slot>` behind a branch)
|
|
1206
|
+
* is held invisibly meanwhile, exactly as native shadow DOM leaves an unassigned light child
|
|
1207
|
+
* unrendered. Idempotent (capture is once per host); the renderer calls it once per host lifetime.
|
|
1208
|
+
*/
|
|
1209
|
+
takeOverSlot._$capture$ = (host, boundary) => {
|
|
1210
|
+
if (globalThis.__veraSsrShimmed)
|
|
1211
|
+
return;
|
|
1212
|
+
capture(host, false, boundary);
|
|
1213
|
+
drain(HOSTS.get(host));
|
|
1214
|
+
};
|
|
1215
|
+
/**
|
|
1216
|
+
* A captured node's HOME — the sentinel marking the end of its host's light region. The renderer's
|
|
1217
|
+
* text-part upgrade calls this: markers for a part whose text node was captured belong in the
|
|
1218
|
+
* HOST, not wherever distribution carried the node (see `_sentinel`). Returns null for a node no
|
|
1219
|
+
* light host has captured, which is every node in an app without light slots.
|
|
1220
|
+
*/
|
|
1221
|
+
takeOverSlot._$home$ = (node) => {
|
|
1222
|
+
const sentinel = HOMES.get(node);
|
|
1223
|
+
return sentinel !== undefined && sentinel.parentNode !== null ? sentinel : null;
|
|
1224
|
+
};
|
|
1225
|
+
/** The server hook — SSR calls this (never the client capture/anchor path). */
|
|
1226
|
+
takeOverSlot._$server$ = serverDistribute;
|
|
1227
|
+
/**
|
|
1228
|
+
* HYDRATION adopt — wrap already-distributed server nodes as a live binding IN PLACE, so a
|
|
1229
|
+
* server-rendered slot component becomes fully interactive with zero node churn: the user's nodes
|
|
1230
|
+
* keep their identity (focus, input values), and the capture map + observer come alive for
|
|
1231
|
+
* re-renders and mutations. Called by the hydrate entry once per `<slot>` it reconciles.
|
|
1232
|
+
*
|
|
1233
|
+
* `assigned` are the user's nodes already sitting at the slot position (null/empty ⇒ the slot
|
|
1234
|
+
* showed its fallback, which is `fallback` — those nodes are already in place too). `parent` and
|
|
1235
|
+
* `before` bound where anchors go. Returns the binding's park state, exactly like `takeOverSlot`.
|
|
1236
|
+
*/
|
|
1237
|
+
const adoptSlot = (host, name, assigned, fallback, parent, before,
|
|
1238
|
+
/** The hydrator's per-instance `<slot>` — a shallow clone of the canonical one, carrying this
|
|
1239
|
+
* instance's committed bindings. Appended, so a slots build paired with an older hydrate build
|
|
1240
|
+
* degrades to no element rather than breaking. */
|
|
1241
|
+
slot) => {
|
|
1242
|
+
const state = capture(host, /* skipChildren */ true);
|
|
1243
|
+
const doc = host.ownerDocument;
|
|
1244
|
+
const start = doc.createComment('');
|
|
1245
|
+
const end = doc.createComment('');
|
|
1246
|
+
/** Anchors bracket whatever is shown (assigned nodes, or the fallback the server rendered). */
|
|
1247
|
+
const firstShown = (assigned && assigned.length > 0 ? assigned[0] : fallback[0]) ?? before;
|
|
1248
|
+
parent.insertBefore(start, firstShown ?? before);
|
|
1249
|
+
parent.insertBefore(end, before);
|
|
1250
|
+
const isAssigned = assigned !== null && assigned.length > 0;
|
|
1251
|
+
/**
|
|
1252
|
+
* `_fallback` holds the DISPLACED region and is empty while the region is on screen, so which
|
|
1253
|
+
* branch this is decides where these nodes belong. Assigned: the server never rendered the
|
|
1254
|
+
* fallback and these are clones off the canonical template, displaced from the start — they go
|
|
1255
|
+
* into the fragment. Unassigned: the server DID render them and they are live in the page
|
|
1256
|
+
* between the anchors, so they stay exactly where they are and the fragment starts empty.
|
|
1257
|
+
* Moving them would delete server output from the document.
|
|
1258
|
+
*/
|
|
1259
|
+
const held = doc.createDocumentFragment();
|
|
1260
|
+
state._parks.add(held);
|
|
1261
|
+
state._observer.observe(held, WATCHING);
|
|
1262
|
+
if (isAssigned)
|
|
1263
|
+
for (const node of fallback)
|
|
1264
|
+
held.appendChild(node);
|
|
1265
|
+
if (isAssigned)
|
|
1266
|
+
for (const node of assigned) {
|
|
1267
|
+
bucketOf(state, name).push(node);
|
|
1268
|
+
state._names.set(node, name);
|
|
1269
|
+
/**
|
|
1270
|
+
* RANKED, like every captured node — the adoption walk visits slots in document order and
|
|
1271
|
+
* the server preserved within-name order, so sequential ranks reproduce the light tree.
|
|
1272
|
+
* Without this, adopted nodes had NO rank: every later rank-ordered merge (a re-slot's
|
|
1273
|
+
* splice, take's interpolation) compared against `undefined` and misplaced them — found by
|
|
1274
|
+
* the run-18 adopted-seam storms as membership-right, ORDER-wrong divergences from native.
|
|
1275
|
+
*/
|
|
1276
|
+
state._rank.set(node, state._next++);
|
|
1277
|
+
}
|
|
1278
|
+
const binding = {
|
|
1279
|
+
_start: start,
|
|
1280
|
+
_end: end,
|
|
1281
|
+
_name: name,
|
|
1282
|
+
_fallback: held,
|
|
1283
|
+
_assigned: isAssigned,
|
|
1284
|
+
_slot: slot,
|
|
1285
|
+
_shown: [],
|
|
1286
|
+
_queued: false,
|
|
1287
|
+
};
|
|
1288
|
+
const seam = bind(state, binding);
|
|
1289
|
+
/** The server already put the assignment in place, so nothing is filled here — but a slot that
|
|
1290
|
+
* HAS an assignment has just been flattened, and the platform fires `slotchange` for that. A
|
|
1291
|
+
* hydrated component therefore hears the same first event a client-rendered one does. */
|
|
1292
|
+
if (isAssigned) {
|
|
1293
|
+
binding._shown = assigned.slice();
|
|
1294
|
+
signal(state, binding);
|
|
1295
|
+
}
|
|
1296
|
+
drain(state);
|
|
1297
|
+
return seam;
|
|
1298
|
+
};
|
|
1299
|
+
takeOverSlot._$adopt$ = adoptSlot;
|
|
1300
|
+
/**
|
|
1301
|
+
* HYDRATION rescue — **the user's content must survive a mismatch.** When adoption fails, the
|
|
1302
|
+
* hydrator discards the container's server markup and clean-renders it; for a light host the
|
|
1303
|
+
* user's slotted nodes are *inside* that markup, so the discard destroyed them and the slots
|
|
1304
|
+
* showed fallback, under a warning promising the page was still correct. It was not: content was
|
|
1305
|
+
* gone from the page for good.
|
|
1306
|
+
*
|
|
1307
|
+
* This un-distributes instead: it lifts the user's nodes back out, using exactly the two things
|
|
1308
|
+
* the server states about them — a named node carries its own `slot`, and the default slot's
|
|
1309
|
+
* parent carries `data-vm-slotted="offset,count"` — and returns them for the caller to re-attach
|
|
1310
|
+
* as the host's children. From there nothing is special-cased: the clean render captures them the
|
|
1311
|
+
* way it captures any first client render.
|
|
1312
|
+
*
|
|
1313
|
+
* **Never descends into another component.** A nested host's children are its own source, which
|
|
1314
|
+
* it will capture (or rescue) itself when it renders; taking them here would hand one component's
|
|
1315
|
+
* content to another. A custom element is therefore collected as a node and never entered.
|
|
1316
|
+
*
|
|
1317
|
+
* One documented edge: a component whose FALLBACK content carries a `slot` attribute
|
|
1318
|
+
* (`<slot name="a"><i slot="a">…</i></slot>`) has that fallback rescued as if the user wrote it.
|
|
1319
|
+
* The attribute is meaningless on fallback in native shadow DOM too — it only means anything on a
|
|
1320
|
+
* host's children — so the markup was already saying something it does not mean.
|
|
1321
|
+
*/
|
|
1322
|
+
const rescue = (host) => {
|
|
1323
|
+
const rescued = [];
|
|
1324
|
+
const collect = (parent) => {
|
|
1325
|
+
const mark = parent.getAttribute(SLOTTED_ATTR);
|
|
1326
|
+
let from = -1;
|
|
1327
|
+
let until = -1;
|
|
1328
|
+
if (mark !== null) {
|
|
1329
|
+
const comma = mark.indexOf(',');
|
|
1330
|
+
from = Number(mark.slice(0, comma));
|
|
1331
|
+
until = from + Number(mark.slice(comma + 1));
|
|
1332
|
+
}
|
|
1333
|
+
let index = 0;
|
|
1334
|
+
for (let child = parent.firstChild; child !== null; child = child.nextSibling, index++) {
|
|
1335
|
+
if (index >= from && index < until) {
|
|
1336
|
+
rescued.push(child);
|
|
1337
|
+
continue;
|
|
1338
|
+
}
|
|
1339
|
+
if (child.nodeType !== 1)
|
|
1340
|
+
continue;
|
|
1341
|
+
const element = child;
|
|
1342
|
+
/**
|
|
1343
|
+
* The inert carrier holds what no slot claimed — user content too, and its nodes live in
|
|
1344
|
+
* `content`, not `childNodes`.
|
|
1345
|
+
*
|
|
1346
|
+
* **The tag is checked, not just the attribute.** This walks the SERVER's subtree, which is
|
|
1347
|
+
* full of the user's own markup, and `data-vm-unassigned` on anything that is not a
|
|
1348
|
+
* `<template>` reached `.content` on an element that has none: a TypeError thrown out of
|
|
1349
|
+
* `renderInto`, so the mismatch never finished falling back and the page was left with no
|
|
1350
|
+
* client render at all. Reserved attribute or not, a user's markup cannot be allowed to do
|
|
1351
|
+
* that — and `capture` was already checking both.
|
|
1352
|
+
*/
|
|
1353
|
+
if (element.localName === 'template' && element.hasAttribute(UNASSIGNED_MARK)) {
|
|
1354
|
+
const held = element.content;
|
|
1355
|
+
for (let node = held.firstChild; node !== null; node = node.nextSibling)
|
|
1356
|
+
rescued.push(node);
|
|
1357
|
+
continue;
|
|
1358
|
+
}
|
|
1359
|
+
if (element.hasAttribute('slot'))
|
|
1360
|
+
rescued.push(element);
|
|
1361
|
+
else if (element.localName.indexOf('-') === -1)
|
|
1362
|
+
collect(element);
|
|
1363
|
+
}
|
|
1364
|
+
};
|
|
1365
|
+
collect(host);
|
|
1366
|
+
for (const node of rescued)
|
|
1367
|
+
node.parentNode?.removeChild(node);
|
|
1368
|
+
return rescued.length > 0 ? rescued : null;
|
|
1369
|
+
};
|
|
1370
|
+
takeOverSlot._$rescue$ = rescue;
|
|
1371
|
+
/**
|
|
1372
|
+
* The insert descriptor — `wire([renderer, slots])` and light-DOM slots exist.
|
|
1373
|
+
*
|
|
1374
|
+
* `fn` no longer needs a cast: `'slot'` is a declared insert point now, so this is checked against
|
|
1375
|
+
* `SlotInsert` rather than asserted past the type system. It used to be `as never`, which is what a
|
|
1376
|
+
* missing insert type looks like from the inside — and from the OUTSIDE it looked like
|
|
1377
|
+
* `wire([renderer, slots])` failing to compile for every TypeScript consumer.
|
|
1378
|
+
*/
|
|
1379
|
+
const slots = {
|
|
1380
|
+
name: '@verajs/renderer/slots',
|
|
1381
|
+
on: 'slot',
|
|
1382
|
+
fn: takeOverSlot,
|
|
1383
|
+
priority: 50,
|
|
1384
|
+
};
|
|
1385
|
+
|
|
1386
|
+
export { slots, slotted };
|
|
1387
|
+
//# sourceMappingURL=vera-renderer-slots.js.map
|