mirror of
https://github.com/mrclay/minify.git
synced 2025-08-12 00:54:35 +02:00
+ URI rewriter in CSS min, fix for protocol-relative URIs, + partial minify.php compatibility
This commit is contained in:
@@ -64,15 +64,21 @@ class Minify_CSS {
|
||||
$css = preg_replace('/#([a-f\\d])\\1([a-f\\d])\\2([a-f\\d])\\3([\\s;\\}])/i'
|
||||
, '#$1$2$3$4', $css);
|
||||
|
||||
$rewrite = false;
|
||||
if (isset($options['prependRelativePath'])) {
|
||||
self::$_tempPrepend = $options['prependRelativePath'];
|
||||
$rewrite = true;
|
||||
} elseif (isset($options['currentPath'])) {
|
||||
self::$_tempCurrentPath = $options['currentPath'];
|
||||
$rewrite = true;
|
||||
}
|
||||
if ($rewrite) {
|
||||
$css = preg_replace_callback('/@import ([\'"])(.*?)[\'"]\\s*;/'
|
||||
,array('Minify_CSS', '_urlCB'), $css);
|
||||
|
||||
$css = preg_replace_callback('/url\\(([^\\)]+)\\)/'
|
||||
,array('Minify_CSS', '_urlCB'), $css);
|
||||
}
|
||||
|
||||
self::$_tempPrepend = self::$_tempCurrentPath = '';
|
||||
return trim($css);
|
||||
}
|
||||
|
||||
@@ -88,6 +94,11 @@ class Minify_CSS {
|
||||
*/
|
||||
private static $_tempPrepend = '';
|
||||
|
||||
/**
|
||||
* @var string path of this stylesheet for rewriting purposes
|
||||
*/
|
||||
private static $_tempCurrentPath = '';
|
||||
|
||||
/**
|
||||
* Process what looks like a comment and return a replacement
|
||||
*
|
||||
@@ -157,17 +168,24 @@ class Minify_CSS {
|
||||
? $m[1]
|
||||
: substr($m[1], 1, strlen($m[1]) - 2);
|
||||
}
|
||||
if ('/' === $url[0]) {
|
||||
if ('/' === $url[1]) {
|
||||
// protocol relative URI!
|
||||
$url = '//' . self::$_tempPrepend . substr($url, 2);
|
||||
}
|
||||
} else {
|
||||
if ('/' !== $url[0]) {
|
||||
if (strpos($url, '//') > 0) {
|
||||
// probably starts with protocol, do not alter
|
||||
} else {
|
||||
// relative URI
|
||||
$url = self::$_tempPrepend . $url;
|
||||
// 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::$_tempCurrentPath
|
||||
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
|
||||
// strip doc root
|
||||
$path = substr($path, strlen($_SERVER['DOCUMENT_ROOT']));
|
||||
// fix to absolute URL
|
||||
$url = strtr($path, DIRECTORY_SEPARATOR, '/');
|
||||
$url = str_replace('/./', '/', $url);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($isImport) {
|
||||
|
@@ -88,6 +88,32 @@ abstract class Minify_Controller_Base {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is a user-given file within document root, existing,
|
||||
* and having an extension js/css/html/txt
|
||||
*
|
||||
* This is a convenience function for controllers that have to accept
|
||||
* user-given paths
|
||||
*
|
||||
* @param string $file full file path (already processed by realpath())
|
||||
* @param string $docRoot root where files are safe to serve
|
||||
* @return bool file is safe
|
||||
*/
|
||||
public static function _fileIsSafe($file, $docRoot)
|
||||
{
|
||||
if (strpos($file, $docRoot) !== 0 || ! file_exists($file)) {
|
||||
return false;
|
||||
}
|
||||
$base = basename($file);
|
||||
if ($base[0] === '.') {
|
||||
return false;
|
||||
}
|
||||
list($revExt) = explode('.', strrev($base));
|
||||
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
|
||||
}
|
||||
|
||||
/*public static function _haveSameExt
|
||||
|
||||
/**
|
||||
* @var array instances of Minify_Source, which provide content and
|
||||
* any individual minification needs.
|
||||
@@ -125,7 +151,8 @@ abstract class Minify_Controller_Base {
|
||||
*
|
||||
* @return array options for Minify
|
||||
*/
|
||||
public final function analyzeSources($options = array()) {
|
||||
public final function analyzeSources($options = array())
|
||||
{
|
||||
if ($this->sources) {
|
||||
if (! isset($options['contentType'])) {
|
||||
$options['contentType'] = Minify_Source::getContentType($this->sources);
|
||||
|
@@ -113,9 +113,9 @@ class Minify_Source {
|
||||
public static function getContentType($sources)
|
||||
{
|
||||
$exts = array(
|
||||
'css' => 'text/css'
|
||||
,'js' => 'application/x-javascript'
|
||||
,'html' => 'text/html'
|
||||
'css' => Minify::TYPE_CSS
|
||||
,'js' => Minify::TYPE_JS
|
||||
,'html' => Minify::TYPE_HTML
|
||||
);
|
||||
foreach ($sources as $source) {
|
||||
if (null !== $source->_filepath) {
|
||||
|
Reference in New Issue
Block a user