Edit File by line
/home/zeestwma/ceyloniy.../wp-conte.../plugins/litespee.../lib
File: html-min.cls.php
<?php
[0] Fix | Delete
// phpcs:ignoreFile
[1] Fix | Delete
/**
[2] Fix | Delete
* Compress HTML
[3] Fix | Delete
*
[4] Fix | Delete
* This is a heavy regex-based removal of whitespace, unnecessary comments and
[5] Fix | Delete
* tokens. IE conditional comments are preserved. There are also options to have
[6] Fix | Delete
* STYLE and SCRIPT blocks compressed by callback functions.
[7] Fix | Delete
*
[8] Fix | Delete
* A test suite is available.
[9] Fix | Delete
*
[10] Fix | Delete
* @package Minify
[11] Fix | Delete
* @author Stephen Clay <steve@mrclay.org>
[12] Fix | Delete
*/
[13] Fix | Delete
namespace LiteSpeed\Lib;
[14] Fix | Delete
[15] Fix | Delete
defined( 'WPINC' ) || exit;
[16] Fix | Delete
[17] Fix | Delete
class HTML_MIN {
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* @var string
[21] Fix | Delete
*/
[22] Fix | Delete
protected $_html = '';
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* @var boolean
[26] Fix | Delete
*/
[27] Fix | Delete
protected $_jsCleanComments = true;
[28] Fix | Delete
protected $_skipComments = array();
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* "Minify" an HTML page
[32] Fix | Delete
*
[33] Fix | Delete
* @param string $html
[34] Fix | Delete
*
[35] Fix | Delete
* @param array $options
[36] Fix | Delete
*
[37] Fix | Delete
* 'cssMinifier' : (optional) callback function to process content of STYLE
[38] Fix | Delete
* elements.
[39] Fix | Delete
*
[40] Fix | Delete
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
[41] Fix | Delete
* elements. Note: the type attribute is ignored.
[42] Fix | Delete
*
[43] Fix | Delete
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
[44] Fix | Delete
* unset, minify will sniff for an XHTML doctype.
[45] Fix | Delete
*
[46] Fix | Delete
* @return string
[47] Fix | Delete
*/
[48] Fix | Delete
public static function minify( $html, $options = array() ) {
[49] Fix | Delete
$min = new self( $html, $options );
[50] Fix | Delete
[51] Fix | Delete
return $min->process();
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Create a minifier object
[56] Fix | Delete
*
[57] Fix | Delete
* @param string $html
[58] Fix | Delete
*
[59] Fix | Delete
* @param array $options
[60] Fix | Delete
*
[61] Fix | Delete
* 'cssMinifier' : (optional) callback function to process content of STYLE
[62] Fix | Delete
* elements.
[63] Fix | Delete
*
[64] Fix | Delete
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
[65] Fix | Delete
* elements. Note: the type attribute is ignored.
[66] Fix | Delete
*
[67] Fix | Delete
* 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block
[68] Fix | Delete
*
[69] Fix | Delete
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
[70] Fix | Delete
* unset, minify will sniff for an XHTML doctype.
[71] Fix | Delete
*/
[72] Fix | Delete
public function __construct( $html, $options = array() ) {
[73] Fix | Delete
$this->_html = str_replace( "\r\n", "\n", trim( $html ) );
[74] Fix | Delete
if ( isset( $options['xhtml'] ) ) {
[75] Fix | Delete
$this->_isXhtml = (bool) $options['xhtml'];
[76] Fix | Delete
}
[77] Fix | Delete
if ( isset( $options['cssMinifier'] ) ) {
[78] Fix | Delete
$this->_cssMinifier = $options['cssMinifier'];
[79] Fix | Delete
}
[80] Fix | Delete
if ( isset( $options['jsMinifier'] ) ) {
[81] Fix | Delete
$this->_jsMinifier = $options['jsMinifier'];
[82] Fix | Delete
}
[83] Fix | Delete
if ( isset( $options['jsCleanComments'] ) ) {
[84] Fix | Delete
$this->_jsCleanComments = (bool) $options['jsCleanComments'];
[85] Fix | Delete
}
[86] Fix | Delete
if ( isset( $options['skipComments'] ) ) {
[87] Fix | Delete
$this->_skipComments = $options['skipComments'];
[88] Fix | Delete
}
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Minify the markeup given in the constructor
[93] Fix | Delete
*
[94] Fix | Delete
* @return string
[95] Fix | Delete
*/
[96] Fix | Delete
public function process() {
[97] Fix | Delete
if ( $this->_isXhtml === null ) {
[98] Fix | Delete
$this->_isXhtml = ( false !== strpos( $this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML' ) );
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
$this->_replacementHash = 'MINIFYHTML' . md5( $_SERVER['REQUEST_TIME'] );
[102] Fix | Delete
$this->_placeholders = array();
[103] Fix | Delete
[104] Fix | Delete
// replace SCRIPTs (and minify) with placeholders
[105] Fix | Delete
$this->_html = preg_replace_callback(
[106] Fix | Delete
'/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i',
[107] Fix | Delete
array( $this, '_removeScriptCB' ),
[108] Fix | Delete
$this->_html
[109] Fix | Delete
);
[110] Fix | Delete
[111] Fix | Delete
// replace STYLEs (and minify) with placeholders
[112] Fix | Delete
$this->_html = preg_replace_callback(
[113] Fix | Delete
'/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i',
[114] Fix | Delete
array( $this, '_removeStyleCB' ),
[115] Fix | Delete
$this->_html
[116] Fix | Delete
);
[117] Fix | Delete
[118] Fix | Delete
// remove HTML comments (not containing IE conditional comments).
[119] Fix | Delete
$this->_html = preg_replace_callback(
[120] Fix | Delete
'/<!--([\\s\\S]*?)-->/',
[121] Fix | Delete
array( $this, '_commentCB' ),
[122] Fix | Delete
$this->_html
[123] Fix | Delete
);
[124] Fix | Delete
[125] Fix | Delete
// replace PREs with placeholders
[126] Fix | Delete
$this->_html = preg_replace_callback(
[127] Fix | Delete
'/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i',
[128] Fix | Delete
array( $this, '_removePreCB' ),
[129] Fix | Delete
$this->_html
[130] Fix | Delete
);
[131] Fix | Delete
[132] Fix | Delete
// replace TEXTAREAs with placeholders
[133] Fix | Delete
$this->_html = preg_replace_callback(
[134] Fix | Delete
'/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i',
[135] Fix | Delete
array( $this, '_removeTextareaCB' ),
[136] Fix | Delete
$this->_html
[137] Fix | Delete
);
[138] Fix | Delete
[139] Fix | Delete
// trim each line.
[140] Fix | Delete
// @todo take into account attribute values that span multiple lines.
[141] Fix | Delete
$this->_html = preg_replace( '/^\\s+|\\s+$/m', '', $this->_html );
[142] Fix | Delete
[143] Fix | Delete
// remove ws around block/undisplayed elements
[144] Fix | Delete
$this->_html = preg_replace(
[145] Fix | Delete
'/\\s+(<\\/?(?:area|article|aside|base(?:font)?|blockquote|body'
[146] Fix | Delete
. '|canvas|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|figcaption|figure|footer|form'
[147] Fix | Delete
. '|frame(?:set)?|h[1-6]|head|header|hgroup|hr|html|legend|li|link|main|map|menu|meta|nav'
[148] Fix | Delete
. '|ol|opt(?:group|ion)|output|p|param|section|t(?:able|body|head|d|h||r|foot|itle)'
[149] Fix | Delete
. '|ul|video)\\b[^>]*>)/i',
[150] Fix | Delete
'$1',
[151] Fix | Delete
$this->_html
[152] Fix | Delete
);
[153] Fix | Delete
[154] Fix | Delete
// remove ws outside of all elements
[155] Fix | Delete
$this->_html = preg_replace(
[156] Fix | Delete
'/>(\\s(?:\\s*))?([^<]+)(\\s(?:\s*))?</',
[157] Fix | Delete
'>$1$2$3<',
[158] Fix | Delete
$this->_html
[159] Fix | Delete
);
[160] Fix | Delete
[161] Fix | Delete
// use newlines before 1st attribute in open tags (to limit line lengths)
[162] Fix | Delete
// $this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
[163] Fix | Delete
[164] Fix | Delete
// fill placeholders
[165] Fix | Delete
$this->_html = str_replace(
[166] Fix | Delete
array_keys( $this->_placeholders ),
[167] Fix | Delete
array_values( $this->_placeholders ),
[168] Fix | Delete
$this->_html
[169] Fix | Delete
);
[170] Fix | Delete
// issue 229: multi-pass to catch scripts that didn't get replaced in textareas
[171] Fix | Delete
$this->_html = str_replace(
[172] Fix | Delete
array_keys( $this->_placeholders ),
[173] Fix | Delete
array_values( $this->_placeholders ),
[174] Fix | Delete
$this->_html
[175] Fix | Delete
);
[176] Fix | Delete
[177] Fix | Delete
return $this->_html;
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
/**
[181] Fix | Delete
* From LSCWP 6.2: Changed the function to test for special comments that will be skipped. See: https://github.com/litespeedtech/lscache_wp/pull/622
[182] Fix | Delete
*/
[183] Fix | Delete
protected function _commentCB( $m ) {
[184] Fix | Delete
// If is IE conditional comment return it.
[185] Fix | Delete
if ( 0 === strpos( $m[1], '[' ) || false !== strpos( $m[1], '<![' ) ) {
[186] Fix | Delete
return $m[0];
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
// Check if comment text is present in Page Optimization -> HTML Settings -> HTML Keep comments
[190] Fix | Delete
if ( count( $this->_skipComments ) > 0 ) {
[191] Fix | Delete
foreach ( $this->_skipComments as $comment ) {
[192] Fix | Delete
if ( $comment && strpos( $m[1], $comment ) !== false ) {
[193] Fix | Delete
return $m[0];
[194] Fix | Delete
}
[195] Fix | Delete
}
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
// Comment can be removed.
[199] Fix | Delete
return '';
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
protected function _reservePlace( $content ) {
[203] Fix | Delete
$placeholder = '%' . $this->_replacementHash . count( $this->_placeholders ) . '%';
[204] Fix | Delete
$this->_placeholders[ $placeholder ] = $content;
[205] Fix | Delete
[206] Fix | Delete
return $placeholder;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
protected $_isXhtml = null;
[210] Fix | Delete
protected $_replacementHash = null;
[211] Fix | Delete
protected $_placeholders = array();
[212] Fix | Delete
protected $_cssMinifier = null;
[213] Fix | Delete
protected $_jsMinifier = null;
[214] Fix | Delete
[215] Fix | Delete
protected function _removePreCB( $m ) {
[216] Fix | Delete
return $this->_reservePlace( "<pre{$m[1]}" );
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
protected function _removeTextareaCB( $m ) {
[220] Fix | Delete
return $this->_reservePlace( "<textarea{$m[1]}" );
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
protected function _removeStyleCB( $m ) {
[224] Fix | Delete
$openStyle = "<style{$m[1]}";
[225] Fix | Delete
$css = $m[2];
[226] Fix | Delete
// remove HTML comments
[227] Fix | Delete
$css = preg_replace( '/(?:^\\s*<!--|-->\\s*$)/', '', $css );
[228] Fix | Delete
[229] Fix | Delete
// remove CDATA section markers
[230] Fix | Delete
$css = $this->_removeCdata( $css );
[231] Fix | Delete
[232] Fix | Delete
// minify
[233] Fix | Delete
$minifier = $this->_cssMinifier
[234] Fix | Delete
? $this->_cssMinifier
[235] Fix | Delete
: 'trim';
[236] Fix | Delete
$css = call_user_func( $minifier, $css );
[237] Fix | Delete
[238] Fix | Delete
return $this->_reservePlace(
[239] Fix | Delete
$this->_needsCdata( $css )
[240] Fix | Delete
? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
[241] Fix | Delete
: "{$openStyle}{$css}</style>"
[242] Fix | Delete
);
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
protected function _removeScriptCB( $m ) {
[246] Fix | Delete
$openScript = "<script{$m[2]}";
[247] Fix | Delete
$js = $m[3];
[248] Fix | Delete
[249] Fix | Delete
// whitespace surrounding? preserve at least one space
[250] Fix | Delete
$ws1 = ( $m[1] === '' ) ? '' : ' ';
[251] Fix | Delete
$ws2 = ( $m[4] === '' ) ? '' : ' ';
[252] Fix | Delete
[253] Fix | Delete
// remove HTML comments (and ending "//" if present)
[254] Fix | Delete
if ( $this->_jsCleanComments ) {
[255] Fix | Delete
$js = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js );
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
// remove CDATA section markers
[259] Fix | Delete
$js = $this->_removeCdata( $js );
[260] Fix | Delete
[261] Fix | Delete
// minify
[262] Fix | Delete
/**
[263] Fix | Delete
* Added 2nd param by LiteSpeed
[264] Fix | Delete
*
[265] Fix | Delete
* @since 2.2.3
[266] Fix | Delete
*/
[267] Fix | Delete
if ( $this->_jsMinifier ) {
[268] Fix | Delete
$js = call_user_func( $this->_jsMinifier, $js, trim( $m[2] ) );
[269] Fix | Delete
} else {
[270] Fix | Delete
$js = trim( $js );
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
return $this->_reservePlace(
[274] Fix | Delete
$this->_needsCdata( $js )
[275] Fix | Delete
? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
[276] Fix | Delete
: "{$ws1}{$openScript}{$js}</script>{$ws2}"
[277] Fix | Delete
);
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
protected function _removeCdata( $str ) {
[281] Fix | Delete
return ( false !== strpos( $str, '<![CDATA[' ) )
[282] Fix | Delete
? str_replace( array( '<![CDATA[', ']]>' ), '', $str )
[283] Fix | Delete
: $str;
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
protected function _needsCdata( $str ) {
[287] Fix | Delete
return ( $this->_isXhtml && preg_match( '/(?:[<&]|\\-\\-|\\]\\]>)/', $str ) );
[288] Fix | Delete
}
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function