blib/lib/CSS/Tidy.pm

filter: Statementstmt 76.2% 90 / 118 Branchbran 65.3% 17 / 26 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 84.6% 11 / 13 Podpod 28.5% 2 / 7 totaltotal 73.9% 125 / 169 CC 18 SCAR 21.2
CC
18
Cov
75%
tidy_css
14.7
copy_css
12.0
strip_comments
3.0
CRAP 8.4
↑ Summary
1 package CSS::Tidy;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8  
9 1 our @ISA = qw(Exporter);
10 1 our @EXPORT_OK = qw/
11 copy_css
12 tidy_css
13 /;
14  
15 1 our %EXPORT_TAGS = (
16 all => \@EXPORT_OK,
17 );
18  
19 1 our $VERSION = '0.04';
20  
21 1 ▶use C::Tokenize '$comment_re';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use File::Slurper qw!read_text write_text!;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub copy_css
25 0 ▶{
Subroutine copy_css: not called CC=3 SCAR=24.8
Pod documented
26 0 my (%options) = @_;
27 0 my $in = get_option (\%options, 'in');
28 0 ▶ if (! $in) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in) 0 0 0
29 0 carp "Specify the input css file with in => 'file'";
30 0 return;
31 }
32 0 my $out = get_option (\%options, 'out');
33 0 ▶ if (! $out) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 0 0 0
34 0 carp "Specify the output css file with out => 'file'";
35 0 return;
36 }
37 0 my $css = read_text ($in);
38 0 $css = tidy_css ($css, %options);
39 0 write_text ($out, $css);
40 }
41  
42 sub get_option
43 16 ▶{
Subroutine get_option: called CC=1 SCAR=0.0
Pod undocumented
44 16 my ($o, $name) = @_;
45 16 my $value = $o->{$name};
46 16 delete $o->{$name};
47 16 return $value;
48 }
49  
50 sub check_options
51 8 ▶{
Subroutine check_options: called CC=2 SCAR=8.1
Pod undocumented
52 8 my ($o) = @_;
53 8 my @k = keys %$o;
54 8 for my $k (@k) {
55 0 carp "Unknown option $k";
56 0 delete $o->{$k};
57 }
58 }
59  
60 sub tidy_css
61 8 ▶{
Subroutine tidy_css: called CC=13 SCAR=26.9
Pod documented
62 8 my ($text, %options) = @_;
63  
64 8 my $decomment = get_option (\%options, 'decomment');
65 #my $verbose =
66 # Discard this at the moment, we have no verbose output yet.
67 8 get_option (\%options, 'verbose');
68 8 check_options (\%options);
69  
70 # Store for comments during processing. They are then restored.
71 8 my $comments;
72 8 ▶ if ($decomment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decomment) { } 0 8 8
73 0 $text = rm_comments ($text);
74 }
75 else {
76 8 ($text, $comments) = strip_comments ($text);
77 }
78 8 $text =~ s!(\{|\}|;)(\s*\S)!$1\n$2!g;
79 8 $text =~ s!(\S\s*)(\})!$1\n$2!g;
80 8 $text =~ s!(\S)(\{)!$1 $2!g;
81 8 $text =~ s!(\S*)(\})!$1\n$2!g;
82  
83 8 my @lines = split /\n/, $text;
84  
85 8 my @tidy;
86  
87 # Line number, but this could be wrong due to comment removal.
88 my $i;
89  
90 # Depth of nested { }
91 8 my $depth = 0;
92  
93 8 for (@lines) {
94 74 $i++;
95 # {} on the same line.
96  
97 # It would be better to deal with these beforehand.
98  
99 # If done before processing it will break the line numbers.
100  
101 # Need to also add then remove line number fake information.
102  
103 74 ▶ if (/^\s*(.*)\{(.*?)\}(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(.*)\{(.*?)\}(.*)$/) 0 74 74
104 0 my ($before, $between, $after) = ($1, $2, $3);
105 0 my $indent = ' ' x ($depth + 1);
106 0 push @tidy, "$indent$before {";
107 0 my @between = split /;\s*/, $between;
108 0 for my $b (@between) {
109 0 push @tidy, "$indent $b";
110 }
111 0 push @tidy, "$indent}";
112 0 push @tidy, "$indent$after";
113 0 next;
114 }
115 74 ▶ while (/\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\}/g) 10 74 84
116 10 $depth--;
117 10 ▶ if ($depth < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0) 0 10 10
118 0 warn "$i: depth = $depth\n";
119 }
120 }
121 74 my $initial = '';
122 74 ▶ if (/^(\s*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\s*)/) 74 0 74
123 74 $initial = $1;
124 }
125 74 ▶ if (length ($initial) != $depth * 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $initial != $depth * 4) 45 29 74
126 45 s/^$initial/' ' x $depth/e;
127 }
128 # If not a CSS pseudoclass or pseudoelement
129 74 ▶ if (! /(?:\.|#)\w+.*?:/ && ! /^\s*:+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/(?:\.|#)\w+.*?:/ or /^\s*:+/) 69 5 74
Condition100%3/3
/(?:\.|#)\w+.*?:/ or /^\s*:+/
l!l&&r!l&&!r
4169
MC/DC100%2/2
/(?:\.|#)\w+.*?:/ or /^\s*:+/
/(?:\.|#)\w+.*?:/ /^\s*:+/
Truth table100%3/3
A or B
A= /(?:\.|#)\w+.*?:/B= /^\s*:+/
ABresult
1X1
011
000
130 # Insert a space after a colon
131 69 s/([^:]):([^:\s])/$1: $2/;
132 }
133 74 s/\s+$//;
134 74 push @tidy, $_;
135 74 ▶ if (/\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\{/) 10 64 74
136 10 $depth++;
137 }
138 }
139  
140 8 my $out = join ("\n", @tidy);
141 # Reduce multiple blank lines to a single one.
142 8 $out =~ s/\n+/\n/g;
143 # Add a blank after }
144 8 $out =~ s/^(\s*\})/$1\n/gsm;
145  
146 8 $out =~ s/^\}\n(\S)/\}\n\n$1/gsm;
147 # Remove a blank line before }. This also tidies up the
148 # aftereffects of the above regex, which puts too many blank
149 # lines.
150 8 $out =~ s/\n\n(\s*\})/\n$1/g;
151  
152 # Add a semicolon after the final CSS instruction if there is not
153 # one.
154  
155 8 $out =~ s!([^\};])\s*(\n\s*\})!$1;$2!g;
156  
157 8 ▶ if (! $decomment) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($decomment) 8 0 8
158 8 $out = restore_comments ($out, $comments);
159 }
160 # Add a blank line after comments.
161 8 $out =~ s!(\*/)!$1\n!g;
162 8 return $out;
163 }
164  
165 # Completely remove all the comments.
166  
167 sub rm_comments
168 0 ▶{
Subroutine rm_comments: not called CC=1 SCAR=6.9
Pod undocumented
169 0 my ($text) = @_;
170 0 $text =~ s!$comment_re!!sm;
171 0 return $text;
172 }
173  
174 1 my $string_re = qr!"(\\"|[^"])*"!;
175  
176 # Strip the comments out in such a way that they can be restored.
177  
178 sub strip_comments
179 8 ▶{
Subroutine strip_comments: called CC=3 SCAR=11.0
Pod undocumented
180 8 my ($text) = @_;
181  
182 # Remove and store all strings so that "http://example.com"
183 # doesn't get turned into a comment.
184  
185 8 my @strings;
186 8 my $s = 0;
187 8 ▶ while ($text =~ s!($string_re)!\@\@ string_#$s \@\@!sm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/($string_re)/\@\@ string_#$s \@\@/ms) 1 8 9
188 1 $s++;
189 1 push @strings, $1;
190 }
191  
192 # Remove and store comments.
193  
194 8 my @comments;
195 8 my $n = 0;
196 8 ▶ while ($text =~ s!($comment_re)!/\@ css_tidy_#$n \@/!sm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s[($CSS::Tidy::comment_re)][/\@ css_tidy_#$n \@/]ms) 1 8 9
197 1 $n++;
198 1 push @comments, $1;
199 }
200  
201 # Restore the strings.
202  
203 8 $text =~ s!\@\@ string_#([0-9]+) \@\@!$strings[$1]!g;
204  
205 8 return ($text, \@comments);
206 }
207  
208 sub restore_comments
209 8 ▶{
Subroutine restore_comments: called CC=1 SCAR=0.0
Pod undocumented
210 8 my ($text, $comments) = @_;
211 8 $text =~ s!/\@ css_tidy_#([0-9]+) \@/!$comments->[$1]!g;
212 8 return $text;
213 }
214  
215 1 1;
↑ Summary