lib/Mojolicious/Plugin/JSLoader.pm

filter: Statementstmt 100% 79 / 79 Branchbran 100% 62 / 62 Conditioncond 100% 10 / 10 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 100% 168 / 168 CC 36 SCAR 22.7
CC
36
Cov
100%
__ANON__
13.0
_match_browser
12.0
__ANON__
10.0
CRAP 9.7
↑ Summary
1 package Mojolicious::Plugin::JSLoader;
2  
3 # ABSTRACT: move js loading to the end of the document
4  
5 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 ▶use parent 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 14 ▶use HTML::ParseBrowser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 our $VERSION = '0.08';
15  
16 14 ▶sub register {
Subroutine register: called CC=4 SCAR=13.9
Pod documented
17 14 my ($self, $app, $config) = @_;
18  
19 14 ▶ my $base = $config->{base} || '';
Condition100%2/2
$config->{'base'} || ''
l!l
59
MC/DC100%1/1
$config->{'base'} || ''
$config->{'base'}
Truth table100%2/2
A
A= $config->{'base'}
Aresult
11
00
20  
21 14 ▶ if ( $base and substr( $base, -1 ) ne '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base and substr($base, -1) ne "/") 3 11 14
Condition100%3/3
$base and substr($base, -1) ne "/"
!ll&&!rl&&r
923
MC/DC100%2/2
$base and substr($base, -1) ne "/"
$base substr($base, -1) ne "/"
Truth table100%3/3
A and B
A= $baseB= substr($base, -1) ne "/"
ABresult
0X0
100
111
22 3 $base .= '/';
23 }
24  
25 61 ▶ $app->helper( js_load => sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
26 61 my $c = shift;
27  
28 61 ▶ return '' unless _match_browser($c, @_);
Branch: 1 branch
BranchTrueFalseTotal
unless _match_browser($c, @_) 4 57 61
29  
30 57 ▶ if ( $_[1]->{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]{"check"}) 9 48 57
31 my $asset = $c->app->static->file(
32 9 ▶ $_[1]->{no_base} ? $_[0] : "$base$_[0]"
Branch: 1 branch
BranchTrueFalseTotal
$_[1]{'no_base'} ? : 1 8 9
33 );
34  
35 9 ▶ return '' if !$asset;
Branch: 1 branch
BranchTrueFalseTotal
unless $asset 3 6 9
36 }
37  
38 54 ▶ if ( $_[1]->{inplace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]{"inplace"}) 10 44 54
39 10 my ($file,$config) = @_;
40 10 ▶ my $local_base = $config->{no_base} ? '' : $base;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'no_base'} ? : 1 9 10
41  
42 10 ▶ $local_base = $c->url_for( $local_base ) if $local_base;
Branch: 1 branch
BranchTrueFalseTotal
if $local_base 2 8 10
43  
44 10 ▶ $config->{no_file} = 1 if $config->{js};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"js"} 1 9 10
45  
46 my $js = $config->{no_file} ?
47 10 ▶ qq~<script type="text/javascript">$file</script>~ :
Branch: 1 branch
BranchTrueFalseTotal
$config->{'no_file'} ? : 1 9 10
48 qq~<script type="text/javascript" src="$local_base$file"></script>~;
49 10 return Mojo::ByteStream->new( $js );
50 }
51  
52 44 push @{ $c->stash->{__JSLOADERFILES__} }, [ @_ ];
53  
54 44 return 1;
55 14 } );
56  
57 36 ▶ $app->hook( after_render => sub {
Subroutine __ANON__: called CC=13 SCAR=25.6
58 36 my ($c, $content, $format) = @_;
59  
60 36 ▶ return if $format ne 'html';
Branch: 1 branch
BranchTrueFalseTotal
if $format ne "html" 1 35 36
61 35 ▶ return if !$c->stash->{__JSLOADERFILES__};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash->{"__JSLOADERFILES__"} 11 24 35
62 24 ▶ return if 'ARRAY' ne ref $c->stash->{__JSLOADERFILES__};
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $c->stash->{"__JSLOADERFILES__"} 1 23 24
63  
64 my $load_js =
65 join "\n",
66 map{
67 42 my ($file,$config) = @{ $_ };
68  
69 42 ▶ $file //= '';
Condition100%2/2
$file //= ""
l!l
411
MC/DC100%1/1
$file //= ""
$file
Truth table100%2/2
A
A= $file
Aresult
11
00
70  
71 42 ▶ my $local_base = $config->{no_base} ? '' : $base;
Branch: 1 branch
BranchTrueFalseTotal
$config->{'no_base'} ? : 1 41 42
72  
73 42 ▶ $config->{no_file} = 1 if $config->{js};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"js"} 4 38 42
74  
75 42 ▶ $local_base = $c->url_for( $local_base ) if $local_base;
Branch: 1 branch
BranchTrueFalseTotal
if $local_base 16 26 42
76  
77 42 ▶ if ( $config->{no_file} and $config->{on_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"no_file"} and $config->{"on_ready"}) 3 39 42
Condition100%3/3
$config->{"no_file"} and $config->{"on_ready"}
!ll&&!rl&&r
27123
MC/DC100%2/2
$config->{"no_file"} and $config->{"on_ready"}
$config->{"no_file"} $config->{"on_ready"}
Truth table100%3/3
A and B
A= $config->{"no_file"}B= $config->{"on_ready"}
ABresult
0X0
100
111
78 3 $file = sprintf '$(document).ready( function(){%s});', $file;
79 }
80  
81 my $return = $config->{no_file} ?
82 42 ▶ qq~<script type="text/javascript">$file</script>~ :
Branch: 1 branch
BranchTrueFalseTotal
$config->{'no_file'} ? : 15 27 42
83 qq~<script type="text/javascript" src="$local_base$file"></script>~;
84  
85 42 ▶ $file ? $return : ();
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 41 1 42
86 }
87 23 @{ $c->stash->{__JSLOADERFILES__} };
88  
89 23 ▶ return if !$load_js;
Branch: 1 branch
BranchTrueFalseTotal
unless $load_js 2 21 23
90  
91 21 ${$content} =~ s!(</body(?:\s|>)|\z)!$load_js$1!;
92 14 });
93 }
94  
95 75 ▶sub _match_browser {
Subroutine _match_browser: called CC=12 SCAR=24.8
96 75 my ($c,$file,$config) = @_;
97  
98 75 ▶ return 1 if !$config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 24 51 75
99 51 ▶ return 1 if ref $config ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $config ne "HASH" 3 48 51
100 48 ▶ return 1 if !$config->{browser};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"browser"} 30 18 48
101 18 ▶ return 1 if ref $config->{browser} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $config->{"browser"} ne "HASH" 1 17 18
102  
103 17 my $ua_string = $c->req->headers->user_agent;
104 17 my $ua = HTML::ParseBrowser->new( $ua_string );
105 17 my $name = $ua->name;
106 17 my $browser = $config->{browser};
107  
108 17 ▶ if ( !exists $browser->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $browser->{$name}) 5 12 17
109 5 ▶ return exists $browser->{default} ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $browser->{'default'} ? : 2 3 5
110 }
111  
112 12 my ($op,$version) = $browser->{$name} =~ m{\A\s*([lg]t|!)?\s*([0-9\.]+)};
113  
114 12 ▶ return if !defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 1 11 12
115  
116 11 ▶ $op = '' if !defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 3 8 11
117  
118 11 ▶ if ( $op eq 'gt' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'gt') { } 3 8 11
elsif ($op eq 'lt') { } 1 7 8
elsif ($op eq '!') { } 4 3 7
119 3 return version->parse( $version ) <= version->parse( $ua->v );
120 }
121 elsif ( $op eq 'lt' ) {
122 1 return version->parse( $version ) >= version->parse( $ua->v );
123 }
124 elsif ( $op eq '!' ) {
125 4 return version->parse( $version ) != version->parse( $ua->v );
126 }
127  
128 3 return version->parse( $ua->v ) == version->parse( $version );
129 }
130  
131 14 1;
132  
133 __END__
↑ Summary