UNPKG

html-minifier

Version:

Highly effective, super-configurable, well-tested web page minifier

3,612 lines • 166 kB
import { HTMLParser } from './htmlparser.js';
import TokenChain from './tokenchain.js';
import { presets, getPreset, getPresetNames } from './presets.js';

import { LRU, decodeForeignText, describeDependencyFailure, escapeXML, identity, isThenable, lowercase, replaceNUL, uniqueId } from './lib/utils.js';
import { collectUsage } from './lib/unused-css.js';

import {
  RE_CR,
  RE_LEGACY_ENTITIES,
  RE_LINE_BREAK_CR,
  RE_ESCAPE_LT,
  RE_ESCAPE_LT_RAW_TEXT,
  RE_HTML_ENCODING,
  RE_WS_START,
  RE_WS_END,
  RE_WS_ONLY,
  RE_WS_NBSP_END,
  RE_STYLE_ELEMENT,
  RE_ENDS_COLLAPSIBLE,
  RE_DOCTYPE_SEGMENT,
  formattingElements,
  inlineElementsToKeepWhitespace,
  inlineElementsToKeepWhitespaceAround,
  inlineElementsToKeepWhitespaceWithin,
  inlineElementsTransparent,
  inlineElementsTransparentAfter,
  inlineElementsTransparentVoid,
  specialContentElements,
  rawTextElements,
  htmlElements,
  optionalStartTags,
  optionalEndTags,
  topLevelElements,
  compactElements,
  looseElements,
  trailingElements,
  specialOptionalEndTags,
  pClosingParents,
  namesForeignMixedCase,
  integrationPointsForeign,
  processingInstructionTag,
  MISSING_DEPENDENCY
} from './lib/constants.js';

import {
  buildInlineSets,
  defaultInlineSets,
  endsWithWhitespace,
  isAsciiWhitespace,
  trimWhitespace,
  collapseWhitespaceAll,
  collapseNoBreakSpaces,
  collapseWhitespace,
  collapseWhitespaceSmart,
  canCollapseWhitespace as defaultCanCollapseWhitespace,
  canTrimWhitespace as defaultCanTrimWhitespace,
  isAllWhitespace
} from './lib/whitespace.js';

import {
  isIgnoredComment,
  isExecutableScript,
  isStyleElement,
  isMetaWithoutEffect,
  normalizeAttr,
  buildAttr,
  deduplicateAttributes,
  replaceContentTypePragma
} from './lib/attributes.js';

import {
  canRemoveParentTag,
  isStartTagMandatory,
  canRemovePrecedingTag,
  canRemoveElement,
  parseRemoveEmptyElementsExcept,
  shouldPreserveEmptyElement
} from './lib/elements.js';

import {
  hasJsonScriptType,
  extractScriptBodies,
  processScript
} from './lib/content.js';

import { declinesMinify, processOptions } from './lib/options.js';
import { canScriptFollow, canStyleSheetsJoin, getMergeKey, holdsHashbang, joinScripts, joinStyleSheets } from './lib/merging.js';
import { toFragment, replaceCustomFragments, isMinifiedCSSSafe, isMinifiedJSSafe } from './lib/fragments.js';

/** @import { ProcessedOptions } from './lib/options.js' */

/**
 * @typedef {Object} HTMLAttribute
 *  Representation of an attribute from the HTML parser.
 *  Internal counterpart: `HTMLAttribute` in lib/attributes.js—keep in sync.
 *
 * @prop {string} name
 * @prop {string} [value]
 * @prop {string} [quote]
 * @prop {string} [customAssign]
 * @prop {string} [customOpen]
 * @prop {string} [customClose]
 */

/**
 * @typedef {Object} MergeRecord
 *  A `script` or `style` element for `mergeScripts` or `mergeStyles`, by where its parts sit
 *  in the output buffer.
 *
 * @prop {'script' | 'style'} kind
 * @prop {number} start - Index of the start tag’s first entry
 * @prop {number} content - Index of the first content entry
 * @prop {number} end - Index of the end tag
 * @prop {string | null} key - What its attributes make of it (`null` to keep it apart)
 * @prop {string} tail - For style sheets, the last CSS of its run that isn’t blank, once parsed
 * @prop {boolean} deferred - For style sheets, whether its CSS is left to be minified with its run’s
 */

/**
 * @typedef {Object} MinifierOptions
 *  Options that control how HTML is minified. All of these are optional
 *  and usually default to a disabled/safe value unless noted.
 *
 * @prop {(tag: string, attrs: HTMLAttribute[], canCollapseWhitespace: (tag: string) => boolean) => boolean} [canCollapseWhitespace]
 *  Predicate that determines whether runs of whitespace inside a given element
 *  can be collapsed (whether its content may be trimmed is up to `canTrimWhitespace`).
 *
 *  Default: Built-in `canCollapseWhitespace` function
 *
 * @prop {((text: string, type?: 'inline' | 'media') => boolean) | null} [canMinifyCSS]
 *  Synchronous predicate that determines whether the built-in `minifyCSS` may
 *  process a given piece of CSS—`type` is `undefined` for `style` elements,
 *  `'inline'` for `style` attributes, and `'media'` for `media` attributes.
 *  Returning false leaves that CSS as if `minifyCSS` were off, including its URLs
 *  and unused rules (within `svg` elements, SVGO still minifies it under `minifySVG`).
 *
 *  Default: `null` (all CSS is minified)
 *
 * @prop {((text: string, inline?: boolean) => boolean) | null} [canMinifyJS]
 *  Synchronous predicate that determines whether the built-in `minifyJS` may
 *  process a given piece of JavaScript—`inline` is true for event handler
 *  attributes. Returning false leaves that JavaScript unminified (`mergeScripts`
 *  still applies).
 *
 *  Default: `null` (all JavaScript is minified)
 *
 * @prop {((text: string) => boolean) | null} [canMinifySVG]
 *  Synchronous predicate that determines whether the built-in `minifySVG` may
 *  pass a given outermost `svg` element, nested ones included, to SVGO. It
 *  receives the element as serialized by the HTML pass (e.g., without comments
 *  under `removeComments`); returning false leaves it as that pass wrote it.
 *
 *  Default: `null` (all SVG is minified)
 *
 * @prop {(tag: string | null, attrs: HTMLAttribute[] | undefined, canTrimWhitespace: (tag: string) => boolean) => boolean} [canTrimWhitespace]
 *  Predicate that determines whether whitespace inside the element may be
 *  trimmed; where not, it stays as written, as in `pre`.
 *
 *  Default: Built-in `canTrimWhitespace` function
 *
 * @prop {number} [cacheCSS]
 *  The maximum number of entries for the CSS minification cache. Higher values
 *  improve performance for inputs with repeated CSS (e.g., batch processing).
 *  - Cache is created on first `minify()` call and persists for the process lifetime
 *  - Cache size is locked after first call—subsequent calls reuse the same cache
 *  - `0` switches the cache off; negative and non-finite values fall back to the default size
 *
 *  Default: `500`
 *
 * @prop {number} [cacheJS]
 *  The maximum number of entries for the JavaScript minification cache. Higher
 *  values improve performance for inputs with repeated JavaScript.
 *  - Cache is created on first `minify()` call and persists for the process lifetime
 *  - Cache size is locked after first call—subsequent calls reuse the same cache
 *  - `0` switches the cache off; negative and non-finite values fall back to the default size
 *
 *  Default: `500`
 *
 * @prop {number} [cacheSVG]
 *  The maximum number of entries for the SVG minification cache. Higher
 *  values improve performance for inputs with repeated SVG content.
 *  - Cache is created on first `minify()` call and persists for the process lifetime
 *  - Cache size is locked after first call—subsequent calls reuse the same cache
 *  - `0` switches the cache off; negative and non-finite values fall back to the default size
 *
 *  Default: `500`
 *
 * @prop {boolean} [caseSensitive]
 *  When true, tag and attribute names are written as in the source rather than in lowercase.
 *  Useful for framework components like `<MyButton>`. HTML names are still read in any case,
 *  so every option applies to them as without it; hooks get names as written.
 *
 *  Default: `false`
 *
 *  @prop {boolean} [collapseAttributeWhitespace]
 *  Collapse multiple whitespace characters within attribute values into a
 *  single space. Also trims leading and trailing whitespace from attribute
 *  values. Applied as an early normalization step before special attribute
 *  handlers (CSS minification, class sorting, etc.) run.
 *
 *  Default: `false`
 *
 * @prop {boolean} [collapseBooleanAttributes]
 *  Collapse boolean attributes to their name only (for example
 *  `disabled="disabled"` → `disabled`).
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#collapse_boolean_attributes
 *
 *  Default: `false`
 *
 * @prop {boolean} [collapseEmptyAttributes]
 *  Collapse attributes with empty values to their name only (for example
 *  `alt=""` → `alt`), which the HTML parser reads the same way.
 *
 *  Default: `false`
 *
 * @prop {boolean} [collapseInlineTagWhitespace]
 *  When false (default) whitespace around `inline` tags is preserved in
 *  more cases. When true, whitespace between inline tags may be collapsed,
 *  except around text-level elements (like `a`, `code`, or `em`); whitespace
 *  next to text is kept as without the option.
 *  Must also enable `collapseWhitespace` to have effect.
 *
 *  Default: `false`
 *
 * @prop {boolean} [collapseNoBreakSpaces]
 *  Remove whitespace next to a no-break space (`U+00A0`), narrow no-break
 *  space (`U+202F`), or figure space (`U+2007`) in text (for example
 *  `a &nbsp; b` → `a&nbsp;b`), which changes how the text renders. Runs of
 *  no-break spaces are kept.
 *  Must also enable `collapseWhitespace` to have effect.
 *
 *  Default: `false`
 *
 * @prop {boolean} [collapseWhitespace]
 *  Collapse multiple whitespace characters into one where allowed. Also
 *  controls trimming behaviour in several code paths.
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#collapse_whitespace
 *
 *  Default: `false`
 *
 * @prop {boolean} [conservativeCollapse]
 *  If true, be conservative when collapsing whitespace (preserve more
 *  whitespace in edge cases). Affects collapse algorithms.
 *  Must also enable `collapseWhitespace` to have effect.
 *
 *  Default: `false`
 *
 * @prop {boolean} [continueOnMinifyError]
 *  When set to `false`, minification errors may throw.
 *  By default, the minifier will attempt to recover from minification
 *  errors, or ignore them and preserve the original content.
 *
 *  Default: `true`
 *
 * @prop {boolean} [continueOnParseError]
 *  When true, the parser will attempt to continue on recoverable parse
 *  errors. Otherwise, parsing errors may throw.
 *
 *  Default: `false`
 *
 * @prop {RegExp[]} [customAttrAssign]
 *  Array of regexes used to recognise custom attribute assignment
 *  operators (e.g., `'<div flex?="{{mode != cover}}"></div>'`).
 *  These are concatenated with the built-in assignment patterns.
 *
 *  Default: `[]`
 *
 * @prop {RegExp} [customAttrCollapse]
 *  Regex matching attribute names whose values should be collapsed.
 *  Basically used to remove newlines and excess spaces inside attribute values,
 *  e.g., `/ng-class/`.
 *
 * @prop {[RegExp, RegExp][]} [customAttrSurround]
 *  Array of `[openRegExp, closeRegExp]` pairs used by the parser to
 *  detect custom attribute surround patterns (for non-standard syntaxes,
 *  e.g., `<input {{#if value}}checked="checked"{{/if}}>`).
 *
 * @prop {RegExp[]} [customEventAttributes]
 *  Array of regexes used to detect event handler attributes for `minifyJS`
 *  (e.g., `ng-click`). The default matches standard `on…` event attributes.
 *
 *  Default: `[/^on[a-z]{3,}$/]`
 *
 * @prop {boolean} [strictCustomFragments]
 *  Rejects `ignoreCustomFragments` patterns that risk catastrophic backtracking,
 *  rather than warning about them: those that compound quantifiers or alternation
 *  (`(a+)+`, `(a|b)*`), those that repeat the same unbounded atom twice in a row
 *  (`.*.*`), and those too long or too deeply nested to analyze at all.
 *
 *  Default: `false`
 *
 * @prop {boolean} [decodeEntities]
 *  When true, decodes HTML entities in text and attributes before
 *  processing, and re-encodes ambiguous ampersands when outputting.
 *
 *  Default: `false`
 *
 *
 * @prop {RegExp[]} [ignoreCustomComments]
 *  Comments matching any pattern in this array of regexes will be
 *  preserved when `removeComments` is enabled. The default preserves
 *  “bang” comments and comments starting with `#`.
 *
 *  Default: `[/^!/, /^\s*#/]`
 *
 * @prop {RegExp[]} [ignoreCustomFragments]
 *  Array of regexes used to identify fragments that should be
 *  preserved (for example server templates). These fragments are temporarily
 *  replaced during minification to avoid corrupting template code.
 *  The default preserves ASP/PHP-style tags and XML declarations.
 *
 *  Default: `[/<%[\s\S]*?%>/, /<\?(?:php[\t\n\r ]|=|\$|xml(?:-stylesheet)?(?![\w-])|\s)[\s\S]*?\?>/i]`
 *
 * @prop {boolean} [includeAutoGeneratedTags]
 *  If false, tags marked as auto-generated by the parser will be omitted
 *  from output. Useful to skip injected tags.
 *
 *  Default: `false`
 *
 * @prop {ArrayLike<string>} [inlineCustomElements]
 *  Collection of custom element tag names that should be treated as inline
 *  elements for white-space handling, alongside the built-in inline elements.
 *
 *  Default: `[]`
 *
 * @prop {boolean} [keepClosingSlash]
 *  Keep the trailing slash on void elements and other start tags that carry one, and read it
 *  as closing the element. The default drops the slash and ignores it, as HTML does; in SVG
 *  and MathML a slash closes the element either way.
 *
 *  Default: `false`
 *
 * @prop {(message: unknown) => void} [log]
 *  Logging function used by the minifier for warnings/errors/info.
 *  You can directly provide `console.log`, but `message` may also be an `Error`
 *  object or other non-string value.
 *
 *  Default: `() => {}` (no-op function)
 *
 * @prop {number} [maxInputLength]
 *  The maximum allowed input length. Used as a guard against ReDoS via
 *  pathological inputs. If the input exceeds this length an error is
 *  thrown.
 *
 *  Default: No limit
 *
 * @prop {number} [maxLineLength]
 *  Maximum line length for the output. When set the minifier will wrap
 *  output to the given number of characters where possible, splitting
 *  lines only in tags—between attributes or before a tag’s `>`.
 *
 *  Default: No limit
 *
 * @prop {boolean} [mergeScripts]
 *  When true, consecutive inline `<script>` elements are merged into one.
 *  Only merges classic scripts whose attributes match (other than a JavaScript
 *  `type`), and none with an `id`. Does not merge external scripts (with `src`).
 *
 *  Default: `false`
 *
 * @prop {boolean} [mergeStyles]
 *  When true, consecutive `<style>` elements are merged into one. Only merges
 *  style sheets whose attributes match (other than a CSS `type`, and `media`
 *  that matches all), and none with an `id`, where the first ends cleanly and
 *  the next doesn’t hold `@import` or `@namespace`.
 *
 *  Default: `false`
 *
 * @prop {boolean | Partial<import("lightningcss").TransformOptions<import("lightningcss").CustomAtRules>> | ((text: string, type?: string) => Promise<string> | string)} [minifyCSS]
 *  When true, enables CSS minification for inline `<style>` tags or
 *  `style` attributes. If an object is provided, it is passed to
 *  [Lightning CSS](https://www.npmjs.com/package/lightningcss)
 *  as transform options. If a function is provided, it will be used to perform
 *  custom CSS minification. If disabled, CSS is not minified.
 *
 *  Default: `false`
 *
 * @prop {boolean | import("terser").MinifyOptions | {engine?: 'terser' | 'swc', [key: string]: any} | ((text: string, inline?: boolean) => Promise<string> | string)} [minifyJS]
 *  When true, enables JS minification for `<script>` contents and
 *  event handler attributes. If an object is provided, it can include:
 *  - `engine`: The minifier to use (`terser` or `swc`). Default: `terser`.
 *  - Engine-specific options (e.g., Terser options if `engine: 'terser'`,
 *    SWC options if `engine: 'swc'`).
 *  If a function is provided, it will be used to perform
 *  custom JS minification. If disabled, JS is not minified.
 *
 *  Default: `false`
 *
 * @prop {boolean} [minifyJSON]
 *  When true, removes whitespace from the content of JSON `<script>` types
 *  (e.g., `application/ld+json`, `importmap`, `speculationrules`), leaving
 *  values as written. Malformed JSON is left as is.
 *
 *  Default: `false`
 *
 * @prop {boolean | string | {site?: string} | ((text: string) => Promise<string> | string)} [minifyURLs]
 *  When true, enables URL rewriting/minification. If an object is provided,
 *  the `site` property sets the base URL for computing relative paths.
 *  If a string is provided, it is treated as an `{ site: string }` options
 *  object. If a function is provided, it will be used to perform custom URL
 *  minification. If disabled, URLs are not minified.
 *
 *  Default: `false`
 *
 * @prop {boolean | Object} [minifySVG]
 *  When true, enables SVG minification using [SVGO](https://github.com/svg/svgo).
 *  Complete SVG subtrees are extracted and optimized as a block, with the SVGO
 *  plugins that assume a standalone SVG file turned off.
 *  If an object is provided, it is passed to SVGO as configuration options; unless
 *  it sets `plugins`, it keeps that plugin set.
 *  If disabled, SVG content is minified using standard HTML rules only.
 *
 *  Default: `false`
 *
 * @prop {boolean} [noNewlinesBeforeTagClose]
 *  When wrapping lines, doesn’t split a line in an end tag (e.g., keeps
 *  `</a>` from becoming `</a` and `>` on two lines).
 *
 *  Default: `false`
 *
 * @prop {boolean} [partialMarkup]
 *  When true, treats input as a partial HTML fragment rather than a complete
 *  document. This preserves stray end tags (closing tags without corresponding
 *  opening tags) and prevents auto-closing of unclosed tags at the end of input.
 *  Useful for minifying template fragments, SSI includes, or other partial HTML
 *  that will be combined with other fragments.
 *
 *  Default: `false`
 *
 * @prop {boolean} [preserveLineBreaks]
 *  Preserve a single line break at the start/end of text nodes when
 *  collapsing/trimming whitespace.
 *  Must also enable `collapseWhitespace` to have effect.
 *
 *  Default: `false`
 *
 * @prop {string} [preset]
 *  Name of a preset configuration to use as a base (`conservative` or
 *  `comprehensive`). Explicitly provided options take precedence over
 *  preset values. Unknown preset names are ignored (with a warning).
 *
 *  Default: No preset
 *
 * @prop {boolean} [preventAttributesEscaping]
 *  When true, attribute values will not be HTML-escaped (dangerous for
 *  untrusted input). By default, attributes are escaped.
 *
 *  Default: `false`
 *
 * @prop {string[]} [processScripts]
 *  Array of `type` attribute values for `<script>` elements whose contents
 *  should be processed as HTML
 *  (e.g., `text/ng-template`, `text/x-handlebars-template`, etc.).
 *  When present, the contents of matching script tags are recursively minified,
 *  like normal HTML content.
 *
 *  Default: `[]`
 *
 * @prop {"\"" | "'"} [quoteCharacter]
 *  Preferred quote character for attribute values. If unspecified, the
 *  minifier picks the safest quote based on the attribute value.
 *
 *  Default: Auto-detected
 *
 * @prop {boolean} [removeAttributeQuotes]
 *  Remove quotes around attribute values where it is safe to do so.
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_attribute_quotes
 *
 *  Default: `false`
 *
 * @prop {boolean} [removeComments]
 *  Remove HTML comments. Comments that match `ignoreCustomComments` will
 *  still be preserved.
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_comments
 *
 *  Default: `false`
 *
 *  @prop {boolean} [removeDefaultTypeAttributes]
 *  Remove default `type` attributes from `<style>`/`<link>` (e.g., `type="text/css"`)
 *  and `<script>` (e.g., `type="text/javascript"`) elements.
 *
 *  Default: `false`
 *
 * @prop {boolean | ((attrName: string, tag: string) => boolean)} [removeEmptyAttributes]
 *  If true, removes attributes whose values are empty (some attributes
 *  are excluded by name). Can also be a function to customise which empty
 *  attributes are removed.
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_empty_or_blank_attributes
 *
 *  Default: `false`
 *
 * @prop {boolean} [removeEmptyElements]
 *  Remove elements that are empty and safe to remove (for example
 *  `<script />` without `src`), including elements left empty that way.
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_empty_elements
 *
 *  Default: `false`
 *
 * @prop {string[]} [removeEmptyElementsExcept]
 *  Specifies empty elements to preserve when `removeEmptyElements` is enabled.
 *  Has no effect unless `removeEmptyElements: true`.
 *
 *  Accepts tag names or HTML-like element specifications:
 *
 *  * Tag name only: `["td", "span"]`—preserves all empty elements of these types
 *  * With valued attributes: `["<i class='test'>"]`—preserves only when attribute values match
 *  * With boolean attributes: `["<input disabled>"]`—preserves only when the
 *    boolean attribute is present
 *  * Mixed: `["<button type='button' disabled>"]`—all specified attributes must match
 *
 *  Attribute matching:
 *
 *  * All specified attributes must be present and match (valued attributes must have exact values)
 *  * Additional attributes on the element are allowed
 *  * Element and attribute names match in any case
 *  * Supports double quotes, single quotes, and unquoted attribute values in specifications
 *
 *  Limitations:
 *
 *  * Self-closing syntax (e.g., `["<span/>"]`) is not supported; use `["span"]` instead
 *  * Definitions containing `>` within quoted attribute values (e.g.,
 *    `["<span title='a>b'>"]`) are not supported
 *
 *  Default: `[]`
 *
 * @prop {boolean} [removeOptionalTags]
 *  Drop optional start/end tags where the HTML specification permits it
 *  (for example `</li>`, optional `<html>` etc.).
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_optional_tags
 *
 *  Default: `false`
 *
 * @prop {boolean} [removeRedundantAttributes]
 *  Remove attributes that are redundant because they match the element’s
 *  default values (for example `<button type="submit">`) or have no effect
 *  (for example `xml:lang`), along with `meta` elements and `rel` keywords
 *  that have no effect.
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_redundant_attributes
 *
 *  Default: `false`
 *
 * @prop {boolean} [removeTagWhitespace]
 *  **Note that this will result in invalid HTML!**
 *
 *  When true, extra whitespace between tag name and attributes (or before
 *  the closing bracket) will be removed where possible. Affects output spacing
 *  such as the space used in the short doctype representation.
 *
 *  Default: `false`
 *
 * @prop {boolean | {safelist?: Array<string | RegExp>, scripts?: boolean}} [removeUnusedCSS]
 *  Remove selectors from `style` elements that name an element, class, or ID the
 *  document doesn’t reference, as well as rules that are left without selectors.
 *  Requires `minifyCSS` (removal runs through Lightning CSS) and has no effect on
 *  `style` or `media` attributes.
 *
 *  Element, class, and ID names are collected from the markup and—unless `scripts`
 *  is set to `false`—from inline `script` elements; class names and IDs also from
 *  `data-*` attributes. Names that only ever appear in external scripts cannot
 *  be detected; list those under `safelist` (strings or regular expressions) to
 *  keep them.
 *
 *  Default: `false`
 *
 * @prop {boolean} [selfCloseForeignElements]
 *  When true, writes empty SVG and MathML elements as self-closing tags (for
 *  example `<path d="…"/>` for `<path d="…"></path>`), which HTML reads as the
 *  same element in foreign content; HTML elements, including those in
 *  `foreignObject` and `annotation-xml`, keep their end tags.
 *
 *  Default: `false`
 *
 * @prop {boolean | ((tag: string, attrs: HTMLAttribute[]) => void)} [sortAttributes]
 *  When true, enables sorting of attributes. If a function is provided it
 *  will be used as a custom attribute sorter, which should mutate `attrs`
 *  in-place to the desired order. If disabled, the minifier will attempt to
 *  preserve the order from the input.
 *
 *  Default: `false`
 *
 * @prop {boolean | ((value: string) => string)} [sortClassNames]
 *  When true, enables sorting of class names inside `class` attributes.
 *  If a function is provided, it will be used to transform/sort the class
 *  name string. If disabled, the minifier will attempt to preserve the
 *  class-name order from the input.
 *
 *  Default: `false`
 *
 * @prop {boolean} [trimCustomFragments]
 *  When true, whitespace around ignored custom fragments is removed rather
 *  than collapsed where the minifier removes whitespace anyway (e.g., between
 *  block-level elements). Whitespace in tags, attribute values, CSS, and
 *  JavaScript is kept.
 *
 *  Default: `false`
 *
 * @prop {boolean} [useShortDoctype]
 *  Convert any doctype to the short `<!doctype html>` form
 *  (switches pages with legacy doctypes to standards mode).
 *  See also: https://perfectionkills.com/experimenting-with-html-minifier/#use_short_doctype
 *
 *  Default: `false`
 */

