lib/HTML/FormFu/OutputProcessor/StripWhitespace.pm

filter: Statementstmt 100% 87 / 87 Branchbran 89.2% 25 / 28 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 18 / 18 Podpod 25.0% 1 / 4 totaltotal 94.7% 144 / 152 SCAR 30.0
CC
20
Cov
97%
process
17.0
prev_tag_name
4.0
__ANON__
1.0
CRAP 20.0
« lib/HTML/FormFu/QueryType/CGI/Simple.pm ↑ Summary lib/HTML/FormFu/Constraint/File/Size.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::OutputProcessor::StripWhitespace;
4 2 $HTML::FormFu::OutputProcessor::StripWhitespace::VERSION = '2.06';
5 # ABSTRACT: Strip shitespace from HTML output
6  
7 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 extends 'HTML::FormFu::OutputProcessor';
10  
11 2 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTML::TokeParser::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has collapse_tags => (
16 is => 'rw',
17 default => sub {
18 1 ▶ [ qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
19 fieldset
20 form
21 hr
22 legend
23 optgroup
24 option
25 table
26 td
27 th
28 tr
29 ) ];
30 },
31 2 lazy => 1,
32 traits => ['Chained'],
33 );
34  
35 has collapse_consecutive_tags => (
36 is => 'rw',
37 default => sub {
38 1 ▶ [ qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
39 span
40 div
41 ) ];
42 },
43 2 lazy => 1,
44 traits => ['Chained'],
45 );
46  
47 sub process {
48 27 ▶ my ( $self, $input ) = @_;
Subroutine process: called CC=17 SCAR=28.3
Pod undocumented
49  
50 27 my $parser = HTML::TokeParser::Simple->new( \$input );
51 27 my @tokens;
52  
53 27 ▶ while ( my $token = $parser->get_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $parser->get_token) 676 27 703
54 676 push @tokens, $token;
55 }
56  
57 27 my $iter
58 = HTML::FormFu::OutputProcessor::StripWhitespace::_iter->new(@tokens);
59  
60 27 my @collapse = @{ $self->collapse_tags };
61 27 my @consecutive = @{ $self->collapse_consecutive_tags };
62 27 my $output = $EMPTY_STR;
63  
64 27 ▶ while ( defined( my $token = $iter->next ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $token = $iter->next)) 676 27 703
65  
66 676 ▶ if ( $token->is_start_tag ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token->is_start_tag) { } 233 443 676
elsif ($token->is_end_tag) { } 182 261 443
67 233 my $tag = $token->get_tag;
68 233 my $prev_tag = $iter->prev_tag_name;
69  
70 233 ▶ if ( any { $tag eq $_ } @collapse ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $tag eq $_; } , @collapse)) { } 80 153 233
elsif (defined $prev_tag) { } 153 0 153
71  
72 # strip \s from before us
73 80 $output =~ s/ \s+ \z //x;
74 }
75 elsif ( defined $prev_tag ) {
76  
77 # strip \s between <start> <start>
78 153 for my $consec (@consecutive) {
79 306 ▶ if ( $tag eq $consec && $tag eq $prev_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq $consec and $tag eq $prev_tag) 29 277 306
Condition100%3/3
$tag eq $consec and $tag eq $prev_tag
!ll&&!rl&&r
2314629
MC/DC100%2/2
$tag eq $consec and $tag eq $prev_tag
$tag eq $consec $tag eq $prev_tag
Truth table100%3/3
A and B
A= $tag eq $consecB= $tag eq $prev_tag
ABresult
0X0
100
111
80 29 $output =~ s/ \s+ \z //x;
81 }
82 }
83 }
84 }
85 elsif ( $token->is_end_tag ) {
86 182 my $tag = $token->get_tag;
87 182 my $prev_tag = $iter->prev_tag_name;
88  
89 182 ▶ if ( any { $tag eq $_ } @collapse ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $tag eq $_; } , @collapse)) { } 77 105 182
elsif (defined $prev_tag) { } 105 0 105
90  
91 # strip \s from before us
92 77 $output =~ s/ \s+ \z //x;
93 }
94 elsif ( defined $prev_tag ) {
95  
96 # strip \s between </end> </end>
97 105 for my $consec (@consecutive) {
98 210 ▶ if ( $tag eq $consec && $tag eq $prev_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq $consec and $tag eq $prev_tag) 21 189 210
Condition100%3/3
$tag eq $consec and $tag eq $prev_tag
!ll&&!rl&&r
1355421
MC/DC100%2/2
$tag eq $consec and $tag eq $prev_tag
$tag eq $consec $tag eq $prev_tag
Truth table100%3/3
A and B
A= $tag eq $consecB= $tag eq $prev_tag
ABresult
0X0
100
111
99 21 $output =~ s/ \s+ \z //x;
100 }
101 }
102 }
103 }
104  
105 676 my $prev_tag = $iter->prev_tag_name;
106  
107 676 ▶ if ( defined $prev_tag && any { $prev_tag eq $_ } @collapse ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_tag and &any(sub { $prev_tag eq $_; } , @collapse)) { } 208 468 676
Condition66%2/3
defined $prev_tag and &any(sub {
!ll&&!rl&&r
0468208
MC/DC50%1/2
defined $prev_tag and &any(sub { $prev_tag eq $_; } , @collapse)
defined $prev_tag &any(sub { $prev_tag eq $_; } , @collapse)
Truth table66%2/3
A and B
A= defined $prev_tagB= &any(sub { $prev_tag eq $_; } , @collapse)
ABresult
0X0
100
111
108 208 $output =~ s/ \s+ \z //x;
109  
110 208 my $part = $token->as_is;
111  
112 208 $part =~ s/ ^ \s+ //x;
113  
114 208 $output .= $part;
115 }
116 else {
117 468 $output .= $token->as_is;
118 }
119 }
120  
121 27 return $output;
122 }
123  
124 2 __PACKAGE__->meta->make_immutable;
125  
126 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 package HTML::FormFu::OutputProcessor::StripWhitespace::_iter;
129 2 $HTML::FormFu::OutputProcessor::StripWhitespace::_iter::VERSION = '2.06';
130 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 2 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 sub new {
134 27 ▶ my ( $class, @tags ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
135  
136 27 my %self = (
137 tags => \@tags,
138 i => 0,
139 );
140  
141 27 return bless \%self, $class;
142 }
143  
144 sub next {
145 703 ▶ my ($self) = @_;
Subroutine next: called CC=1 SCAR=0.0
Pod undocumented
146  
147 703 return $self->{tags}[ $self->{i}++ ];
148 }
149  
150 sub prev_tag_name {
151 1091 ▶ my ($self) = @_;
Subroutine prev_tag_name: called CC=4 SCAR=13.9
Pod undocumented
152  
153 1091 my $i = $self->{i} - 2;
154  
155 1091 ▶ while ( $i >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 1557 54 1611
156  
157 1557 ▶ if ( $self->{tags}[$i]->is_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tags"}[$i]->is_tag) 1037 520 1557
158 1037 ▶ return if !$self->{tags}[$i]->is_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"tags"}[$i]->is_tag 0 1037 1037
159  
160 1037 return $self->{tags}[$i]->get_tag;
161 }
162  
163 520 --$i;
164 }
165 }
166  
167 2 __PACKAGE__->meta->make_immutable( inline_constructor => 0 );
168  
169 2 1;
170  
171 __END__
172  
173 =pod
174  
175 =encoding UTF-8
176  
177 =head1 NAME
178  
179 HTML::FormFu::OutputProcessor::StripWhitespace - Strip shitespace from HTML output
180  
181 =head1 VERSION
182  
183 version 2.06
184  
185 =head1 SYNOPSIS
186  
187 ---
188 output_processors:
189 - StripWhitespace
190  
191 =head1 METHODS
192  
193 =head2 collapse_tags
194  
195 =head2 collapse_consecutive_tags
196  
197 =head1 SEE ALSO
198  
199 Is a sub-class of, and inherits methods from L<HTML::FormFu::OutputProcessor>
200  
201 L<HTML::FormFu>
202  
203 =head1 AUTHOR
204  
205 Carl Franks C<cfranks@cpan.org>
206  
207 =head1 LICENSE
208  
209 This library is free software, you can redistribute it and/or modify it under
210 the same terms as Perl itself.
211  
212 =head1 AUTHOR
213  
214 Carl Franks <cpan@fireartist.com>
215  
216 =head1 COPYRIGHT AND LICENSE
217  
218 This software is copyright (c) 2018 by Carl Franks.
219  
220 This is free software; you can redistribute it and/or modify it under
221 the same terms as the Perl 5 programming language system itself.
222  
223 =cut
« lib/HTML/FormFu/QueryType/CGI/Simple.pm ↑ Summary lib/HTML/FormFu/Constraint/File/Size.pm »