1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-20 21:02:30 +02:00

Minify_CSS: removing more ws (in font-family lists and before values starting with [#'"]) + tests with media queries and CSS3 selectors.

This commit is contained in:
Steve Clay
2008-07-29 19:50:38 +00:00
parent 0697a120ee
commit 6ce0e380f0
8 changed files with 81 additions and 13 deletions

View File

@@ -99,20 +99,20 @@ class Minify_CSS {
{ {
// preserve empty comment after '>' // preserve empty comment after '>'
// http://www.webdevout.net/css-hacks#in_css-selectors // http://www.webdevout.net/css-hacks#in_css-selectors
$css = preg_replace('/>\\/\\*\\s*\\*\\//', '>/*keep*/', $css); $css = preg_replace('@>/\\*\\s*\\*/@', '>/*keep*/', $css);
// preserve empty comment between property and value // preserve empty comment between property and value
// http://css-discuss.incutio.com/?page=BoxModelHack // http://css-discuss.incutio.com/?page=BoxModelHack
$css = preg_replace('/\\/\\*\\s*\\*\\/\\s*:/', '/*keep*/:', $css); $css = preg_replace('@/\\*\\s*\\*/\\s*:@', '/*keep*/:', $css);
$css = preg_replace('/:\\s*\\/\\*\\s*\\*\\//', ':/*keep*/', $css); $css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css);
// apply callback to all valid comments (and strip out surrounding ws // apply callback to all valid comments (and strip out surrounding ws
self::$_inHack = false; self::$_inHack = false;
$css = preg_replace_callback('/\\s*\\/\\*([\\s\\S]*?)\\*\\/\\s*/' $css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
,array('Minify_CSS', '_commentCB'), $css); ,array('Minify_CSS', '_commentCB'), $css);
// compress whitespace. // compress whitespace.
$css = preg_replace('/\s+/', ' ', $css); $css = preg_replace('/\\s+/', ' ', $css);
// leave needed comments // leave needed comments
$css = str_replace('/*keep*/', '/**/', $css); $css = str_replace('/*keep*/', '/**/', $css);
@@ -128,7 +128,7 @@ class Minify_CSS {
$css = preg_replace('/url\\([\\s]*([^\\)]+?)[\\s]*\\)/', 'url($1)', $css); $css = preg_replace('/url\\([\\s]*([^\\)]+?)[\\s]*\\)/', 'url($1)', $css);
// remove ws between rules and colons // remove ws between rules and colons
$css = preg_replace('/\\s*([{;])\\s*([\\w\\-]+)\\s*:\\s*\\b/', '$1$2:', $css); $css = preg_replace('/\\s*([{;])\\s*([\\w\\-]+)\\s*:\\s*(\\b|[#\'"])/', '$1$2:$3', $css);
// remove ws in selectors // remove ws in selectors
$css = preg_replace_callback('/(?:\\s*[^~>+,\\s]+\\s*[,>+~])+\\s*[^~>+,\\s]+{/' $css = preg_replace_callback('/(?:\\s*[^~>+,\\s]+\\s*[,>+~])+\\s*[^~>+,\\s]+{/'
@@ -138,6 +138,10 @@ class Minify_CSS {
$css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i' $css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i'
, '$1#$2$3$4$5', $css); , '$1#$2$3$4$5', $css);
// remove spaces between font families
$css = preg_replace_callback('/font-family:([^;}]+)([;}])/'
,array('Minify_CSS', '_fontFamilyCB'), $css);
$rewrite = false; $rewrite = false;
if (isset($options['prependRelativePath'])) { if (isset($options['prependRelativePath'])) {
self::$_tempPrepend = $options['prependRelativePath']; self::$_tempPrepend = $options['prependRelativePath'];
@@ -190,7 +194,7 @@ class Minify_CSS {
} }
if (self::$_inHack) { if (self::$_inHack) {
// inversion: feeding only to one browser // inversion: feeding only to one browser
if (preg_match('/^\\/\\s*(\\S[\\s\\S]+?)\\s*\\/\\*/', $m, $n)) { if (preg_match('@^/\\s*(\\S[\\s\\S]+?)\\s*/\\*@', $m, $n)) {
self::$_inHack = false; self::$_inHack = false;
return "/*/{$n[1]}/*keep*/"; return "/*/{$n[1]}/*keep*/";
} }
@@ -263,5 +267,17 @@ class Minify_CSS {
return "url({$quote}{$url}{$quote})"; return "url({$quote}{$url}{$quote})";
} }
} }
}
/**
* Process a font-family listing and return a replacement
*
* @param array $m regex matches
*
* @return string
*/
protected static function _fontFamilyCB($m)
{
$m[1] = preg_replace('/\\s*("[^"]+"|\'[^\']+\'|[\\w\\-]+)\\s*/', '$1', $m[1]);
return 'font-family:' . $m[1] . $m[2];
}
}

View File

@@ -0,0 +1,42 @@
/* http://www.w3.org/TR/css3-selectors/ */
*
E[foo]
E[foo="bar"]
E[foo~="bar"]
E[foo^="bar"]
E[foo$="bar"]
E[foo*="bar"]
E[hreflang|="en"]
E:root
E:nth-child(n)
E:nth-last-child(n)
E:nth-of-type(n)
E:nth-last-of-type(n)
E:first-child
E:last-child
E:first-of-type
E:last-of-type
E:only-child
E:only-of-type
E:empty
E:link
E:visited
E:active
E:hover
E:focus
E:target
E:lang(fr)
E:enabled
E:disabled
E:checked
E::first-line
E::first-letter
E::selection
E::before
E::after
E.warning#myid
E:not(s)
> F
+ F
~ F {color: red;}

View File

@@ -0,0 +1 @@
* E[foo] E[foo="bar"] E[foo~="bar"] E[foo^="bar"] E[foo$="bar"] E[foo*="bar"] E[hreflang|="en"] E:root E:nth-child(n) E:nth-last-child(n) E:nth-of-type(n) E:nth-last-of-type(n) E:first-child E:last-child E:first-of-type E:last-of-type E:only-child E:only-of-type E:empty E:link E:visited E:active E:hover E:focus E:target E:lang(fr) E:enabled E:disabled E:checked E::first-line E::first-letter E::selection E::before E::after E.warning#myid E:not(s)>F+F~F{color:red}

View File

@@ -15,7 +15,16 @@
background-color: red; background-color: red;
border: 1px solid #00ff77; border: 1px solid #00ff77;
} }
div#content h1 + p { div#content h1 + p {
padding-top: 0; padding-top: 0;
margin-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; }
}

View File

@@ -1 +1 @@
@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 solid #0f7}div#content h1+p{padding-top:0;margin-top:0} @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 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}}