index.js 104 KB

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