1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-06 22:26:28 +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

@@ -111,6 +111,9 @@ class Minify {
* Expires header. Unlike the old 'setExpires' setting, this setting will NOT * Expires header. Unlike the old 'setExpires' setting, this setting will NOT
* prevent conditional GETs. Note this has nothing to do with server-side caching. * 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 * 'debug' : set to true to minify all sources with the 'Lines' controller, which
* eases the debugging of combined files. This also prevents 304 responses. * eases the debugging of combined files. This also prevents 304 responses.
* @see Minify_Lines::minify() * @see Minify_Lines::minify()
@@ -239,6 +242,19 @@ class Minify {
self::$_options['encodeMethod'] = ''; // identity (no encoding) 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 // check server cache
if (null !== self::$_cache) { if (null !== self::$_cache) {
// using cache // using cache

View File

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

View File

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

View File

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

View File

@@ -30,6 +30,11 @@ class Minify_Source {
*/ */
public $minifyOptions = null; public $minifyOptions = null;
/**
* @var string full path of file
*/
public $filepath = null;
/** /**
* Create a Minify_Source * Create a Minify_Source
* *
@@ -49,7 +54,7 @@ class Minify_Source {
if (0 === strpos($spec['filepath'], '//')) { if (0 === strpos($spec['filepath'], '//')) {
$spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1); $spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1);
} }
$this->_filepath = $spec['filepath']; $this->filepath = $spec['filepath'];
$this->_id = $spec['filepath']; $this->_id = $spec['filepath'];
$this->lastModified = filemtime($spec['filepath']) $this->lastModified = filemtime($spec['filepath'])
// offset for Windows uploaders with out of sync clocks // offset for Windows uploaders with out of sync clocks
@@ -80,8 +85,8 @@ class Minify_Source {
*/ */
public function getContent() public function getContent()
{ {
$content = (null !== $this->_filepath) $content = (null !== $this->filepath)
? file_get_contents($this->_filepath) ? file_get_contents($this->filepath)
: ((null !== $this->_content) : ((null !== $this->_content)
? $this->_content ? $this->_content
: call_user_func($this->_getContentFunc, $this->_id) : call_user_func($this->_getContentFunc, $this->_id)
@@ -154,8 +159,8 @@ class Minify_Source {
,'html' => Minify::TYPE_HTML ,'html' => Minify::TYPE_HTML
); );
foreach ($sources as $source) { foreach ($sources as $source) {
if (null !== $source->_filepath) { if (null !== $source->filepath) {
$segments = explode('.', $source->_filepath); $segments = explode('.', $source->filepath);
$ext = array_pop($segments); $ext = array_pop($segments);
if (isset($exts[$ext])) { if (isset($exts[$ext])) {
return $exts[$ext]; return $exts[$ext];
@@ -167,7 +172,6 @@ class Minify_Source {
protected $_content = null; protected $_content = null;
protected $_getContentFunc = null; protected $_getContentFunc = null;
protected $_filepath = null;
protected $_id = 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 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 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 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:11px}input.catbutton{background:#FAFAFA;border:1px
solid #000;font-size:10px}input.mainoption{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-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 #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 #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 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

@@ -97,15 +97,26 @@ function test_Minify()
// don't allow conditional headers // don't allow conditional headers
unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']); 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( $expected = array(
'success' => true 'success' => true
,'statusCode' => 200 ,'statusCode' => 200
,'content' => file_get_contents($minifyTestPath . '/minified.css') ,'content' => $expectedContent
,'headers' => array ( ,'headers' => array (
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified), 'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
'ETag' => "\"{$lastModified}pub\"", 'ETag' => "\"{$lastModified}pub\"",
'Cache-Control' => 'max-age=0, public, must-revalidate', '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', 'Content-Type' => 'text/css; charset=UTF-8',
) )
); );