File Coverage

blib/lib/Web/AssetLib/Util.pm
Criterion Covered Total %
statement 59 65 90.7
branch 10 20 50.0
condition n/a
subroutine 13 13 100.0
pod n/a
total 82 98 83.6


line stmt bran cond sub pod time code
1             package Web::AssetLib::Util;
2              
3 12     12   3728834 use Method::Signatures;
  12         48534  
  12         80  
4 12     12   3922 use Moose;
  12         280729  
  12         62  
5 12     12   47678 use Carp;
  12         17  
  12         530  
6 12     12   9460 use HTML::Element;
  12         221173  
  12         61  
7              
8 12     12   480 use v5.14;
  12         33  
9 12     12   51 no if $] >= 5.018, warnings => "experimental";
  12         13  
  12         130  
10              
11             my %FILE_TYPES = (
12             js => 'js',
13             javascript => 'js',
14             css => 'css',
15             stylesheet => 'css',
16             jpeg => 'jpg',
17             jpg => 'jpg'
18             );
19              
20             my %MIME_TYPES = (
21             js => 'text/javascript',
22             javascript => 'text/javascript',
23             css => 'text/css',
24             stylesheet => 'text/css',
25             jpg => 'image/jpeg',
26             jpeg => 'image/jpeg'
27             );
28              
29 12 50   12   1128187 func normalizeFileType ($type!) {
  35 50   35   93  
  35         41  
  35         61  
30 35 50       100 if ( my $normalized = $FILE_TYPES{$type} ) {
31 35         697 return $normalized;
32             }
33             else {
34 0         0 croak "could not map type '$type'";
35             }
36             }
37              
38 12 50   12   19585 func normalizeMimeType ($type!) {
  5 50   5   26  
  5         8  
  5         8  
39 5 50       16 if ( my $normalized = $MIME_TYPES{$type} ) {
40 5         8 return $normalized;
41             }
42             else {
43 0         0 croak "could not map type '$type'";
44             }
45             }
46              
47 12 50   12   46078 func generateHtmlTag (:$output!, :$html_attrs = {}) {
  5 50   5   46  
  5 50       15  
  5 50       7  
  5         7  
  5         14  
  5         6  
  5         13  
  5         10  
48 5         67 my $mime = normalizeMimeType( $output->type );
49 5         6 my $el;
50              
51 5         12 for ( ref($output) ) {
52 5         13 when (/Content/) {
53 2         4 for ($mime) {
54 2         7 when ('text/css') {
55 1         5 $el = HTML::Element->new(
56             'style',
57             type => $mime,
58             %$html_attrs
59             );
60 1         34 $el->push_content( $output->content );
61             }
62 1         3 when ('text/javascript') {
63 1         14 $el = HTML::Element->new(
64             'script',
65             type => $mime,
66             %$html_attrs
67             );
68 1         83 $el->push_content( $output->content );
69             }
70 0         0 when ('image/jpeg') {
71 0         0 croak "image/jpeg content output not supported";
72             }
73             }
74             }
75 3         5 when (/Link/) {
76 3         13 for ($mime) {
77 3         6 when ('text/css') {
78 1         14 $el = HTML::Element->new(
79             'link',
80             href => $output->src,
81             rel => 'stylesheet',
82             type => $mime,
83             %$html_attrs
84             );
85             }
86 2         3 when ('text/javascript') {
87 2         30 $el = HTML::Element->new(
88             'script',
89             src => $output->src,
90             type => $mime,
91             %$html_attrs
92             );
93             }
94 0         0 when ('image/jpeg') {
95 0         0 $el = HTML::Element->new(
96             'img',
97             src => $output->src,
98             %$html_attrs
99             );
100             }
101             }
102             }
103             }
104              
105 5         160 return $el->as_HTML;
106             }
107              
108 12     12   4618 no Moose;
  12         19  
  12         88  
109             1;
110              
111             =pod
112            
113             =encoding UTF-8
114            
115             =head1 NAME
116              
117             Web::AssetLib::Util - core utilties for Web::AssetLib
118              
119             =head1 FUNCTIONS
120              
121             =head2 normalizeFileType
122              
123             my $type = normalizeFileType( 'stylesheet' );
124             # $type = 'css'
125              
126             Converts file type string to a normalized version of that string.
127             e.g. "javascript" maps to "js"
128              
129             =head2 normalizeMimeType
130              
131             my $mime = normalizeMimeType( 'stylesheet' );
132             # $mime = 'text/css'
133              
134             Converts file type string to a mime type.
135             e.g. "javascript" maps to "text/javascript"
136              
137             =head2 generateHtmlTag
138              
139             my $output = ... # a Web::AssetLib::Output object
140              
141             my $tag = generateHtmlTag(
142             output => $output,
143             html_attrs => { async => 'async' }
144             );
145              
146             Generates an HTML tag for a L<Web::AssetLib::Output> object. Optionally,
147             C<html_attrs> can be provided.
148              
149             =head1 AUTHOR
150            
151             Ryan Lang <rlang@cpan.org>
152              
153             =cut