blib/lib/CSS/Minifier/XS.pm

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