mirror of
https://github.com/mrclay/minify.git
synced 2025-08-07 14:46:29 +02:00
builder/index.php : fix Issue 67
Minify.php : allow minifier = '' Minify/CSS.php : more extensible (Issue 64) Minify/HTML.php : more extensible Minify/Lines.php : fix Issue 55 Minify/Packer.php : usability test_environment.php : test DOCUMENT_ROOT test_Minify_Lines.php : test Issue 55
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
<?php
|
||||
|
||||
// check for auto-encoding
|
||||
$encodeOutput = ! ini_get('zlib.output_compression');
|
||||
$encodeOutput = (function_exists('gzdeflate')
|
||||
&& !ini_get('zlib.output_compression'));
|
||||
|
||||
require dirname(__FILE__) . '/../config.php';
|
||||
|
||||
|
@@ -12,4 +12,23 @@
|
||||
return array(
|
||||
// 'js' => array('//js/file1.js', '//js/file2.js'),
|
||||
// 'css' => array('//css/file1.css', '//css/file2.css'),
|
||||
|
||||
// custom source example
|
||||
/*'js2' => array(
|
||||
dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
|
||||
// do NOT process this file
|
||||
new Minify_Source(array(
|
||||
'filepath' => dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
|
||||
'minifier' => create_function('$a', 'return $a;')
|
||||
))
|
||||
),//*/
|
||||
|
||||
/*'js3' => array(
|
||||
dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
|
||||
// do NOT process this file
|
||||
new Minify_Source(array(
|
||||
'filepath' => dirname(__FILE__) . '/../min_unit_tests/_test_files/js/before.js',
|
||||
'minifier' => array('Minify_Packer', 'minify')
|
||||
))
|
||||
),//*/
|
||||
);
|
@@ -56,9 +56,9 @@ class Minify {
|
||||
* need to recombine files, minify and encode the output.
|
||||
*
|
||||
* @param mixed $cache object with identical interface as Minify_Cache_File or
|
||||
* a directory path. (default = '')
|
||||
*
|
||||
* @param bool $fileLocking (default = true) This only applies if the first
|
||||
* a directory path. (default = '')
|
||||
*
|
||||
* @param bool $fileLocking (default = true) This only applies if the first
|
||||
* parameter is a string.
|
||||
*
|
||||
* @return null
|
||||
@@ -347,17 +347,17 @@ class Minify {
|
||||
*/
|
||||
public static function setDocRoot($unsetPathInfo = false)
|
||||
{
|
||||
if (isset($_SERVER['SERVER_SOFTWARE'])
|
||||
&& 0 === strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/')
|
||||
) {
|
||||
$_SERVER['DOCUMENT_ROOT'] = substr(
|
||||
$_SERVER['PATH_TRANSLATED']
|
||||
,0
|
||||
,strlen($_SERVER['PATH_TRANSLATED']) - strlen($_SERVER['SCRIPT_NAME'])
|
||||
if (isset($_SERVER['SERVER_SOFTWARE'])
|
||||
&& 0 === strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/')
|
||||
) {
|
||||
$_SERVER['DOCUMENT_ROOT'] = substr(
|
||||
$_SERVER['PATH_TRANSLATED']
|
||||
,0
|
||||
,strlen($_SERVER['PATH_TRANSLATED']) - strlen($_SERVER['SCRIPT_NAME'])
|
||||
);
|
||||
if ($unsetPathInfo) {
|
||||
if ($unsetPathInfo) {
|
||||
unset($_SERVER['PATH_INFO']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -439,7 +439,7 @@ class Minify {
|
||||
$options = (null !== $source->minifyOptions)
|
||||
? array_merge($defaultOptions, $source->minifyOptions)
|
||||
: $defaultOptions;
|
||||
if ($defaultMinifier) {
|
||||
if ($minifier) {
|
||||
self::$_controller->loadMinifier($minifier);
|
||||
// get source content and minify it
|
||||
$pieces[] = call_user_func($minifier, $source->getContent(), $options);
|
||||
|
@@ -1,322 +1,330 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Minify_CSS
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
/**
|
||||
* Compress CSS
|
||||
*
|
||||
* This is a heavy regex-based removal of whitespace, unnecessary
|
||||
* comments and tokens, and some CSS value minimization, where practical.
|
||||
* Many steps have been taken to avoid breaking comment-based hacks,
|
||||
* including the ie5/mac filter (and its inversion), but expect tricky
|
||||
* hacks involving comment tokens in 'content' value strings to break
|
||||
* minimization badly. A test suite is available.
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
*/
|
||||
class Minify_CSS {
|
||||
|
||||
/**
|
||||
* Minify a CSS string
|
||||
*
|
||||
* @param string $css
|
||||
*
|
||||
* @param array $options available options:
|
||||
*
|
||||
* 'preserveComments': (default true) multi-line comments that begin
|
||||
* with "/*!" will be preserved with newlines before and after to
|
||||
* enhance readability.
|
||||
*
|
||||
* 'prependRelativePath': (default null) if given, this string will be
|
||||
* prepended to all relative URIs in import/url declarations
|
||||
*
|
||||
* 'currentDir': (default null) if given, this is assumed to be the
|
||||
* directory of the current CSS file. Using this, minify will rewrite
|
||||
* all relative URIs in import/url declarations to correctly point to
|
||||
* the desired files. For this to work, the files *must* exist and be
|
||||
* visible by the PHP process.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function minify($css, $options = array())
|
||||
{
|
||||
if (isset($options['preserveComments'])
|
||||
&& !$options['preserveComments']) {
|
||||
return self::_minify($css, $options);
|
||||
}
|
||||
require_once 'Minify/CommentPreserver.php';
|
||||
// recursive calls don't preserve comments
|
||||
$options['preserveComments'] = false;
|
||||
return Minify_CommentPreserver::process(
|
||||
$css
|
||||
,array('Minify_CSS', 'minify')
|
||||
,array($options)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Minify a CSS string
|
||||
*
|
||||
* @param string $css
|
||||
*
|
||||
* @param array $options To enable URL rewriting, set the value
|
||||
* for key 'prependRelativePath'.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _minify($css, $options)
|
||||
{
|
||||
$css = str_replace("\r\n", "\n", $css);
|
||||
|
||||
// preserve empty comment after '>'
|
||||
// http://www.webdevout.net/css-hacks#in_css-selectors
|
||||
$css = preg_replace('@>/\\*\\s*\\*/@', '>/*keep*/', $css);
|
||||
|
||||
// preserve empty comment between property and value
|
||||
// http://css-discuss.incutio.com/?page=BoxModelHack
|
||||
$css = preg_replace('@/\\*\\s*\\*/\\s*:@', '/*keep*/:', $css);
|
||||
$css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css);
|
||||
|
||||
// apply callback to all valid comments (and strip out surrounding ws
|
||||
self::$_inHack = false;
|
||||
$css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
|
||||
,array('Minify_CSS', '_commentCB'), $css);
|
||||
|
||||
// remove ws around { } and last semicolon in declaration block
|
||||
$css = preg_replace('/\\s*{\\s*/', '{', $css);
|
||||
$css = preg_replace('/;?\\s*}\\s*/', '}', $css);
|
||||
|
||||
// remove ws surrounding semicolons
|
||||
$css = preg_replace('/\\s*;\\s*/', ';', $css);
|
||||
|
||||
// remove ws around urls
|
||||
$css = preg_replace('/
|
||||
url\\( # url(
|
||||
\\s*
|
||||
([^\\)]+?) # 1 = the URL (really just a bunch of non right parenthesis)
|
||||
\\s*
|
||||
\\) # )
|
||||
/x', 'url($1)', $css);
|
||||
|
||||
// remove ws between rules and colons
|
||||
$css = preg_replace('/
|
||||
\\s*
|
||||
([{;]) # 1 = beginning of block or rule separator
|
||||
\\s*
|
||||
([\\*_]?[\\w\\-]+) # 2 = property (and maybe IE filter)
|
||||
\\s*
|
||||
:
|
||||
\\s*
|
||||
(\\b|[#\'"]) # 3 = first character of a value
|
||||
/x', '$1$2:$3', $css);
|
||||
|
||||
// remove ws in selectors
|
||||
$css = preg_replace_callback('/
|
||||
(?: # non-capture
|
||||
\\s*
|
||||
[^~>+,\\s]+ # selector part
|
||||
\\s*
|
||||
[,>+~] # combinators
|
||||
)+
|
||||
\\s*
|
||||
[^~>+,\\s]+ # selector part
|
||||
{ # open declaration block
|
||||
/x'
|
||||
,array('Minify_CSS', '_selectorsCB'), $css);
|
||||
|
||||
// minimize hex colors
|
||||
$css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i'
|
||||
, '$1#$2$3$4$5', $css);
|
||||
|
||||
// remove spaces between font families
|
||||
$css = preg_replace_callback('/font-family:([^;}]+)([;}])/'
|
||||
,array('Minify_CSS', '_fontFamilyCB'), $css);
|
||||
|
||||
$css = preg_replace('/@import\\s+url/', '@import url', $css);
|
||||
|
||||
// replace any ws involving newlines with a single newline
|
||||
$css = preg_replace('/[ \\t]*\\n+\\s*/', "\n", $css);
|
||||
|
||||
// separate common descendent selectors w/ newlines (to limit line lengths)
|
||||
$css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css);
|
||||
|
||||
// Use newline after 1st numeric value (to limit line lengths).
|
||||
$css = preg_replace('/
|
||||
((?:padding|margin|border|outline):\\d+(?:px|em)?) # 1 = prop : 1st numeric value
|
||||
\\s+
|
||||
/x'
|
||||
,"$1\n", $css);
|
||||
|
||||
$rewrite = false;
|
||||
if (isset($options['prependRelativePath'])) {
|
||||
self::$_tempPrepend = $options['prependRelativePath'];
|
||||
$rewrite = true;
|
||||
} elseif (isset($options['currentDir'])) {
|
||||
self::$_tempCurrentDir = $options['currentDir'];
|
||||
$rewrite = true;
|
||||
}
|
||||
if ($rewrite) {
|
||||
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
|
||||
,array('Minify_CSS', '_urlCB'), $css);
|
||||
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
|
||||
,array('Minify_CSS', '_urlCB'), $css);
|
||||
}
|
||||
self::$_tempPrepend = self::$_tempCurrentDir = '';
|
||||
return trim($css);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace what looks like a set of selectors
|
||||
*
|
||||
* @param array $m regex matches
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _selectorsCB($m)
|
||||
{
|
||||
// remove ws around the combinators
|
||||
return preg_replace('/\\s*([,>+~])\\s*/', '$1', $m[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var bool Are we "in" a hack?
|
||||
*
|
||||
* I.e. are some browsers targetted until the next comment?
|
||||
*/
|
||||
protected static $_inHack = false;
|
||||
|
||||
/**
|
||||
* @var string string to be prepended to relative URIs
|
||||
*/
|
||||
protected static $_tempPrepend = '';
|
||||
|
||||
/**
|
||||
* @var string directory of this stylesheet for rewriting purposes
|
||||
*/
|
||||
protected static $_tempCurrentDir = '';
|
||||
|
||||
/**
|
||||
* Process a comment and return a replacement
|
||||
*
|
||||
* @param array $m regex matches
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _commentCB($m)
|
||||
{
|
||||
$m = $m[1];
|
||||
// $m is the comment content w/o the surrounding tokens,
|
||||
// but the return value will replace the entire comment.
|
||||
if ($m === 'keep') {
|
||||
return '/**/';
|
||||
}
|
||||
if ($m === '" "') {
|
||||
// component of http://tantek.com/CSS/Examples/midpass.html
|
||||
return '/*" "*/';
|
||||
}
|
||||
if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) {
|
||||
// component of http://tantek.com/CSS/Examples/midpass.html
|
||||
return '/*";}}/* */';
|
||||
}
|
||||
if (self::$_inHack) {
|
||||
// inversion: feeding only to one browser
|
||||
if (preg_match('@
|
||||
^/ # comment started like /*/
|
||||
\\s*
|
||||
(\\S[\\s\\S]+?) # has at least some non-ws content
|
||||
\\s*
|
||||
/\\* # ends like /*/ or /**/
|
||||
@x', $m, $n)) {
|
||||
// end hack mode after this comment, but preserve the hack and comment content
|
||||
self::$_inHack = false;
|
||||
return "/*/{$n[1]}/**/";
|
||||
}
|
||||
}
|
||||
if (substr($m, -1) === '\\') { // comment ends like \*/
|
||||
// begin hack mode and preserve hack
|
||||
self::$_inHack = true;
|
||||
return '/*\\*/';
|
||||
}
|
||||
if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */
|
||||
// begin hack mode and preserve hack
|
||||
self::$_inHack = true;
|
||||
return '/*/*/';
|
||||
}
|
||||
if (self::$_inHack) {
|
||||
// a regular comment ends hack mode but should be preserved
|
||||
self::$_inHack = false;
|
||||
return '/**/';
|
||||
}
|
||||
return ''; // remove all other comments
|
||||
}
|
||||
|
||||
protected static function _urlCB($m)
|
||||
{
|
||||
$isImport = (0 === strpos($m[0], '@import'));
|
||||
if ($isImport) {
|
||||
$quote = $m[1];
|
||||
$url = $m[2];
|
||||
} else {
|
||||
// is url()
|
||||
// $m[1] is either quoted or not
|
||||
$quote = ($m[1][0] === "'" || $m[1][0] === '"')
|
||||
? $m[1][0]
|
||||
: '';
|
||||
$url = ($quote === '')
|
||||
? $m[1]
|
||||
: substr($m[1], 1, strlen($m[1]) - 2);
|
||||
}
|
||||
if ('/' !== $url[0]) {
|
||||
if (strpos($url, '//') > 0) {
|
||||
// probably starts with protocol, do not alter
|
||||
} else {
|
||||
// relative URI, rewrite!
|
||||
if (self::$_tempPrepend) {
|
||||
$url = self::$_tempPrepend . $url;
|
||||
} else {
|
||||
// rewrite absolute url from scratch!
|
||||
// prepend path with current dir separator (OS-independent)
|
||||
$path = self::$_tempCurrentDir
|
||||
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
|
||||
// strip doc root
|
||||
$path = substr($path, strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
|
||||
// fix to absolute URL
|
||||
$url = strtr($path, DIRECTORY_SEPARATOR, '/');
|
||||
// remove /./ and /../ where possible
|
||||
$url = str_replace('/./', '/', $url);
|
||||
// inspired by patch from Oleg Cherniy
|
||||
do {
|
||||
$url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, -1, $changed);
|
||||
} while ($changed);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $isImport
|
||||
? "@import {$quote}{$url}{$quote}"
|
||||
: "url({$quote}{$url}{$quote})";
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a font-family listing and return a replacement
|
||||
*
|
||||
* @param array $m regex matches
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _fontFamilyCB($m)
|
||||
{
|
||||
$m[1] = preg_replace('/
|
||||
\\s*
|
||||
(
|
||||
"[^"]+" # 1 = family in double qutoes
|
||||
|\'[^\']+\' # or 1 = family in single quotes
|
||||
|[\\w\\-]+ # or 1 = unquoted family
|
||||
)
|
||||
\\s*
|
||||
/x', '$1', $m[1]);
|
||||
return 'font-family:' . $m[1] . $m[2];
|
||||
}
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Class Minify_CSS
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
/**
|
||||
* Compress CSS
|
||||
*
|
||||
* This is a heavy regex-based removal of whitespace, unnecessary
|
||||
* comments and tokens, and some CSS value minimization, where practical.
|
||||
* Many steps have been taken to avoid breaking comment-based hacks,
|
||||
* including the ie5/mac filter (and its inversion), but expect tricky
|
||||
* hacks involving comment tokens in 'content' value strings to break
|
||||
* minimization badly. A test suite is available.
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch)
|
||||
*/
|
||||
class Minify_CSS {
|
||||
|
||||
/**
|
||||
* Defines which class to call as part of callbacks, change this
|
||||
* if you extend Minify_CSS
|
||||
* @var string
|
||||
*/
|
||||
protected static $className = 'Minify_CSS';
|
||||
|
||||
/**
|
||||
* Minify a CSS string
|
||||
*
|
||||
* @param string $css
|
||||
*
|
||||
* @param array $options available options:
|
||||
*
|
||||
* 'preserveComments': (default true) multi-line comments that begin
|
||||
* with "/*!" will be preserved with newlines before and after to
|
||||
* enhance readability.
|
||||
*
|
||||
* 'prependRelativePath': (default null) if given, this string will be
|
||||
* prepended to all relative URIs in import/url declarations
|
||||
*
|
||||
* 'currentDir': (default null) if given, this is assumed to be the
|
||||
* directory of the current CSS file. Using this, minify will rewrite
|
||||
* all relative URIs in import/url declarations to correctly point to
|
||||
* the desired files. For this to work, the files *must* exist and be
|
||||
* visible by the PHP process.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function minify($css, $options = array())
|
||||
{
|
||||
if (isset($options['preserveComments'])
|
||||
&& !$options['preserveComments']) {
|
||||
return self::_minify($css, $options);
|
||||
}
|
||||
require_once 'Minify/CommentPreserver.php';
|
||||
// recursive calls don't preserve comments
|
||||
$options['preserveComments'] = false;
|
||||
return Minify_CommentPreserver::process(
|
||||
$css
|
||||
,array(self::$className, 'minify')
|
||||
,array($options)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Minify a CSS string
|
||||
*
|
||||
* @param string $css
|
||||
*
|
||||
* @param array $options To enable URL rewriting, set the value
|
||||
* for key 'prependRelativePath'.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _minify($css, $options)
|
||||
{
|
||||
$css = str_replace("\r\n", "\n", $css);
|
||||
|
||||
// preserve empty comment after '>'
|
||||
// http://www.webdevout.net/css-hacks#in_css-selectors
|
||||
$css = preg_replace('@>/\\*\\s*\\*/@', '>/*keep*/', $css);
|
||||
|
||||
// preserve empty comment between property and value
|
||||
// http://css-discuss.incutio.com/?page=BoxModelHack
|
||||
$css = preg_replace('@/\\*\\s*\\*/\\s*:@', '/*keep*/:', $css);
|
||||
$css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css);
|
||||
|
||||
// apply callback to all valid comments (and strip out surrounding ws
|
||||
self::$_inHack = false;
|
||||
$css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
|
||||
,array(self::$className, '_commentCB'), $css);
|
||||
|
||||
// remove ws around { } and last semicolon in declaration block
|
||||
$css = preg_replace('/\\s*{\\s*/', '{', $css);
|
||||
$css = preg_replace('/;?\\s*}\\s*/', '}', $css);
|
||||
|
||||
// remove ws surrounding semicolons
|
||||
$css = preg_replace('/\\s*;\\s*/', ';', $css);
|
||||
|
||||
// remove ws around urls
|
||||
$css = preg_replace('/
|
||||
url\\( # url(
|
||||
\\s*
|
||||
([^\\)]+?) # 1 = the URL (really just a bunch of non right parenthesis)
|
||||
\\s*
|
||||
\\) # )
|
||||
/x', 'url($1)', $css);
|
||||
|
||||
// remove ws between rules and colons
|
||||
$css = preg_replace('/
|
||||
\\s*
|
||||
([{;]) # 1 = beginning of block or rule separator
|
||||
\\s*
|
||||
([\\*_]?[\\w\\-]+) # 2 = property (and maybe IE filter)
|
||||
\\s*
|
||||
:
|
||||
\\s*
|
||||
(\\b|[#\'"]) # 3 = first character of a value
|
||||
/x', '$1$2:$3', $css);
|
||||
|
||||
// remove ws in selectors
|
||||
$css = preg_replace_callback('/
|
||||
(?: # non-capture
|
||||
\\s*
|
||||
[^~>+,\\s]+ # selector part
|
||||
\\s*
|
||||
[,>+~] # combinators
|
||||
)+
|
||||
\\s*
|
||||
[^~>+,\\s]+ # selector part
|
||||
{ # open declaration block
|
||||
/x'
|
||||
,array(self::$className, '_selectorsCB'), $css);
|
||||
|
||||
// minimize hex colors
|
||||
$css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i'
|
||||
, '$1#$2$3$4$5', $css);
|
||||
|
||||
// remove spaces between font families
|
||||
$css = preg_replace_callback('/font-family:([^;}]+)([;}])/'
|
||||
,array(self::$className, '_fontFamilyCB'), $css);
|
||||
|
||||
$css = preg_replace('/@import\\s+url/', '@import url', $css);
|
||||
|
||||
// replace any ws involving newlines with a single newline
|
||||
$css = preg_replace('/[ \\t]*\\n+\\s*/', "\n", $css);
|
||||
|
||||
// separate common descendent selectors w/ newlines (to limit line lengths)
|
||||
$css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css);
|
||||
|
||||
// Use newline after 1st numeric value (to limit line lengths).
|
||||
$css = preg_replace('/
|
||||
((?:padding|margin|border|outline):\\d+(?:px|em)?) # 1 = prop : 1st numeric value
|
||||
\\s+
|
||||
/x'
|
||||
,"$1\n", $css);
|
||||
|
||||
$rewrite = false;
|
||||
if (isset($options['prependRelativePath'])) {
|
||||
self::$_tempPrepend = $options['prependRelativePath'];
|
||||
$rewrite = true;
|
||||
} elseif (isset($options['currentDir'])) {
|
||||
self::$_tempCurrentDir = $options['currentDir'];
|
||||
$rewrite = true;
|
||||
}
|
||||
if ($rewrite) {
|
||||
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
|
||||
,array(self::$className, '_urlCB'), $css);
|
||||
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
|
||||
,array(self::$className, '_urlCB'), $css);
|
||||
}
|
||||
self::$_tempPrepend = self::$_tempCurrentDir = '';
|
||||
return trim($css);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace what looks like a set of selectors
|
||||
*
|
||||
* @param array $m regex matches
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _selectorsCB($m)
|
||||
{
|
||||
// remove ws around the combinators
|
||||
return preg_replace('/\\s*([,>+~])\\s*/', '$1', $m[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var bool Are we "in" a hack?
|
||||
*
|
||||
* I.e. are some browsers targetted until the next comment?
|
||||
*/
|
||||
protected static $_inHack = false;
|
||||
|
||||
/**
|
||||
* @var string string to be prepended to relative URIs
|
||||
*/
|
||||
protected static $_tempPrepend = '';
|
||||
|
||||
/**
|
||||
* @var string directory of this stylesheet for rewriting purposes
|
||||
*/
|
||||
protected static $_tempCurrentDir = '';
|
||||
|
||||
/**
|
||||
* Process a comment and return a replacement
|
||||
*
|
||||
* @param array $m regex matches
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _commentCB($m)
|
||||
{
|
||||
$m = $m[1];
|
||||
// $m is the comment content w/o the surrounding tokens,
|
||||
// but the return value will replace the entire comment.
|
||||
if ($m === 'keep') {
|
||||
return '/**/';
|
||||
}
|
||||
if ($m === '" "') {
|
||||
// component of http://tantek.com/CSS/Examples/midpass.html
|
||||
return '/*" "*/';
|
||||
}
|
||||
if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) {
|
||||
// component of http://tantek.com/CSS/Examples/midpass.html
|
||||
return '/*";}}/* */';
|
||||
}
|
||||
if (self::$_inHack) {
|
||||
// inversion: feeding only to one browser
|
||||
if (preg_match('@
|
||||
^/ # comment started like /*/
|
||||
\\s*
|
||||
(\\S[\\s\\S]+?) # has at least some non-ws content
|
||||
\\s*
|
||||
/\\* # ends like /*/ or /**/
|
||||
@x', $m, $n)) {
|
||||
// end hack mode after this comment, but preserve the hack and comment content
|
||||
self::$_inHack = false;
|
||||
return "/*/{$n[1]}/**/";
|
||||
}
|
||||
}
|
||||
if (substr($m, -1) === '\\') { // comment ends like \*/
|
||||
// begin hack mode and preserve hack
|
||||
self::$_inHack = true;
|
||||
return '/*\\*/';
|
||||
}
|
||||
if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */
|
||||
// begin hack mode and preserve hack
|
||||
self::$_inHack = true;
|
||||
return '/*/*/';
|
||||
}
|
||||
if (self::$_inHack) {
|
||||
// a regular comment ends hack mode but should be preserved
|
||||
self::$_inHack = false;
|
||||
return '/**/';
|
||||
}
|
||||
return ''; // remove all other comments
|
||||
}
|
||||
|
||||
protected static function _urlCB($m)
|
||||
{
|
||||
$isImport = (0 === strpos($m[0], '@import'));
|
||||
if ($isImport) {
|
||||
$quote = $m[1];
|
||||
$url = $m[2];
|
||||
} else {
|
||||
// is url()
|
||||
// $m[1] is either quoted or not
|
||||
$quote = ($m[1][0] === "'" || $m[1][0] === '"')
|
||||
? $m[1][0]
|
||||
: '';
|
||||
$url = ($quote === '')
|
||||
? $m[1]
|
||||
: substr($m[1], 1, strlen($m[1]) - 2);
|
||||
}
|
||||
if ('/' !== $url[0]) {
|
||||
if (strpos($url, '//') > 0) {
|
||||
// probably starts with protocol, do not alter
|
||||
} else {
|
||||
// relative URI, rewrite!
|
||||
if (self::$_tempPrepend) {
|
||||
$url = self::$_tempPrepend . $url;
|
||||
} else {
|
||||
// rewrite absolute url from scratch!
|
||||
// prepend path with current dir separator (OS-independent)
|
||||
$path = self::$_tempCurrentDir
|
||||
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
|
||||
// strip doc root
|
||||
$path = substr($path, strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
|
||||
// fix to absolute URL
|
||||
$url = strtr($path, DIRECTORY_SEPARATOR, '/');
|
||||
// remove /./ and /../ where possible
|
||||
$url = str_replace('/./', '/', $url);
|
||||
// inspired by patch from Oleg Cherniy
|
||||
do {
|
||||
$url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, -1, $changed);
|
||||
} while ($changed);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $isImport
|
||||
? "@import {$quote}{$url}{$quote}"
|
||||
: "url({$quote}{$url}{$quote})";
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a font-family listing and return a replacement
|
||||
*
|
||||
* @param array $m regex matches
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function _fontFamilyCB($m)
|
||||
{
|
||||
$m[1] = preg_replace('/
|
||||
\\s*
|
||||
(
|
||||
"[^"]+" # 1 = family in double qutoes
|
||||
|\'[^\']+\' # or 1 = family in single quotes
|
||||
|[\\w\\-]+ # or 1 = unquoted family
|
||||
)
|
||||
\\s*
|
||||
/x', '$1', $m[1]);
|
||||
return 'font-family:' . $m[1] . $m[2];
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,14 @@
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
*/
|
||||
class Minify_HTML {
|
||||
|
||||
|
||||
/**
|
||||
* Defines which class to call as part of callbacks, change this
|
||||
* if you extend Minify_HTML
|
||||
* @var string
|
||||
*/
|
||||
protected static $className = 'Minify_HTML';
|
||||
|
||||
/**
|
||||
* "Minify" an HTML page
|
||||
*
|
||||
@@ -59,30 +66,30 @@ class Minify_HTML {
|
||||
// replace SCRIPTs (and minify) with placeholders
|
||||
$html = preg_replace_callback(
|
||||
'/\\s*(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>\\s*/i'
|
||||
,array('Minify_HTML', '_removeScriptCB')
|
||||
,array(self::$className, '_removeScriptCB')
|
||||
,$html);
|
||||
|
||||
// replace STYLEs (and minify) with placeholders
|
||||
$html = preg_replace_callback(
|
||||
'/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
|
||||
,array('Minify_HTML', '_removeStyleCB')
|
||||
,array(self::$className, '_removeStyleCB')
|
||||
,$html);
|
||||
|
||||
// remove HTML comments (not containing IE conditional comments).
|
||||
$html = preg_replace_callback(
|
||||
'/<!--([\\s\\S]*?)-->/'
|
||||
,array('Minify_HTML', '_commentCB')
|
||||
,array(self::$className, '_commentCB')
|
||||
,$html);
|
||||
|
||||
// replace PREs with placeholders
|
||||
$html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
|
||||
,array('Minify_HTML', '_removePreCB')
|
||||
,array(self::$className, '_removePreCB')
|
||||
, $html);
|
||||
|
||||
// replace TEXTAREAs with placeholders
|
||||
$html = preg_replace_callback(
|
||||
'/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
|
||||
,array('Minify_HTML', '_removeTaCB')
|
||||
,array(self::$className, '_removeTaCB')
|
||||
, $html);
|
||||
|
||||
// trim each line.
|
||||
@@ -99,7 +106,7 @@ class Minify_HTML {
|
||||
// remove ws outside of all elements
|
||||
$html = preg_replace_callback(
|
||||
'/>([^<]+)</'
|
||||
,array('Minify_HTML', '_outsideTagCB')
|
||||
,array(self::$className, '_outsideTagCB')
|
||||
,$html);
|
||||
|
||||
// use newlines before 1st attribute in open tags (to limit line lengths)
|
||||
|
@@ -9,6 +9,7 @@
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
* @author Adam Pedersen (Issue 55 fix)
|
||||
*/
|
||||
class Minify_Lines {
|
||||
|
||||
@@ -71,7 +72,13 @@ class Minify_Lines {
|
||||
if (false === $pos) {
|
||||
return $inComment;
|
||||
} else {
|
||||
$inComment = ! $inComment;
|
||||
if ($pos == 0
|
||||
|| ($inComment
|
||||
? substr($line, $pos, 3)
|
||||
: substr($line, $pos-1, 3)) != '*/*')
|
||||
{
|
||||
$inComment = ! $inComment;
|
||||
}
|
||||
$line = substr($line, $pos + 2);
|
||||
}
|
||||
}
|
||||
|
@@ -14,7 +14,13 @@
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
require 'class.JavaScriptPacker.php';
|
||||
if (false === (@include 'class.JavaScriptPacker.php')) {
|
||||
trigger_error(
|
||||
'The script "class.JavaScriptPacker.php" is required. Please see: http:'
|
||||
.'//code.google.com/p/minify/source/browse/trunk/min/lib/Minify/Packer.php'
|
||||
,E_USER_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Minify Javascript using Dean Edward's Packer
|
||||
|
2
min_unit_tests/_test_files/minify/lines_bugs.js
Normal file
2
min_unit_tests/_test_files/minify/lines_bugs.js
Normal file
@@ -0,0 +1,2 @@
|
||||
var triggerBug = {_default: "*/*"};
|
||||
var essentialFunctionality = true;
|
@@ -26,6 +26,12 @@
|
||||
/* 23 */ };
|
||||
/* 24 */ })();
|
||||
;
|
||||
/* lines_bugs.js */
|
||||
|
||||
/* 1 */ var triggerBug = {_default: "*/*"};
|
||||
/* 2 */ var essentialFunctionality = true;
|
||||
/* 3 */
|
||||
;
|
||||
/* QueryString.js */
|
||||
|
||||
/* 1 */ var MrClay = window.MrClay || {};
|
||||
|
@@ -15,6 +15,7 @@ function test_Lines()
|
||||
,'encodeOutput' => false
|
||||
,'files' => array(
|
||||
"{$thisDir}/_test_files/minify/email.js"
|
||||
,"{$thisDir}/_test_files/minify/lines_bugs.js"
|
||||
,"{$thisDir}/_test_files/minify/QueryString.js"
|
||||
,"{$thisDir}/_test_files/js/before.js"
|
||||
)
|
||||
|
@@ -19,8 +19,22 @@ require_once '_inc.php';
|
||||
function test_environment()
|
||||
{
|
||||
global $thisDir;
|
||||
|
||||
$thisUrl = 'http://'
|
||||
|
||||
// check DOCROOT
|
||||
$noSlash = assertTrue(
|
||||
0 === preg_match('@[\\\\/]$@', $_SERVER['DOCUMENT_ROOT'])
|
||||
,'environment : DOCUMENT_ROOT should not end in trailing slash'
|
||||
);
|
||||
$goodRoot = assertTrue(
|
||||
0 === strpos(realpath(__FILE__), realpath($_SERVER['DOCUMENT_ROOT']))
|
||||
,'environment : DOCUMENT_ROOT should be real path and contain this test file'
|
||||
);
|
||||
if (! $noSlash || ! $goodRoot) {
|
||||
echo "!NOTE: If you cannot modify DOCUMENT_ROOT, see this comment for a workaround:"
|
||||
,"\n http://code.google.com/p/minify/issues/detail?id=68#c6\n";
|
||||
}
|
||||
|
||||
$thisUrl = 'http://'
|
||||
. $_SERVER['HTTP_HOST'] // avoid redirects when SERVER_NAME doesn't match
|
||||
. ('80' === $_SERVER['SERVER_PORT'] ? '' : ":{$_SERVER['SERVER_PORT']}")
|
||||
. dirname($_SERVER['REQUEST_URI'])
|
||||
@@ -37,9 +51,9 @@ function test_environment()
|
||||
}
|
||||
|
||||
$fp = fopen($thisUrl . '?hello=1', 'r', false, stream_context_create(array(
|
||||
'http' => array(
|
||||
'method' => "GET",
|
||||
'header' => "Accept-Encoding: deflate, gzip\r\n"
|
||||
'http' => array(
|
||||
'method' => "GET",
|
||||
'header' => "Accept-Encoding: deflate, gzip\r\n"
|
||||
)
|
||||
)));
|
||||
|
||||
|
Reference in New Issue
Block a user