| 1 | package Plack::Middleware::Text::Minify; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: remove HTML indentation on the fly | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use v5.14; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use parent qw/ Plack::Middleware /; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Plack::Util::Accessor qw/ path type /; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Ref::Util qw/ is_arrayref is_coderef /; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use Text::Minify::XS v0.7.8 (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # RECOMMEND PREREQ: Ref::Util::XS | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | our $VERSION = 'v0.4.2'; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 19 | ▶ | sub call { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 19 | my ($self, $env) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 19 | my $res = $self->app->($env); | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 19 | ▶ | return $res if $env->{'psgix.no-minify'}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 18 | my $method = $env->{REQUEST_METHOD}; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 18 | ▶ | unless ($method =~ /^(GET|POST)$/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 15 | ▶ | if (my $match = $self->path) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | my $path = $env->{PATH_INFO}; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | unless ( ( is_coderef($match) && $match->( $path, $env ) ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
is_coderef($match) and &$match($path, $env) or $path =~ /$match/u
is_coderef($match) and &$match($path, $env)
MC/DC100%3/3
is_coderef($match) and &$match($path, $env) or $path =~ /$match/u
is_coderef($match)
&$match($path, $env)
$path =~ /$match/u
Truth table80%4/5
A and B or C
A= is_coderef($match)B= &$match($path, $env)C= $path =~ /$match/u
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | || ( $path =~ $match ) ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | return Plack::Util::response_cb( | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | $res, | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 13 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 13 | my ($res) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 13 | ▶ | return unless is_arrayref($res); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 13 | ▶ | return if @$res < 3; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 13 | ▶ | return if Plack::Util::status_with_no_entity_body( $res->[0] ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 11 | my $type = Plack::Util::header_get( $res->[1], 'content-type' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 11 | ▶ | if ( my $match = $self->type ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | return | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | ▶ | unless ( ( is_coderef($match) && $match->( $type, $res ) ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
is_coderef($match) and &$match($type, $res) or $type =~ /$match/u
is_coderef($match) and &$match($type, $res)
MC/DC100%3/3
is_coderef($match) and &$match($type, $res) or $type =~ /$match/u
is_coderef($match)
&$match($type, $res)
$type =~ /$match/u
Truth table80%4/5
A and B or C
A= is_coderef($match)B= &$match($type, $res)C= $type =~ /$match/u
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | || ( $type =~ $match ) ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 7 | ▶ | return unless $type =~ m{^text/}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 7 | my $body = $res->[2]; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | ▶ | if ( is_arrayref($body) ) { # no reason to call a function for each line in the body | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 6 | $res->[2] = [ Text::Minify::XS::minify( join( "", @$body ) ) ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | my $text = ""; | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | Plack::Util::foreach( $body, sub { $text .= $_[0] } ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | $res->[2] = [ Text::Minify::XS::minify($text) ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | ▶ | if (Plack::Util::header_exists( $res->[1], 'content-length' )) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | Plack::Util::header_set( $res->[1], 'content-length', length( $res->[2][0] ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 13 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||