Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrevin committed Dec 29, 2014
1 parent b4662f8 commit 32cdcff
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 170 deletions.
220 changes: 116 additions & 104 deletions HtmlCompressor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,131 +28,143 @@ class HtmlCompressor
*/
public static function compress($data, $options = null)
{
return html_compress($data, $options);
return (new static)
->html_compress($data, $options);
}
}

// HTML Compressor 1.0.0
// Original Author: Tyler Hall <[email protected]>
// Latest Source and Bug Tracker: http://github.com/tylerhall/html-compressor
//
// Attemps to reduce the filesize of an HTML document by removing unnecessary
// whitespace at the beginning and end of lines, inside closing tags, and
// stripping blank lines completely. <pre> tags are respected and their contents
// are left alone. Warning, nested <pre> tags may exhibit unexpected behaviour.
//
// This code is licensed under the MIT Open Source License.
// Copyright (c) 2010 [email protected]
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
function html_compress($data, $options = null)
{
if (!isset($options)) {
$options = array();
}

$data .= "\n";
$out = '';
$inside_pre = false;
$bytecount = 0;

while ($line = get_line($data)) {
$bytecount += strlen($line);

if (!$inside_pre) {
if (strpos($line, '<pre') === false) {
// Since we're not inside a <pre> block, we can trim both ends of the line
$line = trim($line);
/**
* HTML Compressor 1.0.0
* Original Author: Tyler Hall <[email protected]>
* Latest Source and Bug Tracker: http://github.com/tylerhall/html-compressor
*
* Attemps to reduce the filesize of an HTML document by removing unnecessary
* whitespace at the beginning and end of lines, inside closing tags, and
* stripping blank lines completely. <pre> tags are respected and their contents
* are left alone. Warning, nested <pre> tags may exhibit unexpected behaviour.
*
* This code is licensed under the MIT Open Source License.
* Copyright (c) 2010 [email protected]
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @param $data
* @param null $options
* @return bool|mixed|string
*/
private function html_compress($data, $options = null)
{
if (!isset($options)) {
$options = [];
}

// And condense multiple spaces down to one
$line = preg_replace('/\s\s+/', ' ', $line);
$data .= "\n";
$out = '';
$inside_pre = false;
$bytecount = 0;

while ($line = $this->get_line($data)) {
$bytecount += strlen($line);

if (!$inside_pre) {
if (strpos($line, '<pre') === false) {
// Since we're not inside a <pre> block, we can trim both ends of the line
$line = trim($line);

// And condense multiple spaces down to one
$line = preg_replace('/\s\s+/', ' ', $line);
} else {
// Only trim the beginning since we just entered a <pre> block...
$line = ltrim($line);
$inside_pre = true;

// If the <pre> ends on the same line, don't turn on $inside_pre...
if ((strpos($line, '</pre') !== false) && (strripos($line, '</pre') >= strripos($line, '<pre'))) {
$line = rtrim($line);
$inside_pre = false;
}
}
} else {
// Only trim the beginning since we just entered a <pre> block...
$line = ltrim($line);
$inside_pre = true;

// If the <pre> ends on the same line, don't turn on $inside_pre...
if ((strpos($line, '</pre') !== false) && (strripos($line, '</pre') >= strripos($line, '<pre'))) {
// Trim the end of the line now that we found the end of the <pre> block...
$line = rtrim($line);
$inside_pre = false;
}
}
} else {
if ((strpos($line, '</pre') !== false) && (strripos($line, '</pre') >= strripos($line, '<pre'))) {
// Trim the end of the line now that we found the end of the <pre> block...
$line = rtrim($line);
$inside_pre = false;

// Filter out any blank lines that aren't inside a <pre> block...
if ($inside_pre || $line != '') {
$out .= $line . "\n";
}
}

// Filter out any blank lines that aren't inside a <pre> block...
if ($inside_pre || $line != '') {
$out .= $line . "\n";
// Perform any extra (unsafe) compression techniques...
if (array_key_exists('x', $options) || array_key_exists('extra', $options)) {
// Can break layouts that are dependent on whitespace between tags
$out = str_replace(">\n<", '><', $out);
}
}

// Perform any extra (unsafe) compression techniques...
if (array_key_exists('x', $options) || array_key_exists('extra', $options)) {
// Can break layouts that are dependent on whitespace between tags
$out = str_replace(">\n<", '><', $out);
}
// Remove HTML comments...
if (array_key_exists('c', $options) || array_key_exists('no-comments', $options)) {
$out = preg_replace('/(<!--.*?-->)/ms', '', $out);
$out = str_replace('<!>', '', $out);
}

// Remove HTML comments...
if (array_key_exists('c', $options) || array_key_exists('no-comments', $options)) {
$out = preg_replace('/(<!--.*?-->)/ms', '', $out);
$out = str_replace('<!>', '', $out);
}
// Remove the trailing \n
$out = trim($out);

// Remove the trailing \n
$out = trim($out);

// Output either our stats or the compressed data...
if (array_key_exists('s', $options) || array_key_exists('stats', $options)) {
$echo = '';
$echo .= "Original Size: $bytecount\n";
$echo .= "Compressed Size: " . strlen($out) . "\n";
$echo .= "Savings: " . round((1 - strlen($out) / $bytecount) * 100, 2) . "%\n";
echo $echo;
} else {
return $out;
}

return false;
}
// Output either our stats or the compressed data...
if (array_key_exists('s', $options) || array_key_exists('stats', $options)) {
$echo = '';
$echo .= "Original Size: $bytecount\n";
$echo .= "Compressed Size: " . strlen($out) . "\n";
$echo .= "Savings: " . round((1 - strlen($out) / $bytecount) * 100, 2) . "%\n";
echo $echo;
} else {
return $out;
}

// Returns the next line from an open file handle or a string
function get_line(&$data)
{
if (is_resource($data)) {
return fgets($data);
return false;
}

if (is_string($data)) {
if (strlen($data) > 0) {
$pos = strpos($data, "\n");
$return = substr($data, 0, $pos) . "\n";
$data = substr($data, $pos + 1);
/**
* Returns the next line from an open file handle or a string
* @param $data
* @return bool|string
*/
private function get_line(&$data)
{
if (is_resource($data)) {
return fgets($data);
}

return $return;
} else {
return false;
if (is_string($data)) {
if (strlen($data) > 0) {
$pos = strpos($data, "\n");
$return = substr($data, 0, $pos) . "\n";
$data = substr($data, $pos + 1);

return $return;
} else {
return false;
}
}
}

return false;
return false;
}
}
87 changes: 30 additions & 57 deletions View.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,34 @@
class View extends \yii\web\View
{

/**
* @var string path alias to web base
*/
/** @var bool */
public $enableMinify = true;

/** @var string path alias to web base */
public $base_path = '@app/web';

/**
* @var string path alias to save minify result
*/
/** @var string path alias to save minify result */
public $minify_path = '@app/web/minify';

/**
* @var array positions of js files to be minified
*/
/** @var array positions of js files to be minified */
public $js_position = [self::POS_END, self::POS_HEAD];

/**
* @var bool|string charset forcibly assign, otherwise will use all of the files found charset
*/
/** @var bool|string charset forcibly assign, otherwise will use all of the files found charset */
public $force_charset = false;

/**
* @var bool whether to change @import on content
*/
/** @var bool whether to change @import on content */
public $expand_imports = true;

/**
* @var int
*/
/** @var int */
public $css_linebreak_pos = 2048;

/**
* @var int|bool chmod of minified file. If false chmod not set
*/
/** @var int|bool chmod of minified file. If false chmod not set */
public $file_mode = 0664;

/**
* @var array schemes that will be ignored during normalization url
*/
/** @var array schemes that will be ignored during normalization url */
public $schemas = ['//', 'http://', 'https://', 'ftp://'];

/**
* @var bool do I need to compress the result html page.
*/
/** @var bool do I need to compress the result html page. */
public $compress_output = false;

/**
Expand Down Expand Up @@ -103,7 +88,11 @@ public function endPage($ajaxMode = false)
$this->registerAssetFiles($bundle);
}

$this->minify();
if (true === $this->enableMinify) {
$this
->minifyCSS()
->minifyJS();
}

echo strtr(
$content,
Expand All @@ -117,34 +106,6 @@ public function endPage($ajaxMode = false)
$this->clear();
}

/**
* @inheritdoc
*/
protected function registerAssetFiles($name)
{
if (!isset($this->assetBundles[$name])) {
return;
}

$bundle = $this->assetBundles[$name];
if ($bundle) {
foreach ($bundle->depends as $dep) {
$this->registerAssetFiles($dep);
}

$bundle->registerAssetFiles($this);
}

unset($this->assetBundles[$name]);
}

private function minify()
{
$this
->minifyCSS()
->minifyJS();
}

/**
* @return self
*/
Expand Down Expand Up @@ -229,6 +190,10 @@ private function expandImports(&$css)
}
}

/**
* @param string $css
* @return string
*/
private function collectCharsets(&$css)
{
$charsets = '';
Expand All @@ -244,6 +209,10 @@ private function collectCharsets(&$css)
return $charsets;
}

/**
* @param string $css
* @return string
*/
private function collectImports(&$css)
{
$imports = '';
Expand All @@ -257,6 +226,10 @@ private function collectImports(&$css)
return $imports;
}

/**
* @param string $css
* @return string
*/
private function collectFonts(&$css)
{
$fonts = '';
Expand Down Expand Up @@ -359,4 +332,4 @@ private function _getSummaryFilesHash($files)

return sha1($result);
}
}
}
Loading

0 comments on commit 32cdcff

Please sign in to comment.