<?php
// phpcs:ignoreFile
/**
* Compress HTML
*
* This is a heavy regex-based removal of whitespace, unnecessary comments and
* tokens. IE conditional comments are preserved. There are also options to have
* STYLE and SCRIPT blocks compressed by callback functions.
*
* A test suite is available.
*
* @package Minify
* @author Stephen Clay
*/
namespace LiteSpeedLib;

defined( ‘WPINC’ ) || exit;

class HTML_MIN {

/**
* @var string
*/
protected $_html = ”;

/**
* @var boolean
*/
protected $_jsCleanComments = true;
protected $_skipComments = array();

/**
* “Minify” an HTML page
*
* @param string $html
*
* @param array $options
*
* ‘cssMinifier’ : (optional) callback function to process content of STYLE
* elements.
*
* ‘jsMinifier’ : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored.
*
* ‘xhtml’ : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype.
*
* @return string
*/
public static function minify( $html, $options = array() ) {
$min = new self( $html, $options );

return $min->process();
}

/**
* Create a minifier object
*
* @param string $html
*
* @param array $options
*
* ‘cssMinifier’ : (optional) callback function to process content of STYLE
* elements.
*
* ‘jsMinifier’ : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored.
*
* ‘jsCleanComments’ : (optional) whether to remove HTML comments beginning and end of script block
*
* ‘xhtml’ : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype.
*/
public function __construct( $html, $options = array() ) {
$this->_html = str_replace( “rn”, “n”, trim( $html ) );
if ( isset( $options[‘xhtml’] ) ) {
$this->_isXhtml = (bool) $options[‘xhtml’];
}
if ( isset( $options[‘cssMinifier’] ) ) {
$this->_cssMinifier = $options[‘cssMinifier’];
}
if ( isset( $options[‘jsMinifier’] ) ) {
$this->_jsMinifier = $options[‘jsMinifier’];
}
if ( isset( $options[‘jsCleanComments’] ) ) {
$this->_jsCleanComments = (bool) $options[‘jsCleanComments’];
}
if ( isset( $options[‘skipComments’] ) ) {
$this->_skipComments = $options[‘skipComments’];
}
}

/**
* Minify the markeup given in the constructor
*
* @return string
*/
public function process() {
if ( $this->_isXhtml === null ) {
$this->_isXhtml = ( false !== strpos( $this->_html, ‘_replacementHash = ‘MINIFYHTML’ . md5( $_SERVER[‘REQUEST_TIME’] );
$this->_placeholders = array();

// replace SCRIPTs (and minify) with placeholders
$this->_html = preg_replace_callback(
‘/(\s*)]*?>)([\s\S]*?)(\s*)/i’,
array( $this, ‘_removeScriptCB’ ),
$this->_html
);

// replace STYLEs (and minify) with placeholders
$this->_html = preg_replace_callback(
‘/\s*]*>)([\s\S]*?)\s*/i’,
array( $this, ‘_removeStyleCB’ ),
$this->_html
);

// remove HTML comments (not containing IE conditional comments).
$this->_html = preg_replace_callback(
‘//’,
array( $this, ‘_commentCB’ ),
$this->_html
);

// replace PREs with placeholders
$this->_html = preg_replace_callback(
‘/\s*

]*?>[\s\S]*?)\s*/i',
array( $this, '_removePreCB' ),
$this->_html
);

// replace TEXTAREAs with placeholders
$this->_html = preg_replace_callback(
'/\s*