1
0
mirror of https://github.com/mrclay/minify.git synced 2025-04-21 04:41:54 +02:00

Rewrite relative URIs in CSS by default (adjusted test accordingly)

Minify.php: + 'rewriteCssUris' option
Controller/Base.php: + 'rewriteCssUris' default value
Controller/Version1.php: moved rewriting to serve()
Minify/CSS.php: renamed option 'currentPath' to 'currentDir'
Minify/Source.php: made $filepath public
This commit is contained in:
Steve Clay 2008-08-18 23:38:39 +00:00
parent 711fbeb365
commit 03e1989d35
7 changed files with 58 additions and 26 deletions

View File

@ -110,6 +110,9 @@ class Minify {
* before revalidating with the server. This sets Cache-Control: max-age and the
* Expires header. Unlike the old 'setExpires' setting, this setting will NOT
* prevent conditional GETs. Note this has nothing to do with server-side caching.
*
* 'rewriteCssUris' : If true, serve() will automatically set the 'currentDir'
* minifier option to enable URI rewriting in CSS files (default true)
*
* 'debug' : set to true to minify all sources with the 'Lines' controller, which
* eases the debugging of combined files. This also prevents 304 responses.
@ -237,7 +240,20 @@ class Minify {
}
} else {
self::$_options['encodeMethod'] = ''; // identity (no encoding)
}
}
if (self::$_options['contentType'] === self::TYPE_CSS
&& self::$_options['rewriteCssUris']) {
reset($controller->sources);
while (list($key, $source) = each($controller->sources)) {
if ($source->filepath
&& !isset($source->minifyOptions['currentDir'])
&& !isset($source->minifyOptions['prependRelativePath'])
) {
$source->minifyOptions['currentDir'] = dirname($source->filepath);
}
}
}
// check server cache
if (null !== self::$_cache) {

View File

@ -33,8 +33,8 @@ class Minify_CSS {
* 'prependRelativePath': (default null) if given, this string will be
* prepended to all relative URIs in import/url declarations
*
* 'currentPath': (default null) if given, this is assumed to be the
* file path of the current CSS file. Using this, minify will rewrite
* '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.
@ -185,8 +185,8 @@ class Minify_CSS {
if (isset($options['prependRelativePath'])) {
self::$_tempPrepend = $options['prependRelativePath'];
$rewrite = true;
} elseif (isset($options['currentPath'])) {
self::$_tempCurrentPath = $options['currentPath'];
} elseif (isset($options['currentDir'])) {
self::$_tempCurrentDir = $options['currentDir'];
$rewrite = true;
}
if ($rewrite) {
@ -195,7 +195,7 @@ class Minify_CSS {
$css = preg_replace_callback('/url\\(([^\\)]+)\\)/'
,array('Minify_CSS', '_urlCB'), $css);
}
self::$_tempPrepend = self::$_tempCurrentPath = '';
self::$_tempPrepend = self::$_tempCurrentDir = '';
return trim($css);
}
@ -225,9 +225,9 @@ class Minify_CSS {
protected static $_tempPrepend = '';
/**
* @var string path of this stylesheet for rewriting purposes
* @var string directory of this stylesheet for rewriting purposes
*/
protected static $_tempCurrentPath = '';
protected static $_tempCurrentDir = '';
/**
* Process a comment and return a replacement
@ -302,7 +302,7 @@ class Minify_CSS {
} else {
// rewrite absolute url from scratch!
// prepend path with current dir separator (OS-independent)
$path = self::$_tempCurrentPath
$path = self::$_tempCurrentDir
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
// strip doc root
$path = substr($path, strlen($_SERVER['DOCUMENT_ROOT']));

View File

@ -48,7 +48,8 @@ abstract class Minify_Controller_Base {
,'encodeLevel' => 9
,'minifierOptions' => array() // no minifier options
,'contentTypeCharset' => 'UTF-8'
,'maxAge' => 1800 // 30 minutes
,'maxAge' => 1800 // 30 minutes
,'rewriteCssUris' => true
,'quiet' => false // serve() will send headers and output
,'debug' => false

View File

@ -84,9 +84,6 @@ class Minify_Controller_Version1 extends Minify_Controller_Base {
$srcOptions = array(
'filepath' => $file
);
if ('css' === $extension && MINIFY_REWRITE_CSS_URLS) {
$srcOptions['minifyOptions']['currentPath'] = dirname($file);
}
$this->sources[] = new Minify_Source($srcOptions);
} else {
$hasBadSource = true;
@ -95,6 +92,9 @@ class Minify_Controller_Version1 extends Minify_Controller_Base {
}
if ($hasBadSource) {
$this->sources = array();
}
if (! MINIFY_REWRITE_CSS_URLS) {
$options['rewriteCssUris'] = false;
}
return $options;
}

View File

@ -29,7 +29,12 @@ class Minify_Source {
* @var array minification options specific to this source.
*/
public $minifyOptions = null;
/**
* @var string full path of file
*/
public $filepath = null;
/**
* Create a Minify_Source
*
@ -49,7 +54,7 @@ class Minify_Source {
if (0 === strpos($spec['filepath'], '//')) {
$spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1);
}
$this->_filepath = $spec['filepath'];
$this->filepath = $spec['filepath'];
$this->_id = $spec['filepath'];
$this->lastModified = filemtime($spec['filepath'])
// offset for Windows uploaders with out of sync clocks
@ -80,8 +85,8 @@ class Minify_Source {
*/
public function getContent()
{
$content = (null !== $this->_filepath)
? file_get_contents($this->_filepath)
$content = (null !== $this->filepath)
? file_get_contents($this->filepath)
: ((null !== $this->_content)
? $this->_content
: call_user_func($this->_getContentFunc, $this->_id)
@ -154,8 +159,8 @@ class Minify_Source {
,'html' => Minify::TYPE_HTML
);
foreach ($sources as $source) {
if (null !== $source->_filepath) {
$segments = explode('.', $source->_filepath);
if (null !== $source->filepath) {
$segments = explode('.', $source->filepath);
$ext = array_pop($segments);
if (isset($exts[$ext])) {
return $exts[$ext];
@ -166,8 +171,7 @@ class Minify_Source {
}
protected $_content = null;
protected $_getContentFunc = null;
protected $_filepath = null;
protected $_getContentFunc = null;
protected $_id = null;
}

View File

@ -1,4 +1,4 @@
@import url(more.css);body,td,th{font-family:Verdana,"Bitstream Vera Sans",sans-serif;font-size:12px}.nav{margin-left:20%}#main-nav{background-color:red;border:1px
@import url(%PATH_TO_WEB_TEST%/_test_files/css/more.css);body,td,th{font-family:Verdana,"Bitstream Vera Sans",sans-serif;font-size:12px}.nav{margin-left:20%}#main-nav{background-color:red;border:1px
solid #0f7}div#content
h1+p{padding-top:0;margin-top:0}@media all and (min-width: 640px){#media-queries-1{background-color:#0f0}}@media screen and (max-width: 2000px){#media-queries-2{background-color:#0f0}}.float-l{float:left}.form-suggest{height:200px;background:#DEE2D0;vertical-align:top}.form-input
input{font-size:10px}.hide{display:none}.form-input
@ -10,7 +10,7 @@ solid}input{font:11px Verdana,Arial,Helvetica,sans-serif}input.button,input.lite
solid #000;font-size:11px}input.catbutton{background:#FAFAFA;border:1px
solid #000;font-size:10px}input.mainoption{background:#FAFAFA;border:1px
solid #000;font-size:11px;font-weight:bold}input.post,textarea.post{background:#FFF;border:1px
solid #000;font:11px Verdana,Arial,Helvetica,sans-serif;padding-bottom:2px;padding-left:2px}select{background:#FFF;font:11px Verdana,Arial,Helvetica,sans-serif}table{text-align:left}td{vertical-align:middle}td.cat{background-color:#C2C6BA;font-weight:bold;height:20px;letter-spacing:1px;text-indent:4px}td.genmed,.genmed{font-size:11px}td.rowpic{background:#C2C6BA}td.spacerow{background:#E5E6E2}th{background-color:#FADD31;background-image:url(images/cellpic3.gif);background-repeat:repeat-x;color:#68685E;font-size:11px;font-weight:bold;line-height:16px;height:16px;padding-left:8px;padding-right:8px;text-align:center;white-space:nowrap}.admin,.mod{font-size:11px;font-weight:bold}.admin,a.admin,a.admin:visited{color:#FFA34F}.bodyline{background:#FFF;border:1px
solid #000;font:11px Verdana,Arial,Helvetica,sans-serif;padding-bottom:2px;padding-left:2px}select{background:#FFF;font:11px Verdana,Arial,Helvetica,sans-serif}table{text-align:left}td{vertical-align:middle}td.cat{background-color:#C2C6BA;font-weight:bold;height:20px;letter-spacing:1px;text-indent:4px}td.genmed,.genmed{font-size:11px}td.rowpic{background:#C2C6BA}td.spacerow{background:#E5E6E2}th{background-color:#FADD31;background-image:url(%PATH_TO_WEB_TEST%/_test_files/css/images/cellpic3.gif);background-repeat:repeat-x;color:#68685E;font-size:11px;font-weight:bold;line-height:16px;height:16px;padding-left:8px;padding-right:8px;text-align:center;white-space:nowrap}.admin,.mod{font-size:11px;font-weight:bold}.admin,a.admin,a.admin:visited{color:#FFA34F}.bodyline{background:#FFF;border:1px
solid #98AAB1}.center{text-align:center}.code{background:#FAFAFA;border:1px
solid #D1D7DC;color:#060;font:12px Courier,"Courier New",sans-serif;padding:5px}.errorline{background:#E5E6E2;border:1px
solid #8F8B8B;color:#D92A2A}.explaintitle{color:#5C81B1;font-size:11px;font-weight:bold}.forumline{background:#FFF}.gensmall{font-size:10px}.h1-font{color:#069;display:inline;font:bold 13px Verdana,Arial,Helvetica,sans-serif;margin:0;text-decoration:none}.h2-font{display:inline;font-family:Verdana,Arial,Helvetica,sans-serif;font-size:11px}.height1{height:1px}.height22{height:22px}.height25{height:25px}.height28{height:28px}.height30{height:30px}.height40{height:40px}.helpline{border:0

View File

@ -96,16 +96,27 @@ function test_Minify()
// don't allow conditional headers
unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
$pathToWebTest = str_replace(
DIRECTORY_SEPARATOR
,'/'
,substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT']))
);
$expectedContent = str_replace(
'%PATH_TO_WEB_TEST%'
,$pathToWebTest
,file_get_contents($minifyTestPath . '/minified.css')
);
$expected = array(
'success' => true
,'statusCode' => 200
,'content' => file_get_contents($minifyTestPath . '/minified.css')
,'content' => $expectedContent
,'headers' => array (
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
'ETag' => "\"{$lastModified}pub\"",
'Cache-Control' => 'max-age=0, public, must-revalidate',
'Content-Length' => filesize($minifyTestPath . '/minified.css'),
'Content-Length' => strlen($expectedContent),
'Content-Type' => 'text/css; charset=UTF-8',
)
);
@ -117,7 +128,7 @@ function test_Minify()
,'quiet' => true
,'lastModifiedTime' => $lastModified
,'encodeOutput' => false
,'maxAge' => false
,'maxAge' => false
));
$passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');