1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-28 16:19:59 +02:00

CSS.php & CSS/UriRewriter.php : support for data URIs (Issue 80)

This commit is contained in:
Steve Clay
2009-01-25 16:22:03 +00:00
parent 039a25fc7c
commit 323ec7eb53
7 changed files with 28 additions and 8 deletions

View File

@@ -278,7 +278,9 @@ class Minify_CSS {
: substr($m[1], 1, strlen($m[1]) - 2); : substr($m[1], 1, strlen($m[1]) - 2);
} }
if ('/' !== $url[0]) { if ('/' !== $url[0]) {
if (strpos($url, '//') > 0) { if (strpos($url, '//') > 0
|| 0 === strpos($url, 'data:')
) {
// probably starts with protocol, do not alter // probably starts with protocol, do not alter
} else { } else {
// relative URI, rewrite! // relative URI, rewrite!

View File

@@ -14,6 +14,13 @@
*/ */
class Minify_CSS_UriRewriter { class Minify_CSS_UriRewriter {
/**
* Defines which class to call as part of callbacks, change this
* if you extend Minify_CSS_UriRewriter
* @var string
*/
protected static $className = 'Minify_CSS_UriRewriter';
/** /**
* Rewrite file relative URIs as root relative in CSS files * Rewrite file relative URIs as root relative in CSS files
* *
@@ -45,9 +52,9 @@ class Minify_CSS_UriRewriter {
// rewrite // rewrite
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/' $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
,array('Minify_CSS_UriRewriter', '_uriCB'), $css); ,array(self::$className, '_uriCB'), $css);
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/' $css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
,array('Minify_CSS_UriRewriter', '_uriCB'), $css); ,array(self::$className, '_uriCB'), $css);
return $css; return $css;
} }
@@ -79,7 +86,9 @@ class Minify_CSS_UriRewriter {
: substr($m[1], 1, strlen($m[1]) - 2); : substr($m[1], 1, strlen($m[1]) - 2);
} }
if ('/' !== $uri[0]) { if ('/' !== $uri[0]) {
if (strpos($uri, '//') > 0) { if (strpos($uri, '//') > 0
|| 0 === strpos($uri, 'data:')
) {
// probably starts with protocol, do not alter // probably starts with protocol, do not alter
} else { } else {
// it's a file relative URI! // it's a file relative URI!

View File

@@ -5,6 +5,8 @@
@import url(../foo.css) tv, projection; @import url(../foo.css) tv, projection;
@import url("/css/foo.css"); /* abs, should not alter */ @import url("/css/foo.css"); /* abs, should not alter */
@import url(/css2/foo.css); /* abs, should not alter */ @import url(/css2/foo.css); /* abs, should not alter */
@import url(data:image/gif;base64,AAAA); /* data, should not alter */
foo {background:url('bar/foo.png')} foo {background:url('bar/foo.png')}
foo {background:url('http://foo.com/css/foo.css');} /* abs, should not alter */ foo {background:url('http://foo.com/css/foo.css');} /* abs, should not alter */
foo {background:url("//foo.com/css/foo.css");} /* protocol relative, should not alter */ foo {background:url("//foo.com/css/foo.css");} /* protocol relative, should not alter */
foo {background:url(data:image/gif;base64,AAAA);} /* data, should not alter */

View File

@@ -1 +1 @@
@import "../foo.css";@import '../bar/foo.css' print;@import '/css/foo.css';@import 'http://foo.com/css/foo.css';@import url(../../foo.css) tv, projection;@import url("/css/foo.css");@import url(/css2/foo.css);foo{background:url('../bar/foo.png')}foo{background:url('http://foo.com/css/foo.css')}foo{background:url("//foo.com/css/foo.css")} @import "../foo.css";@import '../bar/foo.css' print;@import '/css/foo.css';@import 'http://foo.com/css/foo.css';@import url(../../foo.css) tv, projection;@import url("/css/foo.css");@import url(/css2/foo.css);@import url(data:image/gif;base64,AAAA);foo{background:url('../bar/foo.png')}foo{background:url('http://foo.com/css/foo.css')}foo{background:url("//foo.com/css/foo.css")}foo{background:url(data:image/gif;base64,AAAA)}

View File

@@ -7,6 +7,8 @@
@import url(/_test_files/foo.css) tv, projection; @import url(/_test_files/foo.css) tv, projection;
@import url("/css/foo.css"); /* abs, should not alter */ @import url("/css/foo.css"); /* abs, should not alter */
@import url(/css2/foo.css); /* abs, should not alter */ @import url(/css2/foo.css); /* abs, should not alter */
@import url(data:image/gif;base64,AAAA); /* data, should not alter */
foo {background:url('/_test_files/css_uriRewriter/bar/foo.png')} foo {background:url('/_test_files/css_uriRewriter/bar/foo.png')}
foo {background:url('http://foo.com/css/foo.css');} /* abs, should not alter */ foo {background:url('http://foo.com/css/foo.css');} /* abs, should not alter */
foo {background:url("//foo.com/css/foo.css");} /* protocol relative, should not alter */ foo {background:url("//foo.com/css/foo.css");} /* protocol relative, should not alter */
foo {background:url(data:image/gif;base64,AAAA);} /* data, should not alter */

View File

@@ -7,6 +7,8 @@
@import url(../foo.css) tv, projection; @import url(../foo.css) tv, projection;
@import url("/css/foo.css"); /* abs, should not alter */ @import url("/css/foo.css"); /* abs, should not alter */
@import url(/css2/foo.css); /* abs, should not alter */ @import url(/css2/foo.css); /* abs, should not alter */
@import url(data:image/gif;base64,AAAA); /* data, should not alter */
foo {background:url('bar/foo.png')} foo {background:url('bar/foo.png')}
foo {background:url('http://foo.com/css/foo.css');} /* abs, should not alter */ foo {background:url('http://foo.com/css/foo.css');} /* abs, should not alter */
foo {background:url("//foo.com/css/foo.css");} /* protocol relative, should not alter */ foo {background:url("//foo.com/css/foo.css");} /* protocol relative, should not alter */
foo {background:url(data:image/gif;base64,AAAA);} /* data, should not alter */

View File

@@ -25,12 +25,14 @@
/* 22 */ fixHrefs(); /* 22 */ fixHrefs();
/* 23 */ }; /* 23 */ };
/* 24 */ })(); /* 24 */ })();
; ;
/* lines_bugs.js */ /* lines_bugs.js */
/* 1 */ var triggerBug = {_default: "*/*"}; /* 1 */ var triggerBug = {_default: "*/*"};
/* 2 */ var essentialFunctionality = true; /* 2 */ var essentialFunctionality = true;
/* 3 */ /* 3 */
; ;
/* QueryString.js */ /* QueryString.js */
@@ -211,6 +213,7 @@
/* 166 */ /* 166 */
/* 167 */ return construct_; /* 167 */ return construct_;
/* 168 */ }(); // define and execute /* 168 */ }(); // define and execute
; ;
/* before.js */ /* before.js */