mirror of
https://github.com/mrclay/minify.git
synced 2025-08-20 12:51:52 +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:
@@ -99,20 +99,20 @@ class Minify_CSS {
|
||||
{
|
||||
// preserve empty comment after '>'
|
||||
// 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
|
||||
// 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
|
||||
self::$_inHack = false;
|
||||
$css = preg_replace_callback('/\\s*\\/\\*([\\s\\S]*?)\\*\\/\\s*/'
|
||||
$css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
|
||||
,array('Minify_CSS', '_commentCB'), $css);
|
||||
|
||||
// compress whitespace.
|
||||
$css = preg_replace('/\s+/', ' ', $css);
|
||||
$css = preg_replace('/\\s+/', ' ', $css);
|
||||
|
||||
// leave needed comments
|
||||
$css = str_replace('/*keep*/', '/**/', $css);
|
||||
@@ -128,7 +128,7 @@ class Minify_CSS {
|
||||
$css = preg_replace('/url\\([\\s]*([^\\)]+?)[\\s]*\\)/', 'url($1)', $css);
|
||||
|
||||
// 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
|
||||
$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'
|
||||
, '$1#$2$3$4$5', $css);
|
||||
|
||||
// remove spaces between font families
|
||||
$css = preg_replace_callback('/font-family:([^;}]+)([;}])/'
|
||||
,array('Minify_CSS', '_fontFamilyCB'), $css);
|
||||
|
||||
$rewrite = false;
|
||||
if (isset($options['prependRelativePath'])) {
|
||||
self::$_tempPrepend = $options['prependRelativePath'];
|
||||
@@ -190,7 +194,7 @@ class Minify_CSS {
|
||||
}
|
||||
if (self::$_inHack) {
|
||||
// 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;
|
||||
return "/*/{$n[1]}/*keep*/";
|
||||
}
|
||||
@@ -263,5 +267,17 @@ class Minify_CSS {
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
42
web/test/_test_files/css/selectors.css
Normal file
42
web/test/_test_files/css/selectors.css
Normal 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;}
|
1
web/test/_test_files/css/selectors.min.css
vendored
Normal file
1
web/test/_test_files/css/selectors.min.css
vendored
Normal 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}
|
@@ -15,7 +15,16 @@
|
||||
background-color: red;
|
||||
border: 1px solid #00ff77;
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
2
web/test/_test_files/css/styles.min.css
vendored
2
web/test/_test_files/css/styles.min.css
vendored
@@ -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}}
|
Reference in New Issue
Block a user