index.js 112 KB

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