| 1 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::OutputProcessor::StripWhitespace; | ||||||||||||||||||||||||||||
| 4 | 4 | $HTML::FormFu::OutputProcessor::StripWhitespace::VERSION = '2.09'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: Strip shitespace from HTML output | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use MooseX::Attribute::Chained; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | extends 'HTML::FormFu::OutputProcessor'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use HTML::FormFu::Constants qw( $EMPTY_STR ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use HTML::TokeParser::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use List::Util 1.33 qw( any ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | has collapse_tags => ( | ||||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | [ qw( | |||||||||||||||||||||||||||
| 19 | fieldset | ||||||||||||||||||||||||||||
| 20 | form | ||||||||||||||||||||||||||||
| 21 | hr | ||||||||||||||||||||||||||||
| 22 | legend | ||||||||||||||||||||||||||||
| 23 | optgroup | ||||||||||||||||||||||||||||
| 24 | option | ||||||||||||||||||||||||||||
| 25 | table | ||||||||||||||||||||||||||||
| 26 | td | ||||||||||||||||||||||||||||
| 27 | th | ||||||||||||||||||||||||||||
| 28 | tr | ||||||||||||||||||||||||||||
| 29 | ) ]; | ||||||||||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||||||||||
| 31 | 4 | lazy => 1, | |||||||||||||||||||||||||||
| 32 | traits => ['Chained'], | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | has collapse_consecutive_tags => ( | ||||||||||||||||||||||||||||
| 36 | is => 'rw', | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | [ qw( | |||||||||||||||||||||||||||
| 39 | span | ||||||||||||||||||||||||||||
| 40 | div | ||||||||||||||||||||||||||||
| 41 | ) ]; | ||||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | 4 | lazy => 1, | |||||||||||||||||||||||||||
| 44 | traits => ['Chained'], | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 27 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=17 SCAR=28.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 27 | my ( $self, $input ) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 676 | ▶ | if ( $token->is_start_tag ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$tag eq $consec and $tag eq $prev_tag
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$tag eq $consec and $tag eq $prev_tag
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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $prev_tag and &any(sub {
$prev_tag eq $_;
}
, @collapse)
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)
| |||||||||||||||||||||||||||||
| 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 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | package HTML::FormFu::OutputProcessor::StripWhitespace::_iter; | ||||||||||||||||||||||||||||
| 129 | 4 | $HTML::FormFu::OutputProcessor::StripWhitespace::_iter::VERSION = '2.09'; | |||||||||||||||||||||||||||
| 130 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 4 | ▶ | use MooseX::Attribute::Chained; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 27 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 27 | my ( $class, @tags ) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 27 | my %self = ( | |||||||||||||||||||||||||||
| 137 | tags => \@tags, | ||||||||||||||||||||||||||||
| 138 | i => 0, | ||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 27 | return bless \%self, $class; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 703 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 703 | my ($self) = @_; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 703 | return $self->{tags}[ $self->{i}++ ]; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1091 | ▶ | sub prev_tag_name { | ||||||||||||||||||||||||||
|
Subroutine
prev_tag_name: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 151 | 1091 | my ($self) = @_; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1091 | my $i = $self->{i} - 2; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1091 | ▶ | while ( $i >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1557 | ▶ | if ( $self->{tags}[$i]->is_tag ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 1037 | ▶ | return if !$self->{tags}[$i]->is_tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1037 | return $self->{tags}[$i]->get_tag; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 520 | --$i; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 4 | 1; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||||||||||