mirror of
https://github.com/mrclay/minify.git
synced 2025-02-21 07:22:26 +01:00
beginnings of HTML minification support (again)
This commit is contained in:
parent
60943d4654
commit
5527771acf
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>
|
||||
* @copyright 2007 Ryan Grove. All rights reserved.
|
||||
* @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/
|
||||
*/
|
||||
|
||||
// Uncomment when debugging.
|
||||
// error_reporting(E_ALL | E_STRICT);
|
||||
// ini_set('display_errors', 'on');
|
||||
|
||||
if (!defined('MINIFY_BASE_DIR')) {
|
||||
/**
|
||||
* 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>
|
||||
* @copyright 2007 Ryan Grove. All rights reserved.
|
||||
* @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/
|
||||
*/
|
||||
class Minify {
|
||||
const TYPE_CSS = 'text/css';
|
||||
const TYPE_JS = 'text/javascript';
|
||||
const TYPE_CSS = 'text/css';
|
||||
const TYPE_HTML = 'text/html';
|
||||
const TYPE_JS = 'text/javascript';
|
||||
|
||||
protected $files = array();
|
||||
protected $type = self::TYPE_JS;
|
||||
@ -111,7 +108,13 @@ class Minify {
|
||||
|
||||
// Determine the content type based on the extension of the first file
|
||||
// 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.
|
||||
try {
|
||||
@ -133,14 +136,27 @@ class Minify {
|
||||
/**
|
||||
* 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,
|
||||
* Minify::TYPE_HTML, or Minify::TYPE_JS)
|
||||
* @return string minified string
|
||||
*/
|
||||
public static function minify($string, $type = self::TYPE_JS) {
|
||||
return $type === self::TYPE_JS ? self::minifyJS($string) :
|
||||
self::minifyCSS($string);
|
||||
public static function min($string, $type = self::TYPE_JS) {
|
||||
switch ($type) {
|
||||
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 -----------------------------------------------
|
||||
@ -163,6 +179,11 @@ class Minify {
|
||||
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.
|
||||
*
|
||||
|
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);
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user