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 Pass 3: Pruning
58 We then go back through the token list and prune and remove un-
59 necessary tokens.
60
61 Whitespace
62 Wherever possible, whitespace is removed; before+after comment
63 blocks, and before+after various symbols/sigils.
64
65 Comments
66 Comments that either (a) are needed to implement the "Mac/IE
67 Comment Hack", or that (b) contain the word "copyright" in them are
68 preserved. All other comments are removed.
69
70 Symbols/Sigils
71 Semi-colons that are immediately followed by a closing brace (e.g.
72 ";}") are removed; semi-colons are needed to separate multiple
73 declarations, but aren't required at the end of a group.
74
75 Everything else
76 We keep everything else; identifiers, quoted literal strings,
77 symbols/sigils, etc.
78
79 Pass 4: Re-assembly
80 Lastly, we go back through the token list and re-assemble it all back
81 into a single CSS string, which is then returned back to the caller.
82
84 Graham TerMarsch (cpan@howlingfrog.com)
85
87 Please report bugs via RT
88 (<http://rt.cpan.org/Dist/Display.html?Queue=CSS::Minifier::XS>), and
89 be sure to include the CSS that you're having troubles minifying.
90
92 Copyright (C) 2007-2010, Graham TerMarsch. All Rights Reserved.
93
94 This is free software; you can redistribute it and/or modify it under
95 the same license as Perl itself.
96
98 "CSS::Minifier".
99
100
101
102perl v5.12.1 2010-07-22 CSS::Minifier::XS(3)