| 1 | package HTML::Scrubber; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Perl extension for scrubbing/sanitizing HTML | ||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use 5.008; # enforce minimum perl version of 5.8 | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use HTML::Parser 3.47 (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 16 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 16 | ▶ | use Scalar::Util ('weaken'); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 16 | ▶ | use List::Util 1.33 qw(any); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 16 | our ( @_scrub, @_scrub_fh ); | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 16 | our $VERSION = '0.19'; # VERSION | |||||||||||||||||||||||||||||
| 17 | 16 | our $AUTHORITY = 'cpan:NIGELM'; # AUTHORITY | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | # my my my my, these here to prevent foolishness like | ||||||||||||||||||||||||||||||
| 20 | # http://perlmonks.org/index.pl?node_id=251127#Stealing+Lexicals | ||||||||||||||||||||||||||||||
| 21 | 16 | (@_scrub) = ( \&_scrub, "self, event, tagname, attr, attrseq, text" ); | |||||||||||||||||||||||||||||
| 22 | 16 | (@_scrub_fh) = ( \&_scrub_fh, "self, event, tagname, attr, attrseq, text" ); | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 23 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 26 | 23 | my $package = shift; | |||||||||||||||||||||||||||||
| 27 | 23 | my $p = HTML::Parser->new( | |||||||||||||||||||||||||||||
| 28 | api_version => 3, | ||||||||||||||||||||||||||||||
| 29 | default_h => \@_scrub, | ||||||||||||||||||||||||||||||
| 30 | marked_sections => 0, | ||||||||||||||||||||||||||||||
| 31 | strict_comment => 0, | ||||||||||||||||||||||||||||||
| 32 | unbroken_text => 1, | ||||||||||||||||||||||||||||||
| 33 | case_sensitive => 0, | ||||||||||||||||||||||||||||||
| 34 | boolean_attribute_value => undef, | ||||||||||||||||||||||||||||||
| 35 | empty_element_tags => 1, | ||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||
| 38 | 23 | my $self = { | |||||||||||||||||||||||||||||
| 39 | _p => $p, | ||||||||||||||||||||||||||||||
| 40 | _rules => { '*' => 0, }, | ||||||||||||||||||||||||||||||
| 41 | _comment => 0, | ||||||||||||||||||||||||||||||
| 42 | _process => 0, | ||||||||||||||||||||||||||||||
| 43 | _r => "", | ||||||||||||||||||||||||||||||
| 44 | _optimize => 1, | ||||||||||||||||||||||||||||||
| 45 | _script => 0, | ||||||||||||||||||||||||||||||
| 46 | _style => 0, | ||||||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | 23 | $p->{"\0_s"} = bless $self, $package; | |||||||||||||||||||||||||||||
| 50 | 23 | weaken( $p->{"\0_s"} ); | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 23 | ▶ | return $self unless @_; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||
| 54 | 10 | my (%args) = @_; | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | 10 | for my $f (qw[ default allow deny rules process comment ]) { | |||||||||||||||||||||||||||||
| 57 | 60 | ▶ | next unless exists $args{$f}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 58 | 12 | ▶ | if ( ref $args{$f} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 59 | 10 | $self->$f( @{ $args{$f} } ); | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||||
| 62 | 2 | $self->$f( $args{$f} ); | |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 10 | return $self; | |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | 16 | ▶ | sub comment { | ||||||||||||||||||||||||||||
|
Subroutine
comment: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 71 | return $_[0]->{_comment} | ||||||||||||||||||||||||||||||
| 72 | 16 | ▶ | if @_ == 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 73 | 7 | $_[0]->{_comment} = $_[1]; | |||||||||||||||||||||||||||||
| 74 | 7 | return; | |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | 10 | ▶ | sub process { | ||||||||||||||||||||||||||||
|
Subroutine
process: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 79 | return $_[0]->{_process} | ||||||||||||||||||||||||||||||
| 80 | 10 | ▶ | if @_ == 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 81 | 1 | $_[0]->{_process} = $_[1]; | |||||||||||||||||||||||||||||
| 82 | 1 | return; | |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | sub script { | ||||||||||||||||||||||||||||
|
Subroutine
script: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 87 | return $_[0]->{_script} | ||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | if @_ == 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 89 | 1 | $_[0]->{_script} = $_[1]; | |||||||||||||||||||||||||||||
| 90 | 1 | return; | |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | sub style { | ||||||||||||||||||||||||||||
|
Subroutine
style: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 95 | return $_[0]->{_style} | ||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | if @_ == 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 97 | 1 | $_[0]->{_style} = $_[1]; | |||||||||||||||||||||||||||||
| 98 | 1 | return; | |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||
| 102 | 10 | ▶ | sub allow { | ||||||||||||||||||||||||||||
|
Subroutine
allow: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 103 | 10 | my $self = shift; | |||||||||||||||||||||||||||||
| 104 | 10 | for my $k (@_) { | |||||||||||||||||||||||||||||
| 105 | 43 | $self->{_rules}{ lc $k } = 1; | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||
| 107 | 10 | $self->{_optimize} = 1; # each time a rule changes, reoptimize when parse | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||
| 109 | 10 | return; | |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | sub deny { | ||||||||||||||||||||||||||||
|
Subroutine
deny: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 114 | 2 | my $self = shift; | |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | 2 | for my $k (@_) { | |||||||||||||||||||||||||||||
| 117 | 7 | $self->{_rules}{ lc $k } = 0; | |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | 2 | $self->{_optimize} = 1; # each time a rule changes, reoptimize when parse | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | 2 | return; | |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | 2 | ▶ | sub rules { | ||||||||||||||||||||||||||||
|
Subroutine
rules: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 127 | 2 | my $self = shift; | |||||||||||||||||||||||||||||
| 128 | 2 | my (%rules) = @_; | |||||||||||||||||||||||||||||
| 129 | 2 | for my $k ( keys %rules ) { | |||||||||||||||||||||||||||||
| 130 | 2 | $self->{_rules}{ lc $k } = $rules{$k}; | |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | 2 | $self->{_optimize} = 1; # each time a rule changes, reoptimize when parse | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 2 | return; | |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||
| 139 | 19 | ▶ | sub default { | ||||||||||||||||||||||||||||
|
Subroutine
default: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 140 | 19 | ▶ | return $_[0]->{_rules}{'*'} | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 141 | if @_ == 1; | ||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||
| 143 | 12 | ▶ | $_[0]->{_rules}{'*'} = $_[1] if defined $_[1]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 144 | 12 | ▶ | $_[0]->{_rules}{'_'} = $_[2] if defined $_[2] and ref $_[2]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_[2] and ref $_[2]
MC/DC50%1/2
defined $_[2] and ref $_[2]
defined $_[2]
ref $_[2]
Truth table66%2/3
A and B
A= defined $_[2]B= ref $_[2]
| |||||||||||||||||||||||||||||||
| 145 | 12 | $_[0]->{_optimize} = 1; # each time a rule changes, reoptimize when parse | |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||
| 147 | 12 | return; | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | 2 | ▶ | sub scrub_file { | ||||||||||||||||||||||||||||
|
Subroutine
scrub_file: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | if ( @_ > 2 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 153 | 2 | ▶ | return unless defined $_[0]->_out( $_[2] ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||
| 155 | else { | ||||||||||||||||||||||||||||||
| 156 | 0 | $_[0]->{_p}->handler( default => @_scrub ); | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||
| 159 | 2 | $_[0]->_optimize(); #if $_[0]->{_optimize}; | |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | 2 | $_[0]->{_p}->parse_file( $_[1] ); | |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | return delete $_[0]->{_r} unless exists $_[0]->{_out}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | print { $_[0]->{_out} } $_[0]->{_r} if length $_[0]->{_r}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 165 | 2 | delete $_[0]->{_out}; | |||||||||||||||||||||||||||||
| 166 | 2 | return 1; | |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||
| 170 | 63 | ▶ | sub scrub { | ||||||||||||||||||||||||||||
|
Subroutine
scrub: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 171 | 63 | ▶ | if ( @_ > 2 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 172 | 2 | ▶ | return unless defined $_[0]->_out( $_[2] ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||
| 174 | else { | ||||||||||||||||||||||||||||||
| 175 | 61 | $_[0]->{_p}->handler( default => @_scrub ); | |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||
| 178 | 63 | $_[0]->_optimize(); # if $_[0]->{_optimize}; | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | 63 | ▶ | $_[0]->{_p}->parse( $_[1] ) if defined( $_[1] ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 181 | 63 | $_[0]->{_p}->eof(); | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||
| 183 | 63 | ▶ | return delete $_[0]->{_r} unless exists $_[0]->{_out}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 184 | 2 | delete $_[0]->{_out}; | |||||||||||||||||||||||||||||
| 185 | 2 | return 1; | |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||
| 189 | 4 | ▶ | sub _out { | ||||||||||||||||||||||||||||
|
Subroutine
_out: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||
| 190 | 4 | my ( $self, $o ) = @_; | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||
| 192 | 4 | ▶ | unless ( ref $o and ref \$o ne 'GLOB' ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $o and ref \$o ne "GLOB"
MC/DC50%1/2
ref $o and ref \$o ne "GLOB"
ref $o
ref \$o ne "GLOB"
Truth table66%2/3
A and B
A= ref $oB= ref \$o ne "GLOB"
| |||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | open my $F, '>', $o or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 194 | 2 | binmode $F; | |||||||||||||||||||||||||||||
| 195 | 2 | $self->{_out} = $F; | |||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||||
| 198 | 2 | $self->{_out} = $o; | |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||
| 201 | 4 | $self->{_p}->handler( default => @_scrub_fh ); | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||
| 203 | 4 | return 1; | |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||
| 207 | 91 | ▶ | sub _validate { | ||||||||||||||||||||||||||||
|
Subroutine
_validate: called CC=15 SCAR=27.6
| |||||||||||||||||||||||||||||||
| 208 | 91 | my ( $s, $t, $r, $a, $as ) = @_; | |||||||||||||||||||||||||||||
| 209 | 91 | ▶ | return "<$t>" unless %$a; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||
| 211 | 41 | $r = $s->{_rules}->{$r}; | |||||||||||||||||||||||||||||
| 212 | 41 | my %f; | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||
| 214 | 41 | for my $k ( keys %$a ) { | |||||||||||||||||||||||||||||
| 215 | 61 | ▶ | my $check = exists $r->{$k} ? $r->{$k} : exists $r->{'*'} ? $r->{'*'} : next; | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||
| 217 | 53 | ▶ | if ( ref $check eq 'CODE' ) { | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 218 | 4 | my @v = $check->( $s, $t, $k, $a->{$k}, $a, \%f ); | |||||||||||||||||||||||||||||
| 219 | 4 | ▶ | next unless @v; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 220 | 3 | $f{$k} = shift @v; | |||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||
| 222 | ▶ | elsif ( ref $check || length($check) > 1 ) { | |||||||||||||||||||||||||||||
|
Condition33%1/3
ref $check or length $check > 1
MC/DC0%0/2
ref $check or length $check > 1
ref $check
length $check > 1
Truth table33%1/3
A or B
A= ref $checkB= length $check > 1
| |||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | $f{$k} = $a->{$k} if $a->{$k} =~ m{$check}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||
| 225 | elsif ($check) { | ||||||||||||||||||||||||||||||
| 226 | 47 | $f{$k} = $a->{$k}; | |||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||
| 230 | 41 | ▶ | if (%f) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 231 | 31 | my %seen; | |||||||||||||||||||||||||||||
| 232 | return "<$t $r>" | ||||||||||||||||||||||||||||||
| 233 | if $r = join ' ', map { | ||||||||||||||||||||||||||||||
| 234 | defined $f{$_} | ||||||||||||||||||||||||||||||
| 235 | 50 | ▶ | ? qq[$_="] . encode_entities( $f{$_} ) . q["] | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 236 | : $_; # boolean attribute (TODO?) | ||||||||||||||||||||||||||||||
| 237 | 31 | ▶ | } grep { exists $f{$_} and !$seen{$_}++; } @$as; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $f{$_} && !$seen{$_}++
MC/DC100%2/2
exists $f{$_} && !$seen{$_}++
exists $f{$_}
!$seen{$_}++
Truth table100%3/3
A && not(B)
A= exists $f{$_}B= !$seen{$_}++
| |||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||
| 240 | 10 | return "<$t>"; | |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||
| 244 | 504 | ▶ | sub _scrub_str { | ||||||||||||||||||||||||||||
|
Subroutine
_scrub_str: called CC=22 SCAR=30.9
| |||||||||||||||||||||||||||||||
| 245 | 504 | my ( $p, $e, $t, $a, $as, $text ) = @_; | |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||
| 247 | 504 | my $s = $p->{"\0_s"}; | |||||||||||||||||||||||||||||
| 248 | 504 | my $outstr = ''; | |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||
| 250 | 504 | ▶ | if ( $e eq 'start' ) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 251 | 109 | ▶ | if ( exists $s->{_rules}->{$t} ) # is there a specific rule | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 252 | { | ||||||||||||||||||||||||||||||
| 253 | 61 | ▶ | if ( ref $s->{_rules}->{$t} ) # is it complicated?(not simple;) | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 254 | { | ||||||||||||||||||||||||||||||
| 255 | 5 | $outstr .= $s->_validate( $t, $t, $a, $as ); | |||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||
| 257 | elsif ( $s->{_rules}->{$t} ) # validate using default attribute rule | ||||||||||||||||||||||||||||||
| 258 | { | ||||||||||||||||||||||||||||||
| 259 | 56 | $outstr .= $s->_validate( $t, '_', $a, $as ); | |||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||
| 262 | elsif ( $s->{_rules}->{'*'} ) # default allow tags | ||||||||||||||||||||||||||||||
| 263 | { | ||||||||||||||||||||||||||||||
| 264 | 30 | $outstr .= $s->_validate( $t, '_', $a, $as ); | |||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||
| 267 | elsif ( $e eq 'end' ) { | ||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||
| 269 | # empty tags list taken from | ||||||||||||||||||||||||||||||
| 270 | # https://developer.mozilla.org/en/docs/Glossary/empty_element | ||||||||||||||||||||||||||||||
| 271 | 79 | my @empty_tags = qw(area base br col embed hr img input link meta param source track wbr); | |||||||||||||||||||||||||||||
| 272 | 79 | ▶ | return "" if $text ne '' && any { $t eq $_ } @empty_tags; # skip false closing empty tags | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition100%3/3
$text ne "" and &any(sub {
$t eq $_;
}
, @empty_tags)
MC/DC100%2/2
$text ne "" and &any(sub {
$t eq $_;
}
, @empty_tags)
$text ne ""
&any(sub {
$t eq $_;
}
, @empty_tags)
Truth table100%3/3
A and B
A= $text ne ""B= &any(sub {
$t eq $_;
}
, @empty_tags)
| |||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||
| 274 | 63 | my $place = 0; | |||||||||||||||||||||||||||||
| 275 | 63 | ▶ | if ( exists $s->{_rules}->{$t} ) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 276 | 27 | ▶ | $place = 1 if $s->{_rules}->{$t}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||
| 278 | elsif ( $s->{_rules}->{'*'} ) { | ||||||||||||||||||||||||||||||
| 279 | 23 | $place = 1; | |||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||
| 281 | 63 | ▶ | if ($place) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 282 | 50 | ▶ | if ( length $text ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 283 | 46 | $outstr .= "</$t>"; | |||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||
| 285 | else { | ||||||||||||||||||||||||||||||
| 286 | 4 | substr $s->{_r}, -1, 0, ' /'; | |||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||
| 290 | elsif ( $e eq 'comment' ) { | ||||||||||||||||||||||||||||||
| 291 | 14 | ▶ | if ( $s->{_comment} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||
| 293 | # only copy comments through if they are well formed... | ||||||||||||||||||||||||||||||
| 294 | 6 | ▶ | $outstr .= $text if ( $text =~ m|^<!--.*-->$|ms ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||
| 297 | elsif ( $e eq 'process' ) { | ||||||||||||||||||||||||||||||
| 298 | 10 | ▶ | $outstr .= $text if $s->{_process}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||
| 300 | ▶ | elsif ( $e eq 'text' or $e eq 'default' ) { | |||||||||||||||||||||||||||||
|
Condition66%2/3
$e eq "text" or $e eq "default"
MC/DC50%1/2
$e eq "text" or $e eq "default"
$e eq "text"
$e eq "default"
Truth table66%2/3
A or B
A= $e eq "text"B= $e eq "default"
| |||||||||||||||||||||||||||||||
| 301 | 162 | $text =~ s/</</g; #https://rt.cpan.org/Public/Ticket/Attachment/83958/10332/scrubber.patch | |||||||||||||||||||||||||||||
| 302 | 162 | $text =~ s/>/>/g; | |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||
| 304 | 162 | $outstr .= $text; | |||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||
| 306 | elsif ( $e eq 'start_document' ) { | ||||||||||||||||||||||||||||||
| 307 | 65 | $outstr = ""; | |||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||
| 310 | 488 | return $outstr; | |||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||
| 314 | 38 | ▶ | sub _scrub_fh { | ||||||||||||||||||||||||||||
|
Subroutine
_scrub_fh: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 315 | 38 | my $self = $_[0]->{"\0_s"}; | |||||||||||||||||||||||||||||
| 316 | 38 | ▶ | print { $self->{_out} } $self->{'_r'} if length $self->{_r}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 317 | 38 | $self->{'_r'} = _scrub_str(@_); | |||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||
| 321 | 466 | ▶ | sub _scrub { | ||||||||||||||||||||||||||||
|
Subroutine
_scrub: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||
| 323 | 466 | $_[0]->{"\0_s"}->{_r} .= _scrub_str(@_); | |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||
| 326 | 65 | ▶ | sub _optimize { | ||||||||||||||||||||||||||||
|
Subroutine
_optimize: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||
| 327 | 65 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||
| 329 | 65 | my (@ignore_elements) = grep { not $self->{"_$_"} } qw(script style); | |||||||||||||||||||||||||||||
| 330 | 65 | $self->{_p}->ignore_elements(@ignore_elements); # if @ is empty, we reset ;) | |||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||
| 332 | 65 | ▶ | return unless $self->{_optimize}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||
| 334 | #sub allow | ||||||||||||||||||||||||||||||
| 335 | # return unless $self->{_optimize}; # till I figure it out (huh) | ||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||
| 337 | 26 | ▶ | if ( $self->{_rules}{'*'} ) { # default allow | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 338 | 8 | $self->{_p}->report_tags(); # so clear it | |||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||
| 340 | else { | ||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||
| 342 | my (@reports) = | ||||||||||||||||||||||||||||||
| 343 | grep { # report only tags we want | ||||||||||||||||||||||||||||||
| 344 | 76 | $self->{_rules}{$_} | |||||||||||||||||||||||||||||
| 345 | 18 | } keys %{ $self->{_rules} }; | |||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||
| 347 | $self->{_p}->report_tags( # default deny, so optimize | ||||||||||||||||||||||||||||||
| 348 | @reports | ||||||||||||||||||||||||||||||
| 349 | 18 | ▶ | ) if @reports; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||
| 352 | # sub deny | ||||||||||||||||||||||||||||||
| 353 | # return unless $self->{_optimize}; # till I figure it out (huh) | ||||||||||||||||||||||||||||||
| 354 | my (@ignores) = | ||||||||||||||||||||||||||||||
| 355 | 26 | grep { not $self->{_rules}{$_} } grep { $_ ne '*' } keys %{ $self->{_rules} }; | |||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||
| 357 | $self->{_p}->ignore_tags( # always ignore stuff we don't want | ||||||||||||||||||||||||||||||
| 358 | @ignores | ||||||||||||||||||||||||||||||
| 359 | 26 | ▶ | ) if @ignores; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||
| 361 | 26 | $self->{_optimize} = 0; | |||||||||||||||||||||||||||||
| 362 | 26 | return; | |||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||
| 365 | 16 | 1; | |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||
| 367 | #print sprintf q[ '%-12s => %s,], "$_'", $h{$_} for sort keys %h;# perl! | ||||||||||||||||||||||||||||||
| 368 | #perl -ne"chomp;print $_;print qq'\t\t# test ', ++$a if /ok\(/;print $/" test.pl >test2.pl | ||||||||||||||||||||||||||||||
| 369 | #perl -ne"chomp;print $_;if( /ok\(/ ){s/\#test \d+$//;print qq'\t\t# test ', ++$a }print $/" test.pl >test2.pl | ||||||||||||||||||||||||||||||
| 370 | #perl -ne"chomp;if(/ok\(/){s/# test .*$//;print$_,qq'\t\t# test ',++$a}else{print$_}print$/" test.pl >test2.pl | ||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||
| 372 | __END__ | ||||||||||||||||||||||||||||||