blib/lib/Plack/Middleware/HTMLMinify.pm

filter: Statementstmt 77.5% 31 / 40 Branchbran 25.0% 2 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 2 / 2 totaltotal 69.7% 46 / 66 CC 6 SCAR 17.0
CC
6
Cov
67%
__ANON__
14.6
prepare_app
2.0
__ANON__
2.0
CRAP 5.5
↑ Summary
1 # ABSTRACT: Plack middleware to minify HTML on-the-fly
2  
3 package Plack::Middleware::HTMLMinify;
4 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 $Plack::Middleware::HTMLMinify::VERSION = '1.0.0';
6 }
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Plack::Middleware::HTMLMinify - Plack middleware for HTML minify
14  
15 =head1 VERSION
16  
17 version 1.0.0
18  
19 =head1 DESCRIPTION
20  
21 This module will use L<HTML::Packer> to minify HTML code on-the-fly
22 automatically. Currently it will check if Content-Type is C<text/html>.
23  
24 =head1 SYNOPSIS
25  
26 use Plack::Builder;
27 builder {
28 enable 'HTMLMinify', opt => {remove_newlines => 1};
29 }
30  
31 =cut
32  
33 3 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 ▶use HTML::Packer;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 3 ▶use Plack::Util::Accessor qw/opt packer/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 2 ▶sub call {
Subroutine call: called CC=1 SCAR=2.3
Pod documented
41 2 my ($self, $env) = @_;
42  
43 2 my $res = $self->app->($env);
44 2 ▶ Plack::Util::response_cb($res, sub {
Subroutine __ANON__: called CC=5 SCAR=26.8
45 2 my $res = shift;
46  
47 2 my $h = Plack::Util::headers($res->[1]);
48  
49 # Only process text/html.
50 2 my $ct = $h->get('Content-Type');
51 2 ▶ return unless defined $ct and $ct =~ qr{text/html};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ct and $ct =~ qr"text/html" 2 0 2
Condition33%1/3
defined $ct and $ct =~ qr"text/html"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $ct and $ct =~ qr"text/html"
defined $ct $ct =~ qr"text/html"
Truth table33%1/3
A and B
A= defined $ctB= $ct =~ qr"text/html"
ABresult
0X0
100
111
52  
53 # Don't touch compressed content.
54 0 ▶ return if defined $h->get('Content-Encoding');
Branch: 1 branch
BranchTrueFalseTotal
if defined $h->get("Content-Encoding") 0 0 0
55  
56 # Concat all content, and if response body is undefined then ignore it.
57 0 my $body = [];
58 0 ▶ Plack::Util::foreach($res->[2], sub { push @$body, $_[0]; });
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 $body = join '', @$body;
60  
61 # Minify and replace it.
62 0 ▶ $self->packer->minify(\$body, $self->opt) if '' ne $body;
Branch: 1 branch
BranchTrueFalseTotal
if "" ne $body 0 0 0
63 0 $res->[2] = [$body];
64 0 $h->set('Content-Length', length $body);
65  
66 0 return;
67 2 });
68 }
69  
70 2 ▶sub prepare_app {
Subroutine prepare_app: called CC=2 SCAR=7.0
Pod documented
71 2 my $self = shift;
72 2 my $packer = HTML::Packer->init;
73 2 $self->packer($packer);
74 2 ▶ $self->opt({remove_newlines => 1}) unless defined $self->opt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->opt 2 0 2
75 }
76  
77 =head1 AUTHOR
78  
79 Gea-Suan Lin, C<< <gslin at gslin.org> >>
80  
81 =head1 LICENSE AND COPYRIGHT
82  
83 Copyright 2011 Gea-Suan Lin.
84  
85 This software is released under 3-clause BSD license. See
86 L<http://www.opensource.org/licenses/bsd-license.php> for more
87 information.
88  
89 =cut
90  
91 3 1;
↑ Summary