blib/lib/HTML/MobileJpCSS.pm

filter: Statementstmt 81.2% 113 / 139 Branchbran 65.6% 42 / 64 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.8% 13 / 14 Podpod 100% 2 / 2 totaltotal 75.1% 172 / 229 CC 45 SCAR 29.9
CC
45
Cov
75%
apply
35.7
_fetch
20.0
_read
7.2
CRAP 20.0
↑ Summary
1 package HTML::MobileJpCSS;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use CSS::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use HTTP::MobileAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '0.02';
10  
11 7 our %IstyleDoCoMo = (
12 '1' => '-wap-input-format:"*<ja:h>&;',
13 '2' => '-wap-input-format:"*<ja:hk>"',
14 '3' => '-wap-input-format:"*<ja:en>"',
15 '4' => '-wap-input-format:"*<ja:n>"',
16 );
17  
18 7 our $StyleMap = {
19 'hr' => {
20 'text-align' => {
21 'I' => 'float', 'S' => 'float',
22 },
23 'color' => {
24 'I' => 'border-color', 'S' => 'border-color',
25 },
26 },
27 'div' => {
28 'font-size' => [{
29 '10px' => { 'I' => 'font-size:xx-small', 'S' => 'font-size:small', },
30 '16px' => { 'I' => 'font-size:xx-large', 'S' => 'font-size:xx-large',},
31 }],
32 },
33 'span' => {
34 'font-size' => [{
35 '10px' => { 'I' => 'font-size:xx-small', 'S' => 'font-size:small', },
36 '16px' => { 'I' => 'font-size:xx-large', 'S' => 'font-size:xx-large', },
37 }],
38 },
39 'img' => {
40 'text-align' => [{
41 'left' => { 'I' => 'float:left', 'S' => 'float:left', },
42 'right' => { 'I' => 'float:right', 'S' => 'float:right', },
43 'center' => { 'I' => 'float:none', 'S' => 'float:none', },
44 }],
45 },
46 };
47  
48 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
49 6 my ($class, %args) = @_;
50 6 my $self = bless {%args}, $class;
51 6 $self->_init;
52 6 $self;
53 }
54  
55 6 ▶sub apply {
Subroutine apply: called CC=27 SCAR=35.7
Pod documented
56 6 my ($self, $content) = @_;
57 6 ▶ return $content if $self->{agent}->is_non_mobile;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"agent"}->is_non_mobile 0 6 6
58 6 ▶ return $content if $self->{agent}->is_ezweb && !(map { $self->{$_} } qw/inliner_ezweb css_file css/);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"agent"}->is_ezweb and not map({$self->{$_};} "inliner_ezweb", "css_file", "css") 0 6 6
Condition66%2/3
$self->{"agent"}->is_ezweb and not map({$self->{$_};} "inliner_ezweb", "css_file", "css")
!ll&&!rl&&r
510
MC/DC0%0/2
$self->{"agent"}->is_ezweb and not map({$self->{$_};} "inliner_ezweb", "css_file", "css")
$self->{"agent"}->is_ezweb not map({$self->{$_};} "inliner_ezweb", "css_file", "css")
Truth table66%2/3
A and not(B)
A= $self->{"agent"}->is_ezwebB= not map({$self->{$_};} "inliner_ezweb", "css_file", "css")
ABresult
0X0
100
111
59  
60 6 $content =~ s/(?:\r\n|\n)/\n/g;
61  
62 6 my @css;
63 6 my @link = $content =~ /<link\s.*?rel="stylesheet".*?>/isg;
64 6 for (@link) {
65 4 ▶ if (/href="(.+?)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/href="(.+?)"/) 4 0 4
66 4 my $css = $self->_read_href($1);
67 4 push @css, $css;
68 }
69 }
70 6 ▶ $content =~ s/<link\s.*?rel="stylesheet".*?>\s*//isg if @link;
Branch: 1 branch
BranchTrueFalseTotal
if @link 4 2 6
71  
72 6 ▶ push @css, $self->_read($self->{css_file}) if $self->{css_file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"css_file"} 2 4 6
73 6 ▶ push @css, bless $self->{css}, 'CSS::Tiny' if $self->{css};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"css"} 1 5 6
74  
75 6 my $style = {};
76 6 for my $css (@css) {
77 7 for (keys %$css) {
78 43 ▶ if (/^a:(?:link|focus|visited)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^a:(?:link|focus|visited)$/) { } 6 37 43
elsif (/^(\#(.+))/) { } 0 37 37
elsif (/^(\.(.+))/) { } 31 6 37
79 $style->{pseudo}->{$_} = $style->{pseudo}->{$_}
80 0 ? { %{$style->{pseudo}->{$_}}, %{$css->{$_}} }
81 6 ▶ : $css->{$_};
Branch: 1 branch
BranchTrueFalseTotal
$style->{'pseudo'}{$_} ? : 0 6 6
82 }
83 elsif (/^(\#(.+))/) {
84 $style->{id}->{$2} = $style->{id}->{$2}
85 0 ? { %{$style->{id}->{$2}}, %{$css->{$1}} }
86 0 ▶ : $css->{$1};
Branch: 1 branch
BranchTrueFalseTotal
$style->{'id'}{$2} ? : 0 0 0
87 }
88 elsif (/^(\.(.+))/) {
89 $style->{class}->{$2} = $style->{class}->{$2}
90 1 ? { %{$style->{class}->{$2}}, %{$css->{$1}} }
91 31 ▶ : $css->{$1};
Branch: 1 branch
BranchTrueFalseTotal
$style->{'class'}{$2} ? : 1 30 31
92 }
93 else {
94 $style->{tag}->{$_} = $style->{tag}->{$_}
95 0 ? { %{$style->{tag}->{$_}}, %{$css->{$_}} }
96 6 ▶ : $css->{$_};
Branch: 1 branch
BranchTrueFalseTotal
$style->{'tag'}{$_} ? : 0 6 6
97 }
98 }
99 }
100  
101 # pseudo
102 6 ▶ if ($style->{pseudo}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style->{"pseudo"}) 6 0 6
103 6 my $css = bless $style->{pseudo}, 'CSS::Tiny';
104 6 ▶ my $pseudo = $self->{agent}->is_docomo ? "<![CDATA[\n".$css->write_string."]]>" : $css->write_string;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'agent'}->is_docomo ? : 4 2 6
105 6 $content =~ s{<head>(.*)</head>}{<head>$1<style type="text/css">\n$pseudo</style></head>}is;
106 }
107  
108 # tag
109 6 for my $tag (keys %{$style->{tag}}) {
110 6 my $props = $style->{tag}->{$tag};
111 6 my @node = $content =~ /<$tag[^<>]*?>/isg;
112 6 for my $node (@node) {
113 6 $content = $self->_replace_style($content, $node, $props);
114 }
115 }
116  
117 # id
118 6 for my $id (keys %{$style->{id}}) {
119 0 my $props = $style->{id}->{$id};
120 0 my @node = $content =~ /<[^<>]+?id="$id"[^<>]*?>/isg;
121 0 for my $node (@node) {
122 0 $content = $self->_replace_style($content, $node, $props);
123 }
124 }
125  
126 # class
127 6 for my $class (keys %{$style->{class}}) {
128 30 my $props = $style->{class}->{$class};
129 30 my @node = $content =~ /<[^<>]+?\sclass="$class"[^<>]*?>/isg;
130 30 for my $node (@node) {
131 25 $content = $self->_replace_style($content, $node, $props);
132 }
133 30 $content =~ s/<([^<>]+?)\sclass="$class"([^<>]*?)>/<$1$2>/isg;
134 }
135  
136 # istyle for DoCoMo
137 6 ▶ if ($self->{agent}->is_docomo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"agent"}->is_docomo) 4 2 6
138 4 $content =~ s/(<input[^>]*?)(istyle="(\d)")([^>]*?>)/$1style="$IstyleDoCoMo{$3}"$4/isg;
139 4 $content =~ s/(<textarea[^>]*?)(istyle="(\d)")([^>]*?>)/$1style="$IstyleDoCoMo{$3}"$4/isg;
140 }
141 6 return $content;
142 }
143  
144 6 ▶sub _init {
Subroutine _init: called CC=3 SCAR=12.5
145 6 my $self = shift;
146 6 ▶ if ($self->{agent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'agent'}) { } 6 0 6
147 6 ▶ $self->{agent} = HTTP::MobileAgent->new($self->{agent}) unless (ref $self->{agent}) =~ /^HTTP::MobileAgent/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($self->{"agent"}) =~ /^HTTP::MobileAgent/ 0 6 6
148 }
149 else {
150 0 $self->{agent} = HTTP::MobileAgent->new();
151 }
152 }
153  
154 7 my %CSS;
155 4 ▶sub _read_href {
Subroutine _read_href: called CC=2 SCAR=7.2
156 4 my ($self, $href) = @_;
157 4 ▶ if ($href !~ m{^https?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $href =~ m[^https?://]) { } 4 0 4
158 4 $href =~ s/\?.+$//;
159 4 $href = File::Spec->catdir($self->{base_dir}, $href);
160 4 return $self->_read($href);
161 }
162 else {
163 0 return $self->_fetch($href);
164 }
165 }
166  
167 6 ▶sub _read {
Subroutine _read: called CC=5 SCAR=19.7
168 6 my ($self, $path) = @_;
169 6 my $css;
170 6 ▶ if ($css = $CSS{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($css = $CSS{$path}) 0 6 6
171 0 ▶ if ($css->{mtime} >= (stat($path))[9]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($css->{"mtime"} >= (stat $path)[9]) 0 0 0
172 0 return $css->{style};
173 }
174 }
175 6 ▶ $css = CSS::Tiny->read($path)
Branch: 1 branch
BranchTrueFalseTotal
$css = "CSS::Tiny"->read($path) or croak("Can't open '${path}' by @{[__PACKAGE__];}") 6 0 6
176 0 or croak "Can't open '$path' by @{[ __PACKAGE__ ]}";
177 6 ▶ return unless $css;
Branch: 1 branch
BranchTrueFalseTotal
unless $css 0 6 6
178 6 $CSS{$path} = {
179 style => $css,
180 mtime => (stat($path))[9],
181 };
182 6 return $css;
183 }
184  
185 0 ▶sub _fetch {
Subroutine _fetch: not called CC=4 SCAR=30.0
186 0 my ($self, $url) = @_;
187 0 require LWP::UserAgent;
188 0 ▶ my $ua = $self->{_ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
Condition0%0/3
$self->{'_ua'} || 'LWP::UserAgent'->new('agent', 'HTML::MobileJpCSS' . "/$HTML::MobileJpCSS::VERSION")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_ua'} || 'LWP::UserAgent'->new('agent', 'HTML::MobileJpCSS' . "/$HTML::MobileJpCSS::VERSION")
$self->{'_ua'} 'LWP::UserAgent'->new('agent', 'HTML::MobileJpCSS' . "/$HTML::MobileJpCSS::VERSION")
Truth table0%0/3
A || B
A= $self->{'_ua'}B= 'LWP::UserAgent'->new('agent', 'HTML::MobileJpCSS' . "/$HTML::MobileJpCSS::VERSION")
ABresult
1X1
011
000
189 0 my $res = $ua->get($url);
190 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
191 0 ▶ my $css = CSS::Tiny->read_string($res->content)
Branch: 1 branch
BranchTrueFalseTotal
my $css = "CSS::Tiny"->read_string($res->content) or croak("Can't parse '${url}' by @{[__PACKAGE__];}") 0 0 0
192 0 or croak "Can't parse '$url' by @{[ __PACKAGE__ ]}";
193 0 return $css;
194 }
195 0 croak "Can't fetch $url by @{[ __PACKAGE__ ]}";
196 }
197  
198 31 ▶sub _replace_style {
Subroutine _replace_style: called CC=3 SCAR=11.0
199 31 my ($self, $content, $node, $props) = @_;
200 31 my ($tag) = $node =~ /^<([^\s]+).*?>/is;
201 31 my $replace = $node;
202 31 my $style;
203 31 for (keys %$props) {
204 41 $style .= $self->_filter($tag, $_, $props->{$_});
205 }
206 31 ▶ if ($node =~ /style=".+?"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node =~ /style=".+?"/) { } 1 30 31
207 1 $replace =~ s/(style=".+?)"/$1$style"/is;
208 }
209 else {
210 30 $replace =~ s/<$tag/<$tag style="$style"/is;
211 }
212 31 $replace =~ s/[\n\s]+/ /g;
213 31 $content =~ s/$node/$replace/is;
214 31 return $content;
215 }
216  
217 41 ▶sub _filter {
Subroutine _filter: called CC=7 SCAR=19.5
218 41 my ($self, $tagname, $property, $value) = @_;
219 41 ▶ return "$property:$value;" unless $StyleMap->{$tagname};
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::MobileJpCSS::StyleMap->{$tagname} 6 35 41
220 35 ▶ return "$property:$value;" unless $StyleMap->{$tagname}->{$property};
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::MobileJpCSS::StyleMap->{$tagname}{$property} 9 26 35
221 26 my $style = $StyleMap->{$tagname}->{$property};
222 26 my $carrier = $self->{agent}->carrier;
223 26 $carrier =~ s/^V$/S/;
224 26 $carrier =~ s/^H$/W/;
225 26 ▶ if (ref $style eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $style eq 'ARRAY') { } 14 12 26
elsif ($style) { } 12 0 12
226 14 my $prop = $style->[0]->{$value}->{$carrier};
227 14 ▶ return "$prop;" if $prop;
Branch: 1 branch
BranchTrueFalseTotal
if $prop 11 3 14
228 }
229 elsif ($style) {
230 12 my $prop = $style->{$carrier};
231 12 ▶ return "$prop:$value;" if $prop;
Branch: 1 branch
BranchTrueFalseTotal
if $prop 10 2 12
232 }
233 5 return "$property:$value;";
234 }
235  
236 7 1;
237  
238 __END__
↑ Summary