The previous parts are here (building and testing) and here (what gets minified). Now let's see how YUI CSS min handles CSS hacks.
As you know CSS hacks often use errors in CSS parsers in browsers to target specific browser versions and supply additional rules to work around other issues in said browsers. That makes any CSS tool's job slightly more challenging. Not only does the tool have to avoid repeating the browsers errors, but also has to understand what browsers got wrong and support it too. Fun stuff. Isn't it a joy being a web developer?
So here are some hacks that are tested to work with the YUICopmpressor's CSS min.
Underscore/star hack
The simplest ever hack to target IE6 and IE7. In the example below normal browsers see 1px dropping _width
and *width
as invalid, IE7 ignores the *, drops the _width
as invalid and sees 3pt, IE6 ignores the _ and sees _width
as width
, so it sees 2em.
CSS min doesn't parse and doesn't understand CSS properties, so it accepts pretty much any property.
Before:
#element { width: 1px; *width: 3pt; _width: 2em; }
After:
#element{width:1px;*width:3pt;_width:2em}
Child selector hack
CSS min strips comments but there is this child selector hack people use to hide declarations from IE7 and below.
CSS min retains empty comments that immediately follow >
(thanks go out to Chris Burroughs)
Before:
html >/**/ body p { color: blue; }
After:
html>/**/body p{color:blue}
IE5/Mac hack
This hack targets IE5/Mac, if anyone still worries about this browser. The hack is retained after minification, only it's minified.
Before:
/* Ignore the next rule in IE mac \*/ .selector { color: khaki; } /* Stop ignoring in IE mac */
After:
/*\*/.selector{color:khaki}/**/
Box model hack
This hack uses valid CSS and there's no special use of comments so it's retained.
Before:
#elem { width: 100px; /* IE */ voice-family: "\"}\""; voice-family:inherit; width: 200px; /* others */ } html>body #elem { width: 200px; /* others */ }
After:
#elem{width:100px;voice-family:"\"}\"";voice-family:inherit;width:200px}html>body #elem{width:200px}
Seems like the code highlighter chokes here though. It ain't easy 🙂
That's all, folks!
Thanks and please, feel free to suggest improvements and report bugs. Also play with the web UI of the JS-version here to see for yourself what it does to your code.