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

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