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