mirror of
https://github.com/mrclay/minify.git
synced 2025-08-13 17:44:00 +02:00
+ Minify/CommentPreserver.php w/ unit tests
CSS/UriRewriter.php : unit tests Minify/ImportProcessor.php : unit tests (need JS tests) Minify/CSS.php : refactoring using CommentPreserver.php Minify/Javascript.php : refactoring using CommentPreserver.php Minify/Lines.php : simplification
This commit is contained in:
@@ -47,42 +47,14 @@ class Minify_CSS {
|
||||
&& !$options['preserveComments']) {
|
||||
return self::_minify($css, $options);
|
||||
}
|
||||
$ret = '';
|
||||
while (1) {
|
||||
list($beforeComment, $comment, $afterComment)
|
||||
= self::_nextYuiComment($css);
|
||||
$ret .= self::_minify($beforeComment, $options);
|
||||
if (false === $comment) {
|
||||
break;
|
||||
}
|
||||
$ret .= $comment;
|
||||
$css = $afterComment;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract comments that YUI Compressor preserves.
|
||||
*
|
||||
* @param string $in input
|
||||
*
|
||||
* @return array 3 elements are returned. If a YUI comment is found, the
|
||||
* 2nd element is the comment and the 1st and 2nd are the surrounding
|
||||
* strings. If no comment is found, the entire string is returned as the
|
||||
* 1st element and the other two are false.
|
||||
*/
|
||||
private static function _nextYuiComment($in)
|
||||
{
|
||||
return (
|
||||
(false !== ($start = strpos($in, '/*!')))
|
||||
&& (false !== ($end = strpos($in, '*/', $start + 3)))
|
||||
)
|
||||
? array(
|
||||
substr($in, 0, $start)
|
||||
,"\n/*" . substr($in, $start + 3, $end - $start - 1) . "\n"
|
||||
,substr($in, -(strlen($in) - $end - 2))
|
||||
)
|
||||
: array($in, false, false);
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,9 +85,6 @@ class Minify_CSS {
|
||||
$css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
|
||||
,array('Minify_CSS', '_commentCB'), $css);
|
||||
|
||||
// leave needed comments
|
||||
$css = str_replace('/*keep*/', '/**/', $css);
|
||||
|
||||
// remove ws around { } and last semicolon in declaration block
|
||||
$css = preg_replace('/\\s*{\\s*/', '{', $css);
|
||||
$css = preg_replace('/;?\\s*}\\s*/', '}', $css);
|
||||
@@ -242,7 +211,7 @@ class Minify_CSS {
|
||||
// $m is the comment content w/o the surrounding tokens,
|
||||
// but the return value will replace the entire comment.
|
||||
if ($m === 'keep') {
|
||||
return '/*keep*/';
|
||||
return '/**/';
|
||||
}
|
||||
if ($m === '" "') {
|
||||
// component of http://tantek.com/CSS/Examples/midpass.html
|
||||
@@ -263,7 +232,7 @@ class Minify_CSS {
|
||||
@x', $m, $n)) {
|
||||
// end hack mode after this comment, but preserve the hack and comment content
|
||||
self::$_inHack = false;
|
||||
return "/*/{$n[1]}/*keep*/";
|
||||
return "/*/{$n[1]}/**/";
|
||||
}
|
||||
}
|
||||
if (substr($m, -1) === '\\') { // comment ends like \*/
|
||||
@@ -279,7 +248,7 @@ class Minify_CSS {
|
||||
if (self::$_inHack) {
|
||||
// a regular comment ends hack mode but should be preserved
|
||||
self::$_inHack = false;
|
||||
return '/*keep*/';
|
||||
return '/**/';
|
||||
}
|
||||
return ''; // remove all other comments
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@
|
||||
/**
|
||||
* Rewrite file-relative URIs as root-relative in CSS files
|
||||
*
|
||||
* @todo: unit tests, use in Minify_CSS and Minify_ImportProcessor
|
||||
* @todo: prepend() method
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
@@ -55,14 +55,14 @@ class Minify_CSS_UriRewriter {
|
||||
/**
|
||||
* @var string directory of this stylesheet
|
||||
*/
|
||||
protected static $_currentDir = '';
|
||||
private static $_currentDir = '';
|
||||
|
||||
/**
|
||||
* @var string DOC_ROOT
|
||||
*/
|
||||
protected static $_docRoot = '';
|
||||
private static $_docRoot = '';
|
||||
|
||||
protected static function _uriCB($m)
|
||||
private static function _uriCB($m)
|
||||
{
|
||||
$isImport = ($m[0][0] === '@');
|
||||
if ($isImport) {
|
||||
@@ -90,7 +90,12 @@ class Minify_CSS_UriRewriter {
|
||||
$path = substr($path, strlen(self::$_docRoot));
|
||||
// fix to root-relative URI
|
||||
$uri = strtr($path, DIRECTORY_SEPARATOR, '/');
|
||||
// eat .
|
||||
$uri = str_replace('/./', '/', $uri);
|
||||
// eat ..
|
||||
while (preg_match('@/[^/\\.]+/\\.\\./@', $uri, $m)) {
|
||||
$uri = str_replace($m[0], '/', $uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($isImport) {
|
||||
|
90
min/lib/Minify/CommentPreserver.php
Normal file
90
min/lib/Minify/CommentPreserver.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Minify_CommentPreserver
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
/**
|
||||
* Process a string in pieces preserving C-style comments that begin with "/*!"
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
*/
|
||||
class Minify_CommentPreserver {
|
||||
|
||||
/**
|
||||
* String to be prepended to each preserved comment
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $prepend = "\n";
|
||||
|
||||
/**
|
||||
* String to be appended to each preserved comment
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $append = "\n";
|
||||
|
||||
/**
|
||||
* Process a string outside of C-style comments that begin with "/*!"
|
||||
*
|
||||
* On each non-empty string outside these comments, the given processor
|
||||
* function will be called. The first "!" will be removed from the
|
||||
* preserved comments, and the comments will be surrounded by
|
||||
* Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append.
|
||||
*
|
||||
* @param string $content
|
||||
* @param callback $processor function
|
||||
* @param array $args array of extra arguments to pass to the processor
|
||||
* function (default = array())
|
||||
* @return string
|
||||
*/
|
||||
public static function process($content, $processor, $args = array())
|
||||
{
|
||||
$ret = '';
|
||||
while (true) {
|
||||
list($beforeComment, $comment, $afterComment) = self::_nextComment($content);
|
||||
if ('' !== $beforeComment) {
|
||||
$callArgs = $args;
|
||||
array_unshift($callArgs, $beforeComment);
|
||||
$ret .= call_user_func_array($processor, $callArgs);
|
||||
}
|
||||
if (false === $comment) {
|
||||
break;
|
||||
}
|
||||
$ret .= $comment;
|
||||
$content = $afterComment;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract comments that YUI Compressor preserves.
|
||||
*
|
||||
* @param string $in input
|
||||
*
|
||||
* @return array 3 elements are returned. If a YUI comment is found, the
|
||||
* 2nd element is the comment and the 1st and 2nd are the surrounding
|
||||
* strings. If no comment is found, the entire string is returned as the
|
||||
* 1st element and the other two are false.
|
||||
*/
|
||||
private static function _nextComment($in)
|
||||
{
|
||||
if (
|
||||
false === ($start = strpos($in, '/*!'))
|
||||
|| false === ($end = strpos($in, '*/', $start + 3))
|
||||
) {
|
||||
return array($in, false, false);
|
||||
}
|
||||
$ret = array(
|
||||
substr($in, 0, $start)
|
||||
,self::$prepend . '/*' . substr($in, $start + 3, $end - $start - 1) . self::$append
|
||||
);
|
||||
$endChars = (strlen($in) - $end - 2);
|
||||
$ret[] = (0 === $endChars)
|
||||
? ''
|
||||
: substr($in, -$endChars);
|
||||
return $ret;
|
||||
}
|
||||
}
|
@@ -8,6 +8,10 @@
|
||||
* Linearize a CSS/JS file by including content specified by CSS import
|
||||
* declarations. In CSS files, relative URIs are fixed.
|
||||
*
|
||||
* @imports will be processed regardless of where they appear in the source
|
||||
* files; i.e. @imports commented out or in string content will still be
|
||||
* processed!
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
*/
|
||||
|
@@ -33,42 +33,14 @@ class Minify_Javascript {
|
||||
&& !$options['preserveComments']) {
|
||||
return trim(JSMin::minify($js));
|
||||
}
|
||||
$ret = '';
|
||||
while (1) {
|
||||
list($beforeComment, $comment, $afterComment)
|
||||
= self::_nextYuiComment($js);
|
||||
$ret .= trim(JSMin::minify($beforeComment));
|
||||
if (false === $comment) {
|
||||
break;
|
||||
}
|
||||
$ret .= $comment;
|
||||
$js = $afterComment;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract comments that YUI Compressor preserves.
|
||||
*
|
||||
* @param string $in input
|
||||
*
|
||||
* @return array 3 elements are returned. If a YUI comment is found, the
|
||||
* 2nd element is the comment and the 1st and 2nd are the surrounding
|
||||
* strings. If no comment is found, the entire string is returned as the
|
||||
* 1st element and the other two are false.
|
||||
*/
|
||||
private static function _nextYuiComment($in)
|
||||
{
|
||||
return (
|
||||
(false !== ($start = strpos($in, '/*!')))
|
||||
&& (false !== ($end = strpos($in, '*/', $start + 3)))
|
||||
)
|
||||
? array(
|
||||
substr($in, 0, $start)
|
||||
,"\n/*" . substr($in, $start + 3, $end - $start - 1) . "\n"
|
||||
,substr($in, -(strlen($in) - $end - 2))
|
||||
)
|
||||
: array($in, false, false);
|
||||
require_once 'Minify/CommentPreserver.php';
|
||||
// recursive calls don't preserve comments
|
||||
$options['preserveComments'] = false;
|
||||
return Minify_CommentPreserver::process(
|
||||
$js
|
||||
,array('Minify_Javascript', 'minify')
|
||||
,array($options)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -32,10 +32,8 @@ class Minify_Lines {
|
||||
$id = (isset($options['id']) && $options['id'])
|
||||
? $options['id']
|
||||
: '';
|
||||
if (! $eol = self::_getEol($content)) {
|
||||
return $content;
|
||||
}
|
||||
$lines = explode($eol, $content);
|
||||
$content = str_replace("\r\n", "\n", $content);
|
||||
$lines = explode("\n", $content);
|
||||
$numLines = count($lines);
|
||||
// determine left padding
|
||||
$padTo = strlen($numLines);
|
||||
@@ -50,28 +48,7 @@ class Minify_Lines {
|
||||
$newLines[] = self::_addNote($line, $i, $inComment, $padTo);
|
||||
$inComment = self::_eolInComment($line, $inComment);
|
||||
}
|
||||
return implode($eol, $newLines) . $eol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine EOL character sequence
|
||||
*
|
||||
* @param string $str file content
|
||||
*
|
||||
* @return string EOL char(s) or '' if no EOL could be found
|
||||
*/
|
||||
private static function _getEol($str)
|
||||
{
|
||||
$r = strpos($str, "\r");
|
||||
$n = strpos($str, "\n");
|
||||
if (false === $r && false === $n) {
|
||||
return '';
|
||||
}
|
||||
return ($r !== false)
|
||||
? ($n == ($r + 1)
|
||||
? "\r\n"
|
||||
: "\r")
|
||||
: "\n";
|
||||
return implode("\n", $newLines) . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
|
10
min_extras/unit_tests/_test_files/css_uriRewriter/exp.css
Normal file
10
min_extras/unit_tests/_test_files/css_uriRewriter/exp.css
Normal file
@@ -0,0 +1,10 @@
|
||||
@import "/_test_files/css_uriRewriter/foo.css";
|
||||
@import '/_test_files/css_uriRewriter/bar/foo.css' print;
|
||||
@import '/css/foo.css'; /* abs, should not alter */
|
||||
@import 'http://foo.com/css/foo.css'; /* abs, should not alter */
|
||||
@import url(/_test_files/foo.css) tv, projection;
|
||||
@import url("/css/foo.css"); /* abs, should not alter */
|
||||
@import url(/css2/foo.css); /* abs, should not alter */
|
||||
foo {background:url('/_test_files/css_uriRewriter/bar/foo.png')}
|
||||
foo {background:url('http://foo.com/css/foo.css');} /* abs, should not alter */
|
||||
foo {background:url("//foo.com/css/foo.css");} /* protocol relative, should not alter */
|
10
min_extras/unit_tests/_test_files/css_uriRewriter/in.css
Normal file
10
min_extras/unit_tests/_test_files/css_uriRewriter/in.css
Normal file
@@ -0,0 +1,10 @@
|
||||
@import "foo.css";
|
||||
@import 'bar/foo.css' print;
|
||||
@import '/css/foo.css'; /* abs, should not alter */
|
||||
@import 'http://foo.com/css/foo.css'; /* abs, should not alter */
|
||||
@import url(../foo.css) tv, projection;
|
||||
@import url("/css/foo.css"); /* abs, should not alter */
|
||||
@import url(/css2/foo.css); /* abs, should not alter */
|
||||
foo {background:url('bar/foo.png')}
|
||||
foo {background:url('http://foo.com/css/foo.css');} /* abs, should not alter */
|
||||
foo {background:url("//foo.com/css/foo.css");} /* protocol relative, should not alter */
|
@@ -1,7 +1,7 @@
|
||||
@media screen {
|
||||
/* some CSS to try to exercise things in general */
|
||||
|
||||
|
||||
@import url(/_3rd_party/minify/min_extras/unit_tests/_test_files/css/more.css);
|
||||
|
||||
body, td, th {
|
||||
font-family: Verdana , "Bitstream Vera Sans" , sans-serif ;
|
||||
@@ -30,19 +30,19 @@ h1 + p {
|
||||
@media screen and (max-width: 2000px) {
|
||||
#media-queries-2 { background-color: #0f0; }
|
||||
}
|
||||
|
||||
@import url(http://example.com/hello.css);
|
||||
adjacent foo { background: red url(/red.gif); }
|
||||
adjacent bar { background: url('%TEST_FILES_URI%/cssLinearizer/../green.gif') }
|
||||
adjacent bar { background: url('/_3rd_party/minify/min_extras/unit_tests/_test_files/importProcessor/../green.gif') }
|
||||
}
|
||||
|
||||
@media tv,projection {
|
||||
/* */
|
||||
/* @import url('/_3rd_party/minify/min_extras/unit_tests/_test_files/importProcessor/1/bad.css') bad; */
|
||||
adjacent2 foo { background: red url(/red.gif); }
|
||||
adjacent2 bar { background: url('%TEST_FILES_URI%/cssLinearizer/1/../green.gif') }
|
||||
|
||||
adjacent2 bar { background: url('/_3rd_party/minify/min_extras/unit_tests/_test_files/importProcessor/1/../green.gif') }
|
||||
@import '../input.css';
|
||||
tv foo { background: red url(/red.gif); }
|
||||
tv bar { background: url('%TEST_FILES_URI%/cssLinearizer/1/../green.gif') }
|
||||
tv bar { background: url('/_3rd_party/minify/min_extras/unit_tests/_test_files/importProcessor/1/../green.gif') }
|
||||
}
|
||||
|
||||
|
||||
input foo { background: red url(/red.gif); }
|
||||
input bar { background: url('%TEST_FILES_URI%/cssLinearizer/../green.gif') }
|
||||
input bar { background: url('/_3rd_party/minify/min_extras/unit_tests/_test_files/importProcessor/../green.gif') }
|
30
min_extras/unit_tests/test_Minify_CSS_UriRewriter.php
Normal file
30
min_extras/unit_tests/test_Minify_CSS_UriRewriter.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
require_once '_inc.php';
|
||||
|
||||
require_once 'Minify/CSS/UriRewriter.php';
|
||||
|
||||
function test_Minify_CSS_UriRewriter()
|
||||
{
|
||||
global $thisDir;
|
||||
|
||||
$in = file_get_contents($thisDir . '/_test_files/css_uriRewriter/in.css');
|
||||
$expected = file_get_contents($thisDir . '/_test_files/css_uriRewriter/exp.css');
|
||||
|
||||
$actual = Minify_CSS_UriRewriter::rewrite(
|
||||
$in
|
||||
,$thisDir . '/_test_files/css_uriRewriter'
|
||||
,$thisDir
|
||||
);
|
||||
|
||||
$passed = assertTrue($expected === $actual, 'Minify_CSS_UriRewriter');
|
||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||
echo "\n---Input:\n\n{$in}\n";
|
||||
echo "\n---Output: " .strlen($actual). " bytes\n\n{$actual}\n\n";
|
||||
if (!$passed) {
|
||||
echo "---Expected: " .strlen($expected). " bytes\n\n{$expected}\n\n\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test_Minify_CSS_UriRewriter();
|
37
min_extras/unit_tests/test_Minify_CommentPreserver.php
Normal file
37
min_extras/unit_tests/test_Minify_CommentPreserver.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
require_once '_inc.php';
|
||||
|
||||
require_once 'Minify/CommentPreserver.php';
|
||||
|
||||
function test_Minify_CommentPreserver()
|
||||
{
|
||||
global $thisDir;
|
||||
|
||||
$inOut = array(
|
||||
'/*!*/' => "\n/**/\n"
|
||||
,'/*!*/a' => "\n/**/\n1A"
|
||||
,'a/*!*//*!*/b' => "2A\n/**/\n\n/**/\n3B"
|
||||
,'a/*!*/b/*!*/' => "4A\n/**/\n5B\n/**/\n"
|
||||
);
|
||||
|
||||
foreach ($inOut as $in => $expected) {
|
||||
$actual = Minify_CommentPreserver::process($in, '_test_MCP_processor');
|
||||
$passed = assertTrue($expected === $actual, 'Minify_CommentPreserver');
|
||||
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
|
||||
echo "\n---Output: " .strlen($actual). " bytes\n\n{$actual}\n\n";
|
||||
if (!$passed) {
|
||||
echo "---Expected: " .strlen($expected). " bytes\n\n{$expected}\n\n\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _test_MCP_processor($content, $options = array())
|
||||
{
|
||||
static $callCount = 0;
|
||||
++$callCount;
|
||||
return $callCount . strtoupper($content);
|
||||
}
|
||||
|
||||
test_Minify_CommentPreserver();
|
@@ -5,6 +5,8 @@ require 'test_Minify_Build.php';
|
||||
require 'test_Minify_Cache_File.php';
|
||||
require 'test_Minify_Cache_Memcache.php';
|
||||
require 'test_Minify_CSS.php';
|
||||
require 'test_Minify_CSS_UriRewriter.php';
|
||||
require 'test_Minify_CommentPreserver.php';
|
||||
require 'test_Minify_HTML.php';
|
||||
require 'test_Minify_ImportProcessor.php';
|
||||
require 'test_Minify_Javascript.php';
|
||||
|
Reference in New Issue
Block a user