blib/lib/HTML/Restrict.pm

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