1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
package HTML::FormFu::OutputProcessor::StripWhitespace;
$HTML::FormFu::OutputProcessor::StripWhitespace::VERSION = '2.01';
use Moose;
use MooseX::Attribute::FormFuChained;
extends 'HTML::FormFu::OutputProcessor';
use HTML::FormFu::Constants qw( $EMPTY_STR );
use HTML::TokeParser::Simple;
use List::MoreUtils qw( any );
has collapse_tags => (
is => 'rw',
default => sub {
[ qw(
fieldset
form
hr
legend
optgroup
option
table
td
th
tr
) ];
},
lazy => 1,
traits => ['FormFuChained'],
);
has collapse_consecutive_tags => (
is => 'rw',
default => sub {
[ qw(
span
div
) ];
},
lazy => 1,
traits => ['FormFuChained'],
);
sub process {
my ( $self, $input ) = @_;
my $parser = HTML::TokeParser::Simple->new( \$input );
my @tokens;
while ( my $token = $parser->get_token ) {
push @tokens, $token;
}
my $iter
= HTML::FormFu::OutputProcessor::StripWhitespace::_iter->new(@tokens);
my @collapse = @{ $self->collapse_tags };
my @consecutive = @{ $self->collapse_consecutive_tags };
my $output = $EMPTY_STR;
while ( defined( my $token = $iter->next ) ) {
if ( $token->is_start_tag ) {
my $tag = $token->get_tag;
my $prev_tag = $iter->prev_tag_name;
if ( any { $tag eq $_ } @collapse ) {
# strip \s from before us
$output =~ s/ \s+ \z //x;
}
elsif ( defined $prev_tag ) {
# strip \s between <start> <start>
for my $consec (@consecutive) {
if ( $tag eq $consec && $tag eq $prev_tag ) {
$output =~ s/ \s+ \z //x;
}
}
}
}
elsif ( $token->is_end_tag ) {
my $tag = $token->get_tag;
my $prev_tag = $iter->prev_tag_name;
if ( any { $tag eq $_ } @collapse ) {
# strip \s from before us
$output =~ s/ \s+ \z //x;
}
elsif ( defined $prev_tag ) {
# strip \s between </end> </end>
for my $consec (@consecutive) {
if ( $tag eq $consec && $tag eq $prev_tag ) {
$output =~ s/ \s+ \z //x;
}
}
}
}
my $prev_tag = $iter->prev_tag_name;
if ( defined $prev_tag && any { $prev_tag eq $_ } @collapse ) {
$output =~ s/ \s+ \z //x;
my $part = $token->as_is;
$part =~ s/ ^ \s+ //x;
$output .= $part;
}
else {
$output .= $token->as_is;
}
}
return $output;
}
__PACKAGE__->meta->make_immutable;
package HTML::FormFu::OutputProcessor::StripWhitespace::_iter;
$HTML::FormFu::OutputProcessor::StripWhitespace::_iter::VERSION = '2.01';
use Moose;
use MooseX::Attribute::FormFuChained;
sub new {
my ( $class, @tags ) = @_;
my %self = (
tags => \@tags,
i => 0,
);
return bless \%self, $class;
}
sub next {
my ($self) = @_;
return $self->{tags}[ $self->{i}++ ];
}
sub prev_tag_name {
my ($self) = @_;
my $i = $self->{i} - 2;
while ( $i >= 0 ) {
if ( $self->{tags}[$i]->is_tag ) {
return if !$self->{tags}[$i]->is_tag;
return $self->{tags}[$i]->get_tag;
}
--$i;
}
}
__PACKAGE__->meta->make_immutable( inline_constructor => 0 );
1;
__END__
=head1 NAME
HTML::FormFu::OutputProcessor::StripWhitespace - Strip shitespace from HTML output
=head1 SYNOPSIS
---
output_processors:
- StripWhitespace
=head1 METHODS
=head2 collapse_tags
=head2 collapse_consecutive_tags
=head1 SEE ALSO
Is a sub-class of, and inherits methods from L<HTML::FormFu::OutputProcessor>
L<HTML::FormFu>
=head1 AUTHOR
Carl Franks C<cfranks@cpan.org>
=head1 LICENSE
This library is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|