// Lazy-load heavy dependencies only when needed

/** @type {Promise<Function> | undefined} */
let lightningCSSPromise;
async function getLightningCSS() {
  if (!lightningCSSPromise) {
    lightningCSSPromise = import('lightningcss').then(m => m.transform);
  }
  return lightningCSSPromise;
}

/** @type {Promise<Function> | undefined} */
let terserPromise;
async function getTerser() {
  if (!terserPromise) {
    terserPromise = import('terser').then(m => m.minify);
  }
  return terserPromise;
}

/**
 * @param {string} label - Minifier name as it should read in the message
 * @param {string} specifier - Package that failed to load
 * @param {unknown} cause - Error the import rejected with
 * @returns {Error} Error marked as an unavailable dependency
 */
function unavailableDependency(label, specifier, cause) {
  const err = new Error(describeDependencyFailure(label, specifier, cause), { cause });
  /** @type {any} */ (err).code = MISSING_DEPENDENCY;
  return err;
}

/** @type {Promise<any> | undefined} */
let swcPromise;
async function getSwc() {
  if (!swcPromise) {
    swcPromise = import('@swc/core')
      .then(m => m.default || m)
      .catch(err => {
        throw unavailableDependency('swc', '@swc/core', err);
      });
  }
  return swcPromise;
}

/** @type {Promise<Function> | undefined} */
let svgoPromise;
async function getSvgo() {
  if (!svgoPromise) {
    svgoPromise = import('svgo').then(m => m.optimize);
  }
  return svgoPromise;
}

/** @type {Promise<Function> | undefined} */
let decodeHTMLPromise;
/** @type {((text: string) => string) | undefined} The resolved `decodeHTML`, once the lazy import above has settled */
let decodeHTMLFn;
async function getDecodeHTML() {
  if (!decodeHTMLPromise) {
    decodeHTMLPromise = import('entities').then(m => (decodeHTMLFn = m.decodeHTML));
  }
  return decodeHTMLPromise;
}

// Minification caches (initialized on first use with configurable sizes)
/** @type {LRU | null} */
let cssMinifyCache = null;
/** @type {LRU | null} */
let jsMinifyCache = null;
/** @type {LRU | null} */
let svgMinifyCache = null;

/**
 * @typedef {Object} CacheStats
 *  Hit/miss statistics and current occupancy for a single minification cache.
 *
 * @prop {number} gets Number of lookups attempted against the cache.
 * @prop {number} hits Number of lookups that found a cached entry.
 * @prop {number} size Number of entries currently held in the cache.
 * @prop {number} limit Maximum number of entries the cache can hold.
 */

/**
 * @param {LRU | null} cache
 * @returns {CacheStats}
 */
const cacheStatsOrEmpty = (cache) => cache ? cache.stats() : { gets: 0, hits: 0, size: 0, limit: 0 };

// Patterns for script merging

