, 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 stats['inline_css'];
}
}
}
$this->out[] = $open . $content . $close_tag;
$this->prev = '/' . $lname;
return $close + strlen( $cm[0] );
}
/**
* Process an inline or external script element.
*
* @param string $open Opening tag.
* @param string $attrs Raw attributes.
* @param string $content Script text.
* @return array( string $open, string $content )
*/
private function script( $open, $attrs, $content ) {
++$this->stats['scripts'];
$info = self::parse_attrs( $attrs );
$type = isset( $info['type'] ) ? strtolower( trim( $info['type'] ) ) : '';
$semi = strpos( $type, ';' );
if ( false !== $semi ) {
$type = trim( substr( $type, 0, $semi ) );
}
if ( isset( self::$js_types[ $type ] ) ) {
$kind = 'js';
} elseif ( isset( self::$json_types[ $type ] ) ) {
$kind = 'json';
} else {
$kind = 'other';
}
if ( is_callable( $this->o['script_callback'] ) ) {
$replacement = $this->call(
$this->o['script_callback'],
array(
array(
'open' => $open,
'attrs' => $info,
'type' => $type,
'kind' => $kind,
'content' => $content,
),
)
);
if ( is_string( $replacement ) && '' !== $replacement ) {
$open = $replacement;
}
}
if ( ! isset( $info['src'] ) && '' !== trim( $content ) ) {
if ( 'js' === $kind && is_callable( $this->o['js_callback'] ) ) {
$min = $this->call( $this->o['js_callback'], array( $content, 'module' === $type ) );
if ( is_string( $min ) && $min !== $content && self::safe_in_script( $min, $content ) ) {
$content = $min;
++$this->stats['inline_js'];
}
} elseif ( 'json' === $kind && $this->o['minify_json'] ) {
$min = self::minify_json( $content );
if ( $min !== $content && self::safe_in_script( $min, $content ) ) {
$content = $min;
++$this->stats['json'];
}
}
}
return array( $open, $content );
}
/**
* A transformed script must not introduce sequences that change where the element ends.
*
* @param string $new New content.
* @param string $old Original content.
* @return bool
*/
private static function safe_in_script( $new, $old ) {
foreach ( array( '';
}
$trailing = false !== strpos( self::WS, substr( $attrs, -1 ) );
if ( false === strpbrk( $attrs, "\t\n\r\f" ) && false === strpos( $attrs, ' ' ) && ! $trailing ) {
return '<' . $name . $attrs . $slash . '>';
}
$attrs = preg_replace_callback(
'/=[\t\n\f\r ]*+("[^"]*+"|\'[^\']*+\')|[\t\n\f\r ]++/',
static function ( $m ) {
return isset( $m[1] ) ? '=' . $m[1] : ' ';
},
$attrs
);
$attrs = rtrim( $attrs, ' ' );
// "
" must not become "
" (the slash would join the unquoted value).
if ( '/' === $slash && $trailing && '' !== $attrs ) {
$last = substr( $attrs, -1 );
if ( '"' !== $last && "'" !== $last ) {
$attrs .= ' ';
}
}
return '<' . $name . $attrs . $slash . '>';
}
/**
* Parse attributes into a lowercase-name => decoded-value map (first occurrence wins).
*
* @param string $attrs Raw attribute string.
* @return array
*/
public static function parse_attrs( $attrs ) {
$out = array();
if ( '' === trim( $attrs ) ) {
return $out;
}
preg_match_all( '/((?:=|[^\t\n\f\r \/>=])[^\t\n\f\r \/>=]*+)(?:[\t\n\f\r ]*+=[\t\n\f\r ]*+("[^"]*+"|\'[^\']*+\'|[^\t\n\f\r >]++))?/', $attrs, $mm, PREG_SET_ORDER );
foreach ( $mm as $m ) {
$k = strtolower( $m[1] );
if ( isset( $out[ $k ] ) ) {
continue;
}
$v = isset( $m[2] ) ? $m[2] : '';
if ( '' !== $v && ( '"' === $v[0] || "'" === $v[0] ) ) {
$v = substr( $v, 1, -1 );
}
$out[ $k ] = ( false === strpos( $v, '&' ) ) ? $v : html_entity_decode( $v, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
}
return $out;
}
/**
* Find "", $c ) ) {
return $p;
}
$p += $nl;
}
return false;
}
/**
* Whether "", $c );
}
/**
* Find the end of a script element, honouring the "script data escaped" states
* (a "" inside "" does not end the element).
*
* @param string $html Document.
* @param int $from Position after the opening tag.
* @return int|false Position of " $end ) {
return $end;
}
$len = strlen( $html );
$i = $from;
$state = 0; // 0 = data, 1 = escaped, 2 = double escaped.
while ( $i < $len ) {
if ( 0 === $state ) {
$lt = strpos( $html, '<', $i );
if ( false === $lt ) {
return false;
}
if ( 0 === substr_compare( $html, '', $i );
$lt = strpos( $html, '<', $i );
if ( false !== $dash && ( false === $lt || $dash < $lt ) ) {
$state = 0;
$i = $dash + 3;
continue;
}
if ( false === $lt ) {
return false;
}
if ( 1 === $state ) {
if ( self::tag_at( $html, $lt, '