"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHtml = parseHtml; var tslib_1 = require("tslib"); var char_utils_1 = require("../char-utils"); var utils_1 = require("../utils"); // For debugging: search for other "For debugging" lines // import CliTable from 'cli-table'; var CurrentTag = /** @class */ (function () { function CurrentTag(cfg) { if (cfg === void 0) { cfg = {}; } this.idx = cfg.idx !== undefined ? cfg.idx : -1; this.type = cfg.type || 'tag'; this.name = cfg.name || ''; this.isOpening = !!cfg.isOpening; this.isClosing = !!cfg.isClosing; } return CurrentTag; }()); var noCurrentTag = new CurrentTag(); // shared reference for when there is no current tag currently being read /** * Context object containing all the state needed by the HTML parsing state * machine function. * * ## Historical note * * In v4.1.5, we used nested functions to handle the context via closures, but * this necessitated re-creating the functions for each call to `parseHtml()`, * which made them difficult for v8 to JIT optimize. In v4.1.6, we lifted all of * the functions to the top-level scope and passed the context object between * them, which allows the functions to be JIT compiled once and reused. */ var ParseHtmlContext = /** @class */ (function () { function ParseHtmlContext(html, callbacks) { this.charIdx = 0; // Current character index being processed this.state = 0 /* State.Data */; // begin in the Data state this.currentDataIdx = 0; // where the current data start index is this.currentTag = noCurrentTag; // describes the current tag that is being read this.html = html; this.callbacks = callbacks; } return ParseHtmlContext; }()); /** * Parses an HTML string, calling the callbacks to notify of tags and text. * * ## History * * This file previously used a regular expression to find html tags in the input * text. Unfortunately, we ran into a bunch of catastrophic backtracking issues * with certain input text, causing Autolinker to either hang or just take a * really long time to parse the string. * * The current code is intended to be a O(n) algorithm that walks through * the string in one pass, and tries to be as cheap as possible. We don't need * to implement the full HTML spec, but rather simply determine where the string * looks like an HTML tag, and where it looks like text (so that we can autolink * that). * * This state machine parser is intended just to be a simple but performant * parser of HTML for the subset of requirements we have. We simply need to: * * 1. Determine where HTML tags are * 2. Determine the tag name (Autolinker specifically only cares about , *