@verajs/ssr 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 +507 -390
- package/dist/vera/diagnostics.d.ts +20 -0
- package/dist/vera/diagnostics.js +98 -0
- package/dist/vera/escaping.d.ts +142 -0
- package/dist/vera/escaping.js +267 -0
- package/dist/vera/events.d.ts +41 -0
- package/dist/vera/events.js +162 -0
- package/dist/vera/frames.d.ts +38 -0
- package/dist/vera/frames.js +195 -0
- package/dist/vera/index.d.ts +48 -0
- package/dist/vera/index.js +480 -0
- package/{types → dist/vera}/nodes.d.ts +389 -302
- package/dist/vera/nodes.js +2322 -0
- package/dist/vera/parse.d.ts +37 -0
- package/dist/vera/parse.js +390 -0
- package/dist/vera/reflections.d.ts +566 -0
- package/dist/vera/reflections.js +820 -0
- package/dist/vera/registry.d.ts +14 -0
- package/{src → dist}/vera/registry.js +5 -1
- package/dist/vera/report.d.ts +27 -0
- package/dist/vera/report.js +56 -0
- package/dist/vera/scan.d.ts +37 -0
- package/dist/vera/scan.js +143 -0
- package/dist/vera/select.d.ts +40 -0
- package/dist/vera/select.js +261 -0
- package/dist/vera/serializer.d.ts +39 -0
- package/dist/vera/serializer.js +1372 -0
- package/dist/vera/shim.d.ts +42 -0
- package/dist/vera/shim.js +853 -0
- package/dist/vera/slots.d.ts +5 -0
- package/dist/vera/slots.js +104 -0
- package/dist/vera/stylesheets.d.ts +62 -0
- package/dist/vera/stylesheets.js +180 -0
- package/dist/vera/tokenizer.d.ts +32 -0
- package/dist/vera/tokenizer.js +528 -0
- package/dist/vera/types.d.ts +117 -0
- package/dist/vera/views.d.ts +63 -0
- package/dist/vera/views.js +245 -0
- package/package.json +12 -13
- package/src/vera/escaping.js +0 -135
- package/src/vera/events.js +0 -164
- package/src/vera/frames.js +0 -102
- package/src/vera/index.js +0 -1187
- package/src/vera/nodes.js +0 -2294
- package/src/vera/parse.js +0 -381
- package/src/vera/reflections.js +0 -811
- package/src/vera/select.js +0 -234
- package/src/vera/serializer.js +0 -1080
- package/src/vera/shim.js +0 -761
- package/src/vera/stylesheets.js +0 -155
- package/src/vera/views.js +0 -240
- package/types/escaping.d.ts +0 -27
- package/types/events.d.ts +0 -4
- package/types/frames.d.ts +0 -12
- package/types/index.d.ts +0 -13
- package/types/parse.d.ts +0 -19
- package/types/reflections.d.ts +0 -657
- package/types/registry.d.ts +0 -8
- package/types/select.d.ts +0 -5
- package/types/serializer.d.ts +0 -26
- package/types/shim.d.ts +0 -23
- package/types/stylesheets.d.ts +0 -53
- package/types/views.d.ts +0 -26
package/src/vera/nodes.js
DELETED
|
@@ -1,2294 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The server's DOM nodes: an element, a shadow root, a fragment, and the container they share.
|
|
3
|
-
*
|
|
4
|
-
* These hold their children as a **string**. Nothing in this package parses HTML, so queries answer
|
|
5
|
-
* emptily and `insertBefore`/`cloneNode` are deliberately absent — they need a tree, and faking them
|
|
6
|
-
* would misplace content silently. Everything else a real node exposes is either here or listed in
|
|
7
|
-
* `tests/dom-surface.mjs` as out of scope with a reason, and both directions are enforced: the code
|
|
8
|
-
* that runs on this DOM is *user* code, and for years every missing member was found by someone
|
|
9
|
-
* tripping over it.
|
|
10
|
-
*/
|
|
11
|
-
import { interfaceFor } from './reflections.js';
|
|
12
|
-
import { escapeHtml, escapeStyleText, RAW_TEXT_ELEMENTS, VOID_ELEMENTS } from './escaping.js';
|
|
13
|
-
import { parseFragment } from './parse.js';
|
|
14
|
-
import { addListener, removeListener, dispatch } from './events.js';
|
|
15
|
-
import * as select from './select.js';
|
|
16
|
-
import { datasetView, styleView, tokenListView } from './views.js';
|
|
17
|
-
import { StyleSheetShim } from './stylesheets.js';
|
|
18
|
-
import { registry } from './registry.js';
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The HTML namespace, which is what `createElement` produces and what almost everything here is.
|
|
22
|
-
*
|
|
23
|
-
* It is load-bearing rather than decorative: **an HTML element folds tag and attribute names to
|
|
24
|
-
* lower case, and an element in any other namespace does not.** `svg.setAttribute('viewBox', …)`
|
|
25
|
-
* has to keep its capital B or the viewport is ignored, while `div.setAttribute('Data-Flag', …)`
|
|
26
|
-
* has to lose its capitals or the `getAttribute('data-flag')` beside it reads `null`.
|
|
27
|
-
*/
|
|
28
|
-
const HTML_NS = 'http://www.w3.org/1999/xhtml';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The characters that make a name unusable, checked so the shim refuses what the platform refuses.
|
|
32
|
-
*
|
|
33
|
-
* **Deliberately the engines' rule and not jsdom's.** jsdom implements the XML `Name` production and
|
|
34
|
-
* rejects `a(b)`, `a|b`, `a?b` and about fifty other shapes that every real engine accepts —
|
|
35
|
-
* `tests/browser/spread-names.test.js` records that the engines refuse exactly `a b`, `a>b`, `a=b`
|
|
36
|
-
* and `a/b`, and `CLAUDE.md` records the false finding that came of trusting jsdom here. So this is
|
|
37
|
-
* whitespace, `>`, `=`, `/` and controls, plus the empty string: strictly the set that cannot survive
|
|
38
|
-
* being written into a tag, and nothing invented on top.
|
|
39
|
-
*/
|
|
40
|
-
const UNUSABLE_IN_A_NAME = /[\0-\x20/>=\x7f]/;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* A tag name is checked slightly harder, because a quote or a `<` inside one **does** break the
|
|
44
|
-
* markup where the same character inside an attribute name does not.
|
|
45
|
-
*/
|
|
46
|
-
const UNUSABLE_IN_A_TAG = /[\0-\x20"'<>/=\x7f]/;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Declarative shadow DOM can carry more than the mode, and the extras are **not recoverable on the
|
|
50
|
-
* client**: `attachShadow` reuses a declarative root and ignores the options it is handed, so a
|
|
51
|
-
* component asking for `delegatesFocus: true` over server-rendered markup that omitted it keeps
|
|
52
|
-
* `delegatesFocus === false` for the life of the page. Measured in Chromium. Focus delegation is an
|
|
53
|
-
* accessibility behaviour, so losing it silently under SSR is the kind of difference that never gets
|
|
54
|
-
* reported — it just works worse.
|
|
55
|
-
*
|
|
56
|
-
* `slotAssignment` has no declarative form at all; a component that needs it cannot be faithfully
|
|
57
|
-
* server-rendered, and the README says so rather than pretending.
|
|
58
|
-
*/
|
|
59
|
-
const SHADOW_ATTRIBUTES = [
|
|
60
|
-
['delegatesFocus', 'shadowrootdelegatesfocus'],
|
|
61
|
-
['clonable', 'shadowrootclonable'],
|
|
62
|
-
['serializable', 'shadowrootserializable'],
|
|
63
|
-
];
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* What an element and a shadow root both are: something that holds children as markup and can be
|
|
67
|
-
* appended to, queried and listened to.
|
|
68
|
-
*
|
|
69
|
-
* Shared because they kept drifting. `ShadowRootShim` was built for the renderer and `ElementShim`
|
|
70
|
-
* for core, so each was short of a different set of members — the router threw on
|
|
71
|
-
* `shadowRoot.addEventListener`, component code threw on `element.dataset`, and every gap was found
|
|
72
|
-
* by probing rather than by the two being one thing. Anything a container does belongs here.
|
|
73
|
-
*
|
|
74
|
-
* Queries answer emptily because this holds a **string**, not a tree; nothing in this package parses
|
|
75
|
-
* HTML. `insertBefore` and `cloneNode` are deliberately absent for the same reason — they need a
|
|
76
|
-
* tree, and faking them would misplace content silently.
|
|
77
|
-
*
|
|
78
|
-
* Events come from Node's own `EventTarget`, so `once`, `handleEvent`, `event.target`,
|
|
79
|
-
* `stopImmediatePropagation` and the return value of `dispatchEvent` are all correct without this
|
|
80
|
-
* package implementing any of them. They used to be no-ops that reported every event delivered, so
|
|
81
|
-
* anything a component decided from its own event — a control reading back its change, a callback
|
|
82
|
-
* dispatched at mount, a `preventDefault` deciding whether to proceed — took one branch here and
|
|
83
|
-
* the other in a browser. **Bubbling is still absent**: with children held as a string there is no
|
|
84
|
-
* ancestor chain to walk, so an event reaches its own element's listeners and stops.
|
|
85
|
-
*/
|
|
86
|
-
/**
|
|
87
|
-
* One entry's markup. A retained node serialises itself **at output time**, which is the entire
|
|
88
|
-
* point of keeping it: a mutation made after `appendChild` is still on the node when this runs.
|
|
89
|
-
* The expression is the one `appendChild` used to inline, unchanged, so the bytes are identical.
|
|
90
|
-
*/
|
|
91
|
-
export const serializeElement = (element) =>
|
|
92
|
-
VOID_ELEMENTS.has(element.localName)
|
|
93
|
-
? element.openTag()
|
|
94
|
-
: element.openTag() + element.innerHTML + (element._sourceCloseTag ?? `</${element.localName}>`);
|
|
95
|
-
|
|
96
|
-
const serializeEntry = (entry) =>
|
|
97
|
-
typeof entry === 'string'
|
|
98
|
-
? entry
|
|
99
|
-
: typeof entry?.markup === 'function'
|
|
100
|
-
? entry.markup()
|
|
101
|
-
: (entry?.innerHTML ?? '');
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* The attribute list, with the `NamedNodeMap` methods on it. A free function taking the element so
|
|
105
|
-
* nothing has to alias `this`, and one cast because the methods are added to an array.
|
|
106
|
-
*
|
|
107
|
-
* @param {any} element
|
|
108
|
-
*/
|
|
109
|
-
const namedNodeMap = (element) => {
|
|
110
|
-
const list = /** @type {any} */ (
|
|
111
|
-
[...element._attributes].map(([name, value]) => ({
|
|
112
|
-
name,
|
|
113
|
-
value,
|
|
114
|
-
localName: name,
|
|
115
|
-
namespaceURI: null,
|
|
116
|
-
ownerElement: element,
|
|
117
|
-
}))
|
|
118
|
-
);
|
|
119
|
-
list.getNamedItem = (name) => list.find((entry) => entry.name === `${name}`.toLowerCase()) ?? null;
|
|
120
|
-
list.getNamedItemNS = (_namespace, name) => list.getNamedItem(name);
|
|
121
|
-
list.item = (index) => list[index] ?? null;
|
|
122
|
-
list.setNamedItem = (attribute) => {
|
|
123
|
-
const previous = list.getNamedItem(attribute?.name);
|
|
124
|
-
element.setAttribute(attribute.name, attribute.value);
|
|
125
|
-
return previous;
|
|
126
|
-
};
|
|
127
|
-
list.removeNamedItem = (name) => {
|
|
128
|
-
const previous = list.getNamedItem(name);
|
|
129
|
-
if (!previous)
|
|
130
|
-
throw new DOMException(
|
|
131
|
-
`Failed to execute 'removeNamedItem' on 'NamedNodeMap': No item with name '${name}' was found.`,
|
|
132
|
-
'NotFoundError'
|
|
133
|
-
);
|
|
134
|
-
element.removeAttribute(name);
|
|
135
|
-
return previous;
|
|
136
|
-
};
|
|
137
|
-
return list;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/** A node this DOM keeps by reference, rather than inlining its markup — see `appendChild`. */
|
|
141
|
-
const isNode = (value) => typeof value?.markup === 'function';
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* The slot a node asks for. Text has no attributes, so it can only take the default slot.
|
|
145
|
-
* A free function so neither caller has to alias `this` — see `equalNodes` for the same reason.
|
|
146
|
-
*
|
|
147
|
-
* **`null` means NOT SLOTTABLE, and that is the platform's own line.** Only elements and text are
|
|
148
|
-
* slottables; a comment is never assigned to a slot (native `assignedNodes()` omits it, measured).
|
|
149
|
-
* This answered `''` for everything that was not an element, which put COMMENTS in the default
|
|
150
|
-
* slot's assignment — a server answering a question the client and the platform answer differently.
|
|
151
|
-
*
|
|
152
|
-
* **A deliberate twin of `slotNameOf` in `@verajs/renderer/slots`, byte-for-byte the same rule.**
|
|
153
|
-
* Independent packages (CODE-PRINCIPLES #6) and ssr must not import the renderer at runtime, so
|
|
154
|
-
* the rule is copied rather than shared — recorded here, as `@verajs/cms`'s `escapeHtml` records
|
|
155
|
-
* the same choice, so nobody "fixes" it into a dependency and so a change to one is known to need
|
|
156
|
-
* the other. `tests/ssr-slot-assignment-parity.test.mjs` fails if they ever disagree.
|
|
157
|
-
*
|
|
158
|
-
* @param {any} node
|
|
159
|
-
*/
|
|
160
|
-
const slotNameOf = (node) =>
|
|
161
|
-
node.nodeType === 3 ? '' : node.nodeType === 1 ? (node.getAttribute('slot') ?? '') : null;
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* **What a `<slot>` projects.** A slot shows the host's light-DOM children whose `slot` attribute
|
|
165
|
-
* names it — the default slot taking everything unnamed. That is answerable on a server: the host,
|
|
166
|
-
* its children and their names are all here. It answered nothing, so a component inspecting what it
|
|
167
|
-
* had been given found an empty list and rendered its fallback content, on the server only.
|
|
168
|
-
*
|
|
169
|
-
* `flatten` falls back to the slot's own children when nothing is assigned, which is what makes it
|
|
170
|
-
* the useful form: it answers "what will actually be shown here".
|
|
171
|
-
*
|
|
172
|
-
* @param {any} slot @param {{flatten?: boolean}} [options]
|
|
173
|
-
*/
|
|
174
|
-
const assignedTo = (slot, options) => {
|
|
175
|
-
const root = slot.getRootNode();
|
|
176
|
-
const host = root?._host;
|
|
177
|
-
if (!host) return options?.flatten ? nodesOf(slot) : [];
|
|
178
|
-
const name = slot.getAttribute('name') ?? '';
|
|
179
|
-
/** `slotNameOf` answers `null` for a non-slottable, which matches no name — comments out. */
|
|
180
|
-
const assigned = nodesOf(host).filter((node) => slotNameOf(node) === name);
|
|
181
|
-
return assigned.length || !options?.flatten ? assigned : nodesOf(slot);
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* **`before`, `after` and `replaceWith`** — the `ChildNode` trio, which every one of these classes
|
|
186
|
-
* needs and none can inherit from the other: text and comments extend `EventTarget` directly rather
|
|
187
|
-
* than the container. Written once here and delegated to, rather than three times over.
|
|
188
|
-
*
|
|
189
|
-
* A plain string becomes a text node, which is what the spec says and what makes
|
|
190
|
-
* `node.after('some text')` do the obvious thing instead of nothing.
|
|
191
|
-
*
|
|
192
|
-
* @param {any} node @param {Array<any>} inserted @param {'before' | 'after' | 'replace'} where
|
|
193
|
-
*/
|
|
194
|
-
const insertAround = (node, inserted, where) => {
|
|
195
|
-
const parent = node._parent;
|
|
196
|
-
if (!parent) return;
|
|
197
|
-
const reference = where === 'after' ? node.nextSibling : node;
|
|
198
|
-
for (const one of inserted)
|
|
199
|
-
parent.insertBefore(typeof one === 'string' ? new TextShim(one) : one, reference);
|
|
200
|
-
/**
|
|
201
|
-
* **Unless the node replaced itself.** `x.replaceWith(x)` inserted `x` back where it was and then
|
|
202
|
-
* removed it, so a node replaced by itself **disappeared** — measured against jsdom, which leaves
|
|
203
|
-
* it in place.
|
|
204
|
-
*
|
|
205
|
-
* Same root cause as the `after` case above: this helper treated `node` and `inserted` as disjoint,
|
|
206
|
-
* and every operation here allows them to overlap.
|
|
207
|
-
*/
|
|
208
|
-
if (where === 'replace' && !inserted.includes(node)) parent.removeChild(node);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* **Structural equality, as the spec defines it** — type, name, attributes as a set, and children
|
|
213
|
-
* pairwise. `isEqualNode` used to compare identity, which is what `isSameNode` is for, so two
|
|
214
|
-
* elements built identically reported themselves different.
|
|
215
|
-
*
|
|
216
|
-
* A free function rather than a method so neither node has to be `this`: a shadow root and a
|
|
217
|
-
* fragment are containers with no attributes and no `nodeName` of their own, and reaching for those
|
|
218
|
-
* through `this` needs a cast that the lint rules and the type-checker disagree about.
|
|
219
|
-
*
|
|
220
|
-
* @param {any} a @param {any} b
|
|
221
|
-
*/
|
|
222
|
-
const equalNodes = (a, b) => {
|
|
223
|
-
if (a === b) return true;
|
|
224
|
-
if (!b || b.nodeType !== a.nodeType || b.nodeName !== a.nodeName) return false;
|
|
225
|
-
const mine = a._attributes ?? new Map();
|
|
226
|
-
const theirs = b._attributes ?? new Map();
|
|
227
|
-
if (mine.size !== theirs.size) return false;
|
|
228
|
-
for (const [name, value] of mine) if (theirs.get(name) !== value) return false;
|
|
229
|
-
const ours = nodesOf(a);
|
|
230
|
-
const others = nodesOf(b);
|
|
231
|
-
if (ours.length !== others.length) return false;
|
|
232
|
-
return ours.every((child, index) => child.isEqualNode(others[index]));
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* **Walks the tree**, as `textContent` is defined to. It used to strip tags out of the serialised
|
|
237
|
-
* markup with a regular expression and undo the numeric escapes, which answered `a & b` for an
|
|
238
|
-
* element holding the text `a & b` — the entity spellings this package does not itself emit came
|
|
239
|
-
* back raw. A comment contributes nothing, which is what the platform says.
|
|
240
|
-
*
|
|
241
|
-
* A chunk of markup this DOM could not parse has no nodes to walk, so it keeps the old treatment.
|
|
242
|
-
*/
|
|
243
|
-
const textOf = (container) => {
|
|
244
|
-
parseChunks(container);
|
|
245
|
-
let out = '';
|
|
246
|
-
for (const entry of container._entries) {
|
|
247
|
-
if (typeof entry === 'string')
|
|
248
|
-
out += entry.replace(/<[^>]*>/gu, '').replace(/&#(\d+);/gu, (_, code) => String.fromCharCode(Number(code)));
|
|
249
|
-
else if (entry.nodeType === 8) continue;
|
|
250
|
-
else out += entry.textContent;
|
|
251
|
-
}
|
|
252
|
-
return out;
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
/** Warned once per container, so a component that queries in a loop says it once. */
|
|
256
|
-
const warnedAboutMarkup = /* @__PURE__ */ new WeakSet();
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* The retained nodes among the entries.
|
|
260
|
-
*
|
|
261
|
-
* **Markup supplied as a string is not nodes**, and this is where that shows. Until a parser
|
|
262
|
-
* arrives — a later, deliberately separate step — a container filled by `innerHTML`
|
|
263
|
-
* or by the `children:` option has markup and no node view, so asking for one answers emptily —
|
|
264
|
-
* which is a wrong answer, and says so rather than being discovered later as a defect.
|
|
265
|
-
*/
|
|
266
|
-
/**
|
|
267
|
-
* **Parse the markup chunks, but only keep the result if it reproduces them exactly.**
|
|
268
|
-
*
|
|
269
|
-
* The parse is discarded unless re-serialising it is byte-identical to the string it came from, so
|
|
270
|
-
* reading `children` can never change what the page renders — the worst case is the behaviour that
|
|
271
|
-
* was there before, plus a warning. That check is also what makes a parser defect cheap: a wrong
|
|
272
|
-
* tree that does not round-trip is thrown away rather than served.
|
|
273
|
-
*/
|
|
274
|
-
const parseChunks = (container) => {
|
|
275
|
-
if (container._parsed) return;
|
|
276
|
-
container._parsed = true;
|
|
277
|
-
const entries = [];
|
|
278
|
-
let gained = false;
|
|
279
|
-
for (const entry of container._entries) {
|
|
280
|
-
if (typeof entry !== 'string') {
|
|
281
|
-
entries.push(entry);
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
284
|
-
const parsed = parseFragment(entry, {
|
|
285
|
-
element: (name) => build(name),
|
|
286
|
-
text: (data) => new TextShim(data),
|
|
287
|
-
comment: (data) => new CommentShim(data),
|
|
288
|
-
});
|
|
289
|
-
let round = '';
|
|
290
|
-
if (parsed) for (const node of parsed) round += serializeEntry(node);
|
|
291
|
-
if (!parsed || round !== entry) {
|
|
292
|
-
entries.push(entry);
|
|
293
|
-
continue;
|
|
294
|
-
}
|
|
295
|
-
for (const node of parsed) if (typeof node !== 'string') node._parent = container;
|
|
296
|
-
entries.push(...parsed);
|
|
297
|
-
gained = true;
|
|
298
|
-
}
|
|
299
|
-
if (gained) container._entries = entries;
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* **A collection is a plain array here, and that costs `item()` unless it is given one.**
|
|
304
|
-
*
|
|
305
|
-
* `childNodes`, `children` and the query methods answer with arrays rather than live `NodeList`s and
|
|
306
|
-
* `HTMLCollection`s. That is deliberate and the reason is good — there is nothing to be live *over*
|
|
307
|
-
* while a render is a single pass, and an array is more useful to the caller.
|
|
308
|
-
*
|
|
309
|
-
* But liveness is not the only difference, and the rest was a side effect rather than a decision:
|
|
310
|
-
* `list.item(0)` is a plain method on both interfaces, `namedItem` on `HTMLCollection`, and an array
|
|
311
|
-
* has neither. `list.item(0)` threw `TypeError` — from ordinary code, and from any library written
|
|
312
|
-
* against the DOM rather than against arrays.
|
|
313
|
-
*
|
|
314
|
-
* The prototype is built once and shared. `item` answers `null` out of range, as the platform does,
|
|
315
|
-
* rather than `undefined`.
|
|
316
|
-
*/
|
|
317
|
-
const COLLECTION_PROTOTYPE = Object.assign(Object.create(Array.prototype), {
|
|
318
|
-
/** @this {any[]} @param {number} index */
|
|
319
|
-
item(index) {
|
|
320
|
-
return this[index] ?? null;
|
|
321
|
-
},
|
|
322
|
-
/**
|
|
323
|
-
* `HTMLCollection` only. **Document order, not `id` before `name`** — the spec says "the first
|
|
324
|
-
* element in the collection" for which *either* holds, so a `name="x"` earlier in the tree wins
|
|
325
|
-
* over an `id="x"` later.
|
|
326
|
-
*
|
|
327
|
-
* Written the other way round first, from memory, and caught by comparing against a real DOM
|
|
328
|
-
* instead of against the expectation. The two orders are indistinguishable unless one element
|
|
329
|
-
* carries the `name` and a *later* one carries the `id`, which is why the fixture has exactly that.
|
|
330
|
-
*
|
|
331
|
-
* @this {any[]} @param {any} name
|
|
332
|
-
*/
|
|
333
|
-
namedItem(name) {
|
|
334
|
-
const wanted = `${name}`;
|
|
335
|
-
return (
|
|
336
|
-
this.find(
|
|
337
|
-
(element) => element.getAttribute?.('id') === wanted || element.getAttribute?.('name') === wanted
|
|
338
|
-
) ?? null
|
|
339
|
-
);
|
|
340
|
-
},
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
/** @param {any[]} list */
|
|
344
|
-
const asCollection = (list) => {
|
|
345
|
-
Object.setPrototypeOf(list, COLLECTION_PROTOTYPE);
|
|
346
|
-
return list;
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
/** **Elements only** — what `children` and every element-wise accessor mean. */
|
|
350
|
-
const elementsOf = (container) => asCollection(nodesOf(container).filter((node) => node.openTag));
|
|
351
|
-
|
|
352
|
-
const nodesOf = (container) => {
|
|
353
|
-
parseChunks(container);
|
|
354
|
-
const nodes = container._entries.filter((entry) => typeof entry !== 'string');
|
|
355
|
-
/**
|
|
356
|
-
* **Not `!nodes.length`.** That guard silenced this in the case that needs it most: a container
|
|
357
|
-
* holding *both* parsed nodes and a declined chunk answered `children` with only the nodes, said
|
|
358
|
-
* nothing, and still emitted the declined markup into the output. Some content visible and some
|
|
359
|
-
* invisible is harder to diagnose than none visible, and it was the half with no warning.
|
|
360
|
-
*
|
|
361
|
-
* Dropping the guard is safe because a surviving string means exactly one thing. Measured: markup
|
|
362
|
-
* that parses becomes nodes, `append('text')` becomes a **text node**, and even
|
|
363
|
-
* `append('<p>x</b>')` becomes a text node — a string argument is text, not markup. The only entry
|
|
364
|
-
* still a string after `parseChunks` is a chunk the parser declined, so there is no legitimate
|
|
365
|
-
* content this can fire on.
|
|
366
|
-
*/
|
|
367
|
-
if (
|
|
368
|
-
!warnedAboutMarkup.has(container) &&
|
|
369
|
-
container._entries.some((entry) => typeof entry === 'string' && entry.trim())
|
|
370
|
-
) {
|
|
371
|
-
warnedAboutMarkup.add(container);
|
|
372
|
-
/**
|
|
373
|
-
* **The message has to describe the parser that exists.** It used to say "markup assigned as a
|
|
374
|
-
* string is not parsed on the server", which was true before `parse.js` and is now false for
|
|
375
|
-
* almost everything: nested elements, attributes, void elements, comments, an unclosed tag, a
|
|
376
|
-
* table fragment and raw text all parse. What reaches this line is the narrow case the parser
|
|
377
|
-
* *declined* — markup it cannot re-serialise byte-identically, so it keeps the string rather than
|
|
378
|
-
* hand back a tree the client would not build.
|
|
379
|
-
*
|
|
380
|
-
* The distinction changes the advice. "Not parsed" tells the author to rewrite working code with
|
|
381
|
-
* `createElement`; the truth is that this particular markup was refused, and making it
|
|
382
|
-
* well-formed is usually the fix.
|
|
383
|
-
*/
|
|
384
|
-
console.warn(
|
|
385
|
-
`[vera] ssr: some of this element's markup could not be parsed, so children/querySelector ` +
|
|
386
|
-
`do not see it — they answer emptily, or with only the part that did parse. This DOM parses ` +
|
|
387
|
-
`markup it can reproduce exactly and declines the rest, ` +
|
|
388
|
-
`rather than building a tree the browser would not — a mismatched or stray closing tag is ` +
|
|
389
|
-
`the usual cause. Make the markup well-formed, or build the children with ` +
|
|
390
|
-
`createElement/appendChild if a component needs to read them back.`
|
|
391
|
-
);
|
|
392
|
-
}
|
|
393
|
-
return nodes;
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* **Text and comments are nodes.**
|
|
398
|
-
*
|
|
399
|
-
* They were not: `createTextNode` returned an object literal with an `innerHTML` string, so it had
|
|
400
|
-
* no identity, no parent and no `nodeType`, and appending one inlined its markup and lost the node.
|
|
401
|
-
* `childNodes` therefore reported `1` for `text <b>bold</b> tail`, where every browser says `3`.
|
|
402
|
-
*
|
|
403
|
-
* Like an element, a parsed one keeps **the exact bytes it came from** so re-serialising reproduces
|
|
404
|
-
* the input — `&` stays `&` rather than becoming this package's `&` — and falls back to
|
|
405
|
-
* escaping its data the moment somebody writes to it.
|
|
406
|
-
*/
|
|
407
|
-
class CharacterDataShim extends EventTarget {
|
|
408
|
-
constructor(data) {
|
|
409
|
-
super();
|
|
410
|
-
this._data = `${data}`;
|
|
411
|
-
this._source = /** @type {string | null} */ (null);
|
|
412
|
-
this._parent = /** @type {any} */ (null);
|
|
413
|
-
this.isConnected = true;
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Registered here rather than on the platform's `EventTarget`; see `events.js`.
|
|
417
|
-
* @override
|
|
418
|
-
*/
|
|
419
|
-
addEventListener(type, callback, options) {
|
|
420
|
-
addListener(this, type, callback, options);
|
|
421
|
-
}
|
|
422
|
-
/** @override */
|
|
423
|
-
removeEventListener(type, callback, options) {
|
|
424
|
-
removeListener(this, type, callback, options);
|
|
425
|
-
}
|
|
426
|
-
/** @override */
|
|
427
|
-
dispatchEvent(event) {
|
|
428
|
-
return dispatch(this, event);
|
|
429
|
-
}
|
|
430
|
-
get data() {
|
|
431
|
-
return this._data;
|
|
432
|
-
}
|
|
433
|
-
set data(value) {
|
|
434
|
-
this._data = `${value}`;
|
|
435
|
-
this._source = null;
|
|
436
|
-
}
|
|
437
|
-
get nodeValue() {
|
|
438
|
-
return this._data;
|
|
439
|
-
}
|
|
440
|
-
set nodeValue(value) {
|
|
441
|
-
this.data = value;
|
|
442
|
-
}
|
|
443
|
-
get textContent() {
|
|
444
|
-
return this._data;
|
|
445
|
-
}
|
|
446
|
-
set textContent(value) {
|
|
447
|
-
this.data = value;
|
|
448
|
-
}
|
|
449
|
-
get length() {
|
|
450
|
-
return this._data.length;
|
|
451
|
-
}
|
|
452
|
-
get parentNode() {
|
|
453
|
-
return this._parent;
|
|
454
|
-
}
|
|
455
|
-
get parentElement() {
|
|
456
|
-
return this._parent?.openTag ? this._parent : null;
|
|
457
|
-
}
|
|
458
|
-
get childNodes() {
|
|
459
|
-
return [];
|
|
460
|
-
}
|
|
461
|
-
get firstChild() {
|
|
462
|
-
return null;
|
|
463
|
-
}
|
|
464
|
-
get lastChild() {
|
|
465
|
-
return null;
|
|
466
|
-
}
|
|
467
|
-
hasChildNodes() {
|
|
468
|
-
return false;
|
|
469
|
-
}
|
|
470
|
-
get ownerDocument() {
|
|
471
|
-
return globalThis.document ?? null;
|
|
472
|
-
}
|
|
473
|
-
get nextSibling() {
|
|
474
|
-
return this._siblingAt(1);
|
|
475
|
-
}
|
|
476
|
-
get previousSibling() {
|
|
477
|
-
return this._siblingAt(-1);
|
|
478
|
-
}
|
|
479
|
-
/** @param {number} step @param {(container: any) => Array<any>} view */
|
|
480
|
-
_siblingAt(step, view = nodesOf) {
|
|
481
|
-
if (!this._parent) return null;
|
|
482
|
-
const siblings = view(this._parent);
|
|
483
|
-
const index = siblings.indexOf(this);
|
|
484
|
-
return index === -1 ? null : (siblings[index + step] ?? null);
|
|
485
|
-
}
|
|
486
|
-
remove() {
|
|
487
|
-
this._parent?.removeChild(this);
|
|
488
|
-
}
|
|
489
|
-
before(...nodes) {
|
|
490
|
-
insertAround(this, nodes, 'before');
|
|
491
|
-
}
|
|
492
|
-
after(...nodes) {
|
|
493
|
-
insertAround(this, nodes, 'after');
|
|
494
|
-
}
|
|
495
|
-
replaceWith(...nodes) {
|
|
496
|
-
insertAround(this, nodes, 'replace');
|
|
497
|
-
}
|
|
498
|
-
isSameNode(node) {
|
|
499
|
-
return node === this;
|
|
500
|
-
}
|
|
501
|
-
isEqualNode(node) {
|
|
502
|
-
/** `nodeType` is the subclass's — this base is never instantiated on its own. */
|
|
503
|
-
return node?.nodeType === /** @type {any} */ (this).nodeType && node?.data === this._data;
|
|
504
|
-
}
|
|
505
|
-
contains(node) {
|
|
506
|
-
return node === this;
|
|
507
|
-
}
|
|
508
|
-
getRootNode() {
|
|
509
|
-
if (!this._parent) return this;
|
|
510
|
-
let root = this._parent;
|
|
511
|
-
while (root._parent) root = root._parent;
|
|
512
|
-
return root;
|
|
513
|
-
}
|
|
514
|
-
appendData(value) {
|
|
515
|
-
this.data = this._data + `${value}`;
|
|
516
|
-
}
|
|
517
|
-
substringData(offset, count) {
|
|
518
|
-
return this._data.substr(offset, count);
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
export class TextShim extends CharacterDataShim {
|
|
523
|
-
get nodeType() {
|
|
524
|
-
return 3;
|
|
525
|
-
}
|
|
526
|
-
get nodeName() {
|
|
527
|
-
return '#text';
|
|
528
|
-
}
|
|
529
|
-
get wholeText() {
|
|
530
|
-
return this._data;
|
|
531
|
-
}
|
|
532
|
-
markup() {
|
|
533
|
-
return this._source ?? escapeHtml(this._data);
|
|
534
|
-
}
|
|
535
|
-
cloneNode() {
|
|
536
|
-
const copy = new TextShim(this._data);
|
|
537
|
-
copy._source = this._source;
|
|
538
|
-
return copy;
|
|
539
|
-
}
|
|
540
|
-
/** Splits at `offset`, leaving this node with the first half and inserting the second after it. */
|
|
541
|
-
splitText(offset) {
|
|
542
|
-
const tail = new TextShim(this._data.slice(offset));
|
|
543
|
-
this.data = this._data.slice(0, offset);
|
|
544
|
-
if (this._parent) this._parent.insertBefore(tail, this.nextSibling);
|
|
545
|
-
return tail;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
export class CommentShim extends CharacterDataShim {
|
|
550
|
-
get nodeType() {
|
|
551
|
-
return 8;
|
|
552
|
-
}
|
|
553
|
-
get nodeName() {
|
|
554
|
-
return '#comment';
|
|
555
|
-
}
|
|
556
|
-
markup() {
|
|
557
|
-
return this._source ?? `<!--${this._data}-->`;
|
|
558
|
-
}
|
|
559
|
-
cloneNode() {
|
|
560
|
-
const copy = new CommentShim(this._data);
|
|
561
|
-
copy._source = this._source;
|
|
562
|
-
return copy;
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
export class ContainerShim extends EventTarget {
|
|
567
|
-
constructor() {
|
|
568
|
-
super();
|
|
569
|
-
/**
|
|
570
|
-
* **Children are entries, not one string.** Each entry is either a retained node or a chunk of
|
|
571
|
-
* raw markup. Serialisation happens when `innerHTML` is *read*, not when a child is appended —
|
|
572
|
-
* appending used to serialise immediately and drop the node, so `appendChild(kid)` followed by
|
|
573
|
-
* `kid.textContent = 'x'` rendered `<b></b>` and lost the text with no diagnostic.
|
|
574
|
-
*/
|
|
575
|
-
this._entries = [];
|
|
576
|
-
this._parent = null;
|
|
577
|
-
/** A server-rendered node is in the document being built, so it is connected. */
|
|
578
|
-
this.isConnected = true;
|
|
579
|
-
}
|
|
580
|
-
/**
|
|
581
|
-
* Registered here rather than on the platform's `EventTarget`; see `events.js`.
|
|
582
|
-
* @override
|
|
583
|
-
*/
|
|
584
|
-
addEventListener(type, callback, options) {
|
|
585
|
-
addListener(this, type, callback, options);
|
|
586
|
-
}
|
|
587
|
-
/** @override */
|
|
588
|
-
removeEventListener(type, callback, options) {
|
|
589
|
-
removeListener(this, type, callback, options);
|
|
590
|
-
}
|
|
591
|
-
/** @override */
|
|
592
|
-
dispatchEvent(event) {
|
|
593
|
-
return dispatch(this, event);
|
|
594
|
-
}
|
|
595
|
-
get innerHTML() {
|
|
596
|
-
/**
|
|
597
|
-
* A void element's children exist in the DOM but never in its serialization — the HTML
|
|
598
|
-
* serialization algorithm skips them, so engines answer `""` here even with children attached
|
|
599
|
-
* (measured, Chromium and jsdom agreeing; `childNodes` still reports them, and `textContent`
|
|
600
|
-
* still reads their text, because those are DOM questions, not serialization ones).
|
|
601
|
-
* `serializeElement` already knew this for `outerHTML`; the getter did not, so a component
|
|
602
|
-
* reading its own `innerHTML` got a different answer server-side. `localName` is undefined on
|
|
603
|
-
* a fragment or shadow root, so the guard cannot fire off an element.
|
|
604
|
-
*/
|
|
605
|
-
if (VOID_ELEMENTS.has(/** @type {{ localName?: string }} */ (this).localName ?? '')) return '';
|
|
606
|
-
let out = '';
|
|
607
|
-
for (const entry of this._entries) out += serializeEntry(entry);
|
|
608
|
-
return out;
|
|
609
|
-
}
|
|
610
|
-
set innerHTML(markup) {
|
|
611
|
-
for (const entry of this._entries) if (typeof entry !== 'string') entry._parent = null;
|
|
612
|
-
/**
|
|
613
|
-
* **`[LegacyNullToEmptyString]`** — `innerHTML` stores `''` for `null`, not the word `"null"`.
|
|
614
|
-
* Same rule as `input.value` and `textarea.value` in `reflections.js`, and the same rule the
|
|
615
|
-
* README already records as fixed for `textContent`; it was fixed there for the member rather
|
|
616
|
-
* than for the class, so the rest of the class stayed wrong. `undefined` is not special-cased —
|
|
617
|
-
* the platform stringifies it.
|
|
618
|
-
*/
|
|
619
|
-
const text = markup === null ? '' : `${markup}`;
|
|
620
|
-
this._entries = text === '' ? [] : [text];
|
|
621
|
-
/** New markup has not been looked at yet, whatever was true of the markup it replaced. */
|
|
622
|
-
this._parsed = false;
|
|
623
|
-
}
|
|
624
|
-
appendChild(node) {
|
|
625
|
-
/**
|
|
626
|
-
* **Not a node is a `TypeError`, as it is in a browser.** `appendChild(null)` was a silent no-op
|
|
627
|
-
* here and throws in every engine, so `this.appendChild(maybeMissing)` — ordinary code —
|
|
628
|
-
* rendered a server page with the child quietly absent and then crashed the client.
|
|
629
|
-
*/
|
|
630
|
-
if (node === null || typeof node !== 'object')
|
|
631
|
-
throw new TypeError(
|
|
632
|
-
`Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.`
|
|
633
|
-
);
|
|
634
|
-
/**
|
|
635
|
-
* A registered component that has not rendered is marked, so the scan over this markup renders
|
|
636
|
-
* this instance instead of a new one built from the tag it wrote. See `pendingInstances`.
|
|
637
|
-
*/
|
|
638
|
-
if (node?.openTag && registry.has(node.localName) && !node._rendered) markPending(node);
|
|
639
|
-
/**
|
|
640
|
-
* **A node cannot contain itself.** Retaining nodes makes this reachable where inlining markup
|
|
641
|
-
* never could: appending an ancestor into its own descendant would recurse forever the next
|
|
642
|
-
* time anything read `innerHTML`. Every engine throws `HierarchyRequestError`.
|
|
643
|
-
*/
|
|
644
|
-
let contains = node === this;
|
|
645
|
-
for (let above = this._parent; above && !contains; above = above._parent)
|
|
646
|
-
if (above === node) contains = true;
|
|
647
|
-
if (contains)
|
|
648
|
-
throw new DOMException(
|
|
649
|
-
`Failed to execute 'appendChild' on 'Node': The new child element contains the parent.`,
|
|
650
|
-
'HierarchyRequestError'
|
|
651
|
-
);
|
|
652
|
-
/**
|
|
653
|
-
* A node with no tag of its own — a fragment — contributes its markup exactly as it did before.
|
|
654
|
-
* Moving a fragment's children into this parent is the platform's behaviour and is deliberately
|
|
655
|
-
* *not* step 1: it changes what `appendChild(fragment)` leaves behind.
|
|
656
|
-
*/
|
|
657
|
-
/**
|
|
658
|
-
* **A fragment hands over its children and is left empty**, which is what a browser does and the
|
|
659
|
-
* whole point of the type. Its markup used to be inlined instead, so the fragment still reported
|
|
660
|
-
* the children it had supposedly given away.
|
|
661
|
-
*/
|
|
662
|
-
if (node?.nodeType === 11) {
|
|
663
|
-
for (const entry of node._entries) {
|
|
664
|
-
if (isNode(entry)) entry._parent = this;
|
|
665
|
-
this._entries.push(entry);
|
|
666
|
-
}
|
|
667
|
-
node._entries = [];
|
|
668
|
-
return node;
|
|
669
|
-
}
|
|
670
|
-
if (!isNode(node)) {
|
|
671
|
-
this._entries.push(node?.innerHTML ?? '');
|
|
672
|
-
return node;
|
|
673
|
-
}
|
|
674
|
-
node._parent?._detach(node);
|
|
675
|
-
this._entries.push(node);
|
|
676
|
-
node._parent = this;
|
|
677
|
-
return node;
|
|
678
|
-
}
|
|
679
|
-
/**
|
|
680
|
-
* **Position a node among the children**, which needed a tree and now has one. `reference` of
|
|
681
|
-
* `null` appends, as the platform says, and a reference that is not a child is a `NotFoundError`
|
|
682
|
-
* rather than a silent append — the difference between "put it at the end" and "you asked about
|
|
683
|
-
* a node I do not have" is exactly the kind of thing a server should not paper over.
|
|
684
|
-
*/
|
|
685
|
-
insertBefore(node, reference) {
|
|
686
|
-
if (node === null || typeof node !== 'object')
|
|
687
|
-
throw new TypeError(
|
|
688
|
-
`Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'.`
|
|
689
|
-
);
|
|
690
|
-
if (reference === null || reference === undefined) return this.appendChild(node);
|
|
691
|
-
if (this._entries.indexOf(reference) === -1)
|
|
692
|
-
throw new DOMException(
|
|
693
|
-
`Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be ` +
|
|
694
|
-
`inserted is not a child of this node.`,
|
|
695
|
-
'NotFoundError'
|
|
696
|
-
);
|
|
697
|
-
let contains = node === this;
|
|
698
|
-
for (let above = this._parent; above && !contains; above = above._parent)
|
|
699
|
-
if (above === node) contains = true;
|
|
700
|
-
if (contains)
|
|
701
|
-
throw new DOMException(
|
|
702
|
-
`Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.`,
|
|
703
|
-
'HierarchyRequestError'
|
|
704
|
-
);
|
|
705
|
-
/**
|
|
706
|
-
* **Inserting a node before itself is a no-op, and it was not.** The spec's pre-insert step 3 is
|
|
707
|
-
* "if referenceChild is node, then set referenceChild to node's next sibling", and without it the
|
|
708
|
-
* detach below removes `node` and the `indexOf(reference)` that follows returns `-1` — so
|
|
709
|
-
* `splice(-1, 0, node)` puts it back **before the last child** instead of where it was.
|
|
710
|
-
*
|
|
711
|
-
* Reached in practice through `after()`: `insertAround` computes its reference once, up front, so
|
|
712
|
-
* `a.after(b)` where `b` is already `a`'s next sibling becomes exactly `insertBefore(b, b)`.
|
|
713
|
-
* Measured against jsdom, with `n` children the node landed at index `n - 2` every time — two
|
|
714
|
-
* children put it *first*, five put it second-to-last, and **three put it back where it belonged**,
|
|
715
|
-
* which is the size a hand-written test uses.
|
|
716
|
-
*
|
|
717
|
-
* Found by fuzzing sequences of tree mutations rather than operations one at a time; every
|
|
718
|
-
* individual member here already agreed with jsdom.
|
|
719
|
-
*/
|
|
720
|
-
if (reference === node) {
|
|
721
|
-
reference = node.nextSibling;
|
|
722
|
-
if (reference === null) return this.appendChild(node);
|
|
723
|
-
}
|
|
724
|
-
if (!isNode(node)) {
|
|
725
|
-
this._entries.splice(this._entries.indexOf(reference), 0, node?.innerHTML ?? '');
|
|
726
|
-
return node;
|
|
727
|
-
}
|
|
728
|
-
node._parent?._detach(node);
|
|
729
|
-
/** Re-found after the detach: moving a node forwards within one parent shifts the index. */
|
|
730
|
-
this._entries.splice(this._entries.indexOf(reference), 0, node);
|
|
731
|
-
node._parent = this;
|
|
732
|
-
return node;
|
|
733
|
-
}
|
|
734
|
-
/**
|
|
735
|
-
* **`moveBefore` is `insertBefore` with a precondition**: the node must already have a parent.
|
|
736
|
-
* Measured on Chromium and Firefox, which agree on all of it (WebKit does not implement it yet):
|
|
737
|
-
* an unparented node is a `HierarchyRequestError`, and a *parented* node with a reference that is
|
|
738
|
-
* not a child here is a `NotFoundError` — a different error from the same call depending on which
|
|
739
|
-
* argument is wrong, and checked in that order.
|
|
740
|
-
*
|
|
741
|
-
* The point of the API in a browser is moving a node without tearing down its state. There is no
|
|
742
|
-
* state to preserve on a server, so this is a move; having it means code written for the browser
|
|
743
|
-
* runs here rather than hitting a missing method.
|
|
744
|
-
*/
|
|
745
|
-
moveBefore(node, reference) {
|
|
746
|
-
if (node === null || typeof node !== 'object')
|
|
747
|
-
throw new TypeError(`Failed to execute 'moveBefore' on 'Node': parameter 1 is not of type 'Node'.`);
|
|
748
|
-
if (!node._parent)
|
|
749
|
-
throw new DOMException(
|
|
750
|
-
`Failed to execute 'moveBefore' on 'Node': The node to be moved has no parent.`,
|
|
751
|
-
'HierarchyRequestError'
|
|
752
|
-
);
|
|
753
|
-
if (reference !== null && reference !== undefined && this._entries.indexOf(reference) === -1)
|
|
754
|
-
throw new DOMException(
|
|
755
|
-
`Failed to execute 'moveBefore' on 'Node': The node before which the new node is to be ` +
|
|
756
|
-
`inserted is not a child of this node.`,
|
|
757
|
-
'NotFoundError'
|
|
758
|
-
);
|
|
759
|
-
return this.insertBefore(node, reference ?? null);
|
|
760
|
-
}
|
|
761
|
-
replaceChild(node, old) {
|
|
762
|
-
if (node === null || typeof node !== 'object')
|
|
763
|
-
throw new TypeError(
|
|
764
|
-
`Failed to execute 'replaceChild' on 'Node': parameter 1 is not of type 'Node'.`
|
|
765
|
-
);
|
|
766
|
-
if (this._entries.indexOf(old) === -1)
|
|
767
|
-
throw new DOMException(
|
|
768
|
-
`Failed to execute 'replaceChild' on 'Node': The node to be replaced is not a child of this node.`,
|
|
769
|
-
'NotFoundError'
|
|
770
|
-
);
|
|
771
|
-
/**
|
|
772
|
-
* **The one insertion path that had no ancestor check.** `prepend`, `append`, `appendChild`,
|
|
773
|
-
* `before` and `replaceWith` all route through `insertBefore` and inherit its guard; this does
|
|
774
|
-
* not, so `root.replaceChild(root, child)` was accepted and built a tree containing itself.
|
|
775
|
-
*
|
|
776
|
-
* A cycle in the tree is worse than a misordered one: it is not a wrong answer, it is a structure
|
|
777
|
-
* nothing downstream can walk. The nesting cap in `index.js` would eventually refuse the render,
|
|
778
|
-
* naming recursion rather than the call that caused it.
|
|
779
|
-
*
|
|
780
|
-
* Found by fuzzing mutation *sequences* against jsdom — `ssr-tree-operations` covers this method
|
|
781
|
-
* and could not see it, because the case needs a node and its own ancestor in one call.
|
|
782
|
-
*/
|
|
783
|
-
let contains = node === this;
|
|
784
|
-
for (let above = this._parent; above && !contains; above = above._parent) if (above === node) contains = true;
|
|
785
|
-
if (contains)
|
|
786
|
-
throw new DOMException(
|
|
787
|
-
`Failed to execute 'replaceChild' on 'Node': The new child element contains the parent.`,
|
|
788
|
-
'HierarchyRequestError'
|
|
789
|
-
);
|
|
790
|
-
/**
|
|
791
|
-
* **Where `old` sits, captured before anything moves.** Detaching `node` can remove `old` from
|
|
792
|
-
* this list — they are allowed to be the same node — and the `indexOf(old)` that followed then
|
|
793
|
-
* returned `-1`, so `splice(-1, 1, …)` **replaced the last child instead**: `x.replaceChild(x, x)`
|
|
794
|
-
* moved `x` to the end and destroyed whatever had been there.
|
|
795
|
-
*
|
|
796
|
-
* The spec makes replacing a node with itself a no-op, by the same step `insertBefore` needed —
|
|
797
|
-
* "if referenceChild is node, then set referenceChild to node's next sibling". Third instance of
|
|
798
|
-
* one root cause: this file treated a node and its own argument as necessarily different, and
|
|
799
|
-
* every one of these operations allows them to be the same.
|
|
800
|
-
*/
|
|
801
|
-
const at = this._entries.indexOf(old);
|
|
802
|
-
node._parent?._detach(node);
|
|
803
|
-
/** Re-found after the detach: moving a node forwards within one parent shifts the index. */
|
|
804
|
-
const index = this._entries.indexOf(old);
|
|
805
|
-
const value = isNode(node) ? node : (node?.innerHTML ?? '');
|
|
806
|
-
if (index === -1) this._entries.splice(at, 0, value);
|
|
807
|
-
else this._entries.splice(index, 1, value);
|
|
808
|
-
if (isNode(node)) node._parent = this;
|
|
809
|
-
if (old !== node) old._parent = null;
|
|
810
|
-
return old;
|
|
811
|
-
}
|
|
812
|
-
/**
|
|
813
|
-
* **Where each node sits relative to another**, as the spec's bit field. Two nodes with no common
|
|
814
|
-
* root are `DISCONNECTED | IMPLEMENTATION_SPECIFIC` plus a direction the spec leaves to the
|
|
815
|
-
* implementation, so only the disconnected bit is worth comparing across implementations.
|
|
816
|
-
*/
|
|
817
|
-
compareDocumentPosition(other) {
|
|
818
|
-
if (other === this) return 0;
|
|
819
|
-
if (this.contains(other)) return 16 + 4;
|
|
820
|
-
if (other.contains?.(this)) return 8 + 2;
|
|
821
|
-
const chain = (node) => {
|
|
822
|
-
const out = [];
|
|
823
|
-
for (let current = node; current; current = current._parent) out.unshift(current);
|
|
824
|
-
return out;
|
|
825
|
-
};
|
|
826
|
-
const mine = chain(this);
|
|
827
|
-
const theirs = chain(other);
|
|
828
|
-
if (mine[0] !== theirs[0]) return 1 + 32 + 2;
|
|
829
|
-
let depth = 0;
|
|
830
|
-
while (mine[depth] === theirs[depth]) depth++;
|
|
831
|
-
const siblings = nodesOf(mine[depth - 1]);
|
|
832
|
-
return siblings.indexOf(mine[depth]) < siblings.indexOf(theirs[depth]) ? 4 : 2;
|
|
833
|
-
}
|
|
834
|
-
/** Remove a node from this container's entries without touching its own parent pointer. */
|
|
835
|
-
_detach(node) {
|
|
836
|
-
const index = this._entries.indexOf(node);
|
|
837
|
-
if (index !== -1) this._entries.splice(index, 1);
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* **Absent until nodes were retained**, so `host.removeChild(kid)` was a `TypeError` — there was
|
|
841
|
-
* nothing to remove, the child having been flattened into a string at append time.
|
|
842
|
-
*/
|
|
843
|
-
removeChild(node) {
|
|
844
|
-
const index = this._entries.indexOf(node);
|
|
845
|
-
if (index === -1)
|
|
846
|
-
throw new DOMException(
|
|
847
|
-
`Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.`,
|
|
848
|
-
'NotFoundError'
|
|
849
|
-
);
|
|
850
|
-
this._entries.splice(index, 1);
|
|
851
|
-
node._parent = null;
|
|
852
|
-
return node;
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
/**
|
|
856
|
-
* What a node with no tree above or below it answers — shared, because an element and a shadow
|
|
857
|
-
* root answer these identically and two copies is how they came to disagree in the first place.
|
|
858
|
-
*
|
|
859
|
-
* Every one is the truthful answer rather than a placeholder: a detached node has no parent, no
|
|
860
|
-
* siblings and no box, and a browser returns exactly these. They are here because their
|
|
861
|
-
* *absence* was a `TypeError` that took the whole render down — `this.parentElement && …` is
|
|
862
|
-
* ordinary defensive code and it crashed.
|
|
863
|
-
*/
|
|
864
|
-
get parentNode() {
|
|
865
|
-
return this._parent;
|
|
866
|
-
}
|
|
867
|
-
/** `null` when the parent is not an element — a shadow root is a parent and not an element. */
|
|
868
|
-
get parentElement() {
|
|
869
|
-
return this._parent?.openTag ? this._parent : null;
|
|
870
|
-
}
|
|
871
|
-
get firstChild() {
|
|
872
|
-
return nodesOf(this)[0] ?? null;
|
|
873
|
-
}
|
|
874
|
-
get lastChild() {
|
|
875
|
-
return nodesOf(this).at(-1) ?? null;
|
|
876
|
-
}
|
|
877
|
-
get lastElementChild() {
|
|
878
|
-
return elementsOf(this).at(-1) ?? null;
|
|
879
|
-
}
|
|
880
|
-
/**
|
|
881
|
-
* **The siblings are real now.** These answered `null` unconditionally, which was truthful while a
|
|
882
|
-
* child was flattened into its parent's markup and became a lie the moment nodes were retained —
|
|
883
|
-
* `element.nextSibling` on the middle of three children reported nothing at all. There are no text
|
|
884
|
-
* nodes in this DOM, so the element-wise and node-wise spellings answer the same list.
|
|
885
|
-
*/
|
|
886
|
-
get nextSibling() {
|
|
887
|
-
return this._siblingAt(1);
|
|
888
|
-
}
|
|
889
|
-
get previousSibling() {
|
|
890
|
-
return this._siblingAt(-1);
|
|
891
|
-
}
|
|
892
|
-
get nextElementSibling() {
|
|
893
|
-
return this._siblingAt(1, elementsOf);
|
|
894
|
-
}
|
|
895
|
-
get previousElementSibling() {
|
|
896
|
-
return this._siblingAt(-1, elementsOf);
|
|
897
|
-
}
|
|
898
|
-
/** @param {number} step @param {(container: any) => Array<any>} view */
|
|
899
|
-
_siblingAt(step, view = nodesOf) {
|
|
900
|
-
if (!this._parent) return null;
|
|
901
|
-
const siblings = view(this._parent);
|
|
902
|
-
const index = siblings.indexOf(this);
|
|
903
|
-
return index === -1 ? null : (siblings[index + step] ?? null);
|
|
904
|
-
}
|
|
905
|
-
/**
|
|
906
|
-
* **The slot this node is projected into**, which needs the host's shadow root and the node's own
|
|
907
|
-
* `slot` name — both of which are here. It answered `null` for every node, so a component asking
|
|
908
|
-
* where its light DOM had landed was told "nowhere".
|
|
909
|
-
*/
|
|
910
|
-
get assignedSlot() {
|
|
911
|
-
const root = this._parent?._shadowRoot;
|
|
912
|
-
if (!root) return null;
|
|
913
|
-
const name = slotNameOf(this);
|
|
914
|
-
return (
|
|
915
|
-
root
|
|
916
|
-
.querySelectorAll('slot')
|
|
917
|
-
.find((slot) => (slot.getAttribute('name') ?? '') === name) ?? null
|
|
918
|
-
);
|
|
919
|
-
}
|
|
920
|
-
get offsetParent() {
|
|
921
|
-
return null;
|
|
922
|
-
}
|
|
923
|
-
get childElementCount() {
|
|
924
|
-
return elementsOf(this).length;
|
|
925
|
-
}
|
|
926
|
-
hasChildNodes() {
|
|
927
|
-
return nodesOf(this).length > 0;
|
|
928
|
-
}
|
|
929
|
-
get nodeValue() {
|
|
930
|
-
return null;
|
|
931
|
-
}
|
|
932
|
-
get baseURI() {
|
|
933
|
-
return globalThis.location?.href ?? '';
|
|
934
|
-
}
|
|
935
|
-
get namespaceURI() {
|
|
936
|
-
return HTML_NS;
|
|
937
|
-
}
|
|
938
|
-
get prefix() {
|
|
939
|
-
return null;
|
|
940
|
-
}
|
|
941
|
-
get ownerDocument() {
|
|
942
|
-
return globalThis.document;
|
|
943
|
-
}
|
|
944
|
-
/**
|
|
945
|
-
* **Walks the parent chain**, as the platform does. It compared identity only, so
|
|
946
|
-
* `host.contains(child)` was `false` for a child the host plainly held — ordinary defensive code
|
|
947
|
-
* reading as "this is not mine".
|
|
948
|
-
*/
|
|
949
|
-
contains(node) {
|
|
950
|
-
for (let current = node; current; current = current._parent) if (current === this) return true;
|
|
951
|
-
return false;
|
|
952
|
-
}
|
|
953
|
-
/** The `ChildNode` trio, shared with text and comments through `insertAround`. */
|
|
954
|
-
before(...nodes) {
|
|
955
|
-
insertAround(this, nodes, 'before');
|
|
956
|
-
}
|
|
957
|
-
after(...nodes) {
|
|
958
|
-
insertAround(this, nodes, 'after');
|
|
959
|
-
}
|
|
960
|
-
replaceWith(...nodes) {
|
|
961
|
-
insertAround(this, nodes, 'replace');
|
|
962
|
-
}
|
|
963
|
-
isSameNode(node) {
|
|
964
|
-
return node === this;
|
|
965
|
-
}
|
|
966
|
-
/**
|
|
967
|
-
* **Structural equality, not identity** — that is what `isSameNode` is for, and this answered the
|
|
968
|
-
* same thing, so two elements built identically reported themselves different. The spec compares
|
|
969
|
-
* type, name, attributes as a set, and children pairwise.
|
|
970
|
-
*/
|
|
971
|
-
isEqualNode(node) {
|
|
972
|
-
return equalNodes(this, node);
|
|
973
|
-
}
|
|
974
|
-
/**
|
|
975
|
-
* **Merges adjacent text and drops the empty**, which is what it is for. It was a no-op because
|
|
976
|
-
* there were no text nodes to merge — appending two of them left two entries a browser would have
|
|
977
|
-
* joined into one, so `childNodes.length` disagreed after the most ordinary DOM building there is.
|
|
978
|
-
*/
|
|
979
|
-
normalize() {
|
|
980
|
-
parseChunks(this);
|
|
981
|
-
const merged = [];
|
|
982
|
-
for (const entry of this._entries) {
|
|
983
|
-
const previous = merged[merged.length - 1];
|
|
984
|
-
if (entry?.nodeType === 3 && entry.data === '') continue;
|
|
985
|
-
if (entry?.nodeType === 3 && previous?.nodeType === 3) {
|
|
986
|
-
previous.data += entry.data;
|
|
987
|
-
entry._parent = null;
|
|
988
|
-
continue;
|
|
989
|
-
}
|
|
990
|
-
merged.push(entry);
|
|
991
|
-
}
|
|
992
|
-
this._entries = merged;
|
|
993
|
-
for (const entry of merged) if (isNode(entry) && entry.nodeType === 1) entry.normalize();
|
|
994
|
-
}
|
|
995
|
-
/**
|
|
996
|
-
* The furthest ancestor, which is the shadow root for anything a component rendered into one —
|
|
997
|
-
* unless `composed` is asked for, which keeps going out through the root's host as a browser does.
|
|
998
|
-
*/
|
|
999
|
-
getRootNode(options) {
|
|
1000
|
-
if (!this._parent) return this;
|
|
1001
|
-
let root = this._parent;
|
|
1002
|
-
for (;;) {
|
|
1003
|
-
while (root._parent) root = root._parent;
|
|
1004
|
-
if (!options?.composed || !root._host) return root;
|
|
1005
|
-
root = root._host;
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
/** Needs layout, which a string does not have; a browser with no box returns nothing either. */
|
|
1009
|
-
elementFromPoint() {
|
|
1010
|
-
return null;
|
|
1011
|
-
}
|
|
1012
|
-
elementsFromPoint() {
|
|
1013
|
-
return [];
|
|
1014
|
-
}
|
|
1015
|
-
getSelection() {
|
|
1016
|
-
return null;
|
|
1017
|
-
}
|
|
1018
|
-
/** `append` takes several nodes, and strings as text — the modern spelling of `appendChild`. */
|
|
1019
|
-
/**
|
|
1020
|
-
* A string is markup to escape and keep as a chunk. It used to go through `innerHTML +=`, which
|
|
1021
|
-
* now reads every retained node back into text and writes it as one chunk — flattening the tree
|
|
1022
|
-
* this class exists to keep. Pushing the chunk is the same bytes and keeps the nodes.
|
|
1023
|
-
*/
|
|
1024
|
-
append(...nodes) {
|
|
1025
|
-
for (const node of nodes) {
|
|
1026
|
-
if (typeof node === 'string') {
|
|
1027
|
-
this._entries.push(escapeHtml(node));
|
|
1028
|
-
this._parsed = false;
|
|
1029
|
-
}
|
|
1030
|
-
else this.appendChild(node);
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
replaceChildren(...nodes) {
|
|
1034
|
-
this.innerHTML = '';
|
|
1035
|
-
this.append(...nodes);
|
|
1036
|
-
}
|
|
1037
|
-
/** Splices the entries rather than concatenating markup, for the reason `append` gives. */
|
|
1038
|
-
/**
|
|
1039
|
-
* **The restore has to survive a throw.** `append` can reject a node — an ancestor of `this` is a
|
|
1040
|
-
* `HierarchyRequestError` — and the children were already moved aside by then, so the final
|
|
1041
|
-
* `push` never ran and **every existing child was destroyed by a call that failed**.
|
|
1042
|
-
*
|
|
1043
|
-
* Measured against jsdom: `child.prepend(root)` throws on both, and afterwards jsdom still holds
|
|
1044
|
-
* the child's text while this held nothing. A failed operation that mutates is worse than one that
|
|
1045
|
-
* does the wrong thing, because the caller has already handled the error and moved on.
|
|
1046
|
-
*
|
|
1047
|
-
* `finally`, not a validity pre-pass: matching the spec exactly would mean checking every argument
|
|
1048
|
-
* before inserting any, so that a second bad node undoes a first good one. That difference is
|
|
1049
|
-
* narrower than this one and is left alone deliberately — what is fixed here is the destruction of
|
|
1050
|
-
* children that were never arguments.
|
|
1051
|
-
*
|
|
1052
|
-
* Found by fuzzing sequences of tree mutations against jsdom; the failing sequence ended
|
|
1053
|
-
* `prepend(n1, root)` and the loss showed up as a missing text node four operations later.
|
|
1054
|
-
*/
|
|
1055
|
-
prepend(...nodes) {
|
|
1056
|
-
const existing = this._entries;
|
|
1057
|
-
this._entries = [];
|
|
1058
|
-
try {
|
|
1059
|
-
this.append(...nodes);
|
|
1060
|
-
} finally {
|
|
1061
|
-
/**
|
|
1062
|
-
* **Minus the arguments themselves.** Prepending a node that is ALREADY a child of this
|
|
1063
|
-
* element is a move-to-front, and `append`'s detach cannot see it: the child's entry sits
|
|
1064
|
-
* in this local array while `_entries` is swapped empty, so nothing was removed and pushing
|
|
1065
|
-
* `existing` back restored a second copy — `prepend(b)` on `[a,b,c]` answered `[b,a,b,c]`
|
|
1066
|
-
* where every engine answers `[b,a,c]`. Found by the virgin-seed tree fuzz (three sequences,
|
|
1067
|
-
* one signature, all ending in a same-parent prepend); every other move op detaches through
|
|
1068
|
-
* the live `_entries` and was already right. Strings are always kept — they are text
|
|
1069
|
-
* entries, never arguments' identities.
|
|
1070
|
-
*/
|
|
1071
|
-
for (const entry of existing)
|
|
1072
|
-
if (typeof entry === 'string' || !nodes.includes(entry)) this._entries.push(entry);
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
/**
|
|
1076
|
-
* **Answers from the node view**, which markup assigned as a string now has. Every query used to
|
|
1077
|
-
* return nothing whatever was asked, so a component branched the wrong way with nothing said.
|
|
1078
|
-
* A selector this DOM cannot answer honestly throws rather than answering `null` — see
|
|
1079
|
-
* `select.js`.
|
|
1080
|
-
*/
|
|
1081
|
-
querySelector(selector) {
|
|
1082
|
-
parseChunks(this);
|
|
1083
|
-
return select.querySelector(this, selector);
|
|
1084
|
-
}
|
|
1085
|
-
querySelectorAll(selector) {
|
|
1086
|
-
parseChunks(this);
|
|
1087
|
-
return asCollection(select.querySelectorAll(this, selector));
|
|
1088
|
-
}
|
|
1089
|
-
getElementById(id) {
|
|
1090
|
-
parseChunks(this);
|
|
1091
|
-
return select.querySelector(this, `[id="${`${id}`.replace(/"/gu, '\\"')}"]`);
|
|
1092
|
-
}
|
|
1093
|
-
/**
|
|
1094
|
-
* **Only retained nodes.** Every entry this DOM holds as an element *is* an element, so `children`
|
|
1095
|
-
* and `childNodes` answer the same list — there are no text nodes to separate them, and markup
|
|
1096
|
-
* kept as a string is not nodes at all (`nodesOf` says so).
|
|
1097
|
-
*/
|
|
1098
|
-
get children() {
|
|
1099
|
-
return elementsOf(this);
|
|
1100
|
-
}
|
|
1101
|
-
get childNodes() {
|
|
1102
|
-
return asCollection(nodesOf(this));
|
|
1103
|
-
}
|
|
1104
|
-
get firstElementChild() {
|
|
1105
|
-
return elementsOf(this)[0] ?? null;
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
/**
|
|
1110
|
-
* A fragment is a container and nothing more.
|
|
1111
|
-
*
|
|
1112
|
-
* Its own class rather than a reused `ElementShim`, so `instanceof DocumentFragment` answers `false`
|
|
1113
|
-
* for an element — a check the renderer makes, and one that a shared class would have made true for
|
|
1114
|
-
* everything.
|
|
1115
|
-
*/
|
|
1116
|
-
export class FragmentShim extends ContainerShim {
|
|
1117
|
-
get nodeType() {
|
|
1118
|
-
return 11;
|
|
1119
|
-
}
|
|
1120
|
-
get nodeName() {
|
|
1121
|
-
return '#document-fragment';
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
export class ShadowRootShim extends ContainerShim {
|
|
1126
|
-
constructor(init) {
|
|
1127
|
-
super();
|
|
1128
|
-
this.mode = init.mode ?? 'open';
|
|
1129
|
-
this._init = init;
|
|
1130
|
-
/** Set by `attachShadow`; a shadow root's `host` is part of the contract the router reads. */
|
|
1131
|
-
this._host = null;
|
|
1132
|
-
this.innerHTML = '';
|
|
1133
|
-
this._styles = [];
|
|
1134
|
-
}
|
|
1135
|
-
/** `shadowrootmode` plus whatever else the root was opened with, as the parser expects them. */
|
|
1136
|
-
templateAttributes() {
|
|
1137
|
-
let out = ` shadowrootmode="${this.mode}"`;
|
|
1138
|
-
for (const [option, attribute] of SHADOW_ATTRIBUTES) if (this._init[option]) out += ` ${attribute}=""`;
|
|
1139
|
-
return out;
|
|
1140
|
-
}
|
|
1141
|
-
/**
|
|
1142
|
-
* A `<style>` joins the stylesheet collection; anything else is content.
|
|
1143
|
-
*
|
|
1144
|
-
* Every append used to be treated as a stylesheet, on the assumption that only `adoptStyles`
|
|
1145
|
-
* would ever reach here. A component appending an element to its own shadow root — ordinary DOM
|
|
1146
|
-
* code — therefore had that element silently turned into CSS and its markup lost.
|
|
1147
|
-
*
|
|
1148
|
-
* @override
|
|
1149
|
-
*/
|
|
1150
|
-
appendChild(node) {
|
|
1151
|
-
if (node?.localName === 'style') {
|
|
1152
|
-
this._styles.push(node.innerHTML);
|
|
1153
|
-
return node;
|
|
1154
|
-
}
|
|
1155
|
-
return super.appendChild(node);
|
|
1156
|
-
}
|
|
1157
|
-
/** A shadow root's `host` is part of the contract `@verajs/router` reads. */
|
|
1158
|
-
get host() {
|
|
1159
|
-
return this._host;
|
|
1160
|
-
}
|
|
1161
|
-
/**
|
|
1162
|
-
* The options it was opened with, readable the way a browser exposes them.
|
|
1163
|
-
*
|
|
1164
|
-
* These are already serialized into the declarative template — they have to be, because
|
|
1165
|
-
* `attachShadow` reuses a declarative root and ignores the options it is handed. A component
|
|
1166
|
-
* that *reads* them back, to decide whether to manage focus itself, saw `undefined`.
|
|
1167
|
-
*/
|
|
1168
|
-
get delegatesFocus() {
|
|
1169
|
-
return !!this._init.delegatesFocus;
|
|
1170
|
-
}
|
|
1171
|
-
get clonable() {
|
|
1172
|
-
return !!this._init.clonable;
|
|
1173
|
-
}
|
|
1174
|
-
get serializable() {
|
|
1175
|
-
return !!this._init.serializable;
|
|
1176
|
-
}
|
|
1177
|
-
get slotAssignment() {
|
|
1178
|
-
return this._init.slotAssignment ?? 'named';
|
|
1179
|
-
}
|
|
1180
|
-
get nodeType() {
|
|
1181
|
-
return 11;
|
|
1182
|
-
}
|
|
1183
|
-
get nodeName() {
|
|
1184
|
-
return '#document-fragment';
|
|
1185
|
-
}
|
|
1186
|
-
/** Nothing is focused on a server, and nothing is in a top layer. */
|
|
1187
|
-
get activeElement() {
|
|
1188
|
-
return null;
|
|
1189
|
-
}
|
|
1190
|
-
get fullscreenElement() {
|
|
1191
|
-
return null;
|
|
1192
|
-
}
|
|
1193
|
-
get pointerLockElement() {
|
|
1194
|
-
return null;
|
|
1195
|
-
}
|
|
1196
|
-
get pictureInPictureElement() {
|
|
1197
|
-
return null;
|
|
1198
|
-
}
|
|
1199
|
-
/** `<style>` elements appended here; the constructed ones are `adoptedStyleSheets`. */
|
|
1200
|
-
get styleSheets() {
|
|
1201
|
-
return this._styles.map((cssText) => Object.assign(new StyleSheetShim(), { cssText }));
|
|
1202
|
-
}
|
|
1203
|
-
/** A shadow root's text is its content's text, and setting it replaces the content. */
|
|
1204
|
-
get textContent() {
|
|
1205
|
-
return textOf(this);
|
|
1206
|
-
}
|
|
1207
|
-
set textContent(value) {
|
|
1208
|
-
this.innerHTML = escapeHtml(value);
|
|
1209
|
-
}
|
|
1210
|
-
/**
|
|
1211
|
-
* Constructed sheets land here; serialized alongside string styles.
|
|
1212
|
-
*
|
|
1213
|
-
* **Checked, because the platform checks.** A browser raises `TypeError` for a value that is not
|
|
1214
|
-
* a sequence and for an entry that is not a `CSSStyleSheet`, and taking anything meant that
|
|
1215
|
-
* `root.adoptedStyleSheets = sheet` — the single missing `[…]`, and the most likely way to get
|
|
1216
|
-
* this wrong — was accepted here and threw in the browser, after the server had already rendered.
|
|
1217
|
-
*/
|
|
1218
|
-
set adoptedStyleSheets(sheets) {
|
|
1219
|
-
if (!Array.isArray(sheets))
|
|
1220
|
-
throw new TypeError(
|
|
1221
|
-
`Failed to set the 'adoptedStyleSheets' property: the provided value cannot be converted to a sequence.`
|
|
1222
|
-
);
|
|
1223
|
-
for (const sheet of sheets)
|
|
1224
|
-
if (!(sheet instanceof StyleSheetShim))
|
|
1225
|
-
throw new TypeError(
|
|
1226
|
-
`Failed to set the 'adoptedStyleSheets' property: the provided value is not of type 'CSSStyleSheet'.`
|
|
1227
|
-
);
|
|
1228
|
-
this._adopted = sheets;
|
|
1229
|
-
}
|
|
1230
|
-
get adoptedStyleSheets() {
|
|
1231
|
-
return this._adopted ?? [];
|
|
1232
|
-
}
|
|
1233
|
-
/**
|
|
1234
|
-
* The `<style>` tags, kept separate from the content.
|
|
1235
|
-
*
|
|
1236
|
-
* They used to be concatenated here and the whole string handed to the nested-component scan,
|
|
1237
|
-
* which then read the stylesheet as markup: CSS containing a registered tag name — a
|
|
1238
|
-
* `content: "<some-comp>"` is enough — had that component **rendered inside the stylesheet**.
|
|
1239
|
-
* A scan for elements has no business reading a raw-text element.
|
|
1240
|
-
*/
|
|
1241
|
-
styleTags() {
|
|
1242
|
-
const sheets = (this._adopted ?? []).map((sheet) => sheet.cssText ?? '');
|
|
1243
|
-
/**
|
|
1244
|
-
* **Text first, adopted sheets last — because that is the order the browser cascades them in.**
|
|
1245
|
-
*
|
|
1246
|
-
* A shadow root's own `<style>` elements are its tree-order sheets, and `adoptedStyleSheets`
|
|
1247
|
-
* apply *after* them: measured in Chromium, a constructed rule beats an identical-specificity
|
|
1248
|
-
* rule from a `<style>` in the same root no matter which appears first in the markup. Here both
|
|
1249
|
-
* are `<style>` elements, so the cascade is document order and whichever is written last wins.
|
|
1250
|
-
*
|
|
1251
|
-
* Emitting sheets first therefore inverted it. `static styles = [sheet, '.plain { … }']` — a
|
|
1252
|
-
* legal shape this package serializes on purpose — had the string win on the server and lose in
|
|
1253
|
-
* the browser, so the page changed appearance as it hydrated, in the one direction nothing
|
|
1254
|
-
* compares: markup, node identity and property values all matched.
|
|
1255
|
-
*/
|
|
1256
|
-
return [...this._styles, ...sheets]
|
|
1257
|
-
.filter(Boolean)
|
|
1258
|
-
.map((css) => `<style data-vm-sheet="styles">${escapeStyleText(css)}</style>`)
|
|
1259
|
-
.join('');
|
|
1260
|
-
}
|
|
1261
|
-
}
|
|
1262
|
-
|
|
1263
|
-
/**
|
|
1264
|
-
* Properties that are a **view of an attribute**, and the attribute each one reflects.
|
|
1265
|
-
*
|
|
1266
|
-
* `this.id = 'x'`, `this.className = 'a b'`, `this.ariaLabel = 'Close'`, `this.hidden = true` are
|
|
1267
|
-
* all ordinary component code, and every one of them was a plain property here: the assignment
|
|
1268
|
-
* stuck to the object and never reached the markup, so the server and the client disagreed about
|
|
1269
|
-
* the element's own attributes. Generated from a table rather than written out, because there are
|
|
1270
|
-
* sixty of them and the ARIA half is pure repetition — the list is the specification, and adding
|
|
1271
|
-
* one is adding a name.
|
|
1272
|
-
*
|
|
1273
|
-
* `booleans` are present/absent (`hidden`), the rest carry their value. `role` and the `aria-*`
|
|
1274
|
-
* family are ordinary string reflections; `tabIndex` is a number that still round-trips as text.
|
|
1275
|
-
*/
|
|
1276
|
-
const dashedAria = (name) => `aria-${name.slice(4).toLowerCase()}`;
|
|
1277
|
-
|
|
1278
|
-
/** Plain strings: the attribute's value, or `''` when it is absent. */
|
|
1279
|
-
const REFLECTED = {
|
|
1280
|
-
id: 'id',
|
|
1281
|
-
className: 'class',
|
|
1282
|
-
title: 'title',
|
|
1283
|
-
lang: 'lang',
|
|
1284
|
-
dir: 'dir',
|
|
1285
|
-
slot: 'slot',
|
|
1286
|
-
nonce: 'nonce',
|
|
1287
|
-
accessKey: 'accesskey',
|
|
1288
|
-
role: 'role',
|
|
1289
|
-
autocorrect: 'autocorrect',
|
|
1290
|
-
};
|
|
1291
|
-
|
|
1292
|
-
/** The reflected strings declared `DOMString?`, which answer `null` rather than `''`. */
|
|
1293
|
-
const NULLABLE_REFLECTED = new Set(['role']);
|
|
1294
|
-
|
|
1295
|
-
/**
|
|
1296
|
-
* **Enumerated reflections answer a *state*, not the attribute's text.** `inputmode="bogus"` reads
|
|
1297
|
-
* back as `''` in every engine, not `'bogus'`, and an absent attribute has its own answer that is
|
|
1298
|
-
* often different again — `autocapitalize` is `''` when missing and `'sentences'` when invalid. The
|
|
1299
|
-
* markup is unaffected either way (the attribute is stored verbatim, which is also what the engines
|
|
1300
|
-
* do), so this is only visible to a component that *reads* the property server-side — and that is
|
|
1301
|
-
* exactly the divergence that surfaces later as a hydration mismatch with nothing left to explain it.
|
|
1302
|
-
*
|
|
1303
|
-
* Every value here was measured on Chromium, Firefox and WebKit rather than read off a spec; all
|
|
1304
|
-
* three agree on all of it. `tests/browser/reflected-enumerations.test.js` records the measurement.
|
|
1305
|
-
* Two deliberate omissions:
|
|
1306
|
-
*
|
|
1307
|
-
* - **`spellcheck` is not here** — the engines genuinely disagree (an invalid value reads `true` in
|
|
1308
|
-
* Chromium and WebKit, `false` in Firefox), so there is no single answer to match.
|
|
1309
|
-
* - **`autocorrect` is not here** for the same reason: Chromium does not implement it at all, and
|
|
1310
|
-
* Firefox and WebKit answer a boolean rather than a string.
|
|
1311
|
-
*
|
|
1312
|
-
* `popover: 'hint'` is treated as known because the spec and two engines say so; WebKit does not
|
|
1313
|
-
* implement that state yet and clamps it to `'manual'`. Following the majority is the lesser wrong,
|
|
1314
|
-
* and it cannot affect markup.
|
|
1315
|
-
*/
|
|
1316
|
-
const ENUMERATED = {
|
|
1317
|
-
popover: [['auto', 'manual', 'hint'], null, 'manual'],
|
|
1318
|
-
autocapitalize: [['none', 'off', 'on', 'sentences', 'words', 'characters'], '', 'sentences'],
|
|
1319
|
-
enterKeyHint: [['enter', 'done', 'go', 'next', 'previous', 'search', 'send'], '', ''],
|
|
1320
|
-
inputMode: [['none', 'text', 'tel', 'url', 'email', 'numeric', 'decimal', 'search'], '', ''],
|
|
1321
|
-
writingSuggestions: [['true', 'false'], 'true', 'true'],
|
|
1322
|
-
virtualKeyboardPolicy: [['auto', 'manual'], '', ''],
|
|
1323
|
-
};
|
|
1324
|
-
|
|
1325
|
-
/** The attribute each of them reflects. */
|
|
1326
|
-
const ENUMERATED_ATTRIBUTES = {
|
|
1327
|
-
popover: 'popover',
|
|
1328
|
-
autocapitalize: 'autocapitalize',
|
|
1329
|
-
enterKeyHint: 'enterkeyhint',
|
|
1330
|
-
inputMode: 'inputmode',
|
|
1331
|
-
writingSuggestions: 'writingsuggestions',
|
|
1332
|
-
virtualKeyboardPolicy: 'virtualkeyboardpolicy',
|
|
1333
|
-
};
|
|
1334
|
-
|
|
1335
|
-
/** `contentEditable` is enumerated too, and is the only one whose setter validates. */
|
|
1336
|
-
const CONTENT_EDITABLE_STATES = ['true', 'false', 'plaintext-only'];
|
|
1337
|
-
|
|
1338
|
-
/** Present or absent. */
|
|
1339
|
-
const REFLECTED_PRESENCE = { hidden: 'hidden', autofocus: 'autofocus', inert: 'inert' };
|
|
1340
|
-
|
|
1341
|
-
/**
|
|
1342
|
-
* A boolean in JavaScript, the words `true`/`false` in the markup — **each with its own default**,
|
|
1343
|
-
* because they do not share one. With the attribute absent a browser reports `draggable` as `false`
|
|
1344
|
-
* and `spellcheck` as `true`. One shared rule gave both `true`, so `draggable` was wrong on every
|
|
1345
|
-
* element that had not set it, and `spellcheck` was right by accident.
|
|
1346
|
-
*/
|
|
1347
|
-
const REFLECTED_TRUE_FALSE = { draggable: ['draggable', false], spellcheck: ['spellcheck', true] };
|
|
1348
|
-
|
|
1349
|
-
/** The one that spells its booleans differently. */
|
|
1350
|
-
const REFLECTED_YES_NO = { translate: 'translate' };
|
|
1351
|
-
|
|
1352
|
-
/** A number in JavaScript, its digits in the markup. */
|
|
1353
|
-
const REFLECTED_NUMBERS = { tabIndex: 'tabindex' };
|
|
1354
|
-
|
|
1355
|
-
/**
|
|
1356
|
-
* The tags that are focusable without a `tabindex`, and so report `tabIndex === 0` rather than `-1`.
|
|
1357
|
-
*
|
|
1358
|
-
* `tabIndex` had no default at all — every element answered `0`, which says "in the tab order" about
|
|
1359
|
-
* a `<div>` that is not in it. `element.tabIndex < 0` is the ordinary way to ask whether something
|
|
1360
|
-
* can be focused, so the server answered the opposite of the browser for the common case.
|
|
1361
|
-
*
|
|
1362
|
-
* `a` and `area` need an `href` to be focusable, and `audio`/`video` need `controls`. jsdom reports
|
|
1363
|
-
* `0` for a bare `<a>`, so a differential run against it will flag that one cell — the browsers are
|
|
1364
|
-
* right and jsdom is loose, which is the usual direction.
|
|
1365
|
-
*/
|
|
1366
|
-
const FOCUSABLE = new Set(['button', 'input', 'select', 'textarea', 'details', 'iframe', 'summary']);
|
|
1367
|
-
const CONDITIONALLY_FOCUSABLE = { a: 'href', area: 'href', audio: 'controls', video: 'controls' };
|
|
1368
|
-
|
|
1369
|
-
/**
|
|
1370
|
-
* `Node`'s numeric constants, and the measurements a box that was never laid out reports.
|
|
1371
|
-
*
|
|
1372
|
-
* Zero is what a browser returns for a detached element, so these are accurate rather than
|
|
1373
|
-
* convenient. `currentCSSZoom` is 1 for the same reason.
|
|
1374
|
-
*/
|
|
1375
|
-
export const NODE_CONSTANTS = {
|
|
1376
|
-
ELEMENT_NODE: 1,
|
|
1377
|
-
ATTRIBUTE_NODE: 2,
|
|
1378
|
-
TEXT_NODE: 3,
|
|
1379
|
-
CDATA_SECTION_NODE: 4,
|
|
1380
|
-
ENTITY_REFERENCE_NODE: 5,
|
|
1381
|
-
ENTITY_NODE: 6,
|
|
1382
|
-
PROCESSING_INSTRUCTION_NODE: 7,
|
|
1383
|
-
COMMENT_NODE: 8,
|
|
1384
|
-
DOCUMENT_NODE: 9,
|
|
1385
|
-
DOCUMENT_TYPE_NODE: 10,
|
|
1386
|
-
DOCUMENT_FRAGMENT_NODE: 11,
|
|
1387
|
-
NOTATION_NODE: 12,
|
|
1388
|
-
DOCUMENT_POSITION_DISCONNECTED: 1,
|
|
1389
|
-
DOCUMENT_POSITION_PRECEDING: 2,
|
|
1390
|
-
DOCUMENT_POSITION_FOLLOWING: 4,
|
|
1391
|
-
DOCUMENT_POSITION_CONTAINS: 8,
|
|
1392
|
-
DOCUMENT_POSITION_CONTAINED_BY: 16,
|
|
1393
|
-
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 32,
|
|
1394
|
-
};
|
|
1395
|
-
|
|
1396
|
-
const LAYOUT_ZEROS = [
|
|
1397
|
-
'clientWidth', 'clientHeight', 'clientLeft', 'clientTop',
|
|
1398
|
-
'offsetWidth', 'offsetHeight', 'offsetLeft', 'offsetTop',
|
|
1399
|
-
'scrollWidth', 'scrollHeight', 'scrollLeftMax', 'scrollTopMax',
|
|
1400
|
-
];
|
|
1401
|
-
|
|
1402
|
-
/**
|
|
1403
|
-
* Installs the generated accessors onto the shim's prototype.
|
|
1404
|
-
*
|
|
1405
|
-
* @param {typeof ElementShim} Shim
|
|
1406
|
-
*/
|
|
1407
|
-
const defineReflections = (Shim) => {
|
|
1408
|
-
for (const name of LAYOUT_ZEROS) Object.defineProperty(Shim.prototype, name, { value: 0, writable: true, configurable: true });
|
|
1409
|
-
/** Scroll offsets are writable and read back, which is what a scroll-restoring component does. */
|
|
1410
|
-
for (const name of ['scrollLeft', 'scrollTop'])
|
|
1411
|
-
Object.defineProperty(Shim.prototype, name, { value: 0, writable: true, configurable: true });
|
|
1412
|
-
Object.defineProperty(Shim.prototype, 'currentCSSZoom', { value: 1, configurable: true });
|
|
1413
|
-
/**
|
|
1414
|
-
* **It follows the `contentEditable` *state*, not the attribute's text.** Comparing the raw
|
|
1415
|
-
* attribute to `'true'` answered `false` for `plaintext-only`, for an empty attribute and for
|
|
1416
|
-
* `TRUE` — all three of which are editable in Chromium and Firefox whether the element is in the
|
|
1417
|
-
* document or not, and in WebKit once it is.
|
|
1418
|
-
*
|
|
1419
|
-
* WebKit's answer for a *detached* element is unstable — it flips depending on whether an
|
|
1420
|
-
* attached editable element happens to exist elsewhere in the document — so it is not evidence
|
|
1421
|
-
* about the mapping. The mapping itself is unanimous.
|
|
1422
|
-
*/
|
|
1423
|
-
Object.defineProperty(Shim.prototype, 'isContentEditable', {
|
|
1424
|
-
get() {
|
|
1425
|
-
const state = this.contentEditable;
|
|
1426
|
-
return state === 'true' || state === 'plaintext-only';
|
|
1427
|
-
},
|
|
1428
|
-
configurable: true,
|
|
1429
|
-
});
|
|
1430
|
-
for (const [property, attribute] of Object.entries(REFLECTED)) {
|
|
1431
|
-
Object.defineProperty(Shim.prototype, property, {
|
|
1432
|
-
get() {
|
|
1433
|
-
/**
|
|
1434
|
-
* **`role` and `popover` are nullable and the rest are not.** Both are declared `DOMString?`,
|
|
1435
|
-
* so a browser answers `null` when the attribute is absent where `id` and `title` answer
|
|
1436
|
-
* `''`. Returning `''` for `role` made `element.role === null` false on every element that
|
|
1437
|
-
* had never been given one — a test a component writes to mean "this has no explicit role".
|
|
1438
|
-
*/
|
|
1439
|
-
return this.getAttribute(attribute) ?? (NULLABLE_REFLECTED.has(property) ? null : '');
|
|
1440
|
-
},
|
|
1441
|
-
set(value) {
|
|
1442
|
-
this.setAttribute(attribute, value);
|
|
1443
|
-
},
|
|
1444
|
-
configurable: true,
|
|
1445
|
-
});
|
|
1446
|
-
}
|
|
1447
|
-
for (const [property, attribute] of Object.entries(ENUMERATED_ATTRIBUTES)) {
|
|
1448
|
-
const [known, missing, invalid] = ENUMERATED[property];
|
|
1449
|
-
Object.defineProperty(Shim.prototype, property, {
|
|
1450
|
-
get() {
|
|
1451
|
-
const raw = this.getAttribute(attribute);
|
|
1452
|
-
if (raw === null) return missing;
|
|
1453
|
-
/** Enumerated attributes are ASCII case-insensitive and the getter answers canonically. */
|
|
1454
|
-
const state = raw.toLowerCase();
|
|
1455
|
-
return known.includes(state) ? state : invalid;
|
|
1456
|
-
},
|
|
1457
|
-
/** The setter writes what it is given; only the getter maps to a state. */
|
|
1458
|
-
set(value) {
|
|
1459
|
-
this.setAttribute(attribute, value);
|
|
1460
|
-
},
|
|
1461
|
-
configurable: true,
|
|
1462
|
-
});
|
|
1463
|
-
}
|
|
1464
|
-
/**
|
|
1465
|
-
* **`contentEditable` is the one whose setter validates.** Every engine throws a `SyntaxError`
|
|
1466
|
-
* for a value outside the three states, lowercases what it accepts, and treats `'inherit'` as
|
|
1467
|
-
* "remove the attribute" rather than a value to write. An empty string throws — it is not the
|
|
1468
|
-
* same as `'inherit'`, though the attribute *being* empty reads back as `'true'`.
|
|
1469
|
-
*/
|
|
1470
|
-
Object.defineProperty(Shim.prototype, 'contentEditable', {
|
|
1471
|
-
get() {
|
|
1472
|
-
const raw = this.getAttribute('contenteditable');
|
|
1473
|
-
if (raw === null) return 'inherit';
|
|
1474
|
-
const state = raw.toLowerCase();
|
|
1475
|
-
if (state === '') return 'true';
|
|
1476
|
-
return CONTENT_EDITABLE_STATES.includes(state) ? state : 'inherit';
|
|
1477
|
-
},
|
|
1478
|
-
set(value) {
|
|
1479
|
-
const state = `${value}`.toLowerCase();
|
|
1480
|
-
if (state === 'inherit') {
|
|
1481
|
-
this.removeAttribute('contenteditable');
|
|
1482
|
-
return;
|
|
1483
|
-
}
|
|
1484
|
-
if (!CONTENT_EDITABLE_STATES.includes(state))
|
|
1485
|
-
throw new DOMException(
|
|
1486
|
-
`Failed to set the 'contentEditable' property on 'HTMLElement': The value provided ` +
|
|
1487
|
-
`('${value}') is not one of 'true', 'false', 'plaintext-only' or 'inherit'.`,
|
|
1488
|
-
'SyntaxError'
|
|
1489
|
-
);
|
|
1490
|
-
this.setAttribute('contenteditable', state);
|
|
1491
|
-
},
|
|
1492
|
-
configurable: true,
|
|
1493
|
-
});
|
|
1494
|
-
for (const [property, attribute] of Object.entries(REFLECTED_PRESENCE)) {
|
|
1495
|
-
Object.defineProperty(Shim.prototype, property, {
|
|
1496
|
-
get() {
|
|
1497
|
-
return this.hasAttribute(attribute);
|
|
1498
|
-
},
|
|
1499
|
-
set(value) {
|
|
1500
|
-
if (value) this.setAttribute(attribute, '');
|
|
1501
|
-
else this.removeAttribute(attribute);
|
|
1502
|
-
},
|
|
1503
|
-
configurable: true,
|
|
1504
|
-
});
|
|
1505
|
-
}
|
|
1506
|
-
for (const [words, table] of [
|
|
1507
|
-
[['true', 'false'], REFLECTED_TRUE_FALSE],
|
|
1508
|
-
[['yes', 'no'], REFLECTED_YES_NO],
|
|
1509
|
-
]) {
|
|
1510
|
-
for (const [property, entry] of Object.entries(table)) {
|
|
1511
|
-
/** `['attribute', default]` where the default differs per property; a bare string means `true`. */
|
|
1512
|
-
const [attribute, fallback] = Array.isArray(entry) ? entry : [entry, true];
|
|
1513
|
-
Object.defineProperty(Shim.prototype, property, {
|
|
1514
|
-
get() {
|
|
1515
|
-
const written = this.getAttribute(attribute);
|
|
1516
|
-
return written === null ? fallback : written !== words[1];
|
|
1517
|
-
},
|
|
1518
|
-
set(value) {
|
|
1519
|
-
this.setAttribute(attribute, value ? words[0] : words[1]);
|
|
1520
|
-
},
|
|
1521
|
-
configurable: true,
|
|
1522
|
-
});
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
for (const [property, attribute] of Object.entries(REFLECTED_NUMBERS)) {
|
|
1526
|
-
Object.defineProperty(Shim.prototype, property, {
|
|
1527
|
-
get() {
|
|
1528
|
-
const written = this.getAttribute(attribute);
|
|
1529
|
-
if (written !== null) return Number(written);
|
|
1530
|
-
const conditional = CONDITIONALLY_FOCUSABLE[this.localName];
|
|
1531
|
-
return FOCUSABLE.has(this.localName) || (conditional && this.hasAttribute(conditional)) ? 0 : -1;
|
|
1532
|
-
},
|
|
1533
|
-
set(value) {
|
|
1534
|
-
this.setAttribute(attribute, Number(value));
|
|
1535
|
-
},
|
|
1536
|
-
configurable: true,
|
|
1537
|
-
});
|
|
1538
|
-
}
|
|
1539
|
-
for (const property of ARIA_PROPERTIES) {
|
|
1540
|
-
const attribute = dashedAria(property);
|
|
1541
|
-
Object.defineProperty(Shim.prototype, property, {
|
|
1542
|
-
get() {
|
|
1543
|
-
return this.getAttribute(attribute);
|
|
1544
|
-
},
|
|
1545
|
-
set(value) {
|
|
1546
|
-
if (value == null) this.removeAttribute(attribute);
|
|
1547
|
-
else this.setAttribute(attribute, value);
|
|
1548
|
-
},
|
|
1549
|
-
configurable: true,
|
|
1550
|
-
});
|
|
1551
|
-
}
|
|
1552
|
-
};
|
|
1553
|
-
|
|
1554
|
-
/**
|
|
1555
|
-
* The ARIA reflection family, named once. Each is `aria-` plus the rest, lowercased —
|
|
1556
|
-
* `ariaValueMax` is `aria-valuemax` — with no exceptions in the set a component uses.
|
|
1557
|
-
*/
|
|
1558
|
-
const ARIA_PROPERTIES = [
|
|
1559
|
-
'ariaAtomic', 'ariaAutoComplete', 'ariaBrailleLabel', 'ariaBrailleRoleDescription', 'ariaBusy',
|
|
1560
|
-
'ariaChecked', 'ariaColCount', 'ariaColIndex',
|
|
1561
|
-
'ariaColIndexText', 'ariaColSpan', 'ariaCurrent', 'ariaDescription', 'ariaDisabled',
|
|
1562
|
-
'ariaExpanded', 'ariaHasPopup', 'ariaHidden', 'ariaInvalid', 'ariaKeyShortcuts', 'ariaLabel',
|
|
1563
|
-
'ariaLevel', 'ariaLive', 'ariaModal', 'ariaMultiLine', 'ariaMultiSelectable', 'ariaOrientation',
|
|
1564
|
-
'ariaPlaceholder', 'ariaPosInSet', 'ariaPressed', 'ariaReadOnly', 'ariaRelevant', 'ariaRequired',
|
|
1565
|
-
'ariaRoleDescription', 'ariaRowCount', 'ariaRowIndex', 'ariaRowIndexText', 'ariaRowSpan',
|
|
1566
|
-
'ariaSelected', 'ariaSetSize', 'ariaSort', 'ariaValueMax', 'ariaValueMin', 'ariaValueNow',
|
|
1567
|
-
'ariaValueText',
|
|
1568
|
-
];
|
|
1569
|
-
|
|
1570
|
-
/**
|
|
1571
|
-
* A component's own `attributeChangedCallback`, when it declared one.
|
|
1572
|
-
*
|
|
1573
|
-
* Read through a cast rather than declared as a field: a field in this base class would be an own
|
|
1574
|
-
* property set to `undefined`, which shadows the subclass's method and silently switches observed
|
|
1575
|
-
* attributes back off — the very thing this exists to deliver.
|
|
1576
|
-
*
|
|
1577
|
-
* @param {ElementShim} element
|
|
1578
|
-
* @return {((name: string, previous: string | null, value: string | null) => void) | undefined}
|
|
1579
|
-
*/
|
|
1580
|
-
const observerOf = (element) =>
|
|
1581
|
-
/** @type {{ attributeChangedCallback?: (name: string, previous: string | null, value: string | null) => void }} */ (
|
|
1582
|
-
element
|
|
1583
|
-
).attributeChangedCallback;
|
|
1584
|
-
|
|
1585
|
-
export class ElementShim extends ContainerShim {
|
|
1586
|
-
constructor(localName = '', namespaceURI = HTML_NS) {
|
|
1587
|
-
super();
|
|
1588
|
-
this._ns = namespaceURI;
|
|
1589
|
-
this.localName = localName;
|
|
1590
|
-
this.isConnected = true;
|
|
1591
|
-
this._attributes = new Map();
|
|
1592
|
-
this.innerHTML = '';
|
|
1593
|
-
this._shadowRoot = null;
|
|
1594
|
-
/**
|
|
1595
|
-
* The bytes this element's own tags were parsed from, when it came from markup rather than
|
|
1596
|
-
* `createElement`. Declared here so every element has the same shape — and so a type-checker
|
|
1597
|
-
* knows about a field the parser is what assigns.
|
|
1598
|
-
*/
|
|
1599
|
-
this._sourceOpenTag = /** @type {string | null} */ (null);
|
|
1600
|
-
this._sourceCloseTag = /** @type {string | null} */ (null);
|
|
1601
|
-
}
|
|
1602
|
-
/** @override */
|
|
1603
|
-
get namespaceURI() {
|
|
1604
|
-
return this._ns;
|
|
1605
|
-
}
|
|
1606
|
-
/**
|
|
1607
|
-
* An attribute name as this element stores it.
|
|
1608
|
-
*
|
|
1609
|
-
* A browser lower-cases the name inside `setAttribute` on an HTML element, so `setAttribute('Data-Flag', '1')`
|
|
1610
|
-
* and `getAttribute('data-flag')` are the *same* attribute. Storing the name as written made them two,
|
|
1611
|
-
* and a component that set one spelling and read another got `null` on the server and `'1'` in the
|
|
1612
|
-
* browser — a lifecycle divergence with no symptom until the two renders disagreed. Worse, an
|
|
1613
|
-
* attribute handed in as `{ 'User-ID': … }` never matched an `observedAttributes` entry, so
|
|
1614
|
-
* `attributeChangedCallback` simply did not fire.
|
|
1615
|
-
*/
|
|
1616
|
-
_name(name) {
|
|
1617
|
-
/**
|
|
1618
|
-
* **Template coercion, not `String()`** — the difference is a symbol, which `String()` answers
|
|
1619
|
-
* `'Symbol(s)'` for and which every engine refuses with a `TypeError` (recorded across all
|
|
1620
|
-
* three in `tests/browser/dom-string-coercion.test.js`). Being the lenient one server-side
|
|
1621
|
-
* only moves the failure to the client with the context stripped off.
|
|
1622
|
-
*/
|
|
1623
|
-
return this._ns === HTML_NS ? `${name}`.toLowerCase() : `${name}`;
|
|
1624
|
-
}
|
|
1625
|
-
/**
|
|
1626
|
-
* **`mode: 'closed'` means `element.shadowRoot` is `null`** — that is the entire difference between
|
|
1627
|
-
* the two modes, and it was not honoured: a closed root was handed straight back, so a component
|
|
1628
|
-
* guarding on `this.shadowRoot` took the branch the browser will not, and anything holding the
|
|
1629
|
-
* element could reach inside a root the platform hides. The root is kept on `_shadowRoot`, because
|
|
1630
|
-
* it still has to be serialized — declarative shadow DOM expresses `closed` perfectly well
|
|
1631
|
-
* (`<template shadowrootmode="closed">`) and the client's parser re-creates it just as hidden.
|
|
1632
|
-
*/
|
|
1633
|
-
get shadowRoot() {
|
|
1634
|
-
return this._shadowRoot?.mode === 'closed' ? null : this._shadowRoot;
|
|
1635
|
-
}
|
|
1636
|
-
/**
|
|
1637
|
-
* Both refusals are the platform's. `mode` is a required member, and an element that already has
|
|
1638
|
-
* a root raises `NotSupportedError` rather than getting a second one — which matters here because
|
|
1639
|
-
* silently replacing the first root discards everything rendered into it, on the server only.
|
|
1640
|
-
* `@verajs/core` already guards against calling this twice *because* the browser throws; the shim
|
|
1641
|
-
* accepting it meant the server was the one place that guard was not being checked.
|
|
1642
|
-
*/
|
|
1643
|
-
attachShadow(init = {}) {
|
|
1644
|
-
if (init.mode !== 'open' && init.mode !== 'closed')
|
|
1645
|
-
throw new TypeError("Failed to execute 'attachShadow' on 'Element': Failed to read the 'mode' property from 'ShadowRootInit': Required member is undefined.");
|
|
1646
|
-
if (this._shadowRoot)
|
|
1647
|
-
throw new DOMException(
|
|
1648
|
-
"Failed to execute 'attachShadow' on 'Element': Shadow root cannot be created on a host which already hosts a shadow tree.",
|
|
1649
|
-
'NotSupportedError'
|
|
1650
|
-
);
|
|
1651
|
-
this._shadowRoot = new ShadowRootShim(init);
|
|
1652
|
-
this._shadowRoot._host = this;
|
|
1653
|
-
return this._shadowRoot;
|
|
1654
|
-
}
|
|
1655
|
-
getAttribute(name) {
|
|
1656
|
-
name = this._name(name);
|
|
1657
|
-
return this._attributes.has(name) ? this._attributes.get(name) : null;
|
|
1658
|
-
}
|
|
1659
|
-
setAttribute(name, value) {
|
|
1660
|
-
name = this._name(name);
|
|
1661
|
-
/**
|
|
1662
|
-
* **A name the platform refuses is refused here**, because the alternative is worse than a
|
|
1663
|
-
* throw: `setAttribute('a b', v)` wrote `a b="v"` into the tag, which a parser reads back as
|
|
1664
|
-
* *two* attributes, so the server and the client disagreed about the element's own attributes
|
|
1665
|
-
* and the browser threw `InvalidCharacterError` on the same call anyway.
|
|
1666
|
-
*/
|
|
1667
|
-
if (name === '' || UNUSABLE_IN_A_NAME.test(name))
|
|
1668
|
-
throw new DOMException(
|
|
1669
|
-
`Failed to execute 'setAttribute' on 'Element': '${name}' is not a valid attribute name.`,
|
|
1670
|
-
'InvalidCharacterError'
|
|
1671
|
-
);
|
|
1672
|
-
const previous = this.getAttribute(name);
|
|
1673
|
-
this._attributes.set(name, `${value}`);
|
|
1674
|
-
this._attributeChanged(name, previous);
|
|
1675
|
-
}
|
|
1676
|
-
hasAttribute(name) {
|
|
1677
|
-
return this._attributes.has(this._name(name));
|
|
1678
|
-
}
|
|
1679
|
-
/**
|
|
1680
|
-
* `force` decides, and when the attribute is already in the wanted state **nothing happens** —
|
|
1681
|
-
* no write, no callback. This used to reset the value to `''` on `toggleAttribute(name, true)`
|
|
1682
|
-
* for an attribute that was already there, which silently erased it.
|
|
1683
|
-
*/
|
|
1684
|
-
toggleAttribute(name, force) {
|
|
1685
|
-
name = this._name(name);
|
|
1686
|
-
if (name === '' || UNUSABLE_IN_A_NAME.test(name))
|
|
1687
|
-
throw new DOMException(
|
|
1688
|
-
`Failed to execute 'toggleAttribute' on 'Element': '${name}' is not a valid attribute name.`,
|
|
1689
|
-
'InvalidCharacterError'
|
|
1690
|
-
);
|
|
1691
|
-
const present = this._attributes.has(name);
|
|
1692
|
-
const wanted = force ?? !present;
|
|
1693
|
-
if (wanted === present) return wanted;
|
|
1694
|
-
if (wanted) this._attributes.set(name, '');
|
|
1695
|
-
else this._attributes.delete(name);
|
|
1696
|
-
this._attributeChanged(name, wanted ? null : this.getAttribute(name));
|
|
1697
|
-
return wanted;
|
|
1698
|
-
}
|
|
1699
|
-
|
|
1700
|
-
/**
|
|
1701
|
-
* Upgrade, the way the parser does it: `attributeChangedCallback` fires once per **present**
|
|
1702
|
-
* observed attribute, before `connectedCallback`, with a `null` old value.
|
|
1703
|
-
*
|
|
1704
|
-
* It never fired at all on the server. `attributeChangedCallback` is the only reactive-attribute
|
|
1705
|
-
* mechanism a plain custom element has, so a component that derives its state there rendered
|
|
1706
|
-
* its *initial* state into the page and then corrected itself on the client — a hydration
|
|
1707
|
-
* mismatch on every such component, and a flash of the wrong content for anyone without JS.
|
|
1708
|
-
*/
|
|
1709
|
-
upgrade() {
|
|
1710
|
-
this._observed = /** @type {{ observedAttributes?: string[] }} */ (this.constructor).observedAttributes;
|
|
1711
|
-
this._upgraded = true;
|
|
1712
|
-
const changed = observerOf(this);
|
|
1713
|
-
if (!this._observed || !changed) return;
|
|
1714
|
-
for (const name of this._observed) {
|
|
1715
|
-
const value = this.getAttribute(name);
|
|
1716
|
-
if (value !== null) changed.call(this, name, null, value);
|
|
1717
|
-
}
|
|
1718
|
-
}
|
|
1719
|
-
|
|
1720
|
-
/**
|
|
1721
|
-
* And it keeps firing afterwards, because a browser does: a component that changes its own
|
|
1722
|
-
* attribute during a render sees the callback on the client and must see it here.
|
|
1723
|
-
*
|
|
1724
|
-
* Guarded on `_upgraded` first, which is the common case for the renderer's attribute writes —
|
|
1725
|
-
* an element mid-upgrade has no observers yet, and one that observes nothing never reaches the
|
|
1726
|
-
* `includes`.
|
|
1727
|
-
*/
|
|
1728
|
-
_attributeChanged(name, previous) {
|
|
1729
|
-
/**
|
|
1730
|
-
* **A written element stops being its source text.** A parsed element serves the bytes it came
|
|
1731
|
-
* from so a parse can reproduce the page exactly; the moment an attribute changes, those bytes
|
|
1732
|
-
* are stale and canonical output takes over. Every write — `setAttribute`, `removeAttribute`,
|
|
1733
|
-
* `toggleAttribute` — arrives here, which is why this is the only place it has to be said.
|
|
1734
|
-
*/
|
|
1735
|
-
this._sourceOpenTag = null;
|
|
1736
|
-
if (!this._upgraded || !this._observed?.includes(name)) return;
|
|
1737
|
-
/**
|
|
1738
|
-
* Fired even when the value did not change, because a browser does: `setAttribute` runs the
|
|
1739
|
-
* attribute-change steps unconditionally. Only a *no-op* — removing what was not there,
|
|
1740
|
-
* toggling to the state it is already in — is silent, and those never reach here.
|
|
1741
|
-
*/
|
|
1742
|
-
observerOf(this)?.call(this, name, previous, this.getAttribute(name));
|
|
1743
|
-
}
|
|
1744
|
-
/** Enough of a `NamedNodeMap` to iterate, which is what component code does with it. */
|
|
1745
|
-
/**
|
|
1746
|
-
* An array rather than a live `NamedNodeMap` — already a recorded difference, since there is
|
|
1747
|
-
* nothing to be live over in a single render pass. **The map's own methods are on it**, though:
|
|
1748
|
-
* `attributes.getNamedItem('x')` is how a good deal of existing code reads an attribute, and it
|
|
1749
|
-
* was simply missing, so that code got a `TypeError` on the server and worked in a browser.
|
|
1750
|
-
*/
|
|
1751
|
-
get attributes() {
|
|
1752
|
-
return namedNodeMap(this);
|
|
1753
|
-
}
|
|
1754
|
-
getAttributeNames() {
|
|
1755
|
-
return [...this._attributes.keys()];
|
|
1756
|
-
}
|
|
1757
|
-
|
|
1758
|
-
/**
|
|
1759
|
-
* `this.dataset.x = 'y'` and `this.style.color = 'red'` both change the markup, so both write
|
|
1760
|
-
* through to the attribute they are a view of. A plain object would accept the assignment and
|
|
1761
|
-
* lose it.
|
|
1762
|
-
*/
|
|
1763
|
-
get dataset() {
|
|
1764
|
-
return datasetView(this);
|
|
1765
|
-
}
|
|
1766
|
-
get style() {
|
|
1767
|
-
return styleView(this);
|
|
1768
|
-
}
|
|
1769
|
-
/**
|
|
1770
|
-
* The platform's `style` is `[SameObject, PutForwards=cssText]` — `el.style = 'color: red'`
|
|
1771
|
-
* assigns THROUGH to `cssText` in every engine (and in jsdom, the regression net). The shim
|
|
1772
|
-
* declared only the getter, so the ordinary spelling threw `Cannot set property` server-side:
|
|
1773
|
-
* a component-prop delivery of `.style` was refused as read-only while the client accepted and
|
|
1774
|
-
* reflected it — the exact server/client divergence the differential rule exists to catch.
|
|
1775
|
-
* `[LegacyNullToEmptyString]` rides the same IDL attribute, so `null` means `''`.
|
|
1776
|
-
*/
|
|
1777
|
-
set style(value) {
|
|
1778
|
-
/** @type {{ cssText: string }} */ (styleView(this)).cssText = value === null ? '' : `${value}`;
|
|
1779
|
-
}
|
|
1780
|
-
removeAttribute(name) {
|
|
1781
|
-
name = this._name(name);
|
|
1782
|
-
if (!this._attributes.has(name)) return;
|
|
1783
|
-
const previous = this.getAttribute(name);
|
|
1784
|
-
this._attributes.delete(name);
|
|
1785
|
-
this._attributeChanged(name, previous);
|
|
1786
|
-
}
|
|
1787
|
-
|
|
1788
|
-
/**
|
|
1789
|
-
* The rest of the surface an ordinary custom element reaches for.
|
|
1790
|
-
*
|
|
1791
|
-
* The shim was built as "the smallest DOM core's server path touches", which is the wrong bar:
|
|
1792
|
-
* the code that runs here is *user* code, and a component that emits an event, reads its
|
|
1793
|
-
* `tagName`, or adds a class in `connectedCallback` is doing nothing unusual. Each of these threw
|
|
1794
|
-
* a `TypeError` that took the whole render down — measured: `dispatchEvent`, `ownerDocument`,
|
|
1795
|
-
* `tagName`, `children`, `classList`, `closest` and `getRootNode`, all of them.
|
|
1796
|
-
*
|
|
1797
|
-
* They answer the way a detached, childless element would, because that is what this is.
|
|
1798
|
-
*/
|
|
1799
|
-
get tagName() {
|
|
1800
|
-
return this.localName.toUpperCase();
|
|
1801
|
-
}
|
|
1802
|
-
get nodeType() {
|
|
1803
|
-
return 1;
|
|
1804
|
-
}
|
|
1805
|
-
get nodeName() {
|
|
1806
|
-
return this.tagName;
|
|
1807
|
-
}
|
|
1808
|
-
/** Walks the real parent chain, which exists now that children are nodes. */
|
|
1809
|
-
closest(selector) {
|
|
1810
|
-
return select.closest(this, selector);
|
|
1811
|
-
}
|
|
1812
|
-
matches(selector) {
|
|
1813
|
-
return select.matches(this, selector);
|
|
1814
|
-
}
|
|
1815
|
-
/**
|
|
1816
|
-
* **A no-op until nodes were retained** — an appended child had been flattened into its parent's
|
|
1817
|
-
* markup, so there was no parent to ask and nothing to take out. It silently left the element on
|
|
1818
|
-
* the page.
|
|
1819
|
-
*/
|
|
1820
|
-
remove() {
|
|
1821
|
-
this._parent?.removeChild(this);
|
|
1822
|
-
}
|
|
1823
|
-
/**
|
|
1824
|
-
* Backed by the `class` attribute, so a class added during `connectedCallback` reaches the
|
|
1825
|
-
* markup — which it now can, because the opening tag is written from these attributes rather
|
|
1826
|
-
* than copied from the source text.
|
|
1827
|
-
*/
|
|
1828
|
-
get classList() {
|
|
1829
|
-
return tokenListView(this, 'class');
|
|
1830
|
-
}
|
|
1831
|
-
/** `[PutForwards=value]`, for the same reason as `part`. */
|
|
1832
|
-
set classList(value) {
|
|
1833
|
-
this.setAttribute('class', value);
|
|
1834
|
-
}
|
|
1835
|
-
|
|
1836
|
-
/**
|
|
1837
|
-
* What a **detached, childless** element answers.
|
|
1838
|
-
*
|
|
1839
|
-
* Every one of these is the truthful answer for this element, not a placeholder: an element with
|
|
1840
|
-
* no parent has no siblings and no parent, an element outside a document has no box, and a
|
|
1841
|
-
* childless one contains nothing. A browser returns exactly these values for a detached element.
|
|
1842
|
-
* They are here because their *absence* was a `TypeError` that took the whole render down —
|
|
1843
|
-
* `this.parentElement && …` is ordinary defensive code and it crashed.
|
|
1844
|
-
*/
|
|
1845
|
-
|
|
1846
|
-
/** No layout on a server, and none for a detached element in a browser either. */
|
|
1847
|
-
getBoundingClientRect() {
|
|
1848
|
-
return { x: 0, y: 0, top: 0, left: 0, right: 0, bottom: 0, width: 0, height: 0, toJSON: () => ({}) };
|
|
1849
|
-
}
|
|
1850
|
-
getClientRects() {
|
|
1851
|
-
return [];
|
|
1852
|
-
}
|
|
1853
|
-
checkVisibility() {
|
|
1854
|
-
return false;
|
|
1855
|
-
}
|
|
1856
|
-
/**
|
|
1857
|
-
* **The collection queries answer from the tree.** Each returned an empty array whatever the tree
|
|
1858
|
-
* held — the same placeholder shape `querySelector` had, and just as silent. They are live
|
|
1859
|
-
* `HTMLCollection`s in a browser and plain arrays here, a deliberate difference recorded in the
|
|
1860
|
-
* README: there is nothing to be live *over* while a render is a single pass. (That comment said
|
|
1861
|
-
* "already recorded" while the README said nothing about it — the difference is written down now,
|
|
1862
|
-
* along with the `item()`/`namedItem()` half of it that was a side effect rather than a decision.)
|
|
1863
|
-
*/
|
|
1864
|
-
getElementsByTagName(name) {
|
|
1865
|
-
parseChunks(this);
|
|
1866
|
-
const wanted = `${name}`.toLowerCase();
|
|
1867
|
-
return select.descendantsOf(this).filter((element) => wanted === '*' || element.localName === wanted);
|
|
1868
|
-
}
|
|
1869
|
-
getElementsByTagNameNS(namespace, name) {
|
|
1870
|
-
parseChunks(this);
|
|
1871
|
-
const wanted = `${name}`.toLowerCase();
|
|
1872
|
-
const ns = `${namespace}`;
|
|
1873
|
-
return select
|
|
1874
|
-
.descendantsOf(this)
|
|
1875
|
-
.filter(
|
|
1876
|
-
(element) =>
|
|
1877
|
-
(wanted === '*' || element.localName === wanted) && (ns === '*' || element.namespaceURI === ns)
|
|
1878
|
-
);
|
|
1879
|
-
}
|
|
1880
|
-
getElementsByClassName(names) {
|
|
1881
|
-
parseChunks(this);
|
|
1882
|
-
const wanted = `${names}`.split(/\s+/u).filter(Boolean);
|
|
1883
|
-
if (!wanted.length) return [];
|
|
1884
|
-
return select.descendantsOf(this).filter((element) => {
|
|
1885
|
-
const has = (element.getAttribute('class') ?? '').split(/\s+/u);
|
|
1886
|
-
return wanted.every((one) => has.includes(one));
|
|
1887
|
-
});
|
|
1888
|
-
}
|
|
1889
|
-
/**
|
|
1890
|
-
* Namespaces collapse to the plain attribute methods. This DOM serializes HTML, where an
|
|
1891
|
-
* `xml:lang` or `xlink:href` is one attribute with a colon in its name — which is how the
|
|
1892
|
-
* serializer already treats it, and how the markup reads it back.
|
|
1893
|
-
*/
|
|
1894
|
-
getAttributeNS(_namespace, name) {
|
|
1895
|
-
return this.getAttribute(name);
|
|
1896
|
-
}
|
|
1897
|
-
setAttributeNS(_namespace, name, value) {
|
|
1898
|
-
this.setAttribute(name, value);
|
|
1899
|
-
}
|
|
1900
|
-
hasAttributeNS(_namespace, name) {
|
|
1901
|
-
return this.hasAttribute(name);
|
|
1902
|
-
}
|
|
1903
|
-
removeAttributeNS(_namespace, name) {
|
|
1904
|
-
this.removeAttribute(name);
|
|
1905
|
-
}
|
|
1906
|
-
hasAttributes() {
|
|
1907
|
-
return this._attributes.size > 0;
|
|
1908
|
-
}
|
|
1909
|
-
getAttributeNode(name) {
|
|
1910
|
-
return this.hasAttribute(name) ? { name, value: this.getAttribute(name) } : null;
|
|
1911
|
-
}
|
|
1912
|
-
getAttributeNodeNS(_namespace, name) {
|
|
1913
|
-
return this.getAttributeNode(name);
|
|
1914
|
-
}
|
|
1915
|
-
setAttributeNode(node) {
|
|
1916
|
-
this.setAttribute(node.name, node.value);
|
|
1917
|
-
return null;
|
|
1918
|
-
}
|
|
1919
|
-
setAttributeNodeNS(node) {
|
|
1920
|
-
return this.setAttributeNode(node);
|
|
1921
|
-
}
|
|
1922
|
-
removeAttributeNode(node) {
|
|
1923
|
-
this.removeAttribute(node.name);
|
|
1924
|
-
return node;
|
|
1925
|
-
}
|
|
1926
|
-
/** Aliases the engines still carry. */
|
|
1927
|
-
webkitMatchesSelector() {
|
|
1928
|
-
return false;
|
|
1929
|
-
}
|
|
1930
|
-
mozMatchesSelector() {
|
|
1931
|
-
return false;
|
|
1932
|
-
}
|
|
1933
|
-
/**
|
|
1934
|
-
* Interaction, which a server has none of — but the calls are ordinary and must not throw.
|
|
1935
|
-
*
|
|
1936
|
-
* `click()` is the exception: it *dispatches an event*, and now that listeners are real, a
|
|
1937
|
-
* component that clicks itself to seed its own state gets the same result on both sides.
|
|
1938
|
-
*/
|
|
1939
|
-
focus() {}
|
|
1940
|
-
blur() {}
|
|
1941
|
-
click() {
|
|
1942
|
-
this.dispatchEvent(new globalThis.Event('click', { bubbles: true, cancelable: true, composed: true }));
|
|
1943
|
-
}
|
|
1944
|
-
scrollIntoView() {}
|
|
1945
|
-
scroll() {}
|
|
1946
|
-
scrollTo() {}
|
|
1947
|
-
scrollBy() {}
|
|
1948
|
-
|
|
1949
|
-
/**
|
|
1950
|
-
* All four positions. `beforebegin`/`afterend` used to be refused outright on the claim that
|
|
1951
|
-
* this element has no parent — true for a component root, and FALSE for everything inside one:
|
|
1952
|
-
* `_parent` is live for any nested element (the `outerHTML` setter below runs on it), so a
|
|
1953
|
-
* component doing `this.querySelector(…).insertAdjacentHTML('afterend', …)` worked in the
|
|
1954
|
-
* browser and took the whole server render down. The two positions now splice into the parent's
|
|
1955
|
-
* entries exactly as `outerHTML=` does, and only a genuine orphan refuses — with the platform's
|
|
1956
|
-
* own answer, `NoModificationAllowedError`, measured on jsdom and Chromium (the spec raises it
|
|
1957
|
-
* for a null or Document parent; a bare `Error` here was a second divergence inside the first).
|
|
1958
|
-
*
|
|
1959
|
-
* **A markup sink, deliberately** (CODE-PRINCIPLES #8). It writes what it is given, exactly as
|
|
1960
|
-
* the DOM's own `insertAdjacentHTML` does, because a component calling it has already decided
|
|
1961
|
-
* that: the same call on the client is the same sink, and a server that escaped where the browser
|
|
1962
|
-
* does not would render one page and hydrate into another. `insertAdjacentText` escapes, which is
|
|
1963
|
-
* the difference between the two methods and the one to reach for with anything from a request.
|
|
1964
|
-
*/
|
|
1965
|
-
insertAdjacentHTML(position, markup) {
|
|
1966
|
-
const where = `${position}`.toLowerCase();
|
|
1967
|
-
if (where === 'afterbegin' || where === 'beforeend') {
|
|
1968
|
-
if (where === 'afterbegin') this._entries.unshift(`${markup}`);
|
|
1969
|
-
else this._entries.push(`${markup}`);
|
|
1970
|
-
/** New markup has not been looked at yet. */
|
|
1971
|
-
this._parsed = false;
|
|
1972
|
-
}
|
|
1973
|
-
else if (where !== 'beforebegin' && where !== 'afterend')
|
|
1974
|
-
throw new DOMException(
|
|
1975
|
-
`Failed to execute 'insertAdjacentHTML' on 'Element': The value provided ` +
|
|
1976
|
-
`('${position}') is not one of 'beforeBegin', 'afterBegin', 'beforeEnd', or 'afterEnd'.`,
|
|
1977
|
-
'SyntaxError'
|
|
1978
|
-
);
|
|
1979
|
-
else {
|
|
1980
|
-
const parent = this._parent;
|
|
1981
|
-
if (!parent)
|
|
1982
|
-
throw new DOMException(
|
|
1983
|
-
`Failed to execute 'insertAdjacentHTML' on 'Element': The element has no parent.`,
|
|
1984
|
-
'NoModificationAllowedError'
|
|
1985
|
-
);
|
|
1986
|
-
const index = parent._entries.indexOf(this);
|
|
1987
|
-
parent._entries.splice(where === 'beforebegin' ? index : index + 1, 0, `${markup}`);
|
|
1988
|
-
parent._parsed = false;
|
|
1989
|
-
}
|
|
1990
|
-
}
|
|
1991
|
-
insertAdjacentText(position, text) {
|
|
1992
|
-
this.insertAdjacentHTML(position, escapeHtml(text));
|
|
1993
|
-
}
|
|
1994
|
-
insertAdjacentElement(position, element) {
|
|
1995
|
-
this.insertAdjacentHTML(position, serializeElement(element));
|
|
1996
|
-
return element;
|
|
1997
|
-
}
|
|
1998
|
-
/** The element's own markup, which the serializer builds anyway. */
|
|
1999
|
-
get outerHTML() {
|
|
2000
|
-
return serializeElement(this);
|
|
2001
|
-
}
|
|
2002
|
-
/**
|
|
2003
|
-
* **Replaces this element in its parent with the given markup.** Assigning it used to be a
|
|
2004
|
-
* `TypeError` — there was no setter — so `element.outerHTML = '<p>x</p>'`, an ordinary way to swap
|
|
2005
|
-
* a node out, did nothing on the server and worked in the browser.
|
|
2006
|
-
*
|
|
2007
|
-
* **With no parent it returns silently**, which is what the spec says and what all three engines
|
|
2008
|
-
* do — measured, because the obvious guess was a `NoModificationAllowedError` and that is wrong:
|
|
2009
|
-
* the spec raises it only when the parent is a *Document*, not when there is no parent at all.
|
|
2010
|
-
*/
|
|
2011
|
-
set outerHTML(markup) {
|
|
2012
|
-
const parent = this._parent;
|
|
2013
|
-
if (!parent) return;
|
|
2014
|
-
parent._entries.splice(parent._entries.indexOf(this), 1, `${markup}`);
|
|
2015
|
-
parent._parsed = false;
|
|
2016
|
-
this._parent = null;
|
|
2017
|
-
}
|
|
2018
|
-
get innerText() {
|
|
2019
|
-
return this.textContent;
|
|
2020
|
-
}
|
|
2021
|
-
/**
|
|
2022
|
-
* **A line break becomes a `<br>`, which is the whole difference from `textContent`.** Every
|
|
2023
|
-
* engine splits the assigned string on `\r\n`, `\r` and `\n` and joins the escaped pieces with
|
|
2024
|
-
* `<br>`; assigning through `textContent` instead left a literal newline, which collapses to a
|
|
2025
|
-
* single space when the page is laid out. So a component that set `innerText` rendered its lines
|
|
2026
|
-
* run together on the server and correctly broken on the client — a visible difference in the
|
|
2027
|
-
* markup itself, not merely in what a property reads back.
|
|
2028
|
-
*
|
|
2029
|
-
* Measured on Chromium, Firefox and WebKit, attached and detached, in
|
|
2030
|
-
* `tests/browser/inner-text.test.js`.
|
|
2031
|
-
*/
|
|
2032
|
-
set innerText(value) {
|
|
2033
|
-
this.innerHTML = `${value}`.split(/\r\n|[\r\n]/u).map(escapeHtml).join('<br>');
|
|
2034
|
-
}
|
|
2035
|
-
/** `part` is a token list over the `part` attribute, exactly as `classList` is over `class`. */
|
|
2036
|
-
get part() {
|
|
2037
|
-
return tokenListView(this, 'part');
|
|
2038
|
-
}
|
|
2039
|
-
/**
|
|
2040
|
-
* **Assignable, because the IDL says `[PutForwards=value]`.** Both `part` and `classList` are
|
|
2041
|
-
* declared that way, so `element.part = 'a b'` is a legal operation in every engine and writes
|
|
2042
|
-
* the attribute. A getter with no setter made it a `TypeError` here instead — a server that
|
|
2043
|
-
* refuses what the browser performs, which is the same failure as being too permissive with the
|
|
2044
|
-
* direction reversed.
|
|
2045
|
-
*/
|
|
2046
|
-
set part(value) {
|
|
2047
|
-
this.setAttribute('part', value);
|
|
2048
|
-
}
|
|
2049
|
-
|
|
2050
|
-
/**
|
|
2051
|
-
* Form-associated custom elements.
|
|
2052
|
-
*
|
|
2053
|
-
* `attachInternals()` not existing meant `static formAssociated = true` — the standard way to
|
|
2054
|
-
* write a custom form control — threw during `connectedCallback` and took the page with it. None
|
|
2055
|
-
* of what internals carry reaches markup (form value, validity and implicit ARIA are all
|
|
2056
|
-
* internal state), so this is inert by design rather than by omission: the component runs, and
|
|
2057
|
-
* the client's real internals take over on hydration.
|
|
2058
|
-
*/
|
|
2059
|
-
attachInternals() {
|
|
2060
|
-
/**
|
|
2061
|
-
* **Only a custom element has internals.** Every engine raises `NotSupportedError` for a plain
|
|
2062
|
-
* element, because `ElementInternals` is the mechanism by which a *defined* element joins a form
|
|
2063
|
-
* — there is nothing for a `<div>` to attach. Allowing it here meant a call that cannot work in
|
|
2064
|
-
* a browser worked on the server, which is the leniency this DOM exists not to have.
|
|
2065
|
-
*/
|
|
2066
|
-
if (!registry.has(this.localName))
|
|
2067
|
-
throw new DOMException(
|
|
2068
|
-
`Failed to execute 'attachInternals' on 'HTMLElement': Unable to attach ElementInternals to non-custom elements.`,
|
|
2069
|
-
'NotSupportedError'
|
|
2070
|
-
);
|
|
2071
|
-
/** A second call raises `NotSupportedError` in every engine; returning the first set hid that. */
|
|
2072
|
-
if (internals.has(this))
|
|
2073
|
-
throw new DOMException(
|
|
2074
|
-
`Failed to execute 'attachInternals' on 'HTMLElement': ElementInternals for the specified element was already attached.`,
|
|
2075
|
-
'NotSupportedError'
|
|
2076
|
-
);
|
|
2077
|
-
internals.set(this, {
|
|
2078
|
-
shadowRoot: this._shadowRoot,
|
|
2079
|
-
form: null,
|
|
2080
|
-
labels: [],
|
|
2081
|
-
willValidate: true,
|
|
2082
|
-
validity: { valid: true },
|
|
2083
|
-
validationMessage: '',
|
|
2084
|
-
states: new Set(),
|
|
2085
|
-
setFormValue: () => {},
|
|
2086
|
-
setValidity: () => {},
|
|
2087
|
-
checkValidity: () => true,
|
|
2088
|
-
reportValidity: () => true,
|
|
2089
|
-
});
|
|
2090
|
-
return internals.get(this);
|
|
2091
|
-
}
|
|
2092
|
-
|
|
2093
|
-
/**
|
|
2094
|
-
* The element's own opening tag, written from the attributes it holds **now**.
|
|
2095
|
-
*
|
|
2096
|
-
* Copying the source text instead meant everything a component did to itself during
|
|
2097
|
-
* `connectedCallback` was thrown away: `setAttribute('role', 'button')`, a class, an `aria-*` —
|
|
2098
|
-
* present on the client after hydration, absent in the server markup, so the two disagreed on
|
|
2099
|
-
* every one.
|
|
2100
|
-
*/
|
|
2101
|
-
/**
|
|
2102
|
-
* **A copy that shares nothing.** The reason this was out of scope was that a copy sharing the
|
|
2103
|
-
* markup string would alias it; entries are an array now, so a deep clone copies them. The source
|
|
2104
|
-
* text rides along, so a cloned subtree still reproduces the markup it was parsed from.
|
|
2105
|
-
*/
|
|
2106
|
-
cloneNode(deep = false) {
|
|
2107
|
-
const copy = createElement(this.localName, this._ns);
|
|
2108
|
-
for (const [name, value] of this._attributes) copy.setAttribute(name, value);
|
|
2109
|
-
copy._sourceOpenTag = this._sourceOpenTag;
|
|
2110
|
-
copy._sourceCloseTag = this._sourceCloseTag;
|
|
2111
|
-
if (!deep) return copy;
|
|
2112
|
-
for (const entry of this._entries) {
|
|
2113
|
-
if (typeof entry === 'string') copy._entries.push(entry);
|
|
2114
|
-
else {
|
|
2115
|
-
const child = entry.cloneNode(true);
|
|
2116
|
-
child._parent = copy;
|
|
2117
|
-
copy._entries.push(child);
|
|
2118
|
-
}
|
|
2119
|
-
}
|
|
2120
|
-
copy._parsed = this._parsed;
|
|
2121
|
-
return copy;
|
|
2122
|
-
}
|
|
2123
|
-
/** This element's own markup, end tag included — the call every node kind answers. */
|
|
2124
|
-
/**
|
|
2125
|
-
* A `<slot>` shows the host's children that name it; see `assignedTo`.
|
|
2126
|
-
*
|
|
2127
|
-
* **Getters returning `undefined` off a slot**, because these live on `HTMLSlotElement` and this
|
|
2128
|
-
* DOM has one element class. Defining them as plain methods put them on every element, where
|
|
2129
|
-
* `typeof element.assignedNodes === 'function'` — the ordinary way to ask "is this a slot" —
|
|
2130
|
-
* answered yes for a `<div>`. Being present where the platform has nothing is the same kind of
|
|
2131
|
-
* divergence as answering wrongly, and it is the one a feature check walks straight into.
|
|
2132
|
-
*/
|
|
2133
|
-
get assignedNodes() {
|
|
2134
|
-
if (this.localName !== 'slot') return undefined;
|
|
2135
|
-
return (options) => assignedTo(this, options);
|
|
2136
|
-
}
|
|
2137
|
-
get assignedElements() {
|
|
2138
|
-
if (this.localName !== 'slot') return undefined;
|
|
2139
|
-
return (options) => assignedTo(this, options).filter((node) => node.nodeType === 1);
|
|
2140
|
-
}
|
|
2141
|
-
markup() {
|
|
2142
|
-
return serializeElement(this);
|
|
2143
|
-
}
|
|
2144
|
-
openTag() {
|
|
2145
|
-
/**
|
|
2146
|
-
* **The bytes it was parsed from**, until something writes to it. That is what lets a parsed
|
|
2147
|
-
* tree reproduce its own markup exactly — quoting style, entity spelling, attribute order and
|
|
2148
|
-
* interior whitespace included — instead of normalising the page as a side effect of somebody
|
|
2149
|
-
* reading `children`.
|
|
2150
|
-
*/
|
|
2151
|
-
if (this._sourceOpenTag) return this._sourceOpenTag;
|
|
2152
|
-
let out = `<${this.localName}`;
|
|
2153
|
-
for (const [name, value] of this._attributes) out += ` ${name}="${escapeHtml(value)}"`;
|
|
2154
|
-
return out + '>';
|
|
2155
|
-
}
|
|
2156
|
-
|
|
2157
|
-
/**
|
|
2158
|
-
* Tags stripped and the escaping undone, so what goes in comes back out.
|
|
2159
|
-
*
|
|
2160
|
-
* Reading back what `textContent` had just written returned `<b>` for `<b>` — the setter
|
|
2161
|
-
* escapes and the getter did not decode, so the round trip a component may reasonably rely on was
|
|
2162
|
-
* broken. Only the numeric references the setter emits need undoing; anything else in there came
|
|
2163
|
-
* from author markup and is text as written.
|
|
2164
|
-
*/
|
|
2165
|
-
get textContent() {
|
|
2166
|
-
return textOf(this);
|
|
2167
|
-
}
|
|
2168
|
-
/**
|
|
2169
|
-
* Escaped for an ordinary element, stored as-is for a raw-text one.
|
|
2170
|
-
*
|
|
2171
|
-
* `@verajs/styles` sets a `<style>` element's `textContent` to the stylesheet, and escaping it
|
|
2172
|
-
* turned every `>` into `>` and every `"` into `"`. A browser does not decode those inside
|
|
2173
|
-
* `<style>`, so a component with `static styles = '.a > .b { … }'` shipped a broken stylesheet —
|
|
2174
|
-
* every child selector, every attribute selector, every `content: "…"`. The client never had it:
|
|
2175
|
-
* there, `textContent` sets real text and the serializer of a raw-text element emits it verbatim.
|
|
2176
|
-
*/
|
|
2177
|
-
/**
|
|
2178
|
-
* **`null` and `undefined` are the empty string, not their own names.** `textContent` is a
|
|
2179
|
-
* nullable `DOMString` with `[LegacyNullToEmptyString]`, and WebIDL converts `undefined` to `null`
|
|
2180
|
-
* for a nullable type — so both erase the content in every engine. This wrote the text `null`,
|
|
2181
|
-
* which meant `this.textContent = maybeMissing` put the word on the page server-side and nothing
|
|
2182
|
-
* client-side: a hydration mismatch produced by ordinary defensive code.
|
|
2183
|
-
*/
|
|
2184
|
-
set textContent(value) {
|
|
2185
|
-
const text = value == null ? '' : value;
|
|
2186
|
-
this.innerHTML = RAW_TEXT_ELEMENTS.has(this.localName) ? String(text) : escapeHtml(text);
|
|
2187
|
-
}
|
|
2188
|
-
}
|
|
2189
|
-
|
|
2190
|
-
/**
|
|
2191
|
-
* `document.createElement('my-comp')` builds **the component**, not a blank element.
|
|
2192
|
-
*
|
|
2193
|
-
* A browser upgrades a known tag as it creates it, so the class's constructor runs and its field
|
|
2194
|
-
* initialisers are in place before anyone touches the element. This returned a bare `ElementShim`,
|
|
2195
|
-
* so a component created imperatively had none of its own state, and `instanceof` said no.
|
|
2196
|
-
*/
|
|
2197
|
-
/**
|
|
2198
|
-
* A fresh element carrying the properties its tag actually has — `input.disabled`, `a.href` — as
|
|
2199
|
-
* well as the ones every element shares. See `reflections.js`; without this an assignment to an
|
|
2200
|
-
* element-specific property became a plain JavaScript property that reached no markup at all.
|
|
2201
|
-
*
|
|
2202
|
-
* Only HTML elements get one. An SVG element's properties are a different interface again, and
|
|
2203
|
-
* guessing at them is what this whole file exists not to do.
|
|
2204
|
-
*/
|
|
2205
|
-
const build = (name, namespaceURI = HTML_NS) => {
|
|
2206
|
-
const Element = namespaceURI === HTML_NS ? interfaceFor(name, ElementShim) : ElementShim;
|
|
2207
|
-
return new Element(name, namespaceURI);
|
|
2208
|
-
};
|
|
2209
|
-
|
|
2210
|
-
export const createElement = (localName, namespaceURI = HTML_NS) => {
|
|
2211
|
-
const name = namespaceURI === HTML_NS ? `${localName}`.toLowerCase() : `${localName}`;
|
|
2212
|
-
/**
|
|
2213
|
-
* **A tag name that cannot be written is refused**, as it is in every engine. Accepting one meant
|
|
2214
|
-
* `createElement('a b')` serialized `<a b>` — an element `a` with an empty attribute `b` once a
|
|
2215
|
-
* parser sees it — and `createElement('')` produced `<>`. Both render markup no browser would
|
|
2216
|
-
* have produced from the same call, which is the one thing this DOM exists not to do.
|
|
2217
|
-
*/
|
|
2218
|
-
if (name === '' || UNUSABLE_IN_A_TAG.test(name))
|
|
2219
|
-
throw new DOMException(
|
|
2220
|
-
`Failed to execute 'createElement' on 'Document': The tag name provided ('${name}') is not a valid name.`,
|
|
2221
|
-
'InvalidCharacterError'
|
|
2222
|
-
);
|
|
2223
|
-
const Component = registry.get(name);
|
|
2224
|
-
if (!Component) return build(name, namespaceURI);
|
|
2225
|
-
const element = new Component();
|
|
2226
|
-
element.localName = name;
|
|
2227
|
-
element._ns = namespaceURI;
|
|
2228
|
-
return element;
|
|
2229
|
-
};
|
|
2230
|
-
|
|
2231
|
-
/**
|
|
2232
|
-
* Instances handed to `appendChild` before they have rendered, so the scan renders **them** rather
|
|
2233
|
-
* than a fresh copy built from their markup.
|
|
2234
|
-
*
|
|
2235
|
-
* The nested-component scan reads emitted markup and instantiates what it finds, which is right for
|
|
2236
|
-
* a tag written in a template and wrong for an element the component built itself: everything
|
|
2237
|
-
* assigned to it — `kid.rows = data`, the ordinary way to hand structured data to a child — was
|
|
2238
|
-
* lost, because an attribute cannot carry it and a fresh instance never saw it. The marker attribute
|
|
2239
|
-
* is the handle, and `renderComponent` removes it as it renders.
|
|
2240
|
-
*/
|
|
2241
|
-
export const pendingInstances = new Map();
|
|
2242
|
-
|
|
2243
|
-
/**
|
|
2244
|
-
* The marker's **name** carries a per-process random token, because the markup it is written into is
|
|
2245
|
-
* not all ours.
|
|
2246
|
-
*
|
|
2247
|
-
* `children` and the string form of `attributes` are raw markup by design, so a caller passing
|
|
2248
|
-
* request data through either hands an attacker a way to write attributes into this document. With a
|
|
2249
|
-
* fixed name, injected markup could claim a component's prepared instance — and win, because it is
|
|
2250
|
-
* already in the markup when the real one is appended, and the scan reads in order. Demonstrated: an
|
|
2251
|
-
* injected `<x-y vera-ssr-instance="1">` rendered with the parent's data while the parent's own
|
|
2252
|
-
* child fell back to defaults. A name nothing outside this module can guess closes it, and
|
|
2253
|
-
* `renderComponent` also refuses a marker whose instance is not the tag being rendered.
|
|
2254
|
-
*
|
|
2255
|
-
* Import-time rather than per-render: an attacker cannot read either, and the output never contains
|
|
2256
|
-
* the marker, so nothing observable depends on it.
|
|
2257
|
-
*/
|
|
2258
|
-
export const INSTANCE_ATTRIBUTE = `vera-ssr-${crypto.randomUUID()}`;
|
|
2259
|
-
|
|
2260
|
-
let instanceCount = 0;
|
|
2261
|
-
|
|
2262
|
-
/**
|
|
2263
|
-
* Marks `node` as a prepared instance and returns its id, so the nested-component scan renders THIS
|
|
2264
|
-
* instance rather than a fresh copy built from its markup. Two callers, one discipline:
|
|
2265
|
-
* `appendChild` below, for an element a component built itself, and the template serializer, for a
|
|
2266
|
-
* component tag whose property bindings it is delivering — both stamp the node (so `prepareInstance`
|
|
2267
|
-
* can unregister it by the attribute) and both rely on the unguessable name above.
|
|
2268
|
-
*
|
|
2269
|
-
* @param {any} node @returns {string} the instance id, for the caller that writes markup itself
|
|
2270
|
-
*/
|
|
2271
|
-
export const markPending = (node) => {
|
|
2272
|
-
const id = String(++instanceCount);
|
|
2273
|
-
node.setAttribute(INSTANCE_ATTRIBUTE, id);
|
|
2274
|
-
pendingInstances.set(id, node);
|
|
2275
|
-
return id;
|
|
2276
|
-
};
|
|
2277
|
-
|
|
2278
|
-
/** One `ElementInternals` per element, as `attachInternals` guarantees. */
|
|
2279
|
-
const internals = new WeakMap();
|
|
2280
|
-
|
|
2281
|
-
/**
|
|
2282
|
-
* Installed once, here, because both are properties of *every* node of their kind rather than of any
|
|
2283
|
-
* one of them — and generating them beats sixty hand-written accessors that go stale.
|
|
2284
|
-
*/
|
|
2285
|
-
defineReflections(ElementShim);
|
|
2286
|
-
/** `Node`'s constants are on every node, so they go on the shared base. */
|
|
2287
|
-
Object.assign(ContainerShim.prototype, NODE_CONSTANTS);
|
|
2288
|
-
/**
|
|
2289
|
-
* **And on the interface object itself.** WebIDL puts a constant on both the prototype and the
|
|
2290
|
-
* constructor, so `node.nodeType === Node.TEXT_NODE` is the ordinary spelling — and `Node` is this
|
|
2291
|
-
* class (`shim.js` assigns it). The constants reached instances and not the constructor, so that
|
|
2292
|
-
* comparison was against `undefined` and quietly false for every node.
|
|
2293
|
-
*/
|
|
2294
|
-
Object.assign(ContainerShim, NODE_CONSTANTS);
|