| 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'} || ''
MC/DC100%1/1
$config->{'base'} || ''
$config->{'base'}
Truth table100%2/2
A
A= $config->{'base'}
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 14 | ▶ | if ( $base and substr( $base, -1 ) ne '/' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$base and substr($base, -1) ne "/"
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 "/"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 57 | ▶ | if ( $_[1]->{check} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | my $asset = $c->app->static->file( | ||||||||||||||||||||||||||||
| 32 | 9 | ▶ | $_[1]->{no_base} ? $_[0] : "$base$_[0]" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 9 | ▶ | return '' if !$asset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 54 | ▶ | if ( $_[1]->{inplace} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 10 | my ($file,$config) = @_; | |||||||||||||||||||||||||||
| 40 | 10 | ▶ | my $local_base = $config->{no_base} ? '' : $base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 10 | ▶ | $local_base = $c->url_for( $local_base ) if $local_base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 10 | ▶ | $config->{no_file} = 1 if $config->{js}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | my $js = $config->{no_file} ? | ||||||||||||||||||||||||||||
| 47 | 10 | ▶ | qq~<script type="text/javascript">$file</script>~ : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | 35 | ▶ | return if !$c->stash->{__JSLOADERFILES__}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 24 | ▶ | return if 'ARRAY' ne ref $c->stash->{__JSLOADERFILES__}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my $load_js = | ||||||||||||||||||||||||||||
| 65 | join "\n", | ||||||||||||||||||||||||||||
| 66 | map{ | ||||||||||||||||||||||||||||
| 67 | 42 | my ($file,$config) = @{ $_ }; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 42 | ▶ | $file //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$file //= ""
MC/DC100%1/1
$file //= ""
$file
Truth table100%2/2
A
A= $file
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 42 | ▶ | my $local_base = $config->{no_base} ? '' : $base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 42 | ▶ | $config->{no_file} = 1 if $config->{js}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 42 | ▶ | $local_base = $c->url_for( $local_base ) if $local_base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 42 | ▶ | if ( $config->{no_file} and $config->{on_ready} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$config->{"no_file"} and $config->{"on_ready"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 83 | qq~<script type="text/javascript" src="$local_base$file"></script>~; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 42 | ▶ | $file ? $return : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 23 | @{ $c->stash->{__JSLOADERFILES__} }; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 23 | ▶ | return if !$load_js; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 99 | 51 | ▶ | return 1 if ref $config ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 48 | ▶ | return 1 if !$config->{browser}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 18 | ▶ | return 1 if ref $config->{browser} ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 109 | 5 | ▶ | return exists $browser->{default} ? 1 : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 11 | ▶ | $op = '' if !defined $op; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 11 | ▶ | if ( $op eq 'gt' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||