{"version":3,"sources":["/home/runner/work/polytypo-js/polytypo-js/dist/chunk-6KMUSOJL.cjs","../src/modes/html.ts","../src/modes/parse-error.ts"],"names":[],"mappings":"AAAA;AACE;AACF,wDAA6B;AAC7B;AACA;ACJA,gCAAqC;ADMrC;AACA;AEAA,SAAS,QAAA,CAAS,KAAA,EAAwB;AACxC,EAAA,GAAA,CAAI,OAAO,MAAA,IAAU,SAAA,GAAY,MAAA,IAAU,IAAA,EAAM,OAAO,MAAA,CAAO,KAAK,CAAA;AACpE,EAAA,MAAM,WAAA,EAAa,KAAA;AACnB,EAAA,MAAM,OAAA,EACJ,OAAO,UAAA,CAAW,OAAA,IAAW,SAAA,EAAW,UAAA,CAAW,OAAA,EAAU,KAAA,CAAgB,OAAA;AAC/E,EAAA,MAAM,MAAA,EAAQ,UAAA,CAAW,KAAA;AACzB,EAAA,GAAA,CACE,MAAA,IAAU,KAAA,GACV,MAAA,IAAU,KAAA,EAAA,GACV,OAAO,KAAA,CAAM,KAAA,IAAS,SAAA,GACtB,OAAO,KAAA,CAAM,OAAA,IAAW,QAAA,EACxB;AACA,IAAA,OAAO,CAAA,EAAA;AACT,EAAA;AACO,EAAA;AACT;AAcgB;AACV,EAAA;AACK,IAAA;AACA,EAAA;AACH,IAAA;AACE,IAAA;AACJ,MAAA;AACA,MAAA;AACF,IAAA;AACF,EAAA;AACF;AFjBY;AACA;ACfC;AACX,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACD;AAEK;AAEU;AACP,EAAA;AACT;AAEM;AACA;AACO;AACP;AACA;AAGA;AAEG;AACA,EAAA;AACT;AAES;AACA,EAAA;AACT;AAES;AACA,EAAA;AACT;AAQS;AACH,EAAA;AACI,EAAA;AACC,EAAA;AAEL,EAAA;AACG,IAAA;AACC,IAAA;AACA,IAAA;AACG,IAAA;AACH,IAAA;AAEJ,IAAA;AAGK,MAAA;AACP,I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EAAA;AACT;AAIM;AACJ,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACD;AAEQ;AACE,EAAA;AACD,IAAA;AACF,IAAA;AACE,IAAA;AAGN,IAAA;AACM,MAAA;AACJ,MAAA;AACF,IAAA;AACA,IAAA;AACF,EAAA;AAGS,EAAA;AAEL,EAAA;AAGK,EAAA;AACC,IAAA;AACR,IAAA;AACF,EAAA;AAEK,EAAA;AACC,EAAA;AACN,EAAA;AACF;AAQgB;AACR,EAAA;AACA,EAAA;AACE,EAAA;AACD,EAAA;AACT;AAGgB;AACR,EAAA;AAA4B,IAAA;AAChC,IAAA;AACF,EAAA;AACM,EAAA;AACE,EAAA;AACD,EAAA;AAGT;AD/DY;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/polytypo-js/polytypo-js/dist/chunk-6KMUSOJL.cjs","sourcesContent":[null,"import { parse, parseFragment } from \"parse5\";\nimport type { DefaultTreeAdapterMap } from \"parse5\";\nimport { wrapParserErrors } from \"./parse-error.js\";\nimport type { Span } from \"./spans.js\";\n\ntype Node = DefaultTreeAdapterMap[\"node\"];\ntype ParentNode = DefaultTreeAdapterMap[\"parentNode\"];\n\n/**\n * spec/rules/modes.md 3.6, exhaustive and **closed**: extending it is a spec change, not an\n * implementation decision. `svg` and `math` are here because in MathML a quotation mark, a\n * hyphen and a prime are operators and identifiers — substituting a curly glyph changes what\n * the expression means. Everything not listed is processable, **including unknown and custom\n * elements**: guessing from a tag name is exactly the heuristic that diverges across runtimes.\n */\nexport const SKIPPED_ELEMENTS: readonly string[] = Object.freeze([\n  \"code\",\n  \"pre\",\n  \"kbd\",\n  \"samp\",\n  \"var\",\n  \"script\",\n  \"style\",\n  \"textarea\",\n  \"svg\",\n  \"math\",\n]);\n\nconst SKIPPED_ELEMENT_SET: ReadonlySet<string> = new Set(SKIPPED_ELEMENTS);\n\nexport function isSkippedElement(tagName: string): boolean {\n  return SKIPPED_ELEMENT_SET.has(tagName);\n}\n\nconst AMPERSAND = 0x26;\nconst SEMICOLON = 0x3b;\nconst HASH = 0x23;\nconst LOWER_X = 0x78;\nconst UPPER_X = 0x58;\n\n/** Longest named reference in the WHATWG table is 32 characters; the guard is a bound, not a rule. */\nconst MAX_REFERENCE_BODY = 34;\n\nfunction isAsciiDigit(unit: number): boolean {\n  return unit >= 0x30 && unit <= 0x39;\n}\n\nfunction isAsciiHexDigit(unit: number): boolean {\n  return isAsciiDigit(unit) || (unit >= 0x61 && unit <= 0x66) || (unit >= 0x41 && unit <= 0x46);\n}\n\nfunction isAsciiAlphanumeric(unit: number): boolean {\n  return isAsciiDigit(unit) || (unit >= 0x61 && unit <= 0x7a) || (unit >= 0x41 && unit <= 0x5a);\n}\n\n/**\n * The end offset of a well-formed character reference starting at `at`, or -1. A bare `&` that a\n * parser would repair is deliberately **not** treated as a reference: it stays inside the span,\n * where no rule can emit or delete it, rather than manufacturing a boundary that suppresses\n * conversions around it.\n */\nfunction characterReferenceEnd(source: string, at: number, limit: number): number {\n  if (source.charCodeAt(at) !== AMPERSAND) return -1;\n  let i = at + 1;\n  if (i >= limit) return -1;\n\n  if (source.charCodeAt(i) === HASH) {\n    i += 1;\n    const unit = i < limit ? source.charCodeAt(i) : -1;\n    const hex = unit === LOWER_X || unit === UPPER_X;\n    if (hex) i += 1;\n    const digitsAt = i;\n    while (\n      i < limit &&\n      (hex ? isAsciiHexDigit(source.charCodeAt(i)) : isAsciiDigit(source.charCodeAt(i)))\n    ) {\n      i += 1;\n    }\n    if (i === digitsAt) return -1;\n    return i < limit && source.charCodeAt(i) === SEMICOLON ? i + 1 : -1;\n  }\n\n  const nameAt = i;\n  while (\n    i < limit &&\n    i - nameAt < MAX_REFERENCE_BODY &&\n    isAsciiAlphanumeric(source.charCodeAt(i))\n  ) {\n    i += 1;\n  }\n  if (i === nameAt) return -1;\n  return i < limit && source.charCodeAt(i) === SEMICOLON ? i + 1 : -1;\n}\n\n/**\n * modes.md 3.6: every character reference is skipped as an **opaque unit**, and a text node\n * containing one is split into spans around it. That is the only way `&nbsp;` survives as\n * `&nbsp;` instead of becoming a literal U+00A0 on the way out.\n */\nexport function splitCharacterReferences(source: string, start: number, end: number): Span[] {\n  const spans: Span[] = [];\n  let cursor = start;\n  let i = start;\n  while (i < end) {\n    if (source.charCodeAt(i) === AMPERSAND) {\n      const stop = characterReferenceEnd(source, i, end);\n      if (stop > 0) {\n        if (i > cursor) spans.push({ start: cursor, end: i });\n        cursor = stop;\n        i = stop;\n        continue;\n      }\n    }\n    i += 1;\n  }\n  if (end > cursor) spans.push({ start: cursor, end });\n  return spans;\n}\n\nfunction isParent(node: Node): node is ParentNode {\n  return \"childNodes\" in node;\n}\n\n/**\n * modes.md 3.6 (spec 1.23.0): a text node of only U+0009, U+000A, U+000C, U+000D and U+0020 that\n * holds a line break is not a span, so the gap around it carries the raw line break and 3.2\n * picks the line marker. Pretty-printed markup then reads as `text` mode reads the same lines.\n */\nfunction isLineBreakWhitespace(source: string, start: number, end: number): boolean {\n  let hasLineBreak = false;\n  for (let i = start; i < end; i += 1) {\n    const c = source.charCodeAt(i);\n    if (c === 0x0a || c === 0x0c || c === 0x0d) hasLineBreak = true;\n    else if (c !== 0x09 && c !== 0x20) return false;\n  }\n  return hasLineBreak;\n}\n\nconst LESS_THAN = 0x3c;\nconst SOLIDUS = 0x2f;\nconst GREATER_THAN = 0x3e;\nconst EQUALS = 0x3d;\n\nfunction isAsciiAlpha(unit: number): boolean {\n  return (unit >= 0x41 && unit <= 0x5a) || (unit >= 0x61 && unit <= 0x7a);\n}\n\nfunction isTagSpace(unit: number): boolean {\n  return unit === 0x09 || unit === 0x0a || unit === 0x0c || unit === 0x0d || unit === 0x20;\n}\n\n/**\n * The offset just past the tag that starts at `at`, or -1 when no tag starts there. Follows the\n * HTML tokenizer: `<` plus a letter, `</` plus a letter, or `</>`. A quote opens a value only in\n * the before-attribute-value state, reached by an `=` that follows an attribute name; an `=` that\n * begins a name, or sits inside an unquoted value, is data.\n */\nfunction tagEnd(source: string, at: number, limit: number): number {\n  if (source.charCodeAt(at) !== LESS_THAN) return -1;\n  let i = at + 1;\n  if (i >= limit) return -1;\n  if (source.charCodeAt(i) === SOLIDUS) {\n    i += 1;\n    if (i >= limit) return -1;\n    if (source.charCodeAt(i) === GREATER_THAN) return i + 1;\n  }\n  if (!isAsciiAlpha(source.charCodeAt(i))) return -1;\n  // tag name\n  while (i < limit) {\n    const c = source.charCodeAt(i);\n    if (isTagSpace(c) || c === SOLIDUS || c === GREATER_THAN) break;\n    i += 1;\n  }\n  while (i < limit) {\n    // before attribute name\n    let c = source.charCodeAt(i);\n    if (isTagSpace(c) || c === SOLIDUS) {\n      i += 1;\n      continue;\n    }\n    if (c === GREATER_THAN) return i + 1;\n    // attribute name: an `=` first in the name is part of it\n    i += 1;\n    while (i < limit) {\n      c = source.charCodeAt(i);\n      if (isTagSpace(c) || c === SOLIDUS || c === GREATER_THAN || c === EQUALS) break;\n      i += 1;\n    }\n    // after attribute name\n    while (i < limit && isTagSpace(source.charCodeAt(i))) i += 1;\n    if (i >= limit) return -1;\n    c = source.charCodeAt(i);\n    if (c === GREATER_THAN) return i + 1;\n    if (c !== EQUALS) continue;\n    // before attribute value\n    i += 1;\n    while (i < limit && isTagSpace(source.charCodeAt(i))) i += 1;\n    if (i >= limit) return -1;\n    c = source.charCodeAt(i);\n    if (c === GREATER_THAN) return i + 1;\n    if (c === 0x22 || c === 0x27) {\n      const close = source.indexOf(c === 0x22 ? '\"' : \"'\", i + 1);\n      if (close === -1 || close >= limit) return -1;\n      i = close + 1;\n      continue;\n    }\n    while (i < limit) {\n      c = source.charCodeAt(i);\n      if (isTagSpace(c) || c === GREATER_THAN) break;\n      i += 1;\n    }\n  }\n  return -1;\n}\n\n/**\n * modes.md 3.6 (spec 1.23.1): a tag the parser ignores is still a tag in the source, and parse5\n * merges the text on either side of it into one node. Splitting the run at every tag-shaped token\n * keeps the tag outside every span and puts it in the gap, where 3.2 chooses the marker.\n */\nfunction textPieces(source: string, start: number, end: number): Array<[number, number]> {\n  const pieces: Array<[number, number]> = [];\n  let cursor = start;\n  let i = start;\n  while (i < end) {\n    if (source.charCodeAt(i) === LESS_THAN) {\n      const stop = tagEnd(source, i, end);\n      if (stop > 0) {\n        if (i > cursor) pieces.push([cursor, i]);\n        cursor = stop;\n        i = stop;\n        continue;\n      }\n    }\n    i += 1;\n  }\n  if (end > cursor) pieces.push([cursor, end]);\n  return pieces;\n}\n\n// Their text is character data, not markup: `<` begins no tag inside them (HTML tokenizer RCDATA,\n// RAWTEXT and PLAINTEXT states), so textPieces must not look for one.\nconst TEXT_ONLY_PARENTS = new Set([\n  \"title\",\n  \"xmp\",\n  \"iframe\",\n  \"noembed\",\n  \"noframes\",\n  \"noscript\",\n  \"plaintext\",\n]);\n\nfunction collect(node: Node, source: string, spans: Span[], isTextOnly = false): void {\n  if (node.nodeName === \"#text\") {\n    const location = node.sourceCodeLocation;\n    if (location === undefined || location === null) return;\n    const pieces = isTextOnly\n      ? ([[location.startOffset, location.endOffset]] as Array<[number, number]>)\n      : textPieces(source, location.startOffset, location.endOffset);\n    for (const [from, to] of pieces) {\n      if (isLineBreakWhitespace(source, from, to)) continue;\n      for (const span of splitCharacterReferences(source, from, to)) spans.push(span);\n    }\n    return;\n  }\n\n  // Comments, doctype and processing instructions never contribute a span (modes.md 3.6).\n  if (node.nodeName === \"#comment\" || node.nodeName === \"#documentType\") return;\n\n  if (\"tagName\" in node && isSkippedElement(node.tagName)) return;\n\n  // A `template`'s children live in a separate document fragment; `template` is not skipped.\n  if (node.nodeName === \"template\" && \"content\" in node) {\n    collect(node.content, source, spans);\n    return;\n  }\n\n  if (!isParent(node)) return;\n  const textOnly = \"tagName\" in node && TEXT_ONLY_PARENTS.has(node.tagName);\n  for (const child of node.childNodes) collect(child, source, spans, textOnly);\n}\n\n/**\n * Locate the processable spans of an HTML document. The tree is used only to find offsets and is\n * then discarded — modes.md 4 forbids reserialisation, which is what makes attribute quoting,\n * self-closing forms, entity spelling, tag case and malformed-input recovery preserved by\n * construction rather than by effort.\n */\nexport function htmlSpans(source: string): Span[] {\n  const document = wrapParserErrors(\"HTML\", () => parse(source, { sourceCodeLocationInfo: true }));\n  const spans: Span[] = [];\n  collect(document, source, spans);\n  return spans;\n}\n\n/** The same, for a fragment: used for HTML blocks embedded in markdown (modes.md 3.7). */\nexport function htmlFragmentSpans(source: string, offset: number): Span[] {\n  const fragment = wrapParserErrors(\"HTML\", () =>\n    parseFragment(source, { sourceCodeLocationInfo: true }),\n  );\n  const spans: Span[] = [];\n  collect(fragment, source, spans);\n  return offset === 0\n    ? spans\n    : spans.map((span) => ({ start: span.start + offset, end: span.end + offset }));\n}\n","import { PolytypoError } from \"../errors.js\";\n\ninterface PositionedError {\n  readonly reason?: unknown;\n  readonly place?: { readonly line?: unknown; readonly column?: unknown } | null;\n}\n\nfunction describe(error: unknown): string {\n  if (typeof error !== \"object\" || error === null) return String(error);\n  const positioned = error as PositionedError;\n  const reason =\n    typeof positioned.reason === \"string\" ? positioned.reason : (error as Error).message;\n  const place = positioned.place;\n  if (\n    place !== null &&\n    place !== undefined &&\n    typeof place.line === \"number\" &&\n    typeof place.column === \"number\"\n  ) {\n    return `${reason} (line ${place.line}, column ${place.column})`;\n  }\n  return reason;\n}\n\n/**\n * spec/rules/modes.md 3.7.2. **The parser's own error type must never escape.** A\n * `VFileMessage`, a `Nokogiri::SyntaxError` or a Python exception on the public surface puts a\n * dependency's type into the contract and is unreproducible in the other four runtimes; only the\n * code is contractual (ARCHITECTURE.md 4.6), while the message and the source position are\n * useful and are not.\n *\n * Reachable in exactly one place: `dialect: \"mdx\"`, because MDX embeds JavaScript. HTML parsing\n * is specified with total error recovery and every byte sequence is valid CommonMark, so neither\n * of the other two languages can fail. The wrapper is applied to all of them anyway — the\n * guarantee is that no parser type escapes, not that this particular parser is trusted.\n */\nexport function wrapParserErrors<T>(what: string, run: () => T): T {\n  try {\n    return run();\n  } catch (error) {\n    if (error instanceof PolytypoError) throw error;\n    throw new PolytypoError(\n      \"POLYTYPO_MALFORMED_INPUT\",\n      `Input does not parse as ${what}: ${describe(error)}`,\n    );\n  }\n}\n"]}