12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245 |
- import { Tokenizer, TokenizerMode } from '../tokenizer/index.js';
- import { OpenElementStack } from './open-element-stack.js';
- import { FormattingElementList, EntryType } from './formatting-element-list.js';
- import { defaultTreeAdapter } from '../tree-adapters/default.js';
- import * as doctype from '../common/doctype.js';
- import * as foreignContent from '../common/foreign-content.js';
- import { ERR } from '../common/error-codes.js';
- import * as unicode from '../common/unicode.js';
- import { TAG_ID as $, TAG_NAMES as TN, NS, ATTRS, SPECIAL_ELEMENTS, DOCUMENT_MODE, NUMBERED_HEADERS, getTagID, } from '../common/html.js';
- import { TokenType, getTokenAttr, } from '../common/token.js';
- //Misc constants
- const HIDDEN_INPUT_TYPE = 'hidden';
- //Adoption agency loops iteration count
- const AA_OUTER_LOOP_ITER = 8;
- const AA_INNER_LOOP_ITER = 3;
- //Insertion modes
- var InsertionMode;
- (function (InsertionMode) {
- InsertionMode[InsertionMode["INITIAL"] = 0] = "INITIAL";
- InsertionMode[InsertionMode["BEFORE_HTML"] = 1] = "BEFORE_HTML";
- InsertionMode[InsertionMode["BEFORE_HEAD"] = 2] = "BEFORE_HEAD";
- InsertionMode[InsertionMode["IN_HEAD"] = 3] = "IN_HEAD";
- InsertionMode[InsertionMode["IN_HEAD_NO_SCRIPT"] = 4] = "IN_HEAD_NO_SCRIPT";
- InsertionMode[InsertionMode["AFTER_HEAD"] = 5] = "AFTER_HEAD";
- InsertionMode[InsertionMode["IN_BODY"] = 6] = "IN_BODY";
- InsertionMode[InsertionMode["TEXT"] = 7] = "TEXT";
- InsertionMode[InsertionMode["IN_TABLE"] = 8] = "IN_TABLE";
- InsertionMode[InsertionMode["IN_TABLE_TEXT"] = 9] = "IN_TABLE_TEXT";
- InsertionMode[InsertionMode["IN_CAPTION"] = 10] = "IN_CAPTION";
- InsertionMode[InsertionMode["IN_COLUMN_GROUP"] = 11] = "IN_COLUMN_GROUP";
- InsertionMode[InsertionMode["IN_TABLE_BODY"] = 12] = "IN_TABLE_BODY";
- InsertionMode[InsertionMode["IN_ROW"] = 13] = "IN_ROW";
- InsertionMode[InsertionMode["IN_CELL"] = 14] = "IN_CELL";
- InsertionMode[InsertionMode["IN_SELECT"] = 15] = "IN_SELECT";
- InsertionMode[InsertionMode["IN_SELECT_IN_TABLE"] = 16] = "IN_SELECT_IN_TABLE";
- InsertionMode[InsertionMode["IN_TEMPLATE"] = 17] = "IN_TEMPLATE";
- InsertionMode[InsertionMode["AFTER_BODY"] = 18] = "AFTER_BODY";
- InsertionMode[InsertionMode["IN_FRAMESET"] = 19] = "IN_FRAMESET";
- InsertionMode[InsertionMode["AFTER_FRAMESET"] = 20] = "AFTER_FRAMESET";
- InsertionMode[InsertionMode["AFTER_AFTER_BODY"] = 21] = "AFTER_AFTER_BODY";
- InsertionMode[InsertionMode["AFTER_AFTER_FRAMESET"] = 22] = "AFTER_AFTER_FRAMESET";
- })(InsertionMode || (InsertionMode = {}));
- const BASE_LOC = {
- startLine: -1,
- startCol: -1,
- startOffset: -1,
- endLine: -1,
- endCol: -1,
- endOffset: -1,
- };
- const TABLE_STRUCTURE_TAGS = new Set([$.TABLE, $.TBODY, $.TFOOT, $.THEAD, $.TR]);
- const defaultParserOptions = {
- scriptingEnabled: true,
- sourceCodeLocationInfo: false,
- treeAdapter: defaultTreeAdapter,
- onParseError: null,
- };
- //Parser
- export class Parser {
- constructor(options, document,
- /** @internal */
- fragmentContext = null,
- /** @internal */
- scriptHandler = null) {
- this.fragmentContext = fragmentContext;
- this.scriptHandler = scriptHandler;
- this.currentToken = null;
- this.stopped = false;
- /** @internal */
- this.insertionMode = InsertionMode.INITIAL;
- /** @internal */
- this.originalInsertionMode = InsertionMode.INITIAL;
- /** @internal */
- this.headElement = null;
- /** @internal */
- this.formElement = null;
- /** Indicates that the current node is not an element in the HTML namespace */
- this.currentNotInHTML = false;
- /**
- * The template insertion mode stack is maintained from the left.
- * Ie. the topmost element will always have index 0.
- *
- * @internal
- */
- this.tmplInsertionModeStack = [];
- /** @internal */
- this.pendingCharacterTokens = [];
- /** @internal */
- this.hasNonWhitespacePendingCharacterToken = false;
- /** @internal */
- this.framesetOk = true;
- /** @internal */
- this.skipNextNewLine = false;
- /** @internal */
- this.fosterParentingEnabled = false;
- this.options = {
- ...defaultParserOptions,
- ...options,
- };
- this.treeAdapter = this.options.treeAdapter;
- this.onParseError = this.options.onParseError;
- // Always enable location info if we report parse errors.
- if (this.onParseError) {
- this.options.sourceCodeLocationInfo = true;
- }
- this.document = document !== null && document !== void 0 ? document : this.treeAdapter.createDocument();
- this.tokenizer = new Tokenizer(this.options, this);
- this.activeFormattingElements = new FormattingElementList(this.treeAdapter);
- this.fragmentContextID = fragmentContext ? getTagID(this.treeAdapter.getTagName(fragmentContext)) : $.UNKNOWN;
- this._setContextModes(fragmentContext !== null && fragmentContext !== void 0 ? fragmentContext : this.document, this.fragmentContextID);
- this.openElements = new OpenElementStack(this.document, this.treeAdapter, this);
- }
- // API
- static parse(html, options) {
- const parser = new this(options);
- parser.tokenizer.write(html, true);
- return parser.document;
- }
- static getFragmentParser(fragmentContext, options) {
- const opts = {
- ...defaultParserOptions,
- ...options,
- };
- //NOTE: use a <template> element as the fragment context if no context element was provided,
- //so we will parse in a "forgiving" manner
- fragmentContext !== null && fragmentContext !== void 0 ? fragmentContext : (fragmentContext = opts.treeAdapter.createElement(TN.TEMPLATE, NS.HTML, []));
- //NOTE: create a fake element which will be used as the `document` for fragment parsing.
- //This is important for jsdom, where a new `document` cannot be created. This led to
- //fragment parsing messing with the main `document`.
- const documentMock = opts.treeAdapter.createElement('documentmock', NS.HTML, []);
- const parser = new this(opts, documentMock, fragmentContext);
- if (parser.fragmentContextID === $.TEMPLATE) {
- parser.tmplInsertionModeStack.unshift(InsertionMode.IN_TEMPLATE);
- }
- parser._initTokenizerForFragmentParsing();
- parser._insertFakeRootElement();
- parser._resetInsertionMode();
- parser._findFormInFragmentContext();
- return parser;
- }
- getFragment() {
- const rootElement = this.treeAdapter.getFirstChild(this.document);
- const fragment = this.treeAdapter.createDocumentFragment();
- this._adoptNodes(rootElement, fragment);
- return fragment;
- }
- //Errors
- /** @internal */
- _err(token, code, beforeToken) {
- var _a;
- if (!this.onParseError)
- return;
- const loc = (_a = token.location) !== null && _a !== void 0 ? _a : BASE_LOC;
- const err = {
- code,
- startLine: loc.startLine,
- startCol: loc.startCol,
- startOffset: loc.startOffset,
- endLine: beforeToken ? loc.startLine : loc.endLine,
- endCol: beforeToken ? loc.startCol : loc.endCol,
- endOffset: beforeToken ? loc.startOffset : loc.endOffset,
- };
- this.onParseError(err);
- }
- //Stack events
- /** @internal */
- onItemPush(node, tid, isTop) {
- var _a, _b;
- (_b = (_a = this.treeAdapter).onItemPush) === null || _b === void 0 ? void 0 : _b.call(_a, node);
- if (isTop && this.openElements.stackTop > 0)
- this._setContextModes(node, tid);
- }
- /** @internal */
- onItemPop(node, isTop) {
- var _a, _b;
- if (this.options.sourceCodeLocationInfo) {
- this._setEndLocation(node, this.currentToken);
- }
- (_b = (_a = this.treeAdapter).onItemPop) === null || _b === void 0 ? void 0 : _b.call(_a, node, this.openElements.current);
- if (isTop) {
- let current;
- let currentTagId;
- if (this.openElements.stackTop === 0 && this.fragmentContext) {
- current = this.fragmentContext;
- currentTagId = this.fragmentContextID;
- }
- else {
- ({ current, currentTagId } = this.openElements);
- }
- this._setContextModes(current, currentTagId);
- }
- }
- _setContextModes(current, tid) {
- const isHTML = current === this.document || (current && this.treeAdapter.getNamespaceURI(current) === NS.HTML);
- this.currentNotInHTML = !isHTML;
- this.tokenizer.inForeignNode =
- !isHTML && current !== undefined && tid !== undefined && !this._isIntegrationPoint(tid, current);
- }
- /** @protected */
- _switchToTextParsing(currentToken, nextTokenizerState) {
- this._insertElement(currentToken, NS.HTML);
- this.tokenizer.state = nextTokenizerState;
- this.originalInsertionMode = this.insertionMode;
- this.insertionMode = InsertionMode.TEXT;
- }
- switchToPlaintextParsing() {
- this.insertionMode = InsertionMode.TEXT;
- this.originalInsertionMode = InsertionMode.IN_BODY;
- this.tokenizer.state = TokenizerMode.PLAINTEXT;
- }
- //Fragment parsing
- /** @protected */
- _getAdjustedCurrentElement() {
- return this.openElements.stackTop === 0 && this.fragmentContext
- ? this.fragmentContext
- : this.openElements.current;
- }
- /** @protected */
- _findFormInFragmentContext() {
- let node = this.fragmentContext;
- while (node) {
- if (this.treeAdapter.getTagName(node) === TN.FORM) {
- this.formElement = node;
- break;
- }
- node = this.treeAdapter.getParentNode(node);
- }
- }
- _initTokenizerForFragmentParsing() {
- if (!this.fragmentContext || this.treeAdapter.getNamespaceURI(this.fragmentContext) !== NS.HTML) {
- return;
- }
- switch (this.fragmentContextID) {
- case $.TITLE:
- case $.TEXTAREA: {
- this.tokenizer.state = TokenizerMode.RCDATA;
- break;
- }
- case $.STYLE:
- case $.XMP:
- case $.IFRAME:
- case $.NOEMBED:
- case $.NOFRAMES:
- case $.NOSCRIPT: {
- this.tokenizer.state = TokenizerMode.RAWTEXT;
- break;
- }
- case $.SCRIPT: {
- this.tokenizer.state = TokenizerMode.SCRIPT_DATA;
- break;
- }
- case $.PLAINTEXT: {
- this.tokenizer.state = TokenizerMode.PLAINTEXT;
- break;
- }
- default:
- // Do nothing
- }
- }
- //Tree mutation
- /** @protected */
- _setDocumentType(token) {
- const name = token.name || '';
- const publicId = token.publicId || '';
- const systemId = token.systemId || '';
- this.treeAdapter.setDocumentType(this.document, name, publicId, systemId);
- if (token.location) {
- const documentChildren = this.treeAdapter.getChildNodes(this.document);
- const docTypeNode = documentChildren.find((node) => this.treeAdapter.isDocumentTypeNode(node));
- if (docTypeNode) {
- this.treeAdapter.setNodeSourceCodeLocation(docTypeNode, token.location);
- }
- }
- }
- /** @protected */
- _attachElementToTree(element, location) {
- if (this.options.sourceCodeLocationInfo) {
- const loc = location && {
- ...location,
- startTag: location,
- };
- this.treeAdapter.setNodeSourceCodeLocation(element, loc);
- }
- if (this._shouldFosterParentOnInsertion()) {
- this._fosterParentElement(element);
- }
- else {
- const parent = this.openElements.currentTmplContentOrNode;
- this.treeAdapter.appendChild(parent !== null && parent !== void 0 ? parent : this.document, element);
- }
- }
- /**
- * For self-closing tags. Add an element to the tree, but skip adding it
- * to the stack.
- */
- /** @protected */
- _appendElement(token, namespaceURI) {
- const element = this.treeAdapter.createElement(token.tagName, namespaceURI, token.attrs);
- this._attachElementToTree(element, token.location);
- }
- /** @protected */
- _insertElement(token, namespaceURI) {
- const element = this.treeAdapter.createElement(token.tagName, namespaceURI, token.attrs);
- this._attachElementToTree(element, token.location);
- this.openElements.push(element, token.tagID);
- }
- /** @protected */
- _insertFakeElement(tagName, tagID) {
- const element = this.treeAdapter.createElement(tagName, NS.HTML, []);
- this._attachElementToTree(element, null);
- this.openElements.push(element, tagID);
- }
- /** @protected */
- _insertTemplate(token) {
- const tmpl = this.treeAdapter.createElement(token.tagName, NS.HTML, token.attrs);
- const content = this.treeAdapter.createDocumentFragment();
- this.treeAdapter.setTemplateContent(tmpl, content);
- this._attachElementToTree(tmpl, token.location);
- this.openElements.push(tmpl, token.tagID);
- if (this.options.sourceCodeLocationInfo)
- this.treeAdapter.setNodeSourceCodeLocation(content, null);
- }
- /** @protected */
- _insertFakeRootElement() {
- const element = this.treeAdapter.createElement(TN.HTML, NS.HTML, []);
- if (this.options.sourceCodeLocationInfo)
- this.treeAdapter.setNodeSourceCodeLocation(element, null);
- this.treeAdapter.appendChild(this.openElements.current, element);
- this.openElements.push(element, $.HTML);
- }
- /** @protected */
- _appendCommentNode(token, parent) {
- const commentNode = this.treeAdapter.createCommentNode(token.data);
- this.treeAdapter.appendChild(parent, commentNode);
- if (this.options.sourceCodeLocationInfo) {
- this.treeAdapter.setNodeSourceCodeLocation(commentNode, token.location);
- }
- }
- /** @protected */
- _insertCharacters(token) {
- let parent;
- let beforeElement;
- if (this._shouldFosterParentOnInsertion()) {
- ({ parent, beforeElement } = this._findFosterParentingLocation());
- if (beforeElement) {
- this.treeAdapter.insertTextBefore(parent, token.chars, beforeElement);
- }
- else {
- this.treeAdapter.insertText(parent, token.chars);
- }
- }
- else {
- parent = this.openElements.currentTmplContentOrNode;
- this.treeAdapter.insertText(parent, token.chars);
- }
- if (!token.location)
- return;
- const siblings = this.treeAdapter.getChildNodes(parent);
- const textNodeIdx = beforeElement ? siblings.lastIndexOf(beforeElement) : siblings.length;
- const textNode = siblings[textNodeIdx - 1];
- //NOTE: if we have a location assigned by another token, then just update the end position
- const tnLoc = this.treeAdapter.getNodeSourceCodeLocation(textNode);
- if (tnLoc) {
- const { endLine, endCol, endOffset } = token.location;
- this.treeAdapter.updateNodeSourceCodeLocation(textNode, { endLine, endCol, endOffset });
- }
- else if (this.options.sourceCodeLocationInfo) {
- this.treeAdapter.setNodeSourceCodeLocation(textNode, token.location);
- }
- }
- /** @protected */
- _adoptNodes(donor, recipient) {
- for (let child = this.treeAdapter.getFirstChild(donor); child; child = this.treeAdapter.getFirstChild(donor)) {
- this.treeAdapter.detachNode(child);
- this.treeAdapter.appendChild(recipient, child);
- }
- }
- /** @protected */
- _setEndLocation(element, closingToken) {
- if (this.treeAdapter.getNodeSourceCodeLocation(element) && closingToken.location) {
- const ctLoc = closingToken.location;
- const tn = this.treeAdapter.getTagName(element);
- const endLoc =
- // NOTE: For cases like <p> <p> </p> - First 'p' closes without a closing
- // tag and for cases like <td> <p> </td> - 'p' closes without a closing tag.
- closingToken.type === TokenType.END_TAG && tn === closingToken.tagName
- ? {
- endTag: { ...ctLoc },
- endLine: ctLoc.endLine,
- endCol: ctLoc.endCol,
- endOffset: ctLoc.endOffset,
- }
- : {
- endLine: ctLoc.startLine,
- endCol: ctLoc.startCol,
- endOffset: ctLoc.startOffset,
- };
- this.treeAdapter.updateNodeSourceCodeLocation(element, endLoc);
- }
- }
- //Token processing
- shouldProcessStartTagTokenInForeignContent(token) {
- // Check that neither current === document, or ns === NS.HTML
- if (!this.currentNotInHTML)
- return false;
- let current;
- let currentTagId;
- if (this.openElements.stackTop === 0 && this.fragmentContext) {
- current = this.fragmentContext;
- currentTagId = this.fragmentContextID;
- }
- else {
- ({ current, currentTagId } = this.openElements);
- }
- if (token.tagID === $.SVG &&
- this.treeAdapter.getTagName(current) === TN.ANNOTATION_XML &&
- this.treeAdapter.getNamespaceURI(current) === NS.MATHML) {
- return false;
- }
- return (
- // Check that `current` is not an integration point for HTML or MathML elements.
- this.tokenizer.inForeignNode ||
- // If it _is_ an integration point, then we might have to check that it is not an HTML
- // integration point.
- ((token.tagID === $.MGLYPH || token.tagID === $.MALIGNMARK) &&
- currentTagId !== undefined &&
- !this._isIntegrationPoint(currentTagId, current, NS.HTML)));
- }
- /** @protected */
- _processToken(token) {
- switch (token.type) {
- case TokenType.CHARACTER: {
- this.onCharacter(token);
- break;
- }
- case TokenType.NULL_CHARACTER: {
- this.onNullCharacter(token);
- break;
- }
- case TokenType.COMMENT: {
- this.onComment(token);
- break;
- }
- case TokenType.DOCTYPE: {
- this.onDoctype(token);
- break;
- }
- case TokenType.START_TAG: {
- this._processStartTag(token);
- break;
- }
- case TokenType.END_TAG: {
- this.onEndTag(token);
- break;
- }
- case TokenType.EOF: {
- this.onEof(token);
- break;
- }
- case TokenType.WHITESPACE_CHARACTER: {
- this.onWhitespaceCharacter(token);
- break;
- }
- }
- }
- //Integration points
- /** @protected */
- _isIntegrationPoint(tid, element, foreignNS) {
- const ns = this.treeAdapter.getNamespaceURI(element);
- const attrs = this.treeAdapter.getAttrList(element);
- return foreignContent.isIntegrationPoint(tid, ns, attrs, foreignNS);
- }
- //Active formatting elements reconstruction
- /** @protected */
- _reconstructActiveFormattingElements() {
- const listLength = this.activeFormattingElements.entries.length;
- if (listLength) {
- const endIndex = this.activeFormattingElements.entries.findIndex((entry) => entry.type === EntryType.Marker || this.openElements.contains(entry.element));
- const unopenIdx = endIndex === -1 ? listLength - 1 : endIndex - 1;
- for (let i = unopenIdx; i >= 0; i--) {
- const entry = this.activeFormattingElements.entries[i];
- this._insertElement(entry.token, this.treeAdapter.getNamespaceURI(entry.element));
- entry.element = this.openElements.current;
- }
- }
- }
- //Close elements
- /** @protected */
- _closeTableCell() {
- this.openElements.generateImpliedEndTags();
- this.openElements.popUntilTableCellPopped();
- this.activeFormattingElements.clearToLastMarker();
- this.insertionMode = InsertionMode.IN_ROW;
- }
- /** @protected */
- _closePElement() {
- this.openElements.generateImpliedEndTagsWithExclusion($.P);
- this.openElements.popUntilTagNamePopped($.P);
- }
- //Insertion modes
- /** @protected */
- _resetInsertionMode() {
- for (let i = this.openElements.stackTop; i >= 0; i--) {
- //Insertion mode reset map
- switch (i === 0 && this.fragmentContext ? this.fragmentContextID : this.openElements.tagIDs[i]) {
- case $.TR: {
- this.insertionMode = InsertionMode.IN_ROW;
- return;
- }
- case $.TBODY:
- case $.THEAD:
- case $.TFOOT: {
- this.insertionMode = InsertionMode.IN_TABLE_BODY;
- return;
- }
- case $.CAPTION: {
- this.insertionMode = InsertionMode.IN_CAPTION;
- return;
- }
- case $.COLGROUP: {
- this.insertionMode = InsertionMode.IN_COLUMN_GROUP;
- return;
- }
- case $.TABLE: {
- this.insertionMode = InsertionMode.IN_TABLE;
- return;
- }
- case $.BODY: {
- this.insertionMode = InsertionMode.IN_BODY;
- return;
- }
- case $.FRAMESET: {
- this.insertionMode = InsertionMode.IN_FRAMESET;
- return;
- }
- case $.SELECT: {
- this._resetInsertionModeForSelect(i);
- return;
- }
- case $.TEMPLATE: {
- this.insertionMode = this.tmplInsertionModeStack[0];
- return;
- }
- case $.HTML: {
- this.insertionMode = this.headElement ? InsertionMode.AFTER_HEAD : InsertionMode.BEFORE_HEAD;
- return;
- }
- case $.TD:
- case $.TH: {
- if (i > 0) {
- this.insertionMode = InsertionMode.IN_CELL;
- return;
- }
- break;
- }
- case $.HEAD: {
- if (i > 0) {
- this.insertionMode = InsertionMode.IN_HEAD;
- return;
- }
- break;
- }
- }
- }
- this.insertionMode = InsertionMode.IN_BODY;
- }
- /** @protected */
- _resetInsertionModeForSelect(selectIdx) {
- if (selectIdx > 0) {
- for (let i = selectIdx - 1; i > 0; i--) {
- const tn = this.openElements.tagIDs[i];
- if (tn === $.TEMPLATE) {
- break;
- }
- else if (tn === $.TABLE) {
- this.insertionMode = InsertionMode.IN_SELECT_IN_TABLE;
- return;
- }
- }
- }
- this.insertionMode = InsertionMode.IN_SELECT;
- }
- //Foster parenting
- /** @protected */
- _isElementCausesFosterParenting(tn) {
- return TABLE_STRUCTURE_TAGS.has(tn);
- }
- /** @protected */
- _shouldFosterParentOnInsertion() {
- return (this.fosterParentingEnabled &&
- this.openElements.currentTagId !== undefined &&
- this._isElementCausesFosterParenting(this.openElements.currentTagId));
- }
- /** @protected */
- _findFosterParentingLocation() {
- for (let i = this.openElements.stackTop; i >= 0; i--) {
- const openElement = this.openElements.items[i];
- switch (this.openElements.tagIDs[i]) {
- case $.TEMPLATE: {
- if (this.treeAdapter.getNamespaceURI(openElement) === NS.HTML) {
- return { parent: this.treeAdapter.getTemplateContent(openElement), beforeElement: null };
- }
- break;
- }
- case $.TABLE: {
- const parent = this.treeAdapter.getParentNode(openElement);
- if (parent) {
- return { parent, beforeElement: openElement };
- }
- return { parent: this.openElements.items[i - 1], beforeElement: null };
- }
- default:
- // Do nothing
- }
- }
- return { parent: this.openElements.items[0], beforeElement: null };
- }
- /** @protected */
- _fosterParentElement(element) {
- const location = this._findFosterParentingLocation();
- if (location.beforeElement) {
- this.treeAdapter.insertBefore(location.parent, element, location.beforeElement);
- }
- else {
- this.treeAdapter.appendChild(location.parent, element);
- }
- }
- //Special elements
- /** @protected */
- _isSpecialElement(element, id) {
- const ns = this.treeAdapter.getNamespaceURI(element);
- return SPECIAL_ELEMENTS[ns].has(id);
- }
- /** @internal */
- onCharacter(token) {
- this.skipNextNewLine = false;
- if (this.tokenizer.inForeignNode) {
- characterInForeignContent(this, token);
- return;
- }
- switch (this.insertionMode) {
- case InsertionMode.INITIAL: {
- tokenInInitialMode(this, token);
- break;
- }
- case InsertionMode.BEFORE_HTML: {
- tokenBeforeHtml(this, token);
- break;
- }
- case InsertionMode.BEFORE_HEAD: {
- tokenBeforeHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD: {
- tokenInHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD_NO_SCRIPT: {
- tokenInHeadNoScript(this, token);
- break;
- }
- case InsertionMode.AFTER_HEAD: {
- tokenAfterHead(this, token);
- break;
- }
- case InsertionMode.IN_BODY:
- case InsertionMode.IN_CAPTION:
- case InsertionMode.IN_CELL:
- case InsertionMode.IN_TEMPLATE: {
- characterInBody(this, token);
- break;
- }
- case InsertionMode.TEXT:
- case InsertionMode.IN_SELECT:
- case InsertionMode.IN_SELECT_IN_TABLE: {
- this._insertCharacters(token);
- break;
- }
- case InsertionMode.IN_TABLE:
- case InsertionMode.IN_TABLE_BODY:
- case InsertionMode.IN_ROW: {
- characterInTable(this, token);
- break;
- }
- case InsertionMode.IN_TABLE_TEXT: {
- characterInTableText(this, token);
- break;
- }
- case InsertionMode.IN_COLUMN_GROUP: {
- tokenInColumnGroup(this, token);
- break;
- }
- case InsertionMode.AFTER_BODY: {
- tokenAfterBody(this, token);
- break;
- }
- case InsertionMode.AFTER_AFTER_BODY: {
- tokenAfterAfterBody(this, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- /** @internal */
- onNullCharacter(token) {
- this.skipNextNewLine = false;
- if (this.tokenizer.inForeignNode) {
- nullCharacterInForeignContent(this, token);
- return;
- }
- switch (this.insertionMode) {
- case InsertionMode.INITIAL: {
- tokenInInitialMode(this, token);
- break;
- }
- case InsertionMode.BEFORE_HTML: {
- tokenBeforeHtml(this, token);
- break;
- }
- case InsertionMode.BEFORE_HEAD: {
- tokenBeforeHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD: {
- tokenInHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD_NO_SCRIPT: {
- tokenInHeadNoScript(this, token);
- break;
- }
- case InsertionMode.AFTER_HEAD: {
- tokenAfterHead(this, token);
- break;
- }
- case InsertionMode.TEXT: {
- this._insertCharacters(token);
- break;
- }
- case InsertionMode.IN_TABLE:
- case InsertionMode.IN_TABLE_BODY:
- case InsertionMode.IN_ROW: {
- characterInTable(this, token);
- break;
- }
- case InsertionMode.IN_COLUMN_GROUP: {
- tokenInColumnGroup(this, token);
- break;
- }
- case InsertionMode.AFTER_BODY: {
- tokenAfterBody(this, token);
- break;
- }
- case InsertionMode.AFTER_AFTER_BODY: {
- tokenAfterAfterBody(this, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- /** @internal */
- onComment(token) {
- this.skipNextNewLine = false;
- if (this.currentNotInHTML) {
- appendComment(this, token);
- return;
- }
- switch (this.insertionMode) {
- case InsertionMode.INITIAL:
- case InsertionMode.BEFORE_HTML:
- case InsertionMode.BEFORE_HEAD:
- case InsertionMode.IN_HEAD:
- case InsertionMode.IN_HEAD_NO_SCRIPT:
- case InsertionMode.AFTER_HEAD:
- case InsertionMode.IN_BODY:
- case InsertionMode.IN_TABLE:
- case InsertionMode.IN_CAPTION:
- case InsertionMode.IN_COLUMN_GROUP:
- case InsertionMode.IN_TABLE_BODY:
- case InsertionMode.IN_ROW:
- case InsertionMode.IN_CELL:
- case InsertionMode.IN_SELECT:
- case InsertionMode.IN_SELECT_IN_TABLE:
- case InsertionMode.IN_TEMPLATE:
- case InsertionMode.IN_FRAMESET:
- case InsertionMode.AFTER_FRAMESET: {
- appendComment(this, token);
- break;
- }
- case InsertionMode.IN_TABLE_TEXT: {
- tokenInTableText(this, token);
- break;
- }
- case InsertionMode.AFTER_BODY: {
- appendCommentToRootHtmlElement(this, token);
- break;
- }
- case InsertionMode.AFTER_AFTER_BODY:
- case InsertionMode.AFTER_AFTER_FRAMESET: {
- appendCommentToDocument(this, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- /** @internal */
- onDoctype(token) {
- this.skipNextNewLine = false;
- switch (this.insertionMode) {
- case InsertionMode.INITIAL: {
- doctypeInInitialMode(this, token);
- break;
- }
- case InsertionMode.BEFORE_HEAD:
- case InsertionMode.IN_HEAD:
- case InsertionMode.IN_HEAD_NO_SCRIPT:
- case InsertionMode.AFTER_HEAD: {
- this._err(token, ERR.misplacedDoctype);
- break;
- }
- case InsertionMode.IN_TABLE_TEXT: {
- tokenInTableText(this, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- /** @internal */
- onStartTag(token) {
- this.skipNextNewLine = false;
- this.currentToken = token;
- this._processStartTag(token);
- if (token.selfClosing && !token.ackSelfClosing) {
- this._err(token, ERR.nonVoidHtmlElementStartTagWithTrailingSolidus);
- }
- }
- /**
- * Processes a given start tag.
- *
- * `onStartTag` checks if a self-closing tag was recognized. When a token
- * is moved inbetween multiple insertion modes, this check for self-closing
- * could lead to false positives. To avoid this, `_processStartTag` is used
- * for nested calls.
- *
- * @param token The token to process.
- * @protected
- */
- _processStartTag(token) {
- if (this.shouldProcessStartTagTokenInForeignContent(token)) {
- startTagInForeignContent(this, token);
- }
- else {
- this._startTagOutsideForeignContent(token);
- }
- }
- /** @protected */
- _startTagOutsideForeignContent(token) {
- switch (this.insertionMode) {
- case InsertionMode.INITIAL: {
- tokenInInitialMode(this, token);
- break;
- }
- case InsertionMode.BEFORE_HTML: {
- startTagBeforeHtml(this, token);
- break;
- }
- case InsertionMode.BEFORE_HEAD: {
- startTagBeforeHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD: {
- startTagInHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD_NO_SCRIPT: {
- startTagInHeadNoScript(this, token);
- break;
- }
- case InsertionMode.AFTER_HEAD: {
- startTagAfterHead(this, token);
- break;
- }
- case InsertionMode.IN_BODY: {
- startTagInBody(this, token);
- break;
- }
- case InsertionMode.IN_TABLE: {
- startTagInTable(this, token);
- break;
- }
- case InsertionMode.IN_TABLE_TEXT: {
- tokenInTableText(this, token);
- break;
- }
- case InsertionMode.IN_CAPTION: {
- startTagInCaption(this, token);
- break;
- }
- case InsertionMode.IN_COLUMN_GROUP: {
- startTagInColumnGroup(this, token);
- break;
- }
- case InsertionMode.IN_TABLE_BODY: {
- startTagInTableBody(this, token);
- break;
- }
- case InsertionMode.IN_ROW: {
- startTagInRow(this, token);
- break;
- }
- case InsertionMode.IN_CELL: {
- startTagInCell(this, token);
- break;
- }
- case InsertionMode.IN_SELECT: {
- startTagInSelect(this, token);
- break;
- }
- case InsertionMode.IN_SELECT_IN_TABLE: {
- startTagInSelectInTable(this, token);
- break;
- }
- case InsertionMode.IN_TEMPLATE: {
- startTagInTemplate(this, token);
- break;
- }
- case InsertionMode.AFTER_BODY: {
- startTagAfterBody(this, token);
- break;
- }
- case InsertionMode.IN_FRAMESET: {
- startTagInFrameset(this, token);
- break;
- }
- case InsertionMode.AFTER_FRAMESET: {
- startTagAfterFrameset(this, token);
- break;
- }
- case InsertionMode.AFTER_AFTER_BODY: {
- startTagAfterAfterBody(this, token);
- break;
- }
- case InsertionMode.AFTER_AFTER_FRAMESET: {
- startTagAfterAfterFrameset(this, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- /** @internal */
- onEndTag(token) {
- this.skipNextNewLine = false;
- this.currentToken = token;
- if (this.currentNotInHTML) {
- endTagInForeignContent(this, token);
- }
- else {
- this._endTagOutsideForeignContent(token);
- }
- }
- /** @protected */
- _endTagOutsideForeignContent(token) {
- switch (this.insertionMode) {
- case InsertionMode.INITIAL: {
- tokenInInitialMode(this, token);
- break;
- }
- case InsertionMode.BEFORE_HTML: {
- endTagBeforeHtml(this, token);
- break;
- }
- case InsertionMode.BEFORE_HEAD: {
- endTagBeforeHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD: {
- endTagInHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD_NO_SCRIPT: {
- endTagInHeadNoScript(this, token);
- break;
- }
- case InsertionMode.AFTER_HEAD: {
- endTagAfterHead(this, token);
- break;
- }
- case InsertionMode.IN_BODY: {
- endTagInBody(this, token);
- break;
- }
- case InsertionMode.TEXT: {
- endTagInText(this, token);
- break;
- }
- case InsertionMode.IN_TABLE: {
- endTagInTable(this, token);
- break;
- }
- case InsertionMode.IN_TABLE_TEXT: {
- tokenInTableText(this, token);
- break;
- }
- case InsertionMode.IN_CAPTION: {
- endTagInCaption(this, token);
- break;
- }
- case InsertionMode.IN_COLUMN_GROUP: {
- endTagInColumnGroup(this, token);
- break;
- }
- case InsertionMode.IN_TABLE_BODY: {
- endTagInTableBody(this, token);
- break;
- }
- case InsertionMode.IN_ROW: {
- endTagInRow(this, token);
- break;
- }
- case InsertionMode.IN_CELL: {
- endTagInCell(this, token);
- break;
- }
- case InsertionMode.IN_SELECT: {
- endTagInSelect(this, token);
- break;
- }
- case InsertionMode.IN_SELECT_IN_TABLE: {
- endTagInSelectInTable(this, token);
- break;
- }
- case InsertionMode.IN_TEMPLATE: {
- endTagInTemplate(this, token);
- break;
- }
- case InsertionMode.AFTER_BODY: {
- endTagAfterBody(this, token);
- break;
- }
- case InsertionMode.IN_FRAMESET: {
- endTagInFrameset(this, token);
- break;
- }
- case InsertionMode.AFTER_FRAMESET: {
- endTagAfterFrameset(this, token);
- break;
- }
- case InsertionMode.AFTER_AFTER_BODY: {
- tokenAfterAfterBody(this, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- /** @internal */
- onEof(token) {
- switch (this.insertionMode) {
- case InsertionMode.INITIAL: {
- tokenInInitialMode(this, token);
- break;
- }
- case InsertionMode.BEFORE_HTML: {
- tokenBeforeHtml(this, token);
- break;
- }
- case InsertionMode.BEFORE_HEAD: {
- tokenBeforeHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD: {
- tokenInHead(this, token);
- break;
- }
- case InsertionMode.IN_HEAD_NO_SCRIPT: {
- tokenInHeadNoScript(this, token);
- break;
- }
- case InsertionMode.AFTER_HEAD: {
- tokenAfterHead(this, token);
- break;
- }
- case InsertionMode.IN_BODY:
- case InsertionMode.IN_TABLE:
- case InsertionMode.IN_CAPTION:
- case InsertionMode.IN_COLUMN_GROUP:
- case InsertionMode.IN_TABLE_BODY:
- case InsertionMode.IN_ROW:
- case InsertionMode.IN_CELL:
- case InsertionMode.IN_SELECT:
- case InsertionMode.IN_SELECT_IN_TABLE: {
- eofInBody(this, token);
- break;
- }
- case InsertionMode.TEXT: {
- eofInText(this, token);
- break;
- }
- case InsertionMode.IN_TABLE_TEXT: {
- tokenInTableText(this, token);
- break;
- }
- case InsertionMode.IN_TEMPLATE: {
- eofInTemplate(this, token);
- break;
- }
- case InsertionMode.AFTER_BODY:
- case InsertionMode.IN_FRAMESET:
- case InsertionMode.AFTER_FRAMESET:
- case InsertionMode.AFTER_AFTER_BODY:
- case InsertionMode.AFTER_AFTER_FRAMESET: {
- stopParsing(this, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- /** @internal */
- onWhitespaceCharacter(token) {
- if (this.skipNextNewLine) {
- this.skipNextNewLine = false;
- if (token.chars.charCodeAt(0) === unicode.CODE_POINTS.LINE_FEED) {
- if (token.chars.length === 1) {
- return;
- }
- token.chars = token.chars.substr(1);
- }
- }
- if (this.tokenizer.inForeignNode) {
- this._insertCharacters(token);
- return;
- }
- switch (this.insertionMode) {
- case InsertionMode.IN_HEAD:
- case InsertionMode.IN_HEAD_NO_SCRIPT:
- case InsertionMode.AFTER_HEAD:
- case InsertionMode.TEXT:
- case InsertionMode.IN_COLUMN_GROUP:
- case InsertionMode.IN_SELECT:
- case InsertionMode.IN_SELECT_IN_TABLE:
- case InsertionMode.IN_FRAMESET:
- case InsertionMode.AFTER_FRAMESET: {
- this._insertCharacters(token);
- break;
- }
- case InsertionMode.IN_BODY:
- case InsertionMode.IN_CAPTION:
- case InsertionMode.IN_CELL:
- case InsertionMode.IN_TEMPLATE:
- case InsertionMode.AFTER_BODY:
- case InsertionMode.AFTER_AFTER_BODY:
- case InsertionMode.AFTER_AFTER_FRAMESET: {
- whitespaceCharacterInBody(this, token);
- break;
- }
- case InsertionMode.IN_TABLE:
- case InsertionMode.IN_TABLE_BODY:
- case InsertionMode.IN_ROW: {
- characterInTable(this, token);
- break;
- }
- case InsertionMode.IN_TABLE_TEXT: {
- whitespaceCharacterInTableText(this, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- }
- //Adoption agency algorithm
- //(see: http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#adoptionAgency)
- //------------------------------------------------------------------
- //Steps 5-8 of the algorithm
- function aaObtainFormattingElementEntry(p, token) {
- let formattingElementEntry = p.activeFormattingElements.getElementEntryInScopeWithTagName(token.tagName);
- if (formattingElementEntry) {
- if (!p.openElements.contains(formattingElementEntry.element)) {
- p.activeFormattingElements.removeEntry(formattingElementEntry);
- formattingElementEntry = null;
- }
- else if (!p.openElements.hasInScope(token.tagID)) {
- formattingElementEntry = null;
- }
- }
- else {
- genericEndTagInBody(p, token);
- }
- return formattingElementEntry;
- }
- //Steps 9 and 10 of the algorithm
- function aaObtainFurthestBlock(p, formattingElementEntry) {
- let furthestBlock = null;
- let idx = p.openElements.stackTop;
- for (; idx >= 0; idx--) {
- const element = p.openElements.items[idx];
- if (element === formattingElementEntry.element) {
- break;
- }
- if (p._isSpecialElement(element, p.openElements.tagIDs[idx])) {
- furthestBlock = element;
- }
- }
- if (!furthestBlock) {
- p.openElements.shortenToLength(Math.max(idx, 0));
- p.activeFormattingElements.removeEntry(formattingElementEntry);
- }
- return furthestBlock;
- }
- //Step 13 of the algorithm
- function aaInnerLoop(p, furthestBlock, formattingElement) {
- let lastElement = furthestBlock;
- let nextElement = p.openElements.getCommonAncestor(furthestBlock);
- for (let i = 0, element = nextElement; element !== formattingElement; i++, element = nextElement) {
- //NOTE: store the next element for the next loop iteration (it may be deleted from the stack by step 9.5)
- nextElement = p.openElements.getCommonAncestor(element);
- const elementEntry = p.activeFormattingElements.getElementEntry(element);
- const counterOverflow = elementEntry && i >= AA_INNER_LOOP_ITER;
- const shouldRemoveFromOpenElements = !elementEntry || counterOverflow;
- if (shouldRemoveFromOpenElements) {
- if (counterOverflow) {
- p.activeFormattingElements.removeEntry(elementEntry);
- }
- p.openElements.remove(element);
- }
- else {
- element = aaRecreateElementFromEntry(p, elementEntry);
- if (lastElement === furthestBlock) {
- p.activeFormattingElements.bookmark = elementEntry;
- }
- p.treeAdapter.detachNode(lastElement);
- p.treeAdapter.appendChild(element, lastElement);
- lastElement = element;
- }
- }
- return lastElement;
- }
- //Step 13.7 of the algorithm
- function aaRecreateElementFromEntry(p, elementEntry) {
- const ns = p.treeAdapter.getNamespaceURI(elementEntry.element);
- const newElement = p.treeAdapter.createElement(elementEntry.token.tagName, ns, elementEntry.token.attrs);
- p.openElements.replace(elementEntry.element, newElement);
- elementEntry.element = newElement;
- return newElement;
- }
- //Step 14 of the algorithm
- function aaInsertLastNodeInCommonAncestor(p, commonAncestor, lastElement) {
- const tn = p.treeAdapter.getTagName(commonAncestor);
- const tid = getTagID(tn);
- if (p._isElementCausesFosterParenting(tid)) {
- p._fosterParentElement(lastElement);
- }
- else {
- const ns = p.treeAdapter.getNamespaceURI(commonAncestor);
- if (tid === $.TEMPLATE && ns === NS.HTML) {
- commonAncestor = p.treeAdapter.getTemplateContent(commonAncestor);
- }
- p.treeAdapter.appendChild(commonAncestor, lastElement);
- }
- }
- //Steps 15-19 of the algorithm
- function aaReplaceFormattingElement(p, furthestBlock, formattingElementEntry) {
- const ns = p.treeAdapter.getNamespaceURI(formattingElementEntry.element);
- const { token } = formattingElementEntry;
- const newElement = p.treeAdapter.createElement(token.tagName, ns, token.attrs);
- p._adoptNodes(furthestBlock, newElement);
- p.treeAdapter.appendChild(furthestBlock, newElement);
- p.activeFormattingElements.insertElementAfterBookmark(newElement, token);
- p.activeFormattingElements.removeEntry(formattingElementEntry);
- p.openElements.remove(formattingElementEntry.element);
- p.openElements.insertAfter(furthestBlock, newElement, token.tagID);
- }
- //Algorithm entry point
- function callAdoptionAgency(p, token) {
- for (let i = 0; i < AA_OUTER_LOOP_ITER; i++) {
- const formattingElementEntry = aaObtainFormattingElementEntry(p, token);
- if (!formattingElementEntry) {
- break;
- }
- const furthestBlock = aaObtainFurthestBlock(p, formattingElementEntry);
- if (!furthestBlock) {
- break;
- }
- p.activeFormattingElements.bookmark = formattingElementEntry;
- const lastElement = aaInnerLoop(p, furthestBlock, formattingElementEntry.element);
- const commonAncestor = p.openElements.getCommonAncestor(formattingElementEntry.element);
- p.treeAdapter.detachNode(lastElement);
- if (commonAncestor)
- aaInsertLastNodeInCommonAncestor(p, commonAncestor, lastElement);
- aaReplaceFormattingElement(p, furthestBlock, formattingElementEntry);
- }
- }
- //Generic token handlers
- //------------------------------------------------------------------
- function appendComment(p, token) {
- p._appendCommentNode(token, p.openElements.currentTmplContentOrNode);
- }
- function appendCommentToRootHtmlElement(p, token) {
- p._appendCommentNode(token, p.openElements.items[0]);
- }
- function appendCommentToDocument(p, token) {
- p._appendCommentNode(token, p.document);
- }
- function stopParsing(p, token) {
- p.stopped = true;
- // NOTE: Set end locations for elements that remain on the open element stack.
- if (token.location) {
- // NOTE: If we are not in a fragment, `html` and `body` will stay on the stack.
- // This is a problem, as we might overwrite their end position here.
- const target = p.fragmentContext ? 0 : 2;
- for (let i = p.openElements.stackTop; i >= target; i--) {
- p._setEndLocation(p.openElements.items[i], token);
- }
- // Handle `html` and `body`
- if (!p.fragmentContext && p.openElements.stackTop >= 0) {
- const htmlElement = p.openElements.items[0];
- const htmlLocation = p.treeAdapter.getNodeSourceCodeLocation(htmlElement);
- if (htmlLocation && !htmlLocation.endTag) {
- p._setEndLocation(htmlElement, token);
- if (p.openElements.stackTop >= 1) {
- const bodyElement = p.openElements.items[1];
- const bodyLocation = p.treeAdapter.getNodeSourceCodeLocation(bodyElement);
- if (bodyLocation && !bodyLocation.endTag) {
- p._setEndLocation(bodyElement, token);
- }
- }
- }
- }
- }
- }
- // The "initial" insertion mode
- //------------------------------------------------------------------
- function doctypeInInitialMode(p, token) {
- p._setDocumentType(token);
- const mode = token.forceQuirks ? DOCUMENT_MODE.QUIRKS : doctype.getDocumentMode(token);
- if (!doctype.isConforming(token)) {
- p._err(token, ERR.nonConformingDoctype);
- }
- p.treeAdapter.setDocumentMode(p.document, mode);
- p.insertionMode = InsertionMode.BEFORE_HTML;
- }
- function tokenInInitialMode(p, token) {
- p._err(token, ERR.missingDoctype, true);
- p.treeAdapter.setDocumentMode(p.document, DOCUMENT_MODE.QUIRKS);
- p.insertionMode = InsertionMode.BEFORE_HTML;
- p._processToken(token);
- }
- // The "before html" insertion mode
- //------------------------------------------------------------------
- function startTagBeforeHtml(p, token) {
- if (token.tagID === $.HTML) {
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.BEFORE_HEAD;
- }
- else {
- tokenBeforeHtml(p, token);
- }
- }
- function endTagBeforeHtml(p, token) {
- const tn = token.tagID;
- if (tn === $.HTML || tn === $.HEAD || tn === $.BODY || tn === $.BR) {
- tokenBeforeHtml(p, token);
- }
- }
- function tokenBeforeHtml(p, token) {
- p._insertFakeRootElement();
- p.insertionMode = InsertionMode.BEFORE_HEAD;
- p._processToken(token);
- }
- // The "before head" insertion mode
- //------------------------------------------------------------------
- function startTagBeforeHead(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.HEAD: {
- p._insertElement(token, NS.HTML);
- p.headElement = p.openElements.current;
- p.insertionMode = InsertionMode.IN_HEAD;
- break;
- }
- default: {
- tokenBeforeHead(p, token);
- }
- }
- }
- function endTagBeforeHead(p, token) {
- const tn = token.tagID;
- if (tn === $.HEAD || tn === $.BODY || tn === $.HTML || tn === $.BR) {
- tokenBeforeHead(p, token);
- }
- else {
- p._err(token, ERR.endTagWithoutMatchingOpenElement);
- }
- }
- function tokenBeforeHead(p, token) {
- p._insertFakeElement(TN.HEAD, $.HEAD);
- p.headElement = p.openElements.current;
- p.insertionMode = InsertionMode.IN_HEAD;
- p._processToken(token);
- }
- // The "in head" insertion mode
- //------------------------------------------------------------------
- function startTagInHead(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.BASE:
- case $.BASEFONT:
- case $.BGSOUND:
- case $.LINK:
- case $.META: {
- p._appendElement(token, NS.HTML);
- token.ackSelfClosing = true;
- break;
- }
- case $.TITLE: {
- p._switchToTextParsing(token, TokenizerMode.RCDATA);
- break;
- }
- case $.NOSCRIPT: {
- if (p.options.scriptingEnabled) {
- p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
- }
- else {
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.IN_HEAD_NO_SCRIPT;
- }
- break;
- }
- case $.NOFRAMES:
- case $.STYLE: {
- p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
- break;
- }
- case $.SCRIPT: {
- p._switchToTextParsing(token, TokenizerMode.SCRIPT_DATA);
- break;
- }
- case $.TEMPLATE: {
- p._insertTemplate(token);
- p.activeFormattingElements.insertMarker();
- p.framesetOk = false;
- p.insertionMode = InsertionMode.IN_TEMPLATE;
- p.tmplInsertionModeStack.unshift(InsertionMode.IN_TEMPLATE);
- break;
- }
- case $.HEAD: {
- p._err(token, ERR.misplacedStartTagForHeadElement);
- break;
- }
- default: {
- tokenInHead(p, token);
- }
- }
- }
- function endTagInHead(p, token) {
- switch (token.tagID) {
- case $.HEAD: {
- p.openElements.pop();
- p.insertionMode = InsertionMode.AFTER_HEAD;
- break;
- }
- case $.BODY:
- case $.BR:
- case $.HTML: {
- tokenInHead(p, token);
- break;
- }
- case $.TEMPLATE: {
- templateEndTagInHead(p, token);
- break;
- }
- default: {
- p._err(token, ERR.endTagWithoutMatchingOpenElement);
- }
- }
- }
- function templateEndTagInHead(p, token) {
- if (p.openElements.tmplCount > 0) {
- p.openElements.generateImpliedEndTagsThoroughly();
- if (p.openElements.currentTagId !== $.TEMPLATE) {
- p._err(token, ERR.closingOfElementWithOpenChildElements);
- }
- p.openElements.popUntilTagNamePopped($.TEMPLATE);
- p.activeFormattingElements.clearToLastMarker();
- p.tmplInsertionModeStack.shift();
- p._resetInsertionMode();
- }
- else {
- p._err(token, ERR.endTagWithoutMatchingOpenElement);
- }
- }
- function tokenInHead(p, token) {
- p.openElements.pop();
- p.insertionMode = InsertionMode.AFTER_HEAD;
- p._processToken(token);
- }
- // The "in head no script" insertion mode
- //------------------------------------------------------------------
- function startTagInHeadNoScript(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.BASEFONT:
- case $.BGSOUND:
- case $.HEAD:
- case $.LINK:
- case $.META:
- case $.NOFRAMES:
- case $.STYLE: {
- startTagInHead(p, token);
- break;
- }
- case $.NOSCRIPT: {
- p._err(token, ERR.nestedNoscriptInHead);
- break;
- }
- default: {
- tokenInHeadNoScript(p, token);
- }
- }
- }
- function endTagInHeadNoScript(p, token) {
- switch (token.tagID) {
- case $.NOSCRIPT: {
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_HEAD;
- break;
- }
- case $.BR: {
- tokenInHeadNoScript(p, token);
- break;
- }
- default: {
- p._err(token, ERR.endTagWithoutMatchingOpenElement);
- }
- }
- }
- function tokenInHeadNoScript(p, token) {
- const errCode = token.type === TokenType.EOF ? ERR.openElementsLeftAfterEof : ERR.disallowedContentInNoscriptInHead;
- p._err(token, errCode);
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_HEAD;
- p._processToken(token);
- }
- // The "after head" insertion mode
- //------------------------------------------------------------------
- function startTagAfterHead(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.BODY: {
- p._insertElement(token, NS.HTML);
- p.framesetOk = false;
- p.insertionMode = InsertionMode.IN_BODY;
- break;
- }
- case $.FRAMESET: {
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.IN_FRAMESET;
- break;
- }
- case $.BASE:
- case $.BASEFONT:
- case $.BGSOUND:
- case $.LINK:
- case $.META:
- case $.NOFRAMES:
- case $.SCRIPT:
- case $.STYLE:
- case $.TEMPLATE:
- case $.TITLE: {
- p._err(token, ERR.abandonedHeadElementChild);
- p.openElements.push(p.headElement, $.HEAD);
- startTagInHead(p, token);
- p.openElements.remove(p.headElement);
- break;
- }
- case $.HEAD: {
- p._err(token, ERR.misplacedStartTagForHeadElement);
- break;
- }
- default: {
- tokenAfterHead(p, token);
- }
- }
- }
- function endTagAfterHead(p, token) {
- switch (token.tagID) {
- case $.BODY:
- case $.HTML:
- case $.BR: {
- tokenAfterHead(p, token);
- break;
- }
- case $.TEMPLATE: {
- templateEndTagInHead(p, token);
- break;
- }
- default: {
- p._err(token, ERR.endTagWithoutMatchingOpenElement);
- }
- }
- }
- function tokenAfterHead(p, token) {
- p._insertFakeElement(TN.BODY, $.BODY);
- p.insertionMode = InsertionMode.IN_BODY;
- modeInBody(p, token);
- }
- // The "in body" insertion mode
- //------------------------------------------------------------------
- function modeInBody(p, token) {
- switch (token.type) {
- case TokenType.CHARACTER: {
- characterInBody(p, token);
- break;
- }
- case TokenType.WHITESPACE_CHARACTER: {
- whitespaceCharacterInBody(p, token);
- break;
- }
- case TokenType.COMMENT: {
- appendComment(p, token);
- break;
- }
- case TokenType.START_TAG: {
- startTagInBody(p, token);
- break;
- }
- case TokenType.END_TAG: {
- endTagInBody(p, token);
- break;
- }
- case TokenType.EOF: {
- eofInBody(p, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- function whitespaceCharacterInBody(p, token) {
- p._reconstructActiveFormattingElements();
- p._insertCharacters(token);
- }
- function characterInBody(p, token) {
- p._reconstructActiveFormattingElements();
- p._insertCharacters(token);
- p.framesetOk = false;
- }
- function htmlStartTagInBody(p, token) {
- if (p.openElements.tmplCount === 0) {
- p.treeAdapter.adoptAttributes(p.openElements.items[0], token.attrs);
- }
- }
- function bodyStartTagInBody(p, token) {
- const bodyElement = p.openElements.tryPeekProperlyNestedBodyElement();
- if (bodyElement && p.openElements.tmplCount === 0) {
- p.framesetOk = false;
- p.treeAdapter.adoptAttributes(bodyElement, token.attrs);
- }
- }
- function framesetStartTagInBody(p, token) {
- const bodyElement = p.openElements.tryPeekProperlyNestedBodyElement();
- if (p.framesetOk && bodyElement) {
- p.treeAdapter.detachNode(bodyElement);
- p.openElements.popAllUpToHtmlElement();
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.IN_FRAMESET;
- }
- }
- function addressStartTagInBody(p, token) {
- if (p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- p._insertElement(token, NS.HTML);
- }
- function numberedHeaderStartTagInBody(p, token) {
- if (p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- if (p.openElements.currentTagId !== undefined && NUMBERED_HEADERS.has(p.openElements.currentTagId)) {
- p.openElements.pop();
- }
- p._insertElement(token, NS.HTML);
- }
- function preStartTagInBody(p, token) {
- if (p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- p._insertElement(token, NS.HTML);
- //NOTE: If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move
- //on to the next one. (Newlines at the start of pre blocks are ignored as an authoring convenience.)
- p.skipNextNewLine = true;
- p.framesetOk = false;
- }
- function formStartTagInBody(p, token) {
- const inTemplate = p.openElements.tmplCount > 0;
- if (!p.formElement || inTemplate) {
- if (p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- p._insertElement(token, NS.HTML);
- if (!inTemplate) {
- p.formElement = p.openElements.current;
- }
- }
- }
- function listItemStartTagInBody(p, token) {
- p.framesetOk = false;
- const tn = token.tagID;
- for (let i = p.openElements.stackTop; i >= 0; i--) {
- const elementId = p.openElements.tagIDs[i];
- if ((tn === $.LI && elementId === $.LI) ||
- ((tn === $.DD || tn === $.DT) && (elementId === $.DD || elementId === $.DT))) {
- p.openElements.generateImpliedEndTagsWithExclusion(elementId);
- p.openElements.popUntilTagNamePopped(elementId);
- break;
- }
- if (elementId !== $.ADDRESS &&
- elementId !== $.DIV &&
- elementId !== $.P &&
- p._isSpecialElement(p.openElements.items[i], elementId)) {
- break;
- }
- }
- if (p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- p._insertElement(token, NS.HTML);
- }
- function plaintextStartTagInBody(p, token) {
- if (p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- p._insertElement(token, NS.HTML);
- p.tokenizer.state = TokenizerMode.PLAINTEXT;
- }
- function buttonStartTagInBody(p, token) {
- if (p.openElements.hasInScope($.BUTTON)) {
- p.openElements.generateImpliedEndTags();
- p.openElements.popUntilTagNamePopped($.BUTTON);
- }
- p._reconstructActiveFormattingElements();
- p._insertElement(token, NS.HTML);
- p.framesetOk = false;
- }
- function aStartTagInBody(p, token) {
- const activeElementEntry = p.activeFormattingElements.getElementEntryInScopeWithTagName(TN.A);
- if (activeElementEntry) {
- callAdoptionAgency(p, token);
- p.openElements.remove(activeElementEntry.element);
- p.activeFormattingElements.removeEntry(activeElementEntry);
- }
- p._reconstructActiveFormattingElements();
- p._insertElement(token, NS.HTML);
- p.activeFormattingElements.pushElement(p.openElements.current, token);
- }
- function bStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- p._insertElement(token, NS.HTML);
- p.activeFormattingElements.pushElement(p.openElements.current, token);
- }
- function nobrStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- if (p.openElements.hasInScope($.NOBR)) {
- callAdoptionAgency(p, token);
- p._reconstructActiveFormattingElements();
- }
- p._insertElement(token, NS.HTML);
- p.activeFormattingElements.pushElement(p.openElements.current, token);
- }
- function appletStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- p._insertElement(token, NS.HTML);
- p.activeFormattingElements.insertMarker();
- p.framesetOk = false;
- }
- function tableStartTagInBody(p, token) {
- if (p.treeAdapter.getDocumentMode(p.document) !== DOCUMENT_MODE.QUIRKS && p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- p._insertElement(token, NS.HTML);
- p.framesetOk = false;
- p.insertionMode = InsertionMode.IN_TABLE;
- }
- function areaStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- p._appendElement(token, NS.HTML);
- p.framesetOk = false;
- token.ackSelfClosing = true;
- }
- function isHiddenInput(token) {
- const inputType = getTokenAttr(token, ATTRS.TYPE);
- return inputType != null && inputType.toLowerCase() === HIDDEN_INPUT_TYPE;
- }
- function inputStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- p._appendElement(token, NS.HTML);
- if (!isHiddenInput(token)) {
- p.framesetOk = false;
- }
- token.ackSelfClosing = true;
- }
- function paramStartTagInBody(p, token) {
- p._appendElement(token, NS.HTML);
- token.ackSelfClosing = true;
- }
- function hrStartTagInBody(p, token) {
- if (p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- p._appendElement(token, NS.HTML);
- p.framesetOk = false;
- token.ackSelfClosing = true;
- }
- function imageStartTagInBody(p, token) {
- token.tagName = TN.IMG;
- token.tagID = $.IMG;
- areaStartTagInBody(p, token);
- }
- function textareaStartTagInBody(p, token) {
- p._insertElement(token, NS.HTML);
- //NOTE: If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move
- //on to the next one. (Newlines at the start of textarea elements are ignored as an authoring convenience.)
- p.skipNextNewLine = true;
- p.tokenizer.state = TokenizerMode.RCDATA;
- p.originalInsertionMode = p.insertionMode;
- p.framesetOk = false;
- p.insertionMode = InsertionMode.TEXT;
- }
- function xmpStartTagInBody(p, token) {
- if (p.openElements.hasInButtonScope($.P)) {
- p._closePElement();
- }
- p._reconstructActiveFormattingElements();
- p.framesetOk = false;
- p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
- }
- function iframeStartTagInBody(p, token) {
- p.framesetOk = false;
- p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
- }
- //NOTE: here we assume that we always act as a user agent with enabled plugins/frames, so we parse
- //<noembed>/<noframes> as rawtext.
- function rawTextStartTagInBody(p, token) {
- p._switchToTextParsing(token, TokenizerMode.RAWTEXT);
- }
- function selectStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- p._insertElement(token, NS.HTML);
- p.framesetOk = false;
- p.insertionMode =
- p.insertionMode === InsertionMode.IN_TABLE ||
- p.insertionMode === InsertionMode.IN_CAPTION ||
- p.insertionMode === InsertionMode.IN_TABLE_BODY ||
- p.insertionMode === InsertionMode.IN_ROW ||
- p.insertionMode === InsertionMode.IN_CELL
- ? InsertionMode.IN_SELECT_IN_TABLE
- : InsertionMode.IN_SELECT;
- }
- function optgroupStartTagInBody(p, token) {
- if (p.openElements.currentTagId === $.OPTION) {
- p.openElements.pop();
- }
- p._reconstructActiveFormattingElements();
- p._insertElement(token, NS.HTML);
- }
- function rbStartTagInBody(p, token) {
- if (p.openElements.hasInScope($.RUBY)) {
- p.openElements.generateImpliedEndTags();
- }
- p._insertElement(token, NS.HTML);
- }
- function rtStartTagInBody(p, token) {
- if (p.openElements.hasInScope($.RUBY)) {
- p.openElements.generateImpliedEndTagsWithExclusion($.RTC);
- }
- p._insertElement(token, NS.HTML);
- }
- function mathStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- foreignContent.adjustTokenMathMLAttrs(token);
- foreignContent.adjustTokenXMLAttrs(token);
- if (token.selfClosing) {
- p._appendElement(token, NS.MATHML);
- }
- else {
- p._insertElement(token, NS.MATHML);
- }
- token.ackSelfClosing = true;
- }
- function svgStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- foreignContent.adjustTokenSVGAttrs(token);
- foreignContent.adjustTokenXMLAttrs(token);
- if (token.selfClosing) {
- p._appendElement(token, NS.SVG);
- }
- else {
- p._insertElement(token, NS.SVG);
- }
- token.ackSelfClosing = true;
- }
- function genericStartTagInBody(p, token) {
- p._reconstructActiveFormattingElements();
- p._insertElement(token, NS.HTML);
- }
- function startTagInBody(p, token) {
- switch (token.tagID) {
- case $.I:
- case $.S:
- case $.B:
- case $.U:
- case $.EM:
- case $.TT:
- case $.BIG:
- case $.CODE:
- case $.FONT:
- case $.SMALL:
- case $.STRIKE:
- case $.STRONG: {
- bStartTagInBody(p, token);
- break;
- }
- case $.A: {
- aStartTagInBody(p, token);
- break;
- }
- case $.H1:
- case $.H2:
- case $.H3:
- case $.H4:
- case $.H5:
- case $.H6: {
- numberedHeaderStartTagInBody(p, token);
- break;
- }
- case $.P:
- case $.DL:
- case $.OL:
- case $.UL:
- case $.DIV:
- case $.DIR:
- case $.NAV:
- case $.MAIN:
- case $.MENU:
- case $.ASIDE:
- case $.CENTER:
- case $.FIGURE:
- case $.FOOTER:
- case $.HEADER:
- case $.HGROUP:
- case $.DIALOG:
- case $.DETAILS:
- case $.ADDRESS:
- case $.ARTICLE:
- case $.SEARCH:
- case $.SECTION:
- case $.SUMMARY:
- case $.FIELDSET:
- case $.BLOCKQUOTE:
- case $.FIGCAPTION: {
- addressStartTagInBody(p, token);
- break;
- }
- case $.LI:
- case $.DD:
- case $.DT: {
- listItemStartTagInBody(p, token);
- break;
- }
- case $.BR:
- case $.IMG:
- case $.WBR:
- case $.AREA:
- case $.EMBED:
- case $.KEYGEN: {
- areaStartTagInBody(p, token);
- break;
- }
- case $.HR: {
- hrStartTagInBody(p, token);
- break;
- }
- case $.RB:
- case $.RTC: {
- rbStartTagInBody(p, token);
- break;
- }
- case $.RT:
- case $.RP: {
- rtStartTagInBody(p, token);
- break;
- }
- case $.PRE:
- case $.LISTING: {
- preStartTagInBody(p, token);
- break;
- }
- case $.XMP: {
- xmpStartTagInBody(p, token);
- break;
- }
- case $.SVG: {
- svgStartTagInBody(p, token);
- break;
- }
- case $.HTML: {
- htmlStartTagInBody(p, token);
- break;
- }
- case $.BASE:
- case $.LINK:
- case $.META:
- case $.STYLE:
- case $.TITLE:
- case $.SCRIPT:
- case $.BGSOUND:
- case $.BASEFONT:
- case $.TEMPLATE: {
- startTagInHead(p, token);
- break;
- }
- case $.BODY: {
- bodyStartTagInBody(p, token);
- break;
- }
- case $.FORM: {
- formStartTagInBody(p, token);
- break;
- }
- case $.NOBR: {
- nobrStartTagInBody(p, token);
- break;
- }
- case $.MATH: {
- mathStartTagInBody(p, token);
- break;
- }
- case $.TABLE: {
- tableStartTagInBody(p, token);
- break;
- }
- case $.INPUT: {
- inputStartTagInBody(p, token);
- break;
- }
- case $.PARAM:
- case $.TRACK:
- case $.SOURCE: {
- paramStartTagInBody(p, token);
- break;
- }
- case $.IMAGE: {
- imageStartTagInBody(p, token);
- break;
- }
- case $.BUTTON: {
- buttonStartTagInBody(p, token);
- break;
- }
- case $.APPLET:
- case $.OBJECT:
- case $.MARQUEE: {
- appletStartTagInBody(p, token);
- break;
- }
- case $.IFRAME: {
- iframeStartTagInBody(p, token);
- break;
- }
- case $.SELECT: {
- selectStartTagInBody(p, token);
- break;
- }
- case $.OPTION:
- case $.OPTGROUP: {
- optgroupStartTagInBody(p, token);
- break;
- }
- case $.NOEMBED:
- case $.NOFRAMES: {
- rawTextStartTagInBody(p, token);
- break;
- }
- case $.FRAMESET: {
- framesetStartTagInBody(p, token);
- break;
- }
- case $.TEXTAREA: {
- textareaStartTagInBody(p, token);
- break;
- }
- case $.NOSCRIPT: {
- if (p.options.scriptingEnabled) {
- rawTextStartTagInBody(p, token);
- }
- else {
- genericStartTagInBody(p, token);
- }
- break;
- }
- case $.PLAINTEXT: {
- plaintextStartTagInBody(p, token);
- break;
- }
- case $.COL:
- case $.TH:
- case $.TD:
- case $.TR:
- case $.HEAD:
- case $.FRAME:
- case $.TBODY:
- case $.TFOOT:
- case $.THEAD:
- case $.CAPTION:
- case $.COLGROUP: {
- // Ignore token
- break;
- }
- default: {
- genericStartTagInBody(p, token);
- }
- }
- }
- function bodyEndTagInBody(p, token) {
- if (p.openElements.hasInScope($.BODY)) {
- p.insertionMode = InsertionMode.AFTER_BODY;
- //NOTE: <body> is never popped from the stack, so we need to updated
- //the end location explicitly.
- if (p.options.sourceCodeLocationInfo) {
- const bodyElement = p.openElements.tryPeekProperlyNestedBodyElement();
- if (bodyElement) {
- p._setEndLocation(bodyElement, token);
- }
- }
- }
- }
- function htmlEndTagInBody(p, token) {
- if (p.openElements.hasInScope($.BODY)) {
- p.insertionMode = InsertionMode.AFTER_BODY;
- endTagAfterBody(p, token);
- }
- }
- function addressEndTagInBody(p, token) {
- const tn = token.tagID;
- if (p.openElements.hasInScope(tn)) {
- p.openElements.generateImpliedEndTags();
- p.openElements.popUntilTagNamePopped(tn);
- }
- }
- function formEndTagInBody(p) {
- const inTemplate = p.openElements.tmplCount > 0;
- const { formElement } = p;
- if (!inTemplate) {
- p.formElement = null;
- }
- if ((formElement || inTemplate) && p.openElements.hasInScope($.FORM)) {
- p.openElements.generateImpliedEndTags();
- if (inTemplate) {
- p.openElements.popUntilTagNamePopped($.FORM);
- }
- else if (formElement) {
- p.openElements.remove(formElement);
- }
- }
- }
- function pEndTagInBody(p) {
- if (!p.openElements.hasInButtonScope($.P)) {
- p._insertFakeElement(TN.P, $.P);
- }
- p._closePElement();
- }
- function liEndTagInBody(p) {
- if (p.openElements.hasInListItemScope($.LI)) {
- p.openElements.generateImpliedEndTagsWithExclusion($.LI);
- p.openElements.popUntilTagNamePopped($.LI);
- }
- }
- function ddEndTagInBody(p, token) {
- const tn = token.tagID;
- if (p.openElements.hasInScope(tn)) {
- p.openElements.generateImpliedEndTagsWithExclusion(tn);
- p.openElements.popUntilTagNamePopped(tn);
- }
- }
- function numberedHeaderEndTagInBody(p) {
- if (p.openElements.hasNumberedHeaderInScope()) {
- p.openElements.generateImpliedEndTags();
- p.openElements.popUntilNumberedHeaderPopped();
- }
- }
- function appletEndTagInBody(p, token) {
- const tn = token.tagID;
- if (p.openElements.hasInScope(tn)) {
- p.openElements.generateImpliedEndTags();
- p.openElements.popUntilTagNamePopped(tn);
- p.activeFormattingElements.clearToLastMarker();
- }
- }
- function brEndTagInBody(p) {
- p._reconstructActiveFormattingElements();
- p._insertFakeElement(TN.BR, $.BR);
- p.openElements.pop();
- p.framesetOk = false;
- }
- function genericEndTagInBody(p, token) {
- const tn = token.tagName;
- const tid = token.tagID;
- for (let i = p.openElements.stackTop; i > 0; i--) {
- const element = p.openElements.items[i];
- const elementId = p.openElements.tagIDs[i];
- // Compare the tag name here, as the tag might not be a known tag with an ID.
- if (tid === elementId && (tid !== $.UNKNOWN || p.treeAdapter.getTagName(element) === tn)) {
- p.openElements.generateImpliedEndTagsWithExclusion(tid);
- if (p.openElements.stackTop >= i)
- p.openElements.shortenToLength(i);
- break;
- }
- if (p._isSpecialElement(element, elementId)) {
- break;
- }
- }
- }
- function endTagInBody(p, token) {
- switch (token.tagID) {
- case $.A:
- case $.B:
- case $.I:
- case $.S:
- case $.U:
- case $.EM:
- case $.TT:
- case $.BIG:
- case $.CODE:
- case $.FONT:
- case $.NOBR:
- case $.SMALL:
- case $.STRIKE:
- case $.STRONG: {
- callAdoptionAgency(p, token);
- break;
- }
- case $.P: {
- pEndTagInBody(p);
- break;
- }
- case $.DL:
- case $.UL:
- case $.OL:
- case $.DIR:
- case $.DIV:
- case $.NAV:
- case $.PRE:
- case $.MAIN:
- case $.MENU:
- case $.ASIDE:
- case $.BUTTON:
- case $.CENTER:
- case $.FIGURE:
- case $.FOOTER:
- case $.HEADER:
- case $.HGROUP:
- case $.DIALOG:
- case $.ADDRESS:
- case $.ARTICLE:
- case $.DETAILS:
- case $.SEARCH:
- case $.SECTION:
- case $.SUMMARY:
- case $.LISTING:
- case $.FIELDSET:
- case $.BLOCKQUOTE:
- case $.FIGCAPTION: {
- addressEndTagInBody(p, token);
- break;
- }
- case $.LI: {
- liEndTagInBody(p);
- break;
- }
- case $.DD:
- case $.DT: {
- ddEndTagInBody(p, token);
- break;
- }
- case $.H1:
- case $.H2:
- case $.H3:
- case $.H4:
- case $.H5:
- case $.H6: {
- numberedHeaderEndTagInBody(p);
- break;
- }
- case $.BR: {
- brEndTagInBody(p);
- break;
- }
- case $.BODY: {
- bodyEndTagInBody(p, token);
- break;
- }
- case $.HTML: {
- htmlEndTagInBody(p, token);
- break;
- }
- case $.FORM: {
- formEndTagInBody(p);
- break;
- }
- case $.APPLET:
- case $.OBJECT:
- case $.MARQUEE: {
- appletEndTagInBody(p, token);
- break;
- }
- case $.TEMPLATE: {
- templateEndTagInHead(p, token);
- break;
- }
- default: {
- genericEndTagInBody(p, token);
- }
- }
- }
- function eofInBody(p, token) {
- if (p.tmplInsertionModeStack.length > 0) {
- eofInTemplate(p, token);
- }
- else {
- stopParsing(p, token);
- }
- }
- // The "text" insertion mode
- //------------------------------------------------------------------
- function endTagInText(p, token) {
- var _a;
- if (token.tagID === $.SCRIPT) {
- (_a = p.scriptHandler) === null || _a === void 0 ? void 0 : _a.call(p, p.openElements.current);
- }
- p.openElements.pop();
- p.insertionMode = p.originalInsertionMode;
- }
- function eofInText(p, token) {
- p._err(token, ERR.eofInElementThatCanContainOnlyText);
- p.openElements.pop();
- p.insertionMode = p.originalInsertionMode;
- p.onEof(token);
- }
- // The "in table" insertion mode
- //------------------------------------------------------------------
- function characterInTable(p, token) {
- if (p.openElements.currentTagId !== undefined && TABLE_STRUCTURE_TAGS.has(p.openElements.currentTagId)) {
- p.pendingCharacterTokens.length = 0;
- p.hasNonWhitespacePendingCharacterToken = false;
- p.originalInsertionMode = p.insertionMode;
- p.insertionMode = InsertionMode.IN_TABLE_TEXT;
- switch (token.type) {
- case TokenType.CHARACTER: {
- characterInTableText(p, token);
- break;
- }
- case TokenType.WHITESPACE_CHARACTER: {
- whitespaceCharacterInTableText(p, token);
- break;
- }
- // Ignore null
- }
- }
- else {
- tokenInTable(p, token);
- }
- }
- function captionStartTagInTable(p, token) {
- p.openElements.clearBackToTableContext();
- p.activeFormattingElements.insertMarker();
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.IN_CAPTION;
- }
- function colgroupStartTagInTable(p, token) {
- p.openElements.clearBackToTableContext();
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.IN_COLUMN_GROUP;
- }
- function colStartTagInTable(p, token) {
- p.openElements.clearBackToTableContext();
- p._insertFakeElement(TN.COLGROUP, $.COLGROUP);
- p.insertionMode = InsertionMode.IN_COLUMN_GROUP;
- startTagInColumnGroup(p, token);
- }
- function tbodyStartTagInTable(p, token) {
- p.openElements.clearBackToTableContext();
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.IN_TABLE_BODY;
- }
- function tdStartTagInTable(p, token) {
- p.openElements.clearBackToTableContext();
- p._insertFakeElement(TN.TBODY, $.TBODY);
- p.insertionMode = InsertionMode.IN_TABLE_BODY;
- startTagInTableBody(p, token);
- }
- function tableStartTagInTable(p, token) {
- if (p.openElements.hasInTableScope($.TABLE)) {
- p.openElements.popUntilTagNamePopped($.TABLE);
- p._resetInsertionMode();
- p._processStartTag(token);
- }
- }
- function inputStartTagInTable(p, token) {
- if (isHiddenInput(token)) {
- p._appendElement(token, NS.HTML);
- }
- else {
- tokenInTable(p, token);
- }
- token.ackSelfClosing = true;
- }
- function formStartTagInTable(p, token) {
- if (!p.formElement && p.openElements.tmplCount === 0) {
- p._insertElement(token, NS.HTML);
- p.formElement = p.openElements.current;
- p.openElements.pop();
- }
- }
- function startTagInTable(p, token) {
- switch (token.tagID) {
- case $.TD:
- case $.TH:
- case $.TR: {
- tdStartTagInTable(p, token);
- break;
- }
- case $.STYLE:
- case $.SCRIPT:
- case $.TEMPLATE: {
- startTagInHead(p, token);
- break;
- }
- case $.COL: {
- colStartTagInTable(p, token);
- break;
- }
- case $.FORM: {
- formStartTagInTable(p, token);
- break;
- }
- case $.TABLE: {
- tableStartTagInTable(p, token);
- break;
- }
- case $.TBODY:
- case $.TFOOT:
- case $.THEAD: {
- tbodyStartTagInTable(p, token);
- break;
- }
- case $.INPUT: {
- inputStartTagInTable(p, token);
- break;
- }
- case $.CAPTION: {
- captionStartTagInTable(p, token);
- break;
- }
- case $.COLGROUP: {
- colgroupStartTagInTable(p, token);
- break;
- }
- default: {
- tokenInTable(p, token);
- }
- }
- }
- function endTagInTable(p, token) {
- switch (token.tagID) {
- case $.TABLE: {
- if (p.openElements.hasInTableScope($.TABLE)) {
- p.openElements.popUntilTagNamePopped($.TABLE);
- p._resetInsertionMode();
- }
- break;
- }
- case $.TEMPLATE: {
- templateEndTagInHead(p, token);
- break;
- }
- case $.BODY:
- case $.CAPTION:
- case $.COL:
- case $.COLGROUP:
- case $.HTML:
- case $.TBODY:
- case $.TD:
- case $.TFOOT:
- case $.TH:
- case $.THEAD:
- case $.TR: {
- // Ignore token
- break;
- }
- default: {
- tokenInTable(p, token);
- }
- }
- }
- function tokenInTable(p, token) {
- const savedFosterParentingState = p.fosterParentingEnabled;
- p.fosterParentingEnabled = true;
- // Process token in `In Body` mode
- modeInBody(p, token);
- p.fosterParentingEnabled = savedFosterParentingState;
- }
- // The "in table text" insertion mode
- //------------------------------------------------------------------
- function whitespaceCharacterInTableText(p, token) {
- p.pendingCharacterTokens.push(token);
- }
- function characterInTableText(p, token) {
- p.pendingCharacterTokens.push(token);
- p.hasNonWhitespacePendingCharacterToken = true;
- }
- function tokenInTableText(p, token) {
- let i = 0;
- if (p.hasNonWhitespacePendingCharacterToken) {
- for (; i < p.pendingCharacterTokens.length; i++) {
- tokenInTable(p, p.pendingCharacterTokens[i]);
- }
- }
- else {
- for (; i < p.pendingCharacterTokens.length; i++) {
- p._insertCharacters(p.pendingCharacterTokens[i]);
- }
- }
- p.insertionMode = p.originalInsertionMode;
- p._processToken(token);
- }
- // The "in caption" insertion mode
- //------------------------------------------------------------------
- const TABLE_VOID_ELEMENTS = new Set([$.CAPTION, $.COL, $.COLGROUP, $.TBODY, $.TD, $.TFOOT, $.TH, $.THEAD, $.TR]);
- function startTagInCaption(p, token) {
- const tn = token.tagID;
- if (TABLE_VOID_ELEMENTS.has(tn)) {
- if (p.openElements.hasInTableScope($.CAPTION)) {
- p.openElements.generateImpliedEndTags();
- p.openElements.popUntilTagNamePopped($.CAPTION);
- p.activeFormattingElements.clearToLastMarker();
- p.insertionMode = InsertionMode.IN_TABLE;
- startTagInTable(p, token);
- }
- }
- else {
- startTagInBody(p, token);
- }
- }
- function endTagInCaption(p, token) {
- const tn = token.tagID;
- switch (tn) {
- case $.CAPTION:
- case $.TABLE: {
- if (p.openElements.hasInTableScope($.CAPTION)) {
- p.openElements.generateImpliedEndTags();
- p.openElements.popUntilTagNamePopped($.CAPTION);
- p.activeFormattingElements.clearToLastMarker();
- p.insertionMode = InsertionMode.IN_TABLE;
- if (tn === $.TABLE) {
- endTagInTable(p, token);
- }
- }
- break;
- }
- case $.BODY:
- case $.COL:
- case $.COLGROUP:
- case $.HTML:
- case $.TBODY:
- case $.TD:
- case $.TFOOT:
- case $.TH:
- case $.THEAD:
- case $.TR: {
- // Ignore token
- break;
- }
- default: {
- endTagInBody(p, token);
- }
- }
- }
- // The "in column group" insertion mode
- //------------------------------------------------------------------
- function startTagInColumnGroup(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.COL: {
- p._appendElement(token, NS.HTML);
- token.ackSelfClosing = true;
- break;
- }
- case $.TEMPLATE: {
- startTagInHead(p, token);
- break;
- }
- default: {
- tokenInColumnGroup(p, token);
- }
- }
- }
- function endTagInColumnGroup(p, token) {
- switch (token.tagID) {
- case $.COLGROUP: {
- if (p.openElements.currentTagId === $.COLGROUP) {
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE;
- }
- break;
- }
- case $.TEMPLATE: {
- templateEndTagInHead(p, token);
- break;
- }
- case $.COL: {
- // Ignore token
- break;
- }
- default: {
- tokenInColumnGroup(p, token);
- }
- }
- }
- function tokenInColumnGroup(p, token) {
- if (p.openElements.currentTagId === $.COLGROUP) {
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE;
- p._processToken(token);
- }
- }
- // The "in table body" insertion mode
- //------------------------------------------------------------------
- function startTagInTableBody(p, token) {
- switch (token.tagID) {
- case $.TR: {
- p.openElements.clearBackToTableBodyContext();
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.IN_ROW;
- break;
- }
- case $.TH:
- case $.TD: {
- p.openElements.clearBackToTableBodyContext();
- p._insertFakeElement(TN.TR, $.TR);
- p.insertionMode = InsertionMode.IN_ROW;
- startTagInRow(p, token);
- break;
- }
- case $.CAPTION:
- case $.COL:
- case $.COLGROUP:
- case $.TBODY:
- case $.TFOOT:
- case $.THEAD: {
- if (p.openElements.hasTableBodyContextInTableScope()) {
- p.openElements.clearBackToTableBodyContext();
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE;
- startTagInTable(p, token);
- }
- break;
- }
- default: {
- startTagInTable(p, token);
- }
- }
- }
- function endTagInTableBody(p, token) {
- const tn = token.tagID;
- switch (token.tagID) {
- case $.TBODY:
- case $.TFOOT:
- case $.THEAD: {
- if (p.openElements.hasInTableScope(tn)) {
- p.openElements.clearBackToTableBodyContext();
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE;
- }
- break;
- }
- case $.TABLE: {
- if (p.openElements.hasTableBodyContextInTableScope()) {
- p.openElements.clearBackToTableBodyContext();
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE;
- endTagInTable(p, token);
- }
- break;
- }
- case $.BODY:
- case $.CAPTION:
- case $.COL:
- case $.COLGROUP:
- case $.HTML:
- case $.TD:
- case $.TH:
- case $.TR: {
- // Ignore token
- break;
- }
- default: {
- endTagInTable(p, token);
- }
- }
- }
- // The "in row" insertion mode
- //------------------------------------------------------------------
- function startTagInRow(p, token) {
- switch (token.tagID) {
- case $.TH:
- case $.TD: {
- p.openElements.clearBackToTableRowContext();
- p._insertElement(token, NS.HTML);
- p.insertionMode = InsertionMode.IN_CELL;
- p.activeFormattingElements.insertMarker();
- break;
- }
- case $.CAPTION:
- case $.COL:
- case $.COLGROUP:
- case $.TBODY:
- case $.TFOOT:
- case $.THEAD:
- case $.TR: {
- if (p.openElements.hasInTableScope($.TR)) {
- p.openElements.clearBackToTableRowContext();
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE_BODY;
- startTagInTableBody(p, token);
- }
- break;
- }
- default: {
- startTagInTable(p, token);
- }
- }
- }
- function endTagInRow(p, token) {
- switch (token.tagID) {
- case $.TR: {
- if (p.openElements.hasInTableScope($.TR)) {
- p.openElements.clearBackToTableRowContext();
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE_BODY;
- }
- break;
- }
- case $.TABLE: {
- if (p.openElements.hasInTableScope($.TR)) {
- p.openElements.clearBackToTableRowContext();
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE_BODY;
- endTagInTableBody(p, token);
- }
- break;
- }
- case $.TBODY:
- case $.TFOOT:
- case $.THEAD: {
- if (p.openElements.hasInTableScope(token.tagID) || p.openElements.hasInTableScope($.TR)) {
- p.openElements.clearBackToTableRowContext();
- p.openElements.pop();
- p.insertionMode = InsertionMode.IN_TABLE_BODY;
- endTagInTableBody(p, token);
- }
- break;
- }
- case $.BODY:
- case $.CAPTION:
- case $.COL:
- case $.COLGROUP:
- case $.HTML:
- case $.TD:
- case $.TH: {
- // Ignore end tag
- break;
- }
- default: {
- endTagInTable(p, token);
- }
- }
- }
- // The "in cell" insertion mode
- //------------------------------------------------------------------
- function startTagInCell(p, token) {
- const tn = token.tagID;
- if (TABLE_VOID_ELEMENTS.has(tn)) {
- if (p.openElements.hasInTableScope($.TD) || p.openElements.hasInTableScope($.TH)) {
- p._closeTableCell();
- startTagInRow(p, token);
- }
- }
- else {
- startTagInBody(p, token);
- }
- }
- function endTagInCell(p, token) {
- const tn = token.tagID;
- switch (tn) {
- case $.TD:
- case $.TH: {
- if (p.openElements.hasInTableScope(tn)) {
- p.openElements.generateImpliedEndTags();
- p.openElements.popUntilTagNamePopped(tn);
- p.activeFormattingElements.clearToLastMarker();
- p.insertionMode = InsertionMode.IN_ROW;
- }
- break;
- }
- case $.TABLE:
- case $.TBODY:
- case $.TFOOT:
- case $.THEAD:
- case $.TR: {
- if (p.openElements.hasInTableScope(tn)) {
- p._closeTableCell();
- endTagInRow(p, token);
- }
- break;
- }
- case $.BODY:
- case $.CAPTION:
- case $.COL:
- case $.COLGROUP:
- case $.HTML: {
- // Ignore token
- break;
- }
- default: {
- endTagInBody(p, token);
- }
- }
- }
- // The "in select" insertion mode
- //------------------------------------------------------------------
- function startTagInSelect(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.OPTION: {
- if (p.openElements.currentTagId === $.OPTION) {
- p.openElements.pop();
- }
- p._insertElement(token, NS.HTML);
- break;
- }
- case $.OPTGROUP: {
- if (p.openElements.currentTagId === $.OPTION) {
- p.openElements.pop();
- }
- if (p.openElements.currentTagId === $.OPTGROUP) {
- p.openElements.pop();
- }
- p._insertElement(token, NS.HTML);
- break;
- }
- case $.HR: {
- if (p.openElements.currentTagId === $.OPTION) {
- p.openElements.pop();
- }
- if (p.openElements.currentTagId === $.OPTGROUP) {
- p.openElements.pop();
- }
- p._appendElement(token, NS.HTML);
- token.ackSelfClosing = true;
- break;
- }
- case $.INPUT:
- case $.KEYGEN:
- case $.TEXTAREA:
- case $.SELECT: {
- if (p.openElements.hasInSelectScope($.SELECT)) {
- p.openElements.popUntilTagNamePopped($.SELECT);
- p._resetInsertionMode();
- if (token.tagID !== $.SELECT) {
- p._processStartTag(token);
- }
- }
- break;
- }
- case $.SCRIPT:
- case $.TEMPLATE: {
- startTagInHead(p, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- function endTagInSelect(p, token) {
- switch (token.tagID) {
- case $.OPTGROUP: {
- if (p.openElements.stackTop > 0 &&
- p.openElements.currentTagId === $.OPTION &&
- p.openElements.tagIDs[p.openElements.stackTop - 1] === $.OPTGROUP) {
- p.openElements.pop();
- }
- if (p.openElements.currentTagId === $.OPTGROUP) {
- p.openElements.pop();
- }
- break;
- }
- case $.OPTION: {
- if (p.openElements.currentTagId === $.OPTION) {
- p.openElements.pop();
- }
- break;
- }
- case $.SELECT: {
- if (p.openElements.hasInSelectScope($.SELECT)) {
- p.openElements.popUntilTagNamePopped($.SELECT);
- p._resetInsertionMode();
- }
- break;
- }
- case $.TEMPLATE: {
- templateEndTagInHead(p, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- // The "in select in table" insertion mode
- //------------------------------------------------------------------
- function startTagInSelectInTable(p, token) {
- const tn = token.tagID;
- if (tn === $.CAPTION ||
- tn === $.TABLE ||
- tn === $.TBODY ||
- tn === $.TFOOT ||
- tn === $.THEAD ||
- tn === $.TR ||
- tn === $.TD ||
- tn === $.TH) {
- p.openElements.popUntilTagNamePopped($.SELECT);
- p._resetInsertionMode();
- p._processStartTag(token);
- }
- else {
- startTagInSelect(p, token);
- }
- }
- function endTagInSelectInTable(p, token) {
- const tn = token.tagID;
- if (tn === $.CAPTION ||
- tn === $.TABLE ||
- tn === $.TBODY ||
- tn === $.TFOOT ||
- tn === $.THEAD ||
- tn === $.TR ||
- tn === $.TD ||
- tn === $.TH) {
- if (p.openElements.hasInTableScope(tn)) {
- p.openElements.popUntilTagNamePopped($.SELECT);
- p._resetInsertionMode();
- p.onEndTag(token);
- }
- }
- else {
- endTagInSelect(p, token);
- }
- }
- // The "in template" insertion mode
- //------------------------------------------------------------------
- function startTagInTemplate(p, token) {
- switch (token.tagID) {
- // First, handle tags that can start without a mode change
- case $.BASE:
- case $.BASEFONT:
- case $.BGSOUND:
- case $.LINK:
- case $.META:
- case $.NOFRAMES:
- case $.SCRIPT:
- case $.STYLE:
- case $.TEMPLATE:
- case $.TITLE: {
- startTagInHead(p, token);
- break;
- }
- // Re-process the token in the appropriate mode
- case $.CAPTION:
- case $.COLGROUP:
- case $.TBODY:
- case $.TFOOT:
- case $.THEAD: {
- p.tmplInsertionModeStack[0] = InsertionMode.IN_TABLE;
- p.insertionMode = InsertionMode.IN_TABLE;
- startTagInTable(p, token);
- break;
- }
- case $.COL: {
- p.tmplInsertionModeStack[0] = InsertionMode.IN_COLUMN_GROUP;
- p.insertionMode = InsertionMode.IN_COLUMN_GROUP;
- startTagInColumnGroup(p, token);
- break;
- }
- case $.TR: {
- p.tmplInsertionModeStack[0] = InsertionMode.IN_TABLE_BODY;
- p.insertionMode = InsertionMode.IN_TABLE_BODY;
- startTagInTableBody(p, token);
- break;
- }
- case $.TD:
- case $.TH: {
- p.tmplInsertionModeStack[0] = InsertionMode.IN_ROW;
- p.insertionMode = InsertionMode.IN_ROW;
- startTagInRow(p, token);
- break;
- }
- default: {
- p.tmplInsertionModeStack[0] = InsertionMode.IN_BODY;
- p.insertionMode = InsertionMode.IN_BODY;
- startTagInBody(p, token);
- }
- }
- }
- function endTagInTemplate(p, token) {
- if (token.tagID === $.TEMPLATE) {
- templateEndTagInHead(p, token);
- }
- }
- function eofInTemplate(p, token) {
- if (p.openElements.tmplCount > 0) {
- p.openElements.popUntilTagNamePopped($.TEMPLATE);
- p.activeFormattingElements.clearToLastMarker();
- p.tmplInsertionModeStack.shift();
- p._resetInsertionMode();
- p.onEof(token);
- }
- else {
- stopParsing(p, token);
- }
- }
- // The "after body" insertion mode
- //------------------------------------------------------------------
- function startTagAfterBody(p, token) {
- if (token.tagID === $.HTML) {
- startTagInBody(p, token);
- }
- else {
- tokenAfterBody(p, token);
- }
- }
- function endTagAfterBody(p, token) {
- var _a;
- if (token.tagID === $.HTML) {
- if (!p.fragmentContext) {
- p.insertionMode = InsertionMode.AFTER_AFTER_BODY;
- }
- //NOTE: <html> is never popped from the stack, so we need to updated
- //the end location explicitly.
- if (p.options.sourceCodeLocationInfo && p.openElements.tagIDs[0] === $.HTML) {
- p._setEndLocation(p.openElements.items[0], token);
- // Update the body element, if it doesn't have an end tag
- const bodyElement = p.openElements.items[1];
- if (bodyElement && !((_a = p.treeAdapter.getNodeSourceCodeLocation(bodyElement)) === null || _a === void 0 ? void 0 : _a.endTag)) {
- p._setEndLocation(bodyElement, token);
- }
- }
- }
- else {
- tokenAfterBody(p, token);
- }
- }
- function tokenAfterBody(p, token) {
- p.insertionMode = InsertionMode.IN_BODY;
- modeInBody(p, token);
- }
- // The "in frameset" insertion mode
- //------------------------------------------------------------------
- function startTagInFrameset(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.FRAMESET: {
- p._insertElement(token, NS.HTML);
- break;
- }
- case $.FRAME: {
- p._appendElement(token, NS.HTML);
- token.ackSelfClosing = true;
- break;
- }
- case $.NOFRAMES: {
- startTagInHead(p, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- function endTagInFrameset(p, token) {
- if (token.tagID === $.FRAMESET && !p.openElements.isRootHtmlElementCurrent()) {
- p.openElements.pop();
- if (!p.fragmentContext && p.openElements.currentTagId !== $.FRAMESET) {
- p.insertionMode = InsertionMode.AFTER_FRAMESET;
- }
- }
- }
- // The "after frameset" insertion mode
- //------------------------------------------------------------------
- function startTagAfterFrameset(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.NOFRAMES: {
- startTagInHead(p, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- function endTagAfterFrameset(p, token) {
- if (token.tagID === $.HTML) {
- p.insertionMode = InsertionMode.AFTER_AFTER_FRAMESET;
- }
- }
- // The "after after body" insertion mode
- //------------------------------------------------------------------
- function startTagAfterAfterBody(p, token) {
- if (token.tagID === $.HTML) {
- startTagInBody(p, token);
- }
- else {
- tokenAfterAfterBody(p, token);
- }
- }
- function tokenAfterAfterBody(p, token) {
- p.insertionMode = InsertionMode.IN_BODY;
- modeInBody(p, token);
- }
- // The "after after frameset" insertion mode
- //------------------------------------------------------------------
- function startTagAfterAfterFrameset(p, token) {
- switch (token.tagID) {
- case $.HTML: {
- startTagInBody(p, token);
- break;
- }
- case $.NOFRAMES: {
- startTagInHead(p, token);
- break;
- }
- default:
- // Do nothing
- }
- }
- // The rules for parsing tokens in foreign content
- //------------------------------------------------------------------
- function nullCharacterInForeignContent(p, token) {
- token.chars = unicode.REPLACEMENT_CHARACTER;
- p._insertCharacters(token);
- }
- function characterInForeignContent(p, token) {
- p._insertCharacters(token);
- p.framesetOk = false;
- }
- function popUntilHtmlOrIntegrationPoint(p) {
- while (p.treeAdapter.getNamespaceURI(p.openElements.current) !== NS.HTML &&
- p.openElements.currentTagId !== undefined &&
- !p._isIntegrationPoint(p.openElements.currentTagId, p.openElements.current)) {
- p.openElements.pop();
- }
- }
- function startTagInForeignContent(p, token) {
- if (foreignContent.causesExit(token)) {
- popUntilHtmlOrIntegrationPoint(p);
- p._startTagOutsideForeignContent(token);
- }
- else {
- const current = p._getAdjustedCurrentElement();
- const currentNs = p.treeAdapter.getNamespaceURI(current);
- if (currentNs === NS.MATHML) {
- foreignContent.adjustTokenMathMLAttrs(token);
- }
- else if (currentNs === NS.SVG) {
- foreignContent.adjustTokenSVGTagName(token);
- foreignContent.adjustTokenSVGAttrs(token);
- }
- foreignContent.adjustTokenXMLAttrs(token);
- if (token.selfClosing) {
- p._appendElement(token, currentNs);
- }
- else {
- p._insertElement(token, currentNs);
- }
- token.ackSelfClosing = true;
- }
- }
- function endTagInForeignContent(p, token) {
- if (token.tagID === $.P || token.tagID === $.BR) {
- popUntilHtmlOrIntegrationPoint(p);
- p._endTagOutsideForeignContent(token);
- return;
- }
- for (let i = p.openElements.stackTop; i > 0; i--) {
- const element = p.openElements.items[i];
- if (p.treeAdapter.getNamespaceURI(element) === NS.HTML) {
- p._endTagOutsideForeignContent(token);
- break;
- }
- const tagName = p.treeAdapter.getTagName(element);
- if (tagName.toLowerCase() === token.tagName) {
- //NOTE: update the token tag name for `_setEndLocation`.
- token.tagName = tagName;
- p.openElements.shortenToLength(i);
- break;
- }
- }
- }
|