mirror of
https://github.com/mrclay/minify.git
synced 2025-08-02 20:27:43 +02:00
Added CSS/UriRewriter.php : untested!
Minify/ImportProcessor.php units need fixing
This commit is contained in:
102
min/lib/Minify/CSS/UriRewriter.php
Normal file
102
min/lib/Minify/CSS/UriRewriter.php
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Class Minify_CSS_UriRewriter
|
||||||
|
* @package Minify
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewrite file-relative URIs as root-relative in CSS files
|
||||||
|
*
|
||||||
|
* @todo: unit tests, use in Minify_CSS and Minify_ImportProcessor
|
||||||
|
*
|
||||||
|
* @package Minify
|
||||||
|
* @author Stephen Clay <steve@mrclay.org>
|
||||||
|
*/
|
||||||
|
class Minify_CSS_UriRewriter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewrite file relative URIs as root relative in CSS files
|
||||||
|
*
|
||||||
|
* @param string $css
|
||||||
|
*
|
||||||
|
* @param string $currentDir The directory of the current CSS file.
|
||||||
|
*
|
||||||
|
* @param string $docRoot The document root of the web site in which
|
||||||
|
* the CSS file resides (default = $_SERVER['DOCUMENT_ROOT']).
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function rewrite($css, $currentDir, $docRoot = null)
|
||||||
|
{
|
||||||
|
self::$_docRoot = $docRoot
|
||||||
|
? $docRoot
|
||||||
|
: $_SERVER['DOCUMENT_ROOT'];
|
||||||
|
self::$_docRoot = realpath(self::$_docRoot);
|
||||||
|
self::$_currentDir = realpath($currentDir);
|
||||||
|
|
||||||
|
// remove ws around urls
|
||||||
|
$css = preg_replace('/
|
||||||
|
url\\( # url(
|
||||||
|
\\s*
|
||||||
|
([^\\)]+?) # 1 = URI (really just a bunch of non right parenthesis)
|
||||||
|
\\s*
|
||||||
|
\\) # )
|
||||||
|
/x', 'url($1)', $css);
|
||||||
|
|
||||||
|
// rewrite
|
||||||
|
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
|
||||||
|
,array('Minify_CSS_UriRewriter', '_uriCB'), $css);
|
||||||
|
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
|
||||||
|
,array('Minify_CSS_UriRewriter', '_uriCB'), $css);
|
||||||
|
|
||||||
|
return $css;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string directory of this stylesheet
|
||||||
|
*/
|
||||||
|
protected static $_currentDir = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string DOC_ROOT
|
||||||
|
*/
|
||||||
|
protected static $_docRoot = '';
|
||||||
|
|
||||||
|
protected static function _uriCB($m)
|
||||||
|
{
|
||||||
|
$isImport = ($m[0][0] === '@');
|
||||||
|
if ($isImport) {
|
||||||
|
$quoteChar = $m[1];
|
||||||
|
$uri = $m[2];
|
||||||
|
} else {
|
||||||
|
// is url()
|
||||||
|
// $m[1] is either quoted or not
|
||||||
|
$quoteChar = ($m[1][0] === "'" || $m[1][0] === '"')
|
||||||
|
? $m[1][0]
|
||||||
|
: '';
|
||||||
|
$uri = ($quoteChar === '')
|
||||||
|
? $m[1]
|
||||||
|
: substr($m[1], 1, strlen($m[1]) - 2);
|
||||||
|
}
|
||||||
|
if ('/' !== $uri[0]) {
|
||||||
|
if (strpos($uri, '//') > 0) {
|
||||||
|
// probably starts with protocol, do not alter
|
||||||
|
} else {
|
||||||
|
// it's a file relative URI!
|
||||||
|
// prepend path with current dir separator (OS-independent)
|
||||||
|
$path = strtr(self::$_currentDir, '/', DIRECTORY_SEPARATOR)
|
||||||
|
. DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
|
||||||
|
// strip doc root
|
||||||
|
$path = substr($path, strlen(self::$_docRoot));
|
||||||
|
// fix to root-relative URI
|
||||||
|
$uri = strtr($path, DIRECTORY_SEPARATOR, '/');
|
||||||
|
$uri = str_replace('/./', '/', $uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($isImport) {
|
||||||
|
return "@import {$quoteChar}{$uri}{$quoteChar}";
|
||||||
|
} else {
|
||||||
|
return "url({$quoteChar}{$uri}{$quoteChar})";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1 +0,0 @@
|
|||||||
/* I should not be imported */
|
|
@@ -1,7 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
return; // refactoring
|
|
||||||
|
|
||||||
require_once '_inc.php';
|
require_once '_inc.php';
|
||||||
|
|
||||||
require_once 'Minify/ImportProcessor.php';
|
require_once 'Minify/ImportProcessor.php';
|
||||||
@@ -10,7 +8,7 @@ function test_Minify_ImportProcessor()
|
|||||||
{
|
{
|
||||||
global $thisDir;
|
global $thisDir;
|
||||||
|
|
||||||
$linDir = $thisDir . '/_test_files/cssLinearizer';
|
$linDir = $thisDir . '/_test_files/importProcessor';
|
||||||
|
|
||||||
$testFilesUri = substr(
|
$testFilesUri = substr(
|
||||||
realpath($thisDir . '/_test_files')
|
realpath($thisDir . '/_test_files')
|
||||||
|
Reference in New Issue
Block a user