| 1 | package Mojolicious::Plugin::CSSLoader; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: move css 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 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 14 | ▶ | sub register { | ||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 13 | 14 | my ($self, $app, $config) = @_; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 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'}
| |||||||||||||||||||||||||||||||||
| 16 | 14 | ▶ | my $media = $config->{media} || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'media'} || ''
MC/DC100%1/1
$config->{'media'} || ''
$config->{'media'}
Truth table100%2/2
A
A= $config->{'media'}
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 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 "/"
| |||||||||||||||||||||||||||||||||
| 19 | 4 | $base .= '/'; | |||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 43 | ▶ | $app->helper( css_load => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 23 | 43 | my $c = shift; | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 43 | ▶ | if ( $_[1]->{check} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | my $asset = $c->app->static->file( | ||||||||||||||||||||||||||||||||
| 27 | 8 | ▶ | $_[1]->{no_base} ? $_[0] : "$base$_[0]" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 8 | ▶ | return '' if !$asset; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 40 | push @{ $c->stash->{__CSSLOADERFILES__} }, [ @_ ]; | |||||||||||||||||||||||||||||||
| 34 | 14 | } ); | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 32 | ▶ | $app->hook( after_render => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=19 SCAR=29.4
| |||||||||||||||||||||||||||||||||
| 37 | 32 | my ($c, $content, $format) = @_; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 32 | ▶ | return if $format ne 'html'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 31 | ▶ | return if !$c->stash->{__CSSLOADERFILES__}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 30 | ▶ | return if 'ARRAY' ne ref $c->stash->{__CSSLOADERFILES__}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | my $load_css = | ||||||||||||||||||||||||||||||||
| 44 | join "\n", | ||||||||||||||||||||||||||||||||
| 45 | map{ | ||||||||||||||||||||||||||||||||
| 46 | 40 | my ($file,$config) = @{ $_ }; | |||||||||||||||||||||||||||||||
| 47 | 40 | ▶ | $file //= ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$file //= ""
MC/DC100%1/1
$file //= ""
$file
Truth table100%2/2
A
A= $file
| |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 40 | ▶ | my $local_base = $config->{no_base} ? '' : $base; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 40 | ▶ | $local_base = $c->url_for( $local_base ) if $local_base; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 40 | my $local_media = ""; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 40 | ▶ | $local_media = ' media="' . $media . '"' if $media; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | 40 | ▶ | $local_media = ' media="' . $config->{media} . '"' if $config->{media}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 40 | my $ie_start = ''; | |||||||||||||||||||||||||||||||
| 59 | 40 | my $ie_end = ''; | |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 40 | ▶ | if ( exists $config->{ie} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | 7 | my $start_extra = ''; | |||||||||||||||||||||||||||||||
| 63 | 7 | my $end_extra = ''; | |||||||||||||||||||||||||||||||
| 64 | 7 | my $cmp = ''; | |||||||||||||||||||||||||||||||
| 65 | 7 | my $version = ''; | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 7 | ▶ | if ( !ref $config->{ie} && !$config->{ie} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $config->{'ie'} || $config->{'ie'}
MC/DC100%2/2
ref $config->{'ie'} || $config->{'ie'}
ref $config->{'ie'}
$config->{'ie'}
Truth table100%3/3
A || B
A= ref $config->{'ie'}B= $config->{'ie'}
| |||||||||||||||||||||||||||||||||
| 68 | 1 | $start_extra = '<!-->'; | |||||||||||||||||||||||||||||||
| 69 | 1 | $end_extra = '<!--'; | |||||||||||||||||||||||||||||||
| 70 | 1 | $cmp = '!'; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | elsif( ref $config->{ie} ) { | ||||||||||||||||||||||||||||||||
| 73 | 5 | my %map = qw/> gt >= gte < lt <= lte/; | |||||||||||||||||||||||||||||||
| 74 | 5 | (my $key, $version) = %{ $config->{ie} }; | |||||||||||||||||||||||||||||||
| 75 | 5 | ▶ | $cmp = $map{$key} || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$map{$key} || ''
MC/DC100%1/1
$map{$key} || ''
$map{$key}
Truth table100%2/2
A
A= $map{$key}
| |||||||||||||||||||||||||||||||||
| 76 | 5 | ▶ | $cmp .= ' ' if $cmp; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 7 | $ie_start = sprintf "<!-- [if %sIE %s]>%s", | |||||||||||||||||||||||||||||||
| 80 | $cmp, | ||||||||||||||||||||||||||||||||
| 81 | $version, | ||||||||||||||||||||||||||||||||
| 82 | $start_extra; | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 7 | $ie_end = sprintf "%s<![endif]-->", $end_extra; | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | my $return = $config->{no_file} ? | ||||||||||||||||||||||||||||||||
| 88 | 40 | ▶ | qq~$ie_start<style type="text/css">$file</style>$ie_end~ : | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | qq~$ie_start<link rel="stylesheet" href="$local_base$file"$local_media/>$ie_end~; | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 40 | ▶ | $file ? $return : (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | 29 | @{ $c->stash->{__CSSLOADERFILES__} }; | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 29 | ▶ | return if !$load_css; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 27 | ▶ | ${$content} =~ s!(</head>)!$load_css$1! or ${$content} = $load_css . ${$content}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | 14 | }); | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 14 | 1; | |||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||||||