| 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 ||= {}
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | my $config = $conf->{config} || DEFAULT_CONFIG_FILE; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'config'} || 'sprite.xml'
MC/DC0%0/1
$conf->{'config'} || 'sprite.xml'
$conf->{'config'}
Truth table50%1/2
A
A= $conf->{'config'}
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | my $css_url = $conf->{css_url} || DEFAULT_CSS_URL; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'css_url'} || '/css/sprite.css'
MC/DC0%0/1
$conf->{'css_url'} || '/css/sprite.css'
$conf->{'css_url'}
Truth table50%1/2
A
A= $conf->{'css_url'}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | my $sprite_images = ref($config) eq 'HASH' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | ? $config | ||||||||||||||||||||||||||||
| 24 | : $self->_parse_xml_config($app, $config) | ||||||||||||||||||||||||||||
| 25 | ; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | unless (scalar %$sprite_images) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Only html response | ||||||||||||||||||||||||||||
| 39 | 3 | ▶ | return unless $res->headers->content_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | return if $res->headers->content_type !~ /html/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | return if $res->body !~ /^\s*</; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$app->mode eq "development" and $c->param("no_sprites")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||