1CSS::Minifier::XS(3) User Contributed Perl Documentation CSS::Minifier::XS(3)
2
3
4
6 CSS::Minifier::XS - XS based CSS minifier
7
9 use CSS::Minifier::XS qw(minify);
10 $minified = minify($css);
11
13 "CSS::Minifier::XS" is a CSS "minifier"; its designed to remove un-
14 necessary 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
21 minify($css)
22 Minifies the given $css, returning the minified CSS back to the
23 caller.
24
26 "CSS::Minifier::XS" minifies the CSS by removing un-necessary
27 whitespace from CSS documents. Comment blocks are also removed, except
28 when (a) they contain the word "copyright" in them, or (b) they're
29 needed to implement the "Mac/IE Comment Hack".
30
31 Internally, the minification is done by taking multiple passes through
32 the CSS document:
33
34 Pass 1: Tokenize
35 First, we go through and parse the CSS document into a series of tokens
36 internally. The tokenizing process does not check to make sure that
37 you've got syntactically valid CSS, it just breaks up the text into a
38 stream of tokens suitable for processing by the subsequent stages.
39
40 Pass 2: Collapse
41 We then march through the token list and collapse certain tokens down
42 to their smallest possible representation. If they're still included
43 in the final results we only want to include them at their shortest.
44
45 Whitespace
46 Runs of multiple whitespace characters are reduced down to a single
47 whitespace character. If the whitespace contains any "end of line"
48 (EOL) characters, then the end result is the first EOL character
49 encountered. Otherwise, the result is the first whitespace
50 character in the run.
51
52 Comments
53 Comments implementing the "Mac/IE Comment Hack" are collapsed down
54 to the smallest possible comment that would still implement the
55 hack ("/*\*/" to start the hack, and "/**/" to end it).
56
57 Zero Units
58 Zero Units (e.g. "0px") are reduced down to just "0", as the CSS
59 specification indicates that the unit is not required when its a
60 zero value.
61
62 Pass 3: Pruning
63 We then go back through the token list and prune and remove un-
64 necessary tokens.
65
66 Whitespace
67 Wherever possible, whitespace is removed; before+after comment
68 blocks, and before+after various symbols/sigils.
69
70 Comments
71 Comments that either (a) are needed to implement the "Mac/IE
72 Comment Hack", or that (b) contain the word "copyright" in them are
73 preserved. All other comments are removed.
74
75 Symbols/Sigils
76 Semi-colons that are immediately followed by a closing brace (e.g.
77 ";}") are removed; semi-colons are needed to separate multiple
78 declarations, but aren't required at the end of a group.
79
80 Everything else
81 We keep everything else; identifiers, quoted literal strings,
82 symbols/sigils, etc.
83
84 Pass 4: Re-assembly
85 Lastly, we go back through the token list and re-assemble it all back
86 into a single CSS string, which is then returned back to the caller.
87
89 Graham TerMarsch (cpan@howlingfrog.com)
90
92 Please report bugs via RT
93 (<http://rt.cpan.org/Dist/Display.html?Queue=CSS::Minifier::XS>), and
94 be sure to include the CSS that you're having troubles minifying.
95
97 Copyright (C) 2007-, Graham TerMarsch. All Rights Reserved.
98
99 This is free software; you can redistribute it and/or modify it under
100 the same license as Perl itself.
101
103 "CSS::Minifier".
104
105
106
107perl v5.28.1 2013-10-31 CSS::Minifier::XS(3)