| 1 |
NAME |
| 2 |
|
| 3 |
CSS::Minifier::XS - XS based CSS minifier |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
use CSS::Minifier::XS qw(minify); |
| 8 |
my $css = '...'; |
| 9 |
my $minified = minify($css); |
| 10 |
|
| 11 |
DESCRIPTION |
| 12 |
|
| 13 |
CSS::Minifier::XS is a CSS "minifier"; its designed to remove |
| 14 |
unnecessary whitespace and comments from CSS files, while also not |
| 15 |
breaking the CSS. |
| 16 |
|
| 17 |
CSS::Minifier::XS is similar in function to CSS::Minifier, but is |
| 18 |
substantially faster as its written in XS and not just pure Perl. |
| 19 |
|
| 20 |
METHODS |
| 21 |
|
| 22 |
minify($css) |
| 23 |
|
| 24 |
Minifies the given $css, returning the minified CSS back to the |
| 25 |
caller. |
| 26 |
|
| 27 |
HOW IT WORKS |
| 28 |
|
| 29 |
CSS::Minifier::XS minifies the CSS by removing unnecessary whitespace |
| 30 |
from CSS documents. Comment blocks are also removed, except when (a) |
| 31 |
they contain the word "copyright" in them, or (b) they're needed to |
| 32 |
implement the "Mac/IE Comment Hack". |
| 33 |
|
| 34 |
Internally, the minification is done by taking multiple passes through |
| 35 |
the CSS document: |
| 36 |
|
| 37 |
Pass 1: Tokenize |
| 38 |
|
| 39 |
First, we go through and parse the CSS document into a series of tokens |
| 40 |
internally. The tokenizing process does not check to make sure that |
| 41 |
you've got syntactically valid CSS, it just breaks up the text into a |
| 42 |
stream of tokens suitable for processing by the subsequent stages. |
| 43 |
|
| 44 |
Pass 2: Collapse |
| 45 |
|
| 46 |
We then march through the token list and collapse certain tokens down |
| 47 |
to their smallest possible representation. If they're still included in |
| 48 |
the final results we only want to include them at their shortest. |
| 49 |
|
| 50 |
Whitespace |
| 51 |
|
| 52 |
Runs of multiple whitespace characters are reduced down to a single |
| 53 |
whitespace character. If the whitespace contains any "end of line" |
| 54 |
(EOL) characters, then the end result is the first EOL character |
| 55 |
encountered. Otherwise, the result is the first whitespace character |
| 56 |
in the run. |
| 57 |
|
| 58 |
Comments |
| 59 |
|
| 60 |
Comments implementing the "Mac/IE Comment Hack" are collapsed down to |
| 61 |
the smallest possible comment that would still implement the hack |
| 62 |
("/*\*/" to start the hack, and "/**/" to end it). |
| 63 |
|
| 64 |
Zero Units |
| 65 |
|
| 66 |
Zero Units (e.g. 0px) are reduced down to just "0", as the CSS |
| 67 |
specification indicates that the unit is not required when its a zero |
| 68 |
value. |
| 69 |
|
| 70 |
Pass 3: Pruning |
| 71 |
|
| 72 |
We then go back through the token list and prune and remove unnecessary |
| 73 |
tokens. |
| 74 |
|
| 75 |
Whitespace |
| 76 |
|
| 77 |
Wherever possible, whitespace is removed; before+after comment |
| 78 |
blocks, and before+after various symbols/sigils. |
| 79 |
|
| 80 |
Comments |
| 81 |
|
| 82 |
Comments that either (a) are needed to implement the "Mac/IE Comment |
| 83 |
Hack", or that (b) contain the word "copyright" in them are |
| 84 |
preserved. All other comments are removed. |
| 85 |
|
| 86 |
Symbols/Sigils |
| 87 |
|
| 88 |
Semi-colons that are immediately followed by a closing brace (e.g. |
| 89 |
";}") are removed; semi-colons are needed to separate multiple |
| 90 |
declarations, but aren't required at the end of a group. |
| 91 |
|
| 92 |
Everything else |
| 93 |
|
| 94 |
We keep everything else; identifiers, quoted literal strings, |
| 95 |
symbols/sigils, etc. |
| 96 |
|
| 97 |
Pass 4: Re-assembly |
| 98 |
|
| 99 |
Lastly, we go back through the token list and re-assemble it all back |
| 100 |
into a single CSS string, which is then returned back to the caller. |
| 101 |
|
| 102 |
AUTHOR |
| 103 |
|
| 104 |
Graham TerMarsch ([email protected]) |
| 105 |
|
| 106 |
COPYRIGHT |
| 107 |
|
| 108 |
Copyright (C) 2007-, Graham TerMarsch. All Rights Reserved. |
| 109 |
|
| 110 |
This is free software; you can redistribute it and/or modify it under |
| 111 |
the same license as Perl itself. |
| 112 |
|
| 113 |
SEE ALSO |
| 114 |
|
| 115 |
CSS::Minifier. |
| 116 |
|