File Coverage

blib/lib/Plack/Middleware/Text/Minify.pm
Criterion Covered Total %
statement 49 49 100.0
branch 21 24 87.5
condition 12 12 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 92 95 96.8


line stmt bran cond sub pod time code
1             package Plack::Middleware::Text::Minify;
2              
3             # ABSTRACT: remove HTML indentation on the fly
4              
5 8     8   4357680 use v5.14;
  8         32  
6 8     8   51 use warnings;
  8         39  
  8         476  
7              
8 8     8   43 use parent qw/ Plack::Middleware /;
  8         15  
  8         51  
9              
10 8     8   538 use Plack::Util;
  8         15  
  8         260  
11 8     8   37 use Plack::Util::Accessor qw/ path type /;
  8         12  
  8         61  
12 8     8   4367 use Ref::Util qw/ is_arrayref is_coderef /;
  8         19130  
  8         952  
13 8     8   3535 use Text::Minify::XS v0.7.8 ();
  8         5403  
  8         4200  
14              
15             # RECOMMEND PREREQ: Ref::Util::XS
16              
17             our $VERSION = 'v0.4.2';
18              
19             sub call {
20 19     19 1 286117 my ($self, $env) = @_;
21              
22 19         123 my $res = $self->app->($env);
23              
24 19 100       4285 return $res if $env->{'psgix.no-minify'};
25              
26 18         41 my $method = $env->{REQUEST_METHOD};
27 18 100       94 unless ($method =~ /^(GET|POST)$/) {
28 3         56 return $res;
29             }
30              
31 15 100       58 if (my $match = $self->path) {
32              
33 4         29 my $path = $env->{PATH_INFO};
34              
35 4 100 100     48 unless ( ( is_coderef($match) && $match->( $path, $env ) )
      100        
36             || ( $path =~ $match ) )
37             {
38 2         73 return $res;
39             }
40             }
41              
42             return Plack::Util::response_cb(
43             $res,
44             sub {
45 13     13   128 my ($res) = @_;
46              
47 13 50       37 return unless is_arrayref($res);
48              
49 13 50       35 return if @$res < 3;
50              
51 13 100       32 return if Plack::Util::status_with_no_entity_body( $res->[0] );
52              
53 11         93 my $type = Plack::Util::header_get( $res->[1], 'content-type' );
54 11 100       244 if ( my $match = $self->type ) {
55             return
56 4 100 100     45 unless ( ( is_coderef($match) && $match->( $type, $res ) )
      100        
57             || ( $type =~ $match ) );
58             }
59             else {
60 7 100       81 return unless $type =~ m{^text/};
61             }
62              
63 7         32 my $body = $res->[2];
64 7 100       19 if ( is_arrayref($body) ) { # no reason to call a function for each line in the body
65 6         48 $res->[2] = [ Text::Minify::XS::minify( join( "", @$body ) ) ];
66             }
67             else {
68 1         2 my $text = "";
69 1         5 Plack::Util::foreach( $body, sub { $text .= $_[0] } );
  1         67  
70 1         66 $res->[2] = [ Text::Minify::XS::minify($text) ];
71             }
72              
73 7 50       39 if (Plack::Util::header_exists( $res->[1], 'content-length' )) {
74 7         107 Plack::Util::header_set( $res->[1], 'content-length', length( $res->[2][0] ) );
75             }
76              
77 7         203 return;
78             }
79 13         121 );
80              
81             }
82              
83              
84             1;
85              
86             __END__