File Coverage

blib/lib/Plack/Middleware/Text/Minify.pm
Criterion Covered Total %
statement 52 52 100.0
branch 21 24 87.5
condition 12 12 100.0
subroutine 10 10 100.0
pod 1 1 100.0
total 96 99 96.9


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   7853 use v5.9.3;
  8         25  
6              
7 8     8   37 use strict;
  8         14  
  8         137  
8 8     8   33 use warnings;
  8         14  
  8         189  
9              
10 8     8   38 use parent qw/ Plack::Middleware /;
  8         12  
  8         39  
11              
12 8     8   528 use Plack::Util;
  8         14  
  8         221  
13 8     8   44 use Plack::Util::Accessor qw/ path type /;
  8         15  
  8         59  
14 8     8   3654 use Ref::Util qw/ is_arrayref is_coderef /;
  8         10900  
  8         598  
15 8     8   2929 use Text::Minify::XS v0.6.2 ();
  8         3789  
  8         3317  
16              
17             # RECOMMEND PREREQ: Ref::Util::XS
18              
19             our $VERSION = 'v0.3.2';
20              
21             sub call {
22 19     19 1 225916 my ($self, $env) = @_;
23              
24 19         98 my $res = $self->app->($env);
25              
26 19 100       3730 return $res if $env->{'psgix.no-minify'};
27              
28 18         41 my $method = $env->{REQUEST_METHOD};
29 18 100       86 unless ($method =~ /^(GET|POST)$/) {
30 3         33 return $res;
31             }
32              
33 15 100       63 if (my $match = $self->path) {
34              
35 4         27 my $path = $env->{PATH_INFO};
36              
37 4 100 100     68 unless ( ( is_coderef($match) && $match->( $path, $env ) )
      100        
38             || ( $path =~ $match ) )
39             {
40 2         50 return $res;
41             }
42             }
43              
44             return Plack::Util::response_cb(
45             $res,
46             sub {
47 13     13   161 my ($res) = @_;
48              
49 13 50       69 return unless is_arrayref($res);
50              
51 13 50       56 return if @$res < 3;
52              
53 13 100       83 return if Plack::Util::status_with_no_entity_body( $res->[0] );
54              
55 11         129 my $type = Plack::Util::header_get( $res->[1], 'content-type' );
56 11 100       159 if ( my $match = $self->type ) {
57             return
58 4 100 100     39 unless ( ( is_coderef($match) && $match->( $type, $res ) )
      100        
59             || ( $type =~ $match ) );
60             }
61             else {
62 7 100       61 return unless $type =~ m{^text/};
63             }
64              
65 7         30 my $body = $res->[2];
66 7 100       19 if ( is_arrayref($body) ) { # no reason to call a function for each line in the body
67 6         42 $res->[2] = [ Text::Minify::XS::minify( join( "", @$body ) ) ];
68             }
69             else {
70 1         2 my $text = "";
71 1         4 Plack::Util::foreach( $body, sub { $text .= $_[0] } );
  1         110  
72 1         55 $res->[2] = [ Text::Minify::XS::minify($text) ];
73             }
74              
75 7 50       24 if (Plack::Util::header_exists( $res->[1], 'content-length' )) {
76 7         107 Plack::Util::header_set( $res->[1], 'content-length', length( $res->[2][0] ) );
77             }
78              
79 7         145 return;
80             }
81 13         118 );
82              
83             }
84              
85              
86             1;
87              
88             __END__