@cjser/parse5 0.0.0-stage → 8.0.1-cjser.2

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.
@@ -0,0 +1,3245 @@
1
+ import { Tokenizer, TokenizerMode } from '../tokenizer/index.js';
2
+ import { OpenElementStack } from './open-element-stack.js';
3
+ import { FormattingElementList, EntryType } from './formatting-element-list.js';
4
+ import { defaultTreeAdapter } from '../tree-adapters/default.js';
5
+ import * as doctype from '../common/doctype.js';
6
+ import * as foreignContent from '../common/foreign-content.js';
7
+ import { ERR } from '../common/error-codes.js';
8
+ import * as unicode from '../common/unicode.js';
9
+ import { TAG_ID as $, TAG_NAMES as TN, NS, ATTRS, SPECIAL_ELEMENTS, DOCUMENT_MODE, NUMBERED_HEADERS, getTagID, } from '../common/html.js';
10
+ import { TokenType, getTokenAttr, } from '../common/token.js';
11
+ //Misc constants
12
+ const HIDDEN_INPUT_TYPE = 'hidden';
13
+ //Adoption agency loops iteration count
14
+ const AA_OUTER_LOOP_ITER = 8;
15
+ const AA_INNER_LOOP_ITER = 3;
16
+ //Insertion modes
17
+ var InsertionMode;
18
+ (function (InsertionMode) {
19
+ InsertionMode[InsertionMode["INITIAL"] = 0] = "INITIAL";
20
+ InsertionMode[InsertionMode["BEFORE_HTML"] = 1] = "BEFORE_HTML";
21
+ InsertionMode[InsertionMode["BEFORE_HEAD"] = 2] = "BEFORE_HEAD";
22
+ InsertionMode[InsertionMode["IN_HEAD"] = 3] = "IN_HEAD";
23
+ InsertionMode[InsertionMode["IN_HEAD_NO_SCRIPT"] = 4] = "IN_HEAD_NO_SCRIPT";
24
+ InsertionMode[InsertionMode["AFTER_HEAD"] = 5] = "AFTER_HEAD";
25
+ InsertionMode[InsertionMode["IN_BODY"] = 6] = "IN_BODY";
26
+ InsertionMode[InsertionMode["TEXT"] = 7] = "TEXT";
27
+ InsertionMode[InsertionMode["IN_TABLE"] = 8] = "IN_TABLE";
28
+ InsertionMode[InsertionMode["IN_TABLE_TEXT"] = 9] = "IN_TABLE_TEXT";
29
+ InsertionMode[InsertionMode["IN_CAPTION"] = 10] = "IN_CAPTION";
30
+ InsertionMode[InsertionMode["IN_COLUMN_GROUP"] = 11] = "IN_COLUMN_GROUP";
31
+ InsertionMode[InsertionMode["IN_TABLE_BODY"] = 12] = "IN_TABLE_BODY";
32
+ InsertionMode[InsertionMode["IN_ROW"] = 13] = "IN_ROW";
33
+ InsertionMode[InsertionMode["IN_CELL"] = 14] = "IN_CELL";
34
+ InsertionMode[InsertionMode["IN_SELECT"] = 15] = "IN_SELECT";
35
+ InsertionMode[InsertionMode["IN_SELECT_IN_TABLE"] = 16] = "IN_SELECT_IN_TABLE";
36
+ InsertionMode[InsertionMode["IN_TEMPLATE"] = 17] = "IN_TEMPLATE";
37
+ InsertionMode[InsertionMode["AFTER_BODY"] = 18] = "AFTER_BODY";
38
+ InsertionMode[InsertionMode["IN_FRAMESET"] = 19] = "IN_FRAMESET";
39
+ InsertionMode[InsertionMode["AFTER_FRAMESET"] = 20] = "AFTER_FRAMESET";
40
+ InsertionMode[InsertionMode["AFTER_AFTER_BODY"] = 21] = "AFTER_AFTER_BODY";
41
+ InsertionMode[InsertionMode["AFTER_AFTER_FRAMESET"] = 22] = "AFTER_AFTER_FRAMESET";
42
+ })(InsertionMode || (InsertionMode = {}));
43
+ const BASE_LOC = {
44
+ startLine: -1,
45
+ startCol: -1,
46
+ startOffset: -1,
47
+ endLine: -1,
48
+ endCol: -1,
49
+ endOffset: -1,
50
+ };
51
+ const TABLE_STRUCTURE_TAGS = new Set([$.TABLE, $.TBODY, $.TFOOT, $.THEAD, $.TR]);
52
+ const defaultParserOptions = {
53
+ scriptingEnabled: true,
54
+ sourceCodeLocationInfo: false,
55
+ treeAdapter: defaultTreeAdapter,
56
+ onParseError: null,
57
+ };
58
+ //Parser
59
+ export class Parser {
60
+ constructor(options, document,
61
+ /** @internal */
62
+ fragmentContext = null,
63
+ /** @internal */
64
+ scriptHandler = null) {
65
+ this.fragmentContext = fragmentContext;
66
+ this.scriptHandler = scriptHandler;
67
+ this.currentToken = null;
68
+ this.stopped = false;
69
+ /** @internal */
70
+ this.insertionMode = InsertionMode.INITIAL;
71
+ /** @internal */
72
+ this.originalInsertionMode = InsertionMode.INITIAL;
73
+ /** @internal */
74
+ this.headElement = null;
75
+ /** @internal */
76
+ this.formElement = null;
77
+ /** Indicates that the current node is not an element in the HTML namespace */
78
+ this.currentNotInHTML = false;
79
+ /**
80
+ * The template insertion mode stack is maintained from the left.
81
+ * Ie. the topmost element will always have index 0.
82
+ *
83
+ * @internal
84
+ */
85
+ this.tmplInsertionModeStack = [];
86
+ /** @internal */
87
+ this.pendingCharacterTokens = [];
88
+ /** @internal */
89
+ this.hasNonWhitespacePendingCharacterToken = false;
90
+ /** @internal */
91
+ this.framesetOk = true;
92
+ /** @internal */
93
+ this.skipNextNewLine = false;
94
+ /** @internal */
95
+ this.fosterParentingEnabled = false;
96
+ this.options = {
97
+ ...defaultParserOptions,
98
+ ...options,
99
+ };
100
+ this.treeAdapter = this.options.treeAdapter;
101
+ this.onParseError = this.options.onParseError;
102
+ // Always enable location info if we report parse errors.
103
+ if (this.onParseError) {
104
+ this.options.sourceCodeLocationInfo = true;
105
+ }
106
+ this.document = document !== null && document !== void 0 ? document : this.treeAdapter.createDocument();
107
+ this.tokenizer = new Tokenizer(this.options, this);
108
+ this.activeFormattingElements = new FormattingElementList(this.treeAdapter);
109
+ this.fragmentContextID = fragmentContext ? getTagID(this.treeAdapter.getTagName(fragmentContext)) : $.UNKNOWN;
110
+ this._setContextModes(fragmentContext !== null && fragmentContext !== void 0 ? fragmentContext : this.document, this.fragmentContextID);
111
+ this.openElements = new OpenElementStack(this.document, this.treeAdapter, this);
112
+ }
113
+ // API
114
+ static parse(html, options) {
115
+ const parser = new this(options);
116
+ parser.tokenizer.write(html, true);
117
+ return parser.document;
118
+ }
119
+ static getFragmentParser(fragmentContext, options) {
120
+ const opts = {
121
+ ...defaultParserOptions,
122
+ ...options,
123
+ };
124
+ //NOTE: use a <template> element as the fragment context if no context element was provided,
125
+ //so we will parse in a "forgiving" manner
126
+ fragmentContext !== null && fragmentContext !== void 0 ? fragmentContext : (fragmentContext = opts.treeAdapter.createElement(TN.TEMPLATE, NS.HTML, []));
127
+ //NOTE: create a fake element which will be used as the `document` for fragment parsing.
128
+ //This is important for jsdom, where a new `document` cannot be created. This led to
129
+ //fragment parsing messing with the main `document`.
130
+ const documentMock = opts.treeAdapter.createElement('documentmock', NS.HTML, []);
131
+ const parser = new this(opts, documentMock, fragmentContext);
132
+ if (parser.fragmentContextID === $.TEMPLATE) {
133
+ parser.tmplInsertionModeStack.unshift(InsertionMode.IN_TEMPLATE);
134
+ }
135
+ parser._initTokenizerForFragmentParsing();
136
+ parser._insertFakeRootElement();
137
+ parser._resetInsertionMode();
138
+ parser._findFormInFragmentContext();
139
+ return parser;
140
+ }
141
+ getFragment() {
142
+ const rootElement = this.treeAdapter.getFirstChild(this.document);
143
+ const fragment = this.treeAdapter.createDocumentFragment();
144
+ this._adoptNodes(rootElement, fragment);
145
+ return fragment;
146
+ }
147
+ //Errors
148
+ /** @internal */
149
+ _err(token, code, beforeToken) {
150
+ var _a;
151
+ if (!this.onParseError)
152
+ return;
153
+ const loc = (_a = token.location) !== null && _a !== void 0 ? _a : BASE_LOC;
154
+ const err = {
155
+ code,
156
+ startLine: loc.startLine,
157
+ startCol: loc.startCol,
158
+ startOffset: loc.startOffset,
159
+ endLine: beforeToken ? loc.startLine : loc.endLine,
160
+ endCol: beforeToken ? loc.startCol : loc.endCol,
161
+ endOffset: beforeToken ? loc.startOffset : loc.endOffset,
162
+ };
163
+ this.onParseError(err);
164
+ }
165
+ //Stack events
166
+ /** @internal */
167
+ onItemPush(node, tid, isTop) {
168
+ var _a, _b;
169
+ (_b = (_a = this.treeAdapter).onItemPush) === null || _b === void 0 ? void 0 : _b.call(_a, node);
170
+ if (isTop && this.openElements.stackTop > 0)
171
+ this._setContextModes(node, tid);
172
+ }
173
+ /** @internal */
174
+ onItemPop(node, isTop) {
175
+ var _a, _b;
176
+ if (this.options.sourceCodeLocationInfo) {
177
+ this._setEndLocation(node, this.currentToken);
178
+ }
179
+ (_b = (_a = this.treeAdapter).onItemPop) === null || _b === void 0 ? void 0 : _b.call(_a, node, this.openElements.current);
180
+ if (isTop) {
181
+ let current;
182
+ let currentTagId;
183
+ if (this.openElements.stackTop === 0 && this.fragmentContext) {
184
+ current = this.fragmentContext;
185
+ currentTagId = this.fragmentContextID;
186
+ }
187
+ else {
188
+ ({ current, currentTagId } = this.openElements);
189
+ }
190
+ this._setContextModes(current, currentTagId);
191
+ }
192
+ }
193
+ _setContextModes(current, tid) {
194
+ const isHTML = current === this.document || (current && this.treeAdapter.getNamespaceURI(current) === NS.HTML);
195
+ this.currentNotInHTML = !isHTML;
196
+ this.tokenizer.inForeignNode =
197
+ !isHTML && current !== undefined && tid !== undefined && !this._isIntegrationPoint(tid, current);
198
+ }
199
+ /** @protected */
200
+ _switchToTextParsing(currentToken, nextTokenizerState) {
201
+ this._insertElement(currentToken, NS.HTML);
202
+ this.tokenizer.state = nextTokenizerState;
203
+ this.originalInsertionMode = this.insertionMode;
204
+ this.insertionMode = InsertionMode.TEXT;
205
+ }
206
+ switchToPlaintextParsing() {
207
+ this.insertionMode = InsertionMode.TEXT;
208
+ this.originalInsertionMode = InsertionMode.IN_BODY;
209
+ this.tokenizer.state = TokenizerMode.PLAINTEXT;
210
+ }
211
+ //Fragment parsing
212
+ /** @protected */
213
+ _getAdjustedCurrentElement() {
214
+ return this.openElements.stackTop === 0 && this.fragmentContext
215
+ ? this.fragmentContext
216
+ : this.openElements.current;
217
+ }
218
+ /** @protected */
219
+ _findFormInFragmentContext() {
220
+ let node = this.fragmentContext;
221
+ while (node) {
222
+ if (this.treeAdapter.getTagName(node) === TN.FORM) {
223
+ this.formElement = node;
224
+ break;
225
+ }
226
+ node = this.treeAdapter.getParentNode(node);
227
+ }
228
+ }
229
+ _initTokenizerForFragmentParsing() {
230
+ if (!this.fragmentContext || this.treeAdapter.getNamespaceURI(this.fragmentContext) !== NS.HTML) {
231
+ return;
232
+ }
233
+ switch (this.fragmentContextID) {
234
+ case $.TITLE:
235
+ case $.TEXTAREA: {
236
+ this.tokenizer.state = TokenizerMode.RCDATA;
237
+ break;
238
+ }
239
+ case $.STYLE:
240
+ case $.XMP:
241
+ case $.IFRAME:
242
+ case $.NOEMBED:
243
+ case $.NOFRAMES:
244
+ case $.NOSCRIPT: {
245
+ this.tokenizer.state = TokenizerMode.RAWTEXT;
246
+ break;
247
+ }
248
+ case $.SCRIPT: {
249
+ this.tokenizer.state = TokenizerMode.SCRIPT_DATA;
250
+ break;
251
+ }
252
+ case $.PLAINTEXT: {
253
+ this.tokenizer.state = TokenizerMode.PLAINTEXT;
254
+ break;
255
+ }
256
+ default:
257
+ // Do nothing
258
+ }
259
+ }
260
+ //Tree mutation
261
+ /** @protected */
262
+ _setDocumentType(token) {
263
+ const name = token.name || '';
264
+ const publicId = token.publicId || '';
265
+ const systemId = token.systemId || '';
266
+ this.treeAdapter.setDocumentType(this.document, name, publicId, systemId);
267
+ if (token.location) {
268
+ const documentChildren = this.treeAdapter.getChildNodes(this.document);
269
+ const docTypeNode = documentChildren.find((node) => this.treeAdapter.isDocumentTypeNode(node));
270
+ if (docTypeNode) {
271
+ this.treeAdapter.setNodeSourceCodeLocation(docTypeNode, token.location);
272
+ }
273
+ }
274
+ }
275
+ /** @protected */
276
+ _attachElementToTree(element, location) {
277
+ if (this.options.sourceCodeLocationInfo) {
278
+ const loc = location && {
279
+ ...location,
280
+ startTag: location,
281
+ };
282
+ this.treeAdapter.setNodeSourceCodeLocation(element, loc);
283
+ }
284
+ if (this._shouldFosterParentOnInsertion()) {
285
+ this._fosterParentElement(element);
286
+ }
287
+ else {
288
+ const parent = this.openElements.currentTmplContentOrNode;
289
+ this.treeAdapter.appendChild(parent !== null && parent !== void 0 ? parent : this.document, element);
290
+ }
291
+ }
292
+ /**
293
+ * For self-closing tags. Add an element to the tree, but skip adding it
294
+ * to the stack.
295
+ */
296
+ /** @protected */
297
+ _appendElement(token, namespaceURI) {
298
+ const element = this.treeAdapter.createElement(token.tagName, namespaceURI, token.attrs);
299
+ this._attachElementToTree(element, token.location);
300
+ }
301
+ /** @protected */
302
+ _insertElement(token, namespaceURI) {
303
+ const element = this.treeAdapter.createElement(token.tagName, namespaceURI, token.attrs);
304
+ this._attachElementToTree(element, token.location);
305
+ this.openElements.push(element, token.tagID);
306
+ }
307
+ /** @protected */
308
+ _insertFakeElement(tagName, tagID) {
309
+ const element = this.treeAdapter.createElement(tagName, NS.HTML, []);
310
+ this._attachElementToTree(element, null);
311
+ this.openElements.push(element, tagID);
312
+ }
313
+ /** @protected */
314
+ _insertTemplate(token) {
315
+ const tmpl = this.treeAdapter.createElement(token.tagName, NS.HTML, token.attrs);
316
+ const content = this.treeAdapter.createDocumentFragment();
317
+ this.treeAdapter.setTemplateContent(tmpl, content);
318
+ this._attachElementToTree(tmpl, token.location);
319
+ this.openElements.push(tmpl, token.tagID);
320
+ if (this.options.sourceCodeLocationInfo)
321
+ this.treeAdapter.setNodeSourceCodeLocation(content, null);
322
+ }
323
+ /** @protected */
324
+ _insertFakeRootElement() {
325
+ const element = this.treeAdapter.createElement(TN.HTML, NS.HTML, []);
326
+ if (this.options.sourceCodeLocationInfo)
327
+ this.treeAdapter.setNodeSourceCodeLocation(element, null);
328
+ this.treeAdapter.appendChild(this.openElements.current, element);
329
+ this.openElements.push(element, $.HTML);
330
+ }
331
+ /** @protected */
332
+ _appendCommentNode(token, parent) {
333
+ const commentNode = this.treeAdapter.createCommentNode(token.data);
334
+ this.treeAdapter.appendChild(parent, commentNode);
335
+ if (this.options.sourceCodeLocationInfo) {
336
+ this.treeAdapter.setNodeSourceCodeLocation(commentNode, token.location);
337
+ }
338
+ }
339
+ /** @protected */
340
+ _insertCharacters(token) {
341
+ let parent;
342
+ let beforeElement;
343
+ if (this._shouldFosterParentOnInsertion()) {
344
+ ({ parent, beforeElement } = this._findFosterParentingLocation());
345
+ if (beforeElement) {
346
+ this.treeAdapter.insertTextBefore(parent, token.chars, beforeElement);
347
+ }
348
+ else {
349
+ this.treeAdapter.insertText(parent, token.chars);
350
+ }
351
+ }
352
+ else {
353
+ parent = this.openElements.currentTmplContentOrNode;
354
+ this.treeAdapter.insertText(parent, token.chars);
355
+ }
356
+ if (!token.location)
357
+ return;
358
+ const siblings = this.treeAdapter.getChildNodes(parent);
359
+ const textNodeIdx = beforeElement ? siblings.lastIndexOf(beforeElement) : siblings.length;
360
+ const textNode = siblings[textNodeIdx - 1];
361
+ //NOTE: if we have a location assigned by another token, then just update the end position
362
+ const tnLoc = this.treeAdapter.getNodeSourceCodeLocation(textNode);
363
+ if (tnLoc) {
364
+ const { endLine, endCol, endOffset } = token.location;
365
+ this.treeAdapter.updateNodeSourceCodeLocation(textNode, { endLine, endCol, endOffset });
366
+ }
367
+ else if (this.options.sourceCodeLocationInfo) {
368
+ this.treeAdapter.setNodeSourceCodeLocation(textNode, token.location);
369
+ }
370
+ }
371
+ /** @protected */
372
+ _adoptNodes(donor, recipient) {
373
+ for (let child = this.treeAdapter.getFirstChild(donor); child; child = this.treeAdapter.getFirstChild(donor)) {
374
+ this.treeAdapter.detachNode(child);
375
+ this.treeAdapter.appendChild(recipient, child);
376
+ }
377
+ }
378
+ /** @protected */
379
+ _setEndLocation(element, closingToken) {
380
+ if (this.treeAdapter.getNodeSourceCodeLocation(element) && closingToken.location) {
381
+ const ctLoc = closingToken.location;
382
+ const tn = this.treeAdapter.getTagName(element);
383
+ const endLoc =
384
+ // NOTE: For cases like <p> <p> </p> - First 'p' closes without a closing
385
+ // tag and for cases like <td> <p> </td> - 'p' closes without a closing tag.
386
+ closingToken.type === TokenType.END_TAG && tn === closingToken.tagName
387
+ ? {
388
+ endTag: { ...ctLoc },
389
+ endLine: ctLoc.endLine,
390
+ endCol: ctLoc.endCol,
391
+ endOffset: ctLoc.endOffset,
392
+ }
393
+ : {
394
+ endLine: ctLoc.startLine,
395
+ endCol: ctLoc.startCol,
396
+ endOffset: ctLoc.startOffset,
397
+ };
398
+ this.treeAdapter.updateNodeSourceCodeLocation(element, endLoc);
399
+ }
400
+ }
401
+ //Token processing
402
+ shouldProcessStartTagTokenInForeignContent(token) {
403
+ // Check that neither current === document, or ns === NS.HTML
404
+ if (!this.currentNotInHTML)
405
+ return false;
406
+ let current;
407
+ let currentTagId;
408
+ if (this.openElements.stackTop === 0 && this.fragmentContext) {
409
+ current = this.fragmentContext;
410
+ currentTagId = this.fragmentContextID;
411
+ }
412
+ else {
413
+ ({ current, currentTagId } = this.openElements);
414
+ }
415
+ if (token.tagID === $.SVG &&
416
+ this.treeAdapter.getTagName(current) === TN.ANNOTATION_XML &&
417
+ this.treeAdapter.getNamespaceURI(current) === NS.MATHML) {
418
+ return false;
419
+ }
420
+ return (
421
+ // Check that `current` is not an integration point for HTML or MathML elements.
422
+ this.tokenizer.inForeignNode ||
423
+ // If it _is_ an integration point, then we might have to check that it is not an HTML
424
+ // integration point.
425
+ ((token.tagID === $.MGLYPH || token.tagID === $.MALIGNMARK) &&
426
+ currentTagId !== undefined &&
427
+ !this._isIntegrationPoint(currentTagId, current, NS.HTML)));
428
+ }
429
+ /** @protected */
430
+ _processToken(token) {
431
+ switch (token.type) {
432
+ case TokenType.CHARACTER: {
433
+ this.onCharacter(token);
434
+ break;
435
+ }
436
+ case TokenType.NULL_CHARACTER: {
437
+ this.onNullCharacter(token);
438
+ break;
439
+ }
440
+ case TokenType.COMMENT: {
441
+ this.onComment(token);
442
+ break;
443
+ }
444
+ case TokenType.DOCTYPE: {
445
+ this.onDoctype(token);
446
+ break;
447
+ }
448
+ case TokenType.START_TAG: {
449
+ this._processStartTag(token);
450
+ break;
451
+ }
452
+ case TokenType.END_TAG: {
453
+ this.onEndTag(token);
454
+ break;
455
+ }
456
+ case TokenType.EOF: {
457
+ this.onEof(token);
458
+ break;
459
+ }
460
+ case TokenType.WHITESPACE_CHARACTER: {
461
+ this.onWhitespaceCharacter(token);
462
+ break;
463
+ }
464
+ }
465
+ }
466
+ //Integration points
467
+ /** @protected */
468
+ _isIntegrationPoint(tid, element, foreignNS) {
469
+ const ns = this.treeAdapter.getNamespaceURI(element);
470
+ const attrs = this.treeAdapter.getAttrList(element);
471
+ return foreignContent.isIntegrationPoint(tid, ns, attrs, foreignNS);
472
+ }
473
+ //Active formatting elements reconstruction
474
+ /** @protected */
475
+ _reconstructActiveFormattingElements() {
476
+ const listLength = this.activeFormattingElements.entries.length;
477
+ if (listLength) {
478
+ const endIndex = this.activeFormattingElements.entries.findIndex((entry) => entry.type === EntryType.Marker || this.openElements.contains(entry.element));
479
+ const unopenIdx = endIndex === -1 ? listLength - 1 : endIndex - 1;
480
+ for (let i = unopenIdx; i >= 0; i--) {
481
+ const entry = this.activeFormattingElements.entries[i];
482
+ this._insertElement(entry.token, this.treeAdapter.getNamespaceURI(entry.element));
483
+ entry.element = this.openElements.current;
484
+ }
485
+ }
486
+ }
487
+ //Close elements
488
+ /** @protected */
489
+ _closeTableCell() {
490
+ this.openElements.generateImpliedEndTags();
491
+ this.openElements.popUntilTableCellPopped();
492
+ this.activeFormattingElements.clearToLastMarker();
493
+ this.insertionMode = InsertionMode.IN_ROW;
494
+ }
495
+ /** @protected */
496
+ _closePElement() {
497
+ this.openElements.generateImpliedEndTagsWithExclusion($.P);
498
+ this.openElements.popUntilTagNamePopped($.P);
499
+ }
500
+ //Insertion modes
501
+ /** @protected */
502
+ _resetInsertionMode() {
503
+ for (let i = this.openElements.stackTop; i >= 0; i--) {
504
+ //Insertion mode reset map
505
+ switch (i === 0 && this.fragmentContext ? this.fragmentContextID : this.openElements.tagIDs[i]) {
506
+ case $.TR: {
507
+ this.insertionMode = InsertionMode.IN_ROW;
508
+ return;
509
+ }
510
+ case $.TBODY:
511
+ case $.THEAD:
512
+ case $.TFOOT: {
513
+ this.insertionMode = InsertionMode.IN_TABLE_BODY;
514
+ return;
515
+ }
516
+ case $.CAPTION: {
517
+ this.insertionMode = InsertionMode.IN_CAPTION;
518
+ return;
519
+ }
520
+ case $.COLGROUP: {
521
+ this.insertionMode = InsertionMode.IN_COLUMN_GROUP;
522
+ return;
523
+ }
524
+ case $.TABLE: {
525
+ this.insertionMode = InsertionMode.IN_TABLE;
526
+ return;
527
+ }
528
+ case $.BODY: {
529
+ this.insertionMode = InsertionMode.IN_BODY;
530
+ return;
531
+ }
532
+ case $.FRAMESET: {
533
+ this.insertionMode = InsertionMode.IN_FRAMESET;
534
+ return;
535
+ }
536
+ case $.SELECT: {
537
+ this._resetInsertionModeForSelect(i);
538
+ return;
539
+ }
540
+ case $.TEMPLATE: {
541
+ this.insertionMode = this.tmplInsertionModeStack[0];
542
+ return;
543
+ }
544
+ case $.HTML: {
545
+ this.insertionMode = this.headElement ? InsertionMode.AFTER_HEAD : InsertionMode.BEFORE_HEAD;
546
+ return;
547
+ }
548
+ case $.TD:
549
+ case $.TH: {
550
+ if (i > 0) {
551
+ this.insertionMode = InsertionMode.IN_CELL;
552
+ return;
553
+ }
554
+ break;
555
+ }
556
+ case $.HEAD: {
557
+ if (i > 0) {
558
+ this.insertionMode = InsertionMode.IN_HEAD;
559
+ return;
560
+ }
561
+ break;
562
+ }
563
+ }
564
+ }
565
+ this.insertionMode = InsertionMode.IN_BODY;
566
+ }
567
+ /** @protected */
568
+ _resetInsertionModeForSelect(selectIdx) {
569
+ if (selectIdx > 0) {
570
+ for (let i = selectIdx - 1; i > 0; i--) {
571
+ const tn = this.openElements.tagIDs[i];
572
+ if (tn === $.TEMPLATE) {
573
+ break;
574
+ }
575
+ else if (tn === $.TABLE) {
576
+ this.insertionMode = InsertionMode.IN_SELECT_IN_TABLE;
577
+ return;
578
+ }
579
+ }
580
+ }
581
+ this.insertionMode = InsertionMode.IN_SELECT;
582
+ }
583
+ //Foster parenting
584
+ /** @protected */
585
+ _isElementCausesFosterParenting(tn) {
586
+ return TABLE_STRUCTURE_TAGS.has(tn);
587
+ }
588
+ /** @protected */
589
+ _shouldFosterParentOnInsertion() {
590
+ return (this.fosterParentingEnabled &&
591
+ this.openElements.currentTagId !== undefined &&
592
+ this._isElementCausesFosterParenting(this.openElements.currentTagId));
593
+ }
594
+ /** @protected */
595
+ _findFosterParentingLocation() {
596
+ for (let i = this.openElements.stackTop; i >= 0; i--) {
597
+ const openElement = this.openElements.items[i];
598
+ switch (this.openElements.tagIDs[i]) {
599
+ case $.TEMPLATE: {
600
+ if (this.treeAdapter.getNamespaceURI(openElement) === NS.HTML) {
601
+ return { parent: this.treeAdapter.getTemplateContent(openElement), beforeElement: null };
602
+ }
603
+ break;
604
+ }
605
+ case $.TABLE: {
606
+ const parent = this.treeAdapter.getParentNode(openElement);
607
+ if (parent) {
608
+ return { parent, beforeElement: openElement };
609
+ }
610
+ return { parent: this.openElements.items[i - 1], beforeElement: null };
611
+ }
612
+ default:
613
+ // Do nothing
614
+ }
615
+ }
616
+ return { parent: this.openElements.items[0], beforeElement: null };
617
+ }
618
+ /** @protected */
619
+ _fosterParentElement(element) {
620
+ const location = this._findFosterParentingLocation();
621
+ if (location.beforeElement) {
622
+ this.treeAdapter.insertBefore(location.parent, element, location.beforeElement);
623
+ }
624
+ else {
625
+ this.treeAdapter.appendChild(location.parent, element);
626
+ }
627
+ }
628
+ //Special elements
629
+ /** @protected */
630
+ _isSpecialElement(element, id) {
631
+ const ns = this.treeAdapter.getNamespaceURI(element);
632
+ return SPECIAL_ELEMENTS[ns].has(id);
633
+ }
634
+ /** @internal */
635
+ onCharacter(token) {
636
+ this.skipNextNewLine = false;
637
+ if (this.tokenizer.inForeignNode) {
638
+ characterInForeignContent(this, token);
639
+ return;
640
+ }
641
+ switch (this.insertionMode) {
642
+ case InsertionMode.INITIAL: {
643
+ tokenInInitialMode(this, token);
644
+ break;
645
+ }
646
+ case InsertionMode.BEFORE_HTML: {
647
+ tokenBeforeHtml(this, token);
648
+ break;
649
+ }
650
+ case InsertionMode.BEFORE_HEAD: {
651
+ tokenBeforeHead(this, token);
652
+ break;
653
+ }
654
+ case InsertionMode.IN_HEAD: {
655
+ tokenInHead(this, token);
656
+ break;
657
+ }
658
+ case InsertionMode.IN_HEAD_NO_SCRIPT: {
659
+ tokenInHeadNoScript(this, token);
660
+ break;
661
+ }
662
+ case InsertionMode.AFTER_HEAD: {
663
+ tokenAfterHead(this, token);
664
+ break;
665
+ }
666
+ case InsertionMode.IN_BODY:
667
+ case InsertionMode.IN_CAPTION:
668
+ case InsertionMode.IN_CELL:
669
+ case InsertionMode.IN_TEMPLATE: {
670
+ characterInBody(this, token);
671
+ break;
672
+ }
673
+ case InsertionMode.TEXT:
674
+ case InsertionMode.IN_SELECT:
675
+ case InsertionMode.IN_SELECT_IN_TABLE: {
676
+ this._insertCharacters(token);
677
+ break;
678
+ }
679
+ case InsertionMode.IN_TABLE:
680
+ case InsertionMode.IN_TABLE_BODY:
681
+ case InsertionMode.IN_ROW: {
682
+ characterInTable(this, token);
683
+ break;
684
+ }
685
+ case InsertionMode.IN_TABLE_TEXT: {
686
+ characterInTableText(this, token);
687
+ break;
688
+ }
689
+ case InsertionMode.IN_COLUMN_GROUP: {
690
+ tokenInColumnGroup(this, token);
691
+ break;
692
+ }
693
+ case InsertionMode.AFTER_BODY: {
694
+ tokenAfterBody(this, token);
695
+ break;
696
+ }
697
+ case InsertionMode.AFTER_AFTER_BODY: {
698
+ tokenAfterAfterBody(this, token);
699
+ break;
700
+ }
701
+ default:
702
+ // Do nothing
703
+ }
704
+ }
705
+ /** @internal */
706
+ onNullCharacter(token) {
707
+ this.skipNextNewLine = false;
708
+ if (this.tokenizer.inForeignNode) {
709
+ nullCharacterInForeignContent(this, token);
710
+ return;
711
+ }
712
+ switch (this.insertionMode) {
713
+ case InsertionMode.INITIAL: {
714
+ tokenInInitialMode(this, token);
715
+ break;
716
+ }
717
+ case InsertionMode.BEFORE_HTML: {
718
+ tokenBeforeHtml(this, token);
719
+ break;
720
+ }
721
+ case InsertionMode.BEFORE_HEAD: {
722
+ tokenBeforeHead(this, token);
723
+ break;
724
+ }
725
+ case InsertionMode.IN_HEAD: {
726
+ tokenInHead(this, token);
727
+ break;
728
+ }
729
+ case InsertionMode.IN_HEAD_NO_SCRIPT: {
730
+ tokenInHeadNoScript(this, token);
731
+ break;
732
+ }
733
+ case InsertionMode.AFTER_HEAD: {
734
+ tokenAfterHead(this, token);
735
+ break;
736
+ }
737
+ case InsertionMode.TEXT: {
738
+ this._insertCharacters(token);
739
+ break;
740
+ }
741
+ case InsertionMode.IN_TABLE:
742
+ case InsertionMode.IN_TABLE_BODY:
743
+ case InsertionMode.IN_ROW: {
744
+ characterInTable(this, token);
745
+ break;
746
+ }
747
+ case InsertionMode.IN_COLUMN_GROUP: {
748
+ tokenInColumnGroup(this, token);
749
+ break;
750
+ }
751
+ case InsertionMode.AFTER_BODY: {
752
+ tokenAfterBody(this, token);
753
+ break;
754
+ }
755
+ case InsertionMode.AFTER_AFTER_BODY: {
756
+ tokenAfterAfterBody(this, token);
757
+ break;
758
+ }
759
+ default:
760
+ // Do nothing
761
+ }
762
+ }
763
+ /** @internal */
764
+ onComment(token) {
765
+ this.skipNextNewLine = false;
766
+ if (this.currentNotInHTML) {
767
+ appendComment(this, token);
768
+ return;
769
+ }
770
+ switch (this.insertionMode) {
771
+ case InsertionMode.INITIAL:
772
+ case InsertionMode.BEFORE_HTML:
773
+ case InsertionMode.BEFORE_HEAD:
774
+ case InsertionMode.IN_HEAD:
775
+ case InsertionMode.IN_HEAD_NO_SCRIPT:
776
+ case InsertionMode.AFTER_HEAD:
777
+ case InsertionMode.IN_BODY:
778
+ case InsertionMode.IN_TABLE:
779
+ case InsertionMode.IN_CAPTION:
780
+ case InsertionMode.IN_COLUMN_GROUP:
781
+ case InsertionMode.IN_TABLE_BODY:
782
+ case InsertionMode.IN_ROW:
783
+ case InsertionMode.IN_CELL:
784
+ case InsertionMode.IN_SELECT:
785
+ case InsertionMode.IN_SELECT_IN_TABLE:
786
+ case InsertionMode.IN_TEMPLATE:
787
+ case InsertionMode.IN_FRAMESET:
788
+ case InsertionMode.AFTER_FRAMESET: {
789
+ appendComment(this, token);
790
+ break;
791
+ }
792
+ case InsertionMode.IN_TABLE_TEXT: {
793
+ tokenInTableText(this, token);
794
+ break;
795
+ }
796
+ case InsertionMode.AFTER_BODY: {
797
+ appendCommentToRootHtmlElement(this, token);
798
+ break;
799
+ }
800
+ case InsertionMode.AFTER_AFTER_BODY:
801
+ case InsertionMode.AFTER_AFTER_FRAMESET: {
802
+ appendCommentToDocument(this, token);
803
+ break;
804
+ }
805
+ default:
806
+ // Do nothing
807
+ }
808
+ }
809
+ /** @internal */
810
+ onDoctype(token) {
811
+ this.skipNextNewLine = false;
812
+ switch (this.insertionMode) {
813
+ case InsertionMode.INITIAL: {
814
+ doctypeInInitialMode(this, token);
815
+ break;
816
+ }
817
+ case InsertionMode.BEFORE_HEAD:
818
+ case InsertionMode.IN_HEAD:
819
+ case InsertionMode.IN_HEAD_NO_SCRIPT:
820
+ case InsertionMode.AFTER_HEAD: {
821
+ this._err(token, ERR.misplacedDoctype);
822
+ break;
823
+ }
824
+ case InsertionMode.IN_TABLE_TEXT: {
825
+ tokenInTableText(this, token);
826
+ break;
827
+ }
828
+ default:
829
+ // Do nothing
830
+ }
831
+ }
832
+ /** @internal */
833
+ onStartTag(token) {
834
+ this.skipNextNewLine = false;
835
+ this.currentToken = token;
836
+ this._processStartTag(token);
837
+ if (token.selfClosing && !token.ackSelfClosing) {
838
+ this._err(token, ERR.nonVoidHtmlElementStartTagWithTrailingSolidus);
839
+ }
840
+ }
841
+ /**
842
+ * Processes a given start tag.
843
+ *
844
+ * `onStartTag` checks if a self-closing tag was recognized. When a token
845
+ * is moved inbetween multiple insertion modes, this check for self-closing
846
+ * could lead to false positives. To avoid this, `_processStartTag` is used
847
+ * for nested calls.
848
+ *
849
+ * @param token The token to process.
850
+ * @protected
851
+ */
852
+ _processStartTag(token) {
853
+ if (this.shouldProcessStartTagTokenInForeignContent(token)) {
854
+ startTagInForeignContent(this, token);
855
+ }
856
+ else {
857
+ this._startTagOutsideForeignContent(token);
858
+ }
859
+ }
860
+ /** @protected */
861
+ _startTagOutsideForeignContent(token) {
862
+ switch (this.insertionMode) {
863
+ case InsertionMode.INITIAL: {
864
+ tokenInInitialMode(this, token);
865
+ break;
866
+ }
867
+ case InsertionMode.BEFORE_HTML: {
868
+ startTagBeforeHtml(this, token);
869
+ break;
870
+ }
871
+ case InsertionMode.BEFORE_HEAD: {
872
+ startTagBeforeHead(this, token);
873
+ break;
874
+ }
875
+ case InsertionMode.IN_HEAD: {
876
+ startTagInHead(this, token);
877
+ break;
878
+ }
879
+ case InsertionMode.IN_HEAD_NO_SCRIPT: {
880
+ startTagInHeadNoScript(this, token);
881
+ break;
882
+ }
883
+ case InsertionMode.AFTER_HEAD: {
884
+ startTagAfterHead(this, token);
885
+ break;
886
+ }
887
+ case InsertionMode.IN_BODY: {
888
+ startTagInBody(this, token);
889
+ break;
890
+ }
891
+ case InsertionMode.IN_TABLE: {
892
+ startTagInTable(this, token);
893
+ break;
894
+ }
895
+ case InsertionMode.IN_TABLE_TEXT: {
896
+ tokenInTableText(this, token);
897
+ break;
898
+ }
899
+ case InsertionMode.IN_CAPTION: {
900
+ startTagInCaption(this, token);
901
+ break;
902
+ }
903
+ case InsertionMode.IN_COLUMN_GROUP: {
904
+ startTagInColumnGroup(this, token);
905
+ break;
906
+ }
907
+ case InsertionMode.IN_TABLE_BODY: {
908
+ startTagInTableBody(this, token);
909
+ break;
910
+ }
911
+ case InsertionMode.IN_ROW: {
912
+ startTagInRow(this, token);
913
+ break;
914
+ }
915
+ case InsertionMode.IN_CELL: {
916
+ startTagInCell(this, token);
917
+ break;
918
+ }
919
+ case InsertionMode.IN_SELECT: {
920
+ startTagInSelect(this, token);
921
+ break;
922
+ }
923
+ case InsertionMode.IN_SELECT_IN_TABLE: {
924
+ startTagInSelectInTable(this, token);
925
+ break;
926
+ }
927
+ case InsertionMode.IN_TEMPLATE: {
928
+ startTagInTemplate(this, token);
929
+ break;
930
+ }
931
+ case InsertionMode.AFTER_BODY: {
932
+ startTagAfterBody(this, token);
933
+ break;
934
+ }
935
+ case InsertionMode.IN_FRAMESET: {
936
+ startTagInFrameset(this, token);
937
+ break;
938
+ }
939
+ case InsertionMode.AFTER_FRAMESET: {
940
+ startTagAfterFrameset(this, token);
941
+ break;
942
+ }
943
+ case InsertionMode.AFTER_AFTER_BODY: {
944
+ startTagAfterAfterBody(this, token);
945
+ break;
946
+ }
947
+ case InsertionMode.AFTER_AFTER_FRAMESET: {
948
+ startTagAfterAfterFrameset(this, token);
949
+ break;
950
+ }
951
+ default:
952
+ // Do nothing
953
+ }
954
+ }
955
+ /** @internal */
956
+ onEndTag(token) {
957
+ this.skipNextNewLine = false;
958
+ this.currentToken = token;
959
+ if (this.currentNotInHTML) {
960
+ endTagInForeignContent(this, token);
961
+ }
962
+ else {
963
+ this._endTagOutsideForeignContent(token);
964
+ }
965
+ }
966
+ /** @protected */
967
+ _endTagOutsideForeignContent(token) {
968
+ switch (this.insertionMode) {
969
+ case InsertionMode.INITIAL: {
970
+ tokenInInitialMode(this, token);
971
+ break;
972
+ }
973
+ case InsertionMode.BEFORE_HTML: {
974
+ endTagBeforeHtml(this, token);
975
+ break;
976
+ }
977
+ case InsertionMode.BEFORE_HEAD: {
978
+ endTagBeforeHead(this, token);
979
+ break;
980
+ }
981
+ case InsertionMode.IN_HEAD: {
982
+ endTagInHead(this, token);
983
+ break;
984
+ }
985
+ case InsertionMode.IN_HEAD_NO_SCRIPT: {
986
+ endTagInHeadNoScript(this, token);
987
+ break;
988
+ }
989
+ case InsertionMode.AFTER_HEAD: {
990
+ endTagAfterHead(this, token);
991
+ break;
992
+ }
993
+ case InsertionMode.IN_BODY: {
994
+ endTagInBody(this, token);
995
+ break;
996
+ }
997
+ case InsertionMode.TEXT: {
998
+ endTagInText(this, token);
999
+ break;
1000
+ }
1001
+ case InsertionMode.IN_TABLE: {
1002
+ endTagInTable(this, token);
1003
+ break;
1004
+ }
1005
+ case InsertionMode.IN_TABLE_TEXT: {
1006
+ tokenInTableText(this, token);
1007
+ break;
1008
+ }
1009
+ case InsertionMode.IN_CAPTION: {
1010
+ endTagInCaption(this, token);
1011
+ break;
1012
+ }
1013
+ case InsertionMode.IN_COLUMN_GROUP: {
1014
+ endTagInColumnGroup(this, token);
1015
+ break;
1016
+ }
1017
+ case InsertionMode.IN_TABLE_BODY: {
1018
+ endTagInTableBody(this, token);
1019
+ break;
1020
+ }
1021
+ case InsertionMode.IN_ROW: {
1022
+ endTagInRow(this, token);
1023
+ break;
1024
+ }
1025
+ case InsertionMode.IN_CELL: {
1026
+ endTagInCell(this, token);
1027
+ break;
1028
+ }
1029
+ case InsertionMode.IN_SELECT: {
1030
+ endTagInSelect(this, token);
1031
+ break;
1032
+ }
1033
+ case InsertionMode.IN_SELECT_IN_TABLE: {
1034
+ endTagInSelectInTable(this, token);
1035
+ break;
1036
+ }
1037
+ case InsertionMode.IN_TEMPLATE: {
1038
+ endTagInTemplate(this, token);
1039
+ break;
1040
+ }
1041
+ case InsertionMode.AFTER_BODY: {
1042
+ endTagAfterBody(this, token);
1043
+ break;
1044
+ }
1045
+ case InsertionMode.IN_FRAMESET: {
1046
+ endTagInFrameset(this, token);
1047
+ break;
1048
+ }
1049
+ case InsertionMode.AFTER_FRAMESET: {
1050
+ endTagAfterFrameset(this, token);
1051
+ break;
1052
+ }
1053
+ case InsertionMode.AFTER_AFTER_BODY: {
1054
+ tokenAfterAfterBody(this, token);
1055
+ break;
1056
+ }
1057
+ default:
1058
+ // Do nothing
1059
+ }
1060
+ }
1061
+ /** @internal */
1062
+ onEof(token) {
1063
+ switch (this.insertionMode) {
1064
+ case InsertionMode.INITIAL: {
1065
+ tokenInInitialMode(this, token);
1066
+ break;
1067
+ }
1068
+ case InsertionMode.BEFORE_HTML: {
1069
+ tokenBeforeHtml(this, token);
1070
+ break;
1071
+ }
1072
+ case InsertionMode.BEFORE_HEAD: {
1073
+ tokenBeforeHead(this, token);
1074
+ break;
1075
+ }
1076
+ case InsertionMode.IN_HEAD: {
1077
+ tokenInHead(this, token);
1078
+ break;
1079
+ }
1080
+ case InsertionMode.IN_HEAD_NO_SCRIPT: {
1081
+ tokenInHeadNoScript(this, token);
1082
+ break;
1083
+ }
1084
+ case InsertionMode.AFTER_HEAD: {
1085
+ tokenAfterHead(this, token);
1086
+ break;
1087
+ }
1088
+ case InsertionMode.IN_BODY:
1089
+ case InsertionMode.IN_TABLE:
1090
+ case InsertionMode.IN_CAPTION:
1091
+ case InsertionMode.IN_COLUMN_GROUP:
1092
+ case InsertionMode.IN_TABLE_BODY:
1093
+ case InsertionMode.IN_ROW:
1094
+ case InsertionMode.IN_CELL:
1095
+ case InsertionMode.IN_SELECT:
1096
+ case InsertionMode.IN_SELECT_IN_TABLE: {
1097
+ eofInBody(this, token);
1098
+ break;
1099
+ }
1100
+ case InsertionMode.TEXT: {
1101
+ eofInText(this, token);
1102
+ break;
1103
+ }
1104
+ case InsertionMode.IN_TABLE_TEXT: {
1105
+ tokenInTableText(this, token);
1106
+ break;
1107
+ }
1108
+ case InsertionMode.IN_TEMPLATE: {
1109
+ eofInTemplate(this, token);
1110
+ break;
1111
+ }
1112
+ case InsertionMode.AFTER_BODY:
1113
+ case InsertionMode.IN_FRAMESET:
1114
+ case InsertionMode.AFTER_FRAMESET:
1115
+ case InsertionMode.AFTER_AFTER_BODY:
1116
+ case InsertionMode.AFTER_AFTER_FRAMESET: {
1117
+ stopParsing(this, token);
1118
+ break;
1119
+ }
1120
+ default:
1121
+ // Do nothing
1122
+ }
1123
+ }
1124
+ /** @internal */
1125
+ onWhitespaceCharacter(token) {
1126
+ if (this.skipNextNewLine) {
1127
+ this.skipNextNewLine = false;
1128
+ if (token.chars.charCodeAt(0) === unicode.CODE_POINTS.LINE_FEED) {
1129
+ if (token.chars.length === 1) {
1130
+ return;
1131
+ }
1132
+ token.chars = token.chars.substr(1);
1133
+ }
1134
+ }
1135
+ if (this.tokenizer.inForeignNode) {
1136
+ this._insertCharacters(token);
1137
+ return;
1138
+ }
1139
+ switch (this.insertionMode) {
1140
+ case InsertionMode.IN_HEAD:
1141
+ case InsertionMode.IN_HEAD_NO_SCRIPT:
1142
+ case InsertionMode.AFTER_HEAD:
1143
+ case InsertionMode.TEXT:
1144
+ case InsertionMode.IN_COLUMN_GROUP:
1145
+ case InsertionMode.IN_SELECT:
1146
+ case InsertionMode.IN_SELECT_IN_TABLE:
1147
+ case InsertionMode.IN_FRAMESET:
1148
+ case InsertionMode.AFTER_FRAMESET: {
1149
+ this._insertCharacters(token);
1150
+ break;
1151
+ }
1152
+ case InsertionMode.IN_BODY:
1153
+ case InsertionMode.IN_CAPTION:
1154
+ case InsertionMode.IN_CELL:
1155
+ case InsertionMode.IN_TEMPLATE:
1156
+ case InsertionMode.AFTER_BODY:
1157
+ case InsertionMode.AFTER_AFTER_BODY:
1158
+ case InsertionMode.AFTER_AFTER_FRAMESET: {
1159
+ whitespaceCharacterInBody(this, token);
1160
+ break;
1161
+ }
1162
+ case InsertionMode.IN_TABLE:
1163
+ case InsertionMode.IN_TABLE_BODY:
1164
+ case InsertionMode.IN_ROW: {
1165
+ characterInTable(this, token);
1166
+ break;
1167
+ }
1168
+ case InsertionMode.IN_TABLE_TEXT: {
1169
+ whitespaceCharacterInTableText(this, token);
1170
+ break;
1171
+ }
1172
+ default:
1173
+ // Do nothing
1174
+ }
1175
+ }
1176
+ }
1177
+ //Adoption agency algorithm
1178
+ //(see: http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#adoptionAgency)
1179
+ //------------------------------------------------------------------
1180
+ //Steps 5-8 of the algorithm
1181
+ function aaObtainFormattingElementEntry(p, token) {
1182
+ let formattingElementEntry = p.activeFormattingElements.getElementEntryInScopeWithTagName(token.tagName);
1183
+ if (formattingElementEntry) {
1184
+ if (!p.openElements.contains(formattingElementEntry.element)) {
1185
+ p.activeFormattingElements.removeEntry(formattingElementEntry);
1186
+ formattingElementEntry = null;
1187
+ }
1188
+ else if (!p.openElements.hasInScope(token.tagID)) {
1189
+ formattingElementEntry = null;
1190
+ }
1191
+ }
1192
+ else {
1193
+ genericEndTagInBody(p, token);
1194
+ }
1195
+ return formattingElementEntry;
1196
+ }
1197
+ //Steps 9 and 10 of the algorithm
1198
+ function aaObtainFurthestBlock(p, formattingElementEntry) {
1199
+ let furthestBlock = null;
1200
+ let idx = p.openElements.stackTop;
1201
+ for (; idx >= 0; idx--) {
1202
+ const element = p.openElements.items[idx];
1203
+ if (element === formattingElementEntry.element) {
1204
+ break;
1205
+ }
1206
+ if (p._isSpecialElement(element, p.openElements.tagIDs[idx])) {
1207
+ furthestBlock = element;
1208
+ }
1209
+ }
1210
+ if (!furthestBlock) {
1211
+ p.openElements.shortenToLength(Math.max(idx, 0));
1212
+ p.activeFormattingElements.removeEntry(formattingElementEntry);
1213
+ }
1214
+ return furthestBlock;
1215
+ }
1216
+ //Step 13 of the algorithm
1217
+ function aaInnerLoop(p, furthestBlock, formattingElement) {
1218
+ let lastElement = furthestBlock;
1219
+ let nextElement = p.openElements.getCommonAncestor(furthestBlock);
1220
+ for (let i = 0, element = nextElement; element !== formattingElement; i++, element = nextElement) {
1221
+ //NOTE: store the next element for the next loop iteration (it may be deleted from the stack by step 9.5)
1222
+ nextElement = p.openElements.getCommonAncestor(element);
1223
+ const elementEntry = p.activeFormattingElements.getElementEntry(element);
1224
+ const counterOverflow = elementEntry && i >= AA_INNER_LOOP_ITER;
1225
+ const shouldRemoveFromOpenElements = !elementEntry || counterOverflow;
1226
+ if (shouldRemoveFromOpenElements) {
1227
+ if (counterOverflow) {
1228
+ p.activeFormattingElements.removeEntry(elementEntry);
1229
+ }
1230
+ p.openElements.remove(element);
1231
+ }
1232
+ else {
1233
+ element = aaRecreateElementFromEntry(p, elementEntry);
1234
+ if (lastElement === furthestBlock) {
1235
+ p.activeFormattingElements.bookmark = elementEntry;
1236
+ }
1237
+ p.treeAdapter.detachNode(lastElement);
1238
+ p.treeAdapter.appendChild(element, lastElement);
1239
+ lastElement = element;
1240
+ }
1241
+ }
1242
+ return lastElement;
1243
+ }
1244
+ //Step 13.7 of the algorithm
1245
+ function aaRecreateElementFromEntry(p, elementEntry) {
1246
+ const ns = p.treeAdapter.getNamespaceURI(elementEntry.element);
1247
+ const newElement = p.treeAdapter.createElement(elementEntry.token.tagName, ns, elementEntry.token.attrs);
1248
+ p.openElements.replace(elementEntry.element, newElement);
1249
+ elementEntry.element = newElement;
1250
+ return newElement;
1251
+ }
1252
+ //Step 14 of the algorithm
1253
+ function aaInsertLastNodeInCommonAncestor(p, commonAncestor, lastElement) {
1254
+ const tn = p.treeAdapter.getTagName(commonAncestor);
1255
+ const tid = getTagID(tn);
1256
+ if (p._isElementCausesFosterParenting(tid)) {
1257
+ p._fosterParentElement(lastElement);
1258
+ }
1259
+ else {
1260
+ const ns = p.treeAdapter.getNamespaceURI(commonAncestor);
1261
+ if (tid === $.TEMPLATE && ns === NS.HTML) {
1262
+ commonAncestor = p.treeAdapter.getTemplateContent(commonAncestor);
1263
+ }
1264
+ p.treeAdapter.appendChild(commonAncestor, lastElement);
1265
+ }
1266
+ }
1267
+ //Steps 15-19 of the algorithm
1268
+ function aaReplaceFormattingElement(p, furthestBlock, formattingElementEntry) {
1269
+ const ns = p.treeAdapter.getNamespaceURI(formattingElementEntry.element);
1270
+ const { token } = formattingElementEntry;
1271
+ const newElement = p.treeAdapter.createElement(token.tagName, ns, token.attrs);
1272
+ p._adoptNodes(furthestBlock, newElement);
1273
+ p.treeAdapter.appendChild(furthestBlock, newElement);
1274
+ p.activeFormattingElements.insertElementAfterBookmark(newElement, token);
1275
+ p.activeFormattingElements.removeEntry(formattingElementEntry);
1276
+ p.openElements.remove(formattingElementEntry.element);
1277
+ p.openElements.insertAfter(furthestBlock, newElement, token.tagID);
1278
+ }
1279
+ //Algorithm entry point
1280
+ function callAdoptionAgency(p, token) {
1281
+ for (let i = 0; i < AA_OUTER_LOOP_ITER; i++) {
1282
+ const formattingElementEntry = aaObtainFormattingElementEntry(p, token);
1283
+ if (!formattingElementEntry) {
1284
+ break;
1285
+ }
1286
+ const furthestBlock = aaObtainFurthestBlock(p, formattingElementEntry);
1287
+ if (!furthestBlock) {
1288
+ break;
1289
+ }
1290
+ p.activeFormattingElements.bookmark = formattingElementEntry;
1291
+ const lastElement = aaInnerLoop(p, furthestBlock, formattingElementEntry.element);
1292
+ const commonAncestor = p.openElements.getCommonAncestor(formattingElementEntry.element);
1293
+ p.treeAdapter.detachNode(lastElement);
1294
+ if (commonAncestor)
1295
+ aaInsertLastNodeInCommonAncestor(p, commonAncestor, lastElement);
1296
+ aaReplaceFormattingElement(p, furthestBlock, formattingElementEntry);
1297
+ }
1298
+ }
1299
+ //Generic token handlers
1300
+ //------------------------------------------------------------------
1301
+ function appendComment(p, token) {
1302
+ p._appendCommentNode(token, p.openElements.currentTmplContentOrNode);
1303
+ }
1304
+ function appendCommentToRootHtmlElement(p, token) {
1305
+ p._appendCommentNode(token, p.openElements.items[0]);
1306
+ }
1307
+ function appendCommentToDocument(p, token) {
1308
+ p._appendCommentNode(token, p.document);
1309
+ }
1310
+ function stopParsing(p, token) {
1311
+ p.stopped = true;
1312
+ // NOTE: Set end locations for elements that remain on the open element stack.
1313
+ if (token.location) {
1314
+ // NOTE: If we are not in a fragment, `html` and `body` will stay on the stack.
1315
+ // This is a problem, as we might overwrite their end position here.
1316
+ const target = p.fragmentContext ? 0 : 2;
1317
+ for (let i = p.openElements.stackTop; i >= target; i--) {
1318
+ p._setEndLocation(p.openElements.items[i], token);
1319
+ }
1320
+ // Handle `html` and `body`
1321
+ if (!p.fragmentContext && p.openElements.stackTop >= 0) {
1322
+ const htmlElement = p.openElements.items[0];
1323
+ const htmlLocation = p.treeAdapter.getNodeSourceCodeLocation(htmlElement);
1324
+ if (htmlLocation && !htmlLocation.endTag) {
1325
+ p._setEndLocation(htmlElement, token);
1326
+ if (p.openElements.stackTop >= 1) {
1327
+ const bodyElement = p.openElements.items[1];
1328
+ const bodyLocation = p.treeAdapter.getNodeSourceCodeLocation(bodyElement);
1329
+ if (bodyLocation && !bodyLocation.endTag) {
1330
+ p._setEndLocation(bodyElement, token);
1331
+ }
1332
+ }
1333
+ }
1334
+ }
1335
+ }
1336
+ }
1337
+ // The "initial" insertion mode
1338
+ //------------------------------------------------------------------
1339
+ function doctypeInInitialMode(p, token) {
1340
+ p._setDocumentType(token);
1341
+ const mode = token.forceQuirks ? DOCUMENT_MODE.QUIRKS : doctype.getDocumentMode(token);
1342
+ if (!doctype.isConforming(token)) {
1343
+ p._err(token, ERR.nonConformingDoctype);
1344
+ }
1345
+ p.treeAdapter.setDocumentMode(p.document, mode);
1346
+ p.insertionMode = InsertionMode.BEFORE_HTML;
1347
+ }
1348
+ function tokenInInitialMode(p, token) {
1349
+ p._err(token, ERR.missingDoctype, true);
1350
+ p.treeAdapter.setDocumentMode(p.document, DOCUMENT_MODE.QUIRKS);
1351
+ p.insertionMode = InsertionMode.BEFORE_HTML;
1352
+ p._processToken(token);
1353
+ }
1354
+ // The "before html" insertion mode
1355
+ //------------------------------------------------------------------
1356
+ function startTagBeforeHtml(p, token) {
1357
+ if (token.tagID === $.HTML) {
1358
+ p._insertElement(token, NS.HTML);
1359
+ p.insertionMode = InsertionMode.BEFORE_HEAD;
1360
+ }
1361
+ else {
1362
+ tokenBeforeHtml(p, token);
1363
+ }
1364
+ }
1365
+ function endTagBeforeHtml(p, token) {
1366
+ const tn = token.tagID;
1367
+ if (tn === $.HTML || tn === $.HEAD || tn === $.BODY || tn === $.BR) {
1368
+ tokenBeforeHtml(p, token);
1369
+ }
1370
+ }
1371
+ function tokenBeforeHtml(p, token) {
1372
+ p._insertFakeRootElement();
1373
+ p.insertionMode = InsertionMode.BEFORE_HEAD;
1374
+ p._processToken(token);
1375
+ }
1376
+ // The "before head" insertion mode
1377
+ //------------------------------------------------------------------
1378
+ function startTagBeforeHead(p, token) {
1379
+ switch (token.tagID) {
1380
+ case $.HTML: {
1381
+ startTagInBody(p, token);
1382
+ break;
1383
+ }
1384
+ case $.HEAD: {
1385
+ p._insertElement(token, NS.HTML);
1386
+ p.headElement = p.openElements.current;
1387
+ p.insertionMode = InsertionMode.IN_HEAD;
1388
+ break;
1389
+ }
1390
+ default: {
1391
+ tokenBeforeHead(p, token);
1392
+ }
1393
+ }
1394
+ }
1395
+ function endTagBeforeHead(p, token) {
1396
+ const tn = token.tagID;
1397
+ if (tn === $.HEAD || tn === $.BODY || tn === $.HTML || tn === $.BR) {
1398
+ tokenBeforeHead(p, token);
1399
+ }
1400
+ else {
1401
+ p._err(token, ERR.endTagWithoutMatchingOpenElement);
1402
+ }
1403
+ }
1404
+ function tokenBeforeHead(p, token) {
1405
+ p._insertFakeElement(TN.HEAD, $.HEAD);
1406
+ p.headElement = p.openElements.current;
1407
+ p.insertionMode = InsertionMode.IN_HEAD;
1408
+ p._processToken(token);
1409
+ }
1410
+ // The "in head" insertion mode
1411
+ //------------------------------------------------------------------
1412
+ function startTagInHead(p, token) {
1413
+ switch (token.tagID) {
1414
+ case $.HTML: {
1415
+ startTagInBody(p, token);
1416
+ break;
1417
+ }
1418
+ case $.BASE:
1419
+ case $.BASEFONT:
1420
+ case $.BGSOUND:
1421
+ case $.LINK:
1422
+ case $.META: {
1423
+ p._appendElement(token, NS.HTML);
1424
+ token.ackSelfClosing = true;
1425
+ break;
1426
+ }
1427
+ case $.TITLE: {
1428
+ p._switchToTextParsing(token, TokenizerMode.RCDATA);
1429
+ break;
1430
+ }
1431
+ case $.NOSCRIPT: {
1432
+ if (p.options.scriptingEnabled) {
1433
+ p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
1434
+ }
1435
+ else {
1436
+ p._insertElement(token, NS.HTML);
1437
+ p.insertionMode = InsertionMode.IN_HEAD_NO_SCRIPT;
1438
+ }
1439
+ break;
1440
+ }
1441
+ case $.NOFRAMES:
1442
+ case $.STYLE: {
1443
+ p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
1444
+ break;
1445
+ }
1446
+ case $.SCRIPT: {
1447
+ p._switchToTextParsing(token, TokenizerMode.SCRIPT_DATA);
1448
+ break;
1449
+ }
1450
+ case $.TEMPLATE: {
1451
+ p._insertTemplate(token);
1452
+ p.activeFormattingElements.insertMarker();
1453
+ p.framesetOk = false;
1454
+ p.insertionMode = InsertionMode.IN_TEMPLATE;
1455
+ p.tmplInsertionModeStack.unshift(InsertionMode.IN_TEMPLATE);
1456
+ break;
1457
+ }
1458
+ case $.HEAD: {
1459
+ p._err(token, ERR.misplacedStartTagForHeadElement);
1460
+ break;
1461
+ }
1462
+ default: {
1463
+ tokenInHead(p, token);
1464
+ }
1465
+ }
1466
+ }
1467
+ function endTagInHead(p, token) {
1468
+ switch (token.tagID) {
1469
+ case $.HEAD: {
1470
+ p.openElements.pop();
1471
+ p.insertionMode = InsertionMode.AFTER_HEAD;
1472
+ break;
1473
+ }
1474
+ case $.BODY:
1475
+ case $.BR:
1476
+ case $.HTML: {
1477
+ tokenInHead(p, token);
1478
+ break;
1479
+ }
1480
+ case $.TEMPLATE: {
1481
+ templateEndTagInHead(p, token);
1482
+ break;
1483
+ }
1484
+ default: {
1485
+ p._err(token, ERR.endTagWithoutMatchingOpenElement);
1486
+ }
1487
+ }
1488
+ }
1489
+ function templateEndTagInHead(p, token) {
1490
+ if (p.openElements.tmplCount > 0) {
1491
+ p.openElements.generateImpliedEndTagsThoroughly();
1492
+ if (p.openElements.currentTagId !== $.TEMPLATE) {
1493
+ p._err(token, ERR.closingOfElementWithOpenChildElements);
1494
+ }
1495
+ p.openElements.popUntilTagNamePopped($.TEMPLATE);
1496
+ p.activeFormattingElements.clearToLastMarker();
1497
+ p.tmplInsertionModeStack.shift();
1498
+ p._resetInsertionMode();
1499
+ }
1500
+ else {
1501
+ p._err(token, ERR.endTagWithoutMatchingOpenElement);
1502
+ }
1503
+ }
1504
+ function tokenInHead(p, token) {
1505
+ p.openElements.pop();
1506
+ p.insertionMode = InsertionMode.AFTER_HEAD;
1507
+ p._processToken(token);
1508
+ }
1509
+ // The "in head no script" insertion mode
1510
+ //------------------------------------------------------------------
1511
+ function startTagInHeadNoScript(p, token) {
1512
+ switch (token.tagID) {
1513
+ case $.HTML: {
1514
+ startTagInBody(p, token);
1515
+ break;
1516
+ }
1517
+ case $.BASEFONT:
1518
+ case $.BGSOUND:
1519
+ case $.HEAD:
1520
+ case $.LINK:
1521
+ case $.META:
1522
+ case $.NOFRAMES:
1523
+ case $.STYLE: {
1524
+ startTagInHead(p, token);
1525
+ break;
1526
+ }
1527
+ case $.NOSCRIPT: {
1528
+ p._err(token, ERR.nestedNoscriptInHead);
1529
+ break;
1530
+ }
1531
+ default: {
1532
+ tokenInHeadNoScript(p, token);
1533
+ }
1534
+ }
1535
+ }
1536
+ function endTagInHeadNoScript(p, token) {
1537
+ switch (token.tagID) {
1538
+ case $.NOSCRIPT: {
1539
+ p.openElements.pop();
1540
+ p.insertionMode = InsertionMode.IN_HEAD;
1541
+ break;
1542
+ }
1543
+ case $.BR: {
1544
+ tokenInHeadNoScript(p, token);
1545
+ break;
1546
+ }
1547
+ default: {
1548
+ p._err(token, ERR.endTagWithoutMatchingOpenElement);
1549
+ }
1550
+ }
1551
+ }
1552
+ function tokenInHeadNoScript(p, token) {
1553
+ const errCode = token.type === TokenType.EOF ? ERR.openElementsLeftAfterEof : ERR.disallowedContentInNoscriptInHead;
1554
+ p._err(token, errCode);
1555
+ p.openElements.pop();
1556
+ p.insertionMode = InsertionMode.IN_HEAD;
1557
+ p._processToken(token);
1558
+ }
1559
+ // The "after head" insertion mode
1560
+ //------------------------------------------------------------------
1561
+ function startTagAfterHead(p, token) {
1562
+ switch (token.tagID) {
1563
+ case $.HTML: {
1564
+ startTagInBody(p, token);
1565
+ break;
1566
+ }
1567
+ case $.BODY: {
1568
+ p._insertElement(token, NS.HTML);
1569
+ p.framesetOk = false;
1570
+ p.insertionMode = InsertionMode.IN_BODY;
1571
+ break;
1572
+ }
1573
+ case $.FRAMESET: {
1574
+ p._insertElement(token, NS.HTML);
1575
+ p.insertionMode = InsertionMode.IN_FRAMESET;
1576
+ break;
1577
+ }
1578
+ case $.BASE:
1579
+ case $.BASEFONT:
1580
+ case $.BGSOUND:
1581
+ case $.LINK:
1582
+ case $.META:
1583
+ case $.NOFRAMES:
1584
+ case $.SCRIPT:
1585
+ case $.STYLE:
1586
+ case $.TEMPLATE:
1587
+ case $.TITLE: {
1588
+ p._err(token, ERR.abandonedHeadElementChild);
1589
+ p.openElements.push(p.headElement, $.HEAD);
1590
+ startTagInHead(p, token);
1591
+ p.openElements.remove(p.headElement);
1592
+ break;
1593
+ }
1594
+ case $.HEAD: {
1595
+ p._err(token, ERR.misplacedStartTagForHeadElement);
1596
+ break;
1597
+ }
1598
+ default: {
1599
+ tokenAfterHead(p, token);
1600
+ }
1601
+ }
1602
+ }
1603
+ function endTagAfterHead(p, token) {
1604
+ switch (token.tagID) {
1605
+ case $.BODY:
1606
+ case $.HTML:
1607
+ case $.BR: {
1608
+ tokenAfterHead(p, token);
1609
+ break;
1610
+ }
1611
+ case $.TEMPLATE: {
1612
+ templateEndTagInHead(p, token);
1613
+ break;
1614
+ }
1615
+ default: {
1616
+ p._err(token, ERR.endTagWithoutMatchingOpenElement);
1617
+ }
1618
+ }
1619
+ }
1620
+ function tokenAfterHead(p, token) {
1621
+ p._insertFakeElement(TN.BODY, $.BODY);
1622
+ p.insertionMode = InsertionMode.IN_BODY;
1623
+ modeInBody(p, token);
1624
+ }
1625
+ // The "in body" insertion mode
1626
+ //------------------------------------------------------------------
1627
+ function modeInBody(p, token) {
1628
+ switch (token.type) {
1629
+ case TokenType.CHARACTER: {
1630
+ characterInBody(p, token);
1631
+ break;
1632
+ }
1633
+ case TokenType.WHITESPACE_CHARACTER: {
1634
+ whitespaceCharacterInBody(p, token);
1635
+ break;
1636
+ }
1637
+ case TokenType.COMMENT: {
1638
+ appendComment(p, token);
1639
+ break;
1640
+ }
1641
+ case TokenType.START_TAG: {
1642
+ startTagInBody(p, token);
1643
+ break;
1644
+ }
1645
+ case TokenType.END_TAG: {
1646
+ endTagInBody(p, token);
1647
+ break;
1648
+ }
1649
+ case TokenType.EOF: {
1650
+ eofInBody(p, token);
1651
+ break;
1652
+ }
1653
+ default:
1654
+ // Do nothing
1655
+ }
1656
+ }
1657
+ function whitespaceCharacterInBody(p, token) {
1658
+ p._reconstructActiveFormattingElements();
1659
+ p._insertCharacters(token);
1660
+ }
1661
+ function characterInBody(p, token) {
1662
+ p._reconstructActiveFormattingElements();
1663
+ p._insertCharacters(token);
1664
+ p.framesetOk = false;
1665
+ }
1666
+ function htmlStartTagInBody(p, token) {
1667
+ if (p.openElements.tmplCount === 0) {
1668
+ p.treeAdapter.adoptAttributes(p.openElements.items[0], token.attrs);
1669
+ }
1670
+ }
1671
+ function bodyStartTagInBody(p, token) {
1672
+ const bodyElement = p.openElements.tryPeekProperlyNestedBodyElement();
1673
+ if (bodyElement && p.openElements.tmplCount === 0) {
1674
+ p.framesetOk = false;
1675
+ p.treeAdapter.adoptAttributes(bodyElement, token.attrs);
1676
+ }
1677
+ }
1678
+ function framesetStartTagInBody(p, token) {
1679
+ const bodyElement = p.openElements.tryPeekProperlyNestedBodyElement();
1680
+ if (p.framesetOk && bodyElement) {
1681
+ p.treeAdapter.detachNode(bodyElement);
1682
+ p.openElements.popAllUpToHtmlElement();
1683
+ p._insertElement(token, NS.HTML);
1684
+ p.insertionMode = InsertionMode.IN_FRAMESET;
1685
+ }
1686
+ }
1687
+ function addressStartTagInBody(p, token) {
1688
+ if (p.openElements.hasInButtonScope($.P)) {
1689
+ p._closePElement();
1690
+ }
1691
+ p._insertElement(token, NS.HTML);
1692
+ }
1693
+ function numberedHeaderStartTagInBody(p, token) {
1694
+ if (p.openElements.hasInButtonScope($.P)) {
1695
+ p._closePElement();
1696
+ }
1697
+ if (p.openElements.currentTagId !== undefined && NUMBERED_HEADERS.has(p.openElements.currentTagId)) {
1698
+ p.openElements.pop();
1699
+ }
1700
+ p._insertElement(token, NS.HTML);
1701
+ }
1702
+ function preStartTagInBody(p, token) {
1703
+ if (p.openElements.hasInButtonScope($.P)) {
1704
+ p._closePElement();
1705
+ }
1706
+ p._insertElement(token, NS.HTML);
1707
+ //NOTE: If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move
1708
+ //on to the next one. (Newlines at the start of pre blocks are ignored as an authoring convenience.)
1709
+ p.skipNextNewLine = true;
1710
+ p.framesetOk = false;
1711
+ }
1712
+ function formStartTagInBody(p, token) {
1713
+ const inTemplate = p.openElements.tmplCount > 0;
1714
+ if (!p.formElement || inTemplate) {
1715
+ if (p.openElements.hasInButtonScope($.P)) {
1716
+ p._closePElement();
1717
+ }
1718
+ p._insertElement(token, NS.HTML);
1719
+ if (!inTemplate) {
1720
+ p.formElement = p.openElements.current;
1721
+ }
1722
+ }
1723
+ }
1724
+ function listItemStartTagInBody(p, token) {
1725
+ p.framesetOk = false;
1726
+ const tn = token.tagID;
1727
+ for (let i = p.openElements.stackTop; i >= 0; i--) {
1728
+ const elementId = p.openElements.tagIDs[i];
1729
+ if ((tn === $.LI && elementId === $.LI) ||
1730
+ ((tn === $.DD || tn === $.DT) && (elementId === $.DD || elementId === $.DT))) {
1731
+ p.openElements.generateImpliedEndTagsWithExclusion(elementId);
1732
+ p.openElements.popUntilTagNamePopped(elementId);
1733
+ break;
1734
+ }
1735
+ if (elementId !== $.ADDRESS &&
1736
+ elementId !== $.DIV &&
1737
+ elementId !== $.P &&
1738
+ p._isSpecialElement(p.openElements.items[i], elementId)) {
1739
+ break;
1740
+ }
1741
+ }
1742
+ if (p.openElements.hasInButtonScope($.P)) {
1743
+ p._closePElement();
1744
+ }
1745
+ p._insertElement(token, NS.HTML);
1746
+ }
1747
+ function plaintextStartTagInBody(p, token) {
1748
+ if (p.openElements.hasInButtonScope($.P)) {
1749
+ p._closePElement();
1750
+ }
1751
+ p._insertElement(token, NS.HTML);
1752
+ p.tokenizer.state = TokenizerMode.PLAINTEXT;
1753
+ }
1754
+ function buttonStartTagInBody(p, token) {
1755
+ if (p.openElements.hasInScope($.BUTTON)) {
1756
+ p.openElements.generateImpliedEndTags();
1757
+ p.openElements.popUntilTagNamePopped($.BUTTON);
1758
+ }
1759
+ p._reconstructActiveFormattingElements();
1760
+ p._insertElement(token, NS.HTML);
1761
+ p.framesetOk = false;
1762
+ }
1763
+ function aStartTagInBody(p, token) {
1764
+ const activeElementEntry = p.activeFormattingElements.getElementEntryInScopeWithTagName(TN.A);
1765
+ if (activeElementEntry) {
1766
+ callAdoptionAgency(p, token);
1767
+ p.openElements.remove(activeElementEntry.element);
1768
+ p.activeFormattingElements.removeEntry(activeElementEntry);
1769
+ }
1770
+ p._reconstructActiveFormattingElements();
1771
+ p._insertElement(token, NS.HTML);
1772
+ p.activeFormattingElements.pushElement(p.openElements.current, token);
1773
+ }
1774
+ function bStartTagInBody(p, token) {
1775
+ p._reconstructActiveFormattingElements();
1776
+ p._insertElement(token, NS.HTML);
1777
+ p.activeFormattingElements.pushElement(p.openElements.current, token);
1778
+ }
1779
+ function nobrStartTagInBody(p, token) {
1780
+ p._reconstructActiveFormattingElements();
1781
+ if (p.openElements.hasInScope($.NOBR)) {
1782
+ callAdoptionAgency(p, token);
1783
+ p._reconstructActiveFormattingElements();
1784
+ }
1785
+ p._insertElement(token, NS.HTML);
1786
+ p.activeFormattingElements.pushElement(p.openElements.current, token);
1787
+ }
1788
+ function appletStartTagInBody(p, token) {
1789
+ p._reconstructActiveFormattingElements();
1790
+ p._insertElement(token, NS.HTML);
1791
+ p.activeFormattingElements.insertMarker();
1792
+ p.framesetOk = false;
1793
+ }
1794
+ function tableStartTagInBody(p, token) {
1795
+ if (p.treeAdapter.getDocumentMode(p.document) !== DOCUMENT_MODE.QUIRKS && p.openElements.hasInButtonScope($.P)) {
1796
+ p._closePElement();
1797
+ }
1798
+ p._insertElement(token, NS.HTML);
1799
+ p.framesetOk = false;
1800
+ p.insertionMode = InsertionMode.IN_TABLE;
1801
+ }
1802
+ function areaStartTagInBody(p, token) {
1803
+ p._reconstructActiveFormattingElements();
1804
+ p._appendElement(token, NS.HTML);
1805
+ p.framesetOk = false;
1806
+ token.ackSelfClosing = true;
1807
+ }
1808
+ function isHiddenInput(token) {
1809
+ const inputType = getTokenAttr(token, ATTRS.TYPE);
1810
+ return inputType != null && inputType.toLowerCase() === HIDDEN_INPUT_TYPE;
1811
+ }
1812
+ function inputStartTagInBody(p, token) {
1813
+ p._reconstructActiveFormattingElements();
1814
+ p._appendElement(token, NS.HTML);
1815
+ if (!isHiddenInput(token)) {
1816
+ p.framesetOk = false;
1817
+ }
1818
+ token.ackSelfClosing = true;
1819
+ }
1820
+ function paramStartTagInBody(p, token) {
1821
+ p._appendElement(token, NS.HTML);
1822
+ token.ackSelfClosing = true;
1823
+ }
1824
+ function hrStartTagInBody(p, token) {
1825
+ if (p.openElements.hasInButtonScope($.P)) {
1826
+ p._closePElement();
1827
+ }
1828
+ p._appendElement(token, NS.HTML);
1829
+ p.framesetOk = false;
1830
+ token.ackSelfClosing = true;
1831
+ }
1832
+ function imageStartTagInBody(p, token) {
1833
+ token.tagName = TN.IMG;
1834
+ token.tagID = $.IMG;
1835
+ areaStartTagInBody(p, token);
1836
+ }
1837
+ function textareaStartTagInBody(p, token) {
1838
+ p._insertElement(token, NS.HTML);
1839
+ //NOTE: If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move
1840
+ //on to the next one. (Newlines at the start of textarea elements are ignored as an authoring convenience.)
1841
+ p.skipNextNewLine = true;
1842
+ p.tokenizer.state = TokenizerMode.RCDATA;
1843
+ p.originalInsertionMode = p.insertionMode;
1844
+ p.framesetOk = false;
1845
+ p.insertionMode = InsertionMode.TEXT;
1846
+ }
1847
+ function xmpStartTagInBody(p, token) {
1848
+ if (p.openElements.hasInButtonScope($.P)) {
1849
+ p._closePElement();
1850
+ }
1851
+ p._reconstructActiveFormattingElements();
1852
+ p.framesetOk = false;
1853
+ p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
1854
+ }
1855
+ function iframeStartTagInBody(p, token) {
1856
+ p.framesetOk = false;
1857
+ p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
1858
+ }
1859
+ //NOTE: here we assume that we always act as a user agent with enabled plugins/frames, so we parse
1860
+ //<noembed>/<noframes> as rawtext.
1861
+ function rawTextStartTagInBody(p, token) {
1862
+ p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
1863
+ }
1864
+ function selectStartTagInBody(p, token) {
1865
+ p._reconstructActiveFormattingElements();
1866
+ p._insertElement(token, NS.HTML);
1867
+ p.framesetOk = false;
1868
+ p.insertionMode =
1869
+ p.insertionMode === InsertionMode.IN_TABLE ||
1870
+ p.insertionMode === InsertionMode.IN_CAPTION ||
1871
+ p.insertionMode === InsertionMode.IN_TABLE_BODY ||
1872
+ p.insertionMode === InsertionMode.IN_ROW ||
1873
+ p.insertionMode === InsertionMode.IN_CELL
1874
+ ? InsertionMode.IN_SELECT_IN_TABLE
1875
+ : InsertionMode.IN_SELECT;
1876
+ }
1877
+ function optgroupStartTagInBody(p, token) {
1878
+ if (p.openElements.currentTagId === $.OPTION) {
1879
+ p.openElements.pop();
1880
+ }
1881
+ p._reconstructActiveFormattingElements();
1882
+ p._insertElement(token, NS.HTML);
1883
+ }
1884
+ function rbStartTagInBody(p, token) {
1885
+ if (p.openElements.hasInScope($.RUBY)) {
1886
+ p.openElements.generateImpliedEndTags();
1887
+ }
1888
+ p._insertElement(token, NS.HTML);
1889
+ }
1890
+ function rtStartTagInBody(p, token) {
1891
+ if (p.openElements.hasInScope($.RUBY)) {
1892
+ p.openElements.generateImpliedEndTagsWithExclusion($.RTC);
1893
+ }
1894
+ p._insertElement(token, NS.HTML);
1895
+ }
1896
+ function mathStartTagInBody(p, token) {
1897
+ p._reconstructActiveFormattingElements();
1898
+ foreignContent.adjustTokenMathMLAttrs(token);
1899
+ foreignContent.adjustTokenXMLAttrs(token);
1900
+ if (token.selfClosing) {
1901
+ p._appendElement(token, NS.MATHML);
1902
+ }
1903
+ else {
1904
+ p._insertElement(token, NS.MATHML);
1905
+ }
1906
+ token.ackSelfClosing = true;
1907
+ }
1908
+ function svgStartTagInBody(p, token) {
1909
+ p._reconstructActiveFormattingElements();
1910
+ foreignContent.adjustTokenSVGAttrs(token);
1911
+ foreignContent.adjustTokenXMLAttrs(token);
1912
+ if (token.selfClosing) {
1913
+ p._appendElement(token, NS.SVG);
1914
+ }
1915
+ else {
1916
+ p._insertElement(token, NS.SVG);
1917
+ }
1918
+ token.ackSelfClosing = true;
1919
+ }
1920
+ function genericStartTagInBody(p, token) {
1921
+ p._reconstructActiveFormattingElements();
1922
+ p._insertElement(token, NS.HTML);
1923
+ }
1924
+ function startTagInBody(p, token) {
1925
+ switch (token.tagID) {
1926
+ case $.I:
1927
+ case $.S:
1928
+ case $.B:
1929
+ case $.U:
1930
+ case $.EM:
1931
+ case $.TT:
1932
+ case $.BIG:
1933
+ case $.CODE:
1934
+ case $.FONT:
1935
+ case $.SMALL:
1936
+ case $.STRIKE:
1937
+ case $.STRONG: {
1938
+ bStartTagInBody(p, token);
1939
+ break;
1940
+ }
1941
+ case $.A: {
1942
+ aStartTagInBody(p, token);
1943
+ break;
1944
+ }
1945
+ case $.H1:
1946
+ case $.H2:
1947
+ case $.H3:
1948
+ case $.H4:
1949
+ case $.H5:
1950
+ case $.H6: {
1951
+ numberedHeaderStartTagInBody(p, token);
1952
+ break;
1953
+ }
1954
+ case $.P:
1955
+ case $.DL:
1956
+ case $.OL:
1957
+ case $.UL:
1958
+ case $.DIV:
1959
+ case $.DIR:
1960
+ case $.NAV:
1961
+ case $.MAIN:
1962
+ case $.MENU:
1963
+ case $.ASIDE:
1964
+ case $.CENTER:
1965
+ case $.FIGURE:
1966
+ case $.FOOTER:
1967
+ case $.HEADER:
1968
+ case $.HGROUP:
1969
+ case $.DIALOG:
1970
+ case $.DETAILS:
1971
+ case $.ADDRESS:
1972
+ case $.ARTICLE:
1973
+ case $.SEARCH:
1974
+ case $.SECTION:
1975
+ case $.SUMMARY:
1976
+ case $.FIELDSET:
1977
+ case $.BLOCKQUOTE:
1978
+ case $.FIGCAPTION: {
1979
+ addressStartTagInBody(p, token);
1980
+ break;
1981
+ }
1982
+ case $.LI:
1983
+ case $.DD:
1984
+ case $.DT: {
1985
+ listItemStartTagInBody(p, token);
1986
+ break;
1987
+ }
1988
+ case $.BR:
1989
+ case $.IMG:
1990
+ case $.WBR:
1991
+ case $.AREA:
1992
+ case $.EMBED:
1993
+ case $.KEYGEN: {
1994
+ areaStartTagInBody(p, token);
1995
+ break;
1996
+ }
1997
+ case $.HR: {
1998
+ hrStartTagInBody(p, token);
1999
+ break;
2000
+ }
2001
+ case $.RB:
2002
+ case $.RTC: {
2003
+ rbStartTagInBody(p, token);
2004
+ break;
2005
+ }
2006
+ case $.RT:
2007
+ case $.RP: {
2008
+ rtStartTagInBody(p, token);
2009
+ break;
2010
+ }
2011
+ case $.PRE:
2012
+ case $.LISTING: {
2013
+ preStartTagInBody(p, token);
2014
+ break;
2015
+ }
2016
+ case $.XMP: {
2017
+ xmpStartTagInBody(p, token);
2018
+ break;
2019
+ }
2020
+ case $.SVG: {
2021
+ svgStartTagInBody(p, token);
2022
+ break;
2023
+ }
2024
+ case $.HTML: {
2025
+ htmlStartTagInBody(p, token);
2026
+ break;
2027
+ }
2028
+ case $.BASE:
2029
+ case $.LINK:
2030
+ case $.META:
2031
+ case $.STYLE:
2032
+ case $.TITLE:
2033
+ case $.SCRIPT:
2034
+ case $.BGSOUND:
2035
+ case $.BASEFONT:
2036
+ case $.TEMPLATE: {
2037
+ startTagInHead(p, token);
2038
+ break;
2039
+ }
2040
+ case $.BODY: {
2041
+ bodyStartTagInBody(p, token);
2042
+ break;
2043
+ }
2044
+ case $.FORM: {
2045
+ formStartTagInBody(p, token);
2046
+ break;
2047
+ }
2048
+ case $.NOBR: {
2049
+ nobrStartTagInBody(p, token);
2050
+ break;
2051
+ }
2052
+ case $.MATH: {
2053
+ mathStartTagInBody(p, token);
2054
+ break;
2055
+ }
2056
+ case $.TABLE: {
2057
+ tableStartTagInBody(p, token);
2058
+ break;
2059
+ }
2060
+ case $.INPUT: {
2061
+ inputStartTagInBody(p, token);
2062
+ break;
2063
+ }
2064
+ case $.PARAM:
2065
+ case $.TRACK:
2066
+ case $.SOURCE: {
2067
+ paramStartTagInBody(p, token);
2068
+ break;
2069
+ }
2070
+ case $.IMAGE: {
2071
+ imageStartTagInBody(p, token);
2072
+ break;
2073
+ }
2074
+ case $.BUTTON: {
2075
+ buttonStartTagInBody(p, token);
2076
+ break;
2077
+ }
2078
+ case $.APPLET:
2079
+ case $.OBJECT:
2080
+ case $.MARQUEE: {
2081
+ appletStartTagInBody(p, token);
2082
+ break;
2083
+ }
2084
+ case $.IFRAME: {
2085
+ iframeStartTagInBody(p, token);
2086
+ break;
2087
+ }
2088
+ case $.SELECT: {
2089
+ selectStartTagInBody(p, token);
2090
+ break;
2091
+ }
2092
+ case $.OPTION:
2093
+ case $.OPTGROUP: {
2094
+ optgroupStartTagInBody(p, token);
2095
+ break;
2096
+ }
2097
+ case $.NOEMBED:
2098
+ case $.NOFRAMES: {
2099
+ rawTextStartTagInBody(p, token);
2100
+ break;
2101
+ }
2102
+ case $.FRAMESET: {
2103
+ framesetStartTagInBody(p, token);
2104
+ break;
2105
+ }
2106
+ case $.TEXTAREA: {
2107
+ textareaStartTagInBody(p, token);
2108
+ break;
2109
+ }
2110
+ case $.NOSCRIPT: {
2111
+ if (p.options.scriptingEnabled) {
2112
+ rawTextStartTagInBody(p, token);
2113
+ }
2114
+ else {
2115
+ genericStartTagInBody(p, token);
2116
+ }
2117
+ break;
2118
+ }
2119
+ case $.PLAINTEXT: {
2120
+ plaintextStartTagInBody(p, token);
2121
+ break;
2122
+ }
2123
+ case $.COL:
2124
+ case $.TH:
2125
+ case $.TD:
2126
+ case $.TR:
2127
+ case $.HEAD:
2128
+ case $.FRAME:
2129
+ case $.TBODY:
2130
+ case $.TFOOT:
2131
+ case $.THEAD:
2132
+ case $.CAPTION:
2133
+ case $.COLGROUP: {
2134
+ // Ignore token
2135
+ break;
2136
+ }
2137
+ default: {
2138
+ genericStartTagInBody(p, token);
2139
+ }
2140
+ }
2141
+ }
2142
+ function bodyEndTagInBody(p, token) {
2143
+ if (p.openElements.hasInScope($.BODY)) {
2144
+ p.insertionMode = InsertionMode.AFTER_BODY;
2145
+ //NOTE: <body> is never popped from the stack, so we need to updated
2146
+ //the end location explicitly.
2147
+ if (p.options.sourceCodeLocationInfo) {
2148
+ const bodyElement = p.openElements.tryPeekProperlyNestedBodyElement();
2149
+ if (bodyElement) {
2150
+ p._setEndLocation(bodyElement, token);
2151
+ }
2152
+ }
2153
+ }
2154
+ }
2155
+ function htmlEndTagInBody(p, token) {
2156
+ if (p.openElements.hasInScope($.BODY)) {
2157
+ p.insertionMode = InsertionMode.AFTER_BODY;
2158
+ endTagAfterBody(p, token);
2159
+ }
2160
+ }
2161
+ function addressEndTagInBody(p, token) {
2162
+ const tn = token.tagID;
2163
+ if (p.openElements.hasInScope(tn)) {
2164
+ p.openElements.generateImpliedEndTags();
2165
+ p.openElements.popUntilTagNamePopped(tn);
2166
+ }
2167
+ }
2168
+ function formEndTagInBody(p) {
2169
+ const inTemplate = p.openElements.tmplCount > 0;
2170
+ const { formElement } = p;
2171
+ if (!inTemplate) {
2172
+ p.formElement = null;
2173
+ }
2174
+ if ((formElement || inTemplate) && p.openElements.hasInScope($.FORM)) {
2175
+ p.openElements.generateImpliedEndTags();
2176
+ if (inTemplate) {
2177
+ p.openElements.popUntilTagNamePopped($.FORM);
2178
+ }
2179
+ else if (formElement) {
2180
+ p.openElements.remove(formElement);
2181
+ }
2182
+ }
2183
+ }
2184
+ function pEndTagInBody(p) {
2185
+ if (!p.openElements.hasInButtonScope($.P)) {
2186
+ p._insertFakeElement(TN.P, $.P);
2187
+ }
2188
+ p._closePElement();
2189
+ }
2190
+ function liEndTagInBody(p) {
2191
+ if (p.openElements.hasInListItemScope($.LI)) {
2192
+ p.openElements.generateImpliedEndTagsWithExclusion($.LI);
2193
+ p.openElements.popUntilTagNamePopped($.LI);
2194
+ }
2195
+ }
2196
+ function ddEndTagInBody(p, token) {
2197
+ const tn = token.tagID;
2198
+ if (p.openElements.hasInScope(tn)) {
2199
+ p.openElements.generateImpliedEndTagsWithExclusion(tn);
2200
+ p.openElements.popUntilTagNamePopped(tn);
2201
+ }
2202
+ }
2203
+ function numberedHeaderEndTagInBody(p) {
2204
+ if (p.openElements.hasNumberedHeaderInScope()) {
2205
+ p.openElements.generateImpliedEndTags();
2206
+ p.openElements.popUntilNumberedHeaderPopped();
2207
+ }
2208
+ }
2209
+ function appletEndTagInBody(p, token) {
2210
+ const tn = token.tagID;
2211
+ if (p.openElements.hasInScope(tn)) {
2212
+ p.openElements.generateImpliedEndTags();
2213
+ p.openElements.popUntilTagNamePopped(tn);
2214
+ p.activeFormattingElements.clearToLastMarker();
2215
+ }
2216
+ }
2217
+ function brEndTagInBody(p) {
2218
+ p._reconstructActiveFormattingElements();
2219
+ p._insertFakeElement(TN.BR, $.BR);
2220
+ p.openElements.pop();
2221
+ p.framesetOk = false;
2222
+ }
2223
+ function genericEndTagInBody(p, token) {
2224
+ const tn = token.tagName;
2225
+ const tid = token.tagID;
2226
+ for (let i = p.openElements.stackTop; i > 0; i--) {
2227
+ const element = p.openElements.items[i];
2228
+ const elementId = p.openElements.tagIDs[i];
2229
+ // Compare the tag name here, as the tag might not be a known tag with an ID.
2230
+ if (tid === elementId && (tid !== $.UNKNOWN || p.treeAdapter.getTagName(element) === tn)) {
2231
+ p.openElements.generateImpliedEndTagsWithExclusion(tid);
2232
+ if (p.openElements.stackTop >= i)
2233
+ p.openElements.shortenToLength(i);
2234
+ break;
2235
+ }
2236
+ if (p._isSpecialElement(element, elementId)) {
2237
+ break;
2238
+ }
2239
+ }
2240
+ }
2241
+ function endTagInBody(p, token) {
2242
+ switch (token.tagID) {
2243
+ case $.A:
2244
+ case $.B:
2245
+ case $.I:
2246
+ case $.S:
2247
+ case $.U:
2248
+ case $.EM:
2249
+ case $.TT:
2250
+ case $.BIG:
2251
+ case $.CODE:
2252
+ case $.FONT:
2253
+ case $.NOBR:
2254
+ case $.SMALL:
2255
+ case $.STRIKE:
2256
+ case $.STRONG: {
2257
+ callAdoptionAgency(p, token);
2258
+ break;
2259
+ }
2260
+ case $.P: {
2261
+ pEndTagInBody(p);
2262
+ break;
2263
+ }
2264
+ case $.DL:
2265
+ case $.UL:
2266
+ case $.OL:
2267
+ case $.DIR:
2268
+ case $.DIV:
2269
+ case $.NAV:
2270
+ case $.PRE:
2271
+ case $.MAIN:
2272
+ case $.MENU:
2273
+ case $.ASIDE:
2274
+ case $.BUTTON:
2275
+ case $.CENTER:
2276
+ case $.FIGURE:
2277
+ case $.FOOTER:
2278
+ case $.HEADER:
2279
+ case $.HGROUP:
2280
+ case $.DIALOG:
2281
+ case $.ADDRESS:
2282
+ case $.ARTICLE:
2283
+ case $.DETAILS:
2284
+ case $.SEARCH:
2285
+ case $.SECTION:
2286
+ case $.SUMMARY:
2287
+ case $.LISTING:
2288
+ case $.FIELDSET:
2289
+ case $.BLOCKQUOTE:
2290
+ case $.FIGCAPTION: {
2291
+ addressEndTagInBody(p, token);
2292
+ break;
2293
+ }
2294
+ case $.LI: {
2295
+ liEndTagInBody(p);
2296
+ break;
2297
+ }
2298
+ case $.DD:
2299
+ case $.DT: {
2300
+ ddEndTagInBody(p, token);
2301
+ break;
2302
+ }
2303
+ case $.H1:
2304
+ case $.H2:
2305
+ case $.H3:
2306
+ case $.H4:
2307
+ case $.H5:
2308
+ case $.H6: {
2309
+ numberedHeaderEndTagInBody(p);
2310
+ break;
2311
+ }
2312
+ case $.BR: {
2313
+ brEndTagInBody(p);
2314
+ break;
2315
+ }
2316
+ case $.BODY: {
2317
+ bodyEndTagInBody(p, token);
2318
+ break;
2319
+ }
2320
+ case $.HTML: {
2321
+ htmlEndTagInBody(p, token);
2322
+ break;
2323
+ }
2324
+ case $.FORM: {
2325
+ formEndTagInBody(p);
2326
+ break;
2327
+ }
2328
+ case $.APPLET:
2329
+ case $.OBJECT:
2330
+ case $.MARQUEE: {
2331
+ appletEndTagInBody(p, token);
2332
+ break;
2333
+ }
2334
+ case $.TEMPLATE: {
2335
+ templateEndTagInHead(p, token);
2336
+ break;
2337
+ }
2338
+ default: {
2339
+ genericEndTagInBody(p, token);
2340
+ }
2341
+ }
2342
+ }
2343
+ function eofInBody(p, token) {
2344
+ if (p.tmplInsertionModeStack.length > 0) {
2345
+ eofInTemplate(p, token);
2346
+ }
2347
+ else {
2348
+ stopParsing(p, token);
2349
+ }
2350
+ }
2351
+ // The "text" insertion mode
2352
+ //------------------------------------------------------------------
2353
+ function endTagInText(p, token) {
2354
+ var _a;
2355
+ if (token.tagID === $.SCRIPT) {
2356
+ (_a = p.scriptHandler) === null || _a === void 0 ? void 0 : _a.call(p, p.openElements.current);
2357
+ }
2358
+ p.openElements.pop();
2359
+ p.insertionMode = p.originalInsertionMode;
2360
+ }
2361
+ function eofInText(p, token) {
2362
+ p._err(token, ERR.eofInElementThatCanContainOnlyText);
2363
+ p.openElements.pop();
2364
+ p.insertionMode = p.originalInsertionMode;
2365
+ p.onEof(token);
2366
+ }
2367
+ // The "in table" insertion mode
2368
+ //------------------------------------------------------------------
2369
+ function characterInTable(p, token) {
2370
+ if (p.openElements.currentTagId !== undefined && TABLE_STRUCTURE_TAGS.has(p.openElements.currentTagId)) {
2371
+ p.pendingCharacterTokens.length = 0;
2372
+ p.hasNonWhitespacePendingCharacterToken = false;
2373
+ p.originalInsertionMode = p.insertionMode;
2374
+ p.insertionMode = InsertionMode.IN_TABLE_TEXT;
2375
+ switch (token.type) {
2376
+ case TokenType.CHARACTER: {
2377
+ characterInTableText(p, token);
2378
+ break;
2379
+ }
2380
+ case TokenType.WHITESPACE_CHARACTER: {
2381
+ whitespaceCharacterInTableText(p, token);
2382
+ break;
2383
+ }
2384
+ // Ignore null
2385
+ }
2386
+ }
2387
+ else {
2388
+ tokenInTable(p, token);
2389
+ }
2390
+ }
2391
+ function captionStartTagInTable(p, token) {
2392
+ p.openElements.clearBackToTableContext();
2393
+ p.activeFormattingElements.insertMarker();
2394
+ p._insertElement(token, NS.HTML);
2395
+ p.insertionMode = InsertionMode.IN_CAPTION;
2396
+ }
2397
+ function colgroupStartTagInTable(p, token) {
2398
+ p.openElements.clearBackToTableContext();
2399
+ p._insertElement(token, NS.HTML);
2400
+ p.insertionMode = InsertionMode.IN_COLUMN_GROUP;
2401
+ }
2402
+ function colStartTagInTable(p, token) {
2403
+ p.openElements.clearBackToTableContext();
2404
+ p._insertFakeElement(TN.COLGROUP, $.COLGROUP);
2405
+ p.insertionMode = InsertionMode.IN_COLUMN_GROUP;
2406
+ startTagInColumnGroup(p, token);
2407
+ }
2408
+ function tbodyStartTagInTable(p, token) {
2409
+ p.openElements.clearBackToTableContext();
2410
+ p._insertElement(token, NS.HTML);
2411
+ p.insertionMode = InsertionMode.IN_TABLE_BODY;
2412
+ }
2413
+ function tdStartTagInTable(p, token) {
2414
+ p.openElements.clearBackToTableContext();
2415
+ p._insertFakeElement(TN.TBODY, $.TBODY);
2416
+ p.insertionMode = InsertionMode.IN_TABLE_BODY;
2417
+ startTagInTableBody(p, token);
2418
+ }
2419
+ function tableStartTagInTable(p, token) {
2420
+ if (p.openElements.hasInTableScope($.TABLE)) {
2421
+ p.openElements.popUntilTagNamePopped($.TABLE);
2422
+ p._resetInsertionMode();
2423
+ p._processStartTag(token);
2424
+ }
2425
+ }
2426
+ function inputStartTagInTable(p, token) {
2427
+ if (isHiddenInput(token)) {
2428
+ p._appendElement(token, NS.HTML);
2429
+ }
2430
+ else {
2431
+ tokenInTable(p, token);
2432
+ }
2433
+ token.ackSelfClosing = true;
2434
+ }
2435
+ function formStartTagInTable(p, token) {
2436
+ if (!p.formElement && p.openElements.tmplCount === 0) {
2437
+ p._insertElement(token, NS.HTML);
2438
+ p.formElement = p.openElements.current;
2439
+ p.openElements.pop();
2440
+ }
2441
+ }
2442
+ function startTagInTable(p, token) {
2443
+ switch (token.tagID) {
2444
+ case $.TD:
2445
+ case $.TH:
2446
+ case $.TR: {
2447
+ tdStartTagInTable(p, token);
2448
+ break;
2449
+ }
2450
+ case $.STYLE:
2451
+ case $.SCRIPT:
2452
+ case $.TEMPLATE: {
2453
+ startTagInHead(p, token);
2454
+ break;
2455
+ }
2456
+ case $.COL: {
2457
+ colStartTagInTable(p, token);
2458
+ break;
2459
+ }
2460
+ case $.FORM: {
2461
+ formStartTagInTable(p, token);
2462
+ break;
2463
+ }
2464
+ case $.TABLE: {
2465
+ tableStartTagInTable(p, token);
2466
+ break;
2467
+ }
2468
+ case $.TBODY:
2469
+ case $.TFOOT:
2470
+ case $.THEAD: {
2471
+ tbodyStartTagInTable(p, token);
2472
+ break;
2473
+ }
2474
+ case $.INPUT: {
2475
+ inputStartTagInTable(p, token);
2476
+ break;
2477
+ }
2478
+ case $.CAPTION: {
2479
+ captionStartTagInTable(p, token);
2480
+ break;
2481
+ }
2482
+ case $.COLGROUP: {
2483
+ colgroupStartTagInTable(p, token);
2484
+ break;
2485
+ }
2486
+ default: {
2487
+ tokenInTable(p, token);
2488
+ }
2489
+ }
2490
+ }
2491
+ function endTagInTable(p, token) {
2492
+ switch (token.tagID) {
2493
+ case $.TABLE: {
2494
+ if (p.openElements.hasInTableScope($.TABLE)) {
2495
+ p.openElements.popUntilTagNamePopped($.TABLE);
2496
+ p._resetInsertionMode();
2497
+ }
2498
+ break;
2499
+ }
2500
+ case $.TEMPLATE: {
2501
+ templateEndTagInHead(p, token);
2502
+ break;
2503
+ }
2504
+ case $.BODY:
2505
+ case $.CAPTION:
2506
+ case $.COL:
2507
+ case $.COLGROUP:
2508
+ case $.HTML:
2509
+ case $.TBODY:
2510
+ case $.TD:
2511
+ case $.TFOOT:
2512
+ case $.TH:
2513
+ case $.THEAD:
2514
+ case $.TR: {
2515
+ // Ignore token
2516
+ break;
2517
+ }
2518
+ default: {
2519
+ tokenInTable(p, token);
2520
+ }
2521
+ }
2522
+ }
2523
+ function tokenInTable(p, token) {
2524
+ const savedFosterParentingState = p.fosterParentingEnabled;
2525
+ p.fosterParentingEnabled = true;
2526
+ // Process token in `In Body` mode
2527
+ modeInBody(p, token);
2528
+ p.fosterParentingEnabled = savedFosterParentingState;
2529
+ }
2530
+ // The "in table text" insertion mode
2531
+ //------------------------------------------------------------------
2532
+ function whitespaceCharacterInTableText(p, token) {
2533
+ p.pendingCharacterTokens.push(token);
2534
+ }
2535
+ function characterInTableText(p, token) {
2536
+ p.pendingCharacterTokens.push(token);
2537
+ p.hasNonWhitespacePendingCharacterToken = true;
2538
+ }
2539
+ function tokenInTableText(p, token) {
2540
+ let i = 0;
2541
+ if (p.hasNonWhitespacePendingCharacterToken) {
2542
+ for (; i < p.pendingCharacterTokens.length; i++) {
2543
+ tokenInTable(p, p.pendingCharacterTokens[i]);
2544
+ }
2545
+ }
2546
+ else {
2547
+ for (; i < p.pendingCharacterTokens.length; i++) {
2548
+ p._insertCharacters(p.pendingCharacterTokens[i]);
2549
+ }
2550
+ }
2551
+ p.insertionMode = p.originalInsertionMode;
2552
+ p._processToken(token);
2553
+ }
2554
+ // The "in caption" insertion mode
2555
+ //------------------------------------------------------------------
2556
+ const TABLE_VOID_ELEMENTS = new Set([$.CAPTION, $.COL, $.COLGROUP, $.TBODY, $.TD, $.TFOOT, $.TH, $.THEAD, $.TR]);
2557
+ function startTagInCaption(p, token) {
2558
+ const tn = token.tagID;
2559
+ if (TABLE_VOID_ELEMENTS.has(tn)) {
2560
+ if (p.openElements.hasInTableScope($.CAPTION)) {
2561
+ p.openElements.generateImpliedEndTags();
2562
+ p.openElements.popUntilTagNamePopped($.CAPTION);
2563
+ p.activeFormattingElements.clearToLastMarker();
2564
+ p.insertionMode = InsertionMode.IN_TABLE;
2565
+ startTagInTable(p, token);
2566
+ }
2567
+ }
2568
+ else {
2569
+ startTagInBody(p, token);
2570
+ }
2571
+ }
2572
+ function endTagInCaption(p, token) {
2573
+ const tn = token.tagID;
2574
+ switch (tn) {
2575
+ case $.CAPTION:
2576
+ case $.TABLE: {
2577
+ if (p.openElements.hasInTableScope($.CAPTION)) {
2578
+ p.openElements.generateImpliedEndTags();
2579
+ p.openElements.popUntilTagNamePopped($.CAPTION);
2580
+ p.activeFormattingElements.clearToLastMarker();
2581
+ p.insertionMode = InsertionMode.IN_TABLE;
2582
+ if (tn === $.TABLE) {
2583
+ endTagInTable(p, token);
2584
+ }
2585
+ }
2586
+ break;
2587
+ }
2588
+ case $.BODY:
2589
+ case $.COL:
2590
+ case $.COLGROUP:
2591
+ case $.HTML:
2592
+ case $.TBODY:
2593
+ case $.TD:
2594
+ case $.TFOOT:
2595
+ case $.TH:
2596
+ case $.THEAD:
2597
+ case $.TR: {
2598
+ // Ignore token
2599
+ break;
2600
+ }
2601
+ default: {
2602
+ endTagInBody(p, token);
2603
+ }
2604
+ }
2605
+ }
2606
+ // The "in column group" insertion mode
2607
+ //------------------------------------------------------------------
2608
+ function startTagInColumnGroup(p, token) {
2609
+ switch (token.tagID) {
2610
+ case $.HTML: {
2611
+ startTagInBody(p, token);
2612
+ break;
2613
+ }
2614
+ case $.COL: {
2615
+ p._appendElement(token, NS.HTML);
2616
+ token.ackSelfClosing = true;
2617
+ break;
2618
+ }
2619
+ case $.TEMPLATE: {
2620
+ startTagInHead(p, token);
2621
+ break;
2622
+ }
2623
+ default: {
2624
+ tokenInColumnGroup(p, token);
2625
+ }
2626
+ }
2627
+ }
2628
+ function endTagInColumnGroup(p, token) {
2629
+ switch (token.tagID) {
2630
+ case $.COLGROUP: {
2631
+ if (p.openElements.currentTagId === $.COLGROUP) {
2632
+ p.openElements.pop();
2633
+ p.insertionMode = InsertionMode.IN_TABLE;
2634
+ }
2635
+ break;
2636
+ }
2637
+ case $.TEMPLATE: {
2638
+ templateEndTagInHead(p, token);
2639
+ break;
2640
+ }
2641
+ case $.COL: {
2642
+ // Ignore token
2643
+ break;
2644
+ }
2645
+ default: {
2646
+ tokenInColumnGroup(p, token);
2647
+ }
2648
+ }
2649
+ }
2650
+ function tokenInColumnGroup(p, token) {
2651
+ if (p.openElements.currentTagId === $.COLGROUP) {
2652
+ p.openElements.pop();
2653
+ p.insertionMode = InsertionMode.IN_TABLE;
2654
+ p._processToken(token);
2655
+ }
2656
+ }
2657
+ // The "in table body" insertion mode
2658
+ //------------------------------------------------------------------
2659
+ function startTagInTableBody(p, token) {
2660
+ switch (token.tagID) {
2661
+ case $.TR: {
2662
+ p.openElements.clearBackToTableBodyContext();
2663
+ p._insertElement(token, NS.HTML);
2664
+ p.insertionMode = InsertionMode.IN_ROW;
2665
+ break;
2666
+ }
2667
+ case $.TH:
2668
+ case $.TD: {
2669
+ p.openElements.clearBackToTableBodyContext();
2670
+ p._insertFakeElement(TN.TR, $.TR);
2671
+ p.insertionMode = InsertionMode.IN_ROW;
2672
+ startTagInRow(p, token);
2673
+ break;
2674
+ }
2675
+ case $.CAPTION:
2676
+ case $.COL:
2677
+ case $.COLGROUP:
2678
+ case $.TBODY:
2679
+ case $.TFOOT:
2680
+ case $.THEAD: {
2681
+ if (p.openElements.hasTableBodyContextInTableScope()) {
2682
+ p.openElements.clearBackToTableBodyContext();
2683
+ p.openElements.pop();
2684
+ p.insertionMode = InsertionMode.IN_TABLE;
2685
+ startTagInTable(p, token);
2686
+ }
2687
+ break;
2688
+ }
2689
+ default: {
2690
+ startTagInTable(p, token);
2691
+ }
2692
+ }
2693
+ }
2694
+ function endTagInTableBody(p, token) {
2695
+ const tn = token.tagID;
2696
+ switch (token.tagID) {
2697
+ case $.TBODY:
2698
+ case $.TFOOT:
2699
+ case $.THEAD: {
2700
+ if (p.openElements.hasInTableScope(tn)) {
2701
+ p.openElements.clearBackToTableBodyContext();
2702
+ p.openElements.pop();
2703
+ p.insertionMode = InsertionMode.IN_TABLE;
2704
+ }
2705
+ break;
2706
+ }
2707
+ case $.TABLE: {
2708
+ if (p.openElements.hasTableBodyContextInTableScope()) {
2709
+ p.openElements.clearBackToTableBodyContext();
2710
+ p.openElements.pop();
2711
+ p.insertionMode = InsertionMode.IN_TABLE;
2712
+ endTagInTable(p, token);
2713
+ }
2714
+ break;
2715
+ }
2716
+ case $.BODY:
2717
+ case $.CAPTION:
2718
+ case $.COL:
2719
+ case $.COLGROUP:
2720
+ case $.HTML:
2721
+ case $.TD:
2722
+ case $.TH:
2723
+ case $.TR: {
2724
+ // Ignore token
2725
+ break;
2726
+ }
2727
+ default: {
2728
+ endTagInTable(p, token);
2729
+ }
2730
+ }
2731
+ }
2732
+ // The "in row" insertion mode
2733
+ //------------------------------------------------------------------
2734
+ function startTagInRow(p, token) {
2735
+ switch (token.tagID) {
2736
+ case $.TH:
2737
+ case $.TD: {
2738
+ p.openElements.clearBackToTableRowContext();
2739
+ p._insertElement(token, NS.HTML);
2740
+ p.insertionMode = InsertionMode.IN_CELL;
2741
+ p.activeFormattingElements.insertMarker();
2742
+ break;
2743
+ }
2744
+ case $.CAPTION:
2745
+ case $.COL:
2746
+ case $.COLGROUP:
2747
+ case $.TBODY:
2748
+ case $.TFOOT:
2749
+ case $.THEAD:
2750
+ case $.TR: {
2751
+ if (p.openElements.hasInTableScope($.TR)) {
2752
+ p.openElements.clearBackToTableRowContext();
2753
+ p.openElements.pop();
2754
+ p.insertionMode = InsertionMode.IN_TABLE_BODY;
2755
+ startTagInTableBody(p, token);
2756
+ }
2757
+ break;
2758
+ }
2759
+ default: {
2760
+ startTagInTable(p, token);
2761
+ }
2762
+ }
2763
+ }
2764
+ function endTagInRow(p, token) {
2765
+ switch (token.tagID) {
2766
+ case $.TR: {
2767
+ if (p.openElements.hasInTableScope($.TR)) {
2768
+ p.openElements.clearBackToTableRowContext();
2769
+ p.openElements.pop();
2770
+ p.insertionMode = InsertionMode.IN_TABLE_BODY;
2771
+ }
2772
+ break;
2773
+ }
2774
+ case $.TABLE: {
2775
+ if (p.openElements.hasInTableScope($.TR)) {
2776
+ p.openElements.clearBackToTableRowContext();
2777
+ p.openElements.pop();
2778
+ p.insertionMode = InsertionMode.IN_TABLE_BODY;
2779
+ endTagInTableBody(p, token);
2780
+ }
2781
+ break;
2782
+ }
2783
+ case $.TBODY:
2784
+ case $.TFOOT:
2785
+ case $.THEAD: {
2786
+ if (p.openElements.hasInTableScope(token.tagID) || p.openElements.hasInTableScope($.TR)) {
2787
+ p.openElements.clearBackToTableRowContext();
2788
+ p.openElements.pop();
2789
+ p.insertionMode = InsertionMode.IN_TABLE_BODY;
2790
+ endTagInTableBody(p, token);
2791
+ }
2792
+ break;
2793
+ }
2794
+ case $.BODY:
2795
+ case $.CAPTION:
2796
+ case $.COL:
2797
+ case $.COLGROUP:
2798
+ case $.HTML:
2799
+ case $.TD:
2800
+ case $.TH: {
2801
+ // Ignore end tag
2802
+ break;
2803
+ }
2804
+ default: {
2805
+ endTagInTable(p, token);
2806
+ }
2807
+ }
2808
+ }
2809
+ // The "in cell" insertion mode
2810
+ //------------------------------------------------------------------
2811
+ function startTagInCell(p, token) {
2812
+ const tn = token.tagID;
2813
+ if (TABLE_VOID_ELEMENTS.has(tn)) {
2814
+ if (p.openElements.hasInTableScope($.TD) || p.openElements.hasInTableScope($.TH)) {
2815
+ p._closeTableCell();
2816
+ startTagInRow(p, token);
2817
+ }
2818
+ }
2819
+ else {
2820
+ startTagInBody(p, token);
2821
+ }
2822
+ }
2823
+ function endTagInCell(p, token) {
2824
+ const tn = token.tagID;
2825
+ switch (tn) {
2826
+ case $.TD:
2827
+ case $.TH: {
2828
+ if (p.openElements.hasInTableScope(tn)) {
2829
+ p.openElements.generateImpliedEndTags();
2830
+ p.openElements.popUntilTagNamePopped(tn);
2831
+ p.activeFormattingElements.clearToLastMarker();
2832
+ p.insertionMode = InsertionMode.IN_ROW;
2833
+ }
2834
+ break;
2835
+ }
2836
+ case $.TABLE:
2837
+ case $.TBODY:
2838
+ case $.TFOOT:
2839
+ case $.THEAD:
2840
+ case $.TR: {
2841
+ if (p.openElements.hasInTableScope(tn)) {
2842
+ p._closeTableCell();
2843
+ endTagInRow(p, token);
2844
+ }
2845
+ break;
2846
+ }
2847
+ case $.BODY:
2848
+ case $.CAPTION:
2849
+ case $.COL:
2850
+ case $.COLGROUP:
2851
+ case $.HTML: {
2852
+ // Ignore token
2853
+ break;
2854
+ }
2855
+ default: {
2856
+ endTagInBody(p, token);
2857
+ }
2858
+ }
2859
+ }
2860
+ // The "in select" insertion mode
2861
+ //------------------------------------------------------------------
2862
+ function startTagInSelect(p, token) {
2863
+ switch (token.tagID) {
2864
+ case $.HTML: {
2865
+ startTagInBody(p, token);
2866
+ break;
2867
+ }
2868
+ case $.OPTION: {
2869
+ if (p.openElements.currentTagId === $.OPTION) {
2870
+ p.openElements.pop();
2871
+ }
2872
+ p._insertElement(token, NS.HTML);
2873
+ break;
2874
+ }
2875
+ case $.OPTGROUP: {
2876
+ if (p.openElements.currentTagId === $.OPTION) {
2877
+ p.openElements.pop();
2878
+ }
2879
+ if (p.openElements.currentTagId === $.OPTGROUP) {
2880
+ p.openElements.pop();
2881
+ }
2882
+ p._insertElement(token, NS.HTML);
2883
+ break;
2884
+ }
2885
+ case $.HR: {
2886
+ if (p.openElements.currentTagId === $.OPTION) {
2887
+ p.openElements.pop();
2888
+ }
2889
+ if (p.openElements.currentTagId === $.OPTGROUP) {
2890
+ p.openElements.pop();
2891
+ }
2892
+ p._appendElement(token, NS.HTML);
2893
+ token.ackSelfClosing = true;
2894
+ break;
2895
+ }
2896
+ case $.INPUT:
2897
+ case $.KEYGEN:
2898
+ case $.TEXTAREA:
2899
+ case $.SELECT: {
2900
+ if (p.openElements.hasInSelectScope($.SELECT)) {
2901
+ p.openElements.popUntilTagNamePopped($.SELECT);
2902
+ p._resetInsertionMode();
2903
+ if (token.tagID !== $.SELECT) {
2904
+ p._processStartTag(token);
2905
+ }
2906
+ }
2907
+ break;
2908
+ }
2909
+ case $.SCRIPT:
2910
+ case $.TEMPLATE: {
2911
+ startTagInHead(p, token);
2912
+ break;
2913
+ }
2914
+ default:
2915
+ // Do nothing
2916
+ }
2917
+ }
2918
+ function endTagInSelect(p, token) {
2919
+ switch (token.tagID) {
2920
+ case $.OPTGROUP: {
2921
+ if (p.openElements.stackTop > 0 &&
2922
+ p.openElements.currentTagId === $.OPTION &&
2923
+ p.openElements.tagIDs[p.openElements.stackTop - 1] === $.OPTGROUP) {
2924
+ p.openElements.pop();
2925
+ }
2926
+ if (p.openElements.currentTagId === $.OPTGROUP) {
2927
+ p.openElements.pop();
2928
+ }
2929
+ break;
2930
+ }
2931
+ case $.OPTION: {
2932
+ if (p.openElements.currentTagId === $.OPTION) {
2933
+ p.openElements.pop();
2934
+ }
2935
+ break;
2936
+ }
2937
+ case $.SELECT: {
2938
+ if (p.openElements.hasInSelectScope($.SELECT)) {
2939
+ p.openElements.popUntilTagNamePopped($.SELECT);
2940
+ p._resetInsertionMode();
2941
+ }
2942
+ break;
2943
+ }
2944
+ case $.TEMPLATE: {
2945
+ templateEndTagInHead(p, token);
2946
+ break;
2947
+ }
2948
+ default:
2949
+ // Do nothing
2950
+ }
2951
+ }
2952
+ // The "in select in table" insertion mode
2953
+ //------------------------------------------------------------------
2954
+ function startTagInSelectInTable(p, token) {
2955
+ const tn = token.tagID;
2956
+ if (tn === $.CAPTION ||
2957
+ tn === $.TABLE ||
2958
+ tn === $.TBODY ||
2959
+ tn === $.TFOOT ||
2960
+ tn === $.THEAD ||
2961
+ tn === $.TR ||
2962
+ tn === $.TD ||
2963
+ tn === $.TH) {
2964
+ p.openElements.popUntilTagNamePopped($.SELECT);
2965
+ p._resetInsertionMode();
2966
+ p._processStartTag(token);
2967
+ }
2968
+ else {
2969
+ startTagInSelect(p, token);
2970
+ }
2971
+ }
2972
+ function endTagInSelectInTable(p, token) {
2973
+ const tn = token.tagID;
2974
+ if (tn === $.CAPTION ||
2975
+ tn === $.TABLE ||
2976
+ tn === $.TBODY ||
2977
+ tn === $.TFOOT ||
2978
+ tn === $.THEAD ||
2979
+ tn === $.TR ||
2980
+ tn === $.TD ||
2981
+ tn === $.TH) {
2982
+ if (p.openElements.hasInTableScope(tn)) {
2983
+ p.openElements.popUntilTagNamePopped($.SELECT);
2984
+ p._resetInsertionMode();
2985
+ p.onEndTag(token);
2986
+ }
2987
+ }
2988
+ else {
2989
+ endTagInSelect(p, token);
2990
+ }
2991
+ }
2992
+ // The "in template" insertion mode
2993
+ //------------------------------------------------------------------
2994
+ function startTagInTemplate(p, token) {
2995
+ switch (token.tagID) {
2996
+ // First, handle tags that can start without a mode change
2997
+ case $.BASE:
2998
+ case $.BASEFONT:
2999
+ case $.BGSOUND:
3000
+ case $.LINK:
3001
+ case $.META:
3002
+ case $.NOFRAMES:
3003
+ case $.SCRIPT:
3004
+ case $.STYLE:
3005
+ case $.TEMPLATE:
3006
+ case $.TITLE: {
3007
+ startTagInHead(p, token);
3008
+ break;
3009
+ }
3010
+ // Re-process the token in the appropriate mode
3011
+ case $.CAPTION:
3012
+ case $.COLGROUP:
3013
+ case $.TBODY:
3014
+ case $.TFOOT:
3015
+ case $.THEAD: {
3016
+ p.tmplInsertionModeStack[0] = InsertionMode.IN_TABLE;
3017
+ p.insertionMode = InsertionMode.IN_TABLE;
3018
+ startTagInTable(p, token);
3019
+ break;
3020
+ }
3021
+ case $.COL: {
3022
+ p.tmplInsertionModeStack[0] = InsertionMode.IN_COLUMN_GROUP;
3023
+ p.insertionMode = InsertionMode.IN_COLUMN_GROUP;
3024
+ startTagInColumnGroup(p, token);
3025
+ break;
3026
+ }
3027
+ case $.TR: {
3028
+ p.tmplInsertionModeStack[0] = InsertionMode.IN_TABLE_BODY;
3029
+ p.insertionMode = InsertionMode.IN_TABLE_BODY;
3030
+ startTagInTableBody(p, token);
3031
+ break;
3032
+ }
3033
+ case $.TD:
3034
+ case $.TH: {
3035
+ p.tmplInsertionModeStack[0] = InsertionMode.IN_ROW;
3036
+ p.insertionMode = InsertionMode.IN_ROW;
3037
+ startTagInRow(p, token);
3038
+ break;
3039
+ }
3040
+ default: {
3041
+ p.tmplInsertionModeStack[0] = InsertionMode.IN_BODY;
3042
+ p.insertionMode = InsertionMode.IN_BODY;
3043
+ startTagInBody(p, token);
3044
+ }
3045
+ }
3046
+ }
3047
+ function endTagInTemplate(p, token) {
3048
+ if (token.tagID === $.TEMPLATE) {
3049
+ templateEndTagInHead(p, token);
3050
+ }
3051
+ }
3052
+ function eofInTemplate(p, token) {
3053
+ if (p.openElements.tmplCount > 0) {
3054
+ p.openElements.popUntilTagNamePopped($.TEMPLATE);
3055
+ p.activeFormattingElements.clearToLastMarker();
3056
+ p.tmplInsertionModeStack.shift();
3057
+ p._resetInsertionMode();
3058
+ p.onEof(token);
3059
+ }
3060
+ else {
3061
+ stopParsing(p, token);
3062
+ }
3063
+ }
3064
+ // The "after body" insertion mode
3065
+ //------------------------------------------------------------------
3066
+ function startTagAfterBody(p, token) {
3067
+ if (token.tagID === $.HTML) {
3068
+ startTagInBody(p, token);
3069
+ }
3070
+ else {
3071
+ tokenAfterBody(p, token);
3072
+ }
3073
+ }
3074
+ function endTagAfterBody(p, token) {
3075
+ var _a;
3076
+ if (token.tagID === $.HTML) {
3077
+ if (!p.fragmentContext) {
3078
+ p.insertionMode = InsertionMode.AFTER_AFTER_BODY;
3079
+ }
3080
+ //NOTE: <html> is never popped from the stack, so we need to updated
3081
+ //the end location explicitly.
3082
+ if (p.options.sourceCodeLocationInfo && p.openElements.tagIDs[0] === $.HTML) {
3083
+ p._setEndLocation(p.openElements.items[0], token);
3084
+ // Update the body element, if it doesn't have an end tag
3085
+ const bodyElement = p.openElements.items[1];
3086
+ if (bodyElement && !((_a = p.treeAdapter.getNodeSourceCodeLocation(bodyElement)) === null || _a === void 0 ? void 0 : _a.endTag)) {
3087
+ p._setEndLocation(bodyElement, token);
3088
+ }
3089
+ }
3090
+ }
3091
+ else {
3092
+ tokenAfterBody(p, token);
3093
+ }
3094
+ }
3095
+ function tokenAfterBody(p, token) {
3096
+ p.insertionMode = InsertionMode.IN_BODY;
3097
+ modeInBody(p, token);
3098
+ }
3099
+ // The "in frameset" insertion mode
3100
+ //------------------------------------------------------------------
3101
+ function startTagInFrameset(p, token) {
3102
+ switch (token.tagID) {
3103
+ case $.HTML: {
3104
+ startTagInBody(p, token);
3105
+ break;
3106
+ }
3107
+ case $.FRAMESET: {
3108
+ p._insertElement(token, NS.HTML);
3109
+ break;
3110
+ }
3111
+ case $.FRAME: {
3112
+ p._appendElement(token, NS.HTML);
3113
+ token.ackSelfClosing = true;
3114
+ break;
3115
+ }
3116
+ case $.NOFRAMES: {
3117
+ startTagInHead(p, token);
3118
+ break;
3119
+ }
3120
+ default:
3121
+ // Do nothing
3122
+ }
3123
+ }
3124
+ function endTagInFrameset(p, token) {
3125
+ if (token.tagID === $.FRAMESET && !p.openElements.isRootHtmlElementCurrent()) {
3126
+ p.openElements.pop();
3127
+ if (!p.fragmentContext && p.openElements.currentTagId !== $.FRAMESET) {
3128
+ p.insertionMode = InsertionMode.AFTER_FRAMESET;
3129
+ }
3130
+ }
3131
+ }
3132
+ // The "after frameset" insertion mode
3133
+ //------------------------------------------------------------------
3134
+ function startTagAfterFrameset(p, token) {
3135
+ switch (token.tagID) {
3136
+ case $.HTML: {
3137
+ startTagInBody(p, token);
3138
+ break;
3139
+ }
3140
+ case $.NOFRAMES: {
3141
+ startTagInHead(p, token);
3142
+ break;
3143
+ }
3144
+ default:
3145
+ // Do nothing
3146
+ }
3147
+ }
3148
+ function endTagAfterFrameset(p, token) {
3149
+ if (token.tagID === $.HTML) {
3150
+ p.insertionMode = InsertionMode.AFTER_AFTER_FRAMESET;
3151
+ }
3152
+ }
3153
+ // The "after after body" insertion mode
3154
+ //------------------------------------------------------------------
3155
+ function startTagAfterAfterBody(p, token) {
3156
+ if (token.tagID === $.HTML) {
3157
+ startTagInBody(p, token);
3158
+ }
3159
+ else {
3160
+ tokenAfterAfterBody(p, token);
3161
+ }
3162
+ }
3163
+ function tokenAfterAfterBody(p, token) {
3164
+ p.insertionMode = InsertionMode.IN_BODY;
3165
+ modeInBody(p, token);
3166
+ }
3167
+ // The "after after frameset" insertion mode
3168
+ //------------------------------------------------------------------
3169
+ function startTagAfterAfterFrameset(p, token) {
3170
+ switch (token.tagID) {
3171
+ case $.HTML: {
3172
+ startTagInBody(p, token);
3173
+ break;
3174
+ }
3175
+ case $.NOFRAMES: {
3176
+ startTagInHead(p, token);
3177
+ break;
3178
+ }
3179
+ default:
3180
+ // Do nothing
3181
+ }
3182
+ }
3183
+ // The rules for parsing tokens in foreign content
3184
+ //------------------------------------------------------------------
3185
+ function nullCharacterInForeignContent(p, token) {
3186
+ token.chars = unicode.REPLACEMENT_CHARACTER;
3187
+ p._insertCharacters(token);
3188
+ }
3189
+ function characterInForeignContent(p, token) {
3190
+ p._insertCharacters(token);
3191
+ p.framesetOk = false;
3192
+ }
3193
+ function popUntilHtmlOrIntegrationPoint(p) {
3194
+ while (p.treeAdapter.getNamespaceURI(p.openElements.current) !== NS.HTML &&
3195
+ p.openElements.currentTagId !== undefined &&
3196
+ !p._isIntegrationPoint(p.openElements.currentTagId, p.openElements.current)) {
3197
+ p.openElements.pop();
3198
+ }
3199
+ }
3200
+ function startTagInForeignContent(p, token) {
3201
+ if (foreignContent.causesExit(token)) {
3202
+ popUntilHtmlOrIntegrationPoint(p);
3203
+ p._startTagOutsideForeignContent(token);
3204
+ }
3205
+ else {
3206
+ const current = p._getAdjustedCurrentElement();
3207
+ const currentNs = p.treeAdapter.getNamespaceURI(current);
3208
+ if (currentNs === NS.MATHML) {
3209
+ foreignContent.adjustTokenMathMLAttrs(token);
3210
+ }
3211
+ else if (currentNs === NS.SVG) {
3212
+ foreignContent.adjustTokenSVGTagName(token);
3213
+ foreignContent.adjustTokenSVGAttrs(token);
3214
+ }
3215
+ foreignContent.adjustTokenXMLAttrs(token);
3216
+ if (token.selfClosing) {
3217
+ p._appendElement(token, currentNs);
3218
+ }
3219
+ else {
3220
+ p._insertElement(token, currentNs);
3221
+ }
3222
+ token.ackSelfClosing = true;
3223
+ }
3224
+ }
3225
+ function endTagInForeignContent(p, token) {
3226
+ if (token.tagID === $.P || token.tagID === $.BR) {
3227
+ popUntilHtmlOrIntegrationPoint(p);
3228
+ p._endTagOutsideForeignContent(token);
3229
+ return;
3230
+ }
3231
+ for (let i = p.openElements.stackTop; i > 0; i--) {
3232
+ const element = p.openElements.items[i];
3233
+ if (p.treeAdapter.getNamespaceURI(element) === NS.HTML) {
3234
+ p._endTagOutsideForeignContent(token);
3235
+ break;
3236
+ }
3237
+ const tagName = p.treeAdapter.getTagName(element);
3238
+ if (tagName.toLowerCase() === token.tagName) {
3239
+ //NOTE: update the token tag name for `_setEndLocation`.
3240
+ token.tagName = tagName;
3241
+ p.openElements.shortenToLength(i);
3242
+ break;
3243
+ }
3244
+ }
3245
+ }