lib/Mojolicious/Plugin/Sprite.pm

filter: Statementstmt 82.8% 53 / 64 Branchbran 50.0% 11 / 22 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 70.0% 77 / 110 CC 19 SCAR 22.3
CC
19
Cov
72%
__ANON__
14.9
register
6.6
_parse_xml_config
6.0
CRAP 9.3
↑ Summary
1 package Mojolicious::Plugin::Sprite;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use constant DEFAULT_CONFIG_FILE => 'sprite.xml';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use constant DEFAULT_CSS_URL => '/css/sprite.css';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.01';
12  
13 1 our $parser = XML::LibXML->new();
14  
15 1 ▶sub register {
Subroutine register: called CC=6 SCAR=18.9
Pod documented
16 1 my ( $self, $app, $conf ) = @_;
17 1 ▶ $conf ||= {};
Condition50%1/2
$conf ||= {}
l!l
10
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
Aresult
11
00
18  
19 1 ▶ my $config = $conf->{config} || DEFAULT_CONFIG_FILE;
Condition50%1/2
$conf->{'config'} || 'sprite.xml'
l!l
10
MC/DC0%0/1
$conf->{'config'} || 'sprite.xml'
$conf->{'config'}
Truth table50%1/2
A
A= $conf->{'config'}
Aresult
11
00
20 1 ▶ my $css_url = $conf->{css_url} || DEFAULT_CSS_URL;
Condition50%1/2
$conf->{'css_url'} || '/css/sprite.css'
l!l
10
MC/DC0%0/1
$conf->{'css_url'} || '/css/sprite.css'
$conf->{'css_url'}
Truth table50%1/2
A
A= $conf->{'css_url'}
Aresult
11
00
21  
22 1 ▶ my $sprite_images = ref($config) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $config eq 'HASH' ? : 1 0 1
23 ? $config
24 : $self->_parse_xml_config($app, $config)
25 ;
26  
27 1 ▶ unless (scalar %$sprite_images) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %$sprite_images) 0 1 1
28 0 $app->log->warn( __PACKAGE__ .": Plugin not activated.");
29 }
30  
31 3 ▶ $app->hook(after_dispatch => sub {
Subroutine __ANON__: called CC=13 SCAR=27.0
32 3 my ($c) = @_;
33 3 my $res = $c->res;
34  
35 # Only successful response
36 3 ▶ return if $res->code !~ m/^2/;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code =~ /^2/u 0 3 3
37  
38 # Only html response
39 3 ▶ return unless $res->headers->content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->headers->content_type 0 3 3
40 3 ▶ return if $res->headers->content_type !~ /html/;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->headers->content_type =~ /html/u 0 3 3
41 3 ▶ return if $res->body !~ /^\s*</;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->body =~ /^\s*</u 0 3 3
42  
43 # Skip if "?no_sprites=1" and mode is 'develompent'
44 3 ▶ return if $app->mode eq 'development' && $c->param('no_sprites');
Branch: 1 branch
BranchTrueFalseTotal
if $app->mode eq "development" and $c->param("no_sprites") 0 3 3
Condition33%1/3
$app->mode eq "development" and $c->param("no_sprites")
!ll&&!rl&&r
030
MC/DC0%0/2
$app->mode eq "development" and $c->param("no_sprites")
$app->mode eq "development" $c->param("no_sprites")
Truth table33%1/3
A and B
A= $app->mode eq "development"B= $c->param("no_sprites")
ABresult
0X0
100
111
45  
46 3 my $dom = $parser->parse_html_string($res->body);
47  
48 # Replace "img" tags
49 3 foreach my $img ( $dom->findnodes('//img') ) {
50 # Skip if "src" is empty
51 6 ▶ my $src = $img->getAttribute('src') or last;
Branch: 1 branch
BranchTrueFalseTotal
my $src = $img->getAttribute("src") or last 6 0 6
52  
53 # Remove first "/"
54 6 $src =~ s/^\///;
55  
56 # Skip if not found
57 6 ▶ my $selector = $sprite_images->{$src} or last;
Branch: 1 branch
BranchTrueFalseTotal
my $selector = $sprite_images->{$src} or last 3 3 6
58  
59 # Change node name
60 3 $img->setNodeName('span');
61  
62 # Add single space
63 3 $img->appendText(' ');
64  
65 # Add 'class' and 'id' attribute
66 3 my @class = grep { $_ } ($img->getAttribute('class'), 'spr');
67 3 ▶ if ( substr($selector, 0, 1) eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($selector, 0, 1) eq '.') { } 3 0 3
68 3 push @class, substr($selector, 1);
69 }
70 else {
71 0 $img->setAttribute( 'id', substr($selector, 1) );
72 }
73 3 $img->setAttribute( 'class', join(' ', @class) );
74  
75 # Remove unneeded attributes
76 3 $img->removeAttribute($_) for qw(src width height alt);
77 }
78  
79 # Add 'sprite.css'
80 3 foreach my $head ( $dom->findnodes('/html/head') ) {
81 3 my $style = $head->addNewChild(undef, 'link');
82 3 $style->setAttribute('rel', 'stylesheet');
83 3 $style->setAttribute('href', $css_url);
84 }
85  
86 3 $res->body( $dom->toStringHTML() );
87 1 });
88 }
89  
90 0 ▶sub _parse_xml_config {
Subroutine _parse_xml_config: not called CC=2 SCAR=17.9
91 0 my ($self, $app, $config) = @_;
92  
93 0 ▶ unless (-e $config) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $config) 0 0 0
94 0 $app->log->warn( __PACKAGE__ .": Config file '$config' does not exists.");
95 0 return {};
96 }
97  
98 0 my $parser = XML::LibXML->new();
99 0 my $dom = $parser->parse_file($config);
100  
101 my %sprite_images = map {
102 0 $_->getAttribute('image') => $_->getAttribute('selector')
103 } $dom->findnodes('/root/sprite/image');
104  
105 0 return \%sprite_images;
106 }
107  
108 1 1;
109 __END__
↑ Summary