@verajs/ssr 0.2.1 → 0.3.1
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 +522 -390
- package/dist/vera/diagnostics.d.ts +20 -0
- package/dist/vera/diagnostics.js +102 -0
- package/dist/vera/escaping.d.ts +155 -0
- package/dist/vera/escaping.js +293 -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 +2339 -0
- package/dist/vera/parse.d.ts +53 -0
- package/dist/vera/parse.js +485 -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 +1521 -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 +553 -0
- package/dist/vera/types.d.ts +121 -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
|
@@ -0,0 +1,553 @@
|
|
|
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 > .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 (the client's scanner lists them too since 2026-11-19, as
|
|
32
|
+
* never-shown content), and the browser 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
|
+
/**
|
|
143
|
+
* Where, in THIS text, raw content last BEGAN (after its element's start tag) and first ENDED (its end tag's `<`) —
|
|
144
|
+
* `-1` for none: the bounds of a raw element's content inside the static around a hole (`compile`, a JSON block).
|
|
145
|
+
*/
|
|
146
|
+
let rawFrom = -1;
|
|
147
|
+
let rawUntil = -1;
|
|
148
|
+
let selfClosing = false;
|
|
149
|
+
/** Where the value that opened in THIS text starts — `-1` when none did — so an `encoding` can be read whole. */
|
|
150
|
+
let valueFrom = -1;
|
|
151
|
+
const length = text.length;
|
|
152
|
+
for (let i = 0; i < length; i++) {
|
|
153
|
+
/**
|
|
154
|
+
* Inside `<style>`/`<script>` (`rawTag`) or a text-only element (`textTag`) nothing is markup until that
|
|
155
|
+
* element's own end tag — `</name` followed by whitespace, `/` or `>`, so `</scripts` ends nothing — and a hole
|
|
156
|
+
* here is raw text or escaped text, which the render pass has to know about. The end tag is then read as any end
|
|
157
|
+
* tag is, attributes and all. At the very end of a text, `</name` counts as an end: a hole right after it is
|
|
158
|
+
* refused as a tag-name hole (see `compile`), never written into the raw text, where a value of `>` would end it.
|
|
159
|
+
*/
|
|
160
|
+
if (rawTag !== '' || textTag !== '') {
|
|
161
|
+
const name = rawTag || textTag;
|
|
162
|
+
const at = text.indexOf('</', i);
|
|
163
|
+
if (at === -1)
|
|
164
|
+
break;
|
|
165
|
+
const after = at + 2 + name.length;
|
|
166
|
+
if (text.slice(at + 2, after).toLowerCase() !== name || (after < length && !endsName(text[after]))) {
|
|
167
|
+
i = at + 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (rawTag !== '' && rawUntil === -1)
|
|
171
|
+
rawUntil = at;
|
|
172
|
+
rawTag = '';
|
|
173
|
+
textTag = '';
|
|
174
|
+
phase = TAG_NAME;
|
|
175
|
+
tagName = name;
|
|
176
|
+
closing = true;
|
|
177
|
+
tagAt = at;
|
|
178
|
+
i = after - 1;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Inside a comment nothing is markup until its closer — the client's scanner reads it the same way, and a binding
|
|
183
|
+
* here is dropped on both sides (see `compile`). A comment ends at `-->` or `--!>`; a BOGUS comment — `<!` not
|
|
184
|
+
* followed by `--`, `<?`, or `</` not followed by a letter — at the first `>`, quotes or not.
|
|
185
|
+
*/
|
|
186
|
+
if (comment !== '') {
|
|
187
|
+
if (comment === '>') {
|
|
188
|
+
const at = text.indexOf('>', i);
|
|
189
|
+
if (at === -1)
|
|
190
|
+
break;
|
|
191
|
+
i = at;
|
|
192
|
+
comment = '';
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const at = text.indexOf('--', i);
|
|
196
|
+
if (at === -1)
|
|
197
|
+
break;
|
|
198
|
+
if (text.startsWith('-->', at)) {
|
|
199
|
+
i = at + 2;
|
|
200
|
+
comment = '';
|
|
201
|
+
}
|
|
202
|
+
else if (text.startsWith('--!>', at)) {
|
|
203
|
+
i = at + 3;
|
|
204
|
+
comment = '';
|
|
205
|
+
}
|
|
206
|
+
else
|
|
207
|
+
i = at;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
const character = text[i];
|
|
211
|
+
/** Whether this character's state hands over to an attribute value, and whether it ends the tag. */
|
|
212
|
+
let value = false;
|
|
213
|
+
let emit = false;
|
|
214
|
+
if (inValue) {
|
|
215
|
+
if (quote !== '') {
|
|
216
|
+
const at = text.indexOf(quote, i);
|
|
217
|
+
if (at === -1)
|
|
218
|
+
break;
|
|
219
|
+
if (attrName === 'encoding' && valueFrom !== -1 && encoding === '')
|
|
220
|
+
encoding = text.slice(valueFrom, at);
|
|
221
|
+
i = at;
|
|
222
|
+
inValue = false;
|
|
223
|
+
quote = '';
|
|
224
|
+
phase = AFTER_ATTRIBUTE_VALUE;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
/** An unquoted value ends at whitespace or the tag's own `>`; a quote or `=` inside it is value text. */
|
|
228
|
+
if ((character === '>' || isSpace(character)) && attrName === 'encoding' && valueFrom !== -1 && encoding === '')
|
|
229
|
+
encoding = text.slice(valueFrom, i);
|
|
230
|
+
if (character === '>') {
|
|
231
|
+
inValue = false;
|
|
232
|
+
emit = true;
|
|
233
|
+
}
|
|
234
|
+
else if (isSpace(character)) {
|
|
235
|
+
inValue = false;
|
|
236
|
+
phase = BEFORE_ATTRIBUTE_NAME;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
else if (character === '/' && i === 0 && text[1] === '>') {
|
|
240
|
+
/**
|
|
241
|
+
* A `/>` RIGHT after a hole that ended an unquoted value is the tag's self-close, not value text — `compile`
|
|
242
|
+
* keeps the slash out of the value, as the client's scanner does (`<circle r=${r}/>`), so the markup served is
|
|
243
|
+
* a self-closing tag and is scanned as one.
|
|
244
|
+
*/
|
|
245
|
+
inValue = false;
|
|
246
|
+
phase = SELF_CLOSING;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
else
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
else if (phase === OUTSIDE) {
|
|
253
|
+
const at = text.indexOf('<', i);
|
|
254
|
+
if (at === -1)
|
|
255
|
+
break;
|
|
256
|
+
i = at;
|
|
257
|
+
tagAt = at;
|
|
258
|
+
phase = TAG_OPEN;
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
else if (phase === TAG_OPEN) {
|
|
262
|
+
if (character === '!') {
|
|
263
|
+
if (text.startsWith('--', i + 1)) {
|
|
264
|
+
i += 2;
|
|
265
|
+
/** `<!-->` and `<!--->` end the comment they open — the tokenizer's abrupt close. */
|
|
266
|
+
if (text[i + 1] === '>')
|
|
267
|
+
i += 1;
|
|
268
|
+
else if (text.startsWith('->', i + 1))
|
|
269
|
+
i += 2;
|
|
270
|
+
else
|
|
271
|
+
comment = '-->';
|
|
272
|
+
}
|
|
273
|
+
else
|
|
274
|
+
comment = '>';
|
|
275
|
+
phase = OUTSIDE;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (character === '/') {
|
|
279
|
+
phase = END_TAG_OPEN;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (isAlpha(character)) {
|
|
283
|
+
phase = TAG_NAME;
|
|
284
|
+
tagName = lowerAscii(character);
|
|
285
|
+
closing = false;
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
/** `<?` opens a bogus comment; a `<` before anything else is TEXT — `a < b`, `<1`. */
|
|
289
|
+
if (character === '?')
|
|
290
|
+
comment = '>';
|
|
291
|
+
else
|
|
292
|
+
i--;
|
|
293
|
+
phase = OUTSIDE;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
else if (phase === END_TAG_OPEN) {
|
|
297
|
+
if (isAlpha(character)) {
|
|
298
|
+
phase = TAG_NAME;
|
|
299
|
+
tagName = lowerAscii(character);
|
|
300
|
+
closing = true;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
/** `</>` is nothing at all; `</` before anything else opens a bogus comment that holds that character. */
|
|
304
|
+
if (character !== '>') {
|
|
305
|
+
comment = '>';
|
|
306
|
+
i--;
|
|
307
|
+
}
|
|
308
|
+
phase = OUTSIDE;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
else if (phase === TAG_NAME) {
|
|
312
|
+
/** A tag name runs to whitespace, `/` or `>` — every other character is part of it, `.` `:` `=` and quotes too. */
|
|
313
|
+
if (isSpace(character))
|
|
314
|
+
phase = BEFORE_ATTRIBUTE_NAME;
|
|
315
|
+
else if (character === '/')
|
|
316
|
+
phase = SELF_CLOSING;
|
|
317
|
+
else if (character === '>')
|
|
318
|
+
emit = true;
|
|
319
|
+
else {
|
|
320
|
+
tagName += lowerAscii(character);
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
/** The name is whole: a `<script>` start tag gets the inert type right after it, first among its attributes. */
|
|
324
|
+
if (edits !== undefined && !closing && tagName === 'script')
|
|
325
|
+
edits.push(i, EDIT_INERT);
|
|
326
|
+
if (!emit)
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
else if (phase === BEFORE_ATTRIBUTE_NAME || phase === AFTER_ATTRIBUTE_NAME || phase === AFTER_ATTRIBUTE_VALUE) {
|
|
330
|
+
if (isSpace(character)) {
|
|
331
|
+
phase = BEFORE_ATTRIBUTE_NAME;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
if (character === '/') {
|
|
335
|
+
phase = SELF_CLOSING;
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
if (character === '>')
|
|
339
|
+
emit = true;
|
|
340
|
+
/** `=` after a name opens its value; anywhere else — `<b =x>` — it is the first character of a NAME. */
|
|
341
|
+
else if (character === '=' && phase === AFTER_ATTRIBUTE_NAME)
|
|
342
|
+
value = true;
|
|
343
|
+
else {
|
|
344
|
+
phase = ATTRIBUTE_NAME;
|
|
345
|
+
nameFrom = i;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
else if (phase === ATTRIBUTE_NAME) {
|
|
350
|
+
/** A name runs to whitespace, `/`, `>` or `=` — a quote or `<` inside it is part of the name (`<b x">`). */
|
|
351
|
+
if (!(isSpace(character) || character === '/' || character === '>' || character === '='))
|
|
352
|
+
continue;
|
|
353
|
+
attrRaw = nameFrom === -1 ? attrRaw + text.slice(0, i) : text.slice(nameFrom, i);
|
|
354
|
+
if (edits !== undefined && !closing && tagName === 'template' && nameFrom !== -1) {
|
|
355
|
+
const lower = attrRaw.toLowerCase();
|
|
356
|
+
if (lower === 'shadowrootmode' || lower === 'shadowroot')
|
|
357
|
+
edits.push(nameFrom, EDIT_SHADOW_ROOT);
|
|
358
|
+
}
|
|
359
|
+
if (character === '=')
|
|
360
|
+
value = true;
|
|
361
|
+
else if (character === '>')
|
|
362
|
+
emit = true;
|
|
363
|
+
else {
|
|
364
|
+
phase = character === '/' ? SELF_CLOSING : AFTER_ATTRIBUTE_NAME;
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
/** `SELF_CLOSING`: only a `>` makes the tag self-closing; anything else starts an attribute name. */
|
|
370
|
+
if (character === '>') {
|
|
371
|
+
emit = true;
|
|
372
|
+
selfClosing = true;
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
phase = BEFORE_ATTRIBUTE_NAME;
|
|
376
|
+
i--;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
if (value) {
|
|
381
|
+
/** Which attribute this value belongs to, and a serial per value — the URL-sink check needs both. */
|
|
382
|
+
attrName = attrRaw.toLowerCase();
|
|
383
|
+
serial++;
|
|
384
|
+
let next = i + 1;
|
|
385
|
+
while (next < length && isSpace(text[next]))
|
|
386
|
+
next++;
|
|
387
|
+
quote = text[next] === '"' || text[next] === "'" ? text[next] : '';
|
|
388
|
+
inValue = true;
|
|
389
|
+
/** The value states are `inValue`/`quote`; every way out of them names the phase that follows. */
|
|
390
|
+
phase = BEFORE_ATTRIBUTE_NAME;
|
|
391
|
+
opened = true;
|
|
392
|
+
nameAt = nameFrom === -1 ? 0 : nameFrom;
|
|
393
|
+
attrStart = nameAt;
|
|
394
|
+
while (attrStart > 0 && isSpace(text[attrStart - 1]))
|
|
395
|
+
attrStart--;
|
|
396
|
+
valueStart = quote ? next + 1 : next;
|
|
397
|
+
valueFrom = valueStart;
|
|
398
|
+
i = quote ? next : next - 1;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
/** `emit`: the tag is whole. */
|
|
402
|
+
phase = OUTSIDE;
|
|
403
|
+
/** A tag the component scan wants — see `ScannedTag`. A dashed name never changes parse state, so where it is read is where it sits. */
|
|
404
|
+
if (tags !== undefined && inert === 0 && tagName.includes('-'))
|
|
405
|
+
tags.push({ at: tagAt, end: i + 1, name: tagName, closing, live: !closing && foreign === 0 });
|
|
406
|
+
/**
|
|
407
|
+
* **Most tags change nothing about how what follows parses.** In HTML with nothing open, only the elements in
|
|
408
|
+
* `STRUCTURAL` do, so every other tag — the `<p>`, `<a>` and `<li>` that are nearly all of real markup — skips the
|
|
409
|
+
* stack, namespace and breakout work below.
|
|
410
|
+
*/
|
|
411
|
+
if (foreign === 0 && opens.length === 0 && !STRUCTURAL.has(tagName)) {
|
|
412
|
+
tagName = '';
|
|
413
|
+
closing = false;
|
|
414
|
+
selfClosing = false;
|
|
415
|
+
encoding = '';
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
/** The innermost element this scan opened that changes parsing, and the namespace this tag is read in. */
|
|
419
|
+
let top = opens.length === 0 ? undefined : opens[opens.length - 1];
|
|
420
|
+
let space = foreign === 0 ? 'html' : top !== undefined && top.space !== 'html' ? top.space : '';
|
|
421
|
+
/**
|
|
422
|
+
* **A breakout** — see `BREAKOUT`. The ONE rule here that moves the scan from foreign content toward HTML, the
|
|
423
|
+
* dangerous direction, so it acts only where its target is certain: inside SVG or MathML content (never at an
|
|
424
|
+
* integration point, where these tags are HTML already), popping to the nearest element whose content is HTML. It
|
|
425
|
+
* stops — and changes nothing — at `<noscript>` or anywhere the namespace is unknown, and at the template's own
|
|
426
|
+
* root unless that root is HTML: a template that inherited its depth cannot know what is outside it.
|
|
427
|
+
*/
|
|
428
|
+
if ((space === 'svg' || space === 'math') && (closing ? tagName === 'p' || tagName === 'br' : BREAKOUT.has(tagName))) {
|
|
429
|
+
const depth = breakOut(opens);
|
|
430
|
+
if (depth !== -1) {
|
|
431
|
+
foreign = depth;
|
|
432
|
+
top = opens.length === 0 ? undefined : opens[opens.length - 1];
|
|
433
|
+
space = 'html';
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/** `mglyph`/`malignmark` straight inside a MathML text integration point: read as MathML, not HTML. */
|
|
437
|
+
const reentry = space === 'html' && top !== undefined && MATH_TEXT_POINTS.has(top.name) && (tagName === 'mglyph' || tagName === 'malignmark');
|
|
438
|
+
/**
|
|
439
|
+
* A `/` before the `>` closes only a FOREIGN element: on an HTML one the parser ignores it, so `<template/>`,
|
|
440
|
+
* `<noscript/>` and `<style/>` open exactly as `<template>`, `<noscript>` and `<style>` do. Counting them as closed
|
|
441
|
+
* had the server writing raw text into what the browser parses as `<noscript>` content. `<svg/>` and `<math/>` are
|
|
442
|
+
* foreign elements even in HTML, so their `/>` closes them; inside foreign content every `/>` does.
|
|
443
|
+
*/
|
|
444
|
+
const selfClosed = selfClosing && (space !== 'html' || reentry || tagName === 'svg' || tagName === 'math');
|
|
445
|
+
if (closing) {
|
|
446
|
+
/** An end tag closes the innermost element of its name this scan opened, and everything opened inside it. */
|
|
447
|
+
/**
|
|
448
|
+
* Inside `<template>` content an end tag closes nothing outside the template (see `closeTo`). Each entry holds the
|
|
449
|
+
* foreign depth and the template depth from before it opened, and a close restores both — never a count kept
|
|
450
|
+
* by hand, which a close that also closes a template inside it would leave wrong.
|
|
451
|
+
*/
|
|
452
|
+
const at = closeTo(opens, tagName);
|
|
453
|
+
if (at !== -1) {
|
|
454
|
+
foreign = opens[at].foreign;
|
|
455
|
+
/** How deep inside nested `<template>` content this is: what it was before the closed element opened. */
|
|
456
|
+
inert = opens[at].inert;
|
|
457
|
+
opens.length = at;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
else if (!selfClosed) {
|
|
461
|
+
/**
|
|
462
|
+
* **Foreign content: raw text exists only for HTML elements.** Inside `<svg>`/`<math>`, `<style>`, `<title>`,
|
|
463
|
+
* `<textarea>` and the rest are SVG/MathML elements whose content is MARKUP — read as raw text there, a hole
|
|
464
|
+
* would be served unquoted, unescaped and unrefused (an attribute hole became a live handler). So raw text is
|
|
465
|
+
* recognized only at depth 0, which an HTML integration point restores for its content (see `SVG_POINTS`).
|
|
466
|
+
* A breakout tag (`<svg><p>`) leaves it (see `BREAKOUT`). Deliberately incomplete on the SAFE side: an end tag
|
|
467
|
+
* the parser ignores keeps the depth, and so does `<font color>` — both only ever read raw text as markup, which
|
|
468
|
+
* over-escapes, never injects. `<noscript>` counts as foreign: markup to a parser with scripting off and raw text
|
|
469
|
+
* to one with it on, so it is scanned as the first and escaped as neither can misread (see `TEXT_ONLY`), and
|
|
470
|
+
* nothing inside it is an integration point. The depth a hole sits at is recorded for the template rendered
|
|
471
|
+
* there, which starts from it (see `serializeTemplate`).
|
|
472
|
+
*/
|
|
473
|
+
if (tagName === 'svg' || tagName === 'math') {
|
|
474
|
+
/**
|
|
475
|
+
* An `<svg>`/`<math>` start tag the parser handles by HTML rules — in HTML, or an `<svg>` straight inside
|
|
476
|
+
* `annotation-xml` — opens its own namespace; inside foreign content it is an element of the SURROUNDING one
|
|
477
|
+
* (`<math><svg>` is a MathML element named `svg`, so a `foreignObject` in it is MathML too).
|
|
478
|
+
*/
|
|
479
|
+
const own = space === 'html' || (tagName === 'svg' && top !== undefined && top.name === 'annotation-xml');
|
|
480
|
+
opens.push({ name: tagName, foreign, inert, space: own ? tagName : space });
|
|
481
|
+
foreign++;
|
|
482
|
+
}
|
|
483
|
+
else if (tagName === 'noscript' && space !== 'svg' && space !== 'math') {
|
|
484
|
+
/**
|
|
485
|
+
* Only where HTML rules read the tag (2026-11-19, a rotated-seed find): inside `<svg>`/`<math>` proper a
|
|
486
|
+
* `<noscript>` is a foreign element like any (raw text exists only for HTML elements), so it is not tracked and a
|
|
487
|
+
* breakout after it (`<svg><noscript><p>`) pops it with the foreign content, as the parser does. At an
|
|
488
|
+
* integration point `space` is HTML, so it is tracked there — an HTML `<noscript>`, as `<template>` below.
|
|
489
|
+
*/
|
|
490
|
+
opens.push({ name: tagName, foreign, inert, space: '' });
|
|
491
|
+
foreign++;
|
|
492
|
+
}
|
|
493
|
+
else if (tagName === 'template' && space !== 'svg' && space !== 'math') {
|
|
494
|
+
/** Inert content only for an HTML `<template>`: inside `<svg>`/`<math>` it is a foreign element, its content live markup. */
|
|
495
|
+
opens.push({ name: tagName, foreign, inert, space });
|
|
496
|
+
inert++;
|
|
497
|
+
}
|
|
498
|
+
else if ((space === 'svg' && SVG_POINTS.has(tagName)) ||
|
|
499
|
+
(space === 'math' && (MATH_TEXT_POINTS.has(tagName) || (tagName === 'annotation-xml' && HTML_ENCODINGS.has(encoding.toLowerCase()))))) {
|
|
500
|
+
opens.push({ name: tagName, foreign, inert, space: 'html' });
|
|
501
|
+
foreign = 0;
|
|
502
|
+
}
|
|
503
|
+
else if (space === 'math' && tagName === 'annotation-xml') {
|
|
504
|
+
/** Not an integration point, but an `<svg>` straight inside it is SVG — so it is tracked. */
|
|
505
|
+
opens.push({ name: tagName, foreign, inert, space });
|
|
506
|
+
}
|
|
507
|
+
else if (reentry) {
|
|
508
|
+
opens.push({ name: tagName, foreign, inert, space: 'math' });
|
|
509
|
+
foreign = 1;
|
|
510
|
+
}
|
|
511
|
+
else if ((space === 'svg' || space === 'math') && tagName === 'script') {
|
|
512
|
+
/**
|
|
513
|
+
* A `<script>` in SVG (or a MathML element of that name) is tracked so a hole inside it is refused (see
|
|
514
|
+
* `compile`): its content is markup to the parser, so it is no raw text here, and an SVG script runs on a served
|
|
515
|
+
* page — and from a template, in Chromium and WebKit (BROWSER-LEDGER, 2026-11-18). An HTML `<script>` is raw text
|
|
516
|
+
* (`rawTag`, below) and never reaches `opens`, so every `script` there is a foreign one.
|
|
517
|
+
*/
|
|
518
|
+
opens.push({ name: tagName, foreign, inert, space });
|
|
519
|
+
}
|
|
520
|
+
else if (foreign === 0 && RAWTEXT.has(tagName)) {
|
|
521
|
+
rawTag = tagName;
|
|
522
|
+
rawFrom = i + 1;
|
|
523
|
+
}
|
|
524
|
+
else if (foreign === 0 && TEXT_ONLY.has(tagName))
|
|
525
|
+
textTag = tagName;
|
|
526
|
+
}
|
|
527
|
+
tagName = '';
|
|
528
|
+
closing = false;
|
|
529
|
+
selfClosing = false;
|
|
530
|
+
encoding = '';
|
|
531
|
+
}
|
|
532
|
+
/** A name still being read at the end carries on into the next text (a hole there is refused — see `nameHole`). */
|
|
533
|
+
if (phase === ATTRIBUTE_NAME)
|
|
534
|
+
attrRaw = nameFrom === -1 ? attrRaw + text : text.slice(nameFrom);
|
|
535
|
+
return { inTag: phase !== OUTSIDE, phase, inValue, quote, rawTag, tagName, attrName, serial, closing, inert, comment, textTag, foreign, opens, attrRaw, encoding, opened, attrStart, valueStart, nameAt, tagAt, rawFrom, rawUntil };
|
|
536
|
+
};
|
|
537
|
+
/** 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`. */
|
|
538
|
+
export const closersOf = (end) => (end.comment || (end.rawTag ? `</${end.rawTag}>` : end.textTag ? `</${end.textTag}>` : '')) +
|
|
539
|
+
end.opens.reduceRight((closing, open) => closing + `</${open.name}>`, '');
|
|
540
|
+
/** A scan state at foreign depth `depth` and nothing else open — its own `opens`, since `scanTag` mutates it. */
|
|
541
|
+
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: '' });
|
|
542
|
+
/**
|
|
543
|
+
* What the template compiler reads of this module besides the scan itself — exported as SEPARATE bindings. The scan
|
|
544
|
+
* reads its own constants on every character, and V8 reaches an exported module variable through a cell rather than
|
|
545
|
+
* the module's context: exporting these directly cost compile 4% and a 50 KB \`.innerHTML\` value 11%.
|
|
546
|
+
*/
|
|
547
|
+
export const RAW_TEXT_TAGS = RAWTEXT;
|
|
548
|
+
export const TEXT_ONLY_TAGS = TEXT_ONLY;
|
|
549
|
+
export const PHASE_TAG_OPEN = TAG_OPEN;
|
|
550
|
+
export const PHASE_END_TAG_OPEN = END_TAG_OPEN;
|
|
551
|
+
export const PHASE_TAG_NAME = TAG_NAME;
|
|
552
|
+
export const isTokenizerSpace = isSpace;
|
|
553
|
+
export const INERT_EDIT = EDIT_INERT;
|