, around /); * - remove comments, except conditional comments and framework/server markers; * - normalise whitespace between attributes (attribute values are untouched); * - minify inline JSON, and inline CSS/JS through callbacks. * * Content of pre, code, textarea, listing, SVG text and elements with a * preserved class or inline white-space:pre style is passed through verbatim. * * @package All_In_One_Minifier */ defined( 'ABSPATH' ) || exit; /** * HTML tokenizer/minifier. Has no WordPress dependencies. */ class AIOM_HTML_Minifier { const WS = " \t\n\r\f"; /** * Start tag: name, attributes (quote-aware, per the HTML tokenizer states), self-closing slash. */ const START_TAG = '/\G<([a-zA-Z][^\t\n\f\r \/>]*+)((?:[\t\n\f\r ]++|\/(?!>)|(?:=|[^\t\n\f\r \/>=])[^\t\n\f\r \/>=]*+(?:[\t\n\f\r ]*+=[\t\n\f\r ]*+(?:"[^"]*+"|\'[^\']*+\'|[^\t\n\f\r >]++))?)*+)(\/?)>/'; /** * End tag. Attributes on end tags are ignored by browsers, but they still affect where the tag ends. */ const END_TAG = '/\G<\/([a-zA-Z][^\t\n\f\r \/>]*+)(?:[\t\n\f\r ]++|\/(?!>)|(?:=|[^\t\n\f\r \/>=])[^\t\n\f\r \/>=]*+(?:[\t\n\f\r ]*+=[\t\n\f\r ]*+(?:"[^"]*+"|\'[^\']*+\'|[^\t\n\f\r >]++))?)*+\/?>/'; /** * Elements whose content is raw text and is never tokenized as markup. * * @var array */ private static $raw_text = array( 'script' => 1, 'style' => 1, 'textarea' => 1, 'title' => 1, 'xmp' => 1, 'iframe' => 1, 'noembed' => 1, 'noframes' => 1, 'noscript' => 1, ); /** * Elements whose descendant text keeps its exact whitespace. * * @var array */ private static $preserve_tags = array( 'pre' => 1, 'code' => 1, 'listing' => 1, ); /** * Void elements never have content, so they never open a preserved region. * * @var array */ private static $void = array( 'area' => 1, 'base' => 1, 'br' => 1, 'col' => 1, 'embed' => 1, 'hr' => 1, 'img' => 1, 'input' => 1, 'link' => 1, 'meta' => 1, 'source' => 1, 'track' => 1, 'wbr' => 1, ); /** * Elements that may appear in . Any other start tag implies . * * @var array */ private static $head_tags = array( 'head' => 1, 'meta' => 1, 'link' => 1, 'title' => 1, 'style' => 1, 'script' => 1, 'base' => 1, 'noscript' => 1, 'template' => 1, ); /** * Whitespace directly before these tokens is never rendered. * * @var array */ private static $drop_before = array( 'html' => 1, 'head' => 1, '/head' => 1, 'body' => 1, '/body' => 1, '/html' => 1, '#end' => 1, ); /** * Whitespace directly after these tokens is never rendered. * * @var array */ private static $drop_after = array( '#start' => 1, '!doctype' => 1, 'html' => 1, '/head' => 1, 'body' => 1, '/body' => 1, '/html' => 1, ); /** * Block-level elements for the optional aggressive whitespace removal. *
  • is deliberately absent: menus and tabs commonly render list items as * inline-block, where the whitespace between them is a visible gap. * * @var array */ private static $block = array( 'address' => 1, 'article' => 1, 'aside' => 1, 'base' => 1, 'blockquote' => 1, 'body' => 1, 'caption' => 1, 'col' => 1, 'colgroup' => 1, 'dd' => 1, 'details' => 1, 'dialog' => 1, 'div' => 1, 'dl' => 1, 'dt' => 1, 'fieldset' => 1, 'figcaption' => 1, 'figure' => 1, 'footer' => 1, 'form' => 1, 'h1' => 1, 'h2' => 1, 'h3' => 1, 'h4' => 1, 'h5' => 1, 'h6' => 1, 'head' => 1, 'header' => 1, 'hgroup' => 1, 'hr' => 1, 'html' => 1, 'legend' => 1, 'link' => 1, 'main' => 1, 'meta' => 1, 'nav' => 1, 'noscript' => 1, 'ol' => 1, 'optgroup' => 1, 'option' => 1, 'p' => 1, 'script' => 1, 'section' => 1, 'style' => 1, 'summary' => 1, 'table' => 1, 'tbody' => 1, 'td' => 1, 'template' => 1, 'tfoot' => 1, 'th' => 1, 'thead' => 1, 'title' => 1, 'tr' => 1, 'ul' => 1, ); /** * Comments starting with these prefixes carry meaning (IE conditionals, ESI, * Knockout, Vue/Svelte hydration markers, Lit, SSI, cache-plugin markers...). * * @var array */ private static $keep_comment_prefixes = array( '[if', ' 1, 'text/javascript' => 1, 'application/javascript' => 1, 'module' => 1, 'text/ecmascript' => 1, 'application/ecmascript' => 1, 'application/x-javascript' => 1, 'text/x-javascript' => 1, 'text/jscript' => 1, 'text/livescript' => 1, ); /** * Script types that contain JSON data. * * @var array */ private static $json_types = array( 'application/ld+json' => 1, 'application/json' => 1, 'importmap' => 1, 'speculationrules' => 1, ); /** * Options. * * @var array */ private $o; /** * Output chunks. * * @var array */ private $out = array(); /** * Whitespace-only text waiting for the next token to decide its fate. * * @var string */ private $pending_ws = ''; /** * Key of the last emitted token ('div', '/div', '#text', '!--', ...). * * @var string */ private $prev = '#start'; /** * Whether the tokenizer is inside . * * @var bool */ private $in_head = false; /** * Whether (explicit or implied) has started. * * @var bool */ private $in_body = false; /** * Depth of SVG/MathML foreign content. * * @var int */ private $foreign = 0; /** * Stack of open whitespace-preserving elements: array( name, nesting depth ). * * @var array */ private $preserve = array(); /** * Counters describing what was done. * * @var array */ public $stats = array(); /** * Constructor. * * @param array $options { * @type bool $collapse_whitespace Collapse whitespace in text and tags. Default true. * @type bool $remove_comments Remove non-functional comments. Default true. * @type string[] $keep_comments Extra comment prefixes to keep. * @type bool $collapse_block_whitespace Remove whitespace between two block-level tags. Default false. * @type string[] $preserve_classes Class names whose elements keep exact whitespace. * @type bool $minify_json Remove whitespace from JSON script blocks. Default true. * @type callable $css_callback function( string $css ): string for inline