// Patterns for buffer scanning
const RE_START_TAG = /^<[^/!]/;
const RE_UNQUOTED_VALUE_END = /=[^\s"'=<>`]+$/;
const RE_START_TAG_NAME = /^<([^\s/>]+)/;
const RE_LEADING_WHITESPACE = /^[ \t\n\r\f]/;
const RE_END_TAG = /^<\//;
const RE_WHITESPACE_ONLY = /^\s+$/;
const RE_TABS = /\t/g;
const RE_TRAILING_WHITESPACE = /\s$/;
const RE_LINE_BREAK = /[\n\r]/;
const RE_LINE_BREAK_BLANKS_END = /(?:\r\n?|\n)[ \t\f]*$/;
const RE_BLANKS_LINE_BREAK_START = /^[ \t\f]*(?:\r\n?|\n)/;
const RE_BLANKS_START = /^[ \t\f]+/;
const RE_BLANKS_ONLY = /^[ \t\f]+$/;

// Patterns for `htmlmin:ignore` block content analysis
const RE_HTML_COMMENT_START = /^\s*<!--/;
const RE_CLOSING_TAG_START = /^\s*<\/([a-zA-Z][\w:-]*)/;
const RE_FIRST_HTML_TAG = /^\s*<([a-zA-Z][\w:-]*)/;
const RE_HTML_TAG_AT = /<(\/?[a-zA-Z][\w:-]*)/y;

// Name of the last tag in `content` (prefixed with `/` for an end tag), scanned backward
// from each `<`, as a greedy `[\s\S]*<…` regex backtracks quadratically on tagless content
/**
 * @param {string} content
 * @returns {string | undefined}
 */
function findLastHtmlTag(content) {
  for (let i = content.lastIndexOf('<'); i !== -1; i = i ? content.lastIndexOf('<', i - 1) : -1) {
    RE_HTML_TAG_AT.lastIndex = i;
    const match = RE_HTML_TAG_AT.exec(content);
    if (match) return match[1];
  }
  return undefined;
}


// Probe for SVG/MathML elements—when the input has none (the common case),
// the per-tag foreign-content machinery can be skipped entirely
const RE_FOREIGN_ELEMENT_PROBE = /<(?:svg|math)[\s/>]/i;

// A name in foreign content as the HTML parser reads it
/** @param {string} name */
function nameForeign(name) {
  const nameLower = name.toLowerCase();
  return namesForeignMixedCase.get(nameLower) ?? nameLower;
}

/**
 * @param {string} value
 * @param {ProcessedOptions} options
 * @param {string} uidIgnore
 * @param {string[]} ignoredMarkupChunks
 */
async function createSortFns(value, options, uidIgnore, ignoredMarkupChunks) {
  const attrChains = options.sortAttributes && typeof options.sortAttributes !== 'function' && Object.create(null);
  const classChain = options.sortClassNames && typeof options.sortClassNames !== 'function' && new TokenChain();
  const resolveName = options.name;

  function attrNames(/** @type {HTMLAttribute[]} */ attrs) {
    return attrs.map(function (attr) {
      return resolveName(attr.name);
    });
  }

  const whitespaceSplitPatternScan = /[ \t\n\f\r]+/;
  const whitespaceSplitPatternSort = /[ \n\f\r]+/;

  async function scan(/** @type {string} */ input) {
    /** @type {string} */
    let currentTag;
    /** @type {string | undefined} */
    let currentType;
    const parser = new HTMLParser(input, {
      start: function (/** @type {string} */ tag, /** @type {HTMLAttribute[]} */ attrs, /** @type {boolean} */ unary) {
        if (attrChains) {
          // Lowercase, as the first pass writes names as in the source under `caseSensitive`
          const tagKey = resolveName(tag);
          if (!attrChains[tagKey]) {
            attrChains[tagKey] = new TokenChain();
          }
          attrChains[tagKey].add(attrNames(attrs));
        }
        for (const attr of attrs) {
          if (classChain && attr.value && resolveName(attr.name) === 'class') {
            const classes = trimWhitespace(attr.value).split(whitespaceSplitPatternScan);
            classChain.add(classes);
          } else if (options.processScripts && attr.name.toLowerCase() === 'type') {
            currentTag = tag;
            currentType = attr.value;
          }
        }
        // As on the main path: What a unary tag holds ends with the tag,
        // so no text can belong to it
        if (unary) {
          currentTag = '';
          currentType = undefined;
        }
      },
      end: function () {
        currentTag = '';
      },
      chars: async function (/** @type {string} */ text) {
        // Only HTML content is scanned recursively, not JSON-LD or other non-HTML script types
        if (options.processScripts && specialContentElements.has(currentTag) &&
            options.processScripts.indexOf(currentType ?? '') > -1 &&
            currentType === 'text/html') {
          await scan(text);
        }
      },
      wantsNextTag: false,
      continueOnParseError: options.continueOnParseError,
      selfClosingSlash: options.keepClosingSlash
    });

    try {
      await parser.parse();
    } catch (err) {
      // If parsing fails during analysis pass, skip it—there’s partial frequency
      // data from what can be parsed
      if (!options.continueOnParseError) {
        throw err;
      }
    }
  }

  // First-pass options: Aggressive minification is off, attribute transformations (like
  // `removeDefaultTypeAttributes`) stay. Custom-fragment UID markers (`uidAttr`) don’t
  // exist yet; `htmlmin:ignore` ones (`uidIgnore`) do and are expanded for analysis.
  const firstPassOptions = Object.assign({}, options, {
    sortAttributes: false,
    sortClassNames: false,
    // Disable aggressive minification that doesn’t affect attribute analysis
    collapseWhitespace: false,
    removeAttributeQuotes: false,
    removeTagWhitespace: false,
    decodeEntities: false,
    processScripts: false,
    minifyCSS: identity,
    minifyJS: identity,
    minifyJSON: false,
    minifySVG: null,
    minifyURLs: identity,
    // `ignoreCustomFragments` stays, and template syntax may not parse cleanly
    continueOnParseError: true,
    log: identity
  });

  // `scan()` reads `options` rather than `firstPassOptions`, so it needs the same leniency
  const originalContinueOnParseError = options.continueOnParseError;
  options.continueOnParseError = true;

  // Pre-compile the UID replacement pattern, and read the fragments the way the
  // main path does, so each keeps its own flags
  const uidReplacePattern = uidIgnore && ignoredMarkupChunks
    ? new RegExp('<!--' + uidIgnore + '(\\d+)-->', 'g')
    : null;
  const customFragments = (options.ignoreCustomFragments || []).map(toFragment);

  try {
    // Expand UID tokens back to the original content for frequency analysis
    let expandedValue = value;
    if (uidReplacePattern) {
      expandedValue = value.replace(uidReplacePattern, function (/** @type {string} */ _match, /** @type {string} */ index) {
        return ignoredMarkupChunks[+index] || '';
      });
      uidReplacePattern.lastIndex = 0;
    }

    const firstPassOutput = await minifyHTML(expandedValue, firstPassOptions);

    // Custom fragments are removed for frequency analysis, as HTML comments in start tags keep
    // attributes from parsing; a space in their place preserves attribute boundaries
    let scanValue = firstPassOutput;
    if (customFragments.length) {
      scanValue = replaceCustomFragments(firstPassOutput, customFragments, () => ' ');
    }

    await scan(scanValue);
  } finally {
    if (originalContinueOnParseError !== undefined) {
      options.continueOnParseError = originalContinueOnParseError;
    } else {
      delete options.continueOnParseError;
    }
  }
  if (attrChains) {
    const attrSorters = Object.create(null);
    for (const tag in attrChains) {
      attrSorters[tag] = attrChains[tag].createSorter();
    }
    // Memoize sorted attribute orders—attribute sets often repeat in templates
    const attrOrderCache = new LRU(500);

    options.sortAttributes = function (/** @type {string} */ tag, /** @type {HTMLAttribute[]} */ attrs) {
      if (attrs.length < 2) {
        return;
      }
      // Called with the name as written, as any `sortAttributes` function is
      const sorter = attrSorters[resolveName(tag)];
      if (sorter) {
        const names = attrNames(attrs);

        // Order-independent cache key
        const cacheKey = tag + ':' + names.slice().sort().join(',');
        let sortedNames = /** @type {string[] | undefined} */ (attrOrderCache.get(cacheKey));

        if (sortedNames === undefined) {
          // `sort` works in place, hence the copy
          sortedNames = sorter.sort(names.slice());
          attrOrderCache.set(cacheKey, sortedNames);
        }

        // Apply the sorted order to `attrs`
        const attrMap = Object.create(null);
        names.forEach(function (/** @type {string} */ name, /** @type {number} */ index) {
          (attrMap[name] || (attrMap[name] = [])).push(attrs[index]);
        });
        /** @type {string[]} */ (sortedNames).forEach(function (/** @type {string} */ name, /** @type {number} */ index) {
          attrs[index] = attrMap[name].shift();
        });
      }
    };
  }
  if (classChain) {
    const sorter = classChain.createSorter();
    // Memoize `sortClassNames` results—class lists often repeat in templates
    const classNameCache = new LRU(500);

    options.sortClassNames = function (/** @type {string} */ classNames) {
      // Fast path: Single class (no spaces) needs no sorting
      if (classNames.indexOf(' ') === -1) {
        return classNames;
      }

      const cached = /** @type {string | undefined} */ (classNameCache.get(classNames));
      if (cached !== undefined) {
        return cached;
      }

      // Expand UID tokens back to original content before sorting
      // Fast path: Skip if no HTML comments (UID markers) present
      let expandedValue = classNames;
      if (uidReplacePattern && classNames.indexOf('<!--') !== -1) {
        expandedValue = classNames.replace(uidReplacePattern, function (/** @type {string} */ _match, /** @type {string} */ index) {
          return ignoredMarkupChunks[+index] || '';
        });
        uidReplacePattern.lastIndex = 0;
      }
      const classes = expandedValue.split(whitespaceSplitPatternSort).filter(function(/** @type {string} */ cls) {
        return cls !== '';
      });
      const sorted = sorter.sort(classes);
      const result = sorted.join(' ');

      classNameCache.set(classNames, result);
      return result;
    };
  }
}

// The length of the ASCII whitespace `str` starts or ends with
/**
 * @param {string} str
 * @param {boolean} atEnd
 */
function edgeWhitespaceLength(str, atEnd) {
  let length = 0;
  if (atEnd) {
    while (length < str.length && isAsciiWhitespace(str.charCodeAt(str.length - 1 - length))) length++;
  } else {
    while (length < str.length && isAsciiWhitespace(str.charCodeAt(length))) length++;
  }
  return length;
}

// Match `/^<\/([\w:-]+)>$/` by hand—the regex engine’s per-call setup costs more
// than the scan on these short buffer entries
/**
 * @param {string} str
 * @returns {string | null} The tag name, or `null` where the string is not a plain end tag
 */
function matchPlainEndTag(str) {
  const end = str.length;
  if (end < 4 || str.charCodeAt(0) !== 60 /* < */ || str.charCodeAt(1) !== 47 /* / */ || str.charCodeAt(end - 1) !== 62 /* > */) {
    return null;
  }
  for (let i = 2; i < end - 1; i++) {
    const code = str.charCodeAt(i);
    // `[\w:-]`: Letters, digits, `_`, `:`, `-`
    if (!((code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 95 || code === 58 || code === 45)) {
      return null;
    }
  }
  return str.slice(2, end - 1);
}

// At either end of partial markup, collapsed whitespace may separate the fragment from what
// surrounds it, and so stays where text in its place would keep it
/** @param {ProcessedOptions} options */
function keepsEdgeWhitespace(options) {
  return Boolean(options.collapseWhitespace && options.partialMarkup && !options.conservativeCollapse);
}

/**
 * @param {string} value
 * @param {ProcessedOptions} options
 * @param {boolean} [partialMarkup] - Whether treating input as partial markup
 * @returns {Promise<string>}
 */
async function minifyHTML(value, options, partialMarkup) {
  // Input length limit, against ReDoS
  if (options.maxInputLength && value.length > options.maxInputLength) {
    throw new Error(`Input length (${value.length}) exceeds maximum allowed length (${options.maxInputLength})`);
  }

  const keepsEdges = keepsEdgeWhitespace(options);

  if (options.collapseWhitespace && !keepsEdges) {
    value = collapseWhitespace(value, options, true, true);
  }

  const resolveName = options.name;
  /** @type {HTMLAttribute[]} */
  const emptyAttrs = [];

  // While parsing, entries already in it change through `bufferSet`, `bufferRemove`,
  // or `bufferTruncate`, so that `trimSettledIndex` stays true
  /** @type {string[]} */
  const buffer = [];
  // Where the last trim walk began that came to text it couldn’t trim, past end tags alone: A
  // later walk that gets there finds the same, until an entry up to there changes
  let trimSettledIndex = -1;
  // Runs of entries that walks back through `buffer` step over, empty ones and empty ones or
  // comments—where the last such walk began and the entry it stopped at, so a later walk that
  // gets there can skip them, until one of those changes
  /** @type {{top: number, bottom: number, skips: (entry: string) => boolean}} */
  const runEmpty = { top: -1, bottom: -1, skips: entry => !entry };
  /** @type {{top: number, bottom: number, skips: (entry: string) => boolean}} */
  const runQuiet = {
    top: -1,
    bottom: -1,
    // Comments as `<!…`, not `htmlmin:ignore` content, which renders
    skips: entry => !entry || (entry.charCodeAt(0) === 60 /* < */ && entry.charCodeAt(1) === 33 /* ! */ && !(uidIgnore && entry.indexOf(uidIgnore) !== -1))
  };
  const runsSkipped = [runEmpty, runQuiet];
  // Parallel to `buffer`: whether the entry is a tag—the only place lines may break
  const bufferTags = options.maxLineLength ? /** @type {boolean[]} */ ([]) : null;
  // Under `mergeStyles` and `mergeScripts`, the elements that may be merged, by where their
  // start tag, content, and end tag sit in `buffer`; the one being parsed; and how deep parsing
  // is in `template` and `noscript` elements, whose style sheets don’t apply as written
  /** @type {MergeRecord[] | null} */
  const mergeRecords = options.mergeScripts || options.mergeStyles ? [] : null;
  /** @type {MergeRecord | null} */
  let mergeRecord = null;
  let inertDepth = 0;
  /** @type {string} */
  let charsPrevTag = '';
  let currentChars = '';
  /** @type {boolean} */
  let hasChars = false;
  // Open elements under `removeEmptyElements`, so an element whose content went is removed,
  // too: where its start tag sits in `buffer`, and whether anything kept went in since,
  // and what the text before it was (including entries its start tag trimmed)
  /** @type {Array<{tag: string, index: number, empty: boolean, before: {chars: string, prevTag: string, gapIndex: number, trims: Array<[number, string]>}, omittedStartTag?: {tag: string, text: string} | undefined, omittedEndTag?: {tag: string, text: string} | undefined, impliedEnds?: Array<{tag: string, text: string}> | undefined}>} */
  const openElements = [];
  // Entries changed while a start tag trims, with what they were, while recording
  /** @type {Array<[number, string]> | null} */
  let trimsRecorded = null;
  // Whether the last element is a removed block (or one removed after an element
  // that doesn’t render), which text after it reads past
  let followsRemovedBlock = false;
  // End tags not written, for the start tag that comes next (`text` is empty where one can’t be)
  /** @type {Array<{tag: string, text: string}>} */
  let impliedEnds = [];
  let impliedEndsIndex = -1;
  // Under `selfCloseForeignElements`, the open `svg` and `math` elements and the integration points
  // in them, whose children are HTML, which a slash does not close
  /** @type {string[]} */
  const foreignScopes = [];
  // A start tag the parser implied without writing it, whose end tag, if it comes next, is a stray
  // one that browsers read as an empty element (as `</p>` is), so it must not be omitted
  let impliedStartTag = '';
  // Whether the parser found the document to be in no-quirks mode, where `table` closes a `p`
  let tableClosesP = false;
  // Under `collapseWhitespace`, the text whose trailing whitespace sits before or
  // between elements that don’t render, kept until what follows them shows whether
  // it separates anything; whether the text being processed is such text; and how
  // deep inside such elements parsing is
  let transparentGapIndex = -1;
  let transparentGapPending = false;
  let transparentDepth = 0;
  let currentTag = '';
  /** @type {Array<{name: string, value?: string, quote?: string, customAssign?: string, customOpen?: string, customClose?: string}>} */
  let currentAttrs = [];
  /** @type {string[]} */
  const stackNoTrimWhitespace = [];
  /** @type {string[]} */
  const stackNoCollapseWhitespace = [];
  let preDepth = 0; // Count of `pre` entries in `stackNoTrimWhitespace`
  // Where the last element whose whitespace may not be trimmed ended in `buffer`;
  // what lies up to there stays as written, also where `canTrimWhitespace` decides
  // by attributes the trim walk lacks
  let noTrimEndIndex = -1;
  // The same for the last element whose whitespace may not be collapsed
  let noCollapseEndIndex = -1;
  let optionalStartTag = '';
  let optionalEndTag = '';
  let optionalEndTagEmitted = false;
  // Whether a node was removed since the last output
  let removed = false;
  // Whether the latest text was kept verbatim, as in `pre` or `script`
  let textVerbatim = false;
  /** @type {string[]} */
  const ignoredMarkupChunks = [];
  // Placeholders of `htmlmin:ignore` blocks where whitespace collapses, each followed
  // by where in `buffer` it went
  /** @type {Array<string | number>} */
  const ignoredMarkupCollapsing = [];
  /** @type {Array<[string, string, string]>} */
  const ignoredCustomMarkupChunks = [];
  // Custom fragments whose surrounding whitespace is never trimmed: in tags, where it
  // separates attributes, and in CSS and JavaScript, where minifiers can’t judge it
  /** @type {Set<number>} */
  const customMarkupInTags = new Set();
  /** @type {Set<number>} */
  const customMarkupInCode = new Set();
  // Attributes joined with the custom fragments they run into, to be written as they are
  /** @type {WeakSet<HTMLAttribute>} */
  const opaqueAttrs = new WeakSet();
  /** @type {string | undefined} */
  let uidIgnore;
  /** @type {RegExp | undefined} */
  let uidIgnorePlaceholderPattern;
  /** @type {string | undefined} */
  let uidAttr;
  /** @type {RegExp | undefined} */
  let uidPattern;
  /** @type {RegExp | undefined} */
  let uidAttrLeadingPattern;
  /** @type {RegExp | undefined} */
  let uidAttrTrailingPattern;
  /** @type {RegExp | undefined} */
  let uidAttrExactPattern;
  // Create inline tags/text sets with custom elements
  const customElementsInput = options.inlineCustomElements ?? [];
  const customElementsArr = Array.isArray(customElementsInput) ? customElementsInput : Array.from(customElementsInput);
  const normalizedCustomElements = customElementsArr.map(name => resolveName(name));
  // Fast path: Reuse base sets if no custom elements
  const inlineTextSet = normalizedCustomElements.length
    ? new Set([...inlineElementsToKeepWhitespaceWithin, ...normalizedCustomElements])
    : inlineElementsToKeepWhitespaceWithin;
  const inlineElements = normalizedCustomElements.length
    ? new Set([...inlineElementsToKeepWhitespaceAround, ...normalizedCustomElements])
    : inlineElementsToKeepWhitespaceAround;
  const inlineSets = normalizedCustomElements.length
    ? buildInlineSets(inlineElements, inlineTextSet)
    : defaultInlineSets;

  /** @type {Array<{tag: string, attrs: {[x: string]: string | undefined} | null}>} */
  let removeEmptyElementsExcept;
  if (options.removeEmptyElementsExcept && !Array.isArray(options.removeEmptyElementsExcept)) {
    options.log('Warning: `removeEmptyElementsExcept` option must be an array, received: ' + typeof options.removeEmptyElementsExcept);
    removeEmptyElementsExcept = [];
  } else {
    removeEmptyElementsExcept = parseRemoveEmptyElementsExcept(options.removeEmptyElementsExcept || [], options);
  }

  // Temporarily replace ignored chunks with comments, so whatever they hold needn’t be parsed
  if (value.indexOf('<!-- htmlmin:ignore -->') !== -1) {
    // Use `indexOf`-based O(n) loop instead of a global regex with [\s\S]*? to avoid O(n²)
    // backtracking on adversarial HTML with many `<!--` prefixes but no closing marker
    const ignoreMarker = '<!-- htmlmin:ignore -->';
    const ignoreMarkerLen = ignoreMarker.length;
    let ignoreResult = '';
    let ignorePos = 0;
    while (ignorePos < value.length) {
      const ignoreStart = value.indexOf(ignoreMarker, ignorePos);
      if (ignoreStart === -1) { ignoreResult += value.slice(ignorePos); break; }
      ignoreResult += value.slice(ignorePos, ignoreStart);
      const ignoreEnd = value.indexOf(ignoreMarker, ignoreStart + ignoreMarkerLen);
      if (ignoreEnd === -1) { ignoreResult += value.slice(ignoreStart); break; }
      const group1 = value.slice(ignoreStart + ignoreMarkerLen, ignoreEnd);
      if (!uidIgnore) {
        uidIgnore = uniqueId(value);
        const pattern = new RegExp('^' + uidIgnore + '([0-9]+)$');
        uidIgnorePlaceholderPattern = new RegExp('^<!--' + uidIgnore + '(\\d+)-->$');
        if (options.ignoreCustomComments) {
          options.ignoreCustomComments = options.ignoreCustomComments.slice();
        } else {
          options.ignoreCustomComments = [];
        }
        options.ignoreCustomComments.push(pattern);
      }
      const token = '<!--' + uidIgnore + ignoredMarkupChunks.length + '-->';
      ignoredMarkupChunks.push(group1);
      ignoreResult += token;
      ignorePos = ignoreEnd + ignoreMarkerLen;
    }
    value = ignoreResult;
  }

  // Sort functions are created after `htmlmin:ignore` processing but before custom fragment UID
  // markers, so frequency analysis can reach ignored content via UID tokens
  if ((options.sortAttributes && typeof options.sortAttributes !== 'function') ||
      (options.sortClassNames && typeof options.sortClassNames !== 'function')) {
    await createSortFns(value, options, uidIgnore ?? '', ignoredMarkupChunks);
  }

  const customFragments = (options.ignoreCustomFragments || []).map(toFragment);
  if (customFragments.length) {
    // Temporarily replace custom ignored fragments with unique attributes
    const replaceFragment = function (/** @type {string} */ match) {
      if (!uidAttr) {
        uidAttr = uniqueId(value);
        uidPattern = new RegExp('(\\s*)' + uidAttr + '([0-9]+)' + uidAttr + '(\\s*)', 'g');
        uidAttrLeadingPattern = new RegExp('^\\s*' + uidAttr + '(\\d+)' + uidAttr);
        uidAttrTrailingPattern = new RegExp(uidAttr + '(\\d+)' + uidAttr + '\\s*$');
        uidAttrExactPattern = new RegExp('^' + uidAttr + '(\\d+)' + uidAttr + '$');

        const uidPlaceholderPattern = new RegExp(uidAttr + '\\d+' + uidAttr, 'g');

        /**
         * @param {(text: string, ...args: any[]) => any} fn
         * @param {boolean} isJS
         * @returns {any}
         */
        const wrapCodeMinifier = (fn, isJS) => (/** @type {string} */ text, /** @type {any[]} */ ...args) => {
          if (!text.includes(/** @type {string} */ (uidAttr))) return fn(text, ...args);

          const source = text.replace(/** @type {RegExp} */ (uidPattern), function (/** @type {string} */ _match, /** @type {string} */ _prefix, /** @type {string} */ index) {
            const chunks = ignoredCustomMarkupChunks[+index];
            customMarkupInCode.add(+index);
            return (chunks?.[1] ?? '') + uidAttr + index + uidAttr + (chunks?.[2] ?? '');
          });
          const result = fn(source, ...args);
          const settle = (/** @type {string} */ output, /** @type {boolean} */ isSafe) => {
            if (isSafe) return output;
            options.log('Keeping CSS or JavaScript as written, as minifying could break or lose a custom fragment');
            return source;
          };
          // `args` are the inline and module flags `minifyJS` is called with
          const finish = (/** @type {string} */ output) => isJS
            ? isMinifiedJSSafe(source, output, uidPlaceholderPattern, Boolean(args[0]), Boolean(args[1])).then(isSafe => settle(output, isSafe))
            : settle(output, isMinifiedCSSSafe(source, output, uidPlaceholderPattern));
          return isThenable(result) ? Promise.resolve(result).then(finish) : finish(result);
        };

        if (options.minifyCSS !== identity) options.minifyCSS = wrapCodeMinifier(options.minifyCSS, false);
        if (options.minifyJS !== identity) options.minifyJS = wrapCodeMinifier(options.minifyJS, true);
      }

      const token = uidAttr + ignoredCustomMarkupChunks.length + uidAttr;
      // Split off leading and trailing whitespace without a regex, which would
      // backtrack quadratically over inner whitespace runs
      const leading = match.length - match.trimStart().length;
      const trailing = leading === match.length ? 0 : match.length - match.trimEnd().length;
      ignoredCustomMarkupChunks.push([match, match.slice(0, leading), match.slice(match.length - trailing)]);
      return '\t' + token + '\t';
    };

    value = replaceCustomFragments(value, customFragments, replaceFragment);
  }

  // HTML reads every line break as a line feed, while ignored markup and custom fragments,
  // now placeholders, stay as written
  if (value.indexOf('\r') !== -1) {
    value = value.replace(RE_LINE_BREAK_CR, '\n');
  }

  // Custom fragment and `htmlmin:ignore` placeholders hold these
  const markers = [uidAttr, uidIgnore].filter(marker => marker !== undefined);

  // Record the custom fragments in (part of) a tag
  function markTagFragments(/** @type {string} */ str) {
    for (const uidMatch of str.matchAll(/** @type {RegExp} */ (uidPattern))) {
      customMarkupInTags.add(+(uidMatch[2] ?? 0));
    }
  }

  // Source text of an attribute, as far as it matters
  function rawAttr(/** @type {HTMLAttribute} */ attr) {
    if (opaqueAttrs.has(attr)) {
      return attr.name;
    }
    let raw = attr.name;
    // Skip values the parser filled in for bare boolean attributes
    if (attr.value !== undefined && !(attr.quote === '' && attr.value === attr.name)) {
      raw += (attr.customAssign ?? '=') + (attr.quote ?? '') + attr.value + (attr.quote ?? '');
    }
    return (attr.customOpen ?? '') + raw + (attr.customClose ?? '');
  }

  /**
   * Join each attribute a custom fragment runs into without whitespace with that fragment,
   * so that no later step separates, drops, changes, or reorders them
   * @param {HTMLAttribute[]} attrs - Joined in place
   * @returns {boolean} Whether the first attribute runs into the tag name
   */
  function joinFragmentAttrs(attrs) {
    const pattern = /** @type {RegExp} */ (uidAttrExactPattern);
    let joinsTag = false;
    let writeIndex = 0;
    // Padding of the previous attribute if that is a fragment without value
    /** @type {[string, string, string] | null | undefined} */
    let prevChunks = null;
    for (const attr of attrs) {
      const uidMatch = pattern.exec(attr.name);
      const chunks = uidMatch ? ignoredCustomMarkupChunks[+(uidMatch[1] ?? 0)] : null;
      const joins = prevChunks ? prevChunks[2] === '' : chunks?.[1] === '';
      prevChunks = attr.value === undefined ? chunks : null;
      if (!joins) {
        attrs[writeIndex++] = attr;
        continue;
      }
      const prev = writeIndex ? attrs[writeIndex - 1] : undefined;
      if (!prev) {
        joinsTag = true;
      }
      /** @type {HTMLAttribute} */
      const joined = { name: (prev ? rawAttr(prev) : '') + rawAttr(attr), customAssign: '=', customOpen: '', customClose: '', quote: '' };
      opaqueAttrs.add(joined);
      attrs[prev ? writeIndex - 1 : writeIndex++] = joined;
    }
    attrs.length = writeIndex;
    return joinsTag;
  }

  /**
   * Restore the whitespace on one side of a custom fragment
   * @param {string} str - Whitespace the minifier left there and the fragment’s own padding
   * @param {boolean} trim
   * @param {boolean} leading
   */
  function restoreFragmentSide(str, trim, leading) {
    if (!str) {
      return str;
    }
    return collapseWhitespace(str, {
      preserveLineBreaks: options.preserveLineBreaks,
      conservativeCollapse: !trim
    }, leading, !leading);
  }

  // Under `caseSensitive`, names are read in lowercase all the same, while `buffer` and hooks
  // get them as written, so the defaults a hook may call take any spelling then
  const namesAsWritten = Boolean(options.namesAsWritten);
  const defaultCanCollapse = namesAsWritten
    ? (/** @type {string} */ tag) => defaultCanCollapseWhitespace(tag.toLowerCase())
    : defaultCanCollapseWhitespace;
  const defaultCanTrim = namesAsWritten
    ? (/** @type {string} */ tag) => defaultCanTrimWhitespace(tag.toLowerCase())
    : defaultCanTrimWhitespace;

  // The defaults decide by `tag`, a hook gets `tagOut`; without a hook, the
  // option holds the default itself
  function canCollapseWhitespace(/** @type {string} */ tag, /** @type {string} */ tagOut, /** @type {HTMLAttribute[]} */ attrs) {
    const hook = options.canCollapseWhitespace;
    return hook === defaultCanCollapseWhitespace ? defaultCanCollapseWhitespace(tag) : hook(tagOut, attrs, defaultCanCollapse);
  }

  function canTrimWhitespace(/** @type {string} */ tag, /** @type {string} */ tagOut, /** @type {HTMLAttribute[]} */ attrs) {
    const hook = options.canTrimWhitespace;
    return hook === defaultCanTrimWhitespace ? defaultCanTrimWhitespace(tag) : hook(tagOut, attrs, defaultCanTrim);
  }

  function bufferPush(/** @type {string} */ entry, /** @type {boolean} */ isTag) {
    textTrimmedEndIndex = -1;
    buffer.push(entry);
    bufferTags?.push(isTag);
    if (entry) {
      removed = false;
      followsRemovedBlock = false;
    }
  }

  function bufferSet(/** @type {number} */ index, /** @type {string} */ entry) {
    trimsRecorded?.push([index, buffer[index] ?? '']);
    buffer[index] = entry;
    if (index <= trimSettledIndex) trimSettledIndex = -1;
    for (const run of runsSkipped) {
      if (index <= run.top && (index === run.bottom ? run.skips(entry) : index > run.bottom && !run.skips(entry))) run.top = -1;
    }
  }

  // Entries after `index` move down by one, so a settled walk from there on is no longer one
  function bufferRemove(/** @type {number} */ index) {
    buffer.splice(index, 1);
    bufferTags?.splice(index, 1);
    if (index <= trimSettledIndex) trimSettledIndex = -1;
    // Past an empty entry removed from the run, the run is one shorter
    for (const run of runsSkipped) {
      if (index <= run.top) run.top = index > run.bottom ? run.top - 1 : -1;
    }
    if (mergeRecords) {
      // Recorded in buffer order, so those before `index` stay as they are
      for (let i = mergeRecords.length - 1; i >= 0; i--) {
        const record = /** @type {MergeRecord} */ (mergeRecords[i]);
        if (record.end < index) break;
        shiftMergeRecord(record, index);
      }
      if (mergeRecord) shiftMergeRecord(mergeRecord, index);
    }
  }

  function shiftMergeRecord(/** @type {MergeRecord} */ record, /** @type {number} */ index) {
    if (record.start === index || record.content === index || record.end === index) {
      record.key = null;
    }
    if (record.start > index) record.start--;
    if (record.content > index) record.content--;
    if (record.end > index) record.end--;
  }

  function bufferTruncate(/** @type {number} */ length) {
    buffer.length = Math.max(0, length);
    if (length <= trimSettledIndex) trimSettledIndex = -1;
    for (const run of runsSkipped) {
      if (length <= run.top) run.top = length > run.bottom + 1 ? length - 1 : -1;
    }
    if (bufferTags) bufferTags.length = buffer.length;
    if (transparentGapIndex >= buffer.length) transparentGapIndex = -1;
    if (noTrimEndIndex >= buffer.length) noTrimEndIndex = -1;
    if (noCollapseEndIndex >= buffer.length) noCollapseEndIndex = -1;
    if (mergeRecords) {
      while (mergeRecords.length && /** @type {MergeRecord} */ (mergeRecords[mergeRecords.length - 1]).end >= buffer.length) {
        mergeRecords.pop();
      }
      if (mergeRecord && mergeRecord.start >= buffer.length) mergeRecord = null;
      else if (mergeRecord && mergeRecord.content >= buffer.length) mergeRecord.content = -1;
    }
    removed = true;
  }

  // The nearest entry from `index` down that `run` doesn’t skip (or 0), past what a walk went past
  // before, so that walking back over removed nodes and comments doesn’t grow quadratic
  function skipRun(/** @type {typeof runEmpty} */ run, /** @type {number} */ index) {
    const indexStart = index;
    while (index > 0 && run.skips(buffer[index] ?? '')) {
      if (index <= run.top && index > run.bottom) {
        index = run.bottom;
        break;
      }
      index--;
    }
    // A shorter run doesn’t replace a longer one, which entries removed above it may expose again
    if (index < indexStart && indexStart > run.top && indexStart - index >= run.top - run.bottom) {
      run.top = indexStart;
      run.bottom = index;
    }
    return index;
  }

  // The nearest entries before and from `index` that aren’t empty, as removed nodes leave them
  /** @returns {[number, number]} */
  function findNeighbors(/** @type {number} */ index) {
    let before = index - 1;
    while (before >= 0 && buffer[before] === '') {
      before--;
    }
    let after = index;
    while (after < buffer.length && buffer[after] === '') {
      after++;
    }
    return [before, after];
  }

  function recordContentOf(/** @type {MergeRecord} */ record) {
    return buffer.slice(record.content, record.end).join('');
  }

  // Custom fragments as written, to compare attributes by
  function restoreFragments(/** @type {string} */ text) {
    return uidPattern && text.includes(/** @type {string} */ (uidAttr))
      ? text.replace(uidPattern, (/** @type {string} */ match, /** @type {string} */ prefix, /** @type {string} */ index, /** @type {string} */ suffix) =>
        prefix + (ignoredCustomMarkupChunks[+index]?.[0] ?? match) + suffix)
      : text;
  }

  // Whether the entries from `from` to `to` (exclusive) are whitespace that may sit between
  // merged elements
  function isMergeGap(/** @type {number} */ from, /** @type {number} */ to) {
    for (let i = from; i < to; i++) {
      const entry = buffer[i] ?? '';
      if (entry && !RE_WS_ONLY.test(entry)) return false;
    }
    return true;
  }

  function createMergeRecord(/** @type {'script' | 'style'} */ kind, /** @type {HTMLAttribute[]} */ attrs) {
    return { kind, start: buffer.length, content: -1, end: -1, key: getMergeKey(kind, attrs, restoreFragments), tail: '', deferred: false };
  }

  // Whether an element is like the one before and follows it with only whitespace between
  function followsAlike(/** @type {MergeRecord} */ recordPrev, /** @type {MergeRecord} */ record) {
    return record.key !== null && record.key === recordPrev.key && record.start > recordPrev.end &&
      isMergeGap(recordPrev.end + 1, record.start);
  }

  // Whether the `style` element being parsed, holding `css`, joins the last element recorded
  // (or its run), so that its CSS is minified with the run’s rather than on its own
  function joinsStyleRun(/** @type {MergeRecord} */ record, /** @type {string} */ css) {
    const recordPrev = mergeRecords?.[mergeRecords.length - 1];
    return recordPrev !== undefined && followsAlike(recordPrev, record) && canStyleSheetsJoin(recordPrev.tail, css);
  }

  // Merge each run of `script` or `style` elements that only whitespace separates into its
  // first, as long as they are compatible; whitespace that may separate text goes after it.
  // CSS whose minification was deferred to the run is minified here.
  async function mergeElements(/** @type {MergeRecord[]} */ records) {
    /** @type {Array<{records: MergeRecord[], parts: string[]}>} */
    const runs = [];
    /** @type {{records: MergeRecord[], parts: string[]} | null} */
    let run = null;
    /** @type {MergeRecord | null} */
    let recordPrev = null;
    // What decides whether more may follow: For style sheets, the last CSS of the run that
    // isn’t blank; for scripts, whether the run’s code holds `<!--`
    let tail = '';
    let holdsCommentStart = false;
    for (const record of records) {
      const content = recordContentOf(record);
      const isStyle = record.kind === 'style';
      const isUsable = record.key !== null && RE_START_TAG.test(buffer[record.start] ?? '') && RE_END_TAG.test(buffer[record.end] ?? '') &&
        (isStyle || !holdsHashbang(content));
      if (run && isUsable && recordPrev && followsAlike(recordPrev, record) &&
          (isStyle ? canStyleSheetsJoin(tail, content) : canScriptFollow(holdsCommentStart, content))) {
        run.records.push(record);
        run.parts.push(content);
      } else {
        // Where what was to be merged no longer is, the CSS is still to be minified
        run = isUsable || record.deferred ? { records: [record], parts: [content] } : null;
        if (run) runs.push(run);
        tail = '';
        holdsCommentStart = false;
      }
      if (!isStyle) holdsCommentStart ||= content.indexOf('<!--') !== -1;
      else if (content.trim()) tail = content;
      recordPrev = isUsable ? record : null;
    }

    /** @type {Promise<void>[]} */
    const pending = [];
    for (const { records: merged, parts } of runs) {
      const first = /** @type {MergeRecord} */ (merged[0]);
      if (merged.length < 2) {
        if (first.deferred) {
          pending.push((async () => {
            const css = await options.minifyCSS(parts[0] ?? '', undefined, options.cssContext);
            for (let i = first.content + 1; i < first.end; i++) bufferSet(i, '');
            bufferSet(first.content, css);
          })());
        }
        continue;
      }
      const last = /** @type {MergeRecord} */ (merged[merged.length - 1]);
      // The first whitespace between the elements stays, right after the first one
      let gapFrom = -1;
      let gapTo = -1;
      for (let i = 1; i < merged.length && gapFrom === -1; i++) {
        const from = /** @type {MergeRecord} */ (merged[i - 1]).end + 1;
        const to = /** @type {MergeRecord} */ (merged[i]).start;
        if (buffer.slice(from, to).join('')) {
          gapFrom = from;
          gapTo = to;
        }
      }
      for (let i = first.end + 1; i <= last.end; i++) {
        if (i < gapFrom || i >= gapTo) bufferSet(i, '');
      }
      // Unless nothing or whitespace is on either side anyway
      if (gapFrom !== -1) {
        const [before] = findNeighbors(first.start);
        const [, after] = findNeighbors(last.end + 1);
        const textBefore = buffer[before] ?? '';
        const textAfter = buffer[after] ?? '';
        if ((!keepsEdges && (before < 0 || after >= buffer.length)) || RE_WS_END.test(textBefore) || RE_WS_START.test(textAfter)) {
          for (let i = gapFrom; i < gapTo; i++) bufferSet(i, '');
        }
      }
      for (let i = first.content + 1; i < first.end; i++) bufferSet(i, '');
      if (first.kind === 'script') {
        bufferSet(first.content, joinScripts(parts, markers));
        continue;
      }
      const css = joinStyleSheets(parts);
      bufferSet(first.content, css);
      if (options.minifyCSS === identity) continue;
      pending.push((async () => {
        // Minified as one, the style sheets may share rules—unless CSS of them is to stay as it is
        if (!parts.some(part => part.trim() && declinesMinify(options, 'canMinifyCSS', part, undefined))) {
          const minified = await options.minifyCSS(css, undefined, options.cssContext);
          // Where the whole is left as is, CSS left to it may still be minified on its own
          if (minified !== css || !merged.some(record => record.deferred)) {
            bufferSet(first.content, minified);
            return;
          }
        }
        const minified = await Promise.all(merged.map((record, i) => record.deferred
          ? options.minifyCSS(parts[i] ?? '', undefined, options.cssContext)
          : parts[i] ?? ''));
        bufferSet(first.content, joinStyleSheets(minified));
      })());
    }
    await Promise.all(pending);
  }

  // With whitespace kept as is, a removed node must not leave a line of nothing but
  // whitespace behind: The text around the removal (at `index`) drops that line. The start
  // of the output counts as a line break, and blank lines of the source stay.
  function removeWhitespaceLine(/** @type {number} */ index) {
    const [first, after] = findNeighbors(index);
    // The end of the output is left to `trimOutputWhitespace`
    if (after === buffer.length) {
      return;
    }
    const next = buffer[after] ?? '';
    if (!RE_BLANKS_LINE_BREAK_START.test(next)) {
      return;
    }
    // Blanks between nodes removed from the same line belong to that line
    let before = first;
    while (before >= 0 && (buffer[before] === '' || RE_BLANKS_ONLY.test(buffer[before] ?? ''))) {
      before--;
    }
    const prev = buffer[before] ?? '';
    if (before >= 0 && !RE_LINE_BREAK_BLANKS_END.test(prev)) {
      return;
    }
    for (let i = before + 1; i <= first; i++) {
      bufferSet(i, '');
    }
    // The line break goes on the side of the text before the removal, so that the text after
    // it keeps its own for a removal that may follow
    if (before < 0) {
      bufferSet(after, next.replace(RE_BLANKS_LINE_BREAK_START, ''));
    } else {
      bufferSet(before, prev.replace(RE_LINE_BREAK_BLANKS_END, ''));
      bufferSet(after, next.replace(RE_BLANKS_START, ''));
    }
  }

  // Whitespace at either end of the output goes, but not verbatim whitespace, nor whitespace
  // that `conservativeCollapse` or `preserveLineBreaks` promise to keep, nor that of partial
  // markup, whose ends join other markup
  function trimOutputWhitespace() {
    if (options.partialMarkup || (options.collapseWhitespace && (options.conservativeCollapse || options.preserveLineBreaks))) {
      return;
    }
    // Tags start with `<` and end with `>`, so whitespace at either end belongs to text
    if (!textVerbatim) {
      let index = buffer.length - 1;
      while (index >= 0 && (buffer[index] === '' || RE_WS_ONLY.test(buffer[index] ?? ''))) {
        bufferSet(index--, '');
      }
      if (index >= 0) {
        bufferSet(index, (buffer[index] ?? '').replace(RE_WS_END, ''));
      }
    }
    let index = 0;
    while (index < buffer.length && (buffer[index] === '' || RE_WS_ONLY.test(buffer[index] ?? ''))) {
      bufferSet(index++, '');
    }
    if (index < buffer.length) {
      bufferSet(index, (buffer[index] ?? '').replace(RE_WS_START, ''));
    }
  }

  // Whitespace next to an `htmlmin:ignore` block that starts or ends with whitespace renders
  // the same without it; a line break under `preserveLineBreaks` stays unless the block has one
  function trimIgnoreEdges() {
    const indexStart = '<!--'.length + (uidIgnore ?? '').length;
    for (let i = 0; i < ignoredMarkupCollapsing.length; i += 2) {
      const placeholder = /** @type {string} */ (ignoredMarkupCollapsing[i]);
      const indexPushed = /** @type {number} */ (ignoredMarkupCollapsing[i + 1]);
      // Entries removed before a placeholder move it down
      const index = buffer[indexPushed] === placeholder ? indexPushed : buffer.lastIndexOf(placeholder, indexPushed);
      if (index === -1) continue;
      const content = ignoredMarkupChunks[+placeholder.slice(indexStart, -3)] ?? '';
      trimIgnoreEdge(content, findNeighbors(index)[0], false);
      trimIgnoreEdge(content, findNeighbors(index + 1)[1], true);
    }
  }

  // `atEnd` is the side of the block, `neighbor` the entry on that side
  function trimIgnoreEdge(/** @type {string} */ content, /** @type {number} */ neighbor, /** @type {boolean} */ atEnd) {
    let text = buffer[neighbor];
    if (!text) return;
    // Text passing SVGO as a placeholder is trimmed where it’s kept
    const token = uidSVGRawText && text.startsWith('/*!' + uidSVGRawText) && text.endsWith('*/')
      ? +text.slice(3 + uidSVGRawText.length, -2)
      : -1;
    if (token !== -1) text = svgRawTexts[token];
    if (!text) return;
    const inner = edgeWhitespaceLength(content, atEnd);
    const outer = inner && edgeWhitespaceLength(text, !atEnd);
    if (!outer) return;
    if (options.preserveLineBreaks &&
        RE_LINE_BREAK.test(atEnd ? text.slice(0, outer) : text.slice(-outer)) &&
        !RE_LINE_BREAK.test(atEnd ? content.slice(-inner) : content.slice(0, inner))) return;
    const trimmed = atEnd ? text.slice(outer) : text.slice(0, -outer);
    if (token !== -1) svgRawTexts[token] = trimmed;
    else bufferSet(neighbor, trimmed);
  }

  function lastStartTagIndex() {
    let index = buffer.length - 1;
    while (index > 0 && !RE_START_TAG.test(buffer[index] ?? '')) {
      index--;
    }
    return index;
  }

  function removeStartTag(/** @type {boolean} */ capture = false) {
    const index = lastStartTagIndex();
    const text = capture ? buffer.slice(index).join('') : undefined;
    bufferTruncate(index);
    return text;
  }

  // Write the last start tag as self-closing, where it is the last thing written and holds no
  // custom fragment, which may write anything; returns whether it did
  function closeLastStartTag() {
    const start = lastStartTagIndex();
    let last = buffer.length - 1;
    while (last > start && buffer[last] === '') {
      last--;
    }
    for (let i = start; i <= last; i++) {
      const entry = buffer[i] ?? '';
      if ((i > start && entry[0] === '<') || (uidAttr && entry.indexOf(uidAttr) !== -1)) {
        return false;
      }
    }
    const tagText = buffer[last] ?? '';
    if (!tagText.endsWith('>') || tagText.endsWith('/>')) {
      return false;
    }
    const tagOpen = tagText.slice(0, -1);
    // A slash right after an unquoted value would belong to the value
    bufferSet(last, tagOpen + (RE_UNQUOTED_VALUE_END.test(tagOpen) ? ' />' : '/>'));
    return true;
  }

  /** @param {string} lowerTag */
  function enterForeignScope(lowerTag) {
    if (lowerTag === 'svg' || lowerTag === 'math' ||
        (options.namespaceForeign && integrationPointsForeign[options.namespaceForeign].has(lowerTag))) {
      foreignScopes.push(lowerTag);
    }
  }

  // Leaves the scope the element opened, if any; returns whether a slash would close the element
  /** @param {string} lowerTag */
  function leaveForeignScope(lowerTag) {
    if (foreignScopes[foreignScopes.length - 1] === lowerTag) {
      foreignScopes.pop();
    }
    const scope = foreignScopes[foreignScopes.length - 1];
    return lowerTag === 'svg' || lowerTag === 'math' || scope === undefined || scope === 'svg' || scope === 'math';
  }

  // Browsers can close a formatting element with others left open inside it;
  // any other end tag that doesn’t match the top entry of a stack is stray
  function canCloseBelowTop(/** @type {string} */ tag, /** @type {string} */ lowerTag) {
    return formattingElements.has((lowerTag || tag).toLowerCase());
  }

  // Returns whether `tag` was open (`belowTop` where the parser closed it below the top)
  function removeClosedTag(/** @type {string[]} */ tags, /** @type {string} */ tag, /** @type {string} */ lowerTag, belowTop = false) {
    if (tags[tags.length - 1] === tag) {
      tags.pop();
      return true;
    }
    const index = belowTop || canCloseBelowTop(tag, lowerTag) ? tags.lastIndexOf(tag) : -1;
    if (index === -1) {
      return false;
    }
    tags.splice(index, 1);
    return true;
  }

  function markOpenElementFilled() {
    // Guarded, as reading index -1 is a slow property lookup, and this runs for every text node
    if (openElements.length) {
      /** @type {{empty: boolean}} */ (openElements[openElements.length - 1]).empty = false;
    }
  }

  // Whether the element being parsed holds text rather than markup. The namespace decides
  // this, not the name alone: Only HTML elements do—an SVG or MathML `script` or `title`
  // holds markup like any foreign element
  function holdsRawText() {
    return rawTextElements.has(currentTag) && !options.insideForeignContent;
  }

  // Whether whitespace-only text between tags survives minification
  function keepsWhitespace() {
    return !options.collapseWhitespace || Boolean(options.conservativeCollapse) || Boolean(options.preserveLineBreaks);
  }

  // The two whitespace runs an omitted tag separated become one run, which collapses to the
  // single character it is due: a line break under `preserveLineBreaks`, a no-break space
  // where the run holds one, a space under `conservativeCollapse`
  function mergeWhitespaceRuns(/** @type {number} */ index) {
    const [before, after] = findNeighbors(index);
    if (before < 0 || after >= buffer.length) {
      return;
    }
    const prev = buffer[before] ?? '';
    const next = buffer[after] ?? '';
    if (!RE_WS_NBSP_END.test(prev) || !RE_WS_START.test(next)) {
      return;
    }
    // A line break the run already carries outranks the whitespace ahead of it; trimming
    // the other side takes ASCII whitespace only, so a no-break space stays as is
    if (options.preserveLineBreaks && /[\n\r]/.test((next.match(RE_WS_START) ?? [''])[0])) {
      bufferSet(before, prev.replace(RE_WS_END, ''));
    } else {
      bufferSet(after, next.replace(RE_WS_START, ''));
    }
  }

  // `insideNoTrim` stands in for the stack once the end tag that omits this one has left it
  function removeEndTag(/** @type {boolean} */ insideNoTrim = false) {
    let index = buffer.length - 1;
    while (index > 0 && !RE_END_TAG.test(buffer[index] ?? '')) {
      index--;
    }
    const text = buffer[index] ?? '';
    // Text that follows the end tag is kept as is, so drop the tag alone. Whitespace kept
    // verbatim—in `pre`, in `textarea`, or wherever `canTrimWhitespace` says so—counts as
    // kept text too, no matter how whitespace is collapsed elsewhere.
    const noTrim = insideNoTrim || stackNoTrimWhitespace.length > 0;
    if (index < buffer.length - 1 && (keepsWhitespace() || noTrim) && RE_END_TAG.test(text)) {
      bufferRemove(index);
      // Collapsed whitespace can merge, kept whitespace can leave a line of only whitespace
      if (!noTrim) {
        if (options.collapseWhitespace) {
          mergeWhitespaceRuns(index);
        } else {
          removeWhitespaceLine(index);
        }
      }
      return text;
    }
    bufferTruncate(index);
    return text;
  }

  // Look for trailing whitespace, bypassing inline tags
  function trimTrailingWhitespace(/** @type {number} */ index, /** @type {string} */ nextTag) {
    // Nothing to the left of the text is in play here, so only its trailing run can
    // change—unless preserved line breaks bring the leading run into play, too
    const trailingOnly = !options.preserveLineBreaks;
    const indexStart = index;
    let prevTag = '';
    let prevTagLower = '';
    for (; index > noTrimEndIndex && canTrimWhitespace(prevTagLower, prevTag, emptyAttrs); index--) {
      if (index === trimSettledIndex) {
        trimSettledIndex = indexStart;
        return;
      }
      // Pending whitespace waits for what follows the elements that don’t render
      if (index === transparentGapIndex) {
        return;
      }
      const str = buffer[index] ?? '';
      if (!str) {
        // Up to an entry the checks above stop at
        const indexTo = skipRun(runEmpty, index);
        if (indexTo < index - 1 && !(trimSettledIndex > indexTo && trimSettledIndex < index) && !(transparentGapIndex > indexTo && transparentGapIndex < index)) {
          index = indexTo + 1;
        }
        continue;
      }
      const endTagName = matchPlainEndTag(str);
      if (endTagName !== null) {
        prevTag = endTagName;
        prevTagLower = prevTag.toLowerCase();
        continue;
      }
      // A tag ends the search, and so does text that survives collapsing. Text with no
      // trailing run comes back from the call unchanged, so it can end the search on
      // the spot, and so can every later search that gets as far as this one began
      if (str.charCodeAt(str.length - 1) === 62 /* > */ || (trailingOnly && !endsWithWhitespace(str))) {
        trimSettledIndex = indexStart;
        break;
      }
      bufferSet(index, collapseWhitespaceSmart(str, '', nextTag, emptyAttrs, emptyAttrs, options, inlineSets, !stackNoCollapseWhitespace.length && index > noCollapseEndIndex));
      if (buffer[index]) {
        break;
      }
    }
  }

  // Look for trailing whitespace in earlier text that a following comment, or an empty element
  // removed since, kept from being trimmed
  function squashTrailingWhitespace(/** @type {string} */ nextTag) {
    // Fast path: With no transparent gap pending, a start tag or text without trailing
    // whitespace last in the buffer ends the walk below on that entry. That leaves text
    // alone as well where `preserveLineBreaks` has the walk collapse it again, as the
    // text’s own pass collapsed it already.
    if (transparentGapIndex === -1) {
      const last = buffer[buffer.length - 1] ?? '';
      if (last) {
        if (last.charCodeAt(0) !== 60 /* < */) {
          if (!endsWithWhitespace(last)) {
            return;
          }
        } else {
          const second = last.charCodeAt(1);
          if (second !== 47 /* / */ && second !== 33 /* ! */ && last.charCodeAt(last.length - 1) === 62 /* > */) {
            return;
          }
        }
      }
    }
    if (transparentGapIndex !== -1 && !transparentDepth && !inlineElementsTransparentAfter.has(nextTag) && !inlineElementsTransparent.has(nextTag)) {
      settleTransparentGap(collapseWhitespaceSmart(' ', '', nextTag, emptyAttrs, emptyAttrs, options, inlineSets) !== '');
    }
    // Step back over every trailing comment and empty entry, not just one—a single step
    // stops the search at the comment itself, leaving whitespace behind that a second
    // minification run then removes
    const charsIndex = skipRun(runQuiet, buffer.length - 1);
    // Right before an element that doesn’t render, the text’s own pass has
    // weighed both sides already, and pending whitespace waits for what follows
    if ((charsIndex === buffer.length - 1 || charsIndex === transparentGapIndex) && inlineElementsTransparent.has(nextTag)) {
      return;
    }
    if (holdTransparentGap(charsIndex, nextTag)) {
      return;
    }
    trimTrailingWhitespace(charsIndex, nextTag);
  }

  // Whitespace weighed against a comment or an element removed since waits for what follows
  // the element that doesn’t render, too; returns whether it does
  function holdTransparentGap(/** @type {number} */ index, /** @type {string} */ nextTag) {
    if (transparentGapIndex !== -1 || transparentDepth || !inlineElementsTransparent.has(nextTag)) {
      return false;
    }
    index = skipRun(runEmpty, index);
    if (!endsWithWhitespace(buffer[index] ?? '')) {
      return false;
    }
    transparentGapIndex = index;
    return true;
  }

  // Whether text starts with whitespace, which the tab padding a custom fragment’s placeholder
  // isn’t, unlike whitespace the fragment itself starts with
  function startsWithWhitespace(/** @type {string} */ text) {
    if (uidAttr && text.startsWith('\t' + uidAttr)) {
      const match = uidAttrLeadingPattern?.exec(text);
      return Boolean(match && ignoredCustomMarkupChunks[+(match[1] ?? 0)]?.[1]);
    }
    return RE_LEADING_WHITESPACE.test(text);
  }

  // Whether text ends with whitespace, likewise
  function endsWithTextWhitespace(/** @type {string} */ text) {
    if (uidAttr && text.endsWith(uidAttr + '\t')) {
      const match = uidAttrTrailingPattern?.exec(text);
      return Boolean(match && ignoredCustomMarkupChunks[+(match[1] ?? 0)]?.[2]);
    }
    return endsWithWhitespace(text);
  }

  // Keep or trim the whitespace pending before or between elements that don’t render
  function settleTransparentGap(/** @type {boolean} */ keep) {
    if (!keep) {
      bufferSet(transparentGapIndex, collapseWhitespace(buffer[transparentGapIndex] ?? '', options, false, true));
    }
    transparentGapIndex = -1;
  }

  // The index of the start tag whose attributes, which follow it as entries
  // of their own, end at `index`, or -1 for text
  function findAttributesStartTag(/** @type {number} */ index) {
    if ((buffer[index] ?? '').charCodeAt((buffer[index] ?? '').length - 1) !== 62 /* > */) return -1;
    for (let tagIndex = index - 1; tagIndex >= 0; tagIndex--) {
      const str = buffer[tagIndex] ?? '';
      if (str.charCodeAt(0) === 60 /* < */) {
        return RE_START_TAG.test(str) && str.charCodeAt(str.length - 1) !== 62 /* > */ ? tagIndex : -1;
      }
      if (!str || str.charCodeAt(str.length - 1) === 62 /* > */) return -1;
    }
    return -1;
  }

  // Whether what comes before the elements that don’t render at the end of `buffer`
  // would run into what follows them; without `skipAttributes`, the scan stops at
  // a start tag’s attributes and says yes, as stepping over them on every
  // call makes the scan quadratic over long runs
  function precedesTransparentRun(/** @type {boolean} */ skipAttributes = false) {
    for (let index = buffer.length - 1; index >= 0; index--) {
      let str = buffer[index] ?? '';
      if (!str) continue;
      if (str.charCodeAt(0) !== 60 /* < */) {
        const tagIndex = skipAttributes ? findAttributesStartTag(index) : -1;
        if (tagIndex === -1) {
          return !endsWithWhitespace(str);
        }
        index = tagIndex;
        str = buffer[index] ?? '';
      }
      // Comments don’t render, whereas what an `htmlmin:ignore` block
      // or a processing instruction stands for may
      if (str.charCodeAt(1) === 33 /* ! */ || str.charCodeAt(1) === 63 /* ? */) {
        if (str.charCodeAt(1) === 63 || (uidIgnore && str.indexOf(uidIgnore) !== -1)) return true;
        continue;
      }
      const endTagName = matchPlainEndTag(str);
      const tag = (endTagName === null ? (RE_START_TAG_NAME.exec(str)?.[1] ?? '') : '/' + endTagName).toLowerCase();
      if (!inlineElementsTransparentAfter.has(tag)) {
        return collapseWhitespaceSmart(' ', tag, '', emptyAttrs, emptyAttrs, options, inlineSets) !== '';
      }
      // Skip what an element that doesn’t render holds
      if (endTagName !== null) {
        const startTag = '<' + endTagName.toLowerCase();
        // Only the start of each entry is compared, as what such an element holds can be large
        while (index > 0 && (buffer[index - 1] ?? '').slice(0, startTag.length).toLowerCase() !== startTag) {
          index--;
        }
        index--;
      }
    }
    // What precedes partial markup may run into it, too
    return keepsEdges;
  }

  // Per-text-node context for `charsCollapse`/`charsFinalize`, set by the `chars`
  // handler before either phase runs; shared variables (rather than parameters)
  // let both phases live at this scope—`charsCollapse` reassigns `textPrevTag`
  // and `charsFinalize` must see that change. Defining the phases inside the
  // `chars` handler instead would allocate two closures per text node.
  /** @type {string} */
  let textPrevTag = '';
  /** @type {string} */
  let textNextTag = '';
  /** @type {HTMLAttribute[]} */
  let textPrevAttrs = emptyAttrs;
  /** @type {HTMLAttribute[]} */
  let textNextAttrs = emptyAttrs;
  // Whether the text is in foreign content, and if so, whether it was read as HTML reads it
  // and so is to be escaped again
  let textForeign = false;
  let textForeignDecoded = false;
  // Whether the text loses its trailing whitespace to the tag after it, and its entry
  // in `buffer`, for that whitespace to come back should the element go
  let textTrimmedEnd = false;
  let textTrimmedEndIndex = -1;

  // Whitespace collapsing phase (sync)
  function charsCollapse(/** @type {string} */ text) {
    const textIn = text;
    // Trim a single trailing newline inside `pre`, often a template engine artifact (several are
    // likely intentional formatting); in `textarea`, it is part of the value
    if (options.collapseWhitespace && stackNoTrimWhitespace.length) {
      const topTag = stackNoTrimWhitespace[stackNoTrimWhitespace.length - 1];
      if (preDepth > 0 && topTag?.toLowerCase() !== 'textarea') {
        // Treat CRLF (`\r\n`), CR (`\r`), and LF (`\n`) as single line-ending units
        if (textNextTag && textNextTag === '/' + topTag && /[^\r\n](?:\r\n|\r|\n)[ \t]*$/.test(text)) {
          text = text.replace(RE_LINE_BREAK_BLANKS_END, '');
        }
      }
    }
    if (options.collapseWhitespace) {
      if (!stackNoTrimWhitespace.length) {
        // When the prev item is a UID placeholder, compute its effective tag name for whitespace
        // decisions; this is only used in `collapseWhitespaceSmart`—`textPrevTag` itself is not
        // modified, to avoid side effects on the `inlineTextSet` branch below
        let effectivePrevTag = textPrevTag;
        const followsComment = textPrevTag === 'comment';
        if (followsComment) {
          const prevComment = buffer[buffer.length - 1] ?? '';
          if (!uidIgnore || prevComment.indexOf(uidIgnore) === -1) {
            if (!prevComment) {
              textPrevTag = charsPrevTag;
              effectivePrevTag = textPrevTag;
            }
            // Past comments removed before, and leaving pending whitespace where it is
            const charsIndex = skipRun(runEmpty, buffer.length - 2);
            if (charsIndex >= 0 && charsIndex !== transparentGapIndex && (!prevComment || (!options.conservativeCollapse && / $/.test(currentChars)))) {
              bufferSet(charsIndex, (buffer[charsIndex] ?? '').replace(/\s+$/, function (trailingSpaces) {
                text = trailingSpaces + text;
                // It moved, and is judged again as part of this text; what is left before it
                // doesn’t end in whitespace anymore
                currentChars = currentChars.replace(/\s+$/, '');
                return '';
              }));
            }
          } else if (uidIgnorePlaceholderPattern && textNextTag !== 'comment') {
            // UID placeholder followed by a real element—derive the effective `prevTag` from the
            // placeholder’s last HTML tag so `collapseWhitespaceSmart` can make the right call;
            // `commentFinalize` handles a `textNextTag` of `comment` (another UID placeholder)
            const match = prevComment.match(uidIgnorePlaceholderPattern);
            if (match) {
              const idx = +(match[1] ?? '');
              if (idx < ignoredMarkupChunks.length) {
                const content = ignoredMarkupChunks[idx];
                const group = content && findLastHtmlTag(content);
                if (group) {
                  const isClose = group.charAt(0) === '/';
                  const tagName = resolveName(isClose ? group.slice(1) : group);
                  effectivePrevTag = isClose ? '/' + tagName : tagName;
                }
              }
            }
          }
        }
        if (textPrevTag) {
          if (textPrevTag === '/nobr' || textPrevTag === 'wbr') {
            if (/^\s/.test(text)) {
              let tagIndex = buffer.length - 1;
              while (tagIndex > 0 && (namesAsWritten ? (buffer[tagIndex] ?? '').toLowerCase() : buffer[tagIndex] ?? '').lastIndexOf('<' + textPrevTag) !== 0) {
                tagIndex--;
              }
              trimTrailingWhitespace(tagIndex - 1, 'br');
            }
          } else if (inlineSets.withinEither.has(textPrevTag)) {
            text = collapseWhitespace(text, options, RE_ENDS_COLLAPSIBLE.test(currentChars), false, false, stackNoCollapseWhitespace.length > 0);
          }
        }
        if (textPrevTag || textNextTag) {
          // Beyond an edge of partial markup, anything may render, as for a processing instruction
          const smartPrevTag = keepsEdges && effectivePrevTag === '' ? processingInstructionTag : effectivePrevTag;
          const smartNextTag = keepsEdges && textNextTag === '' ? processingInstructionTag : textNextTag;
          let collapsed = collapseWhitespaceSmart(text, smartPrevTag, smartNextTag, textPrevAttrs, textNextAttrs, options, inlineSets, !stackNoCollapseWhitespace.length);
          if (!transparentDepth && inlineElementsTransparentAfter.has(textPrevTag)) {
            // Between elements that don’t render, whitespace stays pending when it may
            // separate text, and one space does
            if (!collapsed && text && inlineElementsTransparent.has(textNextTag)) {
              if (transparentGapIndex === -1 && precedesTransparentRun()) {
                transparentGapPending = true;
                text = ' ';
              } else {
                text = '';
              }
            } else {
              // Text that follows decides, while a tag that follows does so in
              // `squashTrailingWhitespace`
              if (startsWithWhitespace(collapsed)) {
                if (transparentGapIndex !== -1) settleTransparentGap(false);
                // Where nothing before the elements renders a space, the text’s own goes, too
                if (!options.conservativeCollapse && !precedesTransparentRun(true)) {
                  collapsed = collapseWhitespace(collapsed, options, true, false);
                }
              } else if (transparentGapIndex !== -1 && collapsed) {
                settleTransparentGap(true);
              }
              text = collapsed;
            }
          } else {
            text = collapsed;
            // Neither does a comment or an element removed, so text after one decides on
            // pending whitespace
            if (transparentGapIndex !== -1 && !transparentDepth && text) {
              settleTransparentGap(!startsWithWhitespace(text));
            }
            // Whitespace before an element that doesn’t render waits for what follows it, too
            if (!transparentDepth && inlineElementsTransparent.has(textNextTag) && endsWithWhitespace(text)) {
              transparentGapPending = true;
            }
          }
        } else {
          // Raw text has no tag on either side, unlike an edge of partial markup
          text = collapseWhitespace(text, options, !keepsEdges || textPrevTag !== '', !keepsEdges || textNextTag !== '');
        }
        if (!text && RE_TRAILING_WHITESPACE.test(currentChars) && textPrevTag && textPrevTag.charAt(0) === '/' && !holdTransparentGap(buffer.length - 1, textNextTag)) {
          trimTrailingWhitespace(buffer.length - 1, textNextTag);
        }
      }
      // Whitespace that may not be trimmed stays verbatim, as in `pre`
      if (!stackNoCollapseWhitespace.length && !stackNoTrimWhitespace.length && textNextTag !== 'html' && !(textPrevTag && textNextTag)) {
        text = collapseWhitespace(text, options, false, false, true);
      }
      if (options.collapseNoBreakSpaces && !stackNoCollapseWhitespace.length && !holdsRawText()) {
        text = collapseNoBreakSpaces(text, options);
      }
    }
    // Whitespace-only text loses its whitespace to the next tag if text in its place keeps it
    textTrimmedEnd = Boolean(options.removeEmptyElements && options.collapseWhitespace) && endsWithTextWhitespace(textIn) && !endsWithWhitespace(text) &&
      (text !== '' || (!stackNoTrimWhitespace.length && !endsWithWhitespace(currentChars) && Boolean(textNextTag) && textNextTag.charAt(0) !== '/' && collapseWhitespaceSmart(textIn, textPrevTag, 'comment', textPrevAttrs, emptyAttrs, options, inlineSets) !== ''));
    return text;
  }

  // Finalization phase (sync): Optional tag handling, entity re-encoding, buffer push
  function charsFinalize(/** @type {string} */ text) {
    if (options.removeOptionalTags && text) {
      // UID-attr tokens are padded with `\t`, which would falsely look like leading whitespace;
      // resolve single-token text to its actual content for the space/comment checks below
      let effectiveText = text;
      if (uidAttrLeadingPattern && uidAttr && text.includes(uidAttr)) {
        const uidMatch = uidAttrLeadingPattern.exec(text);
        if (uidMatch) {
          const idx = +(uidMatch[1] ?? 0);
          const chunks = idx < ignoredCustomMarkupChunks.length ? ignoredCustomMarkupChunks[idx] : null;
          if (chunks != null) {
            effectiveText = chunks[0] ?? '';
          }
        }
      }
      // `<html>` may be omitted if first thing inside is not a comment
      // `<body>` may be omitted if first thing inside is not space, comment, `<meta>`, `<link>`,
      //   `<script>`, `<style>`, or `<template>`
      if (optionalStartTag === 'html' || (optionalStartTag === 'body' && !/^\s/.test(effectiveText))) {
        removeStartTag();
      }
      optionalStartTag = '';
      // `</html>` or `</body>` may be omitted if not followed by comment
      // `</head>`, `</colgroup>`, or `</caption>` may be omitted if not followed by space or
      //   comment
      if (optionalEndTagEmitted && (compactElements.has(optionalEndTag) || (looseElements.has(optionalEndTag) && !/^\s/.test(effectiveText)))) {
        removeEndTag();
        // The tag is gone from the buffer, so nothing is left to omit later
        optionalEndTag = '';
        optionalEndTagEmitted = false;
      }
      // Whitespace-only text doesn’t prevent omission of the pending end tag: It’s either
      // collapsed away, or kept in place while the tag alone is dropped—except for
      // `</head>`, `</colgroup>`, and `</caption>`, which may not be followed by space.
      // Only ASCII whitespace counts, as no-break space and friends are content
      // https://html.spec.whatwg.org/multipage/syntax.html#optional-tags
      if (!RE_WS_ONLY.test(text) || (keepsWhitespace() && looseElements.has(optionalEndTag))) {
        optionalEndTag = '';
        optionalEndTagEmitted = false;
      }
    }
    charsPrevTag = isAllWhitespace(text) ? textPrevTag : 'comment';
    const rawText = Boolean(text) && holdsRawText();
    if (text) {
      textVerbatim = rawText || stackNoTrimWhitespace.length > 0;
    }
    if (text && options.insideSVG && options.minifySVG) {
      // SVGO reads XML, so text is escaped for it, while raw text, which XML cannot read as
      // HTML does (as in `foreignObject`), passes SVGO as a placeholder—as does HTML text
      // with whitespace at an end, which SVGO trims (`trim()`, so no-break spaces, too)
      // outside SVG text elements
      if (!rawText) {
        text = escapeXML(text);
        if (!options.insideForeignContent && /^\s|\s$/.test(text)) {
          uidSVGRawText ||= uniqueId(value);
          svgRawTexts.push(text);
          text = '/*!' + uidSVGRawText + (svgRawTexts.length - 1) + '*/';
        }
      } else if (text.indexOf('&') !== -1 || text.indexOf('<') !== -1 || text.indexOf(']]>') !== -1) {
        uidSVGRawText ||= uniqueId(value);
        svgRawTexts.push(text);
        text = '/*!' + uidSVGRawText + (svgRawTexts.length - 1) + '*/';
      }
    } else if (text && (textForeign ? textForeignDecoded : options.decodeEntities && !rawText)) {
      // Escape every `&` that starts a character reference, legacy-named (without `;`) or not;
      // `&amp` needs no semicolon either
      // https://mathiasbynens.be/notes/ambiguous-ampersands
      if (text.indexOf('&') !== -1) {
        text = text.replace(RE_LEGACY_ENTITIES, '&amp$1');
      }
      if (text.indexOf('<') !== -1) {
        // Escapable raw text ends at its own end tag alone, so only that one needs escaping;
        // in foreign content the same element holds markup, where every `<` does
        const escapeLt = (textForeign ? undefined : RE_ESCAPE_LT_RAW_TEXT[currentTag]) ?? RE_ESCAPE_LT;
        text = text.replace(escapeLt, '&lt;');
      }
      if (text.indexOf('\r') !== -1) {
        text = text.replace(RE_CR, '&#13;');
      }
    } else if (text && textForeign && text.lastIndexOf('<![CDATA[') > text.lastIndexOf(']]>')) {
      // A CDATA section left open at the end of the input closes before the
      // end tags written after it
      text += ']]>';
    }
    if (uidPattern && options.collapseWhitespace && stackNoTrimWhitespace.length) {
      text = text.replace(/** @type {RegExp} */ (uidPattern), function (/** @type {string} */ match, /** @type {string} */ _prefix, /** @type {string} */ index) {
        return ignoredCustomMarkupChunks[+index]?.[0] ?? match;
      });
    }
    currentChars += text;
    if (text) {
      hasChars = true;
      markOpenElementFilled();
    }
    const followsRemoval = removed && Boolean(text) && !options.collapseWhitespace && !stackNoTrimWhitespace.length;
    if (transparentGapPending) {
      transparentGapPending = false;
      if (text) transparentGapIndex = buffer.length;
    }
    bufferPush(text, false);
    textTrimmedEndIndex = textTrimmedEnd ? buffer.length - 1 : -1;
    if (followsRemoval) {
      removeWhitespaceLine(buffer.length - 1);
    }
  }

  // Comment finalization (sync): Optional tags, `htmlmin:ignore` whitespace, buffer push
  function commentFinalize(/** @type {string} */ comment) {
    if (options.removeOptionalTags && comment) {
      if (uidIgnorePlaceholderPattern) {
        const match = uidIgnorePlaceholderPattern.exec(comment);
        if (match) {
          // UID placeholders represent real HTML content, not true HTML comments;
          // if there’s a pending optional end tag and the ignored content isn’t itself
          // a comment (which per the HTML spec prevents omission), resolve it now,
          // before the UID is pushed to the buffer
          const idx = +(match[1] ?? 0);
          const content = idx < ignoredMarkupChunks.length ? ignoredMarkupChunks[idx] : null;
          if (optionalEndTag && optionalEndTagEmitted && content != null && !/^\s*<!--/.test(content)) {
            const firstTagMatch = content.match(/^\s*<([a-zA-Z][^\s/>]*)/);
            const firstTagGroup = firstTagMatch?.[1] ?? '';
            const firstTag = firstTagGroup ? resolveName(firstTagGroup) : '';
            if (canRemovePrecedingTag(optionalEndTag, firstTag)) {
              removeEndTag();
            }
          }
        }
      }
      // Comments (real or placeholder) always suppress optional start tag omissions
      optionalStartTag = '';
      optionalEndTag = '';
      optionalEndTagEmitted = false;
    }

    // Whitespace between an element that doesn’t render and an `htmlmin:ignore` block
    // that starts with one depends on what lies before both
    if (options.collapseWhitespace && comment && uidIgnorePlaceholderPattern && !transparentDepth && !stackNoTrimWhitespace.length && buffer.length >= 2) {
      const prevText = buffer[buffer.length - 1] ?? '';
      const match = prevText && RE_WS_ONLY.test(prevText) && uidIgnorePlaceholderPattern.exec(comment);
      const firstTag = match ? RE_FIRST_HTML_TAG.exec(ignoredMarkupChunks[+(match[1] ?? 0)] ?? '')?.[1] : undefined;
      if (firstTag && inlineElementsTransparent.has(resolveName(firstTag)) && inlineElementsTransparentAfter.has(charsPrevTag)) {
        // Emptied first, as `precedesTransparentRun` stops at text
        bufferSet(buffer.length - 1, '');
        if (transparentGapIndex === buffer.length - 1) transparentGapIndex = -1;
        bufferSet(buffer.length - 1, options.conservativeCollapse || precedesTransparentRun(true) ? ' ' : '');
      }
    }
    // What an `htmlmin:ignore` block holds is unknown, so whitespace pending before it stays
    if (transparentGapIndex !== -1 && !transparentDepth && comment && uidIgnorePlaceholderPattern?.test(comment)) {
      settleTransparentGap(true);
    }

    // Optimize whitespace collapsing between consecutive `htmlmin:ignore` placeholder comments
    if (options.collapseWhitespace && comment && uidIgnorePlaceholderPattern) {
      if (uidIgnorePlaceholderPattern.test(comment)) {
        // Check if previous buffer items are: [ignore-placeholder, whitespace-only text]
        if (buffer.length >= 2) {
          const prevText = buffer[buffer.length - 1];
          const prevComment = buffer[buffer.length - 2];

          if (prevText && RE_WHITESPACE_ONLY.test(prevText) && prevComment && uidIgnorePlaceholderPattern.test(prevComment)) {
            const currentMatch = comment.match(uidIgnorePlaceholderPattern);
            const prevMatch = prevComment.match(uidIgnorePlaceholderPattern);

            if (currentMatch && prevMatch) {
              const currentIndex = +(currentMatch[1] ?? 0);
              const prevIndex = +(prevMatch[1] ?? 0);

              if (currentIndex < ignoredMarkupChunks.length && prevIndex < ignoredMarkupChunks.length) {
                const currentContent = ignoredMarkupChunks[currentIndex];
                const prevContent = ignoredMarkupChunks[prevIndex];

                // Only collapse whitespace if both blocks contain HTML (start with `<`)
                // Don’t collapse if either contains plain text, as that would change meaning
                if (currentContent && prevContent && /^\s*</.test(currentContent) && /^\s*</.test(prevContent)) {
                  const currentTagMatch = currentContent.match(RE_FIRST_HTML_TAG);
                  const prevTagMatch = prevContent.match(RE_FIRST_HTML_TAG);
                  // HTML comments are invisible (no block/inline nature), treat as non-inline
                  const prevIsHtmlComment = !prevTagMatch && RE_HTML_COMMENT_START.test(prevContent);
                  const currentIsHtmlComment = !currentTagMatch && RE_HTML_COMMENT_START.test(currentContent);
                  // Closing tags (e.g., `</div>`)—inline-ness determines whether to collapse
                  const prevClosingTagMatch = !prevTagMatch && RE_CLOSING_TAG_START.exec(prevContent);
                  const currentClosingTagMatch = !currentTagMatch && RE_CLOSING_TAG_START.exec(currentContent);

                  // Collapse if both sides are element/closing tags or HTML
                  // comments, and neither is inline
                  if ((currentTagMatch || currentIsHtmlComment || currentClosingTagMatch) &&
                      (prevTagMatch || prevIsHtmlComment || prevClosingTagMatch)) {
                    const currentTagName = currentTagMatch ? resolveName(currentTagMatch[1] ?? '')
                      : currentClosingTagMatch ? resolveName(currentClosingTagMatch[1] ?? '') : null;
                    const prevTagName = prevTagMatch ? resolveName(prevTagMatch[1] ?? '')
                      : prevClosingTagMatch ? resolveName(prevClosingTagMatch[1] ?? '') : null;

                    // Don’t collapse between inline elements (HTML comments count as non-inline)
                    if (!inlineElements.has(currentTagName ?? '') && !inlineElements.has(prevTagName ?? '')) {
                      let collapsedText = prevText;

                      if (!stackNoTrimWhitespace.length && !stackNoCollapseWhitespace.length) {
                        // Not in pre or other no-collapse context
                        if (prevText.includes('\xA0')) {
                          // No-break spaces are content, so every one of them survives in place;
                          // the ASCII whitespace around them collapses as it would anywhere else
                          collapsedText = collapseWhitespace(prevText, options, true, true, true);
                        } else if (options.preserveLineBreaks && /[\n\r]/.test(prevText)) {
                          // Preserve line break as single newline
                          collapsedText = '\n';
                        } else if (options.conservativeCollapse) {
                          // Conservative mode: Keep single space
                          collapsedText = ' ';
                        } else {
                          // Aggressive mode: Remove all whitespace
                          collapsedText = '';
                        }
                      }

                      bufferSet(buffer.length - 1, collapsedText);
                    }
                  }
                }
              }
            }
          }
        }
        if (!stackNoTrimWhitespace.length && !stackNoCollapseWhitespace.length) {
          ignoredMarkupCollapsing.push(comment, buffer.length);
        }
      }
    }

    bufferPush(comment, false);
  }

  // Inline scripts minified as the parse reaches them run one at a time; dispatching them all up
  // front lets the minifier overlap the work. Bodies are matched by exact content, so one the parse
  // asks for in another form misses and takes the original path.
  /** @type {Map<string, Promise<string>> | null} */
  let jsPrewarmed = null;
  if (options.parallelJS && options.minifyJS !== identity && !options.processScripts?.length) {
    const bodies = extractScriptBodies(value);
    if (bodies.length > 1) {
      jsPrewarmed = new Map();
      // `collapseWhitespace` reaches script bodies through the whole-document pass above,
      // so the parse sees them trimmed; other whitespace modes leave them as they are
      const trims = Boolean(options.collapseWhitespace) &&
        !options.conservativeCollapse && !options.preserveLineBreaks;
      for (const { code, isModule } of bodies) {
        const body = replaceNUL(trims ? code.trim() : code);
        const key = (isModule ? 'm|' : '|') + body;
        if (jsPrewarmed.has(key)) {
          continue;
        }
        const pending = Promise.resolve(options.minifyJS(body, false, isModule));
        // The awaiting consumer reports the failure; a body the parse never asks for
        // would not have been minified at all, so its failure is not ours to raise
        pending.catch(() => {});
        jsPrewarmed.set(key, pending);
      }
    }
  }

  // Writes out a start tag for the `start` handler—right away, or once attribute values
  // that need asynchronous work (lazy module imports, minified URLs) resolve
  /**
   * @param {Array<{name: string, value: string | undefined, attr: HTMLAttribute} | undefined>} normalizedAttrs
   * @param {string} tag
   * @param {boolean} unary
   * @param {boolean} autoGenerated
   * @param {boolean | undefined} fragmentJoinsTag
   * @param {string | boolean | undefined} hasUnarySlash
   * @param {boolean | undefined} optional
   * @param {ProcessedOptions | null} restoreOptions - Context a unary tag pushed, or null
   * @param {ProcessedOptions} optionsTag - Context the element’s own attributes are in
   */
  function emitStartTag(normalizedAttrs, tag, unary, autoGenerated, fragmentJoinsTag, hasUnarySlash, optional, restoreOptions, optionsTag) {
    // Find the last kept attribute, then emit in order, with no intermediate array
    let lastKeptIndex = -1;
    for (let i = normalizedAttrs.length - 1; i >= 0; i--) {
      if (normalizedAttrs[i]) {
        lastKeptIndex = i;
        break;
      }
    }
    // Without `maxLineLength`, a start tag is one buffer entry rather than one per attribute,
    // which keeps the buffer and the final join small (lines only break between entries);
    // the loop is written out per case, as a check per attribute measurably slows it down
    let tagText = '';
    if (lastKeptIndex !== -1) {
      if (!bufferTags) {
        tagText = fragmentJoinsTag ? '' : ' ';
        for (let i = 0; i <= lastKeptIndex; i++) {
          const normalizedAttr = normalizedAttrs[i];
          if (normalizedAttr) {
            if (uidAttr && normalizedAttr.name.includes(uidAttr)) {
              markTagFragments(normalizedAttr.name);
            }
            tagText += buildAttr(normalizedAttr, tag, hasUnarySlash, optionsTag, i === lastKeptIndex, uidAttr, markers);
          }
        }
      } else {
        if (!fragmentJoinsTag) {
          bufferPush(' ', true);
        }
        for (let i = 0; i <= lastKeptIndex; i++) {
          const normalizedAttr = normalizedAttrs[i];
          if (normalizedAttr) {
            if (uidAttr && normalizedAttr.name.includes(uidAttr)) {
              markTagFragments(normalizedAttr.name);
            }
            const builtAttr = buildAttr(normalizedAttr, tag, hasUnarySlash, optionsTag, i === lastKeptIndex, uidAttr, markers);
            // One entry with the tag name, so that no line break can come between them
            if (i === 0 && fragmentJoinsTag) {
              bufferSet(buffer.length - 1, (buffer[buffer.length - 1] ?? '') + builtAttr);
            } else {
              bufferPush(builtAttr, true);
            }
          }
        }
      }
    } else if (optional && optionalStartTags.has(tag)) {
      // Start tag must never be omitted if it has any attributes
      optionalStartTag = tag;
    }

    bufferSet(buffer.length - 1, (buffer[buffer.length - 1] ?? '') + tagText + (hasUnarySlash ? '/' : '') + '>');

    impliedStartTag = autoGenerated && !options.includeAutoGeneratedTags ? tag : '';
    if (impliedStartTag) {
      removeStartTag();
      optionalStartTag = '';
      currentTag = '';
    }

    // `end` restores what a start tag pushed, and a unary tag never reaches it
    if (restoreOptions) {
      options = restoreOptions;
    }

    // What a unary tag holds ends with the tag, so no text can belong to it
    if (unary) {
      currentTag = '';
    }
  }

  // SVG subtree capture: When SVGO is active, record buffer positions for post-processing
  /** @type {Array<{start: number, end: number}>} */
  const svgBlocks = [];
  let svgBufferStartIndex = -1;
  let svgDepth = 0;
  // HTML raw text in an SVG block, which XML cannot read as HTML does,
  // passes SVGO as a placeholder—a comment, as SVGO minifies `style` elements
  // wherever they are
  /** @type {string[]} */
  const svgRawTexts = [];
  let uidSVGRawText = '';

  // One-time probe: If the input contains no SVG/MathML elements and the call
  // isn’t already inside foreign content (recursive calls inherit options),
  // the per-tag foreign-content handling in `start`/`end` can be skipped
  const hasForeignContext = Boolean(options.insideForeignContent) || RE_FOREIGN_ELEMENT_PROBE.test(value);

  const parser = new HTMLParser(value, {
    partialMarkup: partialMarkup ?? options.partialMarkup,
    // The short doctype makes the output no-quirks, so the input is read as such
    noQuirksMode: options.useShortDoctype,
    continueOnParseError: options.continueOnParseError,
    // Recovered errors would have aborted without the option, so they are worth knowing about
    parseError: options.continueOnParseError && options.log !== identity
      ? (/** @type {string} */ message) => options.log('Warning: ' + message)
      : undefined,
    customAttrAssign: options.customAttrAssign,
    customAttrSurround: options.customAttrSurround,
    // Compute `nextTag` only when whitespace collapse features require it
    wantsNextTag: !!(options.collapseWhitespace || options.collapseInlineTagWhitespace || options.conservativeCollapse),
    // `keepClosingSlash` calls the slash meaningful, which decides how the input is read as well
    selfClosingSlash: options.keepClosingSlash,

    start: function (/** @type {string} */ tag, /** @type {HTMLAttribute[]} */ attrs, /** @type {boolean} */ unary, /** @type {string} */ unarySlash, /** @type {boolean} */ autoGenerated) {
      // `lowerTag` stays '' when no foreign content is around—the SVG/MathML
      // checks below can then never match, and no per-tag lowercasing is needed
      let lowerTag = '';
      let useNameParentForTag = false;
      // The context the start tag sits in, which the branches below leave behind
      const optionsParent = options;
      let pushedContext = false;
      if (hasForeignContext) {
        lowerTag = tag.toLowerCase();
        if (lowerTag === 'svg' || lowerTag === 'math') {
          // Preserve the surrounding HTML context’s slash handling so
          // `foreignObject`/`annotation-xml` can restore it
          const keepClosingSlashHTML = Boolean(options.keepClosingSlash);
          options = { ...options, optionsParent: options };
          // SVGO reads an SVG block whole, nested MathML included, so what the
          // block is stays true inside it
          options.insideSVG = lowerTag === 'svg' || Boolean(options.insideSVG);
          // XML reads names as written, so wherever SVGO reads, they are written the way HTML reads
          // them (`viewbox` as `viewBox`), and attributes differing only in case are one attribute
          const readBySVGO = options.insideSVG && Boolean(options.minifySVG);
          options.caseSensitive = !readBySVGO;
          options.keepClosingSlash = true;
          options.name = readBySVGO ? nameForeign : identity;
          options.keepClosingSlashHTML = keepClosingSlashHTML;
          options.insideForeignContent = true;
          options.namespaceForeign = lowerTag;
          // Disable HTML-specific options that produce invalid XML:
          // SVG with `minifySVG` enabled is passed to SVGO, which requires valid XML input;
          // MathML is never processed by SVGO, so these restrictions never apply to it
          if (lowerTag === 'svg' && options.minifySVG) {
            options.removeAttributeQuotes = false;
            // Text and attribute values are decoded as HTML reads them, for transforms to read
            // them so, too, and then escaped for XML (see `escapeXML`) rather than for HTML
            options.decodeEntities = true;
            // Omitting a start and end tag both would keep the block well-formed, which this misses
            options.removeOptionalTags = false;
            options.collapseBooleanAttributes = false;
            options.collapseEmptyAttributes = false;
            options.includeAutoGeneratedTags = true;
          }
          options.removeTagWhitespace = false;
          pushedContext = true;
        }
        // `foreignObject` in SVG and `annotation-xml` in MathML contain HTML content
        // (the element itself is in SVG/MathML namespace, only its children are HTML);
        // a repeated attribute is dropped after the first, so the first `encoding` decides
        if (options.insideForeignContent && (lowerTag === 'foreignobject' ||
            (lowerTag === 'annotation-xml' && RE_HTML_ENCODING.test(
              attrs.find((/** @type {HTMLAttribute} */ a) => a.name.toLowerCase() === 'encoding')?.value ?? '')))) {
          const nameParent = options.name;
          options = { ...options, optionsParent: options };
          options.caseSensitive = false;
          // What the parser read the slash as, so that the two agree here as they do in HTML
          options.keepClosingSlash = options.keepClosingSlashHTML ?? false;
          options.nameParent = nameParent; // Preserve for the element tag itself
          options.name = lowercase;
          options.insideForeignContent = false;
          // Note: `removeAttributeQuotes` and `removeTagWhitespace` stay disabled,
          // and values stay escaped for XML (inherited from SVG context), because
          // the entire SVG block must be valid XML for SVGO processing
          useNameParentForTag = true;
          pushedContext = true;
        }
        if (options.selfCloseForeignElements && options.insideForeignContent && !unary) {
          enterForeignScope(lowerTag);
        }
      }
      // `nameParent` is set whenever `useNameParentForTag` is; the extra check narrows the type
      const nameTag = useNameParentForTag && options.nameParent ? options.nameParent : options.name;
      const tagSource = tag;
      tag = nameTag(tag);
      // HTML names are written as in the source under `caseSensitive`, foreign ones as named anyway
      const tagOut = namesAsWritten && nameTag === lowercase ? tagSource : tag;
      if (tag === 'meta' && options.removeRedundantAttributes) {
        deduplicateAttributes(attrs, Boolean(options.caseSensitive));
        // As if not written
        if (isMetaWithoutEffect(attrs, markers)) {
          removed = true;
          return undefined;
        }
        replaceContentTypePragma(attrs, markers);
      }
      const charsBefore = currentChars;
      const charsPrevTagBefore = charsPrevTag;
      currentTag = tag;
      charsPrevTag = tag;
      // An empty element that doesn’t render leaves the text before it in play
      if (!inlineTextSet.has(lowerTag || tag) && !inlineElementsTransparentVoid.has(lowerTag || tag)) {
        // Replaced content counts as content for the whitespace after what holds it
        currentChars = inlineElementsToKeepWhitespace.has(lowerTag || tag) ? '|' : '';
      }
      hasChars = false;
      currentAttrs = attrs;

      // What this start tag closed, which is left to the end tags if its element is removed
      const impliedEndsClosed = impliedEndsIndex === buffer.length ? impliedEnds : undefined;

      // What this start tag made optional, for if the element then turns out to be removed
      /** @type {{tag: string, text: string} | undefined} */
      let omittedStartTag;
      /** @type {{tag: string, text: string} | undefined} */
      let omittedEndTag;
      let optional = options.removeOptionalTags;
      if (optional) {
        const htmlTag = htmlElements.has(tag);
        // `<html>` may be omitted if first thing inside is not a comment
        // `<head>` may be omitted if first thing inside is an element
        // `<body>` may be omitted if first thing inside is not space, comment, `<meta>`, `<link>`,
        //   `<script>`, `<style>`, or `<template>`
        // `<colgroup>` may be omitted if first thing inside is `<col>`
        // `<tbody>` may be omitted if first thing inside is `<tr>`
        if (htmlTag && canRemoveParentTag(optionalStartTag, tag)) {
          const text = removeStartTag(options.removeEmptyElements);
          if (text !== undefined) {
            omittedStartTag = { tag: optionalStartTag, text };
          }
        }
        optionalStartTag = '';
        // End-tag-followed-by-start-tag omission rules
        // Outside no-quirks mode, `table` doesn’t (or may not) close a `p`
        if (htmlTag && canRemovePrecedingTag(optionalEndTag, tag) && (tableClosesP || optionalEndTag !== 'p' || tag !== 'table')) {
          // Only a start tag that gets written can stand in for the end tag
          if (optionalEndTagEmitted && (!autoGenerated || options.includeAutoGeneratedTags)) {
            const text = removeEndTag();
            if (options.removeEmptyElements) {
              omittedEndTag = { tag: optionalEndTag, text };
            }
          }
          // `<colgroup>` cannot be omitted if preceding `</colgroup>` is omitted
          // `<tbody>` cannot be omitted if preceding `</tbody>`, `</thead>`, or `</tfoot>` is
          //   omitted
          optional = !isStartTagMandatory(optionalEndTag, tag);
        }
        optionalEndTag = '';
        optionalEndTagEmitted = false;
      }

      // What the text before loses to an element that may go, with what it was, to come back
      // should it go: What trims here, and what the text trims seeing this tag next
      /** @type {Array<[number, string]>} */
      const trims = [];
      let charsBeforeRestored = charsBefore;
      const recordsTrims = Boolean(options.removeEmptyElements) && !unary;
      if (recordsTrims && textTrimmedEndIndex !== -1 && textTrimmedEndIndex === buffer.length - 1 && !endsWithWhitespace(buffer[textTrimmedEndIndex] ?? '')) {
        trims.push([textTrimmedEndIndex, buffer[textTrimmedEndIndex] + ' ']);
        charsBeforeRestored += ' ';
      }
      // Set whitespace flags for nested tags (e.g., `<code>` within a `<pre>`); verbatim
      // whitespace is tracked without `collapseWhitespace` too, as `removeWhitespaceLine` spares it
      if (options.collapseWhitespace && !stackNoTrimWhitespace.length) {
        if (recordsTrims) trimsRecorded = trims;
        squashTrailingWhitespace(lowerTag || tag);
        trimsRecorded = null;
      }
      // Whitespace that comes back is pending, as before what doesn’t render
      let gapIndexBefore = -1;
      if (!transparentDepth) {
        for (const [index, text] of trims) {
          if (index > gapIndexBefore && endsWithWhitespace(text) && !endsWithWhitespace(buffer[index] ?? '')) gapIndexBefore = index;
        }
      }
      if (options.collapseWhitespace && !unary && inlineElementsTransparent.has(lowerTag || tag)) {
        transparentDepth++;
      }
      if (!unary) {
        const tagLower = lowerTag || tag;
        if (!canTrimWhitespace(tagLower, tagOut, attrs) || stackNoTrimWhitespace.length) {
          stackNoTrimWhitespace.push(tag);
          if (tagLower === 'pre') preDepth++;
        }
        if (options.collapseWhitespace && (!canCollapseWhitespace(tagLower, tagOut, attrs) || stackNoCollapseWhitespace.length)) {
          stackNoCollapseWhitespace.push(tag);
        }
      }

      // Track SVG subtree for SVGO block processing
      if (lowerTag === 'svg' && options.minifySVG && !unary) {
        if (svgDepth === 0) {
          svgBufferStartIndex = buffer.length; // Record position before <svg> is pushed
        }
        svgDepth++;
      }

      const openTag = '<' + tagOut;
      // SVGO reads the whole SVG block as XML, where an element closes itself or not, so
      // every unary element in there is written with a slash
      const needsXMLSlash = unary && Boolean(options.insideSVG) && Boolean(options.minifySVG);
      // `foreignObject` and `annotation-xml` hold HTML, but are foreign elements themselves
      const optionsTag = useNameParentForTag ? optionsParent : options;
      const hasUnarySlash = needsXMLSlash || (unarySlash && optionsTag.keepClosingSlash);

      if (mergeRecords) {
        // An element inside one to merge (in SVG, a `script` may hold some) keeps it apart
        if (mergeRecord) mergeRecord.key = null;
        // Only names as long as `style`, `script`, `template`, and `noscript` need a look
        if (!unary && (tag.length === 5 || tag.length === 6 || tag.length === 8)) {
          const tagLower = tag.toLowerCase();
          if (tagLower === 'template' || tagLower === 'noscript') {
            inertDepth++;
          } else if (tagLower === 'style' && options.mergeStyles && !inertDepth && !options.namespaceForeign) {
            mergeRecord = createMergeRecord('style', attrs);
          } else if (tagLower === 'script' && options.mergeScripts && options.namespaceForeign !== 'math') {
            // In MathML, a `script` neither runs nor is hidden, so its text is shown as it is
            mergeRecord = createMergeRecord('script', attrs);
          }
        }
      }

      bufferPush(openTag, true);

      if (options.removeEmptyElements) {
        if (unary) {
          markOpenElementFilled();
        } else {
          // A start tag not written can’t be removed with its element
          openElements.push({ tag, index: buffer.length - 1, empty: !autoGenerated || Boolean(options.includeAutoGeneratedTags), omittedStartTag, omittedEndTag, impliedEnds: impliedEndsClosed, before: { chars: charsBeforeRestored, prevTag: charsPrevTagBefore, gapIndex: gapIndexBefore, trims } });
        }
      }

      const fragmentJoinsTag = uidAttrExactPattern ? joinFragmentAttrs(attrs) : false;
      // Whatever runs into the tag name has to stay first
      const tagFragment = fragmentJoinsTag ? attrs.shift() : undefined;

      // Duplicate attributes are invalid; per spec, the first one wins
      // https://html.spec.whatwg.org/multipage/parsing.html#attribute-name-state
      deduplicateAttributes(attrs, Boolean(options.caseSensitive));

      if (options.sortAttributes) {
        // By the time tags are processed, `createSortFns` has replaced any
        // truthy non-function value
        /** @type {(tag: string, attrs: HTMLAttribute[]) => void} */ (options.sortAttributes)(tagOut, attrs);
      }
      if (tagFragment) {
        attrs.unshift(tagFragment);
      }

      const attrResults = new Array(attrs.length);
      let anyThenable = false;
      for (let i = 0; i < attrs.length; i++) {
        const attr = /** @type {HTMLAttribute} */ (attrs[i]);
        const result = opaqueAttrs.has(attr)
          ? { name: attr.name, value: undefined, attr }
          : normalizeAttr(attr, attrs, tag, tagOut, options, minifyHTML, markers);
        if (!anyThenable && isThenable(result)) {
          anyThenable = true;
        }
        attrResults[i] = result;
      }
      // Only an attribute value that needs asynchronous work defers the write-out;
      // with none of those, the tag is written before the handler returns
      const restoreOptions = unary && pushedContext ? optionsParent : null;
      if (anyThenable) {
        return Promise.all(attrResults).then(resolved => {
          emitStartTag(resolved, tag, unary, autoGenerated, fragmentJoinsTag, hasUnarySlash, optional, restoreOptions, optionsTag);
        });
      }
      emitStartTag(/** @type {Array<{name: string, value: string | undefined, attr: HTMLAttribute} | undefined>} */ (attrResults), tag, unary, autoGenerated, fragmentJoinsTag, hasUnarySlash, optional, restoreOptions, optionsTag);
      return undefined;
    },
    end: function (/** @type {string} */ tag, /** @type {HTMLAttribute[]} */ attrs, /** @type {boolean} */ autoGenerated, /** @type {string | undefined} */ rest, /** @type {boolean | undefined} */ holdsTable, /** @type {boolean | undefined} */ closesBelowTop) {
      // As in `start`: `lowerTag` stays '' when no foreign content is around
      let lowerTag = '';
      // Named as the start tag was: an `svg` or `math` element in its own context,
      // a `foreignObject` or `annotation-xml` element in its parent’s
      let name = options.name;
      // Whether the element is SVG or MathML, which a slash can close, and SVGO, if it
      // reads the element, writes so anyway
      let selfCloses = Boolean(options.selfCloseForeignElements) && hasForeignContext &&
        !(options.insideSVG && options.minifySVG);
      if (hasForeignContext) {
        lowerTag = tag.toLowerCase();
        // An integration point is named in its parent’s context, which it opened one inside of
        selfCloses &&= Boolean(options.insideForeignContent) || ((lowerTag === 'foreignobject' || lowerTag === 'annotation-xml') &&
          Boolean(options.optionsParent?.insideForeignContent));
        if (foreignScopes.length && !leaveForeignScope(lowerTag)) {
          selfCloses = false;
        }
        // Restore parent context when exiting SVG/MathML or HTML-in-foreign-content elements
        // (a stray end tag, which partial markup keeps, has none to restore)
        if (lowerTag === 'svg' || lowerTag === 'math') {
          options = options.optionsParent ?? options;
        } else if ((lowerTag === 'foreignobject' || lowerTag === 'annotation-xml') &&
                   !options.insideForeignContent && options.optionsParent?.insideForeignContent) {
          options = options.optionsParent;
          name = options.name;
        }
      }
      const tagSource = tag;
      tag = name(tag);
      // As in `start`
      const tagOut = namesAsWritten && name === lowercase ? tagSource : tag;
      let endTagText = '</' + tagOut + '>';
      const endsMergeRecord = mergeRecord !== null && (tag.length === 5 || tag.length === 6) && tag.toLowerCase() === mergeRecord.kind;
      if (endsMergeRecord && mergeRecord && mergeRecord.content === -1) {
        // A place for the content of what is merged into this one
        mergeRecord.content = buffer.length;
        bufferPush('', false);
      } else if (inertDepth && tag.length === 8) {
        const tagLower = tag.toLowerCase();
        if (tagLower === 'template' || tagLower === 'noscript') inertDepth--;
      }
      // Custom fragments after the name are kept, next to it if they were in the source
      if (rest && uidAttr && rest.includes(uidAttr)) {
        markTagFragments(rest);
        const uidMatch = /** @type {RegExp} */ (uidAttrLeadingPattern).exec(rest);
        const joinsTag = uidMatch !== null && rest.startsWith('\t' + uidAttr) &&
          ignoredCustomMarkupChunks[+(uidMatch[1] ?? 0)]?.[1] === '';
        // Anything else an end tag holds is dropped, as without fragments
        const marker = uidAttr;
        const fragments = Array.from(rest.matchAll(/** @type {RegExp} */ (uidPattern)), uid => marker + (uid[2] ?? '') + marker);
        endTagText = '</' + tagOut + (joinsTag ? '' : ' ') + fragments.join(' ') + '>';
        // Which a slash would drop
        selfCloses = false;
      }

      const insideNoTrim = stackNoTrimWhitespace.length > 0;
      let closesNoTrim = false;
      if (stackNoTrimWhitespace.length) {
        if (removeClosedTag(stackNoTrimWhitespace, tag, lowerTag, closesBelowTop)) {
          const tagLower = lowerTag || tag;
          if (tagLower === 'pre') preDepth--;
          closesNoTrim = !stackNoTrimWhitespace.length;
        }
      } else if (options.collapseWhitespace) {
        squashTrailingWhitespace('/' + (lowerTag || tag));
      }
      if (transparentDepth && inlineElementsTransparent.has(lowerTag || tag)) {
        transparentDepth--;
      }
      let closesNoCollapse = false;
      if (stackNoCollapseWhitespace.length && removeClosedTag(stackNoCollapseWhitespace, tag, lowerTag, closesBelowTop)) {
        closesNoCollapse = !stackNoCollapseWhitespace.length;
      }

      const endsImpliedStart = !autoGenerated && impliedStartTag === tag;
      // A `p` that holds a `table` while the mode is unknown needs its end tag as written: One
      // after the `table` that the source lacks would be stray in no-quirks mode, and one that it
      // has, closing the `p` in quirks mode, would be missed there
      const skipsEndTag = autoGenerated && (!options.includeAutoGeneratedTags || Boolean(holdsTable));
      impliedStartTag = '';

      let startTagRemoved = false;
      let isElementEmpty = false;
      if (tag === currentTag) {
        currentTag = '';
        isElementEmpty = !hasChars;
      }
      selfCloses &&= isElementEmpty;
      // A stray end tag has no open element of its own
      let openElement;
      if (options.removeEmptyElements && openElements.length) {
        if (openElements[openElements.length - 1]?.tag === tag) {
          openElement = openElements.pop();
        } else if (closesBelowTop || canCloseBelowTop(tag, lowerTag)) {
          const index = openElements.findLastIndex(entry => entry.tag === tag);
          if (index !== -1) {
            openElement = openElements.splice(index, 1)[0];
            // Closed around what is open inside it, so that holds this end tag now
            currentTag = '';
          }
        }
      }
      // Emptied by removing what it held, as long as its start tag is still the last one written
      const isElementEmptied = !isElementEmpty && Boolean(openElement?.empty) && lastStartTagIndex() === openElement?.index;

      if (options.removeOptionalTags) {
        // `<html>`, `<head>`, or `<body>` may be omitted if the element is empty
        if (isElementEmpty && topLevelElements.has(optionalStartTag)) {
          removeStartTag();
          startTagRemoved = true;
        }
        optionalStartTag = '';
        // `</html>` or `</body>` may be omitted if not followed by comment;
        // `</head>` may be omitted if not followed by space or comment;
        // `</p>`, `</li>`, `</dd>`, and `</dt>` may be omitted if no more content
        // in parent, as long as the parent’s end tag closes these special elements
        // in browsers (`pClosingParents`); except for `</dt>` or `</thead>`,
        // end tags may be omitted if no more content in parent
        // https://html.spec.whatwg.org/multipage/syntax.html#optional-tags
        if (tag && optionalEndTag && optionalEndTagEmitted && !trailingElements.has(optionalEndTag) && (!specialOptionalEndTags.has(optionalEndTag) || pClosingParents.has(tag))) {
          removeEndTag(insideNoTrim);
        }
        optionalEndTag = optionalEndTags.has(tag) && !endsImpliedStart && !holdsTable ? tag : '';
        optionalEndTagEmitted = true;
      }

      let isElementRemoved = false;
      // A formatting element closed by a block is reopened after it by browsers, so it has to stay
      if (options.removeEmptyElements && (isElementEmpty || isElementEmptied) && !options.insideForeignContent && canRemoveElement(tag, attrs, options, tagOut) &&
          !(autoGenerated && formattingElements.has(tag)) && !openElement?.impliedEnds?.some(implied => !implied.text)) {
        let preserve = false;
        if (removeEmptyElementsExcept.length) {
          const normalizedAttrs = attrs.map((/** @type {HTMLAttribute} */ attr) => ({ ...attr, name: resolveName(attr.name) }));
          preserve = shouldPreserveEmptyElement(tag, normalizedAttrs, removeEmptyElementsExcept);
        }

        if (!preserve) {
          // Once it is gone, the last start tag is another element’s
          if (!startTagRemoved) {
            removeStartTag();
          }
          isElementRemoved = true;
          optionalStartTag = '';
          optionalEndTag = '';
          optionalEndTagEmitted = false;
          // The text before it is what comes before what follows now
          if (openElement) {
            currentChars = openElement.before.chars;
            charsPrevTag = openElement.before.prevTag;
            const trims = openElement.before.trims;
            for (let i = trims.length - 1; i >= 0; i--) {
              const [index, text] = /** @type {[number, string]} */ (trims[i]);
              bufferSet(index, text);
            }
            if (openElement.before.gapIndex !== -1) transparentGapIndex = openElement.before.gapIndex;
          }
          // Text after it reads past it if it’s a block or follows an element that doesn’t render
          followsRemovedBlock = !inlineElements.has(lowerTag || tag) && !inlineElementsTransparent.has(lowerTag || tag) || inlineElementsTransparentAfter.has(charsPrevTag);
          // What it made optional is up to what comes next now
          if (openElement?.omittedEndTag) {
            bufferPush(openElement.omittedEndTag.text, true);
            optionalEndTag = openElement.omittedEndTag.tag;
            optionalEndTagEmitted = true;
          }
          if (openElement?.omittedStartTag) {
            bufferPush(openElement.omittedStartTag.text, true);
            optionalStartTag = openElement.omittedStartTag.tag;
          }
          if (openElement?.impliedEnds) {
            // Browsers reopen formatting elements after what closed them, which their end tags
            // would prevent and whitespace following an omitted end tag would change
            const written = openElement.impliedEnds.filter(implied => !formattingElements.has(implied.tag));
            for (const implied of written) {
              bufferPush(implied.text, true);
            }
            const last = written[written.length - 1];
            if (last && written.length === openElement.impliedEnds.length && optionalEndTags.has(last.tag)) {
              optionalEndTag = last.tag;
              optionalEndTagEmitted = true;
            }
          }
        } else {
          // Preserve the element—add closing tag
          if (skipsEndTag) {
            optionalEndTagEmitted = false;
          } else {
            bufferPush(endTagText, true);
          }
          charsPrevTag = '/' + tag;
          if (!inlineElements.has(lowerTag || tag)) {
            currentChars = '';
          } else if (isElementEmpty || !inlineTextSet.has(lowerTag || tag)) {
            currentChars += '|';
          }
        }
      } else {
        if (skipsEndTag) {
          optionalEndTagEmitted = false;
        } else if (!selfCloses || !closeLastStartTag()) {
          bufferPush(endTagText, true);
        }
        charsPrevTag = '/' + tag;
        if (!inlineElements.has(lowerTag || tag)) {
          currentChars = '';
        } else if (isElementEmpty || !inlineTextSet.has(lowerTag || tag)) {
          // What is not text counts as content for the whitespace after it
          currentChars += '|';
        }
      }

      if (skipsEndTag && !isElementRemoved && options.removeEmptyElements) {
        if (impliedEndsIndex !== buffer.length) {
          impliedEnds = [];
          impliedEndsIndex = buffer.length;
        }
        impliedEnds.push({ tag, text: holdsTable ? '' : endTagText });
      }

      if (closesNoTrim && !isElementRemoved) {
        noTrimEndIndex = buffer.length - 1;
      }
      if (closesNoCollapse && !isElementRemoved) {
        noCollapseEndIndex = buffer.length - 1;
      }

      // A kept element fills the one it sits in
      if (openElement && !isElementRemoved) {
        markOpenElementFilled();
      }

      if (endsMergeRecord) {
        if (mergeRecords && mergeRecord && buffer.length - 1 > mergeRecord.content && RE_END_TAG.test(buffer[buffer.length - 1] ?? '')) {
          mergeRecord.end = buffer.length - 1;
          if (mergeRecord.kind === 'style') {
            const css = recordContentOf(mergeRecord);
            mergeRecord.tail = css.trim() || !mergeRecord.deferred ? css : mergeRecords[mergeRecords.length - 1]?.tail ?? '';
          }
          mergeRecords.push(mergeRecord);
        } else if (mergeRecords && mergeRecord && mergeRecord.deferred && mergeRecord.content < buffer.length) {
          // Without an end tag, it stays apart, but its CSS is still to be minified
          mergeRecord.key = null;
          mergeRecord.end = buffer.length;
          mergeRecords.push(mergeRecord);
        }
        mergeRecord = null;
      }

      // SVG subtree capture: Record end position for post-processing with SVGO
      if (lowerTag === 'svg' && options.minifySVG && svgDepth > 0) {
        svgDepth--;
        if (svgDepth === 0 && svgBufferStartIndex >= 0) {
          svgBlocks.push({ start: svgBufferStartIndex, end: buffer.length });
          svgBufferStartIndex = -1;
        }
      }
    },
    chars: function (/** @type {string} */ text, /** @type {string} */ prevTag, /** @type {string} */ nextTag, /** @type {HTMLAttribute[]} */ prevAttrs, /** @type {HTMLAttribute[]} */ nextAttrs, /** @type {boolean} */ source, /** @type {boolean} */ first, /** @type {boolean} */ last) {
      if (mergeRecord) {
        if (mergeRecord.content === -1) mergeRecord.content = buffer.length;
        // Custom fragments may hold anything, which minified CSS can’t take as written
        if (mergeRecord.kind === 'style' && markers.length && markers.some(marker => text.includes(marker))) mergeRecord.key = null;
      }
      // Publish this node’s context for the `charsCollapse`/`charsFinalize` phases
      // An edge of partial markup reads as no tag, so that whitespace there may stay
      textPrevTag = followsRemovedBlock ? charsPrevTag || 'comment' : first && keepsEdges ? '' : prevTag === '' ? 'comment' : prevTag;
      textNextTag = last && keepsEdges ? '' : nextTag === '' ? 'comment' : nextTag;
      textPrevAttrs = prevAttrs || [];
      textNextAttrs = nextAttrs || [];

      // Whether this text node needs any async work. In foreign content, text reads references
      // and CDATA sections, and `script` and `style` hold markup, so their text is a source only
      // where it is all they hold (and in SVG, where they run and apply)
      const foreign = Boolean(options.insideForeignContent);
      textForeign = foreign;
      const holdsSource = !foreign || Boolean(source);
      const needsProcessScript = holdsSource && specialContentElements.has(currentTag) && (options.processScripts || (options.minifyJSON && currentTag === 'script' && hasJsonScriptType(currentAttrs)));
      const needsMinifyJS = holdsSource && options.minifyJS !== identity && isExecutableScript(currentTag, currentAttrs);
      const isModuleScript = needsMinifyJS && currentAttrs.some(
        a => a.name.toLowerCase() === 'type' && (a.value ?? '').trim().toLowerCase() === 'module'
      );
      const needsMinifyCSS = holdsSource && options.minifyCSS !== identity && isStyleElement(currentTag, currentAttrs);
      textForeignDecoded = foreign && Boolean(options.decodeEntities || needsProcessScript || needsMinifyJS || needsMinifyCSS);
      const needsDecode = Boolean(text) && (foreign
        ? textForeignDecoded && (text.indexOf('&') !== -1 || text.indexOf('<![CDATA[') !== -1)
        : options.decodeEntities && text.indexOf('&') !== -1 && !holdsRawText());

      // Fast path: All work is sync—skip async machinery (decoding counts
      // once `entities` has loaded)
      if ((!needsDecode || decodeHTMLFn) && !needsProcessScript && !needsMinifyJS && !needsMinifyCSS) {
        if (needsDecode && decodeHTMLFn) {
          text = foreign ? decodeForeignText(text, decodeHTMLFn) : decodeHTMLFn(text);
        }
        charsFinalize(charsCollapse(text));
        return;
      }

      // Slow path: At least one async step required
      return (async () => {
        if (needsDecode) {
          const decode = /** @type {(text: string) => string} */ (await getDecodeHTML());
          text = foreign ? decodeForeignText(text, decode) : decode(text);
        }
        text = charsCollapse(text);
        if (needsProcessScript) {
          text = await processScript(text, options, currentTag, currentAttrs, minifyHTML);
        }
        if (needsMinifyJS || needsMinifyCSS) {
          text = replaceNUL(text);
        }
        if (needsMinifyJS) {
          const prewarmed = jsPrewarmed?.get((isModuleScript ? 'm|' : '|') + text);
          text = prewarmed ? await prewarmed : await options.minifyJS(text, false, isModuleScript);
        }
        if (needsMinifyCSS) {
          // Style sheets that may be merged are minified once that is known, by run; except
          // under `removeEmptyElements`, which needs to know now whether CSS minifies to nothing,
          // where that only goes for CSS that joins a run
          if (mergeRecord?.kind === 'style' && mergeRecord.key !== null && (!options.removeEmptyElements || joinsStyleRun(mergeRecord, text))) {
            mergeRecord.deferred = true;
          } else {
            text = await options.minifyCSS(text, undefined, options.cssContext);
          }
        }
        charsFinalize(text);
      })();
    },
    // `markup` is the comment as written, where it is to stay that way (`<? x>`)
    comment: function (/** @type {string} */ text, /** @type {boolean} */ nonStandard, /** @type {string | undefined} */ markup) {
      const prefix = nonStandard ? '<!' : '<!--';
      const suffix = nonStandard ? '>' : '-->';

      if (options.removeComments && !isIgnoredComment(text, options)) {
        text = '';
        removed = true;
      } else {
        text = markup ?? prefix + text + suffix;
      }
      commentFinalize(text);
    },
    // Kept as written, as content may be inserted at one (`<template for>`)
    processingInstruction: function (/** @type {string} */ text) {
      hasChars = true;
      markOpenElementFilled();
      commentFinalize(text);
    },
    strayEnd: function () {
      removed = true;
    },
    doctype: function (/** @type {string} */ doctype, /** @type {boolean} */ noQuirksMode, /** @type {boolean} */ isIgnored) {
      tableClosesP = noQuirksMode;
      if (isIgnored) {
        removed = true;
        return;
      }
      bufferPush(options.useShortDoctype
        ? '<!doctype' +
        (options.removeTagWhitespace ? '' : ' ') + 'html>'
        : doctype.replace(RE_DOCTYPE_SEGMENT, (segment, quoted) => quoted ?? collapseWhitespaceAll(segment)), false);
    }
  });

  await parser.parse();

  // Nothing follows at the end, so pending whitespace goes (before SVG blocks shift the buffer)
  if (transparentGapIndex !== -1) {
    settleTransparentGap(Boolean(options.conservativeCollapse) || keepsEdges);
  }

  if (mergeRecords && (mergeRecords.length > 1 || mergeRecords[0]?.deferred)) {
    await mergeElements(mergeRecords);
  }

  // Before SVGO joins entries, which `trimIgnoreEdges` would have to search past
  if (ignoredMarkupCollapsing.length) {
    trimIgnoreEdges();
  }

  // Post-processing: Optimize SVG blocks with SVGO
  // Run all SVGO calls in parallel, then splice results in reverse to preserve indices
  const minifySVG = options.minifySVG;
  if (minifySVG && svgBlocks.length) {
    // Raw text in HTML content holds `htmlmin:ignore` placeholders as text, passing SVGO as a token
    const ignoreTokens = uidIgnore
      ? svgRawTexts.flatMap((text, index) => text.includes(uidIgnore) ? ['/*!' + uidSVGRawText + index + '*/'] : [])
      : [];
    const optimized = await Promise.all(svgBlocks.map(({ start, end }) => {
      const source = buffer.slice(start, end).join('');
      // `htmlmin:ignore` placeholders are comments, which SVGO drops with what they stand for.
      // (Keeping them, or lifting the markup out and dropping it back in, wouldn’t be safe,
      // either: SVGO may move or remove what surrounds them, and it can’t see what they hold,
      // like references to IDs and definitions it then removes or renames.)
      if (uidIgnore && (source.includes(uidIgnore) || ignoreTokens.some((token) => source.includes(token)))) {
        options.log('Skipping SVGO for an `svg` element holding `htmlmin:ignore` markup, which SVGO would drop');
        // Attribute values were escaped for SVGO, placeholders in them included
        const escaped = '&lt;!--' + uidIgnore;
        for (let i = start; i < end; i++) {
          const entry = buffer[i] ?? '';
          if (entry.includes(escaped)) bufferSet(i, entry.replaceAll(escaped, '<!--' + uidIgnore));
        }
        return null;
      }
      return minifySVG(source);
    }));
    for (let i = svgBlocks.length - 1; i >= 0; i--) {
      const block = svgBlocks[i];
      const result = optimized[i];
      if (block && result != null) {
        buffer.splice(block.start, block.end - block.start, result);
        bufferTags?.splice(block.start, block.end - block.start, false);
      }
    }
  }
  // Also where an SVG left open never reached SVGO
  if (svgRawTexts.length) {
    const pattern = new RegExp('/\\*!' + uidSVGRawText + '([0-9]+)\\*/', 'g');
    for (let i = 0; i < buffer.length; i++) {
      const segment = /** @type {string} */ (buffer[i]);
      if (segment.indexOf(uidSVGRawText) !== -1) {
        bufferSet(i, segment.replace(pattern, (/** @type {string} */ match, /** @type {string} */ index) => svgRawTexts[+index] ?? match));
      }
    }
  }

  if (options.removeOptionalTags) {
    // `<html>` may be omitted if first thing inside is not a comment
    // `<head>` or `<body>` may be omitted if empty
    if (topLevelElements.has(optionalStartTag)) {
      removeStartTag();
    }
    // Except for `</dt>` or `</thead>`, end tags may be omitted if no more content in parent;
    // whitespace kept at the end of partial markup follows the tag, which then stays
    if (optionalEndTag && optionalEndTagEmitted && !trailingElements.has(optionalEndTag) &&
        !(keepsEdges && !RE_END_TAG.test(buffer[skipRun(runEmpty, buffer.length - 1)] ?? ''))) {
      removeEndTag();
    }
  }
  // Collapsing also trims whitespace before trailing inline end tags; trimming the output
  // catches what it leaves at either end, like a space after a removed leading comment
  if (options.collapseWhitespace && !keepsEdges) {
    squashTrailingWhitespace('br');
  }
  trimOutputWhitespace();

  return joinResultSegments(buffer, bufferTags ?? [], markers, options, uidPattern
    ? function (/** @type {string} */ str) {
      return str.replace(/** @type {RegExp} */ (uidPattern), function (/** @type {string} */ match, /** @type {string} */ prefix, /** @type {string} */ index, /** @type {string} */ suffix, /** @type {number} */ offset, /** @type {string} */ string) {
        const chunks = ignoredCustomMarkupChunks[+index];
        if (!chunks) {
          return match;
        }
        if (!options.collapseWhitespace) {
          // The fragment took the whitespace around it along, so other whitespace here was
          // written by the minifier (e.g., by sorting) and is needed where the fragment has none
          return (chunks[1] ? '' : prefix.replace(RE_TABS, '')) + chunks[0] + (chunks[2] ? '' : suffix.replace(RE_TABS, ''));
        }
        const leading = chunks[1] ?? '';
        const trailing = chunks[2] ?? '';
        const fragment = chunks[0].slice(leading.length, chunks[0].length - trailing.length);
        const inTag = customMarkupInTags.has(+index);
        const keep = inTag || customMarkupInCode.has(+index) || !options.trimCustomFragments;
        // The padding tabs are whitespace to the minifier, so where one is gone, the
        // minifier removed the whitespace, which makes it safe to trim
        const trimLeading = !keep && prefix === '';
        const trimTrailing = inTag ? string.charCodeAt(offset + match.length) === 62 /* > */ : !keep && suffix === '';
        return restoreFragmentSide(prefix.replace(RE_TABS, '') + leading, trimLeading, true) +
          fragment +
          restoreFragmentSide(suffix.replace(RE_TABS, '') + trailing, trimTrailing, false);
      });
    }
    : identity, uidIgnore
    ? function (/** @type {string} */ str) {
      return str.replace(new RegExp('<!--' + uidIgnore + '([0-9]+)-->', 'g'), function (/** @type {string} */ _match, /** @type {string} */ index) {
        return ignoredMarkupChunks[+index] ?? '';
      });
    }
    : identity);
}

/**
 * @param {string[]} results
 * @param {boolean[]} resultTags Whether each result belongs to a start or end tag
 * @param {string[]} markers Custom fragment and `htmlmin:ignore` placeholder markers
 * @param {ProcessedOptions} options
 * @param {Function} restoreCustom
 * @param {Function} restoreIgnore
 */
function joinResultSegments(results, resultTags, markers, options, restoreCustom, restoreIgnore) {
  let str;
  const maxLineLength = options.maxLineLength;

  if (maxLineLength) {
    // Whitespace in tags is the only whitespace that can’t change the output, so lines break
    // there alone—in place of the space between attributes, or right before a tag’s `>`
    const noNewlinesBeforeTagClose = options.noNewlinesBeforeTagClose;
    /** @type {string[]} */
    const lines = [];
    let lineLength = 0;
    // Output since the last break opportunity, and what that opportunity is when not taken;
    // fragments are restored by chunk, as whitespace around them decides how they come back
    let chunk = '';
    /** @type {string | null} */
    let separator = null;
    const flush = (/** @type {string | null} */ nextSeparator) => {
      chunk = restoreIgnore(restoreCustom(chunk));
      const newline = chunk.indexOf('\n');
      const head = newline < 0 ? chunk.length : newline;
      // A line already too long gains nothing from moving a `>` that ends a line of its own
      const isUseless = separator === '' && head === 1 && lineLength > maxLineLength;
      if (separator !== null) {
        if (lineLength > 0 && lineLength + separator.length + head > maxLineLength && !isUseless) {
          lines.push('\n');
          lineLength = 0;
        } else if (separator) {
          lines.push(separator);
          lineLength += separator.length;
        }
      }
      lines.push(chunk);
      lineLength = newline < 0 ? lineLength + chunk.length : chunk.length - chunk.lastIndexOf('\n') - 1;
      chunk = '';
      separator = nextSeparator;
    };
    for (let index = 0; index < results.length; index++) {
      const result = results[index] ?? '';
      if (!resultTags[index]) {
        chunk += result;
      } else if (markers.length && markers.some(marker => result.includes(marker))) {
        // Whitespace next to a fragment stays as is, as it may belong to it
        if (!chunk && separator !== null) {
          chunk = separator;
          separator = null;
        }
        chunk += result;
      } else if (result.charCodeAt(result.length - 1) === 32 /* space */) {
        chunk += result.slice(0, -1);
        flush(' ');
      } else if (result.charCodeAt(result.length - 1) === 62 /* > */ &&
          result.charCodeAt(result.length - 2) !== 47 /* / */ &&
          !(noNewlinesBeforeTagClose && result.startsWith('</'))) {
        chunk += result.slice(0, -1);
        flush('');
        chunk = '>';
      } else {
        chunk += result;
      }
    }
    flush(null);
    str = lines.join('');
  } else {
    str = restoreIgnore(restoreCustom(results.join('')));
  }
  if (!options.collapseWhitespace) {
    return str;
  }
  const trims = !keepsEdgeWhitespace(options);
  return collapseWhitespace(str, options, trims, trims);
}

/**
 * Initialize minification caches with configurable sizes.
 *
 * Important behavior notes:
 * - Caches are created on the first `minify()` call and persist for the lifetime of the process
 * - Cache sizes are locked after first initialization—subsequent calls use the same caches
 *   even if different `cacheCSS`/`cacheJS`/`cacheSVG` options are provided
 * - Invalid values (NaN, Infinity, negative) fall back to the default size (500)
 */
/**
 * @param {MinifierOptions} options
 * @returns {{cssMinifyCache: LRU, jsMinifyCache: LRU, svgMinifyCache: LRU}}
 */
function initCaches(options) {
  if (!cssMinifyCache || !jsMinifyCache || !svgMinifyCache) {
    const defaultSize = 500;

    // Precedence: Options > env > default; non-finite and negative sizes fall back to the default
    const cacheSize = (/** @type {number | undefined} */ option, /** @type {string | undefined} */ envVar) => {
      const size = option !== undefined ? option : (envVar !== undefined ? Number(envVar) : defaultSize);
      return Number.isFinite(size) && size >= 0 ? Math.floor(size) : defaultSize;
    };

    cssMinifyCache = new LRU(cacheSize(options.cacheCSS, process.env.HM_CACHE_CSS));
    jsMinifyCache = new LRU(cacheSize(options.cacheJS, process.env.HM_CACHE_JS));
    svgMinifyCache = new LRU(cacheSize(options.cacheSVG, process.env.HM_CACHE_SVG));
  }

  return { cssMinifyCache, jsMinifyCache, svgMinifyCache };
}

/**
 * Get hit/miss statistics for the CSS, JavaScript, and SVG minification caches.
 *
 * Each cache is created lazily on the first `minify()` call (see `initCaches`); before
 * that first call, or for a cache never exercised (e.g., `minifyCSS` disabled), its
 * entry reports all-zero stats.
 *
 * @returns {{ css: CacheStats, js: CacheStats, svg: CacheStats }}
 */
export function getCacheStats() {
  return {
    css: cacheStatsOrEmpty(cssMinifyCache),
    js: cacheStatsOrEmpty(jsMinifyCache),
    svg: cacheStatsOrEmpty(svgMinifyCache)
  };
}

// Memoized options processing: Batch runs pass one options object to many `minify()` calls, and
// processing it (regex parsing, closures, option signatures) is expensive. The cache is keyed on
// the object and checked against a deep snapshot, so mutating a reused object, nested values
// included, still triggers reprocessing. Each call works on a shallow copy, as `minifyHTML`
// reassigns top-level keys (wrapped minifiers, sorters, UID comment patterns) per call.
/** @type {MinifierOptions} */
const EMPTY_OPTIONS = {};
/** @type {WeakMap<object, {snapshot: unknown, processed: ProcessedOptions}>} */
const processedOptionsCache = new WeakMap();

// A “plain” value is an array or plain object, which the snapshot copies and
// compares structurally; anything else (functions, RegExps, class instances)
// is kept and compared by identity—a false mismatch merely reprocesses
/** @param {unknown} value */
function isPlainValue(value) {
  if (Array.isArray(value)) return true;
  if (value === null || typeof value !== 'object') return false;
  const proto = Object.getPrototypeOf(value);
  return proto === Object.prototype || proto === null;
}

// Budget for `snapshotValue`: Bounds the work spent on pathological options
// objects; when the budget runs out, the object isn’t memoized and falls
// back to per-call processing
const SNAPSHOT_MAX_NODES = 1024;
const SNAPSHOT_UNSAFE = Symbol('snapshot-unsafe');
// Skipped memoization is fail-safe but silent—warn once per process
let snapshotBudgetWarned = false;

/**
 * @param {unknown} value
 * @param {{budget: number}} state
 * @returns {unknown} The copy, or `SNAPSHOT_UNSAFE` once the budget is exhausted
 */
function snapshotValue(value, state) {
  if (!isPlainValue(value)) return value;
  if (--state.budget < 0) return SNAPSHOT_UNSAFE;
  if (Array.isArray(value)) {
    const copy = new Array(value.length);
    for (let i = 0; i < value.length; i++) {
      const child = snapshotValue(value[i], state);
      if (child === SNAPSHOT_UNSAFE) return SNAPSHOT_UNSAFE;
      copy[i] = child;
    }
    return copy;
  }
  /** @type {Record<string, unknown>} */
  const copy = {};
  const source = /** @type {Record<string, unknown>} */ (value);
  for (const key of Object.keys(source)) {
    const child = snapshotValue(source[key], state);
    if (child === SNAPSHOT_UNSAFE) return SNAPSHOT_UNSAFE;
    copy[key] = child;
  }
  return copy;
}

/**
 * @param {unknown} snapshot
 * @param {unknown} current
 * @returns {boolean}
 */
function valueUnchanged(snapshot, current) {
  if (snapshot === current) return true;
  if (!isPlainValue(snapshot) || !isPlainValue(current)) return false;
  if (Array.isArray(snapshot)) {
    if (!Array.isArray(current) || snapshot.length !== current.length) return false;
    for (let i = 0; i < snapshot.length; i++) {
      if (!valueUnchanged(snapshot[i], current[i])) return false;
    }
    return true;
  }
  if (Array.isArray(current)) return false;
  const snapshotObj = /** @type {Record<string, unknown>} */ (snapshot);
  const currentObj = /** @type {Record<string, unknown>} */ (current);
  const snapshotKeys = Object.keys(snapshotObj);
  if (snapshotKeys.length !== Object.keys(currentObj).length) return false;
  for (const key of snapshotKeys) {
    if (!Object.hasOwn(currentObj, key) || !valueUnchanged(snapshotObj[key], currentObj[key])) {
      return false;
    }
  }
  return true;
}

/**
 * @param {string} value
 * @param {MinifierOptions} [options]
 * @returns {Promise<string>}
 */
export const minify = async function (value, options) {
  const start = Date.now();

  const inputOptions = options || EMPTY_OPTIONS;

  const caches = initCaches(inputOptions);

  // WeakMap keys must be objects
  const canMemoize = typeof inputOptions === 'object' && inputOptions !== null;
  const cached = canMemoize ? processedOptionsCache.get(inputOptions) : undefined;
  /** @type {ProcessedOptions} */
  let processedBase;
  // `valueUnchanged` needs no budget of its own: Recursion is bounded by the
  // stored snapshot, which is a finite tree of at most `SNAPSHOT_MAX_NODES`
  //
  // Note: The comparison takes about 8% of a call on tiny documents; only options
  // known never to change (e.g., deeply frozen ones) could skip it
  if (cached && valueUnchanged(cached.snapshot, inputOptions)) {
    processedBase = cached.processed;
  } else {
    processedBase = processOptions(inputOptions, {
      getLightningCSS,
      getTerser,
      getSwc,
      getSvgo,
      cssMinifyCache: caches.cssMinifyCache,
      jsMinifyCache: caches.jsMinifyCache,
      svgMinifyCache: caches.svgMinifyCache
    });
    if (canMemoize) {
      const snapshot = snapshotValue(inputOptions, { budget: SNAPSHOT_MAX_NODES });
      if (snapshot !== SNAPSHOT_UNSAFE) {
        processedOptionsCache.set(inputOptions, { snapshot, processed: processedBase });
      } else if (!snapshotBudgetWarned) {
        snapshotBudgetWarned = true;
        const warn = typeof inputOptions.log === 'function' ? inputOptions.log : console.warn;
        warn('HTML Minifier: Options object exceeds the memoization complexity limit; options will be processed on every call');
      }
    }
  }
  // Warnings are deduplicated per document, so the state has to live on the per-call
  // copy; the `minifyCSS` closure hangs off the memoized base, shared across calls
  /** @type {NonNullable<ProcessedOptions['cssContext']>} */
  const cssContext = { warned: new Set() };
  // Whether the document holds a `base` element, looked up in it on the first `target=_self`
  /** @type {NonNullable<ProcessedOptions['baseState']>} */
  const baseState = { hasBase: undefined, source: value };
  // Work on a shallow copy so per-call reassignments don’t reach the cached base.
  // The per-document state is part of the copy from the start, as properties added
  // to it later slow down every option lookup; so are the keys of the SVG and MathML
  // contexts, so that their copies share its shape.
  const processedOptions = /** @type {ProcessedOptions} */ ({
    ...processedBase,
    cssContext,
    baseState,
    insideForeignContent: false,
    insideSVG: false,
    keepClosingSlashHTML: undefined,
    namespaceForeign: undefined,
    nameParent: undefined,
    optionsParent: undefined
  });

  // Unused-CSS removal needs the whole document’s symbols before the first `style` element is
  // minified, so they are collected upfront from the raw input—where there is a style sheet at all
  if (processedOptions.removeUnusedCSS && RE_STYLE_ELEMENT.test(value)) {
    Object.assign(cssContext, collectUsage(
      value,
      processedOptions.removeUnusedCSS.scripts,
      value.indexOf('&') !== -1
        ? /** @type {(text: string) => string} */ (await getDecodeHTML())
        : undefined
    ));
  }

  const result = await minifyHTML(value, processedOptions);

  processedOptions.log('minified in: ' + (Date.now() - start) + 'ms');
  return result;
};

export {
  presets,
  getPreset,
  getPresetNames
};

export default {
  minify,
  presets,
  getPreset,
  getPresetNames,
  getCacheStats
};