| 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
| |||||||||||||||||||||||||||||
| 107 | 16 | foreach my $tag_name ( keys %{$rules} ) { | |||||||||||||||||||||||||||
| 108 | 17 | ▶ | if ( lc $tag_name ne $tag_name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 1 | croak "All tag names must be lower cased"; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 16 | ▶ | if ( reftype $rules->{$tag_name} eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 16 | my @attr_names; | |||||||||||||||||||||||||||
| 113 | 16 | foreach my $attr_item ( @{ $rules->{$tag_name} } ) { | |||||||||||||||||||||||||||
| 114 | 26 | ▶ | ref $attr_item eq 'HASH' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 137 | 160 | ▶ | print dump $attr if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { # relative URL | ||||||||||||||||||||||||||||
| 170 | delete $attr->{$source_type} | ||||||||||||||||||||||||||||
| 171 | 16 | unless grep { !defined } | |||||||||||||||||||||||||||
| 172 | 5 | ▶ | @{ $self->get_uri_schemes }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 190 | 2 | $value = $rule->($value); | |||||||||||||||||||||||||||
| 191 | next | ||||||||||||||||||||||||||||
| 192 | 2 | ▶ | if !defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 208 | my $value | ||||||||||||||||||||||||||||
| 209 | 22 | = encode_entities( $attr->{$attr_name} ); | |||||||||||||||||||||||||||
| 210 | 22 | ▶ | $more .= qq[ $attr_name="$value" ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
exists $attr->{"/"} and $attr->{"/"} eq "/"
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 "/"
| |||||||||||||||||||||||||||||
| 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 || ''
MC/DC100%1/1
$self->_processed || ''
$self->_processed
Truth table100%2/2
A
A= $self->_processed
| |||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | ▶ | elsif ( $tagname eq 'img' && $self->replace_img ) { | |||||||||||||||||||||||||||
|
Condition100%3/3
$tagname eq "img" and $self->replace_img
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
| |||||||||||||||||||||||||||||
| 228 | 4 | my $alt; | |||||||||||||||||||||||||||
| 229 | 4 | ▶ | if ( ref $self->replace_img ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 2 | $alt = $self->replace_img->( $tagname, $attr, $text ); | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | else { | ||||||||||||||||||||||||||||
| 233 | $alt | ||||||||||||||||||||||||||||
| 234 | 2 | ▶ | = defined( $attr->{alt} ) ? ": $attr->{alt}" : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 2 | $alt = "[IMAGE$alt]"; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | 4 | ▶ | $self->_processed( ( $self->_processed || q{} ) . $alt ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_processed || ''
MC/DC0%0/1
$self->_processed || ''
$self->_processed
Truth table50%1/2
A
A= $self->_processed
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 255 | 177 | ▶ | if ( any { $_ eq $tagname } keys %{ $self->get_rules } ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 256 | 159 | ▶ | $self->_processed( ( $self->_processed || q{} ) . $text ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_processed || ''
MC/DC0%0/1
$self->_processed || ''
$self->_processed
Truth table50%1/2
A
A= $self->_processed
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 270 | 180 | $text = _fix_text_encoding($text); | |||||||||||||||||||||||||||
| 271 | 180 | ▶ | if ( !@{ $self->_stripper_stack } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 178 | ▶ | $self->_processed( ( $self->_processed || q{} ) . $text ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->_processed || ''
MC/DC100%1/1
$self->_processed || ''
$self->_processed
Truth table100%2/2
A
A= $self->_processed
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 282 | 6 | ▶ | if ( $self->allow_comments ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 2 | ▶ | $self->_processed( ( $self->_processed || q{} ) . $text ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->_processed || ''
MC/DC100%1/1
$self->_processed || ''
$self->_processed
Truth table100%2/2
A
A= $self->_processed
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 293 | 3 | ▶ | if ( $self->allow_declaration ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 2 | ▶ | $self->_processed( ( $self->_processed || q{} ) . $text ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_processed || ''
MC/DC0%0/1
$self->_processed || ''
$self->_processed
Truth table50%1/2
A
A= $self->_processed
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 308 | 191 | ▶ | return $_[0] if !$_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 189 | my ($content) = @_; | |||||||||||||||||||||||||||
| 311 | 189 | ▶ | die 'content must be a string!' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->trim and $text
MC/DC100%2/2
$self->trim and $text
$self->trim
$text
Truth table100%3/3
A and B
A= $self->trimB= $text
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$tag eq $closing_tag and $found == 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 383 | : defined $2 ? "&$2" | ||||||||||||||||||||||||||||
| 384 | : "&" | ||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||