blib/lib/Tags/HTML/Page/Begin.pm

filter: Statementstmt 100% 137 / 137 Branchbran 100% 54 / 54 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 100% 210 / 210 CC 34 SCAR 23.7
CC
34
Cov
100%
_process
18.0
new
11.0
_favicon
6.0
CRAP 10.7
↑ Summary
1 package Tags::HTML::Page::Begin;
2  
3 4 ▶use base qw(Tags::HTML);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Class::Utils qw(set_params split_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use List::MoreUtils qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Constants.
13 4 Readonly::Hash my %LANG => (
14 'title' => 'Page title',
15 );
16  
17 4 our $VERSION = 0.16;
18  
19 # Constructor.
20 sub new {
21 34 ▶ my ($class, @params) = @_;
Subroutine new: called CC=11 SCAR=24.0
Pod documented
22  
23 # Create object.
24 34 my ($object_params_ar, $other_params_ar) = split_params(
25 ['application-name', 'author', 'base_href', 'base_target',
26 'css_init', 'css_src', 'charset', 'description', 'doctype',
27 'favicon', 'generator', 'html_lang', 'http_equiv_content_type',
28 'keywords', 'lang', 'refresh', 'robots', 'rss',
29 'script_js', 'script_js_src', 'viewport'], @params);
30 34 my $self = $class->SUPER::new(@{$other_params_ar});
31  
32 # Application name.
33 32 $self->{'application-name'} = undef;
34  
35 # Author.
36 32 $self->{'author'} = undef;
37  
38 # Base element.
39 32 $self->{'base_href'} = undef;
40 32 $self->{'base_target'} = undef;
41  
42 # Init CSS style.
43 32 $self->{'css_init'} = [
44 ['s', '*'],
45 ['d', 'box-sizing', 'border-box'],
46 ['d', 'margin', 0],
47 ['d', 'padding', 0],
48 ['e'],
49 ];
50  
51 # CSS links.
52 32 $self->{'css_src'} = [];
53  
54 # Charset.
55 32 $self->{'charset'} = 'UTF-8';
56  
57 # Description.
58 32 $self->{'description'} = undef;
59  
60 # Doctype.
61 32 $self->{'doctype'} = '<!DOCTYPE html>';
62  
63 # Favicon.
64 32 $self->{'favicon'} = undef;
65  
66 # Generator.
67 32 $self->{'generator'} = 'Perl module: '.__PACKAGE__.', Version: '.$VERSION;
68  
69 # HTML element lang attribute.
70 32 $self->{'html_lang'} = 'en';
71  
72 # http-equiv content-type.
73 32 $self->{'http_equiv_content_type'} = 'text/html';
74  
75 # Keywords.
76 32 $self->{'keywords'} = undef;
77  
78 # Languages.
79 32 $self->{'lang'} = \%LANG;
80  
81 # Refresh.
82 32 $self->{'refresh'} = undef;
83  
84 # Robots.
85 32 $self->{'robots'} = undef;
86  
87 # RSS
88 32 $self->{'rss'} = undef;
89  
90 # Script js code.
91 32 $self->{'script_js'} = [];
92  
93 # Script js sources.
94 32 $self->{'script_js_src'} = [];
95  
96 # Viewport.
97 32 $self->{'viewport'} = 'width=device-width, initial-scale=1.0';
98  
99 # Process params.
100 32 set_params($self, @{$object_params_ar});
101  
102 # Check for 'css_src' array.
103 32 ▶ if (ref $self->{'css_src'} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"css_src"} ne "ARRAY") 1 31 32
104 1 err "Parameter 'css_src' must be a array.";
105 }
106 31 foreach my $css_src_hr (@{$self->{'css_src'}}) {
107 4 ▶ if (ref $css_src_hr ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $css_src_hr ne "HASH") 1 3 4
108 1 err "Parameter 'css_src' must be a array of hash structures.";
109 }
110 3 foreach my $key (keys %{$css_src_hr}) {
111 4 ▶ if (none { $key eq $_ } qw(link media)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $key eq $_; } , "link", "media")) 1 3 4
112 1 err "Parameter 'css_src' must be a array of hash ".
113 "structures with 'media' and 'link' keys."
114 }
115 }
116 }
117  
118 # Check charset.
119 29 ▶ if (! defined $self->{'charset'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"charset"}) 1 28 29
120 1 err "Parameter 'charset' is required.";
121 }
122  
123 # Check for 'script_js' array.
124 28 ▶ if (ref $self->{'script_js'} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"script_js"} ne "ARRAY") 2 26 28
125 2 err "Parameter 'script_js' must be a array.";
126 }
127  
128 # Check for 'script_js_src' array.
129 26 ▶ if (ref $self->{'script_js_src'} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"script_js_src"} ne "ARRAY") 2 24 26
130 2 err "Parameter 'script_js_src' must be a array.";
131 }
132  
133 # Check for favicon.
134 24 ▶ if (defined $self->{'favicon'} && $self->{'favicon'} !~ m/\.(ico|png|jpg|gif|svg)$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"favicon"} and not $self->{"favicon"} =~ /\.(ico|png|jpg|gif|svg)$/ms) 1 23 24
Condition100%3/3
defined $self->{"favicon"} and not $self->{"favicon"} =~ /\.(ico|png|jpg|gif|svg)$/ms
!ll&&!rl&&r
1851
MC/DC100%2/2
defined $self->{"favicon"} and not $self->{"favicon"} =~ /\.(ico|png|jpg|gif|svg)$/ms
defined $self->{"favicon"} not $self->{"favicon"} =~ /\.(ico|png|jpg|gif|svg)$/ms
Truth table100%3/3
A and not(B)
A= defined $self->{"favicon"}B= not $self->{"favicon"} =~ /\.(ico|png|jpg|gif|svg)$/ms
ABresult
0X0
100
111
135 1 err "Parameter 'favicon' contain bad image type.";
136 }
137  
138 # Object.
139 23 return $self;
140 }
141  
142 # Process 'Tags'.
143 sub _process {
144 20 ▶ my $self = shift;
Subroutine _process: called CC=18 SCAR=28.9
145  
146 20 my $css;
147 20 ▶ if ($self->{'css'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"css"}) 2 18 20
148 2 $css = $self->{'css'}->flush(1);
149 2 ▶ if ($css ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($css ne '') { } 1 1 2
150 1 $css .= "\n";
151 } else {
152 1 undef $css;
153 }
154 }
155  
156 # Begin of page.
157 $self->{'tags'}->put(
158 ['r', $self->{'doctype'}],
159 ['r', "\n"],
160 ['b', 'html'],
161 20 ['a', 'lang', $self->{'html_lang'}],
162 ['b', 'head'],
163 );
164  
165 20 ▶ if (defined $self->{'http_equiv_content_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"http_equiv_content_type"}) 19 1 20
166 $self->{'tags'}->put(
167 ['b', 'meta'],
168 ['a', 'http-equiv', 'Content-Type'],
169 ['a', 'content', $self->{'http_equiv_content_type'}.
170 19 '; charset='.$self->{'charset'}],
171 ['e', 'meta'],
172 );
173 }
174 20 ▶ if (defined $self->{'base_href'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"base_href"}) 2 18 20
175 $self->{'tags'}->put(
176 ['b', 'base'],
177 ['a', 'href', $self->{'base_href'}],
178 defined $self->{'base_target'} ? (
179 2 ▶ ['a', 'target', $self->{'base_target'}],
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base_target'} ? : 1 1 2
180 ) : (),
181 ['e', 'base'],
182 );
183 }
184 20 ▶ if (! defined $self->{'http_equiv_content_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"http_equiv_content_type"}) 1 19 20
185 $self->{'tags'}->put(
186 ['b', 'meta'],
187 1 ['a', 'charset', $self->{'charset'}],
188 ['e', 'meta'],
189 );
190 }
191 20 $self->_meta('application-name');
192 20 $self->_meta('author');
193 20 $self->_meta('description');
194 20 $self->_meta('generator');
195 20 $self->_meta('keywords');
196 20 $self->_meta('robots');
197 20 $self->_meta('viewport');
198 20 ▶ if (defined $self->{'refresh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"refresh"}) 1 19 20
199 $self->{'tags'}->put(
200 ['b', 'meta'],
201 ['a', 'http-equiv', 'refresh'],
202 1 ['a', 'content', $self->{'refresh'}],
203 ['e', 'meta'],
204 );
205 }
206  
207 20 $self->_favicon;
208  
209 20 ▶ if (@{$self->{'script_js'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"script_js"};}) 1 19 20
210 1 foreach my $script_js (@{$self->{'script_js'}}) {
211 2 $self->{'tags'}->put(
212 ['b', 'script'],
213 ['a', 'type', 'text/javascript'],
214 ['d', $script_js],
215 ['e', 'script'],
216 );
217 }
218 }
219 20 ▶ if (@{$self->{'script_js_src'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"script_js_src"};}) 1 19 20
220 1 foreach my $script_js_src (@{$self->{'script_js_src'}}) {
221 2 $self->{'tags'}->put(
222 ['b', 'script'],
223 ['a', 'type', 'text/javascript'],
224 ['a', 'src', $script_js_src],
225 ['e', 'script'],
226 );
227 }
228 }
229  
230 $self->{'tags'}->put(
231 defined $self->{'lang'}->{'title'} ? (
232 ['b', 'title'],
233 20 ▶ ['d', $self->{'lang'}->{'title'}],
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'lang'}{'title'} ? : 19 1 20
defined $css ? : 1 19 20
234 ['e', 'title'],
235 ) : (),
236  
237 (
238 defined $css ? (
239 ['b', 'style'],
240 ['a', 'type', 'text/css'],
241 ['d', $css],
242 ['e', 'style'],
243 ) : (),
244 ),
245 );
246 20 ▶ if (@{$self->{'css_src'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"css_src"};}) 1 19 20
247 1 foreach my $css_src_hr (@{$self->{'css_src'}}) {
248 $self->{'tags'}->put(
249 ['b', 'link'],
250 ['a', 'rel', 'stylesheet'],
251 ['a', 'href', $css_src_hr->{'link'}],
252 $css_src_hr->{'media'} ? (
253 2 ▶ ['a', 'media', $css_src_hr->{'media'}],
Branch: 1 branch
BranchTrueFalseTotal
$css_src_hr->{'media'} ? : 1 1 2
254 ) : (),
255 ['a', 'type', 'text/css'],
256 ['e', 'link'],
257 );
258 }
259 }
260  
261 20 ▶ if (defined $self->{'rss'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"rss"}) 1 19 20
262 $self->{'tags'}->put(
263 ['b', 'link'],
264 ['a', 'rel', 'alternate'],
265 ['a', 'type', 'application/rss+xml'],
266 ['a', 'title', 'RSS'],
267 1 ['a', 'href', $self->{'rss'}],
268 ['e', 'link'],
269 );
270 }
271  
272 20 $self->{'tags'}->put(
273 ['e', 'head'],
274 ['b', 'body'],
275 );
276  
277 20 return;
278 }
279  
280 sub _process_css {
281 1 ▶ my $self = shift;
Subroutine _process_css: called CC=1 SCAR=0.0
282  
283 $self->{'css'}->put(
284 1 @{$self->{'css_init'}},
285 );
286  
287 1 return;
288 }
289  
290 sub _favicon {
291 20 ▶ my $self = shift;
Subroutine _favicon: called CC=6 SCAR=17.9
292  
293 20 ▶ if (! defined $self->{'favicon'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"favicon"}) 15 5 20
294 15 return;
295 }
296  
297 5 my ($suffix) = $self->{'favicon'} =~ m/\.(ico|png|jpg|gif|svg)$/ms;
298 5 my $image_type;
299 5 ▶ if ($suffix eq 'ico') {
Branch: 4 branches
BranchTrueFalseTotal
if ($suffix eq 'ico') { } 1 4 5
elsif ($suffix eq 'png') { } 1 3 4
elsif ($suffix eq 'svg') { } 1 2 3
elsif ($suffix eq 'gif') { } 1 1 2
300 1 $image_type = 'image/vnd.microsoft.icon';
301 } elsif ($suffix eq 'png') {
302 1 $image_type = 'image/png';
303 } elsif ($suffix eq 'svg') {
304 1 $image_type = 'image/svg+xml';
305 } elsif ($suffix eq 'gif') {
306 1 $image_type = 'image/gif';
307 } else {
308 1 $image_type = 'image/jpeg';
309 }
310  
311 $self->{'tags'}->put(
312 ['b', 'link'],
313 ['a', 'rel', 'icon'],
314 5 ['a', 'href', $self->{'favicon'}],
315 ['a', 'type', $image_type],
316 ['e', 'link'],
317 );
318  
319 5 return;
320 }
321  
322 sub _meta {
323 140 ▶ my ($self, $key) = @_;
Subroutine _meta: called CC=2 SCAR=6.9
324  
325 140 ▶ if (! defined $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$key}) 121 19 140
326 121 return;
327 }
328  
329 $self->{'tags'}->put(
330 ['b', 'meta'],
331 ['a', 'name', $key],
332 19 ['a', 'content', $self->{$key}],
333 ['e', 'meta'],
334 );
335  
336 19 return;
337 }
338  
339 4 1;
340  
341 __END__
↑ Summary