blib/lib/HTML/Restrict.pm

filter: Statementstmt 99.4% 181 / 182 Branchbran 86.2% 69 / 80 Conditioncond 79.1% 19 / 24 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 100% 31 / 31 Podpod 100% 1 / 1 totaltotal 93.3% 310 / 332 SCAR 40.7
CC
58
Cov
94%
__ANON__
27.7
_build_parser
9.1
process
6.0
CRAP 58.7
↑ Summary
1 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 15 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package HTML::Restrict;
5  
6 15 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 our $VERSION = 'v3.0.0';
8  
9 15 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use HTML::Parser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use HTML::Entities qw( encode_entities );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Types::Standard 1.000001 qw[ Bool HashRef ArrayRef CodeRef ];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use List::Util 1.33 qw( any none );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Scalar::Util qw( reftype weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use Sub::Quote 'quote_sub';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 15 ▶use Moo 1.002000;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 15 has allow_comments => (
23 is => 'rw',
24 isa => Bool,
25 default => 0,
26 );
27  
28 15 has allow_declaration => (
29 is => 'rw',
30 isa => Bool,
31 default => 0,
32 );
33  
34 15 has debug => (
35 is => 'rw',
36 isa => Bool,
37 default => 0,
38 );
39  
40 15 has parser => (
41 is => 'ro',
42 lazy => 1,
43 builder => '_build_parser',
44 );
45  
46 15 has rules => (
47 is => 'rw',
48 isa => HashRef,
49 required => 0,
50 default => quote_sub(q{ {} }),
51 trigger => \&_build_parser,
52 reader => 'get_rules',
53 writer => 'set_rules',
54 );
55  
56 has strip_enclosed_content => (
57 is => 'rw',
58 isa => ArrayRef,
59 23 ▶ default => sub { [ 'script', 'style' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 15 );
61  
62 15 has replace_img => (
63 is => 'rw',
64 isa => Bool | CodeRef,
65 default => 0,
66 );
67  
68 15 has trim => (
69 is => 'rw',
70 isa => Bool,
71 default => 1,
72 );
73  
74 has uri_schemes => (
75 is => 'rw',
76 isa => ArrayRef,
77 required => 0,
78 22 ▶ default => sub { [ undef, 'http', 'https' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 15 reader => 'get_uri_schemes',
80 writer => 'set_uri_schemes',
81 );
82  
83 15 has _processed => (
84 is => 'rw',
85 isa => quote_sub(
86 q{
87 die "$_[0] is not false or a string!"
88 unless !defined($_[0]) || $_[0] eq "" || "$_[0]" eq '0' || ref(\$_[0]) eq 'SCALAR'
89 }
90 ),
91 clearer => '_clear_processed',
92 );
93  
94 has _stripper_stack => (
95 is => 'rw',
96 isa => ArrayRef,
97 25 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 15 );
99  
100 sub _build_parser {
101 38 ▶ my $self = shift;
Subroutine _build_parser: called CC=9 SCAR=22.0
102 38 my $rules = shift;
103  
104 # don't allow any upper case tag or attribute names
105 # these rules would otherwise silently be ignored
106 38 ▶ if ($rules) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules) 16 22 38
107 16 foreach my $tag_name ( keys %{$rules} ) {
108 17 ▶ if ( lc $tag_name ne $tag_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $tag_name ne $tag_name) 1 16 17
109 1 croak "All tag names must be lower cased";
110 }
111 16 ▶ if ( reftype $rules->{$tag_name} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($rules->{$tag_name}) eq "ARRAY") 16 0 16
112 16 my @attr_names;
113 16 foreach my $attr_item ( @{ $rules->{$tag_name} } ) {
114 26 ▶ ref $attr_item eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $attr_item eq 'HASH' ? : 7 19 26
115 ? push( @attr_names, keys(%$attr_item) )
116 : push( @attr_names, $attr_item );
117 }
118 16 for (@attr_names) {
119 27 ▶ croak "All attribute names must be lower cased"
Branch: 1 branch
BranchTrueFalseTotal
if lc $_ ne $_ 1 26 27
120 if lc $_ ne $_;
121 }
122 }
123 }
124 }
125  
126 36 weaken($self);
127 return HTML::Parser->new(
128 empty_element_tags => 1,
129  
130 start_h => [
131 sub {
132 182 ▶ my ( $p, $tagname, $attr, $text ) = @_;
Subroutine __ANON__: called CC=27 SCAR=33.2
133 182 ▶ print "starting tag: $tagname", "\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 182 182
134 182 my $more = q{};
135  
136 182 ▶ if ( any { $_ eq $tagname } keys %{ $self->get_rules } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 3 branches
BranchTrueFalseTotal
if (&any(sub { $_ eq $tagname; } , keys %{$self->get_rules;})) { } 160 22 182
elsif ($tagname eq 'img' and $self->replace_img) { } 4 18 22
elsif (&any(sub { $_ eq $tagname; } , @{$self->strip_enclosed_content;})) { } 4 14 18
137 160 ▶ print dump $attr if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 160 160
138  
139 160 foreach my $source_type ( 'href', 'src', 'cite' ) {
140  
141 480 my $link = $attr->{$source_type};
142  
143 # Remove unprintable ASCII control characters, which
144 # are 0..31. These characters are not valid in URLs,
145 # but they can prevent the URI parser from recognizing
146 # the scheme when they are used as leading characters.
147 # Browsers will helpfully ignore some of them, meaning
148 # that some of these characters (particularly 1..8 and
149 # 14..31) can be used to defeat HTML::Restrict when
150 # used as leading characters in a link. In our case we
151 # will strip them all regardless of where they are in
152 # the URL. See
153 # https://github.com/oalders/html-restrict/issues/30
154 # https://url.spec.whatwg.org/
155 # https://infra.spec.whatwg.org/#c0-control
156  
157 480 ▶ if ($link) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link) 152 328 480
158  
159 # C0 control chars (decimal 0..31)
160 # sort of like $link =~ s/[[:^print:]]//g
161 152 $link =~ s/[\00-\037]|&#x?0+;/ /g;
162  
163 152 my $url = URI->new($link);
164 152 ▶ if ( defined $url->scheme ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url->scheme) { } 147 5 152
165 delete $attr->{$source_type}
166 30 ▶ if none { $_ eq $url->scheme }
Subroutine __ANON__: called CC=1 SCAR=0.0
167 147 ▶ grep { defined } @{ $self->get_uri_schemes };
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $url->scheme; } , grep({defined $_;} @{$self->get_uri_schemes;})) 136 11 147
168 }
169 else { # relative URL
170 delete $attr->{$source_type}
171 16 unless grep { !defined }
172 5 ▶ @{ $self->get_uri_schemes };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {not defined $_;} @{$self->get_uri_schemes;} 1 4 5
173 }
174 }
175 }
176  
177 160 foreach
178 160 my $attr_item ( @{ $self->get_rules->{$tagname} } ) {
179 181 ▶ if ( ref $attr_item eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_item eq 'HASH') { } 13 168 181
180  
181 # validate or munge with regex or coderef contraints
182 #
183 13 for my $attr_name (
184 sort grep exists $attr->{$_},
185 keys %$attr_item
186 ) {
187 13 my $rule = $attr_item->{$attr_name};
188 13 my $value = $attr->{$attr_name};
189 13 ▶ if ( ref $rule eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rule eq 'CODE') { } 2 11 13
elsif ($value =~ /$rule/) { } 8 3 11
190 2 $value = $rule->($value);
191 next
192 2 ▶ if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1 2
193 }
194 elsif ( $value =~ $rule ) {
195  
196 # ok
197 }
198 else {
199 3 next;
200 }
201 9 $more .= qq[ $attr_name="]
202 . encode_entities($value) . q["];
203 }
204 }
205 else {
206 168 my $attr_name = $attr_item;
207 168 ▶ if ( exists $attr->{$attr_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attr->{$attr_name}) 22 146 168
208 my $value
209 22 = encode_entities( $attr->{$attr_name} );
210 22 ▶ $more .= qq[ $attr_name="$value" ]
Branch: 1 branch
BranchTrueFalseTotal
unless $attr_name eq "/" 22 0 22
211 unless $attr_name eq q{/};
212 }
213 }
214 }
215  
216 # closing slash should (naturally) close the tag
217 160 ▶ if ( exists $attr->{q{/}} && $attr->{q{/}} eq q{/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attr->{"/"} and $attr->{"/"} eq "/") 0 160 160
Condition33%1/3
exists $attr->{"/"} and $attr->{"/"} eq "/"
!ll&&!rl&&r
16000
MC/DC0%0/2
exists $attr->{"/"} and $attr->{"/"} eq "/"
exists $attr->{"/"} $attr->{"/"} eq "/"
Truth table33%1/3
A and B
A= exists $attr->{"/"}B= $attr->{"/"} eq "/"
ABresult
0X0
100
111
218 0 $more .= ' /';
219 }
220  
221 160 my $elem = "<$tagname $more>";
222 160 $elem =~ s{\s*>}{>}gxms;
223 160 $elem =~ s{\s+}{ }gxms;
224  
225 160 ▶ $self->_processed( ( $self->_processed || q{} ) . $elem );
Condition100%2/2
$self->_processed || ''
l!l
3157
MC/DC100%1/1
$self->_processed || ''
$self->_processed
Truth table100%2/2
A
A= $self->_processed
Aresult
11
00
226 }
227 ▶ elsif ( $tagname eq 'img' && $self->replace_img ) {
Condition100%3/3
$tagname eq "img" and $self->replace_img
!ll&&!rl&&r
1444
MC/DC100%2/2
$tagname eq "img" and $self->replace_img
$tagname eq "img" $self->replace_img
Truth table100%3/3
A and B
A= $tagname eq "img"B= $self->replace_img
ABresult
0X0
100
111
228 4 my $alt;
229 4 ▶ if ( ref $self->replace_img ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->replace_img) { } 2 2 4
230 2 $alt = $self->replace_img->( $tagname, $attr, $text );
231 }
232 else {
233 $alt
234 2 ▶ = defined( $attr->{alt} ) ? ": $attr->{alt}" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $attr->{'alt'} ? : 2 0 2
235 2 $alt = "[IMAGE$alt]";
236 }
237 4 ▶ $self->_processed( ( $self->_processed || q{} ) . $alt );
Condition50%1/2
$self->_processed || ''
l!l
04
MC/DC0%0/1
$self->_processed || ''
$self->_processed
Truth table50%1/2
A
A= $self->_processed
Aresult
11
00
238 }
239 elsif (
240 33 ▶ any { $_ eq $tagname }
Subroutine __ANON__: called CC=1 SCAR=0.0
241 18 @{ $self->strip_enclosed_content }
242 ) {
243 4 ▶ print "adding $tagname to strippers" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 4 4
244 4 push @{ $self->_stripper_stack }, $tagname;
245 }
246  
247 },
248 "self,tagname,attr,text"
249 ],
250  
251 end_h => [
252 sub {
253 177 ▶ my ( $p, $tagname, $attr, $text ) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.2
254 177 ▶ print "end: $text\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 177 177
255 177 ▶ if ( any { $_ eq $tagname } keys %{ $self->get_rules } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $_ eq $tagname; } , keys %{$self->get_rules;})) { } 159 18 177
elsif (&any(sub { $_ eq $tagname; } , @{$self->_stripper_stack;})) { } 3 15 18
256 159 ▶ $self->_processed( ( $self->_processed || q{} ) . $text );
Condition50%1/2
$self->_processed || ''
l!l
1590
MC/DC0%0/1
$self->_processed || ''
$self->_processed
Truth table50%1/2
A
A= $self->_processed
Aresult
11
00
257 }
258 18 ▶ elsif ( any { $_ eq $tagname } @{ $self->_stripper_stack } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
259 3 $self->_delete_tag_from_stack($tagname);
260 }
261  
262 },
263 "self,tagname,attr,text"
264 ],
265  
266 text_h => [
267 sub {
268 180 ▶ my ( $p, $text ) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
269 180 ▶ print "text: $text\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 2 178 180
270 180 $text = _fix_text_encoding($text);
271 180 ▶ if ( !@{ $self->_stripper_stack } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$self->_stripper_stack;}) 178 2 180
272 178 ▶ $self->_processed( ( $self->_processed || q{} ) . $text );
Condition100%2/2
$self->_processed || ''
l!l
16216
MC/DC100%1/1
$self->_processed || ''
$self->_processed
Truth table100%2/2
A
A= $self->_processed
Aresult
11
00
273 }
274 },
275 "self,text"
276 ],
277  
278 comment_h => [
279 sub {
280 6 ▶ my ( $p, $text ) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
281 6 ▶ print "comment: $text\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 6 6
282 6 ▶ if ( $self->allow_comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_comments) 2 4 6
283 2 ▶ $self->_processed( ( $self->_processed || q{} ) . $text );
Condition100%2/2
$self->_processed || ''
l!l
11
MC/DC100%1/1
$self->_processed || ''
$self->_processed
Truth table100%2/2
A
A= $self->_processed
Aresult
11
00
284 }
285 },
286 "self,text"
287 ],
288  
289 declaration_h => [
290 sub {
291 3 ▶ my ( $p, $text ) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.2
292 3 ▶ print "declaration: $text\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 3 0 3
293 3 ▶ if ( $self->allow_declaration ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->allow_declaration) 2 1 3
294 2 ▶ $self->_processed( ( $self->_processed || q{} ) . $text );
Condition50%1/2
$self->_processed || ''
l!l
02
MC/DC0%0/1
$self->_processed || ''
$self->_processed
Truth table50%1/2
A
A= $self->_processed
Aresult
11
00
295 }
296 },
297 36 "self,text"
298 ],
299  
300 );
301 }
302  
303 sub process {
304 192 ▶ my $self = shift;
Subroutine process: called CC=6 SCAR=17.9
Pod documented
305  
306 # returns undef if no value was passed
307 192 ▶ return if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 191 192
308 191 ▶ return $_[0] if !$_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 2 189 191
309  
310 189 my ($content) = @_;
311 189 ▶ die 'content must be a string!'
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$content eq "SCALAR" 0 189 189
312 unless ref( \$content ) eq 'SCALAR';
313 189 $self->_clear_processed;
314  
315 189 my $parser = $self->parser;
316 189 $parser->parse($content);
317 189 $parser->eof;
318  
319 189 my $text = $self->_processed;
320  
321 189 ▶ if ( $self->trim && $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trim and $text) 179 10 189
Condition100%3/3
$self->trim and $text
!ll&&!rl&&r
19179
MC/DC100%2/2
$self->trim and $text
$self->trim $text
Truth table100%3/3
A and B
A= $self->trimB= $text
ABresult
0X0
100
111
322 179 $text =~ s{\A\s*}{}gxms;
323 179 $text =~ s{\s*\z}{}gxms;
324 }
325 189 $self->_processed($text);
326  
327 # ensure stripper stack is reset in case of broken html
328 189 $self->_stripper_stack( [] );
329  
330 189 return $self->_processed;
331 }
332  
333 # strip_enclosed_content tags could be nested in the source HTML, so we
334 # maintain a stack of these tags.
335  
336 sub _delete_tag_from_stack {
337 4 ▶ my $self = shift;
Subroutine _delete_tag_from_stack: called CC=4 SCAR=13.9
338 4 my $closing_tag = shift;
339  
340 4 my $found = 0;
341 4 my @tag_list = ();
342  
343 4 foreach my $tag ( reverse @{ $self->_stripper_stack } ) {
344 7 ▶ if ( $tag eq $closing_tag && $found == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq $closing_tag and $found == 0) 4 3 7
Condition100%3/3
$tag eq $closing_tag and $found == 0
!ll&&!rl&&r
214
MC/DC100%2/2
$tag eq $closing_tag and $found == 0
$tag eq $closing_tag $found == 0
Truth table100%3/3
A and B
A= $tag eq $closing_tagB= $found == 0
ABresult
0X0
100
111
345 4 $found = 1;
346 4 next;
347 }
348 3 push @tag_list, $tag;
349 }
350  
351 4 $self->_stripper_stack( [ reverse @tag_list ] );
352  
353 4 return;
354 }
355  
356 # regex for entities that don't require a terminating semicolon
357 15 my ($short_entity_re)
358 = map qr/$_/i,
359 join '|',
360 '#x[0-9a-f]+',
361 '#[0-9]+',
362 grep !/;\z/,
363 sort keys %HTML::Entities::entity2char;
364  
365 # semicolon required
366 15 my ($complete_entity_re)
367 = map qr/$_/i,
368 join '|',
369 grep /;\z/,
370 sort keys %HTML::Entities::entity2char;
371  
372 sub _fix_text_encoding {
373 180 ▶ my $text = shift;
Subroutine _fix_text_encoding: called CC=3 SCAR=11.0
374 180 $text =~ s{
375 &
376 (?:
377 ($short_entity_re);?
378 |
379 ($complete_entity_re)
380 )?
381 }{
382 9 ▶ defined $1 ? "&$1;"
Branch: 2 branches
BranchTrueFalseTotal
defined $1 ? : 4 5 9
defined $2 ? : 1 4 5
383 : defined $2 ? "&$2"
384 : "&amp;"
385 }xgie;
386 180 return encode_entities( $text, '<>' );
387 }
388  
389 15 1; # End of HTML::Restrict
390  
391 # ABSTRACT: Strip unwanted HTML tags and attributes
392  
393 __END__
↑ Summary