parse5 6.0.1 → 7.0.0

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