@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
|
@@ -1,1387 +1,1984 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
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.
|
|
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
|
+
/**
|
|
9
|
+
* **What this copy of the framework has already said** (Brian's ruling, 2026-11-12): the ONE memory behind `warnOnce`, `errorOnce`
|
|
10
|
+
* and `hasWarned`, for DEVELOPMENT-ONLY warnings. It replaced the hand-written once-flags every package kept
|
|
11
|
+
* (`saidLists`, `warnedDrop`, per-element WeakSets, per-key Sets: two naming styles, Brian: no names). **A warning that
|
|
12
|
+
* ships to production keeps its memory at its site** (bytes, measured 2026-11-12: the shared memory costs +80–145 B gz
|
|
13
|
+
* per bundle); development-only warnings use warnOnce. Module state by necessity, since "once" spans every call on the
|
|
14
|
+
* page. One per BUNDLE COPY, exactly the scope each flag had, because this package is inlined into every bundle.
|
|
15
|
+
* A code, or `code key` for a string key, in a Set. An object key (an element, a setup function) in a WeakMap, so its
|
|
16
|
+
* entry goes with the object. Marked pure, so a bundle that never prints once drops both.
|
|
17
|
+
*/
|
|
18
|
+
const printed = /* @__PURE__ */ new Set();
|
|
19
|
+
const printedOn = /* @__PURE__ */ new WeakMap();
|
|
20
|
+
/** Where an object key's (code, value) lives: the value set for that code on that object, made on demand. */
|
|
21
|
+
const valuesOn = (on, code, make) => {
|
|
22
|
+
let codes = printedOn.get(on);
|
|
23
|
+
if (codes === undefined) {
|
|
24
|
+
printedOn.set(on, (codes = new Map()));
|
|
25
|
+
}
|
|
26
|
+
let values = codes.get(code);
|
|
27
|
+
if (values === undefined && make)
|
|
28
|
+
codes.set(code, (values = new Set()));
|
|
29
|
+
return values;
|
|
30
|
+
};
|
|
31
|
+
/** The object and value of an object or pair key. */
|
|
32
|
+
const split = (key) => (Array.isArray(key) ? key : [key, undefined]);
|
|
33
|
+
/** Whether (code, key) is new, recording it if so. */
|
|
34
|
+
const firstTime = (code, key) => {
|
|
35
|
+
if (key === undefined || typeof key === 'string') {
|
|
36
|
+
const entry = key === undefined ? code : `${code} ${key}`;
|
|
37
|
+
if (printed.has(entry))
|
|
38
|
+
return false;
|
|
39
|
+
printed.add(entry);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
const [on, sub] = split(key);
|
|
43
|
+
const values = valuesOn(on, code, true);
|
|
44
|
+
if (values.has(sub))
|
|
45
|
+
return false;
|
|
46
|
+
values.add(sub);
|
|
47
|
+
return true;
|
|
48
|
+
};
|
|
49
|
+
const diagnostic = (inArea, bySubject, withCode, andProse) =>
|
|
50
|
+
/** `true ` HERE as well as at the call: production folds this to the one-line form, branch and all. */
|
|
51
|
+
true && andProse
|
|
52
|
+
? `[vera] ${inArea}: ${bySubject} — ${andProse[0]}${andProse[1] ? ` ${andProse[1]}` : ''} (${withCode})`
|
|
53
|
+
: `[vera] ${inArea}: ${bySubject} — ${DOCS}${withCode}`;
|
|
54
|
+
/**
|
|
55
|
+
* The printer behind both, on one level. `andDetail` is `diagnostic()`'s prose, or a function returning it where building
|
|
56
|
+
* the sentence costs something: it is called only the first time. `...andMore` rides beside the line behind '%s' (an
|
|
57
|
+
* element, a thrown error), never inside it, so text from outside cannot forge a line. The console method is read at the
|
|
58
|
+
* call, so a test that replaces `console.warn` sees it.
|
|
75
59
|
*/
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
60
|
+
const sayOnce = (level) => (inArea, bySubject, withCode, andDetail, forKey, ...andMore) => {
|
|
61
|
+
if (!firstTime(withCode, forKey))
|
|
62
|
+
return;
|
|
63
|
+
const line = diagnostic(inArea, bySubject, withCode, typeof andDetail === 'function' ? andDetail() : andDetail);
|
|
64
|
+
if (andMore.length)
|
|
65
|
+
console[level]('%s', line, ...andMore);
|
|
66
|
+
else
|
|
67
|
+
console[level](line);
|
|
68
|
+
};
|
|
81
69
|
/**
|
|
82
|
-
* **
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
70
|
+
* **A diagnostic said once per page** (per key, where one is given: a string, or an object such as the element it is
|
|
71
|
+
* about): `diagnostic()`'s arguments, then the key, then what rides beside the line. Coded in production exactly as
|
|
72
|
+
* `diagnostic()` is, because it IS `diagnostic()`. `errorOnce` is its twin on `console.error`, for a refusal a page should
|
|
73
|
+
* not miss.
|
|
74
|
+
*/
|
|
75
|
+
const warnOnce = /* @__PURE__ */ sayOnce('warn');
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* core's `html`/`svg`/`mathml` and the renderer's `tag` — a tagged template called as a function. The FACT is shared;
|
|
79
|
+
* the EXAMPLE is each emitter's: `html` writes markup, `tag` an element name — so the emitter passes it.
|
|
90
80
|
*/
|
|
91
|
-
const LANDMARKS = new WeakMap();
|
|
92
81
|
/**
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* the park fragment in `takeOverSlot`.
|
|
82
|
+
* An option a module or loader does not have — autoloader(), directiveLoader(), router(), and the directives packs
|
|
83
|
+
* remote(), sensors(), motion(). One fact and one fix; the module is the line's SUBJECT and the list is its own.
|
|
96
84
|
*/
|
|
97
|
-
const
|
|
85
|
+
const unknownOption = (key, list) => [
|
|
86
|
+
`\`${key}\` is not an option, so it was ignored.`,
|
|
87
|
+
`The options are ${list}.`,
|
|
88
|
+
];
|
|
89
|
+
|
|
98
90
|
/**
|
|
99
|
-
*
|
|
91
|
+
* The hyphenated names SVG and MathML already define, which the custom-elements spec reserves — so a
|
|
92
|
+
* dash in one of these does NOT make it a custom element.
|
|
100
93
|
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
94
|
+
* Measured, the one that matters: JSX treats a dash-named tag as a custom element and compiles its
|
|
95
|
+
* attributes to PROPERTIES, which turned `<annotation-xml encoding="text/html">` into
|
|
96
|
+
* `.encoding=${…}`. The HTML parser decides that element is an integration point from the
|
|
97
|
+
* ATTRIBUTE on its start tag, so HTML content inside was moved out of the `<math>` entirely, and a
|
|
98
|
+
* custom element there was built MathML-namespaced and never upgraded — silent on the server and
|
|
99
|
+
* the client alike. That is also the exact spelling the renderer's own warning recommends.
|
|
100
|
+
*/
|
|
101
|
+
const RESERVED_ELEMENT_NAMES = new Set([
|
|
102
|
+
'annotation-xml', 'color-profile', 'font-face', 'font-face-src',
|
|
103
|
+
'font-face-uri', 'font-face-format', 'font-face-name', 'missing-glyph',
|
|
104
|
+
]);
|
|
105
|
+
/**
|
|
106
|
+
* **A custom element, by its name:** a dash, except the eight names SVG and MathML already own (above). One
|
|
107
|
+
* definition for every package that asks — `@verajs/jsx` compiles such a tag's attributes to properties, and
|
|
108
|
+
* `@verajs/renderer/slots` captures such a host's children. (The renderer's own property paths ask a narrower
|
|
109
|
+
* question, the bare dash, on its hot path; a reserved name never carries a property binding.)
|
|
110
|
+
*/
|
|
111
|
+
const isCustomElementName = (tag) => tag.includes('-') && !RESERVED_ELEMENT_NAMES.has(tag.toLowerCase());
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* **Reports an error nothing else handled, the way the platform reports one.** `reportError` fires
|
|
115
|
+
* the window's `error` event — what `window.onerror`, error trackers and test runners listen for —
|
|
116
|
+
* without unwinding the caller, so one failing hook or ref never stops its siblings. A bare
|
|
117
|
+
* `console.error` reached none of those: a component could stop updating while every page-error
|
|
118
|
+
* listener stayed silent. Off-browser (Node has no `reportError`) the console is all there is.
|
|
119
|
+
* `line` is the framework's own line, printed beside the error — a CODED one (2026-10-09):
|
|
120
|
+
* each caller passes `'[vera] <code>'` in production and its table's `diagnostic()` sentence in development, both
|
|
121
|
+
* literal codes at the call site, so the static checks read them. The subject is fixed wording too (`a hook`, `an
|
|
122
|
+
* element ref`), never a tag name: this line is the console's FORMAT (no `'%s'`, which measured +2–3 B on the renderer
|
|
123
|
+
* bundles), so nothing from outside may reach it — the element goes to the `'error'` handlers instead. Production prints it only where the platform has no
|
|
124
|
+
* `reportError` (Node); in a browser, `reportError` alone reports.
|
|
105
125
|
*
|
|
106
|
-
*
|
|
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`.
|
|
126
|
+
* Shared by core (a hook threw) and the renderer (a ref threw) so the fallback is one rule.
|
|
110
127
|
*/
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
}
|
|
128
|
+
const reportUncaught = (error, line) => {
|
|
129
|
+
if (typeof reportError !== 'function')
|
|
130
|
+
console.error(line, error);
|
|
180
131
|
else {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
132
|
+
if (true )
|
|
133
|
+
console.error(line, error);
|
|
134
|
+
reportError(error);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* **Reports what user code a render called threw — to the app's `'error'` chain, else as uncaught.** One rule for
|
|
139
|
+
* every callback the framework runs on the user's behalf and survives: a hook (core), an element ref, and an element
|
|
140
|
+
* claim's `create`/`mount`/`unmount` (the renderer). The caller catches at the call site and carries on, so one
|
|
141
|
+
* failing callback never stops its siblings or leaves a commit half applied. `element` is the component the error
|
|
142
|
+
* belongs to, handed to each handler. Every throw is reported — nothing is deduped, as a hook's never was.
|
|
143
|
+
* **Core keeps a deliberate twin** (`reportHookError`, the same rule as a `forEach`): routed through here it measured
|
|
144
|
+
* +11 B gzip on `vera.min.js` (2026-10-09). A change to the rule changes both.
|
|
145
|
+
*/
|
|
146
|
+
const reportTo = (handlers, error, element, line) => {
|
|
147
|
+
if (handlers?.length)
|
|
148
|
+
for (const handler of handlers)
|
|
149
|
+
handler(error, element);
|
|
150
|
+
else
|
|
151
|
+
reportUncaught(error, line);
|
|
152
|
+
};
|
|
153
|
+
/** The component a render root belongs to: a shadow root's host, or a light root itself. */
|
|
154
|
+
const hostOf = (root) => root == null ? undefined : root.nodeType === 11 ? root.host : root;
|
|
155
|
+
|
|
156
|
+
const PROSE$1 = {
|
|
157
|
+
'slot-attributes': (slot, attributes) => [
|
|
158
|
+
`${slot} carries ${attributes}, which does nothing in a light-DOM component: the slot element steps out of the page while it has content.`,
|
|
159
|
+
'Events, `name` and `&ref` all work here.',
|
|
160
|
+
],
|
|
161
|
+
'assigned-slot-unpatched': (kind) => [
|
|
162
|
+
`this browser defines no \`assignedSlot\` getter on ${kind}.prototype, so slots left it alone: a light child of that kind reads what the platform answers.`,
|
|
163
|
+
'Read the assignment from the slot instead: `slot.assignedNodes()`, or `slotted(host, name)`.',
|
|
164
|
+
],
|
|
165
|
+
'slots-wire-too-old': () => [
|
|
166
|
+
'the `wire` that called slots did not hand it `wire` itself, so none of slots was wired: that core is older than this module.',
|
|
167
|
+
'Update `@verajs/core` (and `@verajs/inserts`) to the version that passes `wire` to connectors, beside this renderer.',
|
|
168
|
+
],
|
|
169
|
+
'observe-slots-option': (value) => [
|
|
170
|
+
`\`observeSlots: ${value}\` is not a setting, so it reads as 'lazy' (the default): this component is observed from its first \`<slot>\` or light child.`,
|
|
171
|
+
"Set `observeSlots: 'eager'` to observe it from its first render, or leave it out.",
|
|
172
|
+
],
|
|
173
|
+
/** Two outcomes of one fact — a served page nothing hydrates — so one page, the outcome a parameter. */
|
|
174
|
+
'hydration-unwired': (outcome) => [
|
|
175
|
+
`this page was server-rendered and nothing hydrates it, so ${outcome}.`,
|
|
176
|
+
"Wire `hydration` beside the renderer — `wire([renderer, hydration])` — and, where a light host holds the page's content, `hydrateSlots` with `slots`: `wire([renderer, hydration, slots, hydrateSlots])`.",
|
|
177
|
+
],
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const PROSE = {
|
|
181
|
+
'claim-threw': (phase, outcome) => [
|
|
182
|
+
`threw in ${phase}; its error is printed beside this line, and ${outcome}.`,
|
|
183
|
+
"Fix the claim's callback, or wire an 'error' insert to handle what claims throw.",
|
|
184
|
+
],
|
|
185
|
+
'elements-hook-replaced': () => [
|
|
186
|
+
"a 'template' hook set an instance hook before `elements` (priority 10) and is replaced.",
|
|
187
|
+
'Claim elements through the `element` insert instead of a second instance hook.',
|
|
188
|
+
],
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
let registered = null;
|
|
192
|
+
/**
|
|
193
|
+
* **A claim's callbacks follow the ref rule** (Brian, 2026-10-09): user code a render calls is caught AT ITS CALL SITE
|
|
194
|
+
* — so a template with no claims runs exactly the path it did — reported to the app's `'error'` chain with the
|
|
195
|
+
* component it belongs to (else as uncaught), and the render, the other claims and the teardown carry on. A mid-commit
|
|
196
|
+
* throw once left a component's root empty for good; `mount` propagating aborted every later claim's mount.
|
|
197
|
+
*/
|
|
198
|
+
const report = (error, root, line) => reportTo(registered?.get('error'), error, hostOf(root), line);
|
|
199
|
+
/**
|
|
200
|
+
* **The next element of an instance in document pre-order, never leaving it.** A root is a fragment, or — for a
|
|
201
|
+
* single-root template — its one element, which is then position 0. Bounded by the root rather than by the tree it
|
|
202
|
+
* sits in, because a hydrated instance is already in the page.
|
|
203
|
+
*/
|
|
204
|
+
const firstIn = (root) => (root.nodeType === 1 ? root : root.firstElementChild);
|
|
205
|
+
const nextIn = (node, root) => {
|
|
206
|
+
if (node.firstElementChild !== null)
|
|
207
|
+
return node.firstElementChild;
|
|
208
|
+
for (let at = node; at !== null && at !== root; at = at.parentElement)
|
|
209
|
+
if (at.nextElementSibling !== null)
|
|
210
|
+
return at.nextElementSibling;
|
|
211
|
+
return null;
|
|
212
|
+
};
|
|
213
|
+
/** The instance hook for one template, from the positions and behaviors its claimants answered. */
|
|
214
|
+
const hookFor = (positions, behaviors) => ({
|
|
215
|
+
$c: (instance, root, adopted) => {
|
|
216
|
+
const found = [];
|
|
184
217
|
/**
|
|
185
|
-
*
|
|
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.
|
|
218
|
+
* ADOPTED, the instance arrives as its elements in template order (hydration pairs them): its live root already
|
|
219
|
+
* holds what its nested parts rendered, so counting there would land on the wrong element.
|
|
196
220
|
*/
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
last !== undefined &&
|
|
207
|
-
|
|
208
|
-
!(last.parentNode === home && (last.compareDocumentPosition(node) & 4) !== 0)) {
|
|
221
|
+
const listed = Array.isArray(instance);
|
|
222
|
+
let node = listed ? null : firstIn(instance);
|
|
223
|
+
let at = 0;
|
|
224
|
+
for (let k = 0; k < positions.length; k++) {
|
|
225
|
+
if (listed)
|
|
226
|
+
node = instance[positions[k]];
|
|
227
|
+
else
|
|
228
|
+
for (; at < positions[k]; at++)
|
|
229
|
+
node = nextIn(node, instance);
|
|
209
230
|
/**
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* left this bucket (pulled, or another slot's) by walking on; bounded by the sentinel.
|
|
231
|
+
* At creation — before the first update, and before the instance is connected anywhere. A claim whose `create`
|
|
232
|
+
* throws is reported and DROPPED for this instance (it never mounts or unmounts, being half made); the element
|
|
233
|
+
* stays as the template built it — a claim inserts nothing — and every other claim carries on. The ref rule.
|
|
214
234
|
*/
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
}
|
|
235
|
+
try {
|
|
236
|
+
behaviors[k].create?.(node, adopted);
|
|
227
237
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
238
|
+
catch (error) {
|
|
239
|
+
report(error, root, true ? diagnostic('elements', 'an element claim', 'claim-threw', true && PROSE['claim-threw']('create', 'the render continued without it')) : '[vera] claim-threw');
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
found.push(node, behaviors[k]);
|
|
243
|
+
}
|
|
244
|
+
return { f: found, r: root, a: adopted, k: undefined, d: false };
|
|
245
|
+
},
|
|
246
|
+
$m: (given) => {
|
|
247
|
+
const state = given;
|
|
248
|
+
/** Torn down before its render finished: it never mounts. */
|
|
249
|
+
if (state.d)
|
|
250
|
+
return;
|
|
251
|
+
const found = state.f;
|
|
252
|
+
const context = { root: state.r, adopted: state.a };
|
|
253
|
+
state.f = null;
|
|
254
|
+
for (let i = 0; i < found.length; i += 2) {
|
|
255
|
+
const behavior = found[i + 1];
|
|
256
|
+
let value;
|
|
257
|
+
try {
|
|
258
|
+
value = behavior.mount?.(found[i], context);
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
report(error, state.r, true ? diagnostic('elements', 'an element claim', 'claim-threw', true && PROSE['claim-threw']('mount', 'the others still mounted')) : '[vera] claim-threw');
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (value !== undefined && behavior.unmount !== undefined)
|
|
265
|
+
(state.k ??= []).push(found[i], behavior, value);
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
$q: (given) => {
|
|
269
|
+
const state = given;
|
|
270
|
+
state.d = true;
|
|
271
|
+
const kept = state.k;
|
|
272
|
+
if (kept !== undefined)
|
|
273
|
+
for (let i = 0; i < kept.length; i += 3)
|
|
274
|
+
try {
|
|
275
|
+
kept[i + 1].unmount(kept[i + 2], kept[i]);
|
|
251
276
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const member = bucket[i];
|
|
255
|
-
|
|
256
|
-
if (member.parentNode !== home || (node.compareDocumentPosition(member) & 4) !== 0) {
|
|
257
|
-
at = i;
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
report(error, state.r, true ? diagnostic('elements', 'an element claim', 'claim-threw', true && PROSE['claim-threw']('unmount', 'the rest of the teardown ran')) : '[vera] claim-threw');
|
|
260
279
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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)
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
/**
|
|
283
|
+
* Asks the claimants about the template's canonical content, once, as it is built — and gives it an instance hook
|
|
284
|
+
* only if something was claimed. The content is READ, never changed (see the `'template'` insert's contract).
|
|
285
|
+
*/
|
|
286
|
+
const claimTemplate = (built, _result, _readScope, root) => {
|
|
287
|
+
const claims = registered?.get('element');
|
|
288
|
+
if (claims === undefined || claims.length === 0)
|
|
299
289
|
return;
|
|
300
|
-
const
|
|
301
|
-
if (
|
|
302
|
-
|
|
290
|
+
const template = built;
|
|
291
|
+
if (true && template._$inst$ !== undefined)
|
|
292
|
+
console.warn(diagnostic('elements', 'template', 'elements-hook-replaced', true && PROSE['elements-hook-replaced']()));
|
|
293
|
+
const positions = [];
|
|
294
|
+
const behaviors = [];
|
|
295
|
+
let at = 0;
|
|
296
|
+
for (let node = firstIn(root); node !== null; node = nextIn(node, root), at++)
|
|
297
|
+
for (let i = 0; i < claims.length; i++) {
|
|
298
|
+
const behavior = claims[i](node);
|
|
299
|
+
if (behavior !== undefined) {
|
|
300
|
+
positions.push(at);
|
|
301
|
+
behaviors.push(behavior);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (positions.length > 0)
|
|
305
|
+
template._$inst$ = hookFor(positions, behaviors);
|
|
303
306
|
};
|
|
307
|
+
/** Wire it as `wire([renderer, elements, …claims])`. A bare module: it has no options. */
|
|
308
|
+
const elements = [
|
|
309
|
+
(registry) => {
|
|
310
|
+
registered = registry;
|
|
311
|
+
},
|
|
312
|
+
{ name: '@verajs/renderer/elements', on: 'template', fn: claimTemplate, priority: 10 },
|
|
313
|
+
];
|
|
314
|
+
|
|
304
315
|
/**
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
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.)
|
|
316
|
+
* **The renderer's binding kinds and child-part modes** — numbers both `renderer.ts` and the separately bundled
|
|
317
|
+
* `hydration.ts` read, from ONE source (each bundle inlines them; they are literals in the output). A change here
|
|
318
|
+
* changes the hand-off's protocol: bump `$V` in `renderer.ts`.
|
|
313
319
|
*/
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
320
|
+
/** A binding's kind, resolved once per template. */
|
|
321
|
+
/**
|
|
322
|
+
* **The hydration hand-off's positions** — the renderer sets `$H` to an array at `connect`, and `hydration.ts` reads
|
|
323
|
+
* it by these. An array, not an object of named fields: the names cost every app bytes (−35 B gzipped measured,
|
|
324
|
+
* 2026-10-02), and these constants inline to literals in both bundles. The first two are FROZEN across protocols — a
|
|
325
|
+
* hydration from another release reads the protocol at 0 and, on a mismatch, can still install its clearing hook at 1.
|
|
326
|
+
* Any other change here is a protocol change: bump `PROTOCOL`.
|
|
327
|
+
*/
|
|
328
|
+
/**
|
|
329
|
+
* **The protocol of this package's first-party seams** — the renderer's hand-off (`$H`, at `HANDOFF_PROTOCOL`) and slots'
|
|
330
|
+
* capture (`_$capture$`), both read by hydration. One number for both: the three ship in one package, and a change to
|
|
331
|
+
* either seam bumps it. A hydration that meets another number declines (the page renders fresh) and says so.
|
|
332
|
+
*/
|
|
333
|
+
const PROTOCOL = 1;
|
|
334
|
+
|
|
317
335
|
/**
|
|
318
|
-
*
|
|
336
|
+
* **`@verajs/renderer/slots` — `<slot>` in a light-DOM component.** `wire([renderer, slots])`.
|
|
319
337
|
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
338
|
+
* A component that renders into its own element (no shadow root) gets the platform's slot assignment for the
|
|
339
|
+
* children the page gave it: each light child goes to the first `<slot>` of its `slot` name (the default slot for
|
|
340
|
+
* text and unnamed elements), a slot with nothing assigned shows its own content (the fallback), and comments are
|
|
341
|
+
* never assigned.
|
|
342
|
+
*
|
|
343
|
+
* **The renderer knows nothing of it.** A host's light children are divided into UNITS: each static child alone, and
|
|
344
|
+
* each run a binding of the page's template wrote (its markers and everything between them) whole. A unit moves as
|
|
345
|
+
* one piece — never split — so every write the renderer later makes inside it lands where the unit now is, and the
|
|
346
|
+
* renderer's own range stays contiguous. While a `<slot>` has content, the element steps out of the page and its
|
|
347
|
+
* content stands in its place, between this module's two comments; with none, the element is back, showing its
|
|
348
|
+
* fallback. What the page's template later does inside a unit is seen by a `MutationObserver` and re-distributed —
|
|
349
|
+
* synchronously after every component render (core's `'render'` insert), and at the latest by the next microtask.
|
|
327
350
|
*/
|
|
328
|
-
|
|
329
|
-
|
|
351
|
+
/** A slot's assignment name: an element's `slot` attribute, `''` for text; a comment is never slottable. */
|
|
352
|
+
const slotNameOf = (node) => node.nodeType === 3 ? '' : node.nodeType === 1 ? (node.getAttribute('slot') ?? '') : null;
|
|
353
|
+
/**
|
|
354
|
+
* **Unassigned content stays CONNECTED, as under native slots** — it waits in `<ins hidden data-vm-unassigned>`, the
|
|
355
|
+
* host's first child, rather than out of the page: a component in it keeps running, and assigning or unassigning it is a
|
|
356
|
+
* one-op connected move, which core keeps alive. The container is in the host exactly while it holds a light node —
|
|
357
|
+
* runs' markers and stand-ins are comments, which need no connection — so a host with nothing unassigned has no extra
|
|
358
|
+
* child, and the server, which knows only nodes, emits the same element in the same place.
|
|
359
|
+
*
|
|
360
|
+
* **Why `<ins>`** (Brian, 2026-10-08), measured in all three engines: a standard element, so it is valid HTML and
|
|
361
|
+
* `:defined` (the carrier was `<vm-unassigned>`, a dashed name nothing defined, which discovery loaders tried to import
|
|
362
|
+
* and "wait until everything is defined" checks waited on forever); its content model is TRANSPARENT, so it is valid
|
|
363
|
+
* around whatever it holds (a `<span>` holding a `<section>` is not); and it parses inside a `<p>` where a `<div>` would
|
|
364
|
+
* close the paragraph and eject the host's content. It is known by its attribute: a user's own `<ins>` is light content
|
|
365
|
+
* like any other.
|
|
366
|
+
*/
|
|
367
|
+
const UNASSIGNED = 'data-vm-unassigned';
|
|
368
|
+
/** Into the page before a light node moves in, so the move is connected → connected. */
|
|
369
|
+
const park = (light) => {
|
|
370
|
+
const holding = holdingOf(light);
|
|
371
|
+
if (holding.parentNode !== light.host)
|
|
372
|
+
light.host.insertBefore(holding, light.host.firstChild);
|
|
373
|
+
watchHolding(light);
|
|
374
|
+
return holding;
|
|
375
|
+
};
|
|
376
|
+
/**
|
|
377
|
+
* At the end of every pass: in the page exactly while it holds a light node — out once only comments remain, and IN if
|
|
378
|
+
* the renderer wrote a new node straight into a run waiting here while it was out (that node's first connection).
|
|
379
|
+
*/
|
|
380
|
+
const unpark = (light) => {
|
|
381
|
+
const holding = light.holding;
|
|
382
|
+
if (holding === undefined)
|
|
330
383
|
return;
|
|
331
|
-
|
|
332
|
-
|
|
384
|
+
for (let node = holding.firstChild; node !== null; node = node.nextSibling)
|
|
385
|
+
if (node.nodeType !== 8)
|
|
386
|
+
return void park(light);
|
|
387
|
+
holding.remove();
|
|
388
|
+
};
|
|
389
|
+
/**
|
|
390
|
+
* **Each host's light, kept ON the host** — under this module copy's own `Symbol` (2026-10-17, candidate B): every
|
|
391
|
+
* component's render end asks whether its container has a light (`_$done$`), and for one that does not — a plain
|
|
392
|
+
* component — that is now a missing-property read, never a WeakMap lookup. Only a host that gets a light carries the
|
|
393
|
+
* field; host → light → host is a DOM↔JS cycle, so `release` clears it. The same get/has/set/delete surface as the
|
|
394
|
+
* WeakMap it replaces, so hydrate-slots, handed it through the protocol, is unchanged.
|
|
395
|
+
*/
|
|
396
|
+
const LIGHT = Symbol();
|
|
397
|
+
const HOSTS = {
|
|
398
|
+
get: (host) => host[LIGHT],
|
|
399
|
+
has: (host) => host[LIGHT] !== undefined,
|
|
400
|
+
set: (host, light) => {
|
|
401
|
+
host[LIGHT] = light;
|
|
402
|
+
},
|
|
403
|
+
delete: (host) => {
|
|
404
|
+
host[LIGHT] = undefined;
|
|
405
|
+
},
|
|
406
|
+
};
|
|
407
|
+
/** The static children an outer template gave a host, recorded when its instance was created (see `hostBehavior`). */
|
|
408
|
+
const STATICS = new WeakMap();
|
|
409
|
+
/** A run's node in a slot, and the comment holding its place in the run — both ways. */
|
|
410
|
+
const STAND = new WeakMap();
|
|
411
|
+
const REAL = new WeakMap();
|
|
412
|
+
/** The slot a light node was placed in (`null`: holding). */
|
|
413
|
+
const PLACED = new WeakMap();
|
|
414
|
+
/**
|
|
415
|
+
* A light node CAPTURED WHERE IT STANDS — outside the host, in a slot's region (the server put it there) — before that
|
|
416
|
+
* slot has mounted: where it is, it stays. Slots mount one at a time, and a node waiting for a later one would otherwise
|
|
417
|
+
* read as taken away (see `isWhere`).
|
|
418
|
+
*/
|
|
419
|
+
const ADOPTED = {};
|
|
420
|
+
/** The host's light children in the order the page wrote them — a run read through its stand-ins. */
|
|
421
|
+
const lightOf = (light) => {
|
|
422
|
+
const out = [];
|
|
423
|
+
for (const unit of light.units) {
|
|
424
|
+
if (unit.a === unit.z)
|
|
425
|
+
out.push(unit.a);
|
|
426
|
+
else
|
|
427
|
+
for (let node = unit.a.nextSibling; node !== null && node !== unit.z; node = node.nextSibling)
|
|
428
|
+
out.push(REAL.get(node) ?? node);
|
|
429
|
+
}
|
|
430
|
+
return out;
|
|
431
|
+
};
|
|
432
|
+
/** A stand-in is dropped: its node is no longer in a slot (back at its place, or gone). */
|
|
433
|
+
const unstand = (node) => {
|
|
434
|
+
const stand = STAND.get(node);
|
|
435
|
+
if (stand === undefined)
|
|
333
436
|
return;
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
437
|
+
STAND.delete(node);
|
|
438
|
+
REAL.delete(stand);
|
|
439
|
+
stand.remove();
|
|
440
|
+
};
|
|
441
|
+
/**
|
|
442
|
+
* **Where a light node physically is.** A `<slot>` can itself be a light child of another host — slot FORWARDING — and
|
|
443
|
+
* while it has content it is out of the page, its content standing in its place between its two comments: that range is
|
|
444
|
+
* where the node is, and moving the node moves the range.
|
|
445
|
+
*/
|
|
446
|
+
const firstOf = (node) => node.$rec?.rs ?? node;
|
|
447
|
+
const lastOf = (node) => node.$rec?.re ?? node;
|
|
448
|
+
/** A light node moved before `ref` in `parent` — the whole range of a forwarded slot that is out of the page. */
|
|
449
|
+
const move = (parent, node, ref) => {
|
|
450
|
+
const last = lastOf(node);
|
|
451
|
+
for (let at = firstOf(node); at !== null;) {
|
|
452
|
+
const next = at === last ? null : at.nextSibling;
|
|
453
|
+
parent.insertBefore(at, ref);
|
|
454
|
+
at = next;
|
|
337
455
|
}
|
|
338
|
-
|
|
339
|
-
|
|
456
|
+
};
|
|
457
|
+
/** A run's node that sits in a slot, its place in the run held by a comment before `ref` in `parent` — both ways. */
|
|
458
|
+
const standIn = (node, parent, ref) => {
|
|
459
|
+
const stand = node.ownerDocument.createComment('');
|
|
460
|
+
parent.insertBefore(stand, ref);
|
|
461
|
+
STAND.set(node, stand);
|
|
462
|
+
REAL.set(stand, node);
|
|
463
|
+
};
|
|
464
|
+
/** A light node back to holding: a run's node to its place in the run (where its stand-in is), any other to the end. */
|
|
465
|
+
const toHolding = (light, node) => {
|
|
466
|
+
const stand = STAND.get(node);
|
|
467
|
+
if (stand !== undefined) {
|
|
468
|
+
move(park(light), node, stand);
|
|
469
|
+
unstand(node);
|
|
470
|
+
PLACED.delete(node);
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
move(park(light), node, light.holding.lastChild);
|
|
474
|
+
PLACED.set(node, null);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
/* ── tree order, through slots that are out of the page ────────────────────────────────────────── */
|
|
478
|
+
/**
|
|
479
|
+
* The positions that decide a slot's tree order: its own (its element, or the comment standing in its place), and,
|
|
480
|
+
* while it sits inside another slot's element that is OUT of the page (a displaced fallback), that slot's position
|
|
481
|
+
* first. `null` when the chain ends outside the host — a slot parked away by `hold`, which takes part in nothing.
|
|
482
|
+
*/
|
|
483
|
+
const chain = (rec) => {
|
|
484
|
+
const out = [];
|
|
485
|
+
let node = rec.rs ?? rec.slot;
|
|
486
|
+
for (;;) {
|
|
487
|
+
out.unshift(node);
|
|
488
|
+
if (rec.light.host.contains(node))
|
|
489
|
+
return out;
|
|
490
|
+
let root = node;
|
|
491
|
+
while (root.parentNode !== null)
|
|
492
|
+
root = root.parentNode;
|
|
493
|
+
const outer = root.$rec;
|
|
494
|
+
if (outer === undefined || outer.rs === null)
|
|
495
|
+
return null;
|
|
496
|
+
node = outer.rs;
|
|
340
497
|
}
|
|
341
498
|
};
|
|
342
|
-
/**
|
|
343
|
-
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
for (let i = 0; i <
|
|
347
|
-
if (
|
|
348
|
-
return
|
|
349
|
-
return
|
|
499
|
+
/** Whether `b` comes after `a` in tree order — the platform's `DOCUMENT_POSITION_FOLLOWING` bit. */
|
|
500
|
+
|
|
501
|
+
const follows = (a, b) => (a.compareDocumentPosition(b) & 4) !== 0;
|
|
502
|
+
const before = (x, y) => {
|
|
503
|
+
for (let i = 0; i < x.length && i < y.length; i++)
|
|
504
|
+
if (x[i] !== y[i])
|
|
505
|
+
return follows(x[i], y[i]);
|
|
506
|
+
return x.length < y.length;
|
|
350
507
|
};
|
|
351
|
-
|
|
508
|
+
/* ── distribution ──────────────────────────────────────────────────────────────────────────────── */
|
|
352
509
|
/**
|
|
353
|
-
*
|
|
354
|
-
* the
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
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.)
|
|
510
|
+
* **A filled slot's region is bounded by two comments, spelled the same on both sides.** The client creates them here;
|
|
511
|
+
* the server writes exactly them (`@verajs/ssr`), so a server-rendered host and a client-distributed one are the
|
|
512
|
+
* same DOM down to comment data, and hydration ADOPTS the server's instead of creating its own. Measured 2026-10-07:
|
|
513
|
+
* that end state hydrates faster than any marking that makes the client split text and create markers (Chrome −11%,
|
|
514
|
+
* Firefox −37%, WebKit −9% on a worst case), and it needs no counting, so nothing depends on how the browser decodes
|
|
515
|
+
* text. The data carries no slot name or index: pairing is positional, and a name could close the comment.
|
|
368
516
|
*/
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
517
|
+
const RANGE_START = '[';
|
|
518
|
+
const RANGE_END = ']';
|
|
519
|
+
const region = (rec) => {
|
|
520
|
+
if (rec.rs !== null)
|
|
521
|
+
return;
|
|
522
|
+
const doc = rec.slot.ownerDocument;
|
|
523
|
+
const parent = rec.slot.parentNode;
|
|
524
|
+
rec.rs = doc.createComment(RANGE_START);
|
|
525
|
+
rec.re = doc.createComment(RANGE_END);
|
|
526
|
+
parent.insertBefore(rec.rs, rec.slot);
|
|
527
|
+
parent.insertBefore(rec.re, rec.slot);
|
|
528
|
+
/** `removeChild`, never `remove()`: a kept slot's own `remove()` is the USER's (it unregions first, `removeSlot`). */
|
|
529
|
+
parent.removeChild(rec.slot);
|
|
530
|
+
watch(parent, rec.light);
|
|
531
|
+
};
|
|
532
|
+
const unregion = (rec) => {
|
|
533
|
+
if (rec.rs === null)
|
|
534
|
+
return;
|
|
535
|
+
rec.rs.parentNode?.insertBefore(rec.slot, rec.rs);
|
|
536
|
+
rec.rs.remove();
|
|
537
|
+
rec.re.remove();
|
|
538
|
+
rec.rs = rec.re = null;
|
|
539
|
+
};
|
|
540
|
+
/** What a slot shows goes back to holding, and the slot back in its place: every way a slot leaves runs this. */
|
|
541
|
+
const unplace = (rec) => {
|
|
542
|
+
for (const node of lightOf(rec.light))
|
|
543
|
+
if (PLACED.get(node) === rec)
|
|
544
|
+
toHolding(rec.light, node);
|
|
545
|
+
unregion(rec);
|
|
546
|
+
};
|
|
547
|
+
/** The slot leaves its host's list of slots. */
|
|
548
|
+
const unlist = (rec) => rec.light.recs.splice(rec.light.recs.indexOf(rec), 1);
|
|
549
|
+
/** `slotchange`, as the platform fires it: once per slot per change, after the change, on the slot element. */
|
|
550
|
+
const changed = (rec) => {
|
|
551
|
+
if (rec.queued)
|
|
372
552
|
return;
|
|
373
|
-
|
|
553
|
+
rec.queued = true;
|
|
374
554
|
queueMicrotask(() => {
|
|
375
|
-
|
|
376
|
-
slot.dispatchEvent(new
|
|
555
|
+
rec.queued = false;
|
|
556
|
+
rec.slot.dispatchEvent(new (rec.slot.ownerDocument.defaultView.Event)('slotchange', { bubbles: true, composed: false }));
|
|
377
557
|
});
|
|
378
558
|
};
|
|
379
|
-
|
|
380
|
-
|
|
559
|
+
/**
|
|
560
|
+
* Whether a node is in this host's holding or one of its slots' regions — or where the server put it (`ADOPTED`), which
|
|
561
|
+
* holds until its slot mounts, as `isWhere` holds a static: a slot mounting first must not read the run's node in a
|
|
562
|
+
* later slot's served region, not yet registered, as taken.
|
|
563
|
+
*/
|
|
564
|
+
const inLight = (node, light) => {
|
|
565
|
+
if (PLACED.get(node) === ADOPTED)
|
|
566
|
+
return true;
|
|
567
|
+
const parent = node.parentNode;
|
|
568
|
+
if (parent === null)
|
|
381
569
|
return false;
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
570
|
+
if (parent === light.holding)
|
|
571
|
+
return true;
|
|
572
|
+
for (const rec of light.recs)
|
|
573
|
+
if (rec.re !== null && rec.re.parentNode === parent)
|
|
574
|
+
return true;
|
|
575
|
+
return false;
|
|
576
|
+
};
|
|
577
|
+
/** Whether a light node is still where this module put it — false once something else took it (the user's adoption stands). */
|
|
578
|
+
const isWhere = (node, light) => {
|
|
579
|
+
const rec = PLACED.get(node);
|
|
580
|
+
if (rec === ADOPTED)
|
|
581
|
+
return true;
|
|
582
|
+
const parent = firstOf(node).parentNode;
|
|
583
|
+
if (rec === undefined)
|
|
584
|
+
return parent === light.holding;
|
|
585
|
+
/** `PLACED` is shared by every host: a node another host has placed is not this one's, wherever it sits. */
|
|
586
|
+
return rec === null ? parent === light.holding : rec.light === light && parent !== null && parent === rec.re?.parentNode;
|
|
386
587
|
};
|
|
387
588
|
/**
|
|
388
|
-
*
|
|
389
|
-
* name
|
|
390
|
-
*
|
|
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.
|
|
589
|
+
* **Every light node to its slot, in light order** — the whole decision, recomputed. A node something else took away
|
|
590
|
+
* is forgotten first; then each name's first slot in tree order wins, its nodes go between its comments (a run's node
|
|
591
|
+
* leaving a stand-in at its place), a slot left with nothing shows its fallback, and the rest wait in holding.
|
|
406
592
|
*/
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
593
|
+
/**
|
|
594
|
+
* A distribution in progress can render (a slotted component moved is reconnected, and renders): the render's end asks
|
|
595
|
+
* for another, which waits for this one to finish and then runs.
|
|
596
|
+
*/
|
|
597
|
+
let busy = false;
|
|
598
|
+
const distribute = (light) => {
|
|
599
|
+
if (busy) {
|
|
600
|
+
/**
|
|
601
|
+
* Unreached outside the work set in every suite (18 hits, all already marked — a first connection is a fresh capture);
|
|
602
|
+
* kept because the invariant requires a dirty light in work, and redundancy here is not proven. Unlike `settleIn`'s
|
|
603
|
+
* mark (removed: its only caller marks first), this one is equivalent by observation only.
|
|
604
|
+
*/
|
|
605
|
+
mark(light);
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
busy = true;
|
|
609
|
+
try {
|
|
610
|
+
do
|
|
611
|
+
place(light);
|
|
612
|
+
while (light.dirty);
|
|
613
|
+
}
|
|
614
|
+
finally {
|
|
615
|
+
busy = false;
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
const place = (light) => {
|
|
619
|
+
light.dirty = false;
|
|
413
620
|
/**
|
|
414
|
-
* **
|
|
621
|
+
* **THE INVARIANT: a slotted node moves in ONE DOM operation, connected → connected.** Core tells a move from a removal
|
|
622
|
+
* by `isConnected` at `disconnectedCallback` (the platform runs it after the operation), so one `insertBefore` from
|
|
623
|
+
* where the node is to where it goes keeps a slotted component alive; a hop through holding (a fragment) is a real
|
|
624
|
+
* disconnect, and the component is torn down and set up again. `tests/core-keep-alive.test.mjs` pins it.
|
|
415
625
|
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
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.
|
|
626
|
+
* **Captured and not yet distributed: placed straight from the host.** A node a slot takes moves host → slot in ONE
|
|
627
|
+
* operation, which the platform — and core — see as a move, not a removal (`isConnected` stays true through it): a
|
|
628
|
+
* slotted component is not torn down and set up again. Only what no slot takes, and a run's own markers, go to holding.
|
|
424
629
|
*/
|
|
425
|
-
const
|
|
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;
|
|
630
|
+
const fresh = light.fresh;
|
|
437
631
|
/**
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
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.
|
|
632
|
+
* Parked only when the render ENDS (`ending`): its slots have all mounted by then, so a node no slot took really has
|
|
633
|
+
* none. Parked earlier — at the first slot's mount — a node for a later slot would hop host → holding → slot, two
|
|
634
|
+
* operations, and be torn down and set up again on the way.
|
|
444
635
|
*/
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
636
|
+
/**
|
|
637
|
+
* **What clears `fresh`: only a render that ENDS** (`_$done$`, after the commit). A first render that THROWS out of
|
|
638
|
+
* `renderInto` skips it, and the host stays fresh — its captured children still where the page put them, its runs
|
|
639
|
+
* still in the host — until a render completes. That is consistent, not stale: a failed commit mounts no slot (mounts
|
|
640
|
+
* run at a render's end), so no region exists for `inRun` to misread, and a user's edit meanwhile is an ordinary
|
|
641
|
+
* host record (`adopt`). Pinned in `tests/renderer-slots.test.mjs`.
|
|
642
|
+
*/
|
|
643
|
+
if (ending)
|
|
644
|
+
light.fresh = false;
|
|
645
|
+
const host = light.host;
|
|
646
|
+
/** Re-read, never a copy: holding is built at its first use (`holdingOf`), possibly during this pass. */
|
|
647
|
+
const home = (node) => firstOf(node).parentNode === light.holding || (fresh && firstOf(node).parentNode === host);
|
|
648
|
+
light.units = light.units.filter((unit) => (unit.a === unit.z ? isWhere(unit.a, light) || (fresh && firstOf(unit.a).parentNode === host) : home(unit.a) && home(unit.z)));
|
|
649
|
+
for (const unit of light.units)
|
|
650
|
+
if (unit.a !== unit.z)
|
|
651
|
+
for (let node = unit.a.nextSibling; node !== null && node !== unit.z; node = node.nextSibling) {
|
|
652
|
+
const real = REAL.get(node);
|
|
653
|
+
/** The renderer moves a run's nodes between this host's slots (beside a neighbor it follows); only leaving them all is a theft. */
|
|
654
|
+
if (real !== undefined && !inLight(real, light)) {
|
|
655
|
+
PLACED.delete(real);
|
|
656
|
+
unstand(real);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
const live = [];
|
|
660
|
+
/** A slot parked away (by `hold`) takes part in nothing — and what it shows waits with it, untouched. */
|
|
661
|
+
const parked = new Set();
|
|
662
|
+
for (const rec of light.recs) {
|
|
663
|
+
const at = chain(rec);
|
|
664
|
+
if (at !== null)
|
|
665
|
+
live.push({ rec, at });
|
|
666
|
+
else
|
|
667
|
+
parked.add(rec);
|
|
455
668
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
const
|
|
460
|
-
if (
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
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);
|
|
669
|
+
live.sort((x, y) => (before(x.at, y.at) ? -1 : 1));
|
|
670
|
+
const winner = new Map();
|
|
671
|
+
for (const { rec } of live) {
|
|
672
|
+
const name = rec.slot.getAttribute('name') ?? '';
|
|
673
|
+
if (!winner.has(name))
|
|
674
|
+
winner.set(name, rec);
|
|
675
|
+
}
|
|
676
|
+
const nodes = lightOf(light);
|
|
677
|
+
/** A static child moves as itself; only a node of a binding's RUN leaves a stand-in at its place. */
|
|
678
|
+
const statics = new Set();
|
|
679
|
+
for (const unit of light.units)
|
|
680
|
+
if (unit.a === unit.z)
|
|
681
|
+
statics.add(unit.a);
|
|
682
|
+
const wanted = new Map();
|
|
683
|
+
const taken = new Set();
|
|
684
|
+
for (const node of nodes) {
|
|
685
|
+
const name = slotNameOf(node);
|
|
686
|
+
const rec = name === null ? undefined : winner.get(name);
|
|
687
|
+
if (rec === undefined)
|
|
484
688
|
continue;
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
689
|
+
let list = wanted.get(rec);
|
|
690
|
+
if (list === undefined)
|
|
691
|
+
wanted.set(rec, (list = []));
|
|
692
|
+
list.push(node);
|
|
693
|
+
taken.add(node);
|
|
488
694
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
695
|
+
for (const { rec } of live) {
|
|
696
|
+
const mine = wanted.get(rec) ?? [];
|
|
697
|
+
if (mine.length > 0) {
|
|
698
|
+
region(rec);
|
|
699
|
+
const parent = rec.re.parentNode;
|
|
700
|
+
/** Only what is out of place moves: a move disconnects and reconnects a component, re-running its lifecycle. */
|
|
701
|
+
let previous = rec.rs;
|
|
702
|
+
for (const node of mine) {
|
|
703
|
+
const first = firstOf(node);
|
|
704
|
+
if (first.parentNode === parent && first.previousSibling === previous) {
|
|
705
|
+
previous = lastOf(node);
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
/** A run's node, still at its place in holding: a stand-in takes the place before it leaves. */
|
|
709
|
+
if (!statics.has(node) && home(node) && !STAND.has(node))
|
|
710
|
+
standIn(node, first.parentNode, first);
|
|
711
|
+
move(parent, node, previous.nextSibling);
|
|
712
|
+
/** Marked as moved by slots: the renderer follows it here when it inserts beside it (see `into`). */
|
|
713
|
+
node._$slotted$ = true;
|
|
714
|
+
PLACED.set(node, rec);
|
|
715
|
+
previous = lastOf(node);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
if (mine.length !== rec.shown.length || mine.some((node, i) => node !== rec.shown[i])) {
|
|
719
|
+
rec.shown = mine;
|
|
720
|
+
changed(rec);
|
|
721
|
+
}
|
|
495
722
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
/**
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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);
|
|
723
|
+
for (const node of nodes) {
|
|
724
|
+
if (taken.has(node))
|
|
725
|
+
continue;
|
|
726
|
+
const rec = PLACED.get(node);
|
|
727
|
+
if (rec !== undefined && rec !== null && parked.has(rec))
|
|
728
|
+
continue;
|
|
729
|
+
/** Adopted and waiting for its slot, which mounts later in the same render: it stays where the server put it. */
|
|
730
|
+
if (rec === ADOPTED && !ending)
|
|
731
|
+
continue;
|
|
732
|
+
if (STAND.has(node) || (rec !== undefined && rec !== null) || (ending && statics.has(node) && node.parentNode === host))
|
|
733
|
+
toHolding(light, node);
|
|
520
734
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
let node =
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
node = next;
|
|
735
|
+
/** A run captured in the host goes to holding now — its markers, stand-ins and unassigned nodes; its slotted nodes left in one move each. */
|
|
736
|
+
if (fresh && ending)
|
|
737
|
+
for (const unit of light.units)
|
|
738
|
+
if (unit.a !== unit.z && unit.a.parentNode === host) {
|
|
739
|
+
const run = [];
|
|
740
|
+
for (let node = unit.a; node !== null; node = node.nextSibling) {
|
|
741
|
+
run.push(node);
|
|
742
|
+
if (node === unit.z)
|
|
743
|
+
break;
|
|
531
744
|
}
|
|
745
|
+
const holding = holdingOf(light);
|
|
746
|
+
for (const node of run)
|
|
747
|
+
(node.nodeType === 8 ? holding : park(light)).insertBefore(node, holding.lastChild);
|
|
748
|
+
watchHolding(light);
|
|
532
749
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
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
|
-
};
|
|
750
|
+
for (const { rec } of live)
|
|
751
|
+
if (!wanted.has(rec))
|
|
752
|
+
unregion(rec);
|
|
753
|
+
unpark(light);
|
|
543
754
|
};
|
|
755
|
+
/* ── the observer: what the page's templates (and the user) do afterwards ─────────────────────── */
|
|
544
756
|
/**
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
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.
|
|
757
|
+
* **An observer per light** — never one shared by every host: Gecko's `observe()` is linear in what that observer
|
|
758
|
+
* already watches, removed nodes included, through collections (2000 observes 4 → 45 ms over ten batches on one observer,
|
|
759
|
+
* flat on a fresh one, flat on Chromium and WebKit), so a shared one made every new light host slower in Firefox without
|
|
760
|
+
* end (2000 hosts: 262 → 9305 ms over eight renders that replaced them; flat now). A light's observer goes with it.
|
|
552
761
|
*
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
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.
|
|
762
|
+
* From THIS module's own realm, which cannot close while its code runs — a MutationObserver watches nodes of another
|
|
763
|
+
* document (measured on Chromium, Firefox and WebKit). Taking it from a host's window went deaf with that window: a
|
|
764
|
+
* light host in an iframe or a popped-out window, closed later (`tests/browser/slots-realm.test.js`). The host's window
|
|
765
|
+
* is the fallback only where this realm has no observer at all.
|
|
574
766
|
*/
|
|
575
|
-
const
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
shown.push(n);
|
|
586
|
-
return shown.filter((node) => slotNameOf(node) !== null);
|
|
767
|
+
const observerOf = (light) => {
|
|
768
|
+
const Observer = globalThis.MutationObserver ?? light.host.ownerDocument.defaultView.MutationObserver;
|
|
769
|
+
/**
|
|
770
|
+
* Bound to their light (2026-10-17): a record can only reach a light through what THAT light registered, so it arrives
|
|
771
|
+
* already knowing whose it is — no node → lights lookup, and no per-node bookkeeping (a WeakMap entry and a Set for every
|
|
772
|
+
* watched node were ~164 ms of Chrome's slots creation at 10 000 hosts).
|
|
773
|
+
*/
|
|
774
|
+
const handle = (records) => {
|
|
775
|
+
note(light, records);
|
|
776
|
+
flush();
|
|
587
777
|
};
|
|
588
|
-
|
|
589
|
-
|
|
778
|
+
/**
|
|
779
|
+
* ONE per light (2026-10-17; probe P3 vs P2: ~1 point of creation in each engine): its registrations are on distinct nodes —
|
|
780
|
+
* the host's subtree for childList, each light element and slot for its attributes, holding and a fallback's region
|
|
781
|
+
* parent for childList — so none replaces another's options, except a node both a light element and a slot (`own`).
|
|
782
|
+
*/
|
|
783
|
+
return (light.obs ??= new Observer(handle));
|
|
590
784
|
};
|
|
591
785
|
/**
|
|
592
|
-
* **
|
|
593
|
-
*
|
|
594
|
-
*
|
|
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.
|
|
786
|
+
* **A `hold()`-parked branch, watched while it is parked** — a slot's region in it is out of its host's subtree, where
|
|
787
|
+
* the host's own attribute registration cannot see a node in it renamed (`slot=`): re-routed now, as native slots would.
|
|
788
|
+
* One observer for every parked root (the renderer's `_$parked$`), registering the same root again when it parks again.
|
|
600
789
|
*/
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
790
|
+
let parkedObserver;
|
|
791
|
+
/** Each parked branch's owner — the container whose render parked it (a WeakMap on the parked root: parking is rare). */
|
|
792
|
+
const PARKED = new WeakMap();
|
|
793
|
+
const parkedHandle = (records) => {
|
|
794
|
+
noteParked(records);
|
|
795
|
+
flush();
|
|
796
|
+
};
|
|
797
|
+
/**
|
|
798
|
+
* A light's pending records. A light in the work set (`all` false) is not asked while it has no childList registration
|
|
799
|
+
* (its first render): its renames stay with the callback, and `takeRecords()` on every host every pass was ~114 ms of
|
|
800
|
+
* Chrome's slots creation at 10 000 hosts (2026-10-17).
|
|
801
|
+
*/
|
|
802
|
+
const take = (light, all = true) => light.obs === undefined || (!all && light.ho !== true && light.cw !== true) ? [] : light.obs.takeRecords();
|
|
803
|
+
/**
|
|
804
|
+
* **This module's own moves, dropped** — what was pending before them was read at the start of the same pass. Nothing to
|
|
805
|
+
* drop while the light has no childList registration (see `take`). A move never changes an attribute, so a rename found
|
|
806
|
+
* here is someone else's (a moved element's `connectedCallback`): it marks its light and is followed after this pass.
|
|
807
|
+
*/
|
|
808
|
+
const drop = (light) => {
|
|
809
|
+
if (light.obs === undefined || (light.ho !== true && light.cw !== true))
|
|
611
810
|
return;
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
811
|
+
for (const record of light.obs.takeRecords())
|
|
812
|
+
if (record.type === 'attributes') {
|
|
813
|
+
mark(record.target.$rec?.light ?? light);
|
|
814
|
+
queueMicrotask(flush);
|
|
815
|
+
}
|
|
617
816
|
};
|
|
618
|
-
|
|
817
|
+
/** A render is ending (the renderer's `_$done$`): captured children no slot took are parked now, not before. */
|
|
818
|
+
let ending = false;
|
|
819
|
+
/** Records of this module's own moves, taken and dropped once it is done — they are not news. */
|
|
820
|
+
/** How many nodes this batch has put ahead of everything, per host — so a batch's front insertions keep their order. */
|
|
821
|
+
let lead = new WeakMap();
|
|
822
|
+
/** Whether THIS light watches `node` — its holding, a region's parent, one of its slots, or its host as a region parent. */
|
|
823
|
+
const watchedBy = (light, node) => node === light.holding ||
|
|
824
|
+
(node === light.host && light.hw === true) ||
|
|
825
|
+
node.$rec?.light === light ||
|
|
826
|
+
light.recs.some((rec) => rec.re !== null && rec.re.parentNode === node);
|
|
619
827
|
/**
|
|
620
|
-
* The
|
|
621
|
-
*
|
|
622
|
-
* identity. Template-caused records match nothing here: its nodes were never captured, and our
|
|
623
|
-
* own moves were drained before they could arrive.
|
|
828
|
+
* The top of `node`'s tree as slots sees it: a FILLED slot has stepped out of the page, so from it the walk continues at
|
|
829
|
+
* its region (where it stands in the page) — a slot in another slot's fallback is still the host's, though detached.
|
|
624
830
|
*/
|
|
831
|
+
const topOf = (node) => {
|
|
832
|
+
let at = node;
|
|
833
|
+
for (;;) {
|
|
834
|
+
const rec = at.$rec;
|
|
835
|
+
const up = rec !== undefined && rec.rs !== null && rec.slot === at ? rec.rs : at.parentNode;
|
|
836
|
+
if (up === null)
|
|
837
|
+
return at;
|
|
838
|
+
at = up;
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
/** A slot the user removed: unmounted as the template's own teardown does, inside a flush (its records then dropped). */
|
|
842
|
+
/** Whether `node` is the host's, walking through filled slots to where their regions stand. */
|
|
843
|
+
const insideHost = (node, host) => {
|
|
844
|
+
for (let at = node; at !== null;) {
|
|
845
|
+
if (at === host)
|
|
846
|
+
return true;
|
|
847
|
+
const rec = at.$rec;
|
|
848
|
+
at = rec !== undefined && rec.rs !== null && rec.slot === at ? rec.rs : at.parentNode;
|
|
849
|
+
}
|
|
850
|
+
return false;
|
|
851
|
+
};
|
|
852
|
+
const handRemoved = (rec) => {
|
|
853
|
+
unlist(rec);
|
|
854
|
+
unplace(rec);
|
|
855
|
+
mark(rec.light);
|
|
856
|
+
};
|
|
625
857
|
/**
|
|
626
|
-
*
|
|
627
|
-
*
|
|
628
|
-
*
|
|
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.
|
|
858
|
+
* A FILLED slot is not in the page (its region stands in its place), so its own `remove()` / `replaceWith()` cannot reach
|
|
859
|
+
* what it shows: these put its content back to holding and the slot back in place first, then do what was asked — the
|
|
860
|
+
* host's observer then finds it gone (`handRemoved`). ONE function each, shared by every slot (`this`, no closure).
|
|
632
861
|
*/
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
862
|
+
function removeSlot() {
|
|
863
|
+
const rec = this.$rec;
|
|
864
|
+
if (rec !== undefined && rec.rs !== null)
|
|
865
|
+
unplace(rec);
|
|
866
|
+
this.ownerDocument.defaultView.Element.prototype.remove.call(this);
|
|
867
|
+
}
|
|
868
|
+
function replaceSlot(...nodes) {
|
|
869
|
+
const rec = this.$rec;
|
|
870
|
+
if (rec !== undefined && rec.rs !== null)
|
|
871
|
+
unplace(rec);
|
|
872
|
+
this.ownerDocument.defaultView.Element.prototype.replaceWith.apply(this, nodes);
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* **A `<slot>` inserted BY HAND** (KNOWN fix, 2026-10-18): claimed as the template's own are, through `slotBehavior.mount`,
|
|
876
|
+
* once the flush that found it is done (the mount flushes its light itself). Only a slot the component's template tree
|
|
877
|
+
* holds counts: one inside the host's LIGHT content is light DOM, as natively it is no slot of this host.
|
|
878
|
+
*/
|
|
879
|
+
let claims = [];
|
|
880
|
+
/**
|
|
881
|
+
* Claimed in a MICROTASK, never inside the flush that found them: a template's own slot is found by the flush its own
|
|
882
|
+
* `mount` begins with, BEFORE that mount gives it its record — claimed then, it was mounted twice (renderer-slots'
|
|
883
|
+
* three-level chain). A microtask later every mount in progress has finished, and a template slot carries its record
|
|
884
|
+
* (set once, never cleared), so only a slot no template made is claimed — a microtask later, the hand-edit rule.
|
|
885
|
+
*/
|
|
886
|
+
let claimQueued = false;
|
|
887
|
+
const findSlots = (light, node) => {
|
|
888
|
+
if (node.nodeType !== 1)
|
|
889
|
+
return;
|
|
890
|
+
const element = node;
|
|
891
|
+
if (element.localName === 'slot') {
|
|
892
|
+
if (element.$rec === undefined)
|
|
893
|
+
claims.push([element, light]);
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
/** Descendants only when there are any: the template's own small writes (a swapped `<i>`) stay one property read. */
|
|
897
|
+
if (element.firstElementChild === null)
|
|
898
|
+
return;
|
|
899
|
+
const found = element.getElementsByTagName('slot');
|
|
900
|
+
for (let i = 0; i < found.length; i++)
|
|
901
|
+
if (found[i].$rec === undefined)
|
|
902
|
+
claims.push([found[i], light]);
|
|
903
|
+
};
|
|
904
|
+
/** Whether `slot` sits in the component's own tree — not inside a light node this module placed or holds. */
|
|
905
|
+
const ofTemplate = (slot, light) => {
|
|
906
|
+
for (let at = slot.parentNode; at !== null; at = at.parentNode) {
|
|
907
|
+
if (at === light.host)
|
|
640
908
|
return true;
|
|
909
|
+
if (at === light.holding || PLACED.has(at))
|
|
910
|
+
return false;
|
|
641
911
|
}
|
|
642
912
|
return false;
|
|
643
913
|
};
|
|
644
|
-
const
|
|
645
|
-
|
|
646
|
-
const
|
|
914
|
+
const claimInserted = () => {
|
|
915
|
+
claimQueued = false;
|
|
916
|
+
const pending = claims;
|
|
917
|
+
claims = [];
|
|
918
|
+
for (const [slot, light] of pending)
|
|
919
|
+
if (slot.$rec === undefined && HOSTS.get(light.host) === light && ofTemplate(slot, light))
|
|
920
|
+
slotBehavior.mount(slot, { root: light.host });
|
|
921
|
+
};
|
|
922
|
+
/** The batch's added nodes, looked through for a hand-inserted slot — a pass of its own, so `note` stays small. */
|
|
923
|
+
const scanAdded = (light, records) => {
|
|
647
924
|
for (const record of records) {
|
|
925
|
+
if (record.type !== 'childList')
|
|
926
|
+
continue;
|
|
927
|
+
/** Read once: `addedNodes` is a DOM getter. */
|
|
928
|
+
const added = record.addedNodes;
|
|
929
|
+
for (let i = 0, n = added.length; i < n; i++)
|
|
930
|
+
findSlots(light, added[i]);
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
const note = (light, records) => {
|
|
934
|
+
/** Only a component observed from its first render ('eager') follows a `<slot>` inserted by hand — a lazy one pays nothing. */
|
|
935
|
+
if (light.ex === true)
|
|
936
|
+
scanAdded(light, records);
|
|
937
|
+
lead = new WeakMap();
|
|
938
|
+
let removed = false;
|
|
939
|
+
for (const record of records) {
|
|
940
|
+
const target = record.target;
|
|
941
|
+
if (record.removedNodes.length > 0)
|
|
942
|
+
removed = true;
|
|
943
|
+
/** An attribute record names the element: a slot's own name, or a node under something this light watches. */
|
|
648
944
|
if (record.type === 'attributes') {
|
|
649
|
-
const
|
|
650
|
-
|
|
651
|
-
|
|
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
|
-
}
|
|
945
|
+
const rec = target.$rec;
|
|
946
|
+
if (rec !== undefined) {
|
|
947
|
+
mark(rec.light);
|
|
665
948
|
continue;
|
|
666
949
|
}
|
|
667
|
-
|
|
668
|
-
if (
|
|
669
|
-
|
|
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
|
-
}
|
|
950
|
+
/** A light node's own `slot=` — acted on only while it is still THIS light's (a registration stays on a node that left). */
|
|
951
|
+
if (inLight(target, light) || target.parentNode === light.host)
|
|
952
|
+
mark(light);
|
|
695
953
|
continue;
|
|
696
954
|
}
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
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
|
-
}
|
|
955
|
+
/**
|
|
956
|
+
* A watched node's children — holding, a region's parent, a slot — replayed into the runs (the host too, when a region
|
|
957
|
+
* sits directly in it). Any other target in the host's subtree is the component's own template writing: not slotting.
|
|
958
|
+
*/
|
|
959
|
+
if (target === light.host ? light.hw === true : watchedBy(light, target)) {
|
|
960
|
+
mark(light);
|
|
961
|
+
replay(light, record);
|
|
772
962
|
}
|
|
773
|
-
|
|
774
|
-
|
|
963
|
+
/** A host's own children: an addition outside its render's range is a new light child. */
|
|
964
|
+
if (target === light.host) {
|
|
775
965
|
/**
|
|
776
|
-
*
|
|
777
|
-
*
|
|
778
|
-
*
|
|
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.
|
|
966
|
+
* Until its first render ends, a run still sits IN the host — so what the renderer takes from it there (a node a slot
|
|
967
|
+
* already took, through its stand-in) is read back as it is in holding. Missed, the stand-in went and its node stayed
|
|
968
|
+
* in the slot: a commit during the host's own render left the old content beside the new.
|
|
782
969
|
*
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
*
|
|
786
|
-
*
|
|
970
|
+
* **Why the REMOVED half is the whole of it** — the invariant: while the run is physically in the host, every node the
|
|
971
|
+
* renderer ADDS here lands exactly where the run is, between its own markers, so it is already in place and
|
|
972
|
+
* `lightOf` reads it there (nothing to move; replaying it as a run neighbor would move the render's own markers);
|
|
973
|
+
* every change inside a slot's region — a row added beside a node a slot took (`into` follows `_$slotted$` there),
|
|
974
|
+
* or removed from it — is recorded on that region's parent, which is watched, and replayed in full (into the run
|
|
975
|
+
* where it is — the host, while fresh: see `replay`). What a host
|
|
976
|
+
* record alone can carry is a REMOVED stand-in: the one change that leaves state behind (its node, still in a slot).
|
|
977
|
+
* Additions here are otherwise the render's own or a new light child (`adopt`).
|
|
787
978
|
*/
|
|
788
|
-
if (
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
pull(state, node, name);
|
|
793
|
-
state._names.delete(node);
|
|
794
|
-
touched.add(name);
|
|
795
|
-
}
|
|
979
|
+
if (light.fresh)
|
|
980
|
+
unrun(light, record);
|
|
981
|
+
for (const node of record.addedNodes)
|
|
982
|
+
adopt(light, node);
|
|
796
983
|
}
|
|
797
984
|
}
|
|
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
985
|
/**
|
|
838
|
-
*
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
986
|
+
* **A `<slot>` removed BY HAND** (KNOWN fix, 2026-10-18) — it, or its region, left the host with something the user
|
|
987
|
+
* removed (the slot, its parent, an ancestor). The template's own teardown unmounts a slot before removing it, and a
|
|
988
|
+
* `hold()`-parked one stays registered, so whatever is gone from the host otherwise was removed by hand: unmounted like
|
|
989
|
+
* the template's, its content back to holding, as natively it is unassigned. Checked once per batch that removed
|
|
990
|
+
* anything, over this light's slots.
|
|
843
991
|
*/
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
992
|
+
if (removed)
|
|
993
|
+
for (let i = light.recs.length - 1; i >= 0; i--) {
|
|
994
|
+
const rec = light.recs[i];
|
|
995
|
+
const anchor = rec.rs ?? rec.slot;
|
|
996
|
+
if (!insideHost(anchor, light.host) && !PARKED.has(topOf(anchor)))
|
|
997
|
+
handRemoved(rec);
|
|
998
|
+
}
|
|
999
|
+
};
|
|
1000
|
+
/**
|
|
1001
|
+
* A `hold()`-parked branch's renames (`parkedObserver`, one for every parked root): routed by the node's own marks — a
|
|
1002
|
+
* slot's light, a placed node's slot's light, or the host it sits in — since no per-node watcher list exists any more.
|
|
1003
|
+
*/
|
|
1004
|
+
const noteParked = (records) => {
|
|
1005
|
+
for (const record of records) {
|
|
1006
|
+
/**
|
|
1007
|
+
* A write into a parked branch (an applier resolving into it, the outer template committing there): it has left its
|
|
1008
|
+
* host's subtree, so it is replayed for the host whose render parked it — and only while it is still out (restored, the
|
|
1009
|
+
* host's own registration sees it, and replaying it twice would place it twice).
|
|
1010
|
+
*/
|
|
1011
|
+
if (record.type === 'childList') {
|
|
1012
|
+
let top = record.target;
|
|
1013
|
+
while (top.parentNode !== null)
|
|
1014
|
+
top = top.parentNode;
|
|
1015
|
+
const owner = PARKED.get(top);
|
|
1016
|
+
const light = owner === undefined ? undefined : HOSTS.get(owner);
|
|
1017
|
+
if (light !== undefined && !light.host.contains(record.target) && watchedBy(light, record.target)) {
|
|
1018
|
+
mark(light);
|
|
1019
|
+
replay(light, record);
|
|
1020
|
+
}
|
|
1021
|
+
continue;
|
|
860
1022
|
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
LANDMARKS.set(mark, node);
|
|
872
|
-
pending = null;
|
|
1023
|
+
for (let node = record.target; node !== null; node = node.parentNode) {
|
|
1024
|
+
const own = node.$rec ?? PLACED.get(node);
|
|
1025
|
+
if (own) {
|
|
1026
|
+
mark(own.light);
|
|
1027
|
+
break;
|
|
1028
|
+
}
|
|
1029
|
+
const light = HOSTS.get(node);
|
|
1030
|
+
if (light !== undefined) {
|
|
1031
|
+
mark(light);
|
|
1032
|
+
break;
|
|
873
1033
|
}
|
|
874
1034
|
}
|
|
875
1035
|
}
|
|
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
1036
|
};
|
|
886
|
-
/**
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
1037
|
+
/**
|
|
1038
|
+
* **The lights that need work** — dirty, or fresh until their first render ends, never one waiting for its own slots
|
|
1039
|
+
* (`wait`): the only ones `flush` visits. A set of EVERY light ever seen held each removed host for good (a leak) and
|
|
1040
|
+
* made every flush walk them all — hydrating 2000 light hosts slowed 72 → 365 ms over ten renders that replaced them.
|
|
1041
|
+
* A light leaves once it is clean, or clean and disconnected (a first render that threw stays fresh); it comes back
|
|
1042
|
+
* through `mark`, or its own render's end (`_$done$`).
|
|
1043
|
+
*/
|
|
1044
|
+
const work = new Set();
|
|
1045
|
+
/** The one way a light asks to be distributed. */
|
|
1046
|
+
const mark = (light) => {
|
|
1047
|
+
light.dirty = true;
|
|
1048
|
+
if (!light.wait)
|
|
1049
|
+
work.add(light);
|
|
1050
|
+
};
|
|
1051
|
+
/**
|
|
1052
|
+
* **Applies what is pending, now** — after every component render, and before any read of the assignment. It reads the
|
|
1053
|
+
* records of the lights it can name: its `context` (the light being read, mounted into, or rendered), every light whose
|
|
1054
|
+
* element the finished render's `root` created (`FED` — an outer template changing a component's children is
|
|
1055
|
+
* distributed by the end of that render), the work set, and the parked branches. Any other light's records arrive at the
|
|
1056
|
+
* microtask, through its own observer: a light host changed DIRECTLY by the page's code is redistributed by the next
|
|
1057
|
+
* microtask, or at once when its slotted content is read through the slots API (`tests/slots-sync-reads.test.mjs`).
|
|
1058
|
+
*/
|
|
1059
|
+
const flush = (context, root) => {
|
|
1060
|
+
if (busy)
|
|
1061
|
+
return;
|
|
1062
|
+
const near = [];
|
|
1063
|
+
if (context !== undefined)
|
|
1064
|
+
near.push(context);
|
|
1065
|
+
const fed = root === undefined ? undefined : FED.get(root);
|
|
1066
|
+
if (fed !== undefined) {
|
|
1067
|
+
/** A plain loop, no closure: an arrow capturing `root` would tax every call of this function (CLAUDE.md, V8). */
|
|
1068
|
+
const kept = [];
|
|
1069
|
+
const rooted = root.isConnected;
|
|
1070
|
+
for (let i = 0; i < fed.length; i++) {
|
|
1071
|
+
const element = fed[i].deref();
|
|
1072
|
+
if (element === undefined)
|
|
1073
|
+
continue;
|
|
1074
|
+
/**
|
|
1075
|
+
* A host that LEFT a connected root is dropped now, not at a collection: a churned list re-filtered an array growing
|
|
1076
|
+
* with every removed row until GC (2026-10-18). Put back by hand it is still followed — by its own observer from its
|
|
1077
|
+
* first slot (a microtask later; a slots-API read flushes it at once), or by its first slot's reconcile. A render
|
|
1078
|
+
* into a DETACHED root keeps every entry (nothing of it is connected). A host moved by hand to another, connected
|
|
1079
|
+
* root stays here, distributed at this root's renders too: bounded and harmless.
|
|
1080
|
+
*/
|
|
1081
|
+
if (!element.isConnected && rooted)
|
|
1082
|
+
continue;
|
|
1083
|
+
kept.push(fed[i]);
|
|
1084
|
+
const light = HOSTS.get(element);
|
|
1085
|
+
if (light !== undefined)
|
|
1086
|
+
near.push(light);
|
|
1087
|
+
}
|
|
1088
|
+
FED.set(root, kept);
|
|
1089
|
+
}
|
|
1090
|
+
/** Each light's records, read by that light (they arrive knowing whose they are), then the parked branches'. */
|
|
1091
|
+
for (const light of near)
|
|
1092
|
+
note(light, take(light));
|
|
924
1093
|
/**
|
|
925
|
-
*
|
|
926
|
-
*
|
|
927
|
-
*
|
|
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.
|
|
1094
|
+
* The work set's NAME records stay with their callback: distribution reads `slot=` from the DOM as it is, so a rename is
|
|
1095
|
+
* only a trigger — and a light being READ (`near`: the slots API, a mount, its own render) takes them now, so a read
|
|
1096
|
+
* right after a rename is current (tests/slots-sync-reads.test.mjs). ChildList records are taken in order, always.
|
|
939
1097
|
*/
|
|
940
|
-
const
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
1098
|
+
for (const light of work)
|
|
1099
|
+
note(light, take(light, false));
|
|
1100
|
+
if (parkedObserver !== undefined)
|
|
1101
|
+
noteParked(parkedObserver.takeRecords());
|
|
1102
|
+
for (const light of work) {
|
|
1103
|
+
if (light.dirty || (ending && light.fresh)) {
|
|
1104
|
+
distribute(light);
|
|
1105
|
+
drop(light);
|
|
1106
|
+
}
|
|
1107
|
+
if (!light.dirty && (!light.fresh || !light.host.isConnected))
|
|
1108
|
+
work.delete(light);
|
|
1109
|
+
}
|
|
1110
|
+
/** What this module just moved, recorded on the lights it read: not news. */
|
|
1111
|
+
for (const light of near)
|
|
1112
|
+
drop(light);
|
|
1113
|
+
if (claims.length > 0 && !claimQueued) {
|
|
1114
|
+
claimQueued = true;
|
|
1115
|
+
queueMicrotask(claimInserted);
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1118
|
+
/** Whether `node` sits outside the host's subtree registration — detached, or inside a slot (which steps out with its content). */
|
|
1119
|
+
const outside = (node, host) => {
|
|
1120
|
+
for (let at = node; at !== host; at = at.parentNode)
|
|
1121
|
+
if (at === null || at.$rec !== undefined)
|
|
1122
|
+
return true;
|
|
1123
|
+
return false;
|
|
1124
|
+
};
|
|
1125
|
+
const watch = (node, light) => {
|
|
1126
|
+
/** The host's own subtree registration covers it; a region directly in the host also gets the region's handling. */
|
|
1127
|
+
if (node === light.host) {
|
|
1128
|
+
light.hw = true;
|
|
1129
|
+
return;
|
|
945
1130
|
}
|
|
946
|
-
const
|
|
947
|
-
state._parks.add(fallback);
|
|
1131
|
+
const observer = observerOf(light);
|
|
948
1132
|
/**
|
|
949
|
-
*
|
|
950
|
-
*
|
|
951
|
-
*
|
|
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.
|
|
1133
|
+
* A slot: its own name only. A slot with content steps OUT of the page, so this is how a rename reaches it there; a
|
|
1134
|
+
* nested slot in its fallback is claimed and registered on its own. Re-registering replaces the options, so a node that
|
|
1135
|
+
* is also this light's light element keeps `slot=` too (the union; `own`).
|
|
954
1136
|
*/
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
parent
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
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
|
-
};
|
|
1137
|
+
if (node.$rec !== undefined) {
|
|
1138
|
+
observer.observe(node, { attributes: true, attributeFilter: mineAlready(node) ? ['slot', 'name'] : ['name'] });
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
/** Holding (out of the page while empty) and a region's parent inside a slot's fallback (out with that slot): on themselves. */
|
|
1142
|
+
if (node === light.holding || outside(node, light.host)) {
|
|
1143
|
+
light.cw = true;
|
|
1144
|
+
observer.observe(node, { childList: true });
|
|
983
1145
|
}
|
|
984
|
-
|
|
1146
|
+
};
|
|
1147
|
+
/** A light element's own `slot=` — registered when its light is first observed (`register`) and on every arrival after (`adopt`, `replay`, `settleIn`). */
|
|
1148
|
+
const OWN = Symbol();
|
|
1149
|
+
const mineAlready = (node) => node[OWN] === true;
|
|
1150
|
+
const own = (light, node) => {
|
|
1151
|
+
/** Not before the light is observed (`register`, at its first slot), which registers every light element it has then. */
|
|
1152
|
+
if (node.nodeType !== 1 || light.ho !== true)
|
|
1153
|
+
return;
|
|
1154
|
+
node[OWN] = true;
|
|
1155
|
+
observerOf(light).observe(node, { attributes: true, attributeFilter: node.$rec !== undefined ? ['slot', 'name'] : ['slot'] });
|
|
985
1156
|
};
|
|
986
1157
|
/**
|
|
987
|
-
*
|
|
988
|
-
*
|
|
989
|
-
*
|
|
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.
|
|
1158
|
+
* A node added to a host after its render: a light unit, appended in light order — unless it is the render's own. The
|
|
1159
|
+
* render's range runs from the host's first comment to its last that is not one of this module's units (captured
|
|
1160
|
+
* light children not yet distributed sit before it); comments are never slottable, so none is adopted.
|
|
997
1161
|
*/
|
|
998
|
-
const
|
|
999
|
-
if (
|
|
1000
|
-
return
|
|
1001
|
-
const
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1162
|
+
const adopt = (light, node) => {
|
|
1163
|
+
if (node.nodeType === 8 || node.parentNode !== light.host || node === light.holding)
|
|
1164
|
+
return;
|
|
1165
|
+
const ours = new Set();
|
|
1166
|
+
for (const unit of light.units)
|
|
1167
|
+
ours.add(unit.a).add(unit.z);
|
|
1168
|
+
if (ours.has(node))
|
|
1169
|
+
return;
|
|
1170
|
+
let first = null;
|
|
1171
|
+
let last = null;
|
|
1172
|
+
for (const child of light.host.childNodes)
|
|
1173
|
+
if (child.nodeType === 8 && !ours.has(child)) {
|
|
1174
|
+
first ??= child;
|
|
1175
|
+
last = child;
|
|
1176
|
+
}
|
|
1177
|
+
if (first !== null && first !== last && follows(first, node) && follows(node, last))
|
|
1178
|
+
return;
|
|
1179
|
+
/** Before the render's range is ahead of everything distributed away; after it, behind. Read before it moves. */
|
|
1180
|
+
const ahead = first !== null && follows(node, first);
|
|
1181
|
+
park(light).insertBefore(node, light.holding.lastChild);
|
|
1182
|
+
PLACED.set(node, null);
|
|
1183
|
+
own(light, node);
|
|
1184
|
+
if (ahead) {
|
|
1185
|
+
const at = lead.get(light) ?? 0;
|
|
1186
|
+
light.units.splice(at, 0, { a: node, z: node });
|
|
1187
|
+
lead.set(light, at + 1);
|
|
1188
|
+
}
|
|
1189
|
+
else
|
|
1190
|
+
light.units.push({ a: node, z: node });
|
|
1191
|
+
mark(light);
|
|
1192
|
+
};
|
|
1193
|
+
/** Which unit a light node is in — its own, or (a run's node in a slot) the run its stand-in sits in. */
|
|
1194
|
+
const unitIndexOf = (light, node) => {
|
|
1195
|
+
const stand = STAND.get(node);
|
|
1196
|
+
if (stand === undefined)
|
|
1197
|
+
return light.units.findIndex((unit) => unit.a === node);
|
|
1198
|
+
return light.units.findIndex((unit) => unit.a !== unit.z && follows(unit.a, stand) && follows(stand, unit.z));
|
|
1016
1199
|
};
|
|
1017
1200
|
/**
|
|
1018
|
-
*
|
|
1019
|
-
*
|
|
1020
|
-
* array; membership is live, so ask again after mutations). `''`/omitted is the default slot.
|
|
1201
|
+
* A node the USER put inside a slot's region — `slotted.before(node)`, `after()`: a light unit of its own, at that
|
|
1202
|
+
* place in light order. (No `mark`: only `replay` reaches here, for a record `note` has already marked the light for.)
|
|
1021
1203
|
*/
|
|
1022
|
-
const
|
|
1023
|
-
const
|
|
1024
|
-
if (
|
|
1025
|
-
return
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1204
|
+
const settleIn = (light, node) => {
|
|
1205
|
+
const parent = node.parentNode;
|
|
1206
|
+
if (parent === null || node.nodeType === 8)
|
|
1207
|
+
return;
|
|
1208
|
+
for (const rec of light.recs) {
|
|
1209
|
+
if (rec.re === null || rec.re.parentNode !== parent)
|
|
1210
|
+
continue;
|
|
1211
|
+
if (!follows(rec.rs, node) || !follows(node, rec.re))
|
|
1212
|
+
continue;
|
|
1213
|
+
let at = -1;
|
|
1214
|
+
for (let next = node.nextSibling; next !== null && next !== rec.re && at < 0; next = next.nextSibling)
|
|
1215
|
+
at = unitIndexOf(light, next);
|
|
1216
|
+
if (at < 0)
|
|
1217
|
+
light.units.push({ a: node, z: node });
|
|
1218
|
+
else
|
|
1219
|
+
light.units.splice(at, 0, { a: node, z: node });
|
|
1220
|
+
PLACED.set(node, rec);
|
|
1221
|
+
own(light, node);
|
|
1222
|
+
return;
|
|
1223
|
+
}
|
|
1224
|
+
};
|
|
1225
|
+
/**
|
|
1226
|
+
* **What the renderer wrote, read back into the runs.** A run's node in a slot is still the renderer's: an insert
|
|
1227
|
+
* before it (a new row), markers around it (a text becoming a template), its move or removal all happen where the
|
|
1228
|
+
* node now is. Each lands in the run at the place its neighbor's stand-in holds, so the run stays the renderer's
|
|
1229
|
+
* whole range; what has no such neighbor is the user's own edit.
|
|
1230
|
+
*/
|
|
1231
|
+
/**
|
|
1232
|
+
* **What the renderer took away from a run, read back** — the removed half of `replay`: a stand-in it removed takes its
|
|
1233
|
+
* node with it (cleared with its run) or into the fragment `hold` parked it in; a run's node it removed from a slot drops
|
|
1234
|
+
* its place. Called on its own for a host's own records while its first render runs, when a run still sits in the host.
|
|
1235
|
+
*/
|
|
1236
|
+
const unrun = (light, record) => {
|
|
1237
|
+
/** A reader: nothing can be in a holding not yet built, so `undefined` compares false. */
|
|
1238
|
+
const holding = light.holding;
|
|
1239
|
+
for (const node of record.removedNodes) {
|
|
1240
|
+
const real = REAL.get(node);
|
|
1241
|
+
if (real !== undefined) {
|
|
1242
|
+
/**
|
|
1243
|
+
* A stand-in the renderer took: cleared with its run (its node goes too), or parked by `hold` in a fragment (its
|
|
1244
|
+
* node joins it). One the renderer MOVED — beside a node of the run that sits in a slot — is still the run's; its
|
|
1245
|
+
* addition, below, puts it back at its new place.
|
|
1246
|
+
*/
|
|
1247
|
+
const parent = node.parentNode;
|
|
1248
|
+
if (parent === holding || (parent !== null && parent.nodeType !== 11))
|
|
1249
|
+
continue;
|
|
1250
|
+
if (parent === null)
|
|
1251
|
+
real.parentNode?.removeChild(real);
|
|
1252
|
+
else
|
|
1253
|
+
parent.insertBefore(real, node);
|
|
1254
|
+
PLACED.delete(real);
|
|
1255
|
+
unstand(real);
|
|
1256
|
+
}
|
|
1257
|
+
else if (STAND.has(node) && node.parentNode === null) {
|
|
1258
|
+
/** A run's node the renderer removed from its slot (a row deleted): its place goes too. */
|
|
1259
|
+
PLACED.delete(node);
|
|
1260
|
+
unstand(node);
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
};
|
|
1264
|
+
const replay = (light, record) => {
|
|
1265
|
+
/** A writer (runs move into holding below): built before it is captured. */
|
|
1266
|
+
const holding = holdingOf(light);
|
|
1267
|
+
unrun(light, record);
|
|
1268
|
+
for (const node of record.addedNodes) {
|
|
1269
|
+
if (node.parentNode !== record.target)
|
|
1270
|
+
continue;
|
|
1271
|
+
if (record.target === holding) {
|
|
1272
|
+
/** Back in holding (the renderer moved it before a marker there): its own place now, not its stand-in's. */
|
|
1273
|
+
if (STAND.has(node)) {
|
|
1274
|
+
PLACED.delete(node);
|
|
1275
|
+
unstand(node);
|
|
1276
|
+
}
|
|
1277
|
+
/** A run's node written straight into holding (a row the outer template inserted) is a light node from now on. */
|
|
1278
|
+
own(light, node);
|
|
1279
|
+
continue;
|
|
1280
|
+
}
|
|
1281
|
+
if (PLACED.get(node) !== undefined && !STAND.has(node))
|
|
1282
|
+
continue;
|
|
1283
|
+
const next = record.nextSibling;
|
|
1284
|
+
const previous = record.previousSibling;
|
|
1285
|
+
/** Where the runs are: holding — and, until the host's first render ends, the host, where a fresh run still sits. */
|
|
1286
|
+
const inRun = (node) => node.parentNode === holding || (light.fresh && node.parentNode === light.host);
|
|
1287
|
+
const ahead = next === null ? undefined : (STAND.get(next) ?? (inRun(next) ? next : undefined));
|
|
1288
|
+
const behind = previous === null ? undefined : (STAND.get(previous) ?? (inRun(previous) ? previous : undefined));
|
|
1289
|
+
if (ahead === undefined && behind === undefined) {
|
|
1290
|
+
settleIn(light, node);
|
|
1291
|
+
continue;
|
|
1292
|
+
}
|
|
1293
|
+
const place = ahead ?? behind.nextSibling;
|
|
1038
1294
|
/**
|
|
1039
|
-
*
|
|
1040
|
-
*
|
|
1041
|
-
*
|
|
1042
|
-
* counts as the default slot (measured against native `assignedNodes`). One rule,
|
|
1043
|
-
* `(getAttribute('name') ?? '') === name`, is the platform's own and covers both.
|
|
1295
|
+
* A run's node moved within the page: its place in the run moves. New to the run: it joins it, at that place — in
|
|
1296
|
+
* whatever holds the run: holding, or (until its host's first render ends) the HOST itself, where a fresh run still
|
|
1297
|
+
* sits. Inserted into holding regardless, a node new to a fresh run threw there and was left in the slot unrecorded.
|
|
1044
1298
|
*/
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1299
|
+
const stand = STAND.get(node);
|
|
1300
|
+
const run = place === null ? holding : place.parentNode;
|
|
1301
|
+
(run === holding && stand === undefined ? park(light) : run).insertBefore(stand ?? node, place);
|
|
1302
|
+
own(light, node);
|
|
1048
1303
|
}
|
|
1049
|
-
return [];
|
|
1050
1304
|
};
|
|
1305
|
+
/** A captured host that turned out to have a shadow root: its children go back, in light order, for the platform. */
|
|
1306
|
+
const release = (light) => {
|
|
1307
|
+
for (const unit of light.units) {
|
|
1308
|
+
if (unit.a === unit.z) {
|
|
1309
|
+
move(light.host, unit.a, null);
|
|
1310
|
+
continue;
|
|
1311
|
+
}
|
|
1312
|
+
for (let node = unit.a.nextSibling; node !== null && node !== unit.z;) {
|
|
1313
|
+
const next = node.nextSibling;
|
|
1314
|
+
light.host.appendChild(REAL.get(node) ?? node);
|
|
1315
|
+
node = next;
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
light.holding?.remove();
|
|
1319
|
+
HOSTS.delete(light.host);
|
|
1320
|
+
work.delete(light);
|
|
1321
|
+
/**
|
|
1322
|
+
* Its observer goes with it. Bound to its light, it would otherwise keep handling the host's records as this light's
|
|
1323
|
+
* — adopting the platform's own light children into a holding that no longer exists (renderer-slots' native-sequence
|
|
1324
|
+
* row, 2026-10-17). Before the binding, a released light's records were simply not found and dropped.
|
|
1325
|
+
*/
|
|
1326
|
+
light.obs?.disconnect();
|
|
1327
|
+
light.obs = undefined;
|
|
1328
|
+
};
|
|
1329
|
+
/* ── capture ───────────────────────────────────────────────────────────────────────────────────── */
|
|
1051
1330
|
/**
|
|
1052
|
-
*
|
|
1053
|
-
*
|
|
1054
|
-
*
|
|
1055
|
-
*
|
|
1056
|
-
*
|
|
1057
|
-
*
|
|
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.
|
|
1331
|
+
* **A light's childList registrations, once its first render has ENDED** (observer redesign, 2026-10-17).
|
|
1332
|
+
* ONE over the host's subtree — its own children and every region and slot inside it: what native slotting reacts to,
|
|
1333
|
+
* wherever it sits, so a region's parent needs no registration of its own — and its holding (out of the page while empty).
|
|
1334
|
+
* Not before: the first render's own writes would make a record each for nothing (`reconcile` covers that window).
|
|
1335
|
+
* Attribute changes are registered per NODE (`own`, a slot's `name` in `watch`): one subtree attribute registration made
|
|
1336
|
+
* Firefox check every attribute write inside the host (−17.7% of light-slots creation).
|
|
1063
1337
|
*/
|
|
1338
|
+
const register = (light) => {
|
|
1339
|
+
if (light.ho === true)
|
|
1340
|
+
return;
|
|
1341
|
+
light.ho = true;
|
|
1342
|
+
observerOf(light).observe(light.host, { childList: true, subtree: true });
|
|
1343
|
+
for (const node of lightOf(light))
|
|
1344
|
+
own(light, node);
|
|
1345
|
+
/**
|
|
1346
|
+
* A light with a RUN (a part an outer template writes into, for as long as the host lives) has its holding watched
|
|
1347
|
+
* from now — the run moves there, and is written into there, even while holding holds nothing else. One with static
|
|
1348
|
+
* light children only is watched once something is parked (`park`), or never.
|
|
1349
|
+
*/
|
|
1350
|
+
if (
|
|
1351
|
+
/** `hydrate-slots` writes runs straight into holding and relies on it being watched (hydrate-slots.ts, "holding is watched"). */
|
|
1352
|
+
wired?._$hydrateSlots$ !== undefined ||
|
|
1353
|
+
light.units.some((unit) => unit.a !== unit.z) ||
|
|
1354
|
+
(light.holding?.childNodes.length ?? 0) > 2)
|
|
1355
|
+
watchHolding(light);
|
|
1356
|
+
};
|
|
1064
1357
|
/**
|
|
1065
|
-
*
|
|
1066
|
-
*
|
|
1067
|
-
*
|
|
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.
|
|
1358
|
+
* **Holding, watched once anything lives in it** — a run's markers and stand-ins, or a parked node — since the outer
|
|
1359
|
+
* template keeps writing into a run there, in or out of the page. An empty holding (every light node took a slot) is
|
|
1360
|
+
* never written into, so it costs no registration (2026-10-17).
|
|
1077
1361
|
*/
|
|
1078
|
-
const
|
|
1362
|
+
const watchHolding = (light) => {
|
|
1363
|
+
if (light.ho !== true || light.hh === true)
|
|
1364
|
+
return;
|
|
1365
|
+
light.hh = true;
|
|
1366
|
+
watch(holdingOf(light), light);
|
|
1367
|
+
};
|
|
1079
1368
|
/**
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1082
|
-
*
|
|
1083
|
-
*
|
|
1084
|
-
*
|
|
1369
|
+
* **What changed while a light was not yet observed — its first render** — read off the DOM once, at that render's end
|
|
1370
|
+
* (3c): who made the change does not matter, the renderer or user code (a child's `connectedCallback`, a
|
|
1371
|
+
* directive). A stand-in gone takes its node with it, as `unrun` does for a removal record; a host child nothing knows
|
|
1372
|
+
* of is a light child added meanwhile (`adopt`) — unless it is the render's own (between its markers: `adopt` checks the
|
|
1373
|
+
* range), one of the captured units, or a member of a captured run.
|
|
1085
1374
|
*/
|
|
1086
|
-
const
|
|
1087
|
-
const
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
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;
|
|
1375
|
+
const reconcile = (light) => {
|
|
1376
|
+
for (const rec of light.recs)
|
|
1377
|
+
for (const node of [...rec.shown]) {
|
|
1378
|
+
const stand = STAND.get(node);
|
|
1379
|
+
if (stand === undefined)
|
|
1380
|
+
continue;
|
|
1381
|
+
if (node.parentNode === null) {
|
|
1382
|
+
PLACED.delete(node);
|
|
1383
|
+
unstand(node);
|
|
1384
|
+
continue;
|
|
1137
1385
|
}
|
|
1386
|
+
const parent = stand.parentNode;
|
|
1387
|
+
if (parent === null)
|
|
1388
|
+
node.parentNode.removeChild(node);
|
|
1389
|
+
else if (parent.nodeType === 11)
|
|
1390
|
+
parent.insertBefore(node, stand);
|
|
1391
|
+
else
|
|
1392
|
+
continue;
|
|
1393
|
+
PLACED.delete(node);
|
|
1394
|
+
unstand(node);
|
|
1138
1395
|
}
|
|
1139
|
-
parent.removeChild(slot);
|
|
1140
|
-
}
|
|
1141
|
-
/** Whatever no slot claimed goes into the inert carrier, in its original order per name. */
|
|
1142
1396
|
/**
|
|
1143
|
-
*
|
|
1144
|
-
*
|
|
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.
|
|
1397
|
+
* A node written INTO a slot's region meanwhile — a keyed row inserted beside a slotted neighbor follows it there
|
|
1398
|
+
* (`into`) — is handed to `replay` as the record it would have been: its parent, itself, its neighbors now.
|
|
1160
1399
|
*/
|
|
1161
|
-
for (const
|
|
1162
|
-
|
|
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)
|
|
1400
|
+
for (const rec of light.recs) {
|
|
1401
|
+
if (rec.rs === null || rec.re === null)
|
|
1173
1402
|
continue;
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1403
|
+
for (let node = rec.rs.nextSibling; node !== null && node !== rec.re;) {
|
|
1404
|
+
const next = node.nextSibling;
|
|
1405
|
+
if (!PLACED.has(node) && node.nodeType !== 8)
|
|
1406
|
+
replay(light, { target: rec.re.parentNode, addedNodes: [node], removedNodes: [], previousSibling: node.previousSibling, nextSibling: node.nextSibling });
|
|
1407
|
+
node = next;
|
|
1177
1408
|
}
|
|
1178
|
-
for (const node of bucket)
|
|
1179
|
-
carrier.appendChild(node);
|
|
1180
1409
|
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1410
|
+
const host = light.host;
|
|
1411
|
+
/** The render's range, once: its first and last comment that is not a captured unit's (as `adopt` reads it). */
|
|
1412
|
+
const ours = new Set();
|
|
1413
|
+
for (const unit of light.units)
|
|
1414
|
+
ours.add(unit.a).add(unit.z);
|
|
1415
|
+
let first = null;
|
|
1416
|
+
let last = null;
|
|
1417
|
+
for (let child = host.firstChild; child !== null; child = child.nextSibling)
|
|
1418
|
+
if (child.nodeType === 8 && !ours.has(child)) {
|
|
1419
|
+
first ??= child;
|
|
1420
|
+
last = child;
|
|
1421
|
+
}
|
|
1422
|
+
const ranged = first !== null && first !== last;
|
|
1423
|
+
for (const child of [...host.childNodes]) {
|
|
1424
|
+
if (child.nodeType === 8 || child === light.holding || ours.has(child) || PLACED.has(child))
|
|
1425
|
+
continue;
|
|
1426
|
+
if (ranged && follows(first, child) && follows(child, last))
|
|
1427
|
+
continue;
|
|
1428
|
+
if (light.units.some((unit) => unit.a !== unit.z && follows(unit.a, child) && follows(child, unit.z)))
|
|
1429
|
+
continue;
|
|
1430
|
+
adopt(light, child);
|
|
1201
1431
|
}
|
|
1202
1432
|
};
|
|
1433
|
+
/** A host's light record, new: its holding (bracketed by two comments, so `lastChild` is always an anchor), watched. */
|
|
1203
1434
|
/**
|
|
1204
|
-
*
|
|
1205
|
-
*
|
|
1206
|
-
*
|
|
1207
|
-
*
|
|
1435
|
+
* **`node.assignedSlot`, answered for light slots** (Brian, 2026-10-18: race it). The platform's getter knows only shadow
|
|
1436
|
+
* roots; ONE getter replaces it on the prototypes that define it (the Slottable mixin: `Element`, `Text`), per window,
|
|
1437
|
+
* at that window's first light host. It answers from `PLACED` — what distribution already records, so nothing is written
|
|
1438
|
+
* per node — for a node this copy placed in a slot's region, and otherwise defers to the getter it replaced (native, or
|
|
1439
|
+
* another copy's), so shadow-DOM nodes and other copies answer as before. Never on a server: no window with these
|
|
1440
|
+
* prototypes, nothing installed.
|
|
1208
1441
|
*/
|
|
1209
|
-
|
|
1210
|
-
|
|
1442
|
+
const PATCHED = Symbol();
|
|
1443
|
+
const answerAssigned = (win) => {
|
|
1444
|
+
const marked = win;
|
|
1445
|
+
if (marked[PATCHED] === true)
|
|
1211
1446
|
return;
|
|
1212
|
-
|
|
1213
|
-
|
|
1447
|
+
marked[PATCHED] = true;
|
|
1448
|
+
for (const proto of [win.Element?.prototype, win.Text?.prototype]) {
|
|
1449
|
+
const before = proto === undefined ? undefined : Object.getOwnPropertyDescriptor(proto, 'assignedSlot');
|
|
1450
|
+
const native = before?.get;
|
|
1451
|
+
/** Where the platform puts no getter of its own, nothing is defined there (development says so): never a fresh property. */
|
|
1452
|
+
if (native === undefined) {
|
|
1453
|
+
if (true && proto !== undefined)
|
|
1454
|
+
console.warn(diagnostic('slots', 'assignedSlot', 'assigned-slot-unpatched', true && PROSE$1['assigned-slot-unpatched'](proto === win.Text?.prototype ? 'Text' : 'Element')));
|
|
1455
|
+
continue;
|
|
1456
|
+
}
|
|
1457
|
+
Object.defineProperty(proto, 'assignedSlot', {
|
|
1458
|
+
configurable: true,
|
|
1459
|
+
enumerable: before.enumerable,
|
|
1460
|
+
get() {
|
|
1461
|
+
const rec = PLACED.get(this);
|
|
1462
|
+
return rec !== undefined && rec !== null && rec !== ADOPTED ? rec.slot : native.call(this);
|
|
1463
|
+
},
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1214
1466
|
};
|
|
1215
1467
|
/**
|
|
1216
|
-
*
|
|
1217
|
-
*
|
|
1218
|
-
*
|
|
1219
|
-
* light host has captured, which is every node in an app without light slots.
|
|
1468
|
+
* **Holding, built at its FIRST use** (2026-10-18): a host whose light children are all assigned never needs it, and
|
|
1469
|
+
* building it for every host was ~82 ms of Chrome's slotted creation at 10 000 hosts. Every WRITER goes through here;
|
|
1470
|
+
* readers compare against `light.holding`, which is simply `undefined` until then — nothing can be in it.
|
|
1220
1471
|
*/
|
|
1221
|
-
|
|
1222
|
-
const
|
|
1223
|
-
|
|
1472
|
+
const dress = (holding) => {
|
|
1473
|
+
const doc = holding.ownerDocument;
|
|
1474
|
+
holding.setAttribute('hidden', '');
|
|
1475
|
+
holding.setAttribute(UNASSIGNED, '');
|
|
1476
|
+
/**
|
|
1477
|
+
* `hidden` is only the UA's `display: none`, and any author rule that sets `display` beats it — a reset, a design
|
|
1478
|
+
* system's `:where(*)`, a `[hidden]` override — and unassigned content would RENDER. An inline `!important` beats every
|
|
1479
|
+
* author stylesheet; written through CSSOM, which a strict CSP does not block (served markup carries `hidden` only).
|
|
1480
|
+
*/
|
|
1481
|
+
holding.style.setProperty('display', 'none', 'important');
|
|
1482
|
+
holding.insertBefore(doc.createComment(''), holding.firstChild);
|
|
1483
|
+
holding.append(doc.createComment(''));
|
|
1484
|
+
return holding;
|
|
1224
1485
|
};
|
|
1225
|
-
|
|
1226
|
-
takeOverSlot._$server$ = serverDistribute;
|
|
1486
|
+
const holdingOf = (light) => (light.holding ??= dress(light.host.ownerDocument.createElement('ins')));
|
|
1227
1487
|
/**
|
|
1228
|
-
*
|
|
1229
|
-
*
|
|
1230
|
-
|
|
1231
|
-
|
|
1488
|
+
* The capture hydration is handed: hydrate-slots writes a served host's runs straight into holding (and reads its
|
|
1489
|
+
* anchors), so a light captured through the protocol has its holding built at once — a client-only light builds it lazily.
|
|
1490
|
+
*/
|
|
1491
|
+
/** The lights hydration reads: each with its holding built (it reads the anchors), whichever capture made it. */
|
|
1492
|
+
const SERVED_HOSTS = {
|
|
1493
|
+
get: (host) => {
|
|
1494
|
+
const light = HOSTS.get(host);
|
|
1495
|
+
if (light !== undefined)
|
|
1496
|
+
holdingOf(light);
|
|
1497
|
+
return light;
|
|
1498
|
+
},
|
|
1499
|
+
has: (host) => HOSTS.has(host),
|
|
1500
|
+
};
|
|
1501
|
+
const captureServed = (...args) => {
|
|
1502
|
+
const light = capture(...args);
|
|
1503
|
+
holdingOf(light);
|
|
1504
|
+
return light;
|
|
1505
|
+
};
|
|
1506
|
+
const lightFor = (host, late, carrier = null) => {
|
|
1507
|
+
const doc = host.ownerDocument;
|
|
1508
|
+
const win = doc.defaultView;
|
|
1509
|
+
if (win !== null)
|
|
1510
|
+
answerAssigned(win);
|
|
1511
|
+
arm();
|
|
1512
|
+
/** The server's carrier, adopted at once; otherwise holding is built at its first use (`holdingOf`). */
|
|
1513
|
+
const light = { host, units: [], holding: carrier === null ? undefined : dress(carrier), recs: [], dirty: false, fresh: false, late, wait: false };
|
|
1514
|
+
HOSTS.set(host, light);
|
|
1515
|
+
return light;
|
|
1516
|
+
};
|
|
1517
|
+
/**
|
|
1518
|
+
* **Capture**, at `init` — before the host's first render, when everything it holds is the page's. Each static child
|
|
1519
|
+
* is a unit; each run between statics (what the page's template bound there) is one unit, wrapped in this module's
|
|
1520
|
+
* two comments so its extent survives whatever the renderer writes inside it.
|
|
1232
1521
|
*
|
|
1233
|
-
* `
|
|
1234
|
-
*
|
|
1235
|
-
* `
|
|
1522
|
+
* `nodes` and `carrier` are hydration's (`_$capture$`): a served host's light children in light order, wherever the server
|
|
1523
|
+
* distributed them, and the server's unassigned container, which becomes the holding. A node captured in a slot's region
|
|
1524
|
+
* stays there (`ADOPTED`) — distribution then moves only what is out of place, so the server's placement decides at most
|
|
1525
|
+
* WHETHER a node moves, never where it ends up.
|
|
1236
1526
|
*/
|
|
1237
|
-
const
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
const state = capture(host, /* skipChildren */ true);
|
|
1527
|
+
const capture = (host, before = null, nodes = [...host.childNodes], carrier = null, wait = false) => {
|
|
1528
|
+
let light = HOSTS.get(host);
|
|
1529
|
+
/** A slot of a custom element `init` never saw mounted first: its light content arrives now, at the render's end. */
|
|
1530
|
+
if (light !== undefined && !light.late)
|
|
1531
|
+
return light;
|
|
1243
1532
|
const doc = host.ownerDocument;
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
parent.insertBefore(start, firstShown ?? before);
|
|
1249
|
-
parent.insertBefore(end, before);
|
|
1250
|
-
const isAssigned = assigned !== null && assigned.length > 0;
|
|
1533
|
+
light ??= lightFor(host, false, carrier);
|
|
1534
|
+
light.late = false;
|
|
1535
|
+
const statics = STATICS.get(host);
|
|
1536
|
+
let run = null;
|
|
1251
1537
|
/**
|
|
1252
|
-
*
|
|
1253
|
-
*
|
|
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.
|
|
1538
|
+
* Recorded, not moved: the children stay where the page put them — in the host, as they would under a shadow root —
|
|
1539
|
+
* until the host's first render finishes, which distributes them (the ones no slot takes, invisibly to holding).
|
|
1258
1540
|
*/
|
|
1259
|
-
const
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
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,
|
|
1541
|
+
const close = () => {
|
|
1542
|
+
if (run === null)
|
|
1543
|
+
return;
|
|
1544
|
+
const a = doc.createComment('');
|
|
1545
|
+
const z = doc.createComment('');
|
|
1546
|
+
host.insertBefore(a, run[0]);
|
|
1547
|
+
host.insertBefore(z, run[run.length - 1].nextSibling);
|
|
1548
|
+
light.units.push({ a, z });
|
|
1549
|
+
run = null;
|
|
1287
1550
|
};
|
|
1288
|
-
const
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1551
|
+
for (const child of nodes) {
|
|
1552
|
+
/** At a first render's end, only what precedes the render's own range is light content. */
|
|
1553
|
+
if (child === before)
|
|
1554
|
+
break;
|
|
1555
|
+
const parent = child.parentNode;
|
|
1556
|
+
if (parent !== host)
|
|
1557
|
+
PLACED.set(child, parent === light.holding ? null : ADOPTED);
|
|
1558
|
+
own(light, child);
|
|
1559
|
+
if (statics === undefined || statics.has(child)) {
|
|
1560
|
+
close();
|
|
1561
|
+
light.units.push({ a: child, z: child });
|
|
1562
|
+
}
|
|
1563
|
+
else
|
|
1564
|
+
(run ??= []).push(child);
|
|
1295
1565
|
}
|
|
1296
|
-
|
|
1297
|
-
|
|
1566
|
+
close();
|
|
1567
|
+
/**
|
|
1568
|
+
* `wait`: captured by a template that placed content into this host (hydration), not by the host — nothing is
|
|
1569
|
+
* distributed until the host's own slots mount, so a host never defined keeps its nodes where they stand.
|
|
1570
|
+
*/
|
|
1571
|
+
if (wait)
|
|
1572
|
+
light.wait = true;
|
|
1573
|
+
else {
|
|
1574
|
+
light.fresh = true;
|
|
1575
|
+
mark(light);
|
|
1576
|
+
}
|
|
1577
|
+
return light;
|
|
1298
1578
|
};
|
|
1299
|
-
takeOverSlot._$adopt$ = adoptSlot;
|
|
1300
1579
|
/**
|
|
1301
|
-
*
|
|
1302
|
-
*
|
|
1303
|
-
*
|
|
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.
|
|
1580
|
+
* **A dashed element in a template is told it was created** — before the page's template commits into it — so the
|
|
1581
|
+
* children it has then are exactly its statics: a binding position is still the empty text the template parsed it
|
|
1582
|
+
* as. Recorded by node; `init` capture reads it.
|
|
1321
1583
|
*/
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1584
|
+
/**
|
|
1585
|
+
* **Which elements each render root's templates created** — so that root's render end reads their lights' records too:
|
|
1586
|
+
* a host whose light children an OUTER template changed is distributed by the end of that render, synchronously, though
|
|
1587
|
+
* it has an observer of its own (Gecko's `observe()` is linear in its observer's registrations, so none is shared).
|
|
1588
|
+
* Weak both ways: a root or an element gone takes its entries with it, and a dead ref is dropped by the read itself.
|
|
1589
|
+
*/
|
|
1590
|
+
const FED = new WeakMap();
|
|
1591
|
+
/**
|
|
1592
|
+
* Only for a dashed element its TEMPLATE gives children (slot-discovery decides once per template, at preparation): no
|
|
1593
|
+
* outer template can write into one it gives none, so there is nothing to record — an app's list of `<x-row></x-row>`
|
|
1594
|
+
* paid a child scan and a Set per row at create, and a WeakRef per row in its root's `FED` at mount, which that root's
|
|
1595
|
+
* every render then filtered (wiring slots made such a list ~9× slower to create, 2026-10-17).
|
|
1596
|
+
*/
|
|
1597
|
+
const hostBehavior = {
|
|
1598
|
+
mount: (element, { root }) => {
|
|
1599
|
+
if (root === null)
|
|
1600
|
+
return;
|
|
1601
|
+
let fed = FED.get(root);
|
|
1602
|
+
if (fed === undefined) {
|
|
1603
|
+
arm();
|
|
1604
|
+
FED.set(root, (fed = []));
|
|
1332
1605
|
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
const
|
|
1606
|
+
fed.push(new WeakRef(element));
|
|
1607
|
+
},
|
|
1608
|
+
create: (element, adopted) => {
|
|
1609
|
+
if (adopted)
|
|
1610
|
+
return;
|
|
1611
|
+
const statics = new Set();
|
|
1612
|
+
let hole = false;
|
|
1613
|
+
for (const child of [...element.childNodes]) {
|
|
1614
|
+
const placeholder = child.nodeType === 3 && child.data === '';
|
|
1342
1615
|
/**
|
|
1343
|
-
*
|
|
1344
|
-
*
|
|
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.
|
|
1616
|
+
* Two bindings written back to back (`${a}${b}`) have nothing static between them, so their runs would join into
|
|
1617
|
+
* one unit: a comment of this module's goes between them — every binding is its own unit.
|
|
1352
1618
|
*/
|
|
1353
|
-
if (
|
|
1354
|
-
const
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
continue;
|
|
1619
|
+
if (placeholder && hole) {
|
|
1620
|
+
const between = element.ownerDocument.createComment('');
|
|
1621
|
+
element.insertBefore(between, child);
|
|
1622
|
+
statics.add(between);
|
|
1358
1623
|
}
|
|
1359
|
-
if (
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
collect(element);
|
|
1624
|
+
if (!placeholder)
|
|
1625
|
+
statics.add(child);
|
|
1626
|
+
hole = placeholder;
|
|
1363
1627
|
}
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
for (const node of rescued)
|
|
1367
|
-
node.parentNode?.removeChild(node);
|
|
1368
|
-
return rescued.length > 0 ? rescued : null;
|
|
1628
|
+
STATICS.set(element, statics);
|
|
1629
|
+
},
|
|
1369
1630
|
};
|
|
1370
|
-
|
|
1631
|
+
/* ── the slot element ──────────────────────────────────────────────────────────────────────────── */
|
|
1371
1632
|
/**
|
|
1372
|
-
* The
|
|
1373
|
-
*
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1633
|
+
* The `<slot>` element's own answers, from the live assignment (a slot outside a shadow tree answers nothing). With
|
|
1634
|
+
* `flatten`, an unassigned slot answers its fallback's slottables, a nested slot through itself.
|
|
1635
|
+
*/
|
|
1636
|
+
const assigned = (rec, elementsOnly, flatten = false) => {
|
|
1637
|
+
flush(rec.light);
|
|
1638
|
+
const out = rec.shown.filter((node) => !elementsOnly || node.nodeType === 1);
|
|
1639
|
+
/** The fallback is read only when NOTHING is assigned — assigned text with no element still answers [] for elements. */
|
|
1640
|
+
if (rec.shown.length > 0 || !flatten)
|
|
1641
|
+
return out;
|
|
1642
|
+
for (const node of rec.slot.childNodes) {
|
|
1643
|
+
const nested = node.$rec;
|
|
1644
|
+
if (nested !== undefined)
|
|
1645
|
+
out.push(...assigned(nested, elementsOnly, true));
|
|
1646
|
+
else if (node.nodeType === 1 && node.localName === 'slot')
|
|
1647
|
+
continue;
|
|
1648
|
+
else if (node.nodeType === 1 || (!elementsOnly && node.nodeType === 3))
|
|
1649
|
+
out.push(node);
|
|
1650
|
+
}
|
|
1651
|
+
/** A nested slot whose content stands in its place is read where it stands. */
|
|
1652
|
+
return out;
|
|
1653
|
+
};
|
|
1654
|
+
const slotBehavior = {
|
|
1655
|
+
mount: (slot, context) => {
|
|
1656
|
+
const root = context.root;
|
|
1657
|
+
/**
|
|
1658
|
+
* A shadow root distributes natively; only a light host (an element rendered into) is this module's. A host
|
|
1659
|
+
* captured at `init` that then rendered into a shadow root of its own gets its children back, for the platform.
|
|
1660
|
+
*/
|
|
1661
|
+
if (root !== null && root.nodeType === 11) {
|
|
1662
|
+
const owner = HOSTS.get(root.host);
|
|
1663
|
+
if (owner !== undefined && owner.recs.length === 0)
|
|
1664
|
+
release(owner);
|
|
1665
|
+
}
|
|
1666
|
+
if (root === null || root.nodeType !== 1)
|
|
1667
|
+
return undefined;
|
|
1668
|
+
if (true ) {
|
|
1669
|
+
const inert = [...slot.attributes].map((attribute) => attribute.name).filter((name) => name !== 'name');
|
|
1670
|
+
if (inert.length > 0)
|
|
1671
|
+
console.warn(diagnostic('slots', `<${root.localName}>`, 'slot-attributes', true && PROSE$1['slot-attributes'](`<slot${slot.hasAttribute('name') ? ` name="${slot.getAttribute('name')}"` : ''}>`, inert.map((name) => `\`${name}\``).join(', '))));
|
|
1672
|
+
}
|
|
1673
|
+
/** A host `init` never saw has no captured children: what it holds now is its own render, never light content. */
|
|
1674
|
+
/** Its own light's records first: what the user did to it this task is noted before this module's moves, which are dropped. */
|
|
1675
|
+
flush(HOSTS.get(root));
|
|
1676
|
+
const light = HOSTS.get(root) ?? lightFor(root, isCustomElementName(root.localName));
|
|
1677
|
+
const kept = slot;
|
|
1678
|
+
const rec = { slot: kept, light, rs: null, re: null, shown: [], queued: false };
|
|
1679
|
+
kept.$rec = rec;
|
|
1680
|
+
/** A region handed over (hydration: the server's two markers) IS this slot's region, and what is between them stays. */
|
|
1681
|
+
const served = kept._$region$;
|
|
1682
|
+
if (served !== undefined) {
|
|
1683
|
+
kept._$region$ = undefined;
|
|
1684
|
+
[rec.rs, rec.re] = served;
|
|
1685
|
+
for (let node = rec.rs.nextSibling; node !== rec.re; node = node.nextSibling) {
|
|
1686
|
+
PLACED.set(node, rec);
|
|
1687
|
+
node._$slotted$ = true;
|
|
1688
|
+
}
|
|
1689
|
+
watch(rec.re.parentNode, light);
|
|
1690
|
+
}
|
|
1691
|
+
kept.assignedNodes = (options) => assigned(rec, false, options?.flatten);
|
|
1692
|
+
kept.remove = removeSlot;
|
|
1693
|
+
kept.replaceWith = replaceSlot;
|
|
1694
|
+
kept.assignedElements = (options) => assigned(rec, true, options?.flatten);
|
|
1695
|
+
light.recs.push(rec);
|
|
1696
|
+
light.wait = false;
|
|
1697
|
+
watch(kept, light);
|
|
1698
|
+
distribute(light);
|
|
1699
|
+
drop(light);
|
|
1700
|
+
/**
|
|
1701
|
+
* Its FIRST slot, after its first render — in a later render of its own, or in ANOTHER container's (an applier
|
|
1702
|
+
* committing there, which never reaches this host's `_$done$`): once that render is over — a microtask, which no
|
|
1703
|
+
* render is ever halfway through — read what changed while it was unobserved, and observe it from then. Inside its
|
|
1704
|
+
* first render, its own `_$done$` does both.
|
|
1705
|
+
*/
|
|
1706
|
+
if (!light.fresh && light.ho !== true)
|
|
1707
|
+
queueMicrotask(() => {
|
|
1708
|
+
if (light.ho === true || HOSTS.get(light.host) !== light)
|
|
1709
|
+
return;
|
|
1710
|
+
reconcile(light);
|
|
1711
|
+
flush(light);
|
|
1712
|
+
register(light);
|
|
1713
|
+
});
|
|
1714
|
+
return rec;
|
|
1715
|
+
},
|
|
1716
|
+
/** Torn down: its content goes back to holding first, and the element back in its place, before the renderer removes it. */
|
|
1717
|
+
unmount: (rec) => {
|
|
1718
|
+
flush(rec.light);
|
|
1719
|
+
unlist(rec);
|
|
1720
|
+
unplace(rec);
|
|
1721
|
+
distribute(rec.light);
|
|
1722
|
+
drop(rec.light);
|
|
1723
|
+
},
|
|
1724
|
+
};
|
|
1725
|
+
/**
|
|
1726
|
+
* **The render-end hook, installed at the first thing that needs it** (2026-10-18): until then the renderer's
|
|
1727
|
+
* `_$done$` read finds `undefined` and every render ends without calling slots — a page that wired slots but has nothing
|
|
1728
|
+
* slotted yet pays nothing per render. Armed by the first light (`lightFor`), the first host a template feeds (FED), the
|
|
1729
|
+
* first park, and the first render of a dashed element that already holds something (`unadopted`, the renderer-alone path,
|
|
1730
|
+
* which captures at render end).
|
|
1731
|
+
*/
|
|
1732
|
+
let done;
|
|
1733
|
+
let doneOn;
|
|
1734
|
+
const arm = () => {
|
|
1735
|
+
if (doneOn !== undefined && doneOn._$done$ !== done)
|
|
1736
|
+
doneOn._$done$ = done;
|
|
1737
|
+
};
|
|
1738
|
+
/**
|
|
1739
|
+
* **A served light host whose first render nothing adopts** — the renderer asks (`_$unadopted$`) before that render
|
|
1740
|
+
* would APPEND beside the server's markup: two copies, the server's dead. The server states every host it renders while
|
|
1741
|
+
* slots is wired (`data-vm-light`): stated EMPTY (`N:`), the host holds nothing of the page's, so the server's render
|
|
1742
|
+
* is cleared (its SSR stylesheets kept) and the client renders fresh — working, without the server's head start; stated
|
|
1743
|
+
* with light content, that content cannot be told from the server's render without `hydrateSlots`, so the host stands
|
|
1744
|
+
* as served and the coded error names what to wire. Development warns once per page.
|
|
1745
|
+
*/
|
|
1746
|
+
const unadopted = (container) => {
|
|
1747
|
+
if (container.nodeType === 1 && isCustomElementName(container.localName))
|
|
1748
|
+
arm();
|
|
1749
|
+
/** Asked only for a container with something in it (the renderer skips an empty one: no server markup to append beside). */
|
|
1750
|
+
const spec = container.nodeType === 1 ? container.getAttribute(LIGHT_ATTR) : null;
|
|
1751
|
+
if (spec === null)
|
|
1752
|
+
return;
|
|
1753
|
+
if (!spec.endsWith(':'))
|
|
1754
|
+
/**
|
|
1755
|
+
* A WIRING state, not a misused argument, so the diagnostic shape (`[vera] slots: <host> — …`), not `misuse()`'s.
|
|
1756
|
+
* Production keeps its own short line, which already carries the code: the code-and-link shape measured +18 B on
|
|
1757
|
+
* vera-renderer-slots.min.js (Brian's byte rule, 2026-10-09 — a production message converts only where its bundle
|
|
1758
|
+
* is not larger). Development's text is the table's.
|
|
1759
|
+
*/
|
|
1760
|
+
throw new Error(true
|
|
1761
|
+
? diagnostic('slots', `<${container.localName}>`, 'hydration-unwired', true && PROSE$1['hydration-unwired']("this light host, holding the page's content, stands as served"))
|
|
1762
|
+
: '[vera] hydration-unwired');
|
|
1763
|
+
for (let node = container.firstChild; node !== null;) {
|
|
1764
|
+
const next = node.nextSibling;
|
|
1765
|
+
if (node.nodeType !== 1 || !node.hasAttribute('data-vm-sheet'))
|
|
1766
|
+
container.removeChild(node);
|
|
1767
|
+
node = next;
|
|
1768
|
+
}
|
|
1769
|
+
container.removeAttribute(LIGHT_ATTR);
|
|
1770
|
+
if (true )
|
|
1771
|
+
warnOnce('slots', 'the page', 'hydration-unwired', true && (() => PROSE$1['hydration-unwired']('its components re-render on the client instead of adopting the server markup')));
|
|
1772
|
+
};
|
|
1773
|
+
/** A light host's children in light order, as its record holds them — what a template placing content into it adopts. */
|
|
1774
|
+
const lightNodes = (host) => {
|
|
1775
|
+
const light = HOSTS.get(host);
|
|
1776
|
+
if (light === undefined)
|
|
1777
|
+
return null;
|
|
1778
|
+
flush(light);
|
|
1779
|
+
return lightOf(light);
|
|
1780
|
+
};
|
|
1781
|
+
/** The slotted nodes a light host shows in its slot of `name` — or a shadow host's, from the platform. */
|
|
1782
|
+
const slotted = (host, name = '') => {
|
|
1783
|
+
const light = HOSTS.get(host);
|
|
1784
|
+
if (light !== undefined) {
|
|
1785
|
+
flush(light);
|
|
1786
|
+
for (const rec of light.recs)
|
|
1787
|
+
if ((rec.slot.getAttribute('name') ?? '') === name && rec.shown.length > 0)
|
|
1788
|
+
return [...rec.shown];
|
|
1789
|
+
/** Unassigned, a node still belongs to the host: it waits in holding for a slot of its name. */
|
|
1790
|
+
return lightOf(light).filter((node) => slotNameOf(node) === name);
|
|
1791
|
+
}
|
|
1792
|
+
/**
|
|
1793
|
+
* Core's `_$r$` first: a CLOSED root is null through `shadowRoot`, and core keeps the root it attached under that
|
|
1794
|
+
* sigiled name in both modes (`@verajs/styles` reads it the same way); no mangling regex matches a sigil.
|
|
1795
|
+
* Names are compared, never put in a selector — a quote in one would throw.
|
|
1796
|
+
*/
|
|
1797
|
+
const root = host['_$r$'] ?? host.shadowRoot;
|
|
1798
|
+
const slot = [...(root?.querySelectorAll('slot') ?? [])].find((s) => (s.getAttribute('name') ?? '') === name);
|
|
1799
|
+
return slot === undefined ? [] : slot.assignedNodes();
|
|
1800
|
+
};
|
|
1801
|
+
const LIGHT_ATTR = 'data-vm-light';
|
|
1802
|
+
/**
|
|
1803
|
+
* Whether hydration is wired — it marks the registry (`_$hydrating$`) when it installs, which may be before or after this
|
|
1804
|
+
* module: a served host (`data-vm-light`) is then captured by the adoption, never by `init`'s capture.
|
|
1378
1805
|
*/
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1806
|
+
let wired = null;
|
|
1807
|
+
/**
|
|
1808
|
+
* **The `'slot'` insert: a marker** — `@verajs/ssr` distributes light hosts on the server exactly while it is wired. The
|
|
1809
|
+
* client claims each `<slot>` through `'element'`, so the function is never called here.
|
|
1810
|
+
*/
|
|
1811
|
+
const serve = { name: '@verajs/renderer/slots', on: 'slot', fn: () => null, priority: 50 };
|
|
1812
|
+
/** Discovery: `<slot>` elements and dashed hosts claimed in templates, through `elements`. */
|
|
1813
|
+
/**
|
|
1814
|
+
* **`observeSlots: 'lazy' | 'eager'`** (paired as HTML's `loading` pairs them) — a component's own option
|
|
1815
|
+
* (`init({ host, observeSlots: 'eager' }, …)`, `define({ tag, observeSlots })`), else the app's default
|
|
1816
|
+
* (`slots({ observeSlots: 'eager' })`), else `'lazy'`; resolved ONCE here and kept on the light.
|
|
1817
|
+
* - `'lazy'`: observed from its first `<slot>` or light child, and a component with neither gets nothing at all (wiring
|
|
1818
|
+
* slots made plain components 2.1–2.7× slower to create when every one was captured and observed). Fully correct for
|
|
1819
|
+
* everything a template renders.
|
|
1820
|
+
* - `'eager'`: captured and observed from its first render, so a child appended by hand before any slot renders is
|
|
1821
|
+
* hidden and a `<slot>` inserted by hand is seen — what native shadow DOM does for every host, at an observer per
|
|
1822
|
+
* component.
|
|
1823
|
+
*/
|
|
1824
|
+
/**
|
|
1825
|
+
* Development: `observeSlots` is `'lazy'` or `'eager'`; anything else reads as `'lazy'` — the BUILT-IN default, even where
|
|
1826
|
+
* the app's default is `'eager'`, as core does for `foreignNodes` — and says so.
|
|
1827
|
+
*/
|
|
1828
|
+
const checkObserve = (subject, value) => {
|
|
1829
|
+
if (value !== undefined && value !== 'lazy' && value !== 'eager')
|
|
1830
|
+
console.warn(diagnostic('slots', subject, 'observe-slots-option', true && PROSE$1['observe-slots-option'](typeof value === 'string' ? `'${value}'` : String(value))));
|
|
1384
1831
|
};
|
|
1832
|
+
const captureAtInit = (element, options) => {
|
|
1833
|
+
const own = options?.observeSlots;
|
|
1834
|
+
if (true )
|
|
1835
|
+
checkObserve(`<${element.localName}>`, own);
|
|
1836
|
+
const eager = (own !== undefined ? own : wired?._$observeSlots$) === 'eager';
|
|
1837
|
+
/** Nothing to hide and nothing expected (the cheap tests first — they settle every plain component). */
|
|
1838
|
+
if (element.firstChild === null && !eager)
|
|
1839
|
+
return;
|
|
1840
|
+
/**
|
|
1841
|
+
* A SHADOW host distributes natively: core attaches its root before this insert runs and keeps it under the
|
|
1842
|
+
* unmangled `_root` (a closed root is null through `shadowRoot`), quoted so this bundle's mangling leaves it alone.
|
|
1843
|
+
*/
|
|
1844
|
+
if (element['_$r$'] != null || element.shadowRoot !== null)
|
|
1845
|
+
return;
|
|
1846
|
+
/** A SERVER host waits: whether its marks are trusted is the following render's to decide (`read`, under adoption). */
|
|
1847
|
+
if (isCustomElementName(element.localName) && !HOSTS.has(element) && !(wired?._$hydrating$ === true && element.hasAttribute(LIGHT_ATTR))) {
|
|
1848
|
+
const light = capture(element);
|
|
1849
|
+
if (eager)
|
|
1850
|
+
light.ex = true;
|
|
1851
|
+
}
|
|
1852
|
+
};
|
|
1853
|
+
/** Development: the option key this hook reads, so core's unknown-option check accepts it (`$keys` sits on the FUNCTION). */
|
|
1854
|
+
if (true )
|
|
1855
|
+
Object.assign(captureAtInit, { $keys: ['observeSlots'], $module: '@verajs/renderer/slots' });
|
|
1856
|
+
const slotDiscovery = [
|
|
1857
|
+
elements,
|
|
1858
|
+
/**
|
|
1859
|
+
* **Capture at `init`** — core's insert point, before the component's first render, when everything a CUSTOM element
|
|
1860
|
+
* (a dash, not a reserved SVG/MathML name) holds is the page's: its light content, captured even if its `<slot>`
|
|
1861
|
+
* arrives later. Once per host: a reconnect finds it captured and its render untouched.
|
|
1862
|
+
*/
|
|
1863
|
+
{
|
|
1864
|
+
name: '@verajs/renderer/slots-capture',
|
|
1865
|
+
on: 'init',
|
|
1866
|
+
fn: captureAtInit,
|
|
1867
|
+
priority: 10,
|
|
1868
|
+
},
|
|
1869
|
+
{
|
|
1870
|
+
name: '@verajs/renderer/slot-discovery',
|
|
1871
|
+
on: 'element',
|
|
1872
|
+
fn: (el) => el.localName === 'slot' ? slotBehavior : el.firstChild !== null && isCustomElementName(el.localName) ? hostBehavior : undefined,
|
|
1873
|
+
priority: 10,
|
|
1874
|
+
},
|
|
1875
|
+
/**
|
|
1876
|
+
* **After every render** — the renderer says when one finishes (`_$done$`, off-chain like `$t`), and what it did to a
|
|
1877
|
+
* light host's children is distributed before anything reads them.
|
|
1878
|
+
*/
|
|
1879
|
+
(registry) => {
|
|
1880
|
+
/** Hydration's seam — off-chain like `_$done$`, stamped with this package's seam protocol: capture a served host. */
|
|
1881
|
+
wired = registry;
|
|
1882
|
+
registry._$capture$ = [PROTOCOL, captureServed, lightNodes, SERVED_HOSTS, standIn, PLACED, ADOPTED];
|
|
1883
|
+
registry._$unadopted$ = unadopted;
|
|
1884
|
+
registry._$parked$ = (root, owner) => {
|
|
1885
|
+
arm();
|
|
1886
|
+
if (owner !== null)
|
|
1887
|
+
PARKED.set(root, owner);
|
|
1888
|
+
(parkedObserver ??= new (globalThis.MutationObserver ?? root.ownerDocument.defaultView.MutationObserver)(parkedHandle)).observe(root, {
|
|
1889
|
+
attributes: true,
|
|
1890
|
+
attributeFilter: ['slot'],
|
|
1891
|
+
childList: true,
|
|
1892
|
+
subtree: true,
|
|
1893
|
+
});
|
|
1894
|
+
};
|
|
1895
|
+
/** Development only: how many lights wait for work — 0 once every rendered host is distributed (a retention pin). */
|
|
1896
|
+
if (true )
|
|
1897
|
+
registry._$slotsWork$ = () => work.size;
|
|
1898
|
+
/** Development only: how many hosts a root's renders have fed, as recorded — bounded by its live rows (a growth pin). */
|
|
1899
|
+
if (true )
|
|
1900
|
+
registry._$slotsFed$ = (root) => FED.get(root)?.length ?? 0;
|
|
1901
|
+
doneOn = registry;
|
|
1902
|
+
done = (container, start) => {
|
|
1903
|
+
/**
|
|
1904
|
+
* A CUSTOM element rendered into that `init` never captured (the renderer used on its own, without core): what it
|
|
1905
|
+
* held before its first render's range is its light content — Brian's ruling 4, custom elements by their name.
|
|
1906
|
+
*/
|
|
1907
|
+
let own = HOSTS.get(container);
|
|
1908
|
+
if (container.nodeType === 1 && (own?.late ?? container.firstChild !== start) && container.shadowRoot === null && isCustomElementName(container.localName)) {
|
|
1909
|
+
capture(container, start);
|
|
1910
|
+
own = HOSTS.get(container);
|
|
1911
|
+
}
|
|
1912
|
+
/**
|
|
1913
|
+
* Nothing of this module's here — no light, nothing waiting, no host this render fed, nothing parked: the render of
|
|
1914
|
+
* every component that needs no slotting ends here (a full `flush` per render was most of what wiring slots still
|
|
1915
|
+
* cost a plain component in Firefox).
|
|
1916
|
+
*/
|
|
1917
|
+
if (own === undefined && work.size === 0 && parkedObserver === undefined && FED.get(container) === undefined)
|
|
1918
|
+
return;
|
|
1919
|
+
/** Its own render ended: a fresh light that left the work set (disconnected meanwhile) is distributed now. */
|
|
1920
|
+
if (own?.fresh && !own.wait)
|
|
1921
|
+
work.add(own);
|
|
1922
|
+
/**
|
|
1923
|
+
* Unobserved until it has a slot: what changed meanwhile is read once — at its first render's end, and at the end of
|
|
1924
|
+
* the render that mounted its first slot — before the distribution below. A light with no slot shows nothing, so
|
|
1925
|
+
* nothing about it needs following until then.
|
|
1926
|
+
*/
|
|
1927
|
+
const slotted = own !== undefined && own.recs.length > 0;
|
|
1928
|
+
if (own !== undefined && own.ho !== true && (own.fresh || slotted))
|
|
1929
|
+
reconcile(own);
|
|
1930
|
+
ending = true;
|
|
1931
|
+
try {
|
|
1932
|
+
flush(own, container);
|
|
1933
|
+
}
|
|
1934
|
+
finally {
|
|
1935
|
+
ending = false;
|
|
1936
|
+
}
|
|
1937
|
+
/** Observed from here on — after this module's own moves above, which then make no records. */
|
|
1938
|
+
if (own !== undefined && own.ho !== true && (slotted || own.ex === true))
|
|
1939
|
+
register(own);
|
|
1940
|
+
};
|
|
1941
|
+
/** A page whose components never needed slotting keeps `_$done$` undefined: their renders never call into slots. */
|
|
1942
|
+
},
|
|
1943
|
+
];
|
|
1944
|
+
const parts = [slotDiscovery, serve];
|
|
1945
|
+
/**
|
|
1946
|
+
* **`slots` is a DUAL** — `wire([renderer, slots])`, or `wire([renderer, slots({ observeSlots: 'eager' })])` to make
|
|
1947
|
+
* observing from the first render every light component's default (a component's own `observeSlots` wins either way). Bare, `wire` hands it the
|
|
1948
|
+
* registry and `wire` itself, and it wires its parts; called, it returns a connector doing the same and writing the
|
|
1949
|
+
* default on that registry — two cores on one page keep their own.
|
|
1950
|
+
*/
|
|
1951
|
+
/**
|
|
1952
|
+
* A `wire` that hands a connector only the registry — a core older than this module, or code calling connectors itself —
|
|
1953
|
+
* can wire nothing of slots: said, in both builds, rather than leaving every slot quietly undistributed.
|
|
1954
|
+
*/
|
|
1955
|
+
const tooOld = () => {
|
|
1956
|
+
throw new Error(true ? diagnostic('slots', 'wire([slots])', 'slots-wire-too-old', true && PROSE$1['slots-wire-too-old']()) : '[vera] slots-wire-too-old');
|
|
1957
|
+
};
|
|
1958
|
+
const slots = ((given, w) => {
|
|
1959
|
+
/** Told apart by the REGISTRY (its `get`), as the module rule says — never by whether a second argument came. */
|
|
1960
|
+
if (typeof given?.get === 'function') {
|
|
1961
|
+
if (typeof w !== 'function')
|
|
1962
|
+
tooOld();
|
|
1963
|
+
w(parts);
|
|
1964
|
+
return undefined;
|
|
1965
|
+
}
|
|
1966
|
+
const options = given;
|
|
1967
|
+
if (true ) {
|
|
1968
|
+
for (const key in options)
|
|
1969
|
+
if (key !== 'observeSlots')
|
|
1970
|
+
console.warn(diagnostic('slots', 'slots()', 'unknown-option', true && unknownOption(key, 'observeSlots')));
|
|
1971
|
+
checkObserve('slots()', options?.observeSlots);
|
|
1972
|
+
}
|
|
1973
|
+
const observe = options?.observeSlots;
|
|
1974
|
+
return (registry, wire) => {
|
|
1975
|
+
if (typeof wire !== 'function')
|
|
1976
|
+
tooOld();
|
|
1977
|
+
wire(parts);
|
|
1978
|
+
if (observe !== undefined)
|
|
1979
|
+
registry._$observeSlots$ = observe;
|
|
1980
|
+
};
|
|
1981
|
+
});
|
|
1385
1982
|
|
|
1386
|
-
export { slots, slotted };
|
|
1983
|
+
export { slotDiscovery, slots, slotted };
|
|
1387
1984
|
//# sourceMappingURL=vera-renderer-slots.js.map
|