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   9583 use v5.14;
  8         31  
6 8     8   48 use warnings;
  8         16  
  8         234  
7              
8 8     8   47 use parent qw/ Plack::Middleware /;
  8         18  
  8         46  
9              
10 8     8   651 use Plack::Util;
  8         18  
  8         215  
11 8     8   53 use Plack::Util::Accessor qw/ path type /;
  8         29  
  8         75  
12 8     8   4514 use Ref::Util qw/ is_arrayref is_coderef /;
  8         12782  
  8         651  
13 8     8   3496 use Text::Minify::XS v0.6.2 ();
  8         4352  
  8         3992  
14              
15             # RECOMMEND PREREQ: Ref::Util::XS
16              
17             our $VERSION = 'v0.4.0';
18              
19             sub call {
20 19     19 1 272628 my ($self, $env) = @_;
21              
22 19         89 my $res = $self->app->($env);
23              
24 19 100       4566 return $res if $env->{'psgix.no-minify'};
25              
26 18         43 my $method = $env->{REQUEST_METHOD};
27 18 100       100 unless ($method =~ /^(GET|POST)$/) {
28 3         44 return $res;
29             }
30              
31 15 100       83 if (my $match = $self->path) {
32              
33 4         30 my $path = $env->{PATH_INFO};
34              
35 4 100 100     34 unless ( ( is_coderef($match) && $match->( $path, $env ) )
      100        
36             || ( $path =~ $match ) )
37             {
38 2         38 return $res;
39             }
40             }
41              
42             return Plack::Util::response_cb(
43             $res,
44             sub {
45 13     13   174 my ($res) = @_;
46              
47 13 50       58 return unless is_arrayref($res);
48              
49 13 50       84 return if @$res < 3;
50              
51 13 100       69 return if Plack::Util::status_with_no_entity_body( $res->[0] );
52              
53 11         188 my $type = Plack::Util::header_get( $res->[1], 'content-type' );
54 11 100       196 if ( my $match = $self->type ) {
55             return
56 4 100 100     60 unless ( ( is_coderef($match) && $match->( $type, $res ) )
      100        
57             || ( $type =~ $match ) );
58             }
59             else {
60 7 100       72 return unless $type =~ m{^text/};
61             }
62              
63 7         81 my $body = $res->[2];
64 7 100       27 if ( is_arrayref($body) ) { # no reason to call a function for each line in the body
65 6         58 $res->[2] = [ Text::Minify::XS::minify( join( "", @$body ) ) ];
66             }
67             else {
68 1         2 my $text = "";
69 1         6 Plack::Util::foreach( $body, sub { $text .= $_[0] } );
  1         141  
70 1         60 $res->[2] = [ Text::Minify::XS::minify($text) ];
71             }
72              
73 7 50       33 if (Plack::Util::header_exists( $res->[1], 'content-length' )) {
74 7         128 Plack::Util::header_set( $res->[1], 'content-length', length( $res->[2][0] ) );
75             }
76              
77 7         177 return;
78             }
79 13         153 );
80              
81             }
82              
83              
84             1;
85              
86             __END__