Permalink
Browse files

revert Changes

  • Loading branch information...
1 parent faa3442 commit 44e325803bfa4c2bdc34f47c00179d0d3cb54d65 @acidvertigo committed Dec 7, 2011
Showing with 78 additions and 45 deletions.
  1. +78 −45 min/lib/Minify/ImportProcessor.php
@@ -1,46 +1,46 @@
<?php
/**
- * Class Minify_ImportProcessor
+ * Class Minify_ImportProcessor
* @package Minify
*/
/**
* 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
+ *
+ * @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!
- *
+ *
* This has a unit test but should be considered "experimental".
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author Simon Schick <simonsimcity@gmail.com>
*/
class Minify_ImportProcessor {
-
+
public static $filesIncluded = array();
-
+
public static function process($file)
{
self::$filesIncluded = array();
self::$_isCss = (strtolower(substr($file, -4)) === '.css');
$obj = new Minify_ImportProcessor(dirname($file));
return $obj->_getContent($file);
}
-
+
// allows callback funcs to know the current directory
private $_currentDir = null;
-
+
// allows callback funcs to know the directory of the file that inherits this one
private $_previewsDir = null;
-
+
// allows _importCB to write the fetched content back to the obj
private $_importedContent = '';
-
+
private static $_isCss = null;
-
+
/**
* @param String $currentDir
* @param String $previewsDir Is only used internally
@@ -50,7 +50,7 @@ private function __construct($currentDir, $previewsDir = "")
$this->_currentDir = $currentDir;
$this->_previewsDir = $previewsDir;
}
-
+
private function _getContent($file, $is_imported = false)
{
$file = realpath($file);
@@ -63,14 +63,14 @@ private function _getContent($file, $is_imported = false)
}
self::$filesIncluded[] = realpath($file);
$this->_currentDir = dirname($file);
-
+
// remove UTF-8 BOM if present
if (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) {
$content = substr($content, 3);
}
// ensure uniform EOLs
$content = str_replace("\r\n", "\n", $content);
-
+
// process @imports
$content = preg_replace_callback(
'/
@@ -86,7 +86,7 @@ private function _getContent($file, $is_imported = false)
,array($this, '_importCB')
,$content
);
-
+
// You only need to rework the import-path if the script is imported
if (self::$_isCss && $is_imported) {
// rewrite remaining relative URIs
@@ -96,15 +96,15 @@ private function _getContent($file, $is_imported = false)
,$content
);
}
-
+
return $this->_importedContent . $content;
}
-
+
private function _importCB($m)
{
$url = $m[1];
$mediaList = preg_replace('/\\s+/', '', $m[2]);
-
+
if (strpos($url, '://') > 0) {
// protocol, leave in place for CSS, comment for JS
return self::$_isCss
@@ -118,10 +118,10 @@ private function _importCB($m)
. strtr($url, '/', DIRECTORY_SEPARATOR);
} else {
// relative to current path
- $file = $this->_currentDir . DIRECTORY_SEPARATOR
+ $file = $this->_currentDir . DIRECTORY_SEPARATOR
. strtr($url, '/', DIRECTORY_SEPARATOR);
}
- $obj = new Minify_ImportProcessor(dirname($file), $this->__currentDir);
+ $obj = new Minify_ImportProcessor(dirname($file), $this->_currentDir);
$content = $obj->_getContent($file, true);
if ('' === $content) {
// failed. leave in place for CSS, comment for JS
@@ -133,7 +133,7 @@ private function _importCB($m)
? $content
: "@media {$mediaList} {\n{$content}\n}\n";
}
-
+
private function _urlCB($m)
{
// $m[1] is either quoted or not
@@ -148,36 +148,69 @@ private function _urlCB($m)
// probably starts with protocol, do not alter
} else {
// prepend path with current dir separator (OS-independent)
- $path = $this->_currentDir
+ $path = $this->_currentDir
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
-<<<<<<< HEAD
// update the relative path by the directory of the file that imported this one
$url = self::getPathDiff(realpath($this->_previewsDir), $path);
-=======
- // strip doc root
- $path = substr($path, strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
- // fix to absolute URL
- $url = strtr($path, '/\\', '//');
- // remove /./ and /../ where possible
- $url = str_replace('/./', '/', $url);
- // inspired by patch from Oleg Cherniy
- do {
- $url = preg_replace('@/(?!\\.\\.?)[^/]+/\\.\\.@', '/', $url, 1, $changed);
- } while ($changed);
->>>>>>> origin/patch-1
}
}
return "url({$quote}{$url}{$quote})";
}
- private function getPathDiff($from, $to, $ps = '/') {
- $arFrom = explode($ps, rtrim($from, $ps));
- $arTo = explode($ps, rtrim($to, $ps));
- while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
- {
- array_shift($arFrom);
- array_shift($arTo);
- }
- return str_pad("", count($arFrom) * 3, '..'.$ps).implode($ps, $arTo);
- }
+ /**
+ * @param string $from
+ * @param string $to
+ * @param string $ps
+ * @return string
+ */
+ private function getPathDiff($from, $to, $ps = DIRECTORY_SEPARATOR)
+ {
+ $realFrom = $this->truepath($from);
+ $realTo = $this->truepath($to);
+
+ $arFrom = explode($ps, rtrim($realFrom, $ps));
+ $arTo = explode($ps, rtrim($realTo, $ps));
+ while (count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
+ {
+ array_shift($arFrom);
+ array_shift($arTo);
+ }
+ return str_pad("", count($arFrom) * 3, '..' . $ps) . implode($ps, $arTo);
+ }
+
+ /**
+ * This function is to replace PHP's extremely buggy realpath().
+ * @param string $path The original path, can be relative etc.
+ * @return string The resolved path, it might not exist.
+ * @see http://stackoverflow.com/questions/4049856/replace-phps-realpath
+ */
+ function truepath($path)
+ {
+ // whether $path is unix or not
+ $unipath = strlen($path) == 0 || $path{0} != '/';
+ // attempts to detect if path is relative in which case, add cwd
+ if (strpos($path, ':') === false && $unipath)
+ $path = $this->_currentDir . DIRECTORY_SEPARATOR . $path;
+
+ // resolve path parts (single dot, double dot and double delimiters)
+ $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
+ $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
+ $absolutes = array();
+ foreach ($parts as $part) {
+ if ('.' == $part)
+ continue;
+ if ('..' == $part) {
+ array_pop($absolutes);
+ } else {
+ $absolutes[] = $part;
+ }
+ }
+ $path = implode(DIRECTORY_SEPARATOR, $absolutes);
+ // resolve any symlinks
+ if (file_exists($path) && linkinfo($path) > 0)
+ $path = readlink($path);
+ // put initial separator that could have been lost
+ $path = !$unipath ? '/' . $path : $path;
+ return $path;
+ }
}

0 comments on commit 44e3258

Please sign in to comment.