mirror of
https://github.com/mrclay/minify.git
synced 2025-08-17 19:37:22 +02:00
beginnings of HTML minification support (again)
This commit is contained in:
69
lib/htmlmin.php
Normal file
69
lib/htmlmin.php
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
class HTMLMin {
|
||||||
|
// -- Public Static Methods --------------------------------------------------
|
||||||
|
public static function minify($string) {
|
||||||
|
$htmlmin = new HTMLMin($string);
|
||||||
|
return $htmlmin->getMinifiedHtml();
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Private Instance Variables ---------------------------------------------
|
||||||
|
private $input;
|
||||||
|
|
||||||
|
// -- Private Instance Methods -----------------------------------------------
|
||||||
|
private function replaceCSS($matches) {
|
||||||
|
// Remove HTML comment markers from the CSS (they shouldn't be there
|
||||||
|
// anyway).
|
||||||
|
$css = preg_replace('/<!--([\s\S]*?)-->/', "$1", $matches[2]);
|
||||||
|
|
||||||
|
return '<style'.$matches[1].'>'.trim(Minify::min($css, Minify::TYPE_CSS)).
|
||||||
|
'</style>';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function replaceJavaScript($matches) {
|
||||||
|
// Remove HTML comment markers from the JS (they shouldn't be there anyway).
|
||||||
|
$js = preg_replace('/<!--([\s\S]*?)-->/', "$1", $matches[2]);
|
||||||
|
|
||||||
|
return '<script'.$matches[1].'>'.trim(Minify::min($js, Minify::TYPE_JS)).
|
||||||
|
'</script>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Public Instance Methods ------------------------------------------------
|
||||||
|
public function __construct($input = '') {
|
||||||
|
$this->setInput($input);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInput() {
|
||||||
|
return $this->input;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMinifiedHtml() {
|
||||||
|
$html = trim($this->input);
|
||||||
|
|
||||||
|
// Run JavaScript blocks through JSMin.
|
||||||
|
$html = preg_replace_callback('/<script(\s+[\s\S]*?)?>([\s\S]*?)<\/script>/i',
|
||||||
|
array($this, 'replaceJavaScript'), $html);
|
||||||
|
|
||||||
|
// Run CSS blocks through Minify's CSS minifier.
|
||||||
|
$html = preg_replace_callback('/<style(\s+[\s\S]*?)?>([\s\S]*?)<\/style>/i',
|
||||||
|
array($this, 'replaceCSS'), $html);
|
||||||
|
|
||||||
|
// Remove HTML comments (but not IE conditional comments).
|
||||||
|
$html = preg_replace('/<!--[^[][\s\S]*?-->/', '', $html);
|
||||||
|
|
||||||
|
// Remove leading and trailing whitespace from each line.
|
||||||
|
// FIXME: This needs to take into account attribute values that span multiple lines.
|
||||||
|
$html = preg_replace('/^\s*(.*?)\s*$/m', "$1", $html);
|
||||||
|
|
||||||
|
// Remove unnecessary whitespace between and inside elements.
|
||||||
|
$html = preg_replace('/>\s+(\S[\s\S]*?)?</', "> $1<", $html);
|
||||||
|
$html = preg_replace('/>(\S[\s\S]*?)?\s+</', ">$1 <", $html);
|
||||||
|
$html = preg_replace('/>\s+</', "> <", $html);
|
||||||
|
|
||||||
|
return $html;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setInput($input) {
|
||||||
|
$this->input = $input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
47
minify.php
47
minify.php
@@ -16,14 +16,10 @@
|
|||||||
* @author Ryan Grove <ryan@wonko.com>
|
* @author Ryan Grove <ryan@wonko.com>
|
||||||
* @copyright 2007 Ryan Grove. All rights reserved.
|
* @copyright 2007 Ryan Grove. All rights reserved.
|
||||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||||
* @version 1.0.1 (2007-05-05)
|
* @version 1.1.0 (?)
|
||||||
* @link http://code.google.com/p/minify/
|
* @link http://code.google.com/p/minify/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Uncomment when debugging.
|
|
||||||
// error_reporting(E_ALL | E_STRICT);
|
|
||||||
// ini_set('display_errors', 'on');
|
|
||||||
|
|
||||||
if (!defined('MINIFY_BASE_DIR')) {
|
if (!defined('MINIFY_BASE_DIR')) {
|
||||||
/**
|
/**
|
||||||
* Base path from which all relative file paths should be resolved. By default
|
* Base path from which all relative file paths should be resolved. By default
|
||||||
@@ -76,12 +72,13 @@ if (!defined('MINIFY_USE_CACHE')) {
|
|||||||
* @author Ryan Grove <ryan@wonko.com>
|
* @author Ryan Grove <ryan@wonko.com>
|
||||||
* @copyright 2007 Ryan Grove. All rights reserved.
|
* @copyright 2007 Ryan Grove. All rights reserved.
|
||||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||||
* @version 1.0.1 (2007-05-05)
|
* @version 1.1.0 (?)
|
||||||
* @link http://code.google.com/p/minify/
|
* @link http://code.google.com/p/minify/
|
||||||
*/
|
*/
|
||||||
class Minify {
|
class Minify {
|
||||||
const TYPE_CSS = 'text/css';
|
const TYPE_CSS = 'text/css';
|
||||||
const TYPE_JS = 'text/javascript';
|
const TYPE_HTML = 'text/html';
|
||||||
|
const TYPE_JS = 'text/javascript';
|
||||||
|
|
||||||
protected $files = array();
|
protected $files = array();
|
||||||
protected $type = self::TYPE_JS;
|
protected $type = self::TYPE_JS;
|
||||||
@@ -111,7 +108,13 @@ class Minify {
|
|||||||
|
|
||||||
// Determine the content type based on the extension of the first file
|
// Determine the content type based on the extension of the first file
|
||||||
// requested.
|
// requested.
|
||||||
$type = preg_match('/\.js$/iD', $files[0]) ? self::TYPE_JS : self::TYPE_CSS;
|
if (preg_match('/\.js$/iD', $files[0])) {
|
||||||
|
$type = self::TYPE_JS;
|
||||||
|
} else if (preg_match('/\.css$/iD', $files[0])) {
|
||||||
|
$type = self::TYPE_CSS;
|
||||||
|
} else {
|
||||||
|
$type = self::TYPE_HTML;
|
||||||
|
}
|
||||||
|
|
||||||
// Minify and spit out the result.
|
// Minify and spit out the result.
|
||||||
try {
|
try {
|
||||||
@@ -133,14 +136,27 @@ class Minify {
|
|||||||
/**
|
/**
|
||||||
* Minifies the specified string and returns it.
|
* Minifies the specified string and returns it.
|
||||||
*
|
*
|
||||||
* @param string $string JavaScript or CSS string to minify
|
* @param string $string JavaScript, CSS, or HTML string to minify
|
||||||
* @param string $type content type of the string (Minify::TYPE_CSS,
|
* @param string $type content type of the string (Minify::TYPE_CSS,
|
||||||
* Minify::TYPE_HTML, or Minify::TYPE_JS)
|
* Minify::TYPE_HTML, or Minify::TYPE_JS)
|
||||||
* @return string minified string
|
* @return string minified string
|
||||||
*/
|
*/
|
||||||
public static function minify($string, $type = self::TYPE_JS) {
|
public static function min($string, $type = self::TYPE_JS) {
|
||||||
return $type === self::TYPE_JS ? self::minifyJS($string) :
|
switch ($type) {
|
||||||
self::minifyCSS($string);
|
case self::TYPE_CSS:
|
||||||
|
return self::minifyCSS($string);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case self::TYPE_HTML:
|
||||||
|
return self::minifyHTML($string);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case self::TYPE_JS:
|
||||||
|
return self::minifyJS($string);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Protected Static Methods -----------------------------------------------
|
// -- Protected Static Methods -----------------------------------------------
|
||||||
@@ -163,6 +179,11 @@ class Minify {
|
|||||||
return trim($css);
|
return trim($css);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function minifyHTML($html) {
|
||||||
|
require_once dirname(__FILE__).'/lib/htmlmin.php';
|
||||||
|
return HTMLMin::minify($html);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minifies the specified JavaScript string and returns it.
|
* Minifies the specified JavaScript string and returns it.
|
||||||
*
|
*
|
||||||
|
9
test/test.php
Normal file
9
test/test.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
error_reporting(E_ALL | E_STRICT);
|
||||||
|
ini_set('display_errors', 'on');
|
||||||
|
|
||||||
|
define('MINIFY_REWRITE_CSS_URLS', false);
|
||||||
|
|
||||||
|
require '../minify.php';
|
||||||
|
echo Minify::min(file_get_contents('test.html'), Minify::TYPE_HTML);
|
||||||
|
?>
|
Reference in New Issue
Block a user