1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-12 00:54:35 +02:00

Minify_CSS : remove charset at-rules by default & options cleanup

Minify_HTML : speed patch (Issue 192)
Minify_Controller_MinApp : better error logging (Issue 193)
index.php : allow easier custom controller hacking
This commit is contained in:
Steve Clay
2010-09-30 04:31:58 +00:00
parent e9cd5d4b9b
commit c6a2f87641
9 changed files with 306 additions and 284 deletions

View File

@@ -14,14 +14,12 @@
*
* If you want to use a custom error logger, set this to your logger
* instance. Your object should have a method log(string $message).
*
* @todo cache system does not have error logging yet.
*/
$min_errorLogger = false;
/**
* To allow debugging, you must set this option to true.
* To allow debug mode output, you must set this option to true.
*
* Once true, you can send the cookie minDebug to request debug mode output. The
* cookie value should match the URIs you'd like to debug. E.g. to debug

View File

@@ -54,10 +54,9 @@ if ($min_errorLogger) {
require_once 'Minify/Logger.php';
if (true === $min_errorLogger) {
require_once 'FirePHP.php';
Minify_Logger::setLogger(FirePHP::getInstance(true));
} else {
Minify_Logger::setLogger($min_errorLogger);
$min_errorLogger = FirePHP::getInstance(true);
}
Minify_Logger::setLogger($min_errorLogger);
}
// check for URI versioning
@@ -70,7 +69,12 @@ if (isset($_GET['g'])) {
}
if (isset($_GET['f']) || isset($_GET['g'])) {
// serve!
Minify::serve('MinApp', $min_serveOptions);
if (! isset($min_serveController)) {
require 'Minify/Controller/MinApp.php';
$min_serveController = new Minify_Controller_MinApp();
}
Minify::serve($min_serveController, $min_serveOptions);
} elseif ($min_enableBuilder) {
header('Location: builder/');

View File

@@ -27,6 +27,8 @@ class Minify_CSS {
* with "/*!" will be preserved with newlines before and after to
* enhance readability.
*
* 'removeCharsets': (default true) remove all @charset at-rules
*
* 'prependRelativePath': (default null) if given, this string will be
* prepended to all relative URIs in import/url declarations
*
@@ -46,13 +48,27 @@ class Minify_CSS {
* array('//static' => 'D:\\staticStorage') // Windows
* </code>
*
* 'docRoot': (default = $_SERVER['DOCUMENT_ROOT'])
* see Minify_CSS_UriRewriter::rewrite
*
* @return string
*/
public static function minify($css, $options = array())
{
$options = array_merge(array(
'removeCharsets' => true,
'preserveComments' => true,
'currentDir' => null,
'docRoot' => $_SERVER['DOCUMENT_ROOT'],
'prependRelativePath' => null,
'symlinks' => array(),
), $options);
if ($options['removeCharsets']) {
$css = preg_replace('/@charset[^;]+;\\s*/', '', $css);
}
require_once 'Minify/CSS/Compressor.php';
if (isset($options['preserveComments'])
&& !$options['preserveComments']) {
if (! $options['preserveComments']) {
$css = Minify_CSS_Compressor::process($css, $options);
} else {
require_once 'Minify/CommentPreserver.php';
@@ -62,16 +78,16 @@ class Minify_CSS {
,array($options)
);
}
if (! isset($options['currentDir']) && ! isset($options['prependRelativePath'])) {
if (! $options['currentDir'] && ! $options['prependRelativePath']) {
return $css;
}
require_once 'Minify/CSS/UriRewriter.php';
if (isset($options['currentDir'])) {
if ($options['currentDir']) {
return Minify_CSS_UriRewriter::rewrite(
$css
,$options['currentDir']
,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']
,isset($options['symlinks']) ? $options['symlinks'] : array()
,$options['docRoot']
,$options['symlinks']
);
} else {
return Minify_CSS_UriRewriter::prepend(

View File

@@ -65,11 +65,11 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$file = realpath($file);
if ($file && is_file($file)) {
$sources[] = $this->_getFileSource($file, $cOptions);
$realpath = realpath($file);
if ($realpath && is_file($realpath)) {
$sources[] = $this->_getFileSource($realpath, $cOptions);
} else {
$this->log("The path \"{$file}\" could not be found (or was not a file)");
$this->log("The path \"{$file}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
return $options;
}
}
@@ -97,7 +97,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
// no "./"
|| preg_match('/(?:^|[^\\.])\\.\\//', $_GET['f'])
) {
$this->log("GET param 'f' invalid (see MinApp.php line 63)");
$this->log("GET param 'f' was invalid");
return $options;
}
$ext = ".{$m[1]}";
@@ -109,7 +109,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
}
$files = explode(',', $_GET['f']);
if ($files != array_unique($files)) {
$this->log("Duplicate files specified");
$this->log("Duplicate files were specified");
return $options;
}
if (isset($_GET['b'])) {
@@ -120,7 +120,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
// valid base
$base = "/{$_GET['b']}/";
} else {
$this->log("GET param 'b' invalid (see MinApp.php line 84)");
$this->log("GET param 'b' was invalid");
return $options;
}
} else {
@@ -134,25 +134,26 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
foreach ($files as $file) {
$uri = $base . $file;
$path = $_SERVER['DOCUMENT_ROOT'] . $uri;
$file = realpath($path);
if (false === $file || ! is_file($file)) {
$realpath = realpath($path);
if (false === $realpath || ! is_file($realpath)) {
$this->log("The path \"{$path}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
if (! $missingUri) {
$missingUri = $uri;
continue;
} else {
$this->log("At least two files missing: '$missingUri', '$uri'");
$this->log("More than one file was missing: '$missingUri', '$uri'");
return $options;
}
}
try {
parent::checkNotHidden($file);
parent::checkAllowDirs($file, $allowDirs, $uri);
parent::checkNotHidden($realpath);
parent::checkAllowDirs($realpath, $allowDirs, $uri);
} catch (Exception $e) {
$this->log($e->getMessage());
return $options;
}
$sources[] = $this->_getFileSource($file, $cOptions);
$basenames[] = basename($file, $ext);
$sources[] = $this->_getFileSource($realpath, $cOptions);
$basenames[] = basename($realpath, $ext);
}
if ($this->selectionId) {
$this->selectionId .= '_f=';

View File

@@ -91,13 +91,13 @@ class Minify_HTML {
// replace SCRIPTs (and minify) with placeholders
$this->_html = preg_replace_callback(
'/(\\s*)(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
'/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
,array($this, '_removeScriptCB')
,$this->_html);
// replace STYLEs (and minify) with placeholders
$this->_html = preg_replace_callback(
'/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
'/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i'
,array($this, '_removeStyleCB')
,$this->_html);
@@ -108,13 +108,13 @@ class Minify_HTML {
,$this->_html);
// replace PREs with placeholders
$this->_html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
$this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
,array($this, '_removePreCB')
,$this->_html);
// replace TEXTAREAs with placeholders
$this->_html = preg_replace_callback(
'/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
'/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
,array($this, '_removeTextareaCB')
,$this->_html);
@@ -130,9 +130,9 @@ class Minify_HTML {
.'|ul)\\b[^>]*>)/i', '$1', $this->_html);
// remove ws outside of all elements
$this->_html = preg_replace_callback(
'/>([^<]+)</'
,array($this, '_outsideTagCB')
$this->_html = preg_replace(
'/>(\\s(?:\\s*))?([^<]+)(\\s(?:\s*))?</'
,'>$1$2$3<'
,$this->_html);
// use newlines before 1st attribute in open tags (to limit line lengths)
@@ -167,24 +167,19 @@ class Minify_HTML {
protected $_cssMinifier = null;
protected $_jsMinifier = null;
protected function _outsideTagCB($m)
{
return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
}
protected function _removePreCB($m)
{
return $this->_reservePlace($m[1]);
return $this->_reservePlace("<pre{$m[1]}");
}
protected function _removeTextareaCB($m)
{
return $this->_reservePlace($m[1]);
return $this->_reservePlace("<textarea{$m[1]}");
}
protected function _removeStyleCB($m)
{
$openStyle = $m[1];
$openStyle = "<style{$m[1]}";
$css = $m[2];
// remove HTML comments
$css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
@@ -206,7 +201,7 @@ class Minify_HTML {
protected function _removeScriptCB($m)
{
$openScript = $m[2];
$openScript = "<script{$m[2]}";
$js = $m[3];
// whitespace surrounding? preserve at least one space

View File

@@ -1,3 +1,5 @@
@charset "utf-8";
/* some CSS to try to exercise things in general */
@import url( /more.css );

View File

@@ -20,10 +20,12 @@ rel="alternate"
type="application/rss+xml"
title="RSS"
href="http://www.csszengarden.com/zengarden.xml" /></head><body
id="css-zen-garden"> <!--[if !IE]>--><p>Browser != IE</p><!--<![endif]--><div
id="css-zen-garden">
<!--[if !IE]>--><p>Browser != IE</p><!--<![endif]--><div
id="container"><div
id="pageHeader"><h1><span>css Zen Garden</span></h1><h2><span>The Beauty of <acronym
title="Cascading Style Sheets">CSS</acronym> Design</span></h2></div><pre>
title="Cascading Style Sheets">CSS</acronym>
Design</span></h2></div><pre>
White space is important here!
</pre><div
id="quickSummary"><p

View File

@@ -20,10 +20,12 @@ rel="alternate"
type="application/rss+xml"
title="RSS"
href="http://www.csszengarden.com/zengarden.xml"></head><body
id="css-zen-garden"> <!--[if !IE]>--><p>Browser != IE</p><!--<![endif]--><div
id="css-zen-garden">
<!--[if !IE]>--><p>Browser != IE</p><!--<![endif]--><div
id="container"><div
id="pageHeader"><h1><span>css Zen Garden</span></h1><h2><span>The Beauty of <acronym
title="Cascading Style Sheets">CSS</acronym> Design</span></h2></div><pre>
title="Cascading Style Sheets">CSS</acronym>
Design</span></h2></div><pre>
White space is important here!
</pre><div
id="quickSummary"><p

View File

@@ -1,4 +1,6 @@
@media screen {
@charset "utf-8";
/* some CSS to try to exercise things in general */
@import url(/more.css);