blib/lib/HTML/Strip/Whitespace.pm

filter: Statementstmt 12.9% 10 / 77 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 23.5% 4 / 17 Podpod n/a totaltotal 10.5% 14 / 133 SCAR 54.6
CC
17
Cov
9%
process
56.0
out
20.0
is_preserving_start_tag
12.0
CRAP 234.7
↑ Summary Build »
1 package HTML::Strip::Whitespace;
2 $HTML::Strip::Whitespace::VERSION = '0.2.2';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package HTML::Strip::Whitespace::State;
9 $HTML::Strip::Whitespace::State::VERSION = '0.2.2';
10 sub new
11 {
12 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
13 0 my $self = {};
14 0 bless $self, $class;
15 0 $self->initialize(@_);
16 0 return $self;
17 }
18  
19 sub to_array
20 {
21 0 ▶ my $v = shift;
Subroutine to_array: not called CC=2 SCAR=17.9
22 0 ▶ return ( ref($v) eq "ARRAY" ? (@$v) : $v );
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 0 0
23 }
24  
25 sub initialize
26 {
27 0 ▶ my $self = shift;
Subroutine initialize: not called CC=2 SCAR=17.9
28 0 my %args = (@_);
29 0 $self->{'prev'} = undef;
30 0 $self->{'next'} = undef;
31 0 $self->{'this'} = undef;
32 $self->{'parser'} =
33 0 HTML::TokeParser::Simple->new( to_array( $args{'parser_args'} ) );
34  
35 0 ▶ $self->{'strip_newlines'} = $args{'strip_newlines'} || 0;
Condition0%0/2
$args{'strip_newlines'} || 0
l!l
00
MC/DC0%0/1
$args{'strip_newlines'} || 0
$args{'strip_newlines'}
Truth table0%0/2
A
A= $args{'strip_newlines'}
Aresult
11
00
36 0 $self->{'out_fh'} = $args{'out_fh'};
37  
38 # Get the first element to initialize the parser
39 # Otherwise the first call to next_state would return undef;
40 0 $self->next_state();
41  
42 0 return 0;
43 }
44  
45 sub next_state
46 {
47 0 ▶ my $self = shift;
Subroutine next_state: not called CC=2 SCAR=17.9
48 ( $self->{'prev'}, $self->{'this'}, $self->{'next'} ) =
49 0 ( $self->{'this'}, $self->{'next'}, $self->{'parser'}->get_token() );
50 0 ▶ if ( !defined( $self->{'this'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"this"}) 0 0 0
51 {
52 0 return undef;
53 }
54 0 return 1;
55 }
56  
57 sub prev
58 {
59 0 ▶ my $self = shift;
Subroutine prev: not called CC=1 SCAR=6.9
60 0 return $self->{'prev'};
61 }
62  
63 sub next
64 {
65 0 ▶ my $self = shift;
Subroutine next: not called CC=1 SCAR=6.9
66 0 return $self->{'next'};
67 }
68  
69 sub this
70 {
71 0 ▶ my $self = shift;
Subroutine this: not called CC=1 SCAR=6.9
72 0 return $self->{'this'};
73 }
74  
75 sub text_strip
76 {
77 0 ▶ my $self = shift;
Subroutine text_strip: not called CC=2 SCAR=17.9
78  
79 # my $p = $self->prev();
80 # my $n = $self->next();
81  
82 0 my $text = $self->this()->as_is();
83  
84 0 ▶ $text =~ s{([\s\n]+)}{($1 =~ /\n/) ? "\n" : " "}eg;
Branch: 1 branch
BranchTrueFalseTotal
$1 =~ /\n/u ? : 0 0 0
85  
86 0 return $text;
87 }
88  
89 my %preserving_start_tags = ( 'pre' => 1, );
90  
91 sub is_preserving_start_tag
92 {
93 0 ▶ my $self = shift;
Subroutine is_preserving_start_tag: not called CC=3 SCAR=24.8
94 0 my $t = $self->this();
95 0 ▶ if ( $t->is_start_tag()
Branch: 1 branch
BranchTrueFalseTotal
if ($t->is_start_tag and exists $preserving_start_tags{$t->get_tag}) 0 0 0
Condition0%0/3
$t->is_start_tag and exists $preserving_start_tags{$t->get_tag}
!ll&&!rl&&r
000
MC/DC0%0/2
$t->is_start_tag and exists $preserving_start_tags{$t->get_tag}
$t->is_start_tag exists $preserving_start_tags{$t->get_tag}
Truth table0%0/3
A and B
A= $t->is_start_tagB= exists $preserving_start_tags{$t->get_tag}
ABresult
0X0
100
111
96 && exists( $preserving_start_tags{ $t->get_tag() } ) )
97 {
98 0 return $t->get_tag();
99 }
100 0 return undef;
101 }
102  
103 sub handle_text
104 {
105 0 ▶ my $state = shift;
Subroutine handle_text: not called CC=2 SCAR=17.9
106  
107 0 ▶ if ( $state->this->is_text() )
Branch: 1 branch
BranchTrueFalseTotal
if ($state->this->is_text) { } 0 0 0
108 {
109 0 $state->out( $state->text_strip() );
110 0 return 0;
111 }
112 else
113 {
114 0 return 1;
115 }
116 }
117  
118 sub out
119 {
120 0 ▶ my $self = shift;
Subroutine out: not called CC=4 SCAR=30.0
121 0 my $what = shift;
122 0 my $out_fh = $self->{'out_fh'};
123  
124 0 ▶ if ( ref($out_fh) eq "CODE" )
Branch: 3 branches
BranchTrueFalseTotal
if (ref $out_fh eq 'CODE') { } 0 0 0
elsif (ref $out_fh eq 'SCALAR') { } 0 0 0
elsif (ref $out_fh eq 'GLOB') { } 0 0 0
125 {
126 0 &{$out_fh}($what);
127 }
128 elsif ( ref($out_fh) eq "SCALAR" )
129 {
130 0 $$out_fh .= $what;
131 }
132 elsif ( ref($out_fh) eq "GLOB" )
133 {
134 0 print { *{$out_fh} } $what;
135 }
136  
137 0 return 0;
138 }
139  
140 sub out_this
141 {
142 0 ▶ my $state = shift;
Subroutine out_this: not called CC=1 SCAR=6.9
143 0 $state->out( $state->this()->as_is() );
144 }
145  
146 sub process
147 {
148 0 ▶ my $state = shift;
Subroutine process: not called CC=7 SCAR=40.3
149  
150 0 my $tag_type;
151  
152 0 ▶ while ( $state->next_state() )
Branch: 1 branch
BranchTrueFalseTotal
if ($state->next_state) 0 0 0
153 {
154 0 ▶ if ( !$state->handle_text() )
Branch: 2 branches
BranchTrueFalseTotal
if (not $state->handle_text) { } 0 0 0
elsif ($tag_type = $state->is_preserving_start_tag) { } 0 0 0
155 {
156 # Text was handled
157 }
158  
159 # If it's a preserving start tag, preserve all the text inside it.
160 # This is for example, a <pre> tag in which the spaces matter.
161 elsif ( $tag_type = $state->is_preserving_start_tag() )
162 {
163 0 my $do_once = 1;
164 0 ▶ while ( $do_once || $state->next_state() )
Branch: 1 branch
BranchTrueFalseTotal
if ($do_once or $state->next_state) 0 0 0
Condition0%0/3
$do_once or $state->next_state
l!l&&r!l&&!r
000
MC/DC0%0/2
$do_once or $state->next_state
$do_once $state->next_state
Truth table0%0/3
A or B
A= $do_onceB= $state->next_state
ABresult
1X1
011
000
165 {
166 0 $do_once = 0;
167 0 $state->out_this();
168 0 ▶ last if ( $state->this()->is_end_tag($tag_type) );
Branch: 1 branch
BranchTrueFalseTotal
if $state->this->is_end_tag($tag_type) 0 0 0
169 }
170 }
171 else
172 {
173 0 $state->out_this();
174 }
175 }
176  
177 # Return 0 on success.
178 0 return 0;
179 }
180  
181 package HTML::Strip::Whitespace;
182  
183 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 use strict;
185 use warnings;
186  
187 use HTML::TokeParser::Simple;
188  
189 use parent 'Exporter';
190 use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
191  
192 %EXPORT_TAGS = (
193 'all' => [
194 qw(
195 html_strip_whitespace
196 )
197 ]
198 );
199  
200 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
201  
202 @EXPORT = qw(
203  
204 );
205  
206 # Preloaded methods go here.
207  
208 sub html_strip_whitespace
209 {
210 my %args = (@_);
211 my $source = $args{'source'}
212 or die "source argument not specified.";
213 my $strip_newlines = $args{'strip_newlines'} || 0;
214 my $out_fh = $args{'out'}
215 or die "out argument not specified.";
216 my $state = HTML::Strip::Whitespace::State->new(
217 'parser_args' => $source,
218 'strip_newlines' => $strip_newlines,
219 'out_fh' => $out_fh,
220 );
221  
222 return $state->process();
223 }
224  
225 # Autoload methods go after =cut, and are processed by the autosplit program.
226  
227 1;
228  
229 __END__
↑ Summary Build »