"use client";
import { Tokenizer as Ie, TokenizerMode as d } from "../tokenizer/index.js";
import { OpenElementStack as _e } from "./open-element-stack.js";
import { FormattingElementList as Ae, EntryType as he } from "./formatting-element-list.js";
import { defaultTreeAdapter as fe } from "../tree-adapters/default.js";
import { getDocumentMode as ge, isConforming as Ne } from "../common/doctype.js";
import { isIntegrationPoint as Le, causesExit as ue, adjustTokenMathMLAttrs as Q, adjustTokenSVGTagName as Oe, adjustTokenSVGAttrs as j, adjustTokenXMLAttrs as k } from "../common/foreign-content.js";
import { ERR as l } from "../common/error-codes.js";
import { CODE_POINTS as Ce, REPLACEMENT_CHARACTER as Re } from "../common/unicode.js";
import { getTagID as J, TAG_ID as a, TAG_NAMES as I, NS as o, SPECIAL_ELEMENTS as Me, DOCUMENT_MODE as w, NUMBERED_HEADERS as be, ATTRS as Be } from "../common/html.js";
import { TokenType as T, getTokenAttr as De } from "../common/token.js";
const Se = "hidden", Pe = 8, Fe = 3;
var s;
(function(e) {
  e[e.INITIAL = 0] = "INITIAL", e[e.BEFORE_HTML = 1] = "BEFORE_HTML", e[e.BEFORE_HEAD = 2] = "BEFORE_HEAD", e[e.IN_HEAD = 3] = "IN_HEAD", e[e.IN_HEAD_NO_SCRIPT = 4] = "IN_HEAD_NO_SCRIPT", e[e.AFTER_HEAD = 5] = "AFTER_HEAD", e[e.IN_BODY = 6] = "IN_BODY", e[e.TEXT = 7] = "TEXT", e[e.IN_TABLE = 8] = "IN_TABLE", e[e.IN_TABLE_TEXT = 9] = "IN_TABLE_TEXT", e[e.IN_CAPTION = 10] = "IN_CAPTION", e[e.IN_COLUMN_GROUP = 11] = "IN_COLUMN_GROUP", e[e.IN_TABLE_BODY = 12] = "IN_TABLE_BODY", e[e.IN_ROW = 13] = "IN_ROW", e[e.IN_CELL = 14] = "IN_CELL", e[e.IN_SELECT = 15] = "IN_SELECT", e[e.IN_SELECT_IN_TABLE = 16] = "IN_SELECT_IN_TABLE", e[e.IN_TEMPLATE = 17] = "IN_TEMPLATE", e[e.AFTER_BODY = 18] = "AFTER_BODY", e[e.IN_FRAMESET = 19] = "IN_FRAMESET", e[e.AFTER_FRAMESET = 20] = "AFTER_FRAMESET", e[e.AFTER_AFTER_BODY = 21] = "AFTER_AFTER_BODY", e[e.AFTER_AFTER_FRAMESET = 22] = "AFTER_AFTER_FRAMESET";
})(s || (s = {}));
const He = {
  startLine: -1,
  startCol: -1,
  startOffset: -1,
  endLine: -1,
  endCol: -1,
  endOffset: -1
}, q = /* @__PURE__ */ new Set([a.TABLE, a.TBODY, a.TFOOT, a.THEAD, a.TR]), z = {
  scriptingEnabled: !0,
  sourceCodeLocationInfo: !1,
  treeAdapter: fe,
  onParseError: null
};
class ua {
  constructor(t, n, r = null, i = null) {
    this.fragmentContext = r, this.scriptHandler = i, this.currentToken = null, this.stopped = !1, this.insertionMode = s.INITIAL, this.originalInsertionMode = s.INITIAL, this.headElement = null, this.formElement = null, this.currentNotInHTML = !1, this.tmplInsertionModeStack = [], this.pendingCharacterTokens = [], this.hasNonWhitespacePendingCharacterToken = !1, this.framesetOk = !0, this.skipNextNewLine = !1, this.fosterParentingEnabled = !1, this.options = {
      ...z,
      ...t
    }, this.treeAdapter = this.options.treeAdapter, this.onParseError = this.options.onParseError, this.onParseError && (this.options.sourceCodeLocationInfo = !0), this.document = n ?? this.treeAdapter.createDocument(), this.tokenizer = new Ie(this.options, this), this.activeFormattingElements = new Ae(this.treeAdapter), this.fragmentContextID = r ? J(this.treeAdapter.getTagName(r)) : a.UNKNOWN, this._setContextModes(r ?? this.document, this.fragmentContextID), this.openElements = new _e(this.document, this.treeAdapter, this);
  }
  // API
  static parse(t, n) {
    const r = new this(n);
    return r.tokenizer.write(t, !0), r.document;
  }
  static getFragmentParser(t, n) {
    const r = {
      ...z,
      ...n
    };
    t ?? (t = r.treeAdapter.createElement(I.TEMPLATE, o.HTML, []));
    const i = r.treeAdapter.createElement("documentmock", o.HTML, []), c = new this(r, i, t);
    return c.fragmentContextID === a.TEMPLATE && c.tmplInsertionModeStack.unshift(s.IN_TEMPLATE), c._initTokenizerForFragmentParsing(), c._insertFakeRootElement(), c._resetInsertionMode(), c._findFormInFragmentContext(), c;
  }
  getFragment() {
    const t = this.treeAdapter.getFirstChild(this.document), n = this.treeAdapter.createDocumentFragment();
    return this._adoptNodes(t, n), n;
  }
  //Errors
  /** @internal */
  _err(t, n, r) {
    var i;
    if (!this.onParseError)
      return;
    const c = (i = t.location) !== null && i !== void 0 ? i : He, E = {
      code: n,
      startLine: c.startLine,
      startCol: c.startCol,
      startOffset: c.startOffset,
      endLine: r ? c.startLine : c.endLine,
      endCol: r ? c.startCol : c.endCol,
      endOffset: r ? c.startOffset : c.endOffset
    };
    this.onParseError(E);
  }
  //Stack events
  /** @internal */
  onItemPush(t, n, r) {
    var i, c;
    (c = (i = this.treeAdapter).onItemPush) === null || c === void 0 || c.call(i, t), r && this.openElements.stackTop > 0 && this._setContextModes(t, n);
  }
  /** @internal */
  onItemPop(t, n) {
    var r, i;
    if (this.options.sourceCodeLocationInfo && this._setEndLocation(t, this.currentToken), (i = (r = this.treeAdapter).onItemPop) === null || i === void 0 || i.call(r, t, this.openElements.current), n) {
      let c, E;
      this.openElements.stackTop === 0 && this.fragmentContext ? (c = this.fragmentContext, E = this.fragmentContextID) : { current: c, currentTagId: E } = this.openElements, this._setContextModes(c, E);
    }
  }
  _setContextModes(t, n) {
    const r = t === this.document || t && this.treeAdapter.getNamespaceURI(t) === o.HTML;
    this.currentNotInHTML = !r, this.tokenizer.inForeignNode = !r && t !== void 0 && n !== void 0 && !this._isIntegrationPoint(n, t);
  }
  /** @protected */
  _switchToTextParsing(t, n) {
    this._insertElement(t, o.HTML), this.tokenizer.state = n, this.originalInsertionMode = this.insertionMode, this.insertionMode = s.TEXT;
  }
  switchToPlaintextParsing() {
    this.insertionMode = s.TEXT, this.originalInsertionMode = s.IN_BODY, this.tokenizer.state = d.PLAINTEXT;
  }
  //Fragment parsing
  /** @protected */
  _getAdjustedCurrentElement() {
    return this.openElements.stackTop === 0 && this.fragmentContext ? this.fragmentContext : this.openElements.current;
  }
  /** @protected */
  _findFormInFragmentContext() {
    let t = this.fragmentContext;
    for (; t; ) {
      if (this.treeAdapter.getTagName(t) === I.FORM) {
        this.formElement = t;
        break;
      }
      t = this.treeAdapter.getParentNode(t);
    }
  }
  _initTokenizerForFragmentParsing() {
    if (!(!this.fragmentContext || this.treeAdapter.getNamespaceURI(this.fragmentContext) !== o.HTML))
      switch (this.fragmentContextID) {
        case a.TITLE:
        case a.TEXTAREA: {
          this.tokenizer.state = d.RCDATA;
          break;
        }
        case a.STYLE:
        case a.XMP:
        case a.IFRAME:
        case a.NOEMBED:
        case a.NOFRAMES:
        case a.NOSCRIPT: {
          this.tokenizer.state = d.RAWTEXT;
          break;
        }
        case a.SCRIPT: {
          this.tokenizer.state = d.SCRIPT_DATA;
          break;
        }
        case a.PLAINTEXT: {
          this.tokenizer.state = d.PLAINTEXT;
          break;
        }
      }
  }
  //Tree mutation
  /** @protected */
  _setDocumentType(t) {
    const n = t.name || "", r = t.publicId || "", i = t.systemId || "";
    if (this.treeAdapter.setDocumentType(this.document, n, r, i), t.location) {
      const E = this.treeAdapter.getChildNodes(this.document).find((A) => this.treeAdapter.isDocumentTypeNode(A));
      E && this.treeAdapter.setNodeSourceCodeLocation(E, t.location);
    }
  }
  /** @protected */
  _attachElementToTree(t, n) {
    if (this.options.sourceCodeLocationInfo) {
      const r = n && {
        ...n,
        startTag: n
      };
      this.treeAdapter.setNodeSourceCodeLocation(t, r);
    }
    if (this._shouldFosterParentOnInsertion())
      this._fosterParentElement(t);
    else {
      const r = this.openElements.currentTmplContentOrNode;
      this.treeAdapter.appendChild(r ?? this.document, t);
    }
  }
  /**
   * For self-closing tags. Add an element to the tree, but skip adding it
   * to the stack.
   */
  /** @protected */
  _appendElement(t, n) {
    const r = this.treeAdapter.createElement(t.tagName, n, t.attrs);
    this._attachElementToTree(r, t.location);
  }
  /** @protected */
  _insertElement(t, n) {
    const r = this.treeAdapter.createElement(t.tagName, n, t.attrs);
    this._attachElementToTree(r, t.location), this.openElements.push(r, t.tagID);
  }
  /** @protected */
  _insertFakeElement(t, n) {
    const r = this.treeAdapter.createElement(t, o.HTML, []);
    this._attachElementToTree(r, null), this.openElements.push(r, n);
  }
  /** @protected */
  _insertTemplate(t) {
    const n = this.treeAdapter.createElement(t.tagName, o.HTML, t.attrs), r = this.treeAdapter.createDocumentFragment();
    this.treeAdapter.setTemplateContent(n, r), this._attachElementToTree(n, t.location), this.openElements.push(n, t.tagID), this.options.sourceCodeLocationInfo && this.treeAdapter.setNodeSourceCodeLocation(r, null);
  }
  /** @protected */
  _insertFakeRootElement() {
    const t = this.treeAdapter.createElement(I.HTML, o.HTML, []);
    this.options.sourceCodeLocationInfo && this.treeAdapter.setNodeSourceCodeLocation(t, null), this.treeAdapter.appendChild(this.openElements.current, t), this.openElements.push(t, a.HTML);
  }
  /** @protected */
  _appendCommentNode(t, n) {
    const r = this.treeAdapter.createCommentNode(t.data);
    this.treeAdapter.appendChild(n, r), this.options.sourceCodeLocationInfo && this.treeAdapter.setNodeSourceCodeLocation(r, t.location);
  }
  /** @protected */
  _insertCharacters(t) {
    let n, r;
    if (this._shouldFosterParentOnInsertion() ? ({ parent: n, beforeElement: r } = this._findFosterParentingLocation(), r ? this.treeAdapter.insertTextBefore(n, t.chars, r) : this.treeAdapter.insertText(n, t.chars)) : (n = this.openElements.currentTmplContentOrNode, this.treeAdapter.insertText(n, t.chars)), !t.location)
      return;
    const i = this.treeAdapter.getChildNodes(n), c = r ? i.lastIndexOf(r) : i.length, E = i[c - 1];
    if (this.treeAdapter.getNodeSourceCodeLocation(E)) {
      const { endLine: B, endCol: X, endOffset: de } = t.location;
      this.treeAdapter.updateNodeSourceCodeLocation(E, { endLine: B, endCol: X, endOffset: de });
    } else this.options.sourceCodeLocationInfo && this.treeAdapter.setNodeSourceCodeLocation(E, t.location);
  }
  /** @protected */
  _adoptNodes(t, n) {
    for (let r = this.treeAdapter.getFirstChild(t); r; r = this.treeAdapter.getFirstChild(t))
      this.treeAdapter.detachNode(r), this.treeAdapter.appendChild(n, r);
  }
  /** @protected */
  _setEndLocation(t, n) {
    if (this.treeAdapter.getNodeSourceCodeLocation(t) && n.location) {
      const r = n.location, i = this.treeAdapter.getTagName(t), c = (
        // 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.
        n.type === T.END_TAG && i === n.tagName ? {
          endTag: { ...r },
          endLine: r.endLine,
          endCol: r.endCol,
          endOffset: r.endOffset
        } : {
          endLine: r.startLine,
          endCol: r.startCol,
          endOffset: r.startOffset
        }
      );
      this.treeAdapter.updateNodeSourceCodeLocation(t, c);
    }
  }
  //Token processing
  shouldProcessStartTagTokenInForeignContent(t) {
    if (!this.currentNotInHTML)
      return !1;
    let n, r;
    return this.openElements.stackTop === 0 && this.fragmentContext ? (n = this.fragmentContext, r = this.fragmentContextID) : { current: n, currentTagId: r } = this.openElements, t.tagID === a.SVG && this.treeAdapter.getTagName(n) === I.ANNOTATION_XML && this.treeAdapter.getNamespaceURI(n) === o.MATHML ? !1 : (
      // 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.
      (t.tagID === a.MGLYPH || t.tagID === a.MALIGNMARK) && r !== void 0 && !this._isIntegrationPoint(r, n, o.HTML)
    );
  }
  /** @protected */
  _processToken(t) {
    switch (t.type) {
      case T.CHARACTER: {
        this.onCharacter(t);
        break;
      }
      case T.NULL_CHARACTER: {
        this.onNullCharacter(t);
        break;
      }
      case T.COMMENT: {
        this.onComment(t);
        break;
      }
      case T.DOCTYPE: {
        this.onDoctype(t);
        break;
      }
      case T.START_TAG: {
        this._processStartTag(t);
        break;
      }
      case T.END_TAG: {
        this.onEndTag(t);
        break;
      }
      case T.EOF: {
        this.onEof(t);
        break;
      }
      case T.WHITESPACE_CHARACTER: {
        this.onWhitespaceCharacter(t);
        break;
      }
    }
  }
  //Integration points
  /** @protected */
  _isIntegrationPoint(t, n, r) {
    const i = this.treeAdapter.getNamespaceURI(n), c = this.treeAdapter.getAttrList(n);
    return Le(t, i, c, r);
  }
  //Active formatting elements reconstruction
  /** @protected */
  _reconstructActiveFormattingElements() {
    const t = this.activeFormattingElements.entries.length;
    if (t) {
      const n = this.activeFormattingElements.entries.findIndex((i) => i.type === he.Marker || this.openElements.contains(i.element)), r = n === -1 ? t - 1 : n - 1;
      for (let i = r; i >= 0; i--) {
        const c = this.activeFormattingElements.entries[i];
        this._insertElement(c.token, this.treeAdapter.getNamespaceURI(c.element)), c.element = this.openElements.current;
      }
    }
  }
  //Close elements
  /** @protected */
  _closeTableCell() {
    this.openElements.generateImpliedEndTags(), this.openElements.popUntilTableCellPopped(), this.activeFormattingElements.clearToLastMarker(), this.insertionMode = s.IN_ROW;
  }
  /** @protected */
  _closePElement() {
    this.openElements.generateImpliedEndTagsWithExclusion(a.P), this.openElements.popUntilTagNamePopped(a.P);
  }
  //Insertion modes
  /** @protected */
  _resetInsertionMode() {
    for (let t = this.openElements.stackTop; t >= 0; t--)
      switch (t === 0 && this.fragmentContext ? this.fragmentContextID : this.openElements.tagIDs[t]) {
        case a.TR: {
          this.insertionMode = s.IN_ROW;
          return;
        }
        case a.TBODY:
        case a.THEAD:
        case a.TFOOT: {
          this.insertionMode = s.IN_TABLE_BODY;
          return;
        }
        case a.CAPTION: {
          this.insertionMode = s.IN_CAPTION;
          return;
        }
        case a.COLGROUP: {
          this.insertionMode = s.IN_COLUMN_GROUP;
          return;
        }
        case a.TABLE: {
          this.insertionMode = s.IN_TABLE;
          return;
        }
        case a.BODY: {
          this.insertionMode = s.IN_BODY;
          return;
        }
        case a.FRAMESET: {
          this.insertionMode = s.IN_FRAMESET;
          return;
        }
        case a.SELECT: {
          this._resetInsertionModeForSelect(t);
          return;
        }
        case a.TEMPLATE: {
          this.insertionMode = this.tmplInsertionModeStack[0];
          return;
        }
        case a.HTML: {
          this.insertionMode = this.headElement ? s.AFTER_HEAD : s.BEFORE_HEAD;
          return;
        }
        case a.TD:
        case a.TH: {
          if (t > 0) {
            this.insertionMode = s.IN_CELL;
            return;
          }
          break;
        }
        case a.HEAD: {
          if (t > 0) {
            this.insertionMode = s.IN_HEAD;
            return;
          }
          break;
        }
      }
    this.insertionMode = s.IN_BODY;
  }
  /** @protected */
  _resetInsertionModeForSelect(t) {
    if (t > 0)
      for (let n = t - 1; n > 0; n--) {
        const r = this.openElements.tagIDs[n];
        if (r === a.TEMPLATE)
          break;
        if (r === a.TABLE) {
          this.insertionMode = s.IN_SELECT_IN_TABLE;
          return;
        }
      }
    this.insertionMode = s.IN_SELECT;
  }
  //Foster parenting
  /** @protected */
  _isElementCausesFosterParenting(t) {
    return q.has(t);
  }
  /** @protected */
  _shouldFosterParentOnInsertion() {
    return this.fosterParentingEnabled && this.openElements.currentTagId !== void 0 && this._isElementCausesFosterParenting(this.openElements.currentTagId);
  }
  /** @protected */
  _findFosterParentingLocation() {
    for (let t = this.openElements.stackTop; t >= 0; t--) {
      const n = this.openElements.items[t];
      switch (this.openElements.tagIDs[t]) {
        case a.TEMPLATE: {
          if (this.treeAdapter.getNamespaceURI(n) === o.HTML)
            return { parent: this.treeAdapter.getTemplateContent(n), beforeElement: null };
          break;
        }
        case a.TABLE: {
          const r = this.treeAdapter.getParentNode(n);
          return r ? { parent: r, beforeElement: n } : { parent: this.openElements.items[t - 1], beforeElement: null };
        }
      }
    }
    return { parent: this.openElements.items[0], beforeElement: null };
  }
  /** @protected */
  _fosterParentElement(t) {
    const n = this._findFosterParentingLocation();
    n.beforeElement ? this.treeAdapter.insertBefore(n.parent, t, n.beforeElement) : this.treeAdapter.appendChild(n.parent, t);
  }
  //Special elements
  /** @protected */
  _isSpecialElement(t, n) {
    const r = this.treeAdapter.getNamespaceURI(t);
    return Me[r].has(n);
  }
  /** @internal */
  onCharacter(t) {
    if (this.skipNextNewLine = !1, this.tokenizer.inForeignNode) {
      Ea(this, t);
      return;
    }
    switch (this.insertionMode) {
      case s.INITIAL: {
        g(this, t);
        break;
      }
      case s.BEFORE_HTML: {
        L(this, t);
        break;
      }
      case s.BEFORE_HEAD: {
        u(this, t);
        break;
      }
      case s.IN_HEAD: {
        O(this, t);
        break;
      }
      case s.IN_HEAD_NO_SCRIPT: {
        C(this, t);
        break;
      }
      case s.AFTER_HEAD: {
        R(this, t);
        break;
      }
      case s.IN_BODY:
      case s.IN_CAPTION:
      case s.IN_CELL:
      case s.IN_TEMPLATE: {
        $(this, t);
        break;
      }
      case s.TEXT:
      case s.IN_SELECT:
      case s.IN_SELECT_IN_TABLE: {
        this._insertCharacters(t);
        break;
      }
      case s.IN_TABLE:
      case s.IN_TABLE_BODY:
      case s.IN_ROW: {
        y(this, t);
        break;
      }
      case s.IN_TABLE_TEXT: {
        re(this, t);
        break;
      }
      case s.IN_COLUMN_GROUP: {
        S(this, t);
        break;
      }
      case s.AFTER_BODY: {
        P(this, t);
        break;
      }
      case s.AFTER_AFTER_BODY: {
        D(this, t);
        break;
      }
    }
  }
  /** @internal */
  onNullCharacter(t) {
    if (this.skipNextNewLine = !1, this.tokenizer.inForeignNode) {
      ca(this, t);
      return;
    }
    switch (this.insertionMode) {
      case s.INITIAL: {
        g(this, t);
        break;
      }
      case s.BEFORE_HTML: {
        L(this, t);
        break;
      }
      case s.BEFORE_HEAD: {
        u(this, t);
        break;
      }
      case s.IN_HEAD: {
        O(this, t);
        break;
      }
      case s.IN_HEAD_NO_SCRIPT: {
        C(this, t);
        break;
      }
      case s.AFTER_HEAD: {
        R(this, t);
        break;
      }
      case s.TEXT: {
        this._insertCharacters(t);
        break;
      }
      case s.IN_TABLE:
      case s.IN_TABLE_BODY:
      case s.IN_ROW: {
        y(this, t);
        break;
      }
      case s.IN_COLUMN_GROUP: {
        S(this, t);
        break;
      }
      case s.AFTER_BODY: {
        P(this, t);
        break;
      }
      case s.AFTER_AFTER_BODY: {
        D(this, t);
        break;
      }
    }
  }
  /** @internal */
  onComment(t) {
    if (this.skipNextNewLine = !1, this.currentNotInHTML) {
      Y(this, t);
      return;
    }
    switch (this.insertionMode) {
      case s.INITIAL:
      case s.BEFORE_HTML:
      case s.BEFORE_HEAD:
      case s.IN_HEAD:
      case s.IN_HEAD_NO_SCRIPT:
      case s.AFTER_HEAD:
      case s.IN_BODY:
      case s.IN_TABLE:
      case s.IN_CAPTION:
      case s.IN_COLUMN_GROUP:
      case s.IN_TABLE_BODY:
      case s.IN_ROW:
      case s.IN_CELL:
      case s.IN_SELECT:
      case s.IN_SELECT_IN_TABLE:
      case s.IN_TEMPLATE:
      case s.IN_FRAMESET:
      case s.AFTER_FRAMESET: {
        Y(this, t);
        break;
      }
      case s.IN_TABLE_TEXT: {
        N(this, t);
        break;
      }
      case s.AFTER_BODY: {
        we(this, t);
        break;
      }
      case s.AFTER_AFTER_BODY:
      case s.AFTER_AFTER_FRAMESET: {
        Ge(this, t);
        break;
      }
    }
  }
  /** @internal */
  onDoctype(t) {
    switch (this.skipNextNewLine = !1, this.insertionMode) {
      case s.INITIAL: {
        ve(this, t);
        break;
      }
      case s.BEFORE_HEAD:
      case s.IN_HEAD:
      case s.IN_HEAD_NO_SCRIPT:
      case s.AFTER_HEAD: {
        this._err(t, l.misplacedDoctype);
        break;
      }
      case s.IN_TABLE_TEXT: {
        N(this, t);
        break;
      }
    }
  }
  /** @internal */
  onStartTag(t) {
    this.skipNextNewLine = !1, this.currentToken = t, this._processStartTag(t), t.selfClosing && !t.ackSelfClosing && this._err(t, l.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(t) {
    this.shouldProcessStartTagTokenInForeignContent(t) ? Ta(this, t) : this._startTagOutsideForeignContent(t);
  }
  /** @protected */
  _startTagOutsideForeignContent(t) {
    switch (this.insertionMode) {
      case s.INITIAL: {
        g(this, t);
        break;
      }
      case s.BEFORE_HTML: {
        We(this, t);
        break;
      }
      case s.BEFORE_HEAD: {
        ze(this, t);
        break;
      }
      case s.IN_HEAD: {
        _(this, t);
        break;
      }
      case s.IN_HEAD_NO_SCRIPT: {
        Qe(this, t);
        break;
      }
      case s.AFTER_HEAD: {
        Je(this, t);
        break;
      }
      case s.IN_BODY: {
        m(this, t);
        break;
      }
      case s.IN_TABLE: {
        f(this, t);
        break;
      }
      case s.IN_TABLE_TEXT: {
        N(this, t);
        break;
      }
      case s.IN_CAPTION: {
        Kt(this, t);
        break;
      }
      case s.IN_COLUMN_GROUP: {
        W(this, t);
        break;
      }
      case s.IN_TABLE_BODY: {
        p(this, t);
        break;
      }
      case s.IN_ROW: {
        U(this, t);
        break;
      }
      case s.IN_CELL: {
        jt(this, t);
        break;
      }
      case s.IN_SELECT: {
        ce(this, t);
        break;
      }
      case s.IN_SELECT_IN_TABLE: {
        qt(this, t);
        break;
      }
      case s.IN_TEMPLATE: {
        $t(this, t);
        break;
      }
      case s.AFTER_BODY: {
        ta(this, t);
        break;
      }
      case s.IN_FRAMESET: {
        aa(this, t);
        break;
      }
      case s.AFTER_FRAMESET: {
        na(this, t);
        break;
      }
      case s.AFTER_AFTER_BODY: {
        ia(this, t);
        break;
      }
      case s.AFTER_AFTER_FRAMESET: {
        oa(this, t);
        break;
      }
    }
  }
  /** @internal */
  onEndTag(t) {
    this.skipNextNewLine = !1, this.currentToken = t, this.currentNotInHTML ? la(this, t) : this._endTagOutsideForeignContent(t);
  }
  /** @protected */
  _endTagOutsideForeignContent(t) {
    switch (this.insertionMode) {
      case s.INITIAL: {
        g(this, t);
        break;
      }
      case s.BEFORE_HTML: {
        Xe(this, t);
        break;
      }
      case s.BEFORE_HEAD: {
        Ke(this, t);
        break;
      }
      case s.IN_HEAD: {
        Ve(this, t);
        break;
      }
      case s.IN_HEAD_NO_SCRIPT: {
        je(this, t);
        break;
      }
      case s.AFTER_HEAD: {
        qe(this, t);
        break;
      }
      case s.IN_BODY: {
        H(this, t);
        break;
      }
      case s.TEXT: {
        yt(this, t);
        break;
      }
      case s.IN_TABLE: {
        M(this, t);
        break;
      }
      case s.IN_TABLE_TEXT: {
        N(this, t);
        break;
      }
      case s.IN_CAPTION: {
        Vt(this, t);
        break;
      }
      case s.IN_COLUMN_GROUP: {
        Qt(this, t);
        break;
      }
      case s.IN_TABLE_BODY: {
        x(this, t);
        break;
      }
      case s.IN_ROW: {
        oe(this, t);
        break;
      }
      case s.IN_CELL: {
        Jt(this, t);
        break;
      }
      case s.IN_SELECT: {
        Ee(this, t);
        break;
      }
      case s.IN_SELECT_IN_TABLE: {
        Zt(this, t);
        break;
      }
      case s.IN_TEMPLATE: {
        ea(this, t);
        break;
      }
      case s.AFTER_BODY: {
        le(this, t);
        break;
      }
      case s.IN_FRAMESET: {
        sa(this, t);
        break;
      }
      case s.AFTER_FRAMESET: {
        ra(this, t);
        break;
      }
      case s.AFTER_AFTER_BODY: {
        D(this, t);
        break;
      }
    }
  }
  /** @internal */
  onEof(t) {
    switch (this.insertionMode) {
      case s.INITIAL: {
        g(this, t);
        break;
      }
      case s.BEFORE_HTML: {
        L(this, t);
        break;
      }
      case s.BEFORE_HEAD: {
        u(this, t);
        break;
      }
      case s.IN_HEAD: {
        O(this, t);
        break;
      }
      case s.IN_HEAD_NO_SCRIPT: {
        C(this, t);
        break;
      }
      case s.AFTER_HEAD: {
        R(this, t);
        break;
      }
      case s.IN_BODY:
      case s.IN_TABLE:
      case s.IN_CAPTION:
      case s.IN_COLUMN_GROUP:
      case s.IN_TABLE_BODY:
      case s.IN_ROW:
      case s.IN_CELL:
      case s.IN_SELECT:
      case s.IN_SELECT_IN_TABLE: {
        se(this, t);
        break;
      }
      case s.TEXT: {
        Yt(this, t);
        break;
      }
      case s.IN_TABLE_TEXT: {
        N(this, t);
        break;
      }
      case s.IN_TEMPLATE: {
        Te(this, t);
        break;
      }
      case s.AFTER_BODY:
      case s.IN_FRAMESET:
      case s.AFTER_FRAMESET:
      case s.AFTER_AFTER_BODY:
      case s.AFTER_AFTER_FRAMESET: {
        v(this, t);
        break;
      }
    }
  }
  /** @internal */
  onWhitespaceCharacter(t) {
    if (this.skipNextNewLine && (this.skipNextNewLine = !1, t.chars.charCodeAt(0) === Ce.LINE_FEED)) {
      if (t.chars.length === 1)
        return;
      t.chars = t.chars.substr(1);
    }
    if (this.tokenizer.inForeignNode) {
      this._insertCharacters(t);
      return;
    }
    switch (this.insertionMode) {
      case s.IN_HEAD:
      case s.IN_HEAD_NO_SCRIPT:
      case s.AFTER_HEAD:
      case s.TEXT:
      case s.IN_COLUMN_GROUP:
      case s.IN_SELECT:
      case s.IN_SELECT_IN_TABLE:
      case s.IN_FRAMESET:
      case s.AFTER_FRAMESET: {
        this._insertCharacters(t);
        break;
      }
      case s.IN_BODY:
      case s.IN_CAPTION:
      case s.IN_CELL:
      case s.IN_TEMPLATE:
      case s.AFTER_BODY:
      case s.AFTER_AFTER_BODY:
      case s.AFTER_AFTER_FRAMESET: {
        Z(this, t);
        break;
      }
      case s.IN_TABLE:
      case s.IN_TABLE_BODY:
      case s.IN_ROW: {
        y(this, t);
        break;
      }
      case s.IN_TABLE_TEXT: {
        ne(this, t);
        break;
      }
    }
  }
}
function pe(e, t) {
  let n = e.activeFormattingElements.getElementEntryInScopeWithTagName(t.tagName);
  return n ? e.openElements.contains(n.element) ? e.openElements.hasInScope(t.tagID) || (n = null) : (e.activeFormattingElements.removeEntry(n), n = null) : ae(e, t), n;
}
function Ue(e, t) {
  let n = null, r = e.openElements.stackTop;
  for (; r >= 0; r--) {
    const i = e.openElements.items[r];
    if (i === t.element)
      break;
    e._isSpecialElement(i, e.openElements.tagIDs[r]) && (n = i);
  }
  return n || (e.openElements.shortenToLength(Math.max(r, 0)), e.activeFormattingElements.removeEntry(t)), n;
}
function ye(e, t, n) {
  let r = t, i = e.openElements.getCommonAncestor(t);
  for (let c = 0, E = i; E !== n; c++, E = i) {
    i = e.openElements.getCommonAncestor(E);
    const A = e.activeFormattingElements.getElementEntry(E), B = A && c >= Fe;
    !A || B ? (B && e.activeFormattingElements.removeEntry(A), e.openElements.remove(E)) : (E = Ye(e, A), r === t && (e.activeFormattingElements.bookmark = A), e.treeAdapter.detachNode(r), e.treeAdapter.appendChild(E, r), r = E);
  }
  return r;
}
function Ye(e, t) {
  const n = e.treeAdapter.getNamespaceURI(t.element), r = e.treeAdapter.createElement(t.token.tagName, n, t.token.attrs);
  return e.openElements.replace(t.element, r), t.element = r, r;
}
function xe(e, t, n) {
  const r = e.treeAdapter.getTagName(t), i = J(r);
  if (e._isElementCausesFosterParenting(i))
    e._fosterParentElement(n);
  else {
    const c = e.treeAdapter.getNamespaceURI(t);
    i === a.TEMPLATE && c === o.HTML && (t = e.treeAdapter.getTemplateContent(t)), e.treeAdapter.appendChild(t, n);
  }
}
function ke(e, t, n) {
  const r = e.treeAdapter.getNamespaceURI(n.element), { token: i } = n, c = e.treeAdapter.createElement(i.tagName, r, i.attrs);
  e._adoptNodes(t, c), e.treeAdapter.appendChild(t, c), e.activeFormattingElements.insertElementAfterBookmark(c, i), e.activeFormattingElements.removeEntry(n), e.openElements.remove(n.element), e.openElements.insertAfter(t, c, i.tagID);
}
function G(e, t) {
  for (let n = 0; n < Pe; n++) {
    const r = pe(e, t);
    if (!r)
      break;
    const i = Ue(e, r);
    if (!i)
      break;
    e.activeFormattingElements.bookmark = r;
    const c = ye(e, i, r.element), E = e.openElements.getCommonAncestor(r.element);
    e.treeAdapter.detachNode(c), E && xe(e, E, c), ke(e, i, r);
  }
}
function Y(e, t) {
  e._appendCommentNode(t, e.openElements.currentTmplContentOrNode);
}
function we(e, t) {
  e._appendCommentNode(t, e.openElements.items[0]);
}
function Ge(e, t) {
  e._appendCommentNode(t, e.document);
}
function v(e, t) {
  if (e.stopped = !0, t.location) {
    const n = e.fragmentContext ? 0 : 2;
    for (let r = e.openElements.stackTop; r >= n; r--)
      e._setEndLocation(e.openElements.items[r], t);
    if (!e.fragmentContext && e.openElements.stackTop >= 0) {
      const r = e.openElements.items[0], i = e.treeAdapter.getNodeSourceCodeLocation(r);
      if (i && !i.endTag && (e._setEndLocation(r, t), e.openElements.stackTop >= 1)) {
        const c = e.openElements.items[1], E = e.treeAdapter.getNodeSourceCodeLocation(c);
        E && !E.endTag && e._setEndLocation(c, t);
      }
    }
  }
}
function ve(e, t) {
  e._setDocumentType(t);
  const n = t.forceQuirks ? w.QUIRKS : ge(t);
  Ne(t) || e._err(t, l.nonConformingDoctype), e.treeAdapter.setDocumentMode(e.document, n), e.insertionMode = s.BEFORE_HTML;
}
function g(e, t) {
  e._err(t, l.missingDoctype, !0), e.treeAdapter.setDocumentMode(e.document, w.QUIRKS), e.insertionMode = s.BEFORE_HTML, e._processToken(t);
}
function We(e, t) {
  t.tagID === a.HTML ? (e._insertElement(t, o.HTML), e.insertionMode = s.BEFORE_HEAD) : L(e, t);
}
function Xe(e, t) {
  const n = t.tagID;
  (n === a.HTML || n === a.HEAD || n === a.BODY || n === a.BR) && L(e, t);
}
function L(e, t) {
  e._insertFakeRootElement(), e.insertionMode = s.BEFORE_HEAD, e._processToken(t);
}
function ze(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.HEAD: {
      e._insertElement(t, o.HTML), e.headElement = e.openElements.current, e.insertionMode = s.IN_HEAD;
      break;
    }
    default:
      u(e, t);
  }
}
function Ke(e, t) {
  const n = t.tagID;
  n === a.HEAD || n === a.BODY || n === a.HTML || n === a.BR ? u(e, t) : e._err(t, l.endTagWithoutMatchingOpenElement);
}
function u(e, t) {
  e._insertFakeElement(I.HEAD, a.HEAD), e.headElement = e.openElements.current, e.insertionMode = s.IN_HEAD, e._processToken(t);
}
function _(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.BASE:
    case a.BASEFONT:
    case a.BGSOUND:
    case a.LINK:
    case a.META: {
      e._appendElement(t, o.HTML), t.ackSelfClosing = !0;
      break;
    }
    case a.TITLE: {
      e._switchToTextParsing(t, d.RCDATA);
      break;
    }
    case a.NOSCRIPT: {
      e.options.scriptingEnabled ? e._switchToTextParsing(t, d.RAWTEXT) : (e._insertElement(t, o.HTML), e.insertionMode = s.IN_HEAD_NO_SCRIPT);
      break;
    }
    case a.NOFRAMES:
    case a.STYLE: {
      e._switchToTextParsing(t, d.RAWTEXT);
      break;
    }
    case a.SCRIPT: {
      e._switchToTextParsing(t, d.SCRIPT_DATA);
      break;
    }
    case a.TEMPLATE: {
      e._insertTemplate(t), e.activeFormattingElements.insertMarker(), e.framesetOk = !1, e.insertionMode = s.IN_TEMPLATE, e.tmplInsertionModeStack.unshift(s.IN_TEMPLATE);
      break;
    }
    case a.HEAD: {
      e._err(t, l.misplacedStartTagForHeadElement);
      break;
    }
    default:
      O(e, t);
  }
}
function Ve(e, t) {
  switch (t.tagID) {
    case a.HEAD: {
      e.openElements.pop(), e.insertionMode = s.AFTER_HEAD;
      break;
    }
    case a.BODY:
    case a.BR:
    case a.HTML: {
      O(e, t);
      break;
    }
    case a.TEMPLATE: {
      h(e, t);
      break;
    }
    default:
      e._err(t, l.endTagWithoutMatchingOpenElement);
  }
}
function h(e, t) {
  e.openElements.tmplCount > 0 ? (e.openElements.generateImpliedEndTagsThoroughly(), e.openElements.currentTagId !== a.TEMPLATE && e._err(t, l.closingOfElementWithOpenChildElements), e.openElements.popUntilTagNamePopped(a.TEMPLATE), e.activeFormattingElements.clearToLastMarker(), e.tmplInsertionModeStack.shift(), e._resetInsertionMode()) : e._err(t, l.endTagWithoutMatchingOpenElement);
}
function O(e, t) {
  e.openElements.pop(), e.insertionMode = s.AFTER_HEAD, e._processToken(t);
}
function Qe(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.BASEFONT:
    case a.BGSOUND:
    case a.HEAD:
    case a.LINK:
    case a.META:
    case a.NOFRAMES:
    case a.STYLE: {
      _(e, t);
      break;
    }
    case a.NOSCRIPT: {
      e._err(t, l.nestedNoscriptInHead);
      break;
    }
    default:
      C(e, t);
  }
}
function je(e, t) {
  switch (t.tagID) {
    case a.NOSCRIPT: {
      e.openElements.pop(), e.insertionMode = s.IN_HEAD;
      break;
    }
    case a.BR: {
      C(e, t);
      break;
    }
    default:
      e._err(t, l.endTagWithoutMatchingOpenElement);
  }
}
function C(e, t) {
  const n = t.type === T.EOF ? l.openElementsLeftAfterEof : l.disallowedContentInNoscriptInHead;
  e._err(t, n), e.openElements.pop(), e.insertionMode = s.IN_HEAD, e._processToken(t);
}
function Je(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.BODY: {
      e._insertElement(t, o.HTML), e.framesetOk = !1, e.insertionMode = s.IN_BODY;
      break;
    }
    case a.FRAMESET: {
      e._insertElement(t, o.HTML), e.insertionMode = s.IN_FRAMESET;
      break;
    }
    case a.BASE:
    case a.BASEFONT:
    case a.BGSOUND:
    case a.LINK:
    case a.META:
    case a.NOFRAMES:
    case a.SCRIPT:
    case a.STYLE:
    case a.TEMPLATE:
    case a.TITLE: {
      e._err(t, l.abandonedHeadElementChild), e.openElements.push(e.headElement, a.HEAD), _(e, t), e.openElements.remove(e.headElement);
      break;
    }
    case a.HEAD: {
      e._err(t, l.misplacedStartTagForHeadElement);
      break;
    }
    default:
      R(e, t);
  }
}
function qe(e, t) {
  switch (t.tagID) {
    case a.BODY:
    case a.HTML:
    case a.BR: {
      R(e, t);
      break;
    }
    case a.TEMPLATE: {
      h(e, t);
      break;
    }
    default:
      e._err(t, l.endTagWithoutMatchingOpenElement);
  }
}
function R(e, t) {
  e._insertFakeElement(I.BODY, a.BODY), e.insertionMode = s.IN_BODY, F(e, t);
}
function F(e, t) {
  switch (t.type) {
    case T.CHARACTER: {
      $(e, t);
      break;
    }
    case T.WHITESPACE_CHARACTER: {
      Z(e, t);
      break;
    }
    case T.COMMENT: {
      Y(e, t);
      break;
    }
    case T.START_TAG: {
      m(e, t);
      break;
    }
    case T.END_TAG: {
      H(e, t);
      break;
    }
    case T.EOF: {
      se(e, t);
      break;
    }
  }
}
function Z(e, t) {
  e._reconstructActiveFormattingElements(), e._insertCharacters(t);
}
function $(e, t) {
  e._reconstructActiveFormattingElements(), e._insertCharacters(t), e.framesetOk = !1;
}
function Ze(e, t) {
  e.openElements.tmplCount === 0 && e.treeAdapter.adoptAttributes(e.openElements.items[0], t.attrs);
}
function $e(e, t) {
  const n = e.openElements.tryPeekProperlyNestedBodyElement();
  n && e.openElements.tmplCount === 0 && (e.framesetOk = !1, e.treeAdapter.adoptAttributes(n, t.attrs));
}
function et(e, t) {
  const n = e.openElements.tryPeekProperlyNestedBodyElement();
  e.framesetOk && n && (e.treeAdapter.detachNode(n), e.openElements.popAllUpToHtmlElement(), e._insertElement(t, o.HTML), e.insertionMode = s.IN_FRAMESET);
}
function tt(e, t) {
  e.openElements.hasInButtonScope(a.P) && e._closePElement(), e._insertElement(t, o.HTML);
}
function at(e, t) {
  e.openElements.hasInButtonScope(a.P) && e._closePElement(), e.openElements.currentTagId !== void 0 && be.has(e.openElements.currentTagId) && e.openElements.pop(), e._insertElement(t, o.HTML);
}
function st(e, t) {
  e.openElements.hasInButtonScope(a.P) && e._closePElement(), e._insertElement(t, o.HTML), e.skipNextNewLine = !0, e.framesetOk = !1;
}
function nt(e, t) {
  const n = e.openElements.tmplCount > 0;
  (!e.formElement || n) && (e.openElements.hasInButtonScope(a.P) && e._closePElement(), e._insertElement(t, o.HTML), n || (e.formElement = e.openElements.current));
}
function rt(e, t) {
  e.framesetOk = !1;
  const n = t.tagID;
  for (let r = e.openElements.stackTop; r >= 0; r--) {
    const i = e.openElements.tagIDs[r];
    if (n === a.LI && i === a.LI || (n === a.DD || n === a.DT) && (i === a.DD || i === a.DT)) {
      e.openElements.generateImpliedEndTagsWithExclusion(i), e.openElements.popUntilTagNamePopped(i);
      break;
    }
    if (i !== a.ADDRESS && i !== a.DIV && i !== a.P && e._isSpecialElement(e.openElements.items[r], i))
      break;
  }
  e.openElements.hasInButtonScope(a.P) && e._closePElement(), e._insertElement(t, o.HTML);
}
function it(e, t) {
  e.openElements.hasInButtonScope(a.P) && e._closePElement(), e._insertElement(t, o.HTML), e.tokenizer.state = d.PLAINTEXT;
}
function ot(e, t) {
  e.openElements.hasInScope(a.BUTTON) && (e.openElements.generateImpliedEndTags(), e.openElements.popUntilTagNamePopped(a.BUTTON)), e._reconstructActiveFormattingElements(), e._insertElement(t, o.HTML), e.framesetOk = !1;
}
function ct(e, t) {
  const n = e.activeFormattingElements.getElementEntryInScopeWithTagName(I.A);
  n && (G(e, t), e.openElements.remove(n.element), e.activeFormattingElements.removeEntry(n)), e._reconstructActiveFormattingElements(), e._insertElement(t, o.HTML), e.activeFormattingElements.pushElement(e.openElements.current, t);
}
function Et(e, t) {
  e._reconstructActiveFormattingElements(), e._insertElement(t, o.HTML), e.activeFormattingElements.pushElement(e.openElements.current, t);
}
function Tt(e, t) {
  e._reconstructActiveFormattingElements(), e.openElements.hasInScope(a.NOBR) && (G(e, t), e._reconstructActiveFormattingElements()), e._insertElement(t, o.HTML), e.activeFormattingElements.pushElement(e.openElements.current, t);
}
function lt(e, t) {
  e._reconstructActiveFormattingElements(), e._insertElement(t, o.HTML), e.activeFormattingElements.insertMarker(), e.framesetOk = !1;
}
function mt(e, t) {
  e.treeAdapter.getDocumentMode(e.document) !== w.QUIRKS && e.openElements.hasInButtonScope(a.P) && e._closePElement(), e._insertElement(t, o.HTML), e.framesetOk = !1, e.insertionMode = s.IN_TABLE;
}
function ee(e, t) {
  e._reconstructActiveFormattingElements(), e._appendElement(t, o.HTML), e.framesetOk = !1, t.ackSelfClosing = !0;
}
function te(e) {
  const t = De(e, Be.TYPE);
  return t != null && t.toLowerCase() === Se;
}
function dt(e, t) {
  e._reconstructActiveFormattingElements(), e._appendElement(t, o.HTML), te(t) || (e.framesetOk = !1), t.ackSelfClosing = !0;
}
function It(e, t) {
  e._appendElement(t, o.HTML), t.ackSelfClosing = !0;
}
function _t(e, t) {
  e.openElements.hasInButtonScope(a.P) && e._closePElement(), e._appendElement(t, o.HTML), e.framesetOk = !1, t.ackSelfClosing = !0;
}
function At(e, t) {
  t.tagName = I.IMG, t.tagID = a.IMG, ee(e, t);
}
function ht(e, t) {
  e._insertElement(t, o.HTML), e.skipNextNewLine = !0, e.tokenizer.state = d.RCDATA, e.originalInsertionMode = e.insertionMode, e.framesetOk = !1, e.insertionMode = s.TEXT;
}
function ft(e, t) {
  e.openElements.hasInButtonScope(a.P) && e._closePElement(), e._reconstructActiveFormattingElements(), e.framesetOk = !1, e._switchToTextParsing(t, d.RAWTEXT);
}
function gt(e, t) {
  e.framesetOk = !1, e._switchToTextParsing(t, d.RAWTEXT);
}
function K(e, t) {
  e._switchToTextParsing(t, d.RAWTEXT);
}
function Nt(e, t) {
  e._reconstructActiveFormattingElements(), e._insertElement(t, o.HTML), e.framesetOk = !1, e.insertionMode = e.insertionMode === s.IN_TABLE || e.insertionMode === s.IN_CAPTION || e.insertionMode === s.IN_TABLE_BODY || e.insertionMode === s.IN_ROW || e.insertionMode === s.IN_CELL ? s.IN_SELECT_IN_TABLE : s.IN_SELECT;
}
function Lt(e, t) {
  e.openElements.currentTagId === a.OPTION && e.openElements.pop(), e._reconstructActiveFormattingElements(), e._insertElement(t, o.HTML);
}
function ut(e, t) {
  e.openElements.hasInScope(a.RUBY) && e.openElements.generateImpliedEndTags(), e._insertElement(t, o.HTML);
}
function Ot(e, t) {
  e.openElements.hasInScope(a.RUBY) && e.openElements.generateImpliedEndTagsWithExclusion(a.RTC), e._insertElement(t, o.HTML);
}
function Ct(e, t) {
  e._reconstructActiveFormattingElements(), Q(t), k(t), t.selfClosing ? e._appendElement(t, o.MATHML) : e._insertElement(t, o.MATHML), t.ackSelfClosing = !0;
}
function Rt(e, t) {
  e._reconstructActiveFormattingElements(), j(t), k(t), t.selfClosing ? e._appendElement(t, o.SVG) : e._insertElement(t, o.SVG), t.ackSelfClosing = !0;
}
function V(e, t) {
  e._reconstructActiveFormattingElements(), e._insertElement(t, o.HTML);
}
function m(e, t) {
  switch (t.tagID) {
    case a.I:
    case a.S:
    case a.B:
    case a.U:
    case a.EM:
    case a.TT:
    case a.BIG:
    case a.CODE:
    case a.FONT:
    case a.SMALL:
    case a.STRIKE:
    case a.STRONG: {
      Et(e, t);
      break;
    }
    case a.A: {
      ct(e, t);
      break;
    }
    case a.H1:
    case a.H2:
    case a.H3:
    case a.H4:
    case a.H5:
    case a.H6: {
      at(e, t);
      break;
    }
    case a.P:
    case a.DL:
    case a.OL:
    case a.UL:
    case a.DIV:
    case a.DIR:
    case a.NAV:
    case a.MAIN:
    case a.MENU:
    case a.ASIDE:
    case a.CENTER:
    case a.FIGURE:
    case a.FOOTER:
    case a.HEADER:
    case a.HGROUP:
    case a.DIALOG:
    case a.DETAILS:
    case a.ADDRESS:
    case a.ARTICLE:
    case a.SEARCH:
    case a.SECTION:
    case a.SUMMARY:
    case a.FIELDSET:
    case a.BLOCKQUOTE:
    case a.FIGCAPTION: {
      tt(e, t);
      break;
    }
    case a.LI:
    case a.DD:
    case a.DT: {
      rt(e, t);
      break;
    }
    case a.BR:
    case a.IMG:
    case a.WBR:
    case a.AREA:
    case a.EMBED:
    case a.KEYGEN: {
      ee(e, t);
      break;
    }
    case a.HR: {
      _t(e, t);
      break;
    }
    case a.RB:
    case a.RTC: {
      ut(e, t);
      break;
    }
    case a.RT:
    case a.RP: {
      Ot(e, t);
      break;
    }
    case a.PRE:
    case a.LISTING: {
      st(e, t);
      break;
    }
    case a.XMP: {
      ft(e, t);
      break;
    }
    case a.SVG: {
      Rt(e, t);
      break;
    }
    case a.HTML: {
      Ze(e, t);
      break;
    }
    case a.BASE:
    case a.LINK:
    case a.META:
    case a.STYLE:
    case a.TITLE:
    case a.SCRIPT:
    case a.BGSOUND:
    case a.BASEFONT:
    case a.TEMPLATE: {
      _(e, t);
      break;
    }
    case a.BODY: {
      $e(e, t);
      break;
    }
    case a.FORM: {
      nt(e, t);
      break;
    }
    case a.NOBR: {
      Tt(e, t);
      break;
    }
    case a.MATH: {
      Ct(e, t);
      break;
    }
    case a.TABLE: {
      mt(e, t);
      break;
    }
    case a.INPUT: {
      dt(e, t);
      break;
    }
    case a.PARAM:
    case a.TRACK:
    case a.SOURCE: {
      It(e, t);
      break;
    }
    case a.IMAGE: {
      At(e, t);
      break;
    }
    case a.BUTTON: {
      ot(e, t);
      break;
    }
    case a.APPLET:
    case a.OBJECT:
    case a.MARQUEE: {
      lt(e, t);
      break;
    }
    case a.IFRAME: {
      gt(e, t);
      break;
    }
    case a.SELECT: {
      Nt(e, t);
      break;
    }
    case a.OPTION:
    case a.OPTGROUP: {
      Lt(e, t);
      break;
    }
    case a.NOEMBED:
    case a.NOFRAMES: {
      K(e, t);
      break;
    }
    case a.FRAMESET: {
      et(e, t);
      break;
    }
    case a.TEXTAREA: {
      ht(e, t);
      break;
    }
    case a.NOSCRIPT: {
      e.options.scriptingEnabled ? K(e, t) : V(e, t);
      break;
    }
    case a.PLAINTEXT: {
      it(e, t);
      break;
    }
    case a.COL:
    case a.TH:
    case a.TD:
    case a.TR:
    case a.HEAD:
    case a.FRAME:
    case a.TBODY:
    case a.TFOOT:
    case a.THEAD:
    case a.CAPTION:
    case a.COLGROUP:
      break;
    default:
      V(e, t);
  }
}
function Mt(e, t) {
  if (e.openElements.hasInScope(a.BODY) && (e.insertionMode = s.AFTER_BODY, e.options.sourceCodeLocationInfo)) {
    const n = e.openElements.tryPeekProperlyNestedBodyElement();
    n && e._setEndLocation(n, t);
  }
}
function bt(e, t) {
  e.openElements.hasInScope(a.BODY) && (e.insertionMode = s.AFTER_BODY, le(e, t));
}
function Bt(e, t) {
  const n = t.tagID;
  e.openElements.hasInScope(n) && (e.openElements.generateImpliedEndTags(), e.openElements.popUntilTagNamePopped(n));
}
function Dt(e) {
  const t = e.openElements.tmplCount > 0, { formElement: n } = e;
  t || (e.formElement = null), (n || t) && e.openElements.hasInScope(a.FORM) && (e.openElements.generateImpliedEndTags(), t ? e.openElements.popUntilTagNamePopped(a.FORM) : n && e.openElements.remove(n));
}
function St(e) {
  e.openElements.hasInButtonScope(a.P) || e._insertFakeElement(I.P, a.P), e._closePElement();
}
function Pt(e) {
  e.openElements.hasInListItemScope(a.LI) && (e.openElements.generateImpliedEndTagsWithExclusion(a.LI), e.openElements.popUntilTagNamePopped(a.LI));
}
function Ft(e, t) {
  const n = t.tagID;
  e.openElements.hasInScope(n) && (e.openElements.generateImpliedEndTagsWithExclusion(n), e.openElements.popUntilTagNamePopped(n));
}
function Ht(e) {
  e.openElements.hasNumberedHeaderInScope() && (e.openElements.generateImpliedEndTags(), e.openElements.popUntilNumberedHeaderPopped());
}
function pt(e, t) {
  const n = t.tagID;
  e.openElements.hasInScope(n) && (e.openElements.generateImpliedEndTags(), e.openElements.popUntilTagNamePopped(n), e.activeFormattingElements.clearToLastMarker());
}
function Ut(e) {
  e._reconstructActiveFormattingElements(), e._insertFakeElement(I.BR, a.BR), e.openElements.pop(), e.framesetOk = !1;
}
function ae(e, t) {
  const n = t.tagName, r = t.tagID;
  for (let i = e.openElements.stackTop; i > 0; i--) {
    const c = e.openElements.items[i], E = e.openElements.tagIDs[i];
    if (r === E && (r !== a.UNKNOWN || e.treeAdapter.getTagName(c) === n)) {
      e.openElements.generateImpliedEndTagsWithExclusion(r), e.openElements.stackTop >= i && e.openElements.shortenToLength(i);
      break;
    }
    if (e._isSpecialElement(c, E))
      break;
  }
}
function H(e, t) {
  switch (t.tagID) {
    case a.A:
    case a.B:
    case a.I:
    case a.S:
    case a.U:
    case a.EM:
    case a.TT:
    case a.BIG:
    case a.CODE:
    case a.FONT:
    case a.NOBR:
    case a.SMALL:
    case a.STRIKE:
    case a.STRONG: {
      G(e, t);
      break;
    }
    case a.P: {
      St(e);
      break;
    }
    case a.DL:
    case a.UL:
    case a.OL:
    case a.DIR:
    case a.DIV:
    case a.NAV:
    case a.PRE:
    case a.MAIN:
    case a.MENU:
    case a.ASIDE:
    case a.BUTTON:
    case a.CENTER:
    case a.FIGURE:
    case a.FOOTER:
    case a.HEADER:
    case a.HGROUP:
    case a.DIALOG:
    case a.ADDRESS:
    case a.ARTICLE:
    case a.DETAILS:
    case a.SEARCH:
    case a.SECTION:
    case a.SUMMARY:
    case a.LISTING:
    case a.FIELDSET:
    case a.BLOCKQUOTE:
    case a.FIGCAPTION: {
      Bt(e, t);
      break;
    }
    case a.LI: {
      Pt(e);
      break;
    }
    case a.DD:
    case a.DT: {
      Ft(e, t);
      break;
    }
    case a.H1:
    case a.H2:
    case a.H3:
    case a.H4:
    case a.H5:
    case a.H6: {
      Ht(e);
      break;
    }
    case a.BR: {
      Ut(e);
      break;
    }
    case a.BODY: {
      Mt(e, t);
      break;
    }
    case a.HTML: {
      bt(e, t);
      break;
    }
    case a.FORM: {
      Dt(e);
      break;
    }
    case a.APPLET:
    case a.OBJECT:
    case a.MARQUEE: {
      pt(e, t);
      break;
    }
    case a.TEMPLATE: {
      h(e, t);
      break;
    }
    default:
      ae(e, t);
  }
}
function se(e, t) {
  e.tmplInsertionModeStack.length > 0 ? Te(e, t) : v(e, t);
}
function yt(e, t) {
  var n;
  t.tagID === a.SCRIPT && ((n = e.scriptHandler) === null || n === void 0 || n.call(e, e.openElements.current)), e.openElements.pop(), e.insertionMode = e.originalInsertionMode;
}
function Yt(e, t) {
  e._err(t, l.eofInElementThatCanContainOnlyText), e.openElements.pop(), e.insertionMode = e.originalInsertionMode, e.onEof(t);
}
function y(e, t) {
  if (e.openElements.currentTagId !== void 0 && q.has(e.openElements.currentTagId))
    switch (e.pendingCharacterTokens.length = 0, e.hasNonWhitespacePendingCharacterToken = !1, e.originalInsertionMode = e.insertionMode, e.insertionMode = s.IN_TABLE_TEXT, t.type) {
      case T.CHARACTER: {
        re(e, t);
        break;
      }
      case T.WHITESPACE_CHARACTER: {
        ne(e, t);
        break;
      }
    }
  else
    b(e, t);
}
function xt(e, t) {
  e.openElements.clearBackToTableContext(), e.activeFormattingElements.insertMarker(), e._insertElement(t, o.HTML), e.insertionMode = s.IN_CAPTION;
}
function kt(e, t) {
  e.openElements.clearBackToTableContext(), e._insertElement(t, o.HTML), e.insertionMode = s.IN_COLUMN_GROUP;
}
function wt(e, t) {
  e.openElements.clearBackToTableContext(), e._insertFakeElement(I.COLGROUP, a.COLGROUP), e.insertionMode = s.IN_COLUMN_GROUP, W(e, t);
}
function Gt(e, t) {
  e.openElements.clearBackToTableContext(), e._insertElement(t, o.HTML), e.insertionMode = s.IN_TABLE_BODY;
}
function vt(e, t) {
  e.openElements.clearBackToTableContext(), e._insertFakeElement(I.TBODY, a.TBODY), e.insertionMode = s.IN_TABLE_BODY, p(e, t);
}
function Wt(e, t) {
  e.openElements.hasInTableScope(a.TABLE) && (e.openElements.popUntilTagNamePopped(a.TABLE), e._resetInsertionMode(), e._processStartTag(t));
}
function Xt(e, t) {
  te(t) ? e._appendElement(t, o.HTML) : b(e, t), t.ackSelfClosing = !0;
}
function zt(e, t) {
  !e.formElement && e.openElements.tmplCount === 0 && (e._insertElement(t, o.HTML), e.formElement = e.openElements.current, e.openElements.pop());
}
function f(e, t) {
  switch (t.tagID) {
    case a.TD:
    case a.TH:
    case a.TR: {
      vt(e, t);
      break;
    }
    case a.STYLE:
    case a.SCRIPT:
    case a.TEMPLATE: {
      _(e, t);
      break;
    }
    case a.COL: {
      wt(e, t);
      break;
    }
    case a.FORM: {
      zt(e, t);
      break;
    }
    case a.TABLE: {
      Wt(e, t);
      break;
    }
    case a.TBODY:
    case a.TFOOT:
    case a.THEAD: {
      Gt(e, t);
      break;
    }
    case a.INPUT: {
      Xt(e, t);
      break;
    }
    case a.CAPTION: {
      xt(e, t);
      break;
    }
    case a.COLGROUP: {
      kt(e, t);
      break;
    }
    default:
      b(e, t);
  }
}
function M(e, t) {
  switch (t.tagID) {
    case a.TABLE: {
      e.openElements.hasInTableScope(a.TABLE) && (e.openElements.popUntilTagNamePopped(a.TABLE), e._resetInsertionMode());
      break;
    }
    case a.TEMPLATE: {
      h(e, t);
      break;
    }
    case a.BODY:
    case a.CAPTION:
    case a.COL:
    case a.COLGROUP:
    case a.HTML:
    case a.TBODY:
    case a.TD:
    case a.TFOOT:
    case a.TH:
    case a.THEAD:
    case a.TR:
      break;
    default:
      b(e, t);
  }
}
function b(e, t) {
  const n = e.fosterParentingEnabled;
  e.fosterParentingEnabled = !0, F(e, t), e.fosterParentingEnabled = n;
}
function ne(e, t) {
  e.pendingCharacterTokens.push(t);
}
function re(e, t) {
  e.pendingCharacterTokens.push(t), e.hasNonWhitespacePendingCharacterToken = !0;
}
function N(e, t) {
  let n = 0;
  if (e.hasNonWhitespacePendingCharacterToken)
    for (; n < e.pendingCharacterTokens.length; n++)
      b(e, e.pendingCharacterTokens[n]);
  else
    for (; n < e.pendingCharacterTokens.length; n++)
      e._insertCharacters(e.pendingCharacterTokens[n]);
  e.insertionMode = e.originalInsertionMode, e._processToken(t);
}
const ie = /* @__PURE__ */ new Set([a.CAPTION, a.COL, a.COLGROUP, a.TBODY, a.TD, a.TFOOT, a.TH, a.THEAD, a.TR]);
function Kt(e, t) {
  const n = t.tagID;
  ie.has(n) ? e.openElements.hasInTableScope(a.CAPTION) && (e.openElements.generateImpliedEndTags(), e.openElements.popUntilTagNamePopped(a.CAPTION), e.activeFormattingElements.clearToLastMarker(), e.insertionMode = s.IN_TABLE, f(e, t)) : m(e, t);
}
function Vt(e, t) {
  const n = t.tagID;
  switch (n) {
    case a.CAPTION:
    case a.TABLE: {
      e.openElements.hasInTableScope(a.CAPTION) && (e.openElements.generateImpliedEndTags(), e.openElements.popUntilTagNamePopped(a.CAPTION), e.activeFormattingElements.clearToLastMarker(), e.insertionMode = s.IN_TABLE, n === a.TABLE && M(e, t));
      break;
    }
    case a.BODY:
    case a.COL:
    case a.COLGROUP:
    case a.HTML:
    case a.TBODY:
    case a.TD:
    case a.TFOOT:
    case a.TH:
    case a.THEAD:
    case a.TR:
      break;
    default:
      H(e, t);
  }
}
function W(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.COL: {
      e._appendElement(t, o.HTML), t.ackSelfClosing = !0;
      break;
    }
    case a.TEMPLATE: {
      _(e, t);
      break;
    }
    default:
      S(e, t);
  }
}
function Qt(e, t) {
  switch (t.tagID) {
    case a.COLGROUP: {
      e.openElements.currentTagId === a.COLGROUP && (e.openElements.pop(), e.insertionMode = s.IN_TABLE);
      break;
    }
    case a.TEMPLATE: {
      h(e, t);
      break;
    }
    case a.COL:
      break;
    default:
      S(e, t);
  }
}
function S(e, t) {
  e.openElements.currentTagId === a.COLGROUP && (e.openElements.pop(), e.insertionMode = s.IN_TABLE, e._processToken(t));
}
function p(e, t) {
  switch (t.tagID) {
    case a.TR: {
      e.openElements.clearBackToTableBodyContext(), e._insertElement(t, o.HTML), e.insertionMode = s.IN_ROW;
      break;
    }
    case a.TH:
    case a.TD: {
      e.openElements.clearBackToTableBodyContext(), e._insertFakeElement(I.TR, a.TR), e.insertionMode = s.IN_ROW, U(e, t);
      break;
    }
    case a.CAPTION:
    case a.COL:
    case a.COLGROUP:
    case a.TBODY:
    case a.TFOOT:
    case a.THEAD: {
      e.openElements.hasTableBodyContextInTableScope() && (e.openElements.clearBackToTableBodyContext(), e.openElements.pop(), e.insertionMode = s.IN_TABLE, f(e, t));
      break;
    }
    default:
      f(e, t);
  }
}
function x(e, t) {
  const n = t.tagID;
  switch (t.tagID) {
    case a.TBODY:
    case a.TFOOT:
    case a.THEAD: {
      e.openElements.hasInTableScope(n) && (e.openElements.clearBackToTableBodyContext(), e.openElements.pop(), e.insertionMode = s.IN_TABLE);
      break;
    }
    case a.TABLE: {
      e.openElements.hasTableBodyContextInTableScope() && (e.openElements.clearBackToTableBodyContext(), e.openElements.pop(), e.insertionMode = s.IN_TABLE, M(e, t));
      break;
    }
    case a.BODY:
    case a.CAPTION:
    case a.COL:
    case a.COLGROUP:
    case a.HTML:
    case a.TD:
    case a.TH:
    case a.TR:
      break;
    default:
      M(e, t);
  }
}
function U(e, t) {
  switch (t.tagID) {
    case a.TH:
    case a.TD: {
      e.openElements.clearBackToTableRowContext(), e._insertElement(t, o.HTML), e.insertionMode = s.IN_CELL, e.activeFormattingElements.insertMarker();
      break;
    }
    case a.CAPTION:
    case a.COL:
    case a.COLGROUP:
    case a.TBODY:
    case a.TFOOT:
    case a.THEAD:
    case a.TR: {
      e.openElements.hasInTableScope(a.TR) && (e.openElements.clearBackToTableRowContext(), e.openElements.pop(), e.insertionMode = s.IN_TABLE_BODY, p(e, t));
      break;
    }
    default:
      f(e, t);
  }
}
function oe(e, t) {
  switch (t.tagID) {
    case a.TR: {
      e.openElements.hasInTableScope(a.TR) && (e.openElements.clearBackToTableRowContext(), e.openElements.pop(), e.insertionMode = s.IN_TABLE_BODY);
      break;
    }
    case a.TABLE: {
      e.openElements.hasInTableScope(a.TR) && (e.openElements.clearBackToTableRowContext(), e.openElements.pop(), e.insertionMode = s.IN_TABLE_BODY, x(e, t));
      break;
    }
    case a.TBODY:
    case a.TFOOT:
    case a.THEAD: {
      (e.openElements.hasInTableScope(t.tagID) || e.openElements.hasInTableScope(a.TR)) && (e.openElements.clearBackToTableRowContext(), e.openElements.pop(), e.insertionMode = s.IN_TABLE_BODY, x(e, t));
      break;
    }
    case a.BODY:
    case a.CAPTION:
    case a.COL:
    case a.COLGROUP:
    case a.HTML:
    case a.TD:
    case a.TH:
      break;
    default:
      M(e, t);
  }
}
function jt(e, t) {
  const n = t.tagID;
  ie.has(n) ? (e.openElements.hasInTableScope(a.TD) || e.openElements.hasInTableScope(a.TH)) && (e._closeTableCell(), U(e, t)) : m(e, t);
}
function Jt(e, t) {
  const n = t.tagID;
  switch (n) {
    case a.TD:
    case a.TH: {
      e.openElements.hasInTableScope(n) && (e.openElements.generateImpliedEndTags(), e.openElements.popUntilTagNamePopped(n), e.activeFormattingElements.clearToLastMarker(), e.insertionMode = s.IN_ROW);
      break;
    }
    case a.TABLE:
    case a.TBODY:
    case a.TFOOT:
    case a.THEAD:
    case a.TR: {
      e.openElements.hasInTableScope(n) && (e._closeTableCell(), oe(e, t));
      break;
    }
    case a.BODY:
    case a.CAPTION:
    case a.COL:
    case a.COLGROUP:
    case a.HTML:
      break;
    default:
      H(e, t);
  }
}
function ce(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.OPTION: {
      e.openElements.currentTagId === a.OPTION && e.openElements.pop(), e._insertElement(t, o.HTML);
      break;
    }
    case a.OPTGROUP: {
      e.openElements.currentTagId === a.OPTION && e.openElements.pop(), e.openElements.currentTagId === a.OPTGROUP && e.openElements.pop(), e._insertElement(t, o.HTML);
      break;
    }
    case a.HR: {
      e.openElements.currentTagId === a.OPTION && e.openElements.pop(), e.openElements.currentTagId === a.OPTGROUP && e.openElements.pop(), e._appendElement(t, o.HTML), t.ackSelfClosing = !0;
      break;
    }
    case a.INPUT:
    case a.KEYGEN:
    case a.TEXTAREA:
    case a.SELECT: {
      e.openElements.hasInSelectScope(a.SELECT) && (e.openElements.popUntilTagNamePopped(a.SELECT), e._resetInsertionMode(), t.tagID !== a.SELECT && e._processStartTag(t));
      break;
    }
    case a.SCRIPT:
    case a.TEMPLATE: {
      _(e, t);
      break;
    }
  }
}
function Ee(e, t) {
  switch (t.tagID) {
    case a.OPTGROUP: {
      e.openElements.stackTop > 0 && e.openElements.currentTagId === a.OPTION && e.openElements.tagIDs[e.openElements.stackTop - 1] === a.OPTGROUP && e.openElements.pop(), e.openElements.currentTagId === a.OPTGROUP && e.openElements.pop();
      break;
    }
    case a.OPTION: {
      e.openElements.currentTagId === a.OPTION && e.openElements.pop();
      break;
    }
    case a.SELECT: {
      e.openElements.hasInSelectScope(a.SELECT) && (e.openElements.popUntilTagNamePopped(a.SELECT), e._resetInsertionMode());
      break;
    }
    case a.TEMPLATE: {
      h(e, t);
      break;
    }
  }
}
function qt(e, t) {
  const n = t.tagID;
  n === a.CAPTION || n === a.TABLE || n === a.TBODY || n === a.TFOOT || n === a.THEAD || n === a.TR || n === a.TD || n === a.TH ? (e.openElements.popUntilTagNamePopped(a.SELECT), e._resetInsertionMode(), e._processStartTag(t)) : ce(e, t);
}
function Zt(e, t) {
  const n = t.tagID;
  n === a.CAPTION || n === a.TABLE || n === a.TBODY || n === a.TFOOT || n === a.THEAD || n === a.TR || n === a.TD || n === a.TH ? e.openElements.hasInTableScope(n) && (e.openElements.popUntilTagNamePopped(a.SELECT), e._resetInsertionMode(), e.onEndTag(t)) : Ee(e, t);
}
function $t(e, t) {
  switch (t.tagID) {
    // First, handle tags that can start without a mode change
    case a.BASE:
    case a.BASEFONT:
    case a.BGSOUND:
    case a.LINK:
    case a.META:
    case a.NOFRAMES:
    case a.SCRIPT:
    case a.STYLE:
    case a.TEMPLATE:
    case a.TITLE: {
      _(e, t);
      break;
    }
    // Re-process the token in the appropriate mode
    case a.CAPTION:
    case a.COLGROUP:
    case a.TBODY:
    case a.TFOOT:
    case a.THEAD: {
      e.tmplInsertionModeStack[0] = s.IN_TABLE, e.insertionMode = s.IN_TABLE, f(e, t);
      break;
    }
    case a.COL: {
      e.tmplInsertionModeStack[0] = s.IN_COLUMN_GROUP, e.insertionMode = s.IN_COLUMN_GROUP, W(e, t);
      break;
    }
    case a.TR: {
      e.tmplInsertionModeStack[0] = s.IN_TABLE_BODY, e.insertionMode = s.IN_TABLE_BODY, p(e, t);
      break;
    }
    case a.TD:
    case a.TH: {
      e.tmplInsertionModeStack[0] = s.IN_ROW, e.insertionMode = s.IN_ROW, U(e, t);
      break;
    }
    default:
      e.tmplInsertionModeStack[0] = s.IN_BODY, e.insertionMode = s.IN_BODY, m(e, t);
  }
}
function ea(e, t) {
  t.tagID === a.TEMPLATE && h(e, t);
}
function Te(e, t) {
  e.openElements.tmplCount > 0 ? (e.openElements.popUntilTagNamePopped(a.TEMPLATE), e.activeFormattingElements.clearToLastMarker(), e.tmplInsertionModeStack.shift(), e._resetInsertionMode(), e.onEof(t)) : v(e, t);
}
function ta(e, t) {
  t.tagID === a.HTML ? m(e, t) : P(e, t);
}
function le(e, t) {
  var n;
  if (t.tagID === a.HTML) {
    if (e.fragmentContext || (e.insertionMode = s.AFTER_AFTER_BODY), e.options.sourceCodeLocationInfo && e.openElements.tagIDs[0] === a.HTML) {
      e._setEndLocation(e.openElements.items[0], t);
      const r = e.openElements.items[1];
      r && !(!((n = e.treeAdapter.getNodeSourceCodeLocation(r)) === null || n === void 0) && n.endTag) && e._setEndLocation(r, t);
    }
  } else
    P(e, t);
}
function P(e, t) {
  e.insertionMode = s.IN_BODY, F(e, t);
}
function aa(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.FRAMESET: {
      e._insertElement(t, o.HTML);
      break;
    }
    case a.FRAME: {
      e._appendElement(t, o.HTML), t.ackSelfClosing = !0;
      break;
    }
    case a.NOFRAMES: {
      _(e, t);
      break;
    }
  }
}
function sa(e, t) {
  t.tagID === a.FRAMESET && !e.openElements.isRootHtmlElementCurrent() && (e.openElements.pop(), !e.fragmentContext && e.openElements.currentTagId !== a.FRAMESET && (e.insertionMode = s.AFTER_FRAMESET));
}
function na(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.NOFRAMES: {
      _(e, t);
      break;
    }
  }
}
function ra(e, t) {
  t.tagID === a.HTML && (e.insertionMode = s.AFTER_AFTER_FRAMESET);
}
function ia(e, t) {
  t.tagID === a.HTML ? m(e, t) : D(e, t);
}
function D(e, t) {
  e.insertionMode = s.IN_BODY, F(e, t);
}
function oa(e, t) {
  switch (t.tagID) {
    case a.HTML: {
      m(e, t);
      break;
    }
    case a.NOFRAMES: {
      _(e, t);
      break;
    }
  }
}
function ca(e, t) {
  t.chars = Re, e._insertCharacters(t);
}
function Ea(e, t) {
  e._insertCharacters(t), e.framesetOk = !1;
}
function me(e) {
  for (; e.treeAdapter.getNamespaceURI(e.openElements.current) !== o.HTML && e.openElements.currentTagId !== void 0 && !e._isIntegrationPoint(e.openElements.currentTagId, e.openElements.current); )
    e.openElements.pop();
}
function Ta(e, t) {
  if (ue(t))
    me(e), e._startTagOutsideForeignContent(t);
  else {
    const n = e._getAdjustedCurrentElement(), r = e.treeAdapter.getNamespaceURI(n);
    r === o.MATHML ? Q(t) : r === o.SVG && (Oe(t), j(t)), k(t), t.selfClosing ? e._appendElement(t, r) : e._insertElement(t, r), t.ackSelfClosing = !0;
  }
}
function la(e, t) {
  if (t.tagID === a.P || t.tagID === a.BR) {
    me(e), e._endTagOutsideForeignContent(t);
    return;
  }
  for (let n = e.openElements.stackTop; n > 0; n--) {
    const r = e.openElements.items[n];
    if (e.treeAdapter.getNamespaceURI(r) === o.HTML) {
      e._endTagOutsideForeignContent(t);
      break;
    }
    const i = e.treeAdapter.getTagName(r);
    if (i.toLowerCase() === t.tagName) {
      t.tagName = i, e.openElements.shortenToLength(n);
      break;
    }
  }
}
export {
  ua as Parser
};
