@verajs/ssr 0.2.0 → 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.
Files changed (63) hide show
  1. package/README.md +510 -373
  2. package/dist/vera/diagnostics.d.ts +20 -0
  3. package/dist/vera/diagnostics.js +98 -0
  4. package/dist/vera/escaping.d.ts +142 -0
  5. package/dist/vera/escaping.js +267 -0
  6. package/dist/vera/events.d.ts +41 -0
  7. package/dist/vera/events.js +162 -0
  8. package/dist/vera/frames.d.ts +38 -0
  9. package/dist/vera/frames.js +195 -0
  10. package/dist/vera/index.d.ts +48 -0
  11. package/dist/vera/index.js +480 -0
  12. package/{types → dist/vera}/nodes.d.ts +397 -300
  13. package/dist/vera/nodes.js +2322 -0
  14. package/dist/vera/parse.d.ts +37 -0
  15. package/dist/vera/parse.js +390 -0
  16. package/dist/vera/reflections.d.ts +566 -0
  17. package/dist/vera/reflections.js +820 -0
  18. package/dist/vera/registry.d.ts +14 -0
  19. package/{src → dist}/vera/registry.js +5 -1
  20. package/dist/vera/report.d.ts +27 -0
  21. package/dist/vera/report.js +56 -0
  22. package/dist/vera/scan.d.ts +37 -0
  23. package/dist/vera/scan.js +143 -0
  24. package/dist/vera/select.d.ts +40 -0
  25. package/dist/vera/select.js +261 -0
  26. package/dist/vera/serializer.d.ts +39 -0
  27. package/dist/vera/serializer.js +1372 -0
  28. package/dist/vera/shim.d.ts +42 -0
  29. package/dist/vera/shim.js +853 -0
  30. package/dist/vera/slots.d.ts +5 -0
  31. package/dist/vera/slots.js +104 -0
  32. package/dist/vera/stylesheets.d.ts +62 -0
  33. package/dist/vera/stylesheets.js +180 -0
  34. package/dist/vera/tokenizer.d.ts +32 -0
  35. package/dist/vera/tokenizer.js +528 -0
  36. package/dist/vera/types.d.ts +117 -0
  37. package/dist/vera/views.d.ts +63 -0
  38. package/dist/vera/views.js +245 -0
  39. package/package.json +12 -13
  40. package/src/vera/escaping.js +0 -135
  41. package/src/vera/events.js +0 -164
  42. package/src/vera/frames.js +0 -102
  43. package/src/vera/index.js +0 -1171
  44. package/src/vera/nodes.js +0 -2270
  45. package/src/vera/parse.js +0 -381
  46. package/src/vera/reflections.js +0 -811
  47. package/src/vera/select.js +0 -234
  48. package/src/vera/serializer.js +0 -928
  49. package/src/vera/shim.js +0 -761
  50. package/src/vera/stylesheets.js +0 -155
  51. package/src/vera/views.js +0 -240
  52. package/types/escaping.d.ts +0 -27
  53. package/types/events.d.ts +0 -4
  54. package/types/frames.d.ts +0 -12
  55. package/types/index.d.ts +0 -13
  56. package/types/parse.d.ts +0 -19
  57. package/types/reflections.d.ts +0 -657
  58. package/types/registry.d.ts +0 -8
  59. package/types/select.d.ts +0 -5
  60. package/types/serializer.d.ts +0 -26
  61. package/types/shim.d.ts +0 -23
  62. package/types/stylesheets.d.ts +0 -53
  63. package/types/views.d.ts +0 -26
