@tiavision/sinter-mail 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +113 -2
- package/docs/mail.md +298 -0
- package/docs/proposals/progressive-rendering.md +180 -0
- package/docs/proposals/sinter-mail.md +520 -0
- package/llms.txt +21 -0
- package/package.json +65 -4
- package/sinter/autolink.ts +98 -0
- package/sinter/components/internal/mail-a.html +5 -0
- package/sinter/components/internal/mail-abbr.html +5 -0
- package/sinter/components/internal/mail-address.html +5 -0
- package/sinter/components/internal/mail-article.html +5 -0
- package/sinter/components/internal/mail-aside.html +5 -0
- package/sinter/components/internal/mail-b.html +5 -0
- package/sinter/components/internal/mail-bdi.html +5 -0
- package/sinter/components/internal/mail-bdo.html +5 -0
- package/sinter/components/internal/mail-blockquote.html +5 -0
- package/sinter/components/internal/mail-br.html +5 -0
- package/sinter/components/internal/mail-center.html +5 -0
- package/sinter/components/internal/mail-cite.html +5 -0
- package/sinter/components/internal/mail-code.html +5 -0
- package/sinter/components/internal/mail-dd.html +5 -0
- package/sinter/components/internal/mail-del.html +5 -0
- package/sinter/components/internal/mail-details.html +5 -0
- package/sinter/components/internal/mail-dfn.html +5 -0
- package/sinter/components/internal/mail-div.html +5 -0
- package/sinter/components/internal/mail-dl.html +5 -0
- package/sinter/components/internal/mail-dt.html +5 -0
- package/sinter/components/internal/mail-em.html +5 -0
- package/sinter/components/internal/mail-figcaption.html +5 -0
- package/sinter/components/internal/mail-figure.html +5 -0
- package/sinter/components/internal/mail-font.html +5 -0
- package/sinter/components/internal/mail-footer.html +5 -0
- package/sinter/components/internal/mail-h1.html +5 -0
- package/sinter/components/internal/mail-h2.html +5 -0
- package/sinter/components/internal/mail-h3.html +5 -0
- package/sinter/components/internal/mail-h4.html +5 -0
- package/sinter/components/internal/mail-h5.html +5 -0
- package/sinter/components/internal/mail-h6.html +5 -0
- package/sinter/components/internal/mail-header.html +5 -0
- package/sinter/components/internal/mail-hr.html +5 -0
- package/sinter/components/internal/mail-i.html +5 -0
- package/sinter/components/internal/mail-img.html +5 -0
- package/sinter/components/internal/mail-ins.html +5 -0
- package/sinter/components/internal/mail-kbd.html +5 -0
- package/sinter/components/internal/mail-li.html +5 -0
- package/sinter/components/internal/mail-main.html +5 -0
- package/sinter/components/internal/mail-mark.html +5 -0
- package/sinter/components/internal/mail-nav.html +5 -0
- package/sinter/components/internal/mail-node.html +5 -0
- package/sinter/components/internal/mail-ol.html +5 -0
- package/sinter/components/internal/mail-orphan.html +5 -0
- package/sinter/components/internal/mail-p.html +5 -0
- package/sinter/components/internal/mail-pre.html +5 -0
- package/sinter/components/internal/mail-q.html +5 -0
- package/sinter/components/internal/mail-s.html +5 -0
- package/sinter/components/internal/mail-samp.html +5 -0
- package/sinter/components/internal/mail-section.html +5 -0
- package/sinter/components/internal/mail-small.html +5 -0
- package/sinter/components/internal/mail-span.html +5 -0
- package/sinter/components/internal/mail-steps.html +5 -0
- package/sinter/components/internal/mail-steps.ts +46 -0
- package/sinter/components/internal/mail-strike.html +5 -0
- package/sinter/components/internal/mail-strong.html +5 -0
- package/sinter/components/internal/mail-sub.html +5 -0
- package/sinter/components/internal/mail-summary.html +5 -0
- package/sinter/components/internal/mail-sup.html +5 -0
- package/sinter/components/internal/mail-table.html +5 -0
- package/sinter/components/internal/mail-time.html +5 -0
- package/sinter/components/internal/mail-tt.html +5 -0
- package/sinter/components/internal/mail-u.html +5 -0
- package/sinter/components/internal/mail-ul.html +5 -0
- package/sinter/components/internal/mail-var.html +5 -0
- package/sinter/components/internal/mail-wbr.html +5 -0
- package/sinter/components/mail-body.html +21 -0
- package/sinter/css/hints.ts +259 -0
- package/sinter/css/index.ts +348 -0
- package/sinter/css/media.ts +254 -0
- package/sinter/css/parse.ts +124 -0
- package/sinter/css/selectors.ts +530 -0
- package/sinter/css/tokenize.ts +279 -0
- package/sinter/css/values.ts +669 -0
- package/sinter/engine.ts +6 -0
- package/sinter/index.ts +5 -0
- package/sinter/parse.ts +359 -0
- package/sinter/parser/entities.ts +22 -0
- package/sinter/parser/index.ts +54 -0
- package/sinter/parser/tokenizer.ts +275 -0
- package/sinter/parser/tree.ts +1265 -0
- package/sinter/plain.ts +125 -0
- package/sinter/raw.ts +28 -0
- package/sinter/sinter.helpers.ts +11 -0
- package/sinter/sinter.types.ts +144 -0
- package/sinter/tags.ts +169 -0
- package/sinter/tree.ts +168 -0
|
@@ -0,0 +1,1265 @@
|
|
|
1
|
+
// The HTML standard's tree construction, as parse5 runs it (test/reference.ts is the oracle): the insertion
|
|
2
|
+
// modes a document goes through, the stack of open elements and its scopes, the list of active formatting
|
|
3
|
+
// elements (with the Noah's Ark clause and reconstruction), the adoption agency, foster parenting, implied
|
|
4
|
+
// end tags and the head and form element pointers. Scripting is on, as in parse5's default: `<noscript>` is
|
|
5
|
+
// raw text. Where parse5 departs from the standard's text, this follows parse5, as browsers' trees are
|
|
6
|
+
// what it has to match: table scope stops only at `html` and `table`, implied end tags "except for" one use
|
|
7
|
+
// the thorough list, `</tbody>` in a row closes it when a `tr` is in scope, and `<![CDATA[` is a section only
|
|
8
|
+
// where the current node is foreign and not an integration point.
|
|
9
|
+
//
|
|
10
|
+
// Foreign content (`<svg>`, `<math>`) is parsed as the standard says, so that what follows it lands where
|
|
11
|
+
// it should; the raw tree leaves it out with its subtree (index.ts). A `<template>`'s content goes to a
|
|
12
|
+
// fragment of its own, which the raw tree leaves out too.
|
|
13
|
+
//
|
|
14
|
+
// Bounded: the stack holds at most `maxDepth` elements. An element that would open deeper closes the
|
|
15
|
+
// current node first and opens beside it (as Blink flattens deep trees), so every search of the stack is
|
|
16
|
+
// bounded and the parse stays linear; below the bound nothing changes.
|
|
17
|
+
import { tokenize, DATA, PLAINTEXT, RAWTEXT, RCDATA, SCRIPT, type Attrs, type Sink } from './tokenizer.js';
|
|
18
|
+
|
|
19
|
+
export const HTML = 0, SVG = 1, MATH = 2;
|
|
20
|
+
|
|
21
|
+
export interface Node {
|
|
22
|
+
tag: string;
|
|
23
|
+
ns: number;
|
|
24
|
+
attrs: Attrs;
|
|
25
|
+
/** elements and texts (strings, adjacent ones joined) */
|
|
26
|
+
kids: (Node | string)[];
|
|
27
|
+
parent: Node | null;
|
|
28
|
+
/** on the stack of open elements */
|
|
29
|
+
open: boolean;
|
|
30
|
+
/** in the list of active formatting elements */
|
|
31
|
+
listed: boolean;
|
|
32
|
+
/** a template's content */
|
|
33
|
+
content: Node | null;
|
|
34
|
+
/** what the element is to the stack's searches: the bits below */
|
|
35
|
+
bits: number;
|
|
36
|
+
/** a formatting element's key (keyOf) */
|
|
37
|
+
key?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// the scopes an element bounds, and whether it is special (other than HTML `address`, `div` and `p`, which
|
|
41
|
+
// do not stop the search for a list item to close)
|
|
42
|
+
const SCOPE_BOUND = 1, LIST_BOUND = 2, BUTTON_BOUND = 4, TABLE_BOUND = 8, SPECIAL_BIT = 16, LIST_ITEM_STOP = 32;
|
|
43
|
+
|
|
44
|
+
interface Tag {
|
|
45
|
+
name: string;
|
|
46
|
+
attrs: Attrs;
|
|
47
|
+
selfClosing: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const INITIAL = 0, BEFORE_HTML = 1, BEFORE_HEAD = 2, IN_HEAD = 3, AFTER_HEAD = 4, IN_BODY = 5, TEXT = 6,
|
|
51
|
+
IN_TABLE = 7, IN_TABLE_TEXT = 8, IN_CAPTION = 9, IN_COLUMN_GROUP = 10, IN_TABLE_BODY = 11, IN_ROW = 12,
|
|
52
|
+
IN_CELL = 13, IN_SELECT = 14, IN_SELECT_IN_TABLE = 15, IN_TEMPLATE = 16, AFTER_BODY = 17, IN_FRAMESET = 18,
|
|
53
|
+
AFTER_FRAMESET = 19, AFTER_AFTER_BODY = 20, AFTER_AFTER_FRAMESET = 21;
|
|
54
|
+
// scopes, as the bits of the elements that bound them
|
|
55
|
+
const DEFAULT = SCOPE_BOUND, LIST_ITEM = SCOPE_BOUND | LIST_BOUND, BUTTON = SCOPE_BOUND | BUTTON_BOUND, TABLE = TABLE_BOUND;
|
|
56
|
+
|
|
57
|
+
const set = (s: string): Set<string> => new Set(s.split(' '));
|
|
58
|
+
const FORMATTING = set('b big code em font i s small strike strong tt u');
|
|
59
|
+
const HEADINGS = set('h1 h2 h3 h4 h5 h6');
|
|
60
|
+
const BLOCKS = 'address article aside blockquote center details dialog dir div dl fieldset figcaption figure footer header hgroup main menu nav ol search section summary ul';
|
|
61
|
+
const BLOCK_START = set(BLOCKS + ' p');
|
|
62
|
+
const BLOCK_END = set(BLOCKS + ' button listing pre');
|
|
63
|
+
const SPECIAL = set('address applet area article aside base basefont bgsound blockquote body br button caption center col colgroup dd details dir div dl dt embed fieldset figcaption figure footer form frame frameset h1 h2 h3 h4 h5 h6 head header hgroup hr html iframe img input keygen li link listing main marquee menu meta nav noembed noframes noscript object ol p param plaintext pre script section select source style summary table tbody td template textarea tfoot th thead title tr track ul wbr xmp');
|
|
64
|
+
const SCOPE = set('applet caption html marquee object table td template th');
|
|
65
|
+
const MARKERS = set('applet caption marquee object td template th');
|
|
66
|
+
const MATH_TEXT = set('mi mo mn ms mtext');
|
|
67
|
+
const MATH_SCOPE = set('mi mo mn ms mtext annotation-xml');
|
|
68
|
+
const SVG_HTML = set('foreignobject desc title');
|
|
69
|
+
const IMPLIED = set('dd dt li optgroup option p rb rp rt rtc');
|
|
70
|
+
const THOROUGH = set('dd dt li optgroup option p rb rp rt rtc caption colgroup tbody td tfoot th thead tr');
|
|
71
|
+
const TABLE_STRUCTURE = set('table tbody tfoot thead tr');
|
|
72
|
+
const TABLE_PARTS = set('caption col colgroup tbody td tfoot th thead tr');
|
|
73
|
+
const SELECT_ENDS = set('caption table tbody td tfoot th thead tr');
|
|
74
|
+
const TABLE_BODIES = set('tbody tfoot thead');
|
|
75
|
+
const CELLS = set('td th');
|
|
76
|
+
const TABLE_CONTEXT = set('table template html');
|
|
77
|
+
const BODY_CONTEXT = set('tbody tfoot thead template html');
|
|
78
|
+
const ROW_CONTEXT = set('tr template html');
|
|
79
|
+
const IN_HEAD_TAGS = set('base basefont bgsound link meta noframes script style template title');
|
|
80
|
+
const EXITS = set('b big blockquote body br center code dd div dl dt em embed h1 h2 h3 h4 h5 h6 head hr i img li listing menu meta nobr ol p pre ruby s small span strong strike sub sup table tt u ul var');
|
|
81
|
+
const MODE_TAGS = set('tr tbody thead tfoot caption colgroup table frameset select template td th head body html');
|
|
82
|
+
// doctypes that put the document in quirks mode, where `<p>` stays open around a `<table>`: these public
|
|
83
|
+
// identifiers, and those that start with one of the prefixes (the standard's lists, lowercased)
|
|
84
|
+
const QUIRKY_IDS = new Set(['-//w3o//dtd w3 html strict 3.0//en//', '-/w3c/dtd html 4.0 transitional/en', 'html']);
|
|
85
|
+
const QUIRKY_PREFIXES = ("+//silmaril//dtd html pro v0r11 19970101//|-//as//dtd html 3.0 aswedit + extensions//|-//advasoft ltd//dtd html 3.0 aswedit + extensions//|-//ietf//dtd html 2.0 level 1//|-//ietf//dtd html 2.0 level 2//|-//ietf//dtd html 2.0 strict level 1//|-//ietf//dtd html 2.0 strict level 2//|-//ietf//dtd html 2.0 strict//|-//ietf//dtd html 2.0//|-//ietf//dtd html 2.1e//|-//ietf//dtd html 3.0//|-//ietf//dtd html 3.2 final//|-//ietf//dtd html 3.2//|-//ietf//dtd html 3//|-//ietf//dtd html level 0//|-//ietf//dtd html level 1//|-//ietf//dtd html level 2//|-//ietf//dtd html level 3//|-//ietf//dtd html strict level 0//|-//ietf//dtd html strict level 1//|-//ietf//dtd html strict level 2//|-//ietf//dtd html strict level 3//|-//ietf//dtd html strict//|-//ietf//dtd html//|-//metrius//dtd metrius presentational//|-//microsoft//dtd internet explorer 2.0 html strict//|-//microsoft//dtd internet explorer 2.0 html//|-//microsoft//dtd internet explorer 2.0 tables//|-//microsoft//dtd internet explorer 3.0 html strict//|-//microsoft//dtd internet explorer 3.0 html//|-//microsoft//dtd internet explorer 3.0 tables//|-//netscape comm. corp.//dtd html//|-//netscape comm. corp.//dtd strict html//|-//o'reilly and associates//dtd html 2.0//|-//o'reilly and associates//dtd html extended 1.0//|-//o'reilly and associates//dtd html extended relaxed 1.0//|-//sq//dtd html 2.0 hotmetal + extensions//|-//softquad software//dtd hotmetal pro 6.0::19990601::extensions to html 4.0//|-//softquad//dtd hotmetal pro 4.0::19971010::extensions to html 4.0//|-//spyglass//dtd html 2.0 extended//|-//sun microsystems corp.//dtd hotjava html//|-//sun microsystems corp.//dtd hotjava strict html//|-//w3c//dtd html 3 1995-03-24//|-//w3c//dtd html 3.2 draft//|-//w3c//dtd html 3.2 final//|-//w3c//dtd html 3.2//|-//w3c//dtd html 3.2s draft//|-//w3c//dtd html 4.0 frameset//|-//w3c//dtd html 4.0 transitional//|-//w3c//dtd html experimental 19960712//|-//w3c//dtd html experimental 970421//|-//w3c//dtd w3 html//|-//w3o//dtd w3 html 3.0//|-//webtechs//dtd mozilla html 2.0//|-//webtechs//dtd mozilla html//").split('|');
|
|
86
|
+
|
|
87
|
+
/** whether a doctype puts the document in quirks mode */
|
|
88
|
+
function quirky(name: string | null, publicId: string | null, systemId: string | null, force: boolean): boolean {
|
|
89
|
+
if (force || name !== 'html') return true;
|
|
90
|
+
if (systemId && systemId.toLowerCase() === 'http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd') return true;
|
|
91
|
+
if (publicId === null) return false;
|
|
92
|
+
const id = publicId.toLowerCase();
|
|
93
|
+
return QUIRKY_IDS.has(id) || QUIRKY_PREFIXES.some((p) => id.startsWith(p)) ||
|
|
94
|
+
(systemId === null && (id.startsWith('-//w3c//dtd html 4.01 frameset//') || id.startsWith('-//w3c//dtd html 4.01 transitional//')));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const create = (tag: string, attrs: Attrs, ns: number): Node => ({
|
|
98
|
+
tag, ns, attrs, kids: [], parent: null, open: false, listed: false,
|
|
99
|
+
content: ns === HTML && tag === 'template' ? create('#content', [], HTML) : null,
|
|
100
|
+
bits: ns === HTML
|
|
101
|
+
? (SCOPE.has(tag) ? SCOPE_BOUND : 0) | (tag === 'ol' || tag === 'ul' ? LIST_BOUND : 0) | (tag === 'button' ? BUTTON_BOUND : 0) |
|
|
102
|
+
(tag === 'html' || tag === 'table' ? TABLE_BOUND : 0) | (SPECIAL.has(tag) ? (tag === 'address' || tag === 'div' || tag === 'p' ? SPECIAL_BIT : SPECIAL_BIT | LIST_ITEM_STOP) : 0)
|
|
103
|
+
: (ns === MATH ? MATH_SCOPE : SVG_HTML).has(tag) ? SCOPE_BOUND | SPECIAL_BIT | LIST_ITEM_STOP : 0,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const isWhitespace = (s: string): boolean => !/[^\t\n\f\r ]/.test(s);
|
|
107
|
+
const leading = (s: string): number => /^[\t\n\f\r ]*/.exec(s)![0].length;
|
|
108
|
+
const noNul = (s: string): string => (s.includes('\0') ? s.replace(/\0/g, '') : s);
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A formatting element's tag and attributes, in any order, as one string (Noah's Ark compares them): NUL
|
|
112
|
+
* separates the parts, as the tokenizer leaves none in names and values.
|
|
113
|
+
*/
|
|
114
|
+
const keyOf = (n: Node): string => (n.key ??= n.tag + n.attrs.map(([a, v]) => `\0${a}\0${v}`).sort().join(''));
|
|
115
|
+
|
|
116
|
+
/** `html` parsed into its `<html>` element, with foreign elements and template contents still in it */
|
|
117
|
+
export function build(html: string, maxDepth: number): Node {
|
|
118
|
+
maxDepth = Math.max(maxDepth, 3); // html, body and one element; the bound never closes the body
|
|
119
|
+
const stack: Node[] = [];
|
|
120
|
+
/** the list of active formatting elements; null is a marker */
|
|
121
|
+
const list: (Node | null)[] = [];
|
|
122
|
+
const templateModes: number[] = [];
|
|
123
|
+
let mode = INITIAL, original = INITIAL, quirks = false, framesetOk = true, foster = false, skipNewline = false;
|
|
124
|
+
let root: Node | null = null, head: Node | null = null, form: Node | null = null, templates = 0, raw = DATA, pending = '';
|
|
125
|
+
|
|
126
|
+
// the stack of open elements
|
|
127
|
+
|
|
128
|
+
const cur = (): Node => stack[stack.length - 1];
|
|
129
|
+
const is = (n: Node | undefined, tag: string): boolean => !!n && n.ns === HTML && n.tag === tag;
|
|
130
|
+
const push = (n: Node): void => {
|
|
131
|
+
stack.push(n);
|
|
132
|
+
n.open = true;
|
|
133
|
+
if (is(n, 'template')) templates++;
|
|
134
|
+
};
|
|
135
|
+
const closed = (n: Node): void => {
|
|
136
|
+
n.open = false;
|
|
137
|
+
if (is(n, 'template')) templates--;
|
|
138
|
+
};
|
|
139
|
+
const pop = (): Node => {
|
|
140
|
+
const n = stack.pop()!;
|
|
141
|
+
closed(n);
|
|
142
|
+
return n;
|
|
143
|
+
};
|
|
144
|
+
/** pops down to `length` elements; the root always stays */
|
|
145
|
+
const popTo = (length: number): void => { while (stack.length > Math.max(length, 1)) pop(); };
|
|
146
|
+
const removeAt = (i: number): void => {
|
|
147
|
+
if (i < 1) return;
|
|
148
|
+
closed(stack[i]);
|
|
149
|
+
stack.splice(i, 1);
|
|
150
|
+
};
|
|
151
|
+
/** the index of the topmost open HTML element with this tag (or one of these tags), or -1 */
|
|
152
|
+
const top = (t: string | Set<string>): number => {
|
|
153
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
154
|
+
const n = stack[i];
|
|
155
|
+
if (n.ns === HTML && (typeof t === 'string' ? n.tag === t : t.has(n.tag))) return i;
|
|
156
|
+
}
|
|
157
|
+
return -1;
|
|
158
|
+
};
|
|
159
|
+
const popUntil = (t: string | Set<string>): void => {
|
|
160
|
+
const i = top(t);
|
|
161
|
+
if (i >= 0) popTo(i);
|
|
162
|
+
};
|
|
163
|
+
const special = (n: Node): number => n.bits & SPECIAL_BIT;
|
|
164
|
+
const inScope = (t: string | Set<string>, scope = DEFAULT, el?: Node): boolean => {
|
|
165
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
166
|
+
const n = stack[i];
|
|
167
|
+
if (n.ns === HTML && (typeof t === 'string' ? n.tag === t : t.has(n.tag)) && (!el || n === el)) return true;
|
|
168
|
+
if (n.bits & scope) return false;
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
};
|
|
172
|
+
const inSelectScope = (tag: string): boolean => {
|
|
173
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
174
|
+
const n = stack[i];
|
|
175
|
+
if (n.ns !== HTML || n.tag === 'option' || n.tag === 'optgroup') continue;
|
|
176
|
+
return n.tag === tag;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
};
|
|
180
|
+
/** generates implied end tags, but not for `except` */
|
|
181
|
+
const implied = (except?: string, tags = IMPLIED): void => {
|
|
182
|
+
for (let c = cur(); c.ns === HTML && c.tag !== except && tags.has(c.tag); c = cur()) pop();
|
|
183
|
+
};
|
|
184
|
+
const closeP = (): void => {
|
|
185
|
+
implied('p', THOROUGH);
|
|
186
|
+
popUntil('p');
|
|
187
|
+
};
|
|
188
|
+
const closePInButtonScope = (): void => { if (inScope('p', BUTTON)) closeP(); };
|
|
189
|
+
|
|
190
|
+
// integration points: where foreign content holds HTML (or MathML text)
|
|
191
|
+
const htmlPoint = (n: Node): boolean =>
|
|
192
|
+
n.ns === SVG ? SVG_HTML.has(n.tag)
|
|
193
|
+
: n.ns === MATH && n.tag === 'annotation-xml' && n.attrs.some(([a, v]) => a === 'encoding' && /^(?:text\/html|application\/xhtml\+xml)$/i.test(v));
|
|
194
|
+
const point = (n: Node): boolean => htmlPoint(n) || (n.ns === MATH && MATH_TEXT.has(n.tag));
|
|
195
|
+
/** the current node is foreign and no integration point: text and start tags are foreign content */
|
|
196
|
+
const inForeign = (): boolean => {
|
|
197
|
+
const c = cur();
|
|
198
|
+
return !!c && c.ns !== HTML && !point(c);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// inserting
|
|
202
|
+
|
|
203
|
+
/** where a node goes: [parent, the node it goes before] */
|
|
204
|
+
const place = (): [Node, Node | null] => {
|
|
205
|
+
const c = cur();
|
|
206
|
+
return foster && TABLE_STRUCTURE.has(c.tag) ? fosterPlace() : [c.content ?? c, null];
|
|
207
|
+
};
|
|
208
|
+
/** foster parenting: before the last table, or in the template opened after it */
|
|
209
|
+
const fosterPlace = (): [Node, Node | null] => {
|
|
210
|
+
const t = top('table'), tp = top('template');
|
|
211
|
+
if (tp > t) return [stack[tp].content!, null];
|
|
212
|
+
if (t < 0) return [stack[0], null];
|
|
213
|
+
const table = stack[t];
|
|
214
|
+
return table.parent ? [table.parent, table] : [stack[t - 1], null];
|
|
215
|
+
};
|
|
216
|
+
const attach = (n: Node, [parent, before]: [Node, Node | null]): void => {
|
|
217
|
+
n.parent = parent;
|
|
218
|
+
if (before) parent.kids.splice(parent.kids.lastIndexOf(before), 0, n);
|
|
219
|
+
else parent.kids.push(n);
|
|
220
|
+
};
|
|
221
|
+
const detach = (n: Node): void => {
|
|
222
|
+
const p = n.parent;
|
|
223
|
+
if (p) p.kids.splice(p.kids.lastIndexOf(n), 1);
|
|
224
|
+
n.parent = null;
|
|
225
|
+
};
|
|
226
|
+
const insertText = (s: string): void => {
|
|
227
|
+
const [parent, before] = place();
|
|
228
|
+
const k = parent.kids, i = before ? k.lastIndexOf(before) : k.length;
|
|
229
|
+
if (typeof k[i - 1] === 'string') k[i - 1] += s;
|
|
230
|
+
else k.splice(i, 0, s);
|
|
231
|
+
};
|
|
232
|
+
/** inserts an element and opens it; at the bound, the current node closes first */
|
|
233
|
+
const insert = (tag: string, attrs: Attrs = [], ns = HTML): Node => {
|
|
234
|
+
if (stack.length >= maxDepth) {
|
|
235
|
+
const n = pop();
|
|
236
|
+
if (n.listed) unlist(n);
|
|
237
|
+
if (n.ns === HTML && MARKERS.has(n.tag)) clearToMarker();
|
|
238
|
+
if (is(n, 'template')) templateModes.pop();
|
|
239
|
+
}
|
|
240
|
+
const n = create(tag, attrs, ns);
|
|
241
|
+
attach(n, place());
|
|
242
|
+
push(n);
|
|
243
|
+
return n;
|
|
244
|
+
};
|
|
245
|
+
/** inserts an element that does not open (a void element, a self-closing foreign one) */
|
|
246
|
+
const append = (tag: string, attrs: Attrs, ns = HTML): void => attach(create(tag, attrs, ns), place());
|
|
247
|
+
const mergeAttrs = (n: Node, attrs: Attrs): void => {
|
|
248
|
+
const names = new Set(n.attrs.map((a) => a[0]));
|
|
249
|
+
for (const a of attrs) if (!names.has(a[0])) n.attrs.push(a);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// the list of active formatting elements
|
|
253
|
+
|
|
254
|
+
const unlist = (n: Node): void => {
|
|
255
|
+
const i = list.lastIndexOf(n);
|
|
256
|
+
if (i >= 0) list.splice(i, 1);
|
|
257
|
+
n.listed = false;
|
|
258
|
+
};
|
|
259
|
+
const clearToMarker = (): void => {
|
|
260
|
+
for (let e = list.pop(); e; e = list.pop()) e.listed = false;
|
|
261
|
+
};
|
|
262
|
+
const listPush = (n: Node): void => {
|
|
263
|
+
// Noah's Ark: at most three entries with the same tag and attributes after the last marker
|
|
264
|
+
let count = 0, earliest: Node | null = null;
|
|
265
|
+
const key = keyOf(n);
|
|
266
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
267
|
+
const e = list[i];
|
|
268
|
+
if (!e) break;
|
|
269
|
+
if (keyOf(e) === key) { count++; earliest = e; }
|
|
270
|
+
}
|
|
271
|
+
if (count >= 3) unlist(earliest!);
|
|
272
|
+
list.push(n);
|
|
273
|
+
n.listed = true;
|
|
274
|
+
};
|
|
275
|
+
/** the last formatting element after the last marker with this tag */
|
|
276
|
+
const listed = (tag: string): Node | null => {
|
|
277
|
+
for (let i = list.length - 1; i >= 0 && list[i]; i--) if (list[i]!.tag === tag) return list[i];
|
|
278
|
+
return null;
|
|
279
|
+
};
|
|
280
|
+
const reconstruct = (): void => {
|
|
281
|
+
let i = list.length;
|
|
282
|
+
if (!i || !list[i - 1] || list[i - 1]!.open) return;
|
|
283
|
+
while (i > 0 && list[i - 1] && !list[i - 1]!.open) i--;
|
|
284
|
+
// not past the bound: what is left is reconstructed once the stack is shallower
|
|
285
|
+
for (; i < list.length && stack.length < maxDepth; i++) {
|
|
286
|
+
const e = list[i]!, n = insert(e.tag, e.attrs, e.ns);
|
|
287
|
+
e.listed = false;
|
|
288
|
+
n.listed = true;
|
|
289
|
+
list[i] = n;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
/** the adoption agency algorithm, for an end tag of a formatting element (or `<a>`, `<nobr>`) */
|
|
294
|
+
const adopt = (tag: string): void => {
|
|
295
|
+
const c = cur();
|
|
296
|
+
if (is(c, tag) && !c.listed) { pop(); return; }
|
|
297
|
+
for (let outer = 0; outer < 8; outer++) {
|
|
298
|
+
const fe = listed(tag);
|
|
299
|
+
if (!fe) return endInBodyOther(tag);
|
|
300
|
+
if (!fe.open) return unlist(fe);
|
|
301
|
+
if (!inScope(tag, DEFAULT, fe)) return;
|
|
302
|
+
const fi = stack.lastIndexOf(fe);
|
|
303
|
+
let bi = fi + 1;
|
|
304
|
+
while (bi < stack.length && !special(stack[bi])) bi++;
|
|
305
|
+
if (bi === stack.length) {
|
|
306
|
+
popTo(fi);
|
|
307
|
+
unlist(fe);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const block = stack[bi], common = stack[fi - 1];
|
|
311
|
+
let bookmark: Node = fe, last = block;
|
|
312
|
+
for (let i = 0, k = bi - 1; stack[k] !== fe; i++, k--) {
|
|
313
|
+
const n = stack[k];
|
|
314
|
+
if (!n.listed || i >= 3) {
|
|
315
|
+
if (n.listed) unlist(n);
|
|
316
|
+
removeAt(k);
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const clone = create(n.tag, n.attrs, n.ns);
|
|
320
|
+
list[list.lastIndexOf(n)] = clone;
|
|
321
|
+
n.listed = false;
|
|
322
|
+
clone.listed = true;
|
|
323
|
+
closed(n);
|
|
324
|
+
stack[k] = clone;
|
|
325
|
+
clone.open = true;
|
|
326
|
+
if (last === block) bookmark = clone;
|
|
327
|
+
detach(last);
|
|
328
|
+
attach(last, [clone, null]);
|
|
329
|
+
last = clone;
|
|
330
|
+
}
|
|
331
|
+
detach(last);
|
|
332
|
+
if (common) {
|
|
333
|
+
attach(last, TABLE_STRUCTURE.has(common.tag) ? fosterPlace() : [common.content ?? common, null]);
|
|
334
|
+
}
|
|
335
|
+
const n = create(fe.tag, fe.attrs, fe.ns);
|
|
336
|
+
n.kids = block.kids;
|
|
337
|
+
for (const k of n.kids) if (typeof k !== 'string') k.parent = n;
|
|
338
|
+
block.kids = [];
|
|
339
|
+
attach(n, [block, null]);
|
|
340
|
+
list.splice(list.lastIndexOf(bookmark) + 1, 0, n);
|
|
341
|
+
n.listed = true;
|
|
342
|
+
unlist(fe);
|
|
343
|
+
removeAt(stack.lastIndexOf(fe));
|
|
344
|
+
stack.splice(stack.lastIndexOf(block) + 1, 0, n);
|
|
345
|
+
n.open = true;
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
// modes
|
|
350
|
+
|
|
351
|
+
const resetMode = (): void => {
|
|
352
|
+
const i = top(MODE_TAGS);
|
|
353
|
+
const tag = i < 0 ? '' : stack[i].tag;
|
|
354
|
+
switch (tag) {
|
|
355
|
+
case 'tr': mode = IN_ROW; return;
|
|
356
|
+
case 'tbody': case 'thead': case 'tfoot': mode = IN_TABLE_BODY; return;
|
|
357
|
+
case 'caption': mode = IN_CAPTION; return;
|
|
358
|
+
case 'colgroup': mode = IN_COLUMN_GROUP; return;
|
|
359
|
+
case 'table': mode = IN_TABLE; return;
|
|
360
|
+
case 'body': mode = IN_BODY; return;
|
|
361
|
+
case 'frameset': mode = IN_FRAMESET; return;
|
|
362
|
+
case 'select': {
|
|
363
|
+
const t = top('table');
|
|
364
|
+
mode = i > 0 && t > 0 && t > top('template') ? IN_SELECT_IN_TABLE : IN_SELECT;
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
case 'template': mode = templateModes[templateModes.length - 1]; return;
|
|
368
|
+
case 'html': mode = head ? AFTER_HEAD : BEFORE_HEAD; return;
|
|
369
|
+
case 'td': case 'th': if (i > 0) { mode = IN_CELL; return; } break;
|
|
370
|
+
case 'head': if (i > 0) { mode = IN_HEAD; return; } break;
|
|
371
|
+
}
|
|
372
|
+
mode = IN_BODY;
|
|
373
|
+
};
|
|
374
|
+
const textMode = (t: Tag, state: number): void => {
|
|
375
|
+
insert(t.name, t.attrs);
|
|
376
|
+
raw = state;
|
|
377
|
+
original = mode;
|
|
378
|
+
mode = TEXT;
|
|
379
|
+
};
|
|
380
|
+
const endTemplate = (): void => {
|
|
381
|
+
if (!templates) return;
|
|
382
|
+
implied(undefined, THOROUGH);
|
|
383
|
+
popUntil('template');
|
|
384
|
+
clearToMarker();
|
|
385
|
+
templateModes.pop();
|
|
386
|
+
resetMode();
|
|
387
|
+
};
|
|
388
|
+
const clearToTable = (): void => popTo(top(TABLE_CONTEXT) + 1);
|
|
389
|
+
const clearToBody = (): void => popTo(top(BODY_CONTEXT) + 1);
|
|
390
|
+
const clearToRow = (): void => popTo(top(ROW_CONTEXT) + 1);
|
|
391
|
+
const closeCell = (): void => {
|
|
392
|
+
implied();
|
|
393
|
+
popUntil(CELLS);
|
|
394
|
+
clearToMarker();
|
|
395
|
+
mode = IN_ROW;
|
|
396
|
+
};
|
|
397
|
+
/** a token in a table that does not belong there: processed in body, with foster parenting */
|
|
398
|
+
const fostered = (f: () => void): void => {
|
|
399
|
+
const was = foster;
|
|
400
|
+
foster = true;
|
|
401
|
+
f();
|
|
402
|
+
foster = was;
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
// text
|
|
406
|
+
|
|
407
|
+
const bodyText = (s: string): void => {
|
|
408
|
+
s = noNul(s);
|
|
409
|
+
if (!s) return;
|
|
410
|
+
reconstruct();
|
|
411
|
+
insertText(s);
|
|
412
|
+
if (!isWhitespace(s)) framesetOk = false;
|
|
413
|
+
};
|
|
414
|
+
const flushTableText = (): void => {
|
|
415
|
+
const s = pending;
|
|
416
|
+
pending = '';
|
|
417
|
+
mode = original;
|
|
418
|
+
if (!isWhitespace(s)) fostered(() => bodyText(s));
|
|
419
|
+
else if (s) insertText(s);
|
|
420
|
+
};
|
|
421
|
+
const text = (s: string): void => {
|
|
422
|
+
for (;;) {
|
|
423
|
+
switch (mode) {
|
|
424
|
+
case INITIAL:
|
|
425
|
+
case BEFORE_HTML:
|
|
426
|
+
case BEFORE_HEAD: {
|
|
427
|
+
s = s.slice(leading(s));
|
|
428
|
+
if (!s) return;
|
|
429
|
+
anythingElse();
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
case IN_HEAD:
|
|
433
|
+
case AFTER_HEAD:
|
|
434
|
+
case IN_COLUMN_GROUP: {
|
|
435
|
+
const ws = leading(s);
|
|
436
|
+
if (ws) insertText(s.slice(0, ws));
|
|
437
|
+
s = s.slice(ws);
|
|
438
|
+
if (!s) return;
|
|
439
|
+
if (mode === IN_COLUMN_GROUP && !is(cur(), 'colgroup')) return;
|
|
440
|
+
anythingElse();
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
case TEXT: return insertText(s);
|
|
444
|
+
case IN_TABLE:
|
|
445
|
+
case IN_TABLE_BODY:
|
|
446
|
+
case IN_ROW: {
|
|
447
|
+
if (TABLE_STRUCTURE.has(cur().tag)) {
|
|
448
|
+
pending = '';
|
|
449
|
+
original = mode;
|
|
450
|
+
mode = IN_TABLE_TEXT;
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
return fostered(() => bodyText(s));
|
|
454
|
+
}
|
|
455
|
+
case IN_TABLE_TEXT: pending += noNul(s); return;
|
|
456
|
+
case IN_SELECT:
|
|
457
|
+
case IN_SELECT_IN_TABLE: {
|
|
458
|
+
s = noNul(s);
|
|
459
|
+
if (s) insertText(s);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
case AFTER_BODY:
|
|
463
|
+
case AFTER_AFTER_BODY: {
|
|
464
|
+
const ws = leading(s);
|
|
465
|
+
if (ws) bodyText(s.slice(0, ws));
|
|
466
|
+
s = s.slice(ws);
|
|
467
|
+
if (!s) return;
|
|
468
|
+
mode = IN_BODY;
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
case IN_FRAMESET:
|
|
472
|
+
case AFTER_FRAMESET:
|
|
473
|
+
case AFTER_AFTER_FRAMESET: {
|
|
474
|
+
s = s.replace(/[^\t\n\f\r ]+/g, '');
|
|
475
|
+
if (s) mode === AFTER_AFTER_FRAMESET ? bodyText(s) : insertText(s);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
default: return bodyText(s); // in body, caption, cell, template
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
/** the "anything else" of the modes before the body, whose effect does not depend on the token */
|
|
483
|
+
const anythingElse = (): void => {
|
|
484
|
+
switch (mode) {
|
|
485
|
+
case INITIAL: quirks = true; mode = BEFORE_HTML; return;
|
|
486
|
+
case BEFORE_HTML: push(root = create('html', [], HTML)); mode = BEFORE_HEAD; return;
|
|
487
|
+
case BEFORE_HEAD: head = insert('head'); mode = IN_HEAD; return;
|
|
488
|
+
case IN_HEAD: pop(); mode = AFTER_HEAD; return;
|
|
489
|
+
case AFTER_HEAD: insert('body'); mode = IN_BODY; return;
|
|
490
|
+
case IN_COLUMN_GROUP: pop(); mode = IN_TABLE; return;
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// start tags
|
|
495
|
+
|
|
496
|
+
const startTag = (t: Tag): void => {
|
|
497
|
+
const c = cur();
|
|
498
|
+
if (!c || c.ns === HTML || (t.name === 'svg' && c.ns === MATH && c.tag === 'annotation-xml')) return start(t);
|
|
499
|
+
if (point(c) && !((t.name === 'mglyph' || t.name === 'malignmark') && !htmlPoint(c))) return start(t);
|
|
500
|
+
if (EXITS.has(t.name) || (t.name === 'font' && t.attrs.some(([a]) => a === 'color' || a === 'face' || a === 'size'))) {
|
|
501
|
+
while (cur().ns !== HTML && !point(cur())) pop();
|
|
502
|
+
return start(t);
|
|
503
|
+
}
|
|
504
|
+
if (t.selfClosing) append(t.name, t.attrs, c.ns);
|
|
505
|
+
else insert(t.name, t.attrs, c.ns);
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
const start = (t: Tag): void => {
|
|
509
|
+
const name = t.name;
|
|
510
|
+
switch (mode) {
|
|
511
|
+
case INITIAL:
|
|
512
|
+
case BEFORE_HTML:
|
|
513
|
+
if (mode === BEFORE_HTML && name === 'html') {
|
|
514
|
+
push(root = create('html', t.attrs, HTML));
|
|
515
|
+
mode = BEFORE_HEAD;
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
anythingElse();
|
|
519
|
+
return start(t);
|
|
520
|
+
case BEFORE_HEAD:
|
|
521
|
+
if (name === 'html') return startInBody(t);
|
|
522
|
+
if (name === 'head') {
|
|
523
|
+
head = insert('head', t.attrs);
|
|
524
|
+
mode = IN_HEAD;
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
anythingElse();
|
|
528
|
+
return start(t);
|
|
529
|
+
case IN_HEAD:
|
|
530
|
+
if (startInHead(t)) return;
|
|
531
|
+
anythingElse();
|
|
532
|
+
return start(t);
|
|
533
|
+
case AFTER_HEAD:
|
|
534
|
+
switch (name) {
|
|
535
|
+
case 'html': return startInBody(t);
|
|
536
|
+
case 'body': insert(name, t.attrs); framesetOk = false; mode = IN_BODY; return;
|
|
537
|
+
case 'frameset': insert(name, t.attrs); mode = IN_FRAMESET; return;
|
|
538
|
+
case 'head': return;
|
|
539
|
+
}
|
|
540
|
+
if (IN_HEAD_TAGS.has(name)) {
|
|
541
|
+
push(head!);
|
|
542
|
+
startInHead(t);
|
|
543
|
+
removeAt(stack.lastIndexOf(head!));
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
anythingElse();
|
|
547
|
+
return startInBody(t);
|
|
548
|
+
case IN_BODY:
|
|
549
|
+
return startInBody(t);
|
|
550
|
+
case IN_TABLE:
|
|
551
|
+
return startInTable(t);
|
|
552
|
+
case IN_TABLE_TEXT:
|
|
553
|
+
flushTableText();
|
|
554
|
+
return startTag(t);
|
|
555
|
+
case IN_CAPTION:
|
|
556
|
+
if (!TABLE_PARTS.has(name)) return startInBody(t);
|
|
557
|
+
if (!inScope('caption', TABLE)) return;
|
|
558
|
+
implied();
|
|
559
|
+
popUntil('caption');
|
|
560
|
+
clearToMarker();
|
|
561
|
+
mode = IN_TABLE;
|
|
562
|
+
return startInTable(t);
|
|
563
|
+
case IN_COLUMN_GROUP:
|
|
564
|
+
return startInColumnGroup(t);
|
|
565
|
+
case IN_TABLE_BODY:
|
|
566
|
+
return startInTableBody(t);
|
|
567
|
+
case IN_ROW:
|
|
568
|
+
return startInRow(t);
|
|
569
|
+
case IN_CELL:
|
|
570
|
+
if (!TABLE_PARTS.has(name)) return startInBody(t);
|
|
571
|
+
if (inScope('td', TABLE) || inScope('th', TABLE)) {
|
|
572
|
+
closeCell();
|
|
573
|
+
startInRow(t);
|
|
574
|
+
}
|
|
575
|
+
return;
|
|
576
|
+
case IN_SELECT_IN_TABLE:
|
|
577
|
+
if (SELECT_ENDS.has(name)) {
|
|
578
|
+
popUntil('select');
|
|
579
|
+
resetMode();
|
|
580
|
+
return startTag(t);
|
|
581
|
+
}
|
|
582
|
+
return startInSelect(t);
|
|
583
|
+
case IN_SELECT:
|
|
584
|
+
return startInSelect(t);
|
|
585
|
+
case IN_TEMPLATE: {
|
|
586
|
+
if (IN_HEAD_TAGS.has(name)) return void startInHead(t);
|
|
587
|
+
const m = name === 'caption' || name === 'colgroup' || name === 'tbody' || name === 'tfoot' || name === 'thead' ? IN_TABLE
|
|
588
|
+
: name === 'col' ? IN_COLUMN_GROUP : name === 'tr' ? IN_TABLE_BODY : name === 'td' || name === 'th' ? IN_ROW : IN_BODY;
|
|
589
|
+
templateModes[templateModes.length - 1] = mode = m;
|
|
590
|
+
return start(t);
|
|
591
|
+
}
|
|
592
|
+
case AFTER_BODY:
|
|
593
|
+
case AFTER_AFTER_BODY:
|
|
594
|
+
if (name === 'html') return startInBody(t);
|
|
595
|
+
mode = IN_BODY;
|
|
596
|
+
return startInBody(t);
|
|
597
|
+
case IN_FRAMESET:
|
|
598
|
+
switch (name) {
|
|
599
|
+
case 'html': return startInBody(t);
|
|
600
|
+
case 'frameset': insert(name, t.attrs); return;
|
|
601
|
+
case 'frame': append(name, t.attrs); return;
|
|
602
|
+
case 'noframes': startInHead(t); return;
|
|
603
|
+
}
|
|
604
|
+
return;
|
|
605
|
+
default: // after frameset, after after frameset
|
|
606
|
+
if (name === 'html') return startInBody(t);
|
|
607
|
+
if (name === 'noframes') startInHead(t);
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
/** a start tag by the rules of "in head"; false for the "anything else" ones */
|
|
612
|
+
const startInHead = (t: Tag): boolean => {
|
|
613
|
+
switch (t.name) {
|
|
614
|
+
case 'html': startInBody(t); return true;
|
|
615
|
+
case 'base': case 'basefont': case 'bgsound': case 'link': case 'meta': append(t.name, t.attrs); return true;
|
|
616
|
+
case 'title': textMode(t, RCDATA); return true;
|
|
617
|
+
case 'noscript': case 'noframes': case 'style': textMode(t, RAWTEXT); return true;
|
|
618
|
+
case 'script': textMode(t, SCRIPT); return true;
|
|
619
|
+
case 'template':
|
|
620
|
+
insert(t.name, t.attrs);
|
|
621
|
+
list.push(null);
|
|
622
|
+
framesetOk = false;
|
|
623
|
+
mode = IN_TEMPLATE;
|
|
624
|
+
templateModes.push(IN_TEMPLATE);
|
|
625
|
+
return true;
|
|
626
|
+
case 'head': return true;
|
|
627
|
+
}
|
|
628
|
+
return false;
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
const startInBody = (t: Tag): void => {
|
|
632
|
+
const { name, attrs } = t;
|
|
633
|
+
if (FORMATTING.has(name)) {
|
|
634
|
+
reconstruct();
|
|
635
|
+
return listPush(insert(name, attrs));
|
|
636
|
+
}
|
|
637
|
+
if (BLOCK_START.has(name)) {
|
|
638
|
+
closePInButtonScope();
|
|
639
|
+
insert(name, attrs);
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
if (HEADINGS.has(name)) {
|
|
643
|
+
closePInButtonScope();
|
|
644
|
+
if (cur().ns === HTML && HEADINGS.has(cur().tag)) pop();
|
|
645
|
+
insert(name, attrs);
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
switch (name) {
|
|
649
|
+
case 'a': {
|
|
650
|
+
const a = listed('a');
|
|
651
|
+
if (a) {
|
|
652
|
+
adopt('a');
|
|
653
|
+
if (a.open) removeAt(stack.lastIndexOf(a));
|
|
654
|
+
if (a.listed) unlist(a);
|
|
655
|
+
}
|
|
656
|
+
reconstruct();
|
|
657
|
+
return listPush(insert(name, attrs));
|
|
658
|
+
}
|
|
659
|
+
case 'li': case 'dd': case 'dt': {
|
|
660
|
+
framesetOk = false;
|
|
661
|
+
const i = name === 'li' ? top('li') : Math.max(top('dd'), top('dt'));
|
|
662
|
+
if (i >= 0 && !stack.slice(i + 1).some((n) => n.bits & LIST_ITEM_STOP)) {
|
|
663
|
+
implied(stack[i].tag, THOROUGH);
|
|
664
|
+
popUntil(stack[i].tag);
|
|
665
|
+
}
|
|
666
|
+
closePInButtonScope();
|
|
667
|
+
insert(name, attrs);
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
case 'br': case 'img': case 'wbr': case 'area': case 'embed': case 'keygen': case 'image':
|
|
671
|
+
reconstruct();
|
|
672
|
+
append(name === 'image' ? 'img' : name, attrs);
|
|
673
|
+
framesetOk = false;
|
|
674
|
+
return;
|
|
675
|
+
case 'hr':
|
|
676
|
+
closePInButtonScope();
|
|
677
|
+
append(name, attrs);
|
|
678
|
+
framesetOk = false;
|
|
679
|
+
return;
|
|
680
|
+
case 'rb': case 'rtc':
|
|
681
|
+
if (inScope('ruby')) implied();
|
|
682
|
+
insert(name, attrs);
|
|
683
|
+
return;
|
|
684
|
+
case 'rt': case 'rp':
|
|
685
|
+
if (inScope('ruby')) implied('rtc', THOROUGH);
|
|
686
|
+
insert(name, attrs);
|
|
687
|
+
return;
|
|
688
|
+
case 'pre': case 'listing':
|
|
689
|
+
closePInButtonScope();
|
|
690
|
+
insert(name, attrs);
|
|
691
|
+
skipNewline = true;
|
|
692
|
+
framesetOk = false;
|
|
693
|
+
return;
|
|
694
|
+
case 'xmp':
|
|
695
|
+
closePInButtonScope();
|
|
696
|
+
reconstruct();
|
|
697
|
+
framesetOk = false;
|
|
698
|
+
return textMode(t, RAWTEXT);
|
|
699
|
+
case 'svg': case 'math': {
|
|
700
|
+
reconstruct();
|
|
701
|
+
const ns = name === 'svg' ? SVG : MATH;
|
|
702
|
+
if (t.selfClosing) append(name, attrs, ns);
|
|
703
|
+
else insert(name, attrs, ns);
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
case 'html':
|
|
707
|
+
if (!templates) mergeAttrs(stack[0], attrs);
|
|
708
|
+
return;
|
|
709
|
+
case 'body':
|
|
710
|
+
if (is(stack[1], 'body') && !templates) {
|
|
711
|
+
framesetOk = false;
|
|
712
|
+
mergeAttrs(stack[1], attrs);
|
|
713
|
+
}
|
|
714
|
+
return;
|
|
715
|
+
case 'form': {
|
|
716
|
+
if (form && !templates) return;
|
|
717
|
+
closePInButtonScope();
|
|
718
|
+
const f = insert(name, attrs);
|
|
719
|
+
if (!templates) form = f;
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
case 'nobr':
|
|
723
|
+
reconstruct();
|
|
724
|
+
if (inScope('nobr')) {
|
|
725
|
+
adopt('nobr');
|
|
726
|
+
reconstruct();
|
|
727
|
+
}
|
|
728
|
+
return listPush(insert(name, attrs));
|
|
729
|
+
case 'table':
|
|
730
|
+
if (!quirks) closePInButtonScope();
|
|
731
|
+
insert(name, attrs);
|
|
732
|
+
framesetOk = false;
|
|
733
|
+
mode = IN_TABLE;
|
|
734
|
+
return;
|
|
735
|
+
case 'input':
|
|
736
|
+
reconstruct();
|
|
737
|
+
append(name, attrs);
|
|
738
|
+
if (!hidden(attrs)) framesetOk = false;
|
|
739
|
+
return;
|
|
740
|
+
case 'param': case 'track': case 'source':
|
|
741
|
+
append(name, attrs);
|
|
742
|
+
return;
|
|
743
|
+
case 'button':
|
|
744
|
+
if (inScope('button')) {
|
|
745
|
+
implied();
|
|
746
|
+
popUntil('button');
|
|
747
|
+
}
|
|
748
|
+
reconstruct();
|
|
749
|
+
insert(name, attrs);
|
|
750
|
+
framesetOk = false;
|
|
751
|
+
return;
|
|
752
|
+
case 'applet': case 'object': case 'marquee':
|
|
753
|
+
reconstruct();
|
|
754
|
+
insert(name, attrs);
|
|
755
|
+
list.push(null);
|
|
756
|
+
framesetOk = false;
|
|
757
|
+
return;
|
|
758
|
+
case 'iframe':
|
|
759
|
+
framesetOk = false;
|
|
760
|
+
return textMode(t, RAWTEXT);
|
|
761
|
+
case 'noembed': case 'noframes': case 'noscript':
|
|
762
|
+
return textMode(t, RAWTEXT);
|
|
763
|
+
case 'select':
|
|
764
|
+
reconstruct();
|
|
765
|
+
insert(name, attrs);
|
|
766
|
+
framesetOk = false;
|
|
767
|
+
mode = mode === IN_TABLE || mode === IN_CAPTION || mode === IN_TABLE_BODY || mode === IN_ROW || mode === IN_CELL ? IN_SELECT_IN_TABLE : IN_SELECT;
|
|
768
|
+
return;
|
|
769
|
+
case 'option': case 'optgroup':
|
|
770
|
+
if (is(cur(), 'option')) pop();
|
|
771
|
+
reconstruct();
|
|
772
|
+
insert(name, attrs);
|
|
773
|
+
return;
|
|
774
|
+
case 'frameset':
|
|
775
|
+
if (framesetOk && is(stack[1], 'body')) {
|
|
776
|
+
detach(stack[1]);
|
|
777
|
+
popTo(1);
|
|
778
|
+
insert(name, attrs);
|
|
779
|
+
mode = IN_FRAMESET;
|
|
780
|
+
}
|
|
781
|
+
return;
|
|
782
|
+
case 'textarea':
|
|
783
|
+
insert(name, attrs);
|
|
784
|
+
skipNewline = true;
|
|
785
|
+
raw = RCDATA;
|
|
786
|
+
original = mode;
|
|
787
|
+
framesetOk = false;
|
|
788
|
+
mode = TEXT;
|
|
789
|
+
return;
|
|
790
|
+
case 'plaintext':
|
|
791
|
+
closePInButtonScope();
|
|
792
|
+
insert(name, attrs);
|
|
793
|
+
raw = PLAINTEXT;
|
|
794
|
+
return;
|
|
795
|
+
case 'base': case 'basefont': case 'bgsound': case 'link': case 'meta': case 'script': case 'style': case 'template': case 'title':
|
|
796
|
+
startInHead(t);
|
|
797
|
+
return;
|
|
798
|
+
case 'caption': case 'col': case 'colgroup': case 'frame': case 'head': case 'tbody': case 'td': case 'tfoot': case 'th': case 'thead': case 'tr':
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
reconstruct();
|
|
802
|
+
insert(name, attrs);
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
const startInTable = (t: Tag): void => {
|
|
806
|
+
const { name, attrs } = t;
|
|
807
|
+
switch (name) {
|
|
808
|
+
case 'td': case 'th': case 'tr':
|
|
809
|
+
clearToTable();
|
|
810
|
+
insert('tbody');
|
|
811
|
+
mode = IN_TABLE_BODY;
|
|
812
|
+
return startInTableBody(t);
|
|
813
|
+
case 'style': case 'script': case 'template':
|
|
814
|
+
startInHead(t);
|
|
815
|
+
return;
|
|
816
|
+
case 'col':
|
|
817
|
+
clearToTable();
|
|
818
|
+
insert('colgroup');
|
|
819
|
+
mode = IN_COLUMN_GROUP;
|
|
820
|
+
return startInColumnGroup(t);
|
|
821
|
+
case 'form':
|
|
822
|
+
if (!form && !templates) {
|
|
823
|
+
form = insert(name, attrs);
|
|
824
|
+
pop();
|
|
825
|
+
}
|
|
826
|
+
return;
|
|
827
|
+
case 'table':
|
|
828
|
+
if (inScope('table', TABLE)) {
|
|
829
|
+
popUntil('table');
|
|
830
|
+
resetMode();
|
|
831
|
+
startTag(t);
|
|
832
|
+
}
|
|
833
|
+
return;
|
|
834
|
+
case 'tbody': case 'tfoot': case 'thead':
|
|
835
|
+
clearToTable();
|
|
836
|
+
insert(name, attrs);
|
|
837
|
+
mode = IN_TABLE_BODY;
|
|
838
|
+
return;
|
|
839
|
+
case 'input':
|
|
840
|
+
if (hidden(attrs)) return append(name, attrs);
|
|
841
|
+
break;
|
|
842
|
+
case 'caption':
|
|
843
|
+
clearToTable();
|
|
844
|
+
list.push(null);
|
|
845
|
+
insert(name, attrs);
|
|
846
|
+
mode = IN_CAPTION;
|
|
847
|
+
return;
|
|
848
|
+
case 'colgroup':
|
|
849
|
+
clearToTable();
|
|
850
|
+
insert(name, attrs);
|
|
851
|
+
mode = IN_COLUMN_GROUP;
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
fostered(() => startInBody(t));
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
const startInColumnGroup = (t: Tag): void => {
|
|
858
|
+
switch (t.name) {
|
|
859
|
+
case 'html': return startInBody(t);
|
|
860
|
+
case 'col': return append(t.name, t.attrs);
|
|
861
|
+
case 'template': startInHead(t); return;
|
|
862
|
+
}
|
|
863
|
+
if (!is(cur(), 'colgroup')) return;
|
|
864
|
+
anythingElse();
|
|
865
|
+
startTag(t);
|
|
866
|
+
};
|
|
867
|
+
|
|
868
|
+
const startInTableBody = (t: Tag): void => {
|
|
869
|
+
const name = t.name;
|
|
870
|
+
if (name === 'tr') {
|
|
871
|
+
clearToBody();
|
|
872
|
+
insert(name, t.attrs);
|
|
873
|
+
mode = IN_ROW;
|
|
874
|
+
} else if (name === 'th' || name === 'td') {
|
|
875
|
+
clearToBody();
|
|
876
|
+
insert('tr');
|
|
877
|
+
mode = IN_ROW;
|
|
878
|
+
startInRow(t);
|
|
879
|
+
} else if (TABLE_PARTS.has(name) && name !== 'td' && name !== 'th') {
|
|
880
|
+
if (!inScope(TABLE_BODIES, TABLE)) return;
|
|
881
|
+
clearToBody();
|
|
882
|
+
pop();
|
|
883
|
+
mode = IN_TABLE;
|
|
884
|
+
startInTable(t);
|
|
885
|
+
} else startInTable(t);
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
const startInRow = (t: Tag): void => {
|
|
889
|
+
const name = t.name;
|
|
890
|
+
if (name === 'th' || name === 'td') {
|
|
891
|
+
clearToRow();
|
|
892
|
+
insert(name, t.attrs);
|
|
893
|
+
mode = IN_CELL;
|
|
894
|
+
list.push(null);
|
|
895
|
+
} else if (TABLE_PARTS.has(name)) {
|
|
896
|
+
if (!inScope('tr', TABLE)) return;
|
|
897
|
+
clearToRow();
|
|
898
|
+
pop();
|
|
899
|
+
mode = IN_TABLE_BODY;
|
|
900
|
+
startInTableBody(t);
|
|
901
|
+
} else startInTable(t);
|
|
902
|
+
};
|
|
903
|
+
|
|
904
|
+
const startInSelect = (t: Tag): void => {
|
|
905
|
+
const name = t.name;
|
|
906
|
+
switch (name) {
|
|
907
|
+
case 'html': return startInBody(t);
|
|
908
|
+
case 'option':
|
|
909
|
+
if (is(cur(), 'option')) pop();
|
|
910
|
+
insert(name, t.attrs);
|
|
911
|
+
return;
|
|
912
|
+
case 'optgroup': case 'hr':
|
|
913
|
+
if (is(cur(), 'option')) pop();
|
|
914
|
+
if (is(cur(), 'optgroup')) pop();
|
|
915
|
+
if (name === 'hr') append(name, t.attrs);
|
|
916
|
+
else insert(name, t.attrs);
|
|
917
|
+
return;
|
|
918
|
+
case 'input': case 'keygen': case 'textarea': case 'select':
|
|
919
|
+
if (inSelectScope('select')) {
|
|
920
|
+
popUntil('select');
|
|
921
|
+
resetMode();
|
|
922
|
+
if (name !== 'select') startTag(t);
|
|
923
|
+
}
|
|
924
|
+
return;
|
|
925
|
+
case 'script': case 'template':
|
|
926
|
+
startInHead(t);
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
|
|
930
|
+
// end tags
|
|
931
|
+
|
|
932
|
+
const endTag = (name: string): void => {
|
|
933
|
+
const c = cur();
|
|
934
|
+
if (!c || c.ns === HTML) return end(name);
|
|
935
|
+
if (name === 'p' || name === 'br') {
|
|
936
|
+
while (cur().ns !== HTML && !point(cur())) pop();
|
|
937
|
+
return end(name);
|
|
938
|
+
}
|
|
939
|
+
// the topmost foreign element with this name closes, unless an HTML element is open above it
|
|
940
|
+
for (let i = stack.length - 1; i > 0; i--) {
|
|
941
|
+
const n = stack[i];
|
|
942
|
+
if (n.ns === HTML) break;
|
|
943
|
+
if (n.tag === name) return popTo(i);
|
|
944
|
+
}
|
|
945
|
+
end(name);
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
const end = (name: string): void => {
|
|
949
|
+
switch (mode) {
|
|
950
|
+
case INITIAL:
|
|
951
|
+
anythingElse();
|
|
952
|
+
return end(name);
|
|
953
|
+
case BEFORE_HTML:
|
|
954
|
+
case BEFORE_HEAD:
|
|
955
|
+
if (name !== 'html' && name !== 'head' && name !== 'body' && name !== 'br') return;
|
|
956
|
+
anythingElse();
|
|
957
|
+
return end(name);
|
|
958
|
+
case IN_HEAD:
|
|
959
|
+
if (name === 'head') {
|
|
960
|
+
pop();
|
|
961
|
+
mode = AFTER_HEAD;
|
|
962
|
+
} else if (name === 'template') endTemplate();
|
|
963
|
+
else if (name === 'body' || name === 'html' || name === 'br') {
|
|
964
|
+
anythingElse();
|
|
965
|
+
end(name);
|
|
966
|
+
}
|
|
967
|
+
return;
|
|
968
|
+
case AFTER_HEAD:
|
|
969
|
+
if (name === 'template') endTemplate();
|
|
970
|
+
else if (name === 'body' || name === 'html' || name === 'br') {
|
|
971
|
+
anythingElse();
|
|
972
|
+
endInBody(name);
|
|
973
|
+
}
|
|
974
|
+
return;
|
|
975
|
+
case IN_BODY:
|
|
976
|
+
return endInBody(name);
|
|
977
|
+
case TEXT:
|
|
978
|
+
pop();
|
|
979
|
+
mode = original;
|
|
980
|
+
return;
|
|
981
|
+
case IN_TABLE:
|
|
982
|
+
return endInTable(name);
|
|
983
|
+
case IN_TABLE_TEXT:
|
|
984
|
+
flushTableText();
|
|
985
|
+
return endTag(name);
|
|
986
|
+
case IN_CAPTION:
|
|
987
|
+
if (name === 'caption' || name === 'table') {
|
|
988
|
+
if (!inScope('caption', TABLE)) return;
|
|
989
|
+
implied();
|
|
990
|
+
popUntil('caption');
|
|
991
|
+
clearToMarker();
|
|
992
|
+
mode = IN_TABLE;
|
|
993
|
+
if (name === 'table') endInTable(name);
|
|
994
|
+
} else if (!TABLE_PARTS.has(name) && name !== 'body' && name !== 'html') endInBody(name);
|
|
995
|
+
return;
|
|
996
|
+
case IN_COLUMN_GROUP:
|
|
997
|
+
if (name === 'colgroup') {
|
|
998
|
+
if (is(cur(), 'colgroup')) {
|
|
999
|
+
pop();
|
|
1000
|
+
mode = IN_TABLE;
|
|
1001
|
+
}
|
|
1002
|
+
} else if (name === 'template') endTemplate();
|
|
1003
|
+
else if (name !== 'col' && is(cur(), 'colgroup')) {
|
|
1004
|
+
anythingElse();
|
|
1005
|
+
endTag(name);
|
|
1006
|
+
}
|
|
1007
|
+
return;
|
|
1008
|
+
case IN_TABLE_BODY:
|
|
1009
|
+
return endInTableBody(name);
|
|
1010
|
+
case IN_ROW:
|
|
1011
|
+
return endInRow(name);
|
|
1012
|
+
case IN_CELL:
|
|
1013
|
+
if (name === 'td' || name === 'th') {
|
|
1014
|
+
if (!inScope(name, TABLE)) return;
|
|
1015
|
+
implied();
|
|
1016
|
+
popUntil(name);
|
|
1017
|
+
clearToMarker();
|
|
1018
|
+
mode = IN_ROW;
|
|
1019
|
+
} else if (TABLE_STRUCTURE.has(name)) {
|
|
1020
|
+
if (!inScope(name, TABLE)) return;
|
|
1021
|
+
closeCell();
|
|
1022
|
+
endInRow(name);
|
|
1023
|
+
} else if (name !== 'body' && name !== 'caption' && name !== 'col' && name !== 'colgroup' && name !== 'html') endInBody(name);
|
|
1024
|
+
return;
|
|
1025
|
+
case IN_SELECT_IN_TABLE:
|
|
1026
|
+
if (SELECT_ENDS.has(name)) {
|
|
1027
|
+
if (inScope(name, TABLE)) {
|
|
1028
|
+
popUntil('select');
|
|
1029
|
+
resetMode();
|
|
1030
|
+
endTag(name);
|
|
1031
|
+
}
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
return endInSelect(name);
|
|
1035
|
+
case IN_SELECT:
|
|
1036
|
+
return endInSelect(name);
|
|
1037
|
+
case IN_TEMPLATE:
|
|
1038
|
+
if (name === 'template') endTemplate();
|
|
1039
|
+
return;
|
|
1040
|
+
case AFTER_BODY:
|
|
1041
|
+
if (name === 'html') mode = AFTER_AFTER_BODY;
|
|
1042
|
+
else {
|
|
1043
|
+
mode = IN_BODY;
|
|
1044
|
+
endInBody(name);
|
|
1045
|
+
}
|
|
1046
|
+
return;
|
|
1047
|
+
case IN_FRAMESET:
|
|
1048
|
+
if (name === 'frameset' && stack.length > 1) {
|
|
1049
|
+
pop();
|
|
1050
|
+
if (!is(cur(), 'frameset')) mode = AFTER_FRAMESET;
|
|
1051
|
+
}
|
|
1052
|
+
return;
|
|
1053
|
+
case AFTER_FRAMESET:
|
|
1054
|
+
if (name === 'html') mode = AFTER_AFTER_FRAMESET;
|
|
1055
|
+
return;
|
|
1056
|
+
case AFTER_AFTER_BODY:
|
|
1057
|
+
mode = IN_BODY;
|
|
1058
|
+
return endInBody(name);
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1061
|
+
|
|
1062
|
+
const endInBody = (name: string): void => {
|
|
1063
|
+
if (FORMATTING.has(name) || name === 'a' || name === 'nobr') return adopt(name);
|
|
1064
|
+
if (BLOCK_END.has(name)) {
|
|
1065
|
+
if (inScope(name)) {
|
|
1066
|
+
implied();
|
|
1067
|
+
popUntil(name);
|
|
1068
|
+
}
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
if (HEADINGS.has(name)) {
|
|
1072
|
+
if (inScope(HEADINGS)) {
|
|
1073
|
+
implied();
|
|
1074
|
+
popUntil(HEADINGS);
|
|
1075
|
+
}
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
switch (name) {
|
|
1079
|
+
case 'p':
|
|
1080
|
+
if (!inScope('p', BUTTON)) insert('p');
|
|
1081
|
+
return closeP();
|
|
1082
|
+
case 'li':
|
|
1083
|
+
if (inScope('li', LIST_ITEM)) {
|
|
1084
|
+
implied('li', THOROUGH);
|
|
1085
|
+
popUntil('li');
|
|
1086
|
+
}
|
|
1087
|
+
return;
|
|
1088
|
+
case 'dd': case 'dt':
|
|
1089
|
+
if (inScope(name)) {
|
|
1090
|
+
implied(name, THOROUGH);
|
|
1091
|
+
popUntil(name);
|
|
1092
|
+
}
|
|
1093
|
+
return;
|
|
1094
|
+
case 'br':
|
|
1095
|
+
reconstruct();
|
|
1096
|
+
insert('br');
|
|
1097
|
+
pop();
|
|
1098
|
+
framesetOk = false;
|
|
1099
|
+
return;
|
|
1100
|
+
case 'body':
|
|
1101
|
+
if (inScope('body')) mode = AFTER_BODY;
|
|
1102
|
+
return;
|
|
1103
|
+
case 'html':
|
|
1104
|
+
if (inScope('body')) mode = AFTER_AFTER_BODY;
|
|
1105
|
+
return;
|
|
1106
|
+
case 'form': {
|
|
1107
|
+
const f = form;
|
|
1108
|
+
if (!templates) form = null;
|
|
1109
|
+
if ((f || templates) && inScope('form')) {
|
|
1110
|
+
implied();
|
|
1111
|
+
if (templates) popUntil('form');
|
|
1112
|
+
else removeAt(stack.lastIndexOf(f!));
|
|
1113
|
+
}
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
case 'applet': case 'object': case 'marquee':
|
|
1117
|
+
if (inScope(name)) {
|
|
1118
|
+
implied();
|
|
1119
|
+
popUntil(name);
|
|
1120
|
+
clearToMarker();
|
|
1121
|
+
}
|
|
1122
|
+
return;
|
|
1123
|
+
case 'template':
|
|
1124
|
+
return endTemplate();
|
|
1125
|
+
}
|
|
1126
|
+
endInBodyOther(name);
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1129
|
+
/** "any other end tag": the topmost element with this name closes, unless a special element is above it */
|
|
1130
|
+
const endInBodyOther = (name: string): void => {
|
|
1131
|
+
const i = top(name);
|
|
1132
|
+
if (i <= 0) return;
|
|
1133
|
+
for (let k = i + 1; k < stack.length; k++) if (special(stack[k])) return;
|
|
1134
|
+
implied(name, THOROUGH);
|
|
1135
|
+
popTo(i);
|
|
1136
|
+
};
|
|
1137
|
+
|
|
1138
|
+
const endInTable = (name: string): void => {
|
|
1139
|
+
if (name === 'table') {
|
|
1140
|
+
if (inScope('table', TABLE)) {
|
|
1141
|
+
popUntil('table');
|
|
1142
|
+
resetMode();
|
|
1143
|
+
}
|
|
1144
|
+
} else if (name === 'template') endTemplate();
|
|
1145
|
+
else if (!TABLE_PARTS.has(name) && name !== 'body' && name !== 'html') fostered(() => endInBody(name));
|
|
1146
|
+
};
|
|
1147
|
+
|
|
1148
|
+
const endInTableBody = (name: string): void => {
|
|
1149
|
+
if (name === 'tbody' || name === 'tfoot' || name === 'thead') {
|
|
1150
|
+
if (!inScope(name, TABLE)) return;
|
|
1151
|
+
clearToBody();
|
|
1152
|
+
pop();
|
|
1153
|
+
mode = IN_TABLE;
|
|
1154
|
+
} else if (name === 'table') {
|
|
1155
|
+
if (!inScope(TABLE_BODIES, TABLE)) return;
|
|
1156
|
+
clearToBody();
|
|
1157
|
+
pop();
|
|
1158
|
+
mode = IN_TABLE;
|
|
1159
|
+
endInTable(name);
|
|
1160
|
+
} else if (!TABLE_PARTS.has(name) && name !== 'body' && name !== 'html') endInTable(name);
|
|
1161
|
+
};
|
|
1162
|
+
|
|
1163
|
+
const endInRow = (name: string): void => {
|
|
1164
|
+
if (name === 'tr' || name === 'table' || name === 'tbody' || name === 'tfoot' || name === 'thead') {
|
|
1165
|
+
if (!inScope('tr', TABLE) && !(name !== 'tr' && name !== 'table' && inScope(name, TABLE))) return;
|
|
1166
|
+
clearToRow();
|
|
1167
|
+
pop();
|
|
1168
|
+
mode = IN_TABLE_BODY;
|
|
1169
|
+
if (name !== 'tr') endInTableBody(name);
|
|
1170
|
+
} else if (!TABLE_PARTS.has(name) && name !== 'body' && name !== 'html') endInTable(name);
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
const endInSelect = (name: string): void => {
|
|
1174
|
+
switch (name) {
|
|
1175
|
+
case 'optgroup':
|
|
1176
|
+
if (is(cur(), 'option') && is(stack[stack.length - 2], 'optgroup')) pop();
|
|
1177
|
+
if (is(cur(), 'optgroup')) pop();
|
|
1178
|
+
return;
|
|
1179
|
+
case 'option':
|
|
1180
|
+
if (is(cur(), 'option')) pop();
|
|
1181
|
+
return;
|
|
1182
|
+
case 'select':
|
|
1183
|
+
if (inSelectScope('select')) {
|
|
1184
|
+
popUntil('select');
|
|
1185
|
+
resetMode();
|
|
1186
|
+
}
|
|
1187
|
+
return;
|
|
1188
|
+
case 'template':
|
|
1189
|
+
endTemplate();
|
|
1190
|
+
}
|
|
1191
|
+
};
|
|
1192
|
+
|
|
1193
|
+
// the end of the input
|
|
1194
|
+
|
|
1195
|
+
const eof = (): void => {
|
|
1196
|
+
for (;;) {
|
|
1197
|
+
switch (mode) {
|
|
1198
|
+
case INITIAL: case BEFORE_HTML: case BEFORE_HEAD: case IN_HEAD: case AFTER_HEAD:
|
|
1199
|
+
anythingElse();
|
|
1200
|
+
continue;
|
|
1201
|
+
case TEXT:
|
|
1202
|
+
pop();
|
|
1203
|
+
mode = original;
|
|
1204
|
+
continue;
|
|
1205
|
+
case IN_TABLE_TEXT:
|
|
1206
|
+
flushTableText();
|
|
1207
|
+
continue;
|
|
1208
|
+
case IN_TEMPLATE:
|
|
1209
|
+
case IN_BODY: case IN_TABLE: case IN_CAPTION: case IN_COLUMN_GROUP: case IN_TABLE_BODY: case IN_ROW: case IN_CELL: case IN_SELECT: case IN_SELECT_IN_TABLE:
|
|
1210
|
+
if (mode !== IN_TEMPLATE && !templateModes.length) return;
|
|
1211
|
+
if (!templates) return;
|
|
1212
|
+
while (templates) {
|
|
1213
|
+
popUntil('template');
|
|
1214
|
+
clearToMarker();
|
|
1215
|
+
templateModes.pop();
|
|
1216
|
+
}
|
|
1217
|
+
resetMode();
|
|
1218
|
+
continue;
|
|
1219
|
+
default:
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
};
|
|
1224
|
+
|
|
1225
|
+
const sink: Sink = {
|
|
1226
|
+
text(s) {
|
|
1227
|
+
if (skipNewline) {
|
|
1228
|
+
skipNewline = false;
|
|
1229
|
+
if (s[0] === '\n') s = s.slice(1);
|
|
1230
|
+
if (!s) return;
|
|
1231
|
+
}
|
|
1232
|
+
if (!inForeign()) return text(s);
|
|
1233
|
+
// foreign content: NUL is U+FFFD, and only characters other than whitespace and NUL end frameset-ok
|
|
1234
|
+
if (!isWhitespace(noNul(s))) framesetOk = false;
|
|
1235
|
+
insertText(s.replace(/\0/g, '\uFFFD'));
|
|
1236
|
+
},
|
|
1237
|
+
start(name, attrs, selfClosing) {
|
|
1238
|
+
skipNewline = false;
|
|
1239
|
+
raw = DATA;
|
|
1240
|
+
startTag({ name, attrs, selfClosing });
|
|
1241
|
+
return raw;
|
|
1242
|
+
},
|
|
1243
|
+
end(name) {
|
|
1244
|
+
skipNewline = false;
|
|
1245
|
+
endTag(name);
|
|
1246
|
+
},
|
|
1247
|
+
comment() {
|
|
1248
|
+
skipNewline = false;
|
|
1249
|
+
if (mode === IN_TABLE_TEXT && (!cur() || cur().ns === HTML)) flushTableText();
|
|
1250
|
+
},
|
|
1251
|
+
doctype(name, publicId, systemId, force) {
|
|
1252
|
+
skipNewline = false;
|
|
1253
|
+
if (mode === INITIAL) {
|
|
1254
|
+
quirks = quirky(name, publicId, systemId, force);
|
|
1255
|
+
mode = BEFORE_HTML;
|
|
1256
|
+
} else if (mode === IN_TABLE_TEXT) flushTableText();
|
|
1257
|
+
},
|
|
1258
|
+
cdata: inForeign,
|
|
1259
|
+
eof,
|
|
1260
|
+
};
|
|
1261
|
+
tokenize(html, sink);
|
|
1262
|
+
return root!;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
const hidden = (attrs: Attrs): boolean => attrs.some(([n, v]) => n === 'type' && v.toLowerCase() === 'hidden');
|