@@ -0,0 +1,528 @@
1
+ /**
2
+ * Where a static leaves us: inside a tag, and if so, inside an attribute **value**.
3
+ *
4
+ * The two are different questions and only the second separates `<input ${ref} />` from
5
+ * `<b class="a ${x} c">`. Both are values inside a tag; only the first is an *element position*,
6
+ * where the renderer hands the element to a ref or a spread and there is no markup to write. The
7
+ * second is text between two halves of an attribute the statics already carry.
8
+ *
9
+ * A tail test cannot answer it, because the quote that opened the value may be several statics
10
+ * back — `class="a ` ends in a space and is still inside a value. So the state is carried, one
11
+ * character at a time, and only at compile time: this runs once per template, ever.
12
+ */
13
+ /**
14
+ * The two **RAWTEXT** elements. A browser does not decode a character reference inside either, so
15
+ * escaping their content does not protect anything and does corrupt it — `<style>` gets `.a &#62; .b`
16
+ * for `.a > .b`, a selector that matches nothing, and a `<script>` gets broken source.
17
+ *
18
+ * `<title>` and `<textarea>` are **RCDATA**, not RAWTEXT: references *are* decoded there, so those
19
+ * two keep ordinary escaping, which is also what the client produces. They are deliberately not in
20
+ * this set.
21
+ */
22
+ const RAWTEXT = new Set(['style', 'script']);
23
+ /**
24
+ * Every other element whose content the BROWSER never reads as markup: they keep ordinary escaping — the one
25
+ * direction that is always safe — but nothing inside them is a tag, so a `<style>` written inside one is text.
26
+ *
27
+ * **The proof the raw path rests on:** this scanner writes a value UNESCAPED only inside a `<style>`/`<script>` it
28
+ * saw open at foreign depth 0, and every other doubt resolves to escaping. Reading raw text as markup over-escapes —
29
+ * a visible difference, never an injection; reading markup as raw text writes a value's `<img onerror>` into the
30
+ * page. So the set has to hold everything the parser treats as raw: the first three are the client's `RAW_TEXT_TAGS`;
31
+ * `xmp`, `noembed`, `noframes` and `plaintext` are obsolete and the client does not list them, but the browser
32
+ * still parses them as raw text — without them, `<xmp><style>${x}</style></xmp>` wrote `x` raw inside what the
33
+ * browser reads as one run of text, and `</xmp>` in the value closed it. `plaintext` never closes at all; ending it
34
+ * at `</plaintext` here only ever resumes escaping too early, which writes `<style>` content raw into text the
35
+ * browser shows verbatim — still text.
36
+ *
37
+ * `<noscript>` — the client's fourth — is not here, because it is not always raw: a browser with scripting OFF, the
38
+ * one that shows it, parses its content as markup, so `<noscript><a href="${url}">` must be URL-checked like any
39
+ * other link. It is a depth instead, beside foreign content (see `scanTag`): read as markup, with raw text never
40
+ * recognized inside, so every value in it is escaped — which is safe under both parses.
41
+ */
42
+ const TEXT_ONLY = new Set(['textarea', 'title', 'iframe', 'xmp', 'noembed', 'noframes', 'plaintext']);
43
+ /**
44
+ * **The HTML integration points: foreign elements whose content the parser reads as HTML.** In SVG, `foreignObject`,
45
+ * `desc` and `title`; in MathML, the text integration points `mi`, `mo`, `mn`, `ms` and `mtext`, and an
46
+ * `annotation-xml` whose `encoding` is `text/html` or `application/xhtml+xml` (ASCII case-insensitive). Each is one
47
+ * ONLY in its own namespace — `<math><foreignObject>` is a MathML element and its content MathML — and only when not
48
+ * self-closed, since foreign content honors `/>`. Inside a MathML text integration point, `mglyph` and `malignmark`
49
+ * are the exception: the dispatcher keeps them foreign, so their content is MathML again.
50
+ */
51
+ const SVG_POINTS = new Set(['foreignobject', 'desc', 'title']);
52
+ const MATH_TEXT_POINTS = new Set(['mi', 'mo', 'mn', 'ms', 'mtext']);
53
+ const HTML_ENCODINGS = new Set(['text/html', 'application/xhtml+xml']);
54
+ /**
55
+ * **The breakout tags: start tags that END foreign content** — the parser pops out of `<svg>`/`<math>` to the nearest
56
+ * HTML element or integration point and reads the tag as HTML (`<svg><p><style>` is an HTML style, its content raw
57
+ * text). Exactly the standard's list; `font` breaks out only with a `color`, `face` or `size` attribute, which this
58
+ * scan does not track, so `font` never breaks out here — the safe side. `</p>` and `</br>` are the two end tags that do.
59
+ */
60
+ /** The elements that change how what follows them parses, when read in HTML — see the fast path in `scanTag`. */
61
+ const STRUCTURAL = new Set(['svg', 'math', 'noscript', 'template', ...RAWTEXT, ...TEXT_ONLY]);
62
+ const BREAKOUT = new Set([
63
+ 'b', 'big', 'blockquote', 'body', 'br', 'center', 'code', 'dd', 'div', 'dl', 'dt', 'em', 'embed', 'h1', 'h2', 'h3', 'h4',
64
+ 'h5', 'h6', 'head', 'hr', 'i', 'img', 'li', 'listing', 'menu', 'meta', 'nobr', 'ol', 'p', 'pre', 'ruby', 's', 'small',
65
+ 'span', 'strong', 'strike', 'sub', 'sup', 'table', 'tt', 'u', 'ul', 'var',
66
+ ]);
67
+ /**
68
+ * **Where a tag scan stands: the HTML tokenizer's own states**, from a `<` to its tag's `>`, so this scanner reads a
69
+ * tag exactly where the browser does. It used to approximate them — a name stopped at the first character outside
70
+ * `[a-zA-Z0-9-]`, any `<` opened a tag, any `=` opened a value, a quote anywhere opened one — and every difference
71
+ * was a place where the two disagreed about what is markup. `<script.x>` read as a `<script>` (the browser reads an
72
+ * unknown element named `script.x`), so a hole inside it was written raw and a value's `<img onerror>` ran; `<b x">`
73
+ * read the quote as opening a value (the tokenizer reads it as part of the name `x"`), so the `.innerHTML` scan that
74
+ * shared none of this missed the live `<script>` after it. Each state below is the tokenizer's, under its name in the
75
+ * HTML standard; the value states are `inValue`/`quote`.
76
+ */
77
+ const OUTSIDE = 0;
78
+ const TAG_OPEN = 1;
79
+ const END_TAG_OPEN = 2;
80
+ const TAG_NAME = 3;
81
+ const BEFORE_ATTRIBUTE_NAME = 4;
82
+ const ATTRIBUTE_NAME = 5;
83
+ const AFTER_ATTRIBUTE_NAME = 6;
84
+ const AFTER_ATTRIBUTE_VALUE = 7;
85
+ const SELF_CLOSING = 8;
86
+ /** The tokenizer's whitespace — never `\s`, which also takes `\v` and U+00A0, both NAME characters to a tokenizer. */
87
+ const isSpace = (c) => c === ' ' || c === '\n' || c === '\t' || c === '\f' || c === '\r';
88
+ const isAlpha = (c) => c !== undefined && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
89
+ /** The tokenizer lowercases ASCII only; `toLowerCase` also folds the Kelvin sign into `k`. */
90
+ const lowerAscii = (c) => (c >= 'A' && c <= 'Z' ? String.fromCharCode(c.charCodeAt(0) + 32) : c);
91
+ /** What may follow `</name` for it to END a raw-text or text-only element: the tokenizer's "appropriate end tag". */
92
+ const endsName = (c) => isSpace(c) || c === '/' || c === '>';
93
+ /** What `scanTag` reports to `fortify`, as (position, edit) pairs — see `fortify`. */
94
+ const EDIT_INERT = 0;
95
+ const EDIT_SHADOW_ROOT = 1;
96
+ /**
97
+ * The entry an end tag named `name` closes, or `-1`. The search stops at an open `<template>` — inside its content an
98
+ * end tag closes nothing outside it — and an end tag this scan never opened closes nothing at all. It used to lower
99
+ * the depth for `</svg>`, `</math>` and `</noscript>`, guessing it closed an element a parent template opened, but the
100
+ * parser closes only what is actually open: inside `<math>` a `</svg>` is ignored, so `<math></svg><style>${v}` read
101
+ * the style as HTML raw text while the browser parsed MathML markup, and a value's `<img onerror>` ran. Keeping the
102
+ * depth only ever over-escapes.
103
+ */
104
+ const closeTo = (opens, name) => {
105
+ let at = opens.length - 1;
106
+ while (at >= 0 && opens[at].name !== name && opens[at].name !== 'template')
107
+ at--;
108
+ return at >= 0 && opens[at].name === name ? at : -1;
109
+ };
110
+ /**
111
+ * Pops `opens` to the element a breakout lands in and answers the foreign depth there, or `-1` — leaving `opens` alone
112
+ * — when that is not certain (see the breakout in `scanTag`). It runs only at a breakout, so it sits outside the scan.
113
+ */
114
+ const breakOut = (opens) => {
115
+ let at = opens.length - 1;
116
+ while (at >= 0 && (opens[at].space === 'svg' || opens[at].space === 'math'))
117
+ at--;
118
+ if (!(at >= 0 ? opens[at].space === 'html' : opens.length > 0 && opens[0].foreign === 0))
119
+ return -1;
120
+ const depth = opens[at + 1].foreign;
121
+ opens.length = at + 1;
122
+ return depth;
123
+ };
124
+ /**
125
+ * `edits`, when given, collects the positions `fortify` rewrites — a start tag's name end where it is `script`, and
126
+ * a `<template>` start tag's `shadowroot`/`shadowrootmode` attribute names. They are reported from THIS scan, so the
127
+ * `.innerHTML` scan and the template scan cannot disagree about what is a tag: they are one scan. `tags`, when given,
128
+ * collects the component scan's candidates the same way (see `ScannedTag` and `scan.ts`).
129
+ */
130
+ export const scanTag = (text, state, edits, tags) => {
131
+ let { phase, inValue, quote, rawTag, tagName, attrName, serial, closing, inert, comment, textTag, foreign, attrRaw, encoding } = state;
132
+ /** The parser-state elements THIS template has open, innermost last — what its end must close (see `compile`). Mutated. */
133
+ const { opens } = state;
134
+ /** Where, in THIS text, the last attribute to open here starts (its leading space) and its value starts — see `compile`. */
135
+ let opened = false;
136
+ let attrStart = 0;
137
+ let valueStart = 0;
138
+ let nameAt = 0;
139
+ /** Where the attribute name being read began in THIS text — `-1` when it began in an earlier one. */
140
+ let nameFrom = -1;
141
+ let tagAt = -1;
142
+ let selfClosing = false;
143
+ /** Where the value that opened in THIS text starts — `-1` when none did — so an `encoding` can be read whole. */
144
+ let valueFrom = -1;
145
+ const length = text.length;
146
+ for (let i = 0; i < length; i++) {
147
+ /**
148
+ * Inside `<style>`/`<script>` (`rawTag`) or a text-only element (`textTag`) nothing is markup until that
149
+ * element's own end tag — `</name` followed by whitespace, `/` or `>`, so `</scripts` ends nothing — and a hole
150
+ * here is raw text or escaped text, which the render pass has to know about. The end tag is then read as any end
151
+ * tag is, attributes and all. At the very end of a text, `</name` counts as an end: a hole right after it is
152
+ * refused as a tag-name hole (see `compile`), never written into the raw text, where a value of `>` would end it.
153
+ */
154
+ if (rawTag !== '' || textTag !== '') {
155
+ const name = rawTag || textTag;
156
+ const at = text.indexOf('</', i);
157
+ if (at === -1)
158
+ break;
159
+ const after = at + 2 + name.length;
160
+ if (text.slice(at + 2, after).toLowerCase() !== name || (after < length && !endsName(text[after]))) {
161
+ i = at + 1;
162
+ continue;
163
+ }
164
+ rawTag = '';
165
+ textTag = '';
166
+ phase = TAG_NAME;
167
+ tagName = name;
168
+ closing = true;
169
+ tagAt = at;
170
+ i = after - 1;
171
+ continue;
172
+ }
173
+ /**
174
+ * Inside a comment nothing is markup until its closer — the client's scanner reads it the same way, and a binding
175
+ * here is dropped on both sides (see `compile`). A comment ends at `-->` or `--!>`; a BOGUS comment — `<!` not
176
+ * followed by `--`, `<?`, or `</` not followed by a letter — at the first `>`, quotes or not.
177
+ */
178
+ if (comment !== '') {
179
+ if (comment === '>') {
180
+ const at = text.indexOf('>', i);
181
+ if (at === -1)
182
+ break;
183
+ i = at;
184
+ comment = '';
185
+ continue;
186
+ }
187
+ const at = text.indexOf('--', i);
188
+ if (at === -1)
189
+ break;
190
+ if (text.startsWith('-->', at)) {
191
+ i = at + 2;
192
+ comment = '';
193
+ }
194
+ else if (text.startsWith('--!>', at)) {
195
+ i = at + 3;
196
+ comment = '';
197
+ }
198
+ else
199
+ i = at;
200
+ continue;
201
+ }
202
+ const character = text[i];
203
+ /** Whether this character's state hands over to an attribute value, and whether it ends the tag. */
204
+ let value = false;
205
+ let emit = false;
206
+ if (inValue) {
207
+ if (quote !== '') {
208
+ const at = text.indexOf(quote, i);
209
+ if (at === -1)
210
+ break;
211
+ if (attrName === 'encoding' && valueFrom !== -1 && encoding === '')
212
+ encoding = text.slice(valueFrom, at);
213
+ i = at;
214
+ inValue = false;
215
+ quote = '';
216
+ phase = AFTER_ATTRIBUTE_VALUE;
217
+ continue;
218
+ }
219
+ /** An unquoted value ends at whitespace or the tag's own `>`; a quote or `=` inside it is value text. */
220
+ if ((character === '>' || isSpace(character)) && attrName === 'encoding' && valueFrom !== -1 && encoding === '')
221
+ encoding = text.slice(valueFrom, i);
222
+ if (character === '>') {
223
+ inValue = false;
224
+ emit = true;
225
+ }
226
+ else if (isSpace(character)) {
227
+ inValue = false;
228
+ phase = BEFORE_ATTRIBUTE_NAME;
229
+ continue;
230
+ }
231
+ else if (character === '/' && i === 0 && text[1] === '>') {
232
+ /**
233
+ * A `/>` RIGHT after a hole that ended an unquoted value is the tag's self-close, not value text — `compile`
234
+ * keeps the slash out of the value, as the client's scanner does (`<circle r=${r}/>`), so the markup served is
235
+ * a self-closing tag and is scanned as one.
236
+ */
237
+ inValue = false;
238
+ phase = SELF_CLOSING;
239
+ continue;
240
+ }
241
+ else
242
+ continue;
243
+ }
244
+ else if (phase === OUTSIDE) {
245
+ const at = text.indexOf('<', i);
246
+ if (at === -1)
247
+ break;
248
+ i = at;
249
+ tagAt = at;
250
+ phase = TAG_OPEN;
251
+ continue;
252
+ }
253
+ else if (phase === TAG_OPEN) {
254
+ if (character === '!') {
255
+ if (text.startsWith('--', i + 1)) {
256
+ i += 2;
257
+ /** `<!-->` and `<!--->` end the comment they open — the tokenizer's abrupt close. */
258
+ if (text[i + 1] === '>')
259
+ i += 1;
260
+ else if (text.startsWith('->', i + 1))
261
+ i += 2;
262
+ else
263
+ comment = '-->';
264
+ }
265
+ else
266
+ comment = '>';
267
+ phase = OUTSIDE;
268
+ continue;
269
+ }
270
+ if (character === '/') {
271
+ phase = END_TAG_OPEN;
272
+ continue;
273
+ }
274
+ if (isAlpha(character)) {
275
+ phase = TAG_NAME;
276
+ tagName = lowerAscii(character);
277
+ closing = false;
278
+ continue;
279
+ }
280
+ /** `<?` opens a bogus comment; a `<` before anything else is TEXT — `a < b`, `<1`. */
281
+ if (character === '?')
282
+ comment = '>';
283
+ else
284
+ i--;
285
+ phase = OUTSIDE;
286
+ continue;
287
+ }
288
+ else if (phase === END_TAG_OPEN) {
289
+ if (isAlpha(character)) {
290
+ phase = TAG_NAME;
291
+ tagName = lowerAscii(character);
292
+ closing = true;
293
+ continue;
294
+ }
295
+ /** `</>` is nothing at all; `</` before anything else opens a bogus comment that holds that character. */
296
+ if (character !== '>') {
297
+ comment = '>';
298
+ i--;
299
+ }
300
+ phase = OUTSIDE;
301
+ continue;
302
+ }
303
+ else if (phase === TAG_NAME) {
304
+ /** A tag name runs to whitespace, `/` or `>` — every other character is part of it, `.` `:` `=` and quotes too. */
305
+ if (isSpace(character))
306
+ phase = BEFORE_ATTRIBUTE_NAME;
307
+ else if (character === '/')
308
+ phase = SELF_CLOSING;
309
+ else if (character === '>')
310
+ emit = true;
311
+ else {
312
+ tagName += lowerAscii(character);
313
+ continue;
314
+ }
315
+ /** The name is whole: a `<script>` start tag gets the inert type right after it, first among its attributes. */
316
+ if (edits !== undefined && !closing && tagName === 'script')
317
+ edits.push(i, EDIT_INERT);
318
+ if (!emit)
319
+ continue;
320
+ }
321
+ else if (phase === BEFORE_ATTRIBUTE_NAME || phase === AFTER_ATTRIBUTE_NAME || phase === AFTER_ATTRIBUTE_VALUE) {
322
+ if (isSpace(character)) {
323
+ phase = BEFORE_ATTRIBUTE_NAME;
324
+ continue;
325
+ }
326
+ if (character === '/') {
327
+ phase = SELF_CLOSING;
328
+ continue;
329
+ }
330
+ if (character === '>')
331
+ emit = true;
332
+ /** `=` after a name opens its value; anywhere else — `<b =x>` — it is the first character of a NAME. */
333
+ else if (character === '=' && phase === AFTER_ATTRIBUTE_NAME)
334
+ value = true;
335
+ else {
336
+ phase = ATTRIBUTE_NAME;
337
+ nameFrom = i;
338
+ continue;
339
+ }
340
+ }
341
+ else if (phase === ATTRIBUTE_NAME) {
342
+ /** A name runs to whitespace, `/`, `>` or `=` — a quote or `<` inside it is part of the name (`<b x">`). */
343
+ if (!(isSpace(character) || character === '/' || character === '>' || character === '='))
344
+ continue;
345
+ attrRaw = nameFrom === -1 ? attrRaw + text.slice(0, i) : text.slice(nameFrom, i);
346
+ if (edits !== undefined && !closing && tagName === 'template' && nameFrom !== -1) {
347
+ const lower = attrRaw.toLowerCase();
348
+ if (lower === 'shadowrootmode' || lower === 'shadowroot')
349
+ edits.push(nameFrom, EDIT_SHADOW_ROOT);
350
+ }
351
+ if (character === '=')
352
+ value = true;
353
+ else if (character === '>')
354
+ emit = true;
355
+ else {
356
+ phase = character === '/' ? SELF_CLOSING : AFTER_ATTRIBUTE_NAME;
357
+ continue;
358
+ }
359
+ }
360
+ else {
361
+ /** `SELF_CLOSING`: only a `>` makes the tag self-closing; anything else starts an attribute name. */
362
+ if (character === '>') {
363
+ emit = true;
364
+ selfClosing = true;
365
+ }
366
+ else {
367
+ phase = BEFORE_ATTRIBUTE_NAME;
368
+ i--;
369
+ continue;
370
+ }
371
+ }
372
+ if (value) {
373
+ /** Which attribute this value belongs to, and a serial per value — the URL-sink check needs both. */
374
+ attrName = attrRaw.toLowerCase();
375
+ serial++;
376
+ let next = i + 1;
377
+ while (next < length && isSpace(text[next]))
378
+ next++;
379
+ quote = text[next] === '"' || text[next] === "'" ? text[next] : '';
380
+ inValue = true;
381
+ /** The value states are `inValue`/`quote`; every way out of them names the phase that follows. */
382
+ phase = BEFORE_ATTRIBUTE_NAME;
383
+ opened = true;
384
+ nameAt = nameFrom === -1 ? 0 : nameFrom;
385
+ attrStart = nameAt;
386
+ while (attrStart > 0 && isSpace(text[attrStart - 1]))
387
+ attrStart--;
388
+ valueStart = quote ? next + 1 : next;
389
+ valueFrom = valueStart;
390
+ i = quote ? next : next - 1;
391
+ continue;
392
+ }
393
+ /** `emit`: the tag is whole. */
394
+ phase = OUTSIDE;
395
+ /** A tag the component scan wants — see `ScannedTag`. A dashed name never changes parse state, so where it is read is where it sits. */
396
+ if (tags !== undefined && inert === 0 && tagName.includes('-'))
397
+ tags.push({ at: tagAt, end: i + 1, name: tagName, closing, live: !closing && foreign === 0 });
398
+ /**
399
+ * **Most tags change nothing about how what follows parses.** In HTML with nothing open, only the elements in
400
+ * `STRUCTURAL` do, so every other tag — the `<p>`, `<a>` and `<li>` that are nearly all of real markup — skips the
401
+ * stack, namespace and breakout work below.
402
+ */
403
+ if (foreign === 0 && opens.length === 0 && !STRUCTURAL.has(tagName)) {
404
+ tagName = '';
405
+ closing = false;
406
+ selfClosing = false;
407
+ encoding = '';
408
+ continue;
409
+ }
410
+ /** The innermost element this scan opened that changes parsing, and the namespace this tag is read in. */
411
+ let top = opens.length === 0 ? undefined : opens[opens.length - 1];
412
+ let space = foreign === 0 ? 'html' : top !== undefined && top.space !== 'html' ? top.space : '';
413
+ /**
414
+ * **A breakout** — see `BREAKOUT`. The ONE rule here that moves the scan from foreign content toward HTML, the
415
+ * dangerous direction, so it acts only where its target is certain: inside SVG or MathML content (never at an
416
+ * integration point, where these tags are HTML already), popping to the nearest element whose content is HTML. It
417
+ * stops — and changes nothing — at `<noscript>` or anywhere the namespace is unknown, and at the template's own
418
+ * root unless that root is HTML: a template that inherited its depth cannot know what is outside it.
419
+ */
420
+ if ((space === 'svg' || space === 'math') && (closing ? tagName === 'p' || tagName === 'br' : BREAKOUT.has(tagName))) {
421
+ const depth = breakOut(opens);
422
+ if (depth !== -1) {
423
+ foreign = depth;
424
+ top = opens.length === 0 ? undefined : opens[opens.length - 1];
425
+ space = 'html';
426
+ }
427
+ }
428
+ /** `mglyph`/`malignmark` straight inside a MathML text integration point: read as MathML, not HTML. */
429
+ const reentry = space === 'html' && top !== undefined && MATH_TEXT_POINTS.has(top.name) && (tagName === 'mglyph' || tagName === 'malignmark');
430
+ /**
431
+ * A `/` before the `>` closes only a FOREIGN element: on an HTML one the parser ignores it, so `<template/>`,
432
+ * `<noscript/>` and `<style/>` open exactly as `<template>`, `<noscript>` and `<style>` do. Counting them as closed
433
+ * had the server writing raw text into what the browser parses as `<noscript>` content. `<svg/>` and `<math/>` are
434
+ * foreign elements even in HTML, so their `/>` closes them; inside foreign content every `/>` does.
435
+ */
436
+ const selfClosed = selfClosing && (space !== 'html' || reentry || tagName === 'svg' || tagName === 'math');
437
+ if (closing) {
438
+ /** An end tag closes the innermost element of its name this scan opened, and everything opened inside it. */
439
+ /**
440
+ * Inside `<template>` content an end tag closes nothing outside the template (see `closeTo`). Each entry holds the
441
+ * foreign depth and the template depth from before it opened, and a close restores both — never a count kept
442
+ * by hand, which a close that also closes a template inside it would leave wrong.
443
+ */
444
+ const at = closeTo(opens, tagName);
445
+ if (at !== -1) {
446
+ foreign = opens[at].foreign;
447
+ /** How deep inside nested `<template>` content this is: what it was before the closed element opened. */
448
+ inert = opens[at].inert;
449
+ opens.length = at;
450
+ }
451
+ }
452
+ else if (!selfClosed) {
453
+ /**
454
+ * **Foreign content: raw text exists only for HTML elements.** Inside `<svg>`/`<math>`, `<style>`, `<title>`,
455
+ * `<textarea>` and the rest are SVG/MathML elements whose content is MARKUP — read as raw text there, a hole
456
+ * would be served unquoted, unescaped and unrefused (an attribute hole became a live handler). So raw text is
457
+ * recognized only at depth 0, which an HTML integration point restores for its content (see `SVG_POINTS`).
458
+ * A breakout tag (`<svg><p>`) leaves it (see `BREAKOUT`). Deliberately incomplete on the SAFE side: an end tag
459
+ * the parser ignores keeps the depth, and so does `<font color>` — both only ever read raw text as markup, which
460
+ * over-escapes, never injects. `<noscript>` counts as foreign: markup to a parser with scripting off and raw text
461
+ * to one with it on, so it is scanned as the first and escaped as neither can misread (see `TEXT_ONLY`), and
462
+ * nothing inside it is an integration point. The depth a hole sits at is recorded for the template rendered
463
+ * there, which starts from it (see `serializeTemplate`).
464
+ */
465
+ if (tagName === 'svg' || tagName === 'math') {
466
+ /**
467
+ * An `<svg>`/`<math>` start tag the parser handles by HTML rules — in HTML, or an `<svg>` straight inside
468
+ * `annotation-xml` — opens its own namespace; inside foreign content it is an element of the SURROUNDING one
469
+ * (`<math><svg>` is a MathML element named `svg`, so a `foreignObject` in it is MathML too).
470
+ */
471
+ const own = space === 'html' || (tagName === 'svg' && top !== undefined && top.name === 'annotation-xml');
472
+ opens.push({ name: tagName, foreign, inert, space: own ? tagName : space });
473
+ foreign++;
474
+ }
475
+ else if (tagName === 'noscript') {
476
+ opens.push({ name: tagName, foreign, inert, space: '' });
477
+ foreign++;
478
+ }
479
+ else if (tagName === 'template' && space !== 'svg' && space !== 'math') {
480
+ /** Inert content only for an HTML `<template>`: inside `<svg>`/`<math>` it is a foreign element, its content live markup. */
481
+ opens.push({ name: tagName, foreign, inert, space });
482
+ inert++;
483
+ }
484
+ else if ((space === 'svg' && SVG_POINTS.has(tagName)) ||
485
+ (space === 'math' && (MATH_TEXT_POINTS.has(tagName) || (tagName === 'annotation-xml' && HTML_ENCODINGS.has(encoding.toLowerCase()))))) {
486
+ opens.push({ name: tagName, foreign, inert, space: 'html' });
487
+ foreign = 0;
488
+ }
489
+ else if (space === 'math' && tagName === 'annotation-xml') {
490
+ /** Not an integration point, but an `<svg>` straight inside it is SVG — so it is tracked. */
491
+ opens.push({ name: tagName, foreign, inert, space });
492
+ }
493
+ else if (reentry) {
494
+ opens.push({ name: tagName, foreign, inert, space: 'math' });
495
+ foreign = 1;
496
+ }
497
+ else if (foreign === 0 && RAWTEXT.has(tagName))
498
+ rawTag = tagName;
499
+ else if (foreign === 0 && TEXT_ONLY.has(tagName))
500
+ textTag = tagName;
501
+ }
502
+ tagName = '';
503
+ closing = false;
504
+ selfClosing = false;
505
+ encoding = '';
506
+ }
507
+ /** A name still being read at the end carries on into the next text (a hole there is refused — see `nameHole`). */
508
+ if (phase === ATTRIBUTE_NAME)
509
+ attrRaw = nameFrom === -1 ? attrRaw + text : text.slice(nameFrom);
510
+ return { inTag: phase !== OUTSIDE, phase, inValue, quote, rawTag, tagName, attrName, serial, closing, inert, comment, textTag, foreign, opens, attrRaw, encoding, opened, attrStart, valueStart, nameAt, tagAt };
511
+ };
512
+ /** The end-of-input closers a scan state owes: its open comment or raw-text or text-only element, then every element it opened that changes parsing (svg, math, noscript, template, an integration point), innermost first. Shared by `compile` and `fortify`. */
513
+ export const closersOf = (end) => (end.comment || (end.rawTag ? `</${end.rawTag}>` : end.textTag ? `</${end.textTag}>` : '')) +
514
+ end.opens.reduceRight((closing, open) => closing + `</${open.name}>`, '');
515
+ /** A scan state at foreign depth `depth` and nothing else open — its own `opens`, since `scanTag` mutates it. */
516
+ export const freshScan = (depth) => ({ inTag: false, phase: OUTSIDE, inValue: false, quote: '', rawTag: '', tagName: '', attrName: '', serial: 0, closing: false, inert: 0, comment: '', textTag: '', foreign: depth, opens: [], attrRaw: '', encoding: '' });
517
+ /**
518
+ * What the template compiler reads of this module besides the scan itself — exported as SEPARATE bindings. The scan
519
+ * reads its own constants on every character, and V8 reaches an exported module variable through a cell rather than
520
+ * the module's context: exporting these directly cost compile 4% and a 50 KB \`.innerHTML\` value 11%.
521
+ */
522
+ export const RAW_TEXT_TAGS = RAWTEXT;
523
+ export const TEXT_ONLY_TAGS = TEXT_ONLY;
524
+ export const PHASE_TAG_OPEN = TAG_OPEN;
525
+ export const PHASE_END_TAG_OPEN = END_TAG_OPEN;
526
+ export const PHASE_TAG_NAME = TAG_NAME;
527
+ export const isTokenizerSpace = isSpace;
528
+ export const INERT_EDIT = EDIT_INERT;
@@ -0,0 +1,117 @@
1
+ /**
2
+ * `@verajs/ssr`'s cross-file and public types (CODE-PRINCIPLES §1 Types). It imports nothing from inside this package,
3
+ * so it sits at the root of the package's import graph; a type that NAMES one of this package's runtime classes (the
4
+ * DOM shim's `ElementShim` and the rest) cannot be hoisted above that class, and stays beside it in `nodes.ts`.
5
+ * A type one file uses stays in that file, unexported.
6
+ */
7
+ /**
8
+ * A template value as the serializer receives it: a tagged template literal's result, or data shaped like one. `strings`
9
+ * is `unknown` on purpose — only a literal's strings array, which owns `raw`, makes a template (`isLiteral` in
10
+ * `serializer.ts`); anything else is rendered as the text any object renders as, never as markup.
11
+ */
12
+ export type SsrTemplate = {
13
+ readonly strings: unknown;
14
+ readonly values: readonly unknown[];
15
+ readonly _$litType$?: number;
16
+ };
17
+ /** The custom-element definitions this process has seen, by tag name — filled as component modules execute. */
18
+ export type SsrRegistry = Map<string, CustomElementConstructor>;
19
+ /**
20
+ * The options `renderToString` and `renderToStringAsync` take — each one is documented on `renderToString`. Checked at
21
+ * run time too, so a JavaScript caller passing the wrong kind of value gets a `TypeError` naming the option.
22
+ */
23
+ export type SsrRenderOptions = {
24
+ readonly tag?: string;
25
+ readonly attributes?: string | Record<string, unknown>;
26
+ readonly props?: Record<string, unknown>;
27
+ readonly children?: string;
28
+ readonly seen?: Set<string>;
29
+ readonly base?: string | URL;
30
+ readonly static?: boolean;
31
+ readonly location?: string | URL;
32
+ /**
33
+ * How long `renderToStringAsync` waits on promises a component starts before serving what it has (ms; 2000). `0`
34
+ * waits for nothing — it is not "no limit", which cannot be asked for.
35
+ */
36
+ readonly timeout?: number;
37
+ };
38
+ /**
39
+ * What a render resolves to: the entry element's markup, the hoisted styles this render is responsible for (escaped, for
40
+ * the caller to place in a `<style>`), and the document title the render left set.
41
+ */
42
+ export type SsrRenderResult = {
43
+ html: string;
44
+ styles: string;
45
+ title: string;
46
+ };
47
+ /**
48
+ * **The tag scanner's state** (`tokenizer.ts`), shared by the template compiler and the `.innerHTML` and component
49
+ * scans. Internal to this package: nothing in its public API names these.
50
+ */
51
+ /**
52
+ * An element that changes how what follows it parses, as the scan opened it: its name (what closes it, and what a
53
+ * template's end writes to close it), the foreign and `<template>` depths BEFORE it (restored when it closes), and the namespace its
54
+ * content is read in — `html`, `svg`, `math`, or `''` when unknown (a template that starts at a foreign depth its
55
+ * parent gave it cannot know which namespace that is, so it recognizes no integration point: the safe side).
56
+ */
57
+ export type Open = {
58
+ readonly name: string;
59
+ readonly foreign: number;
60
+ readonly inert: number;
61
+ readonly space: string;
62
+ };
63
+ export type Phase = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
64
+ /** Where the scan stands at the end of a static — carried into the next one, and what a template's end must close. */
65
+ export type ScanState = {
66
+ readonly inTag: boolean;
67
+ /** Which tokenizer state the tag being read is in — `OUTSIDE` when there is none (see the phases above). */
68
+ readonly phase: Phase;
69
+ readonly inValue: boolean;
70
+ readonly quote: string;
71
+ readonly rawTag: string;
72
+ readonly tagName: string;
73
+ readonly attrName: string;
74
+ readonly serial: number;
75
+ readonly closing: boolean;
76
+ readonly inert: number;
77
+ /** The open comment's closer — `-->` for a comment, `>` for a bogus one (`<!x`, `<?x`, `</ x`) — or `''`. */
78
+ readonly comment: string;
79
+ readonly textTag: string;
80
+ readonly foreign: number;
81
+ /** Mutated by `scanTag`, so every fresh state owns its own. */
82
+ readonly opens: Open[];
83
+ /** The `encoding` of the tag being read, while one is — what makes an `annotation-xml` an integration point. */
84
+ readonly encoding: string;
85
+ readonly attrRaw: string;
86
+ };
87
+ /** A state before any static has been scanned: it carries none of the offsets a scan reports about its own text. */
88
+ export interface ScanStart extends ScanState {
89
+ readonly opened?: undefined;
90
+ readonly attrStart?: undefined;
91
+ readonly valueStart?: undefined;
92
+ readonly nameAt?: undefined;
93
+ readonly tagAt?: undefined;
94
+ }
95
+ /** What `scanTag` answers: the state carried on, and where in THIS text the last attribute to open here starts. */
96
+ export interface ScanResult extends ScanState {
97
+ readonly opened: boolean;
98
+ readonly attrStart: number;
99
+ readonly valueStart: number;
100
+ /** Where, in THIS text, the name of the attribute whose value opened here starts — `0` when it began earlier. */
101
+ readonly nameAt: number;
102
+ /** Where, in THIS text, the tag still open at its end began — `-1` when it began in an earlier one, or none is. */
103
+ readonly tagAt: number;
104
+ }
105
+ /**
106
+ * A tag the component scan collects (`scanTag`'s `tags`): its span in the markup, its name as the tokenizer reads it,
107
+ * whether it is an end tag, and whether it is LIVE — a start tag in HTML, where the parser creates an element a custom
108
+ * element definition upgrades (never inside `<svg>`/`<math>` outside an integration point). Only names with a `-`
109
+ * are collected, and none inside `<template>` content.
110
+ */
111
+ export type ScannedTag = {
112
+ readonly at: number;
113
+ readonly end: number;
114
+ readonly name: string;
115
+ readonly closing: boolean;
116
+ readonly live: boolean;
117
+ };