| 1 | ######################################################## | ||||||||||||||||||||||||||||
| 2 | # Copyright © 2009 Six Apart, Ltd. | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package HTML::Laundry; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 16 | ▶ | use version; our $VERSION = 0.0105; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | HTML::Laundry - Perl module to clean HTML by the piece | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 VERSION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Version 0.0102 | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 23 | use strict; | ||||||||||||||||||||||||||||
| 24 | use HTML::Laundry; | ||||||||||||||||||||||||||||
| 25 | my $laundry = HTML::Laundry->new(); | ||||||||||||||||||||||||||||
| 26 | my $snippet = q{ | ||||||||||||||||||||||||||||
| 27 | <P STYLE="font-size: 300%"><BLINK>"You may get to touch her<BR> | ||||||||||||||||||||||||||||
| 28 | If your gloves are sterilized<BR></BR> | ||||||||||||||||||||||||||||
| 29 | Rinse your mouth with Listerine</BR> | ||||||||||||||||||||||||||||
| 30 | Blow disinfectant in her eyes"</BLINK><BR> | ||||||||||||||||||||||||||||
| 31 | -- X-Ray Spex, <I>Germ-Free Adolescents<I> | ||||||||||||||||||||||||||||
| 32 | <SCRIPT>alert('!!');</SCRIPT> | ||||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||
| 34 | my $germfree = $laundry->clean($snippet); | ||||||||||||||||||||||||||||
| 35 | # $germfree is now: | ||||||||||||||||||||||||||||
| 36 | # <p>"You may get to touch her<br /> | ||||||||||||||||||||||||||||
| 37 | # If your gloves are sterilized<br /> | ||||||||||||||||||||||||||||
| 38 | # Rinse your mouth with Listerine<br /> | ||||||||||||||||||||||||||||
| 39 | # Blow disinfectant in her eyes"<br /> | ||||||||||||||||||||||||||||
| 40 | # -- X-Ray Spex, <i>Germ-Free Adolescents</i></p> | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | HTML::Laundry is an L<HTML::Parser|HTML::Parser>-based HTML normalizer, | ||||||||||||||||||||||||||||
| 45 | meant for small pieces of HTML, such as user comments, Atom feed entries, | ||||||||||||||||||||||||||||
| 46 | and the like, rather than full pages. Laundry takes these and returns clean, | ||||||||||||||||||||||||||||
| 47 | sanitary, UTF-8-based XHTML. The parser's behavior may be changed with | ||||||||||||||||||||||||||||
| 48 | callbacks, and the whitelist of acceptable elements and attributes may be | ||||||||||||||||||||||||||||
| 49 | updated on the fly. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | A snippet is cleaned several ways: | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =over 4 | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =item * Normalized, using HTML::Parser: attributes and elements will be | ||||||||||||||||||||||||||||
| 56 | lowercased, empty elements such as <img /> and <br /> will be forced into | ||||||||||||||||||||||||||||
| 57 | the empty tag syntax if needed, and unknown attributes and elements will be | ||||||||||||||||||||||||||||
| 58 | stripped. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item * Sanitized, using an extensible whitelist of valid attributes and | ||||||||||||||||||||||||||||
| 61 | elements based on Mark Pilgrim and Aaron Swartz's work on C<sanitize.py>: tags | ||||||||||||||||||||||||||||
| 62 | and attributes which are known to be possible attack vectors are removed. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =item * Tidied, using L<HTML::Tidy|HTML::Tidy> or L<HTML::Tidy::libXML|HTML::Tidy::libXML> | ||||||||||||||||||||||||||||
| 65 | (as available): unclosed tags will be closed and the output generally | ||||||||||||||||||||||||||||
| 66 | neatened; future version may also use tidying to deal with character encoding | ||||||||||||||||||||||||||||
| 67 | issues. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item * Optionally rebased, to turn relative URLs in attributes into | ||||||||||||||||||||||||||||
| 70 | absolute ones. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =back | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | HTML::Laundry provides mechanisms to extend the list of known allowed | ||||||||||||||||||||||||||||
| 75 | (and disallowed) tags, along with callback methods to allow scripts using | ||||||||||||||||||||||||||||
| 76 | HTML::Laundry to extend the behavior in various ways. Future versions | ||||||||||||||||||||||||||||
| 77 | may provide additional options for altering the rules used to clean | ||||||||||||||||||||||||||||
| 78 | snippets. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Out of the box, HTML::Laundry does not currently know about the <head> tag | ||||||||||||||||||||||||||||
| 81 | and its children. For santizing full HTML pages, consider using L<HTML::Scrubber|HTML::Scrubber> | ||||||||||||||||||||||||||||
| 82 | or L<HTML::Defang|HTML::Defang>. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 16 | require HTML::Laundry::Rules; | |||||||||||||||||||||||||||
| 87 | 16 | require HTML::Laundry::Rules::Default; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 16 | require HTML::Parser; | |||||||||||||||||||||||||||
| 90 | 16 | ▶ | use HTML::Entities qw(encode_entities encode_entities_numeric); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 16 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 16 | ▶ | use URI::Escape qw(uri_unescape uri_escape uri_escape_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 16 | ▶ | use URI::Split qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | 16 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 16 | my @fragments; | |||||||||||||||||||||||||||
| 97 | my $unacceptable_count; | ||||||||||||||||||||||||||||
| 98 | 16 | my $local_unacceptable_count; | |||||||||||||||||||||||||||
| 99 | 16 | my $cdata_dirty; | |||||||||||||||||||||||||||
| 100 | 16 | my $in_cdata; | |||||||||||||||||||||||||||
| 101 | 16 | my $tag_leading_whitespace = qr/ | |||||||||||||||||||||||||||
| 102 | (?<=<) # Left bracket followed by | ||||||||||||||||||||||||||||
| 103 | \s* # any amount of whitespace | ||||||||||||||||||||||||||||
| 104 | (\/?) # optionally with a forward slash | ||||||||||||||||||||||||||||
| 105 | \s* # and then more whitespace | ||||||||||||||||||||||||||||
| 106 | /x; | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 new | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Create an HTML::Laundry object. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | my $l = HTML::Laundry->new(); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Takes an optional anonymous hash of arguments: | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =over 4 | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item * base_url | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | This turns relative URIs, as in C<<img src="surly_otter.png">>, into | ||||||||||||||||||||||||||||
| 123 | absolute URIs, as for use in feed parsing. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | my $l = HTML::Laundry->new({ base_uri => 'http://example.com/foo/' }); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item * notidy | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Disable use of HTML::Tidy or HTML::Tidy::libXML, even if | ||||||||||||||||||||||||||||
| 131 | they are available on your system. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | my $l = HTML::Laundry->new({ notidy => 1 }); | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub new { | ||||||||||||||||||||||||||||
| 140 | 25 | ▶ | my $self = {}; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 25 | my $class = shift; | |||||||||||||||||||||||||||
| 142 | 25 | my $args = shift; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 25 | ▶ | if ( blessed $args ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | if ( $args->isa('HTML::Laundry::Rules') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 1 | $args = { rules => $args }; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||
| 149 | 0 | $args = {}; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | elsif ( ref $args ne 'HASH' ) { | ||||||||||||||||||||||||||||
| 153 | 4 | my $rules; | |||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 4 | local $@; | |||||||||||||||||||||||||||
| 156 | 4 | eval { | |||||||||||||||||||||||||||
| 157 | 4 | ▶ | $args->isa('HTML::Laundry::Rules') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | and $rules = $args->new; | ||||||||||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 4 | ▶ | if ($rules) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 1 | $args = { rules => $args }; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||
| 165 | 3 | $args = {}; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 25 | $self->{tidy} = undef; | |||||||||||||||||||||||||||
| 170 | 25 | $self->{tidy_added_inline} = {}; | |||||||||||||||||||||||||||
| 171 | 25 | $self->{tidy_added_empty} = {}; | |||||||||||||||||||||||||||
| 172 | 25 | $self->{base_uri} = q{}; | |||||||||||||||||||||||||||
| 173 | 25 | bless $self, $class; | |||||||||||||||||||||||||||
| 174 | 25 | $self->clear_callback('start_tag'); | |||||||||||||||||||||||||||
| 175 | 25 | $self->clear_callback('end_tag'); | |||||||||||||||||||||||||||
| 176 | 25 | $self->clear_callback('uri'); | |||||||||||||||||||||||||||
| 177 | 25 | $self->clear_callback('text'); | |||||||||||||||||||||||||||
| 178 | 25 | $self->clear_callback('output'); | |||||||||||||||||||||||||||
| 179 | $self->{parser} = HTML::Parser->new( | ||||||||||||||||||||||||||||
| 180 | api_version => 3, | ||||||||||||||||||||||||||||
| 181 | utf8_mode => 1, | ||||||||||||||||||||||||||||
| 182 | 481 | ▶ | start_h => [ sub { $self->_tag_start_handler(@_) }, 'tagname,attr' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 183 | 453 | ▶ | end_h => [ sub { $self->_tag_end_handler(@_) }, 'tagname,attr' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 184 | 25 | ▶ | text_h => [ sub { $self->_text_handler(@_) }, 'dtext,is_cdata' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 185 | empty_element_tags => 1, | ||||||||||||||||||||||||||||
| 186 | marked_sections => 1, | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | $self->{cdata_parser} = HTML::Parser->new( | ||||||||||||||||||||||||||||
| 189 | api_version => 3, | ||||||||||||||||||||||||||||
| 190 | utf8_mode => 1, | ||||||||||||||||||||||||||||
| 191 | 5 | ▶ | start_h => [ sub { $self->_tag_start_handler(@_) }, 'tagname,attr' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 192 | 5 | ▶ | end_h => [ sub { $self->_tag_end_handler(@_) }, 'tagname,attr' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | 25 | ▶ | text_h => [ sub { $self->_text_handler(@_) }, 'dtext' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 194 | empty_element_tags => 1, | ||||||||||||||||||||||||||||
| 195 | unbroken_text => 1, | ||||||||||||||||||||||||||||
| 196 | marked_sections => 0, | ||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||
| 198 | 25 | $self->initialize($args); | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 25 | ▶ | if ( !$args->{notidy} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 8 | $self->_generate_tidy; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 25 | return $self; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head2 initialize | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Instantiates the Laundry object properties based on an | ||||||||||||||||||||||||||||
| 209 | HTML::Laundry::Rules module. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | sub initialize { | ||||||||||||||||||||||||||||
| 214 | 25 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
initialize: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # Set defaults | ||||||||||||||||||||||||||||
| 217 | 25 | $self->{tidy_added_tags} = undef; | |||||||||||||||||||||||||||
| 218 | 25 | $self->{tidy_empty_tags} = undef; | |||||||||||||||||||||||||||
| 219 | 25 | $self->{trim_trailing_whitespace} = 1; | |||||||||||||||||||||||||||
| 220 | 25 | $self->{trim_tag_whitespace} = 0; | |||||||||||||||||||||||||||
| 221 | $self->{base_uri} = URI->new( $args->{base_uri} ) | ||||||||||||||||||||||||||||
| 222 | 25 | ▶ | if $args->{base_uri}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 25 | my $rules = $args->{rules}; | |||||||||||||||||||||||||||
| 224 | 25 | ▶ | $rules ||= HTML::Laundry::Rules::Default->new(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$rules ||= "HTML::Laundry::Rules::Default"->new
MC/DC0%0/2
$rules ||= "HTML::Laundry::Rules::Default"->new
$rules
"HTML::Laundry::Rules::Default"->new
Truth table66%2/3
A ||= B
A= $rulesB= "HTML::Laundry::Rules::Default"->new
| |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 25 | $self->{ruleset} = $rules; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # Initialize based on ruleset | ||||||||||||||||||||||||||||
| 229 | 25 | $self->{acceptable_a} = $rules->acceptable_a(); | |||||||||||||||||||||||||||
| 230 | 25 | $self->{acceptable_e} = $rules->acceptable_e(); | |||||||||||||||||||||||||||
| 231 | 25 | $self->{empty_e} = $rules->empty_e(); | |||||||||||||||||||||||||||
| 232 | 25 | $self->{unacceptable_e} = $rules->unacceptable_e(); | |||||||||||||||||||||||||||
| 233 | 25 | $self->{uri_list} = $rules->uri_list(); | |||||||||||||||||||||||||||
| 234 | 25 | $self->{allowed_schemes} = $rules->allowed_schemes(); | |||||||||||||||||||||||||||
| 235 | 25 | $rules->finalize_initialization($self); | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 25 | return; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head2 add_callback | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Adds a callback of type "start_tag", "end_tag", "text", "uri", or "output" to | ||||||||||||||||||||||||||||
| 243 | the appropriate internal array. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | $l->add_callback('start_tag', sub { | ||||||||||||||||||||||||||||
| 246 | my ($laundry, $tagref, $attrhashref) = @_; | ||||||||||||||||||||||||||||
| 247 | # Now, perform actions and return | ||||||||||||||||||||||||||||
| 248 | }); | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | start_tag, end_tag, text, and uri callbacks that return false values will | ||||||||||||||||||||||||||||
| 251 | suppress the return value of the element they are processing; this allows | ||||||||||||||||||||||||||||
| 252 | additional checks to be done (for instance, images can be allowed only from | ||||||||||||||||||||||||||||
| 253 | whitelisted source domains). | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | sub add_callback { | ||||||||||||||||||||||||||||
| 258 | 21 | ▶ | my ( $self, $action, $ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_callback: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 259 | 21 | ▶ | return if ( ref($ref) ne 'CODE' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 21 | ▶ | if ($action eq q{start_tag}) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 261 | 4 | push @{ $self->{start_tag_callback} }, $ref; | |||||||||||||||||||||||||||
| 262 | } elsif ($action eq q{end_tag}) { | ||||||||||||||||||||||||||||
| 263 | 4 | push @{ $self->{end_tag_callback} }, $ref; | |||||||||||||||||||||||||||
| 264 | } elsif ($action eq q{text}) { | ||||||||||||||||||||||||||||
| 265 | 6 | push @{ $self->{text_callback} }, $ref; | |||||||||||||||||||||||||||
| 266 | } elsif ($action eq q{uri}) { | ||||||||||||||||||||||||||||
| 267 | 4 | push @{ $self->{uri_callback} }, $ref; | |||||||||||||||||||||||||||
| 268 | } elsif ($action eq q{output}) { | ||||||||||||||||||||||||||||
| 269 | 3 | push @{ $self->{output_callback} }, $ref; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | 21 | return; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head2 clear_callback | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | Removes all callbacks of given type. | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | $l->clear_callback('start_tag'); | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =cut | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | sub clear_callback { | ||||||||||||||||||||||||||||
| 283 | 139 | ▶ | my ( $self, $action ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear_callback: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 284 | 139 | ▶ | if ($action eq q{start_tag}) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 285 | 27 | ▶ | $self->{start_tag_callback} = [ sub { 1; } ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 286 | } elsif ($action eq q{end_tag}) { | ||||||||||||||||||||||||||||
| 287 | 27 | ▶ | $self->{end_tag_callback} = [ sub { 1; } ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 288 | } elsif ($action eq q{text}) { | ||||||||||||||||||||||||||||
| 289 | 29 | ▶ | $self->{text_callback} = [ sub { 1; } ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 290 | } elsif ($action eq q{uri}) { | ||||||||||||||||||||||||||||
| 291 | 29 | ▶ | $self->{uri_callback} = [ sub { 1; } ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 292 | } elsif ($action eq q{output}) { | ||||||||||||||||||||||||||||
| 293 | 27 | ▶ | $self->{output_callback} = [ sub { 1; } ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 139 | return; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =head2 clean | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | Cleans a snippet of HTML, using the ruleset and object creation options given | ||||||||||||||||||||||||||||
| 301 | to the Laundry object. The snippet should be passed as a scalar. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | $output1 = $l->clean( '<p>The X-rays were penetrating' ); | ||||||||||||||||||||||||||||
| 304 | $output2 = $l->clean( $snippet ); | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =cut | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | sub clean { | ||||||||||||||||||||||||||||
| 309 | 462 | ▶ | my ( $self, $chunk, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clean: called CC=7 SCAR=20.1
Pod
documented
| |||||||||||||||||||||||||||||
| 310 | 462 | $self->_reset_state(); | |||||||||||||||||||||||||||
| 311 | 462 | ▶ | if ( $self->{trim_tag_whitespace} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 0 | $chunk =~ s/$tag_leading_whitespace/$1/gs; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | 462 | my $p = $self->{parser}; | |||||||||||||||||||||||||||
| 315 | 462 | my $cp = $self->{cdata_parser}; | |||||||||||||||||||||||||||
| 316 | 462 | $p->parse($chunk); | |||||||||||||||||||||||||||
| 317 | 462 | ▶ | if ( !$in_cdata && !$unacceptable_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$in_cdata or $unacceptable_count
MC/DC50%1/2
$in_cdata or $unacceptable_count
$in_cdata
$unacceptable_count
Truth table66%2/3
A or B
A= $in_cdataB= $unacceptable_count
| |||||||||||||||||||||||||||||
| 318 | 461 | $p->eof(); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | 462 | ▶ | if ( $in_cdata && !$local_unacceptable_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$in_cdata and not $local_unacceptable_count
MC/DC0%0/2
$in_cdata and not $local_unacceptable_count
$in_cdata
not $local_unacceptable_count
Truth table33%1/3
A and not(B)
A= $in_cdataB= not $local_unacceptable_count
| |||||||||||||||||||||||||||||
| 321 | 0 | $cp->eof(); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | 462 | my $output = $self->gen_output; | |||||||||||||||||||||||||||
| 324 | 462 | $cp->eof(); # Clear buffer if we haven't already | |||||||||||||||||||||||||||
| 325 | 462 | ▶ | if ($cdata_dirty) { # Overkill to get out of CDATA parser state | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | $self->{parser} = HTML::Parser->new( | ||||||||||||||||||||||||||||
| 327 | api_version => 3, | ||||||||||||||||||||||||||||
| 328 | start_h => | ||||||||||||||||||||||||||||
| 329 | 7 | ▶ | [ sub { $self->_tag_start_handler(@_) }, 'tagname,attr' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 330 | 9 | ▶ | end_h => [ sub { $self->_tag_end_handler(@_) }, 'tagname,attr' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 331 | 4 | ▶ | text_h => [ sub { $self->_text_handler(@_) }, 'dtext,is_cdata' ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 332 | empty_element_tags => 1, | ||||||||||||||||||||||||||||
| 333 | marked_sections => 1, | ||||||||||||||||||||||||||||
| 334 | ); | ||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | else { | ||||||||||||||||||||||||||||
| 337 | 458 | $p->eof(); # Clear buffer if we haven't already | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | 462 | return $output; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =head2 base_uri | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | Used to get or set the base_uri property, used in URI rebasing. | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | my $base_uri = $l->base_uri; # returns current base_uri | ||||||||||||||||||||||||||||
| 347 | $l->base_uri(q{http://example.com}); # return 'http://example.com' | ||||||||||||||||||||||||||||
| 348 | $l->base_uri(''); # unsets base_uri | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =cut | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | sub base_uri { | ||||||||||||||||||||||||||||
| 353 | 3 | ▶ | my ( $self, $new_base ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
base_uri: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 354 | 3 | ▶ | if ( defined $new_base and !ref $new_base ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $new_base and not ref $new_base
MC/DC50%1/2
defined $new_base and not ref $new_base
defined $new_base
not ref $new_base
Truth table66%2/3
A and not(B)
A= defined $new_baseB= not ref $new_base
| |||||||||||||||||||||||||||||
| 355 | 2 | $self->{base_uri} = $new_base; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | 3 | return $self->{base_uri}; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | sub _run_callbacks { | ||||||||||||||||||||||||||||
| 361 | 1623 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_run_callbacks: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 362 | 1623 | my $action = shift; | |||||||||||||||||||||||||||
| 363 | 1623 | ▶ | return unless $action; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | 1623 | my $type = $action . q{_callback}; | |||||||||||||||||||||||||||
| 365 | 1623 | for my $callback ( @{ $self->{$type} } ) { | |||||||||||||||||||||||||||
| 366 | 1650 | my $result = $callback->( $self, @_ ); | |||||||||||||||||||||||||||
| 367 | 1650 | ▶ | return unless $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | 1619 | return 1; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =head2 gen_output | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | Used to generate the final, XHTML output from the internal stack of text and | ||||||||||||||||||||||||||||
| 375 | tag tokens. Generally meant to be used internally, but potentially useful for | ||||||||||||||||||||||||||||
| 376 | callbacks that require a snapshot of what the output would look like | ||||||||||||||||||||||||||||
| 377 | before the cleaning process is complete. | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | my $xhtml = $l->gen_output; | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =cut | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | sub gen_output { | ||||||||||||||||||||||||||||
| 384 | 462 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
gen_output: called CC=6 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||
| 385 | 462 | ▶ | if ( !$self->_run_callbacks( q{output}, \@fragments ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 0 | return q{}; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | 462 | my $output = join '', @fragments; | |||||||||||||||||||||||||||
| 389 | 462 | ▶ | if ( $self->{tidy} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | if ( $self->{tidy_engine} eq q{HTML::Tidy} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 391 | 0 | $output = $self->{tidy}->clean($output); | |||||||||||||||||||||||||||
| 392 | 0 | $self->{tidy}->clear_messages; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | elsif ( $self->{tidy_engine} eq q{HTML::Tidy::libXML} ) { | ||||||||||||||||||||||||||||
| 395 | my $clean | ||||||||||||||||||||||||||||
| 396 | = $self->{tidy} | ||||||||||||||||||||||||||||
| 397 | ->clean( $self->{tidy_head} . $output . $self->{tidy_foot}, | ||||||||||||||||||||||||||||
| 398 | 0 | 'UTF-8', 1 ); | |||||||||||||||||||||||||||
| 399 | 0 | $output = substr( $clean, length $self->{tidy_head} ); | |||||||||||||||||||||||||||
| 400 | 0 | $output = substr( $output, 0, -1 * length $self->{tidy_foot} ); | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | 462 | ▶ | if ( $self->{trim_trailing_whitespace} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | 462 | $output =~ s/\s+$//; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | 462 | return $output; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =head2 empty_elements | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | Returns a list of the Laundry object's known empty elements: elements such | ||||||||||||||||||||||||||||
| 412 | as <img /> or <br /> which must not contain any children. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =cut | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | sub empty_elements { | ||||||||||||||||||||||||||||
| 417 | 0 | ▶ | my ( $self, $listref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
empty_elements: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 418 | 0 | ▶ | if ($listref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 0 | my @list = @{$listref}; | |||||||||||||||||||||||||||
| 420 | 0 | my %empty = map { ( $_, 1 ) } @list; | |||||||||||||||||||||||||||
| 421 | 0 | $self->{empty_e} = \%empty; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | 0 | return keys %{ $self->{empty_e} }; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =head2 remove_empty_element | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | Removes an element (or, if given an array reference, multiple elements) from | ||||||||||||||||||||||||||||
| 429 | the "empty elements" list maintained by the Laundry object. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | $l->remove_empty_element(['img', 'br']); # Let's break XHTML! | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | This will not affect the acceptable/unacceptable status of the elements. | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =cut | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | sub remove_empty_element { | ||||||||||||||||||||||||||||
| 438 | 4 | ▶ | my ( $self, $new_e, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove_empty_element: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 439 | 4 | my $empty = $self->{empty_e}; | |||||||||||||||||||||||||||
| 440 | 4 | ▶ | if ( ref($new_e) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 441 | 1 | foreach my $e ( @{$new_e} ) { | |||||||||||||||||||||||||||
| 442 | 2 | $self->remove_empty_element( $e, $args ); | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | else { | ||||||||||||||||||||||||||||
| 446 | 3 | delete $empty->{$new_e}; | |||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | 4 | return 1; | |||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =head2 acceptable_elements | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Returns a list of the Laundry object's known acceptable elements, which will | ||||||||||||||||||||||||||||
| 454 | not be stripped during the sanitizing process. | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =cut | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | sub acceptable_elements { | ||||||||||||||||||||||||||||
| 459 | 4 | ▶ | my ( $self, $listref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
acceptable_elements: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 460 | 4 | ▶ | if ( ref($listref) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 461 | 1 | my @list = @{$listref}; | |||||||||||||||||||||||||||
| 462 | 1 | my %acceptable = map { ( $_, 1 ) } @list; | |||||||||||||||||||||||||||
| 463 | 1 | $self->{acceptable_e} = \%acceptable; | |||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||
| 465 | 4 | return keys %{ $self->{acceptable_e} }; | |||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =head2 add_acceptable_element | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | Adds an element (or, if given an array reference, multiple elements) to the | ||||||||||||||||||||||||||||
| 471 | "acceptable elements" list maintained by the Laundry object. Items added in | ||||||||||||||||||||||||||||
| 472 | this manner will automatically be removed from the "unacceptable elements" | ||||||||||||||||||||||||||||
| 473 | list if they are present. | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | $l->add_acceptable_element('style'); | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | Elements which are empty may be flagged as such with an optional argument. | ||||||||||||||||||||||||||||
| 478 | If this flag is set, all elements provided by the call will be added to | ||||||||||||||||||||||||||||
| 479 | the "empty element" list. | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | $l->add_acceptable_element(['applet', 'script'], { empty => 1 }); | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =cut | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | sub add_acceptable_element { | ||||||||||||||||||||||||||||
| 486 | 10 | ▶ | my ( $self, $new_e, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_acceptable_element: called CC=6 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||
| 487 | 10 | my $acceptable = $self->{acceptable_e}; | |||||||||||||||||||||||||||
| 488 | 10 | my $empty = $self->{empty_e}; | |||||||||||||||||||||||||||
| 489 | 10 | my $unacceptable = $self->{unacceptable_e}; | |||||||||||||||||||||||||||
| 490 | 10 | ▶ | if ( ref($new_e) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | 2 | foreach my $e ( @{$new_e} ) { | |||||||||||||||||||||||||||
| 492 | 4 | $self->add_acceptable_element( $e, $args ); | |||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | else { | ||||||||||||||||||||||||||||
| 496 | 8 | $acceptable->{$new_e} = 1; | |||||||||||||||||||||||||||
| 497 | 8 | ▶ | if ( $args->{empty} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 498 | 4 | $empty->{$new_e} = 1; | |||||||||||||||||||||||||||
| 499 | 4 | ▶ | if ( $self->{tidy} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 500 | 0 | $self->{tidy_added_inline}->{$new_e} = 1; | |||||||||||||||||||||||||||
| 501 | 0 | $self->{tidy_added_empty}->{$new_e} = 1; | |||||||||||||||||||||||||||
| 502 | 0 | $self->_generate_tidy; | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | elsif ( $self->{tidy} ) { | ||||||||||||||||||||||||||||
| 506 | 0 | $self->{tidy_added_inline}->{$new_e} = 1; | |||||||||||||||||||||||||||
| 507 | 0 | $self->_generate_tidy; | |||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||
| 509 | 8 | delete $unacceptable->{$new_e}; | |||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||
| 512 | 10 | return 1; | |||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | =head2 remove_acceptable_element | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | Removes an element (or, if given an array reference, multiple elements) to the | ||||||||||||||||||||||||||||
| 518 | "acceptable elements" list maintained by the Laundry object. These items | ||||||||||||||||||||||||||||
| 519 | (although not their child elements) will now be stripped during parsing. | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | $l->remove_acceptable_element(['img', 'h1', 'h2']); | ||||||||||||||||||||||||||||
| 522 | $l->clean(q{<h1>The Day the World Turned Day-Glo</h1>}); | ||||||||||||||||||||||||||||
| 523 | # returns 'The Day the World Turned Day-Glo' | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =cut | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | sub remove_acceptable_element { | ||||||||||||||||||||||||||||
| 528 | 16 | ▶ | my ( $self, $new_e, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove_acceptable_element: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 529 | 16 | my $acceptable = $self->{acceptable_e}; | |||||||||||||||||||||||||||
| 530 | 16 | ▶ | if ( ref($new_e) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | 2 | foreach my $e ( @{$new_e} ) { | |||||||||||||||||||||||||||
| 532 | 4 | $self->remove_acceptable_element( $e, $args ); | |||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||
| 535 | else { | ||||||||||||||||||||||||||||
| 536 | 14 | delete $acceptable->{$new_e}; | |||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||
| 538 | 16 | return 1; | |||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | =head2 unacceptable_elements | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | Returns a list of the Laundry object's unacceptable elements, which will be | ||||||||||||||||||||||||||||
| 544 | stripped -- B<including> child objects -- during the cleaning process. | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | =cut | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | sub unacceptable_elements { | ||||||||||||||||||||||||||||
| 549 | 3 | ▶ | my ( $self, $listref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
unacceptable_elements: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 550 | 3 | ▶ | if ( ref($listref) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 551 | 1 | my @list = @{$listref}; | |||||||||||||||||||||||||||
| 552 | my %unacceptable | ||||||||||||||||||||||||||||
| 553 | 1 | = map { $self->remove_acceptable_element($_); ( $_, 1 ); } @list; | |||||||||||||||||||||||||||
| 554 | 1 | $self->{unacceptable_e} = \%unacceptable; | |||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||
| 556 | 3 | return keys %{ $self->{unacceptable_e} }; | |||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | =head2 add_unacceptable_element | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | Adds an element (or, if given an array reference, multiple elements) to the | ||||||||||||||||||||||||||||
| 562 | "unacceptable elements" list maintained by the Laundry object. | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | $l->add_unacceptable_element(['h1', 'h2']); | ||||||||||||||||||||||||||||
| 565 | $l->clean(q{<h1>The Day the World Turned Day-Glo</h1>}); | ||||||||||||||||||||||||||||
| 566 | # returns null string | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =cut | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | sub add_unacceptable_element { | ||||||||||||||||||||||||||||
| 571 | 4 | ▶ | my ( $self, $new_e, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_unacceptable_element: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 572 | 4 | my $unacceptable = $self->{unacceptable_e}; | |||||||||||||||||||||||||||
| 573 | 4 | ▶ | if ( ref($new_e) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | 1 | foreach my $e ( @{$new_e} ) { | |||||||||||||||||||||||||||
| 575 | 2 | $self->add_unacceptable_element( $e, $args ); | |||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||
| 578 | else { | ||||||||||||||||||||||||||||
| 579 | 3 | $self->remove_acceptable_element($new_e); | |||||||||||||||||||||||||||
| 580 | 3 | $unacceptable->{$new_e} = 1; | |||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||
| 582 | 4 | return 1; | |||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | =head2 remove_unacceptable_element | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | Removes an element (or, if given an array reference, multiple elements) from | ||||||||||||||||||||||||||||
| 588 | the "unacceptable elements" list maintained by the Laundry object. Note that | ||||||||||||||||||||||||||||
| 589 | this does not automatically add the element to the acceptable_element list. | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | $l->clean(q{<script>alert('!')</script>}); | ||||||||||||||||||||||||||||
| 592 | # returns null string | ||||||||||||||||||||||||||||
| 593 | $l->remove_unacceptable_element( q{script} ); | ||||||||||||||||||||||||||||
| 594 | $l->clean(q{<script>alert('!')</script>}); | ||||||||||||||||||||||||||||
| 595 | # returns "alert('!')" | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | =cut | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | sub remove_unacceptable_element { | ||||||||||||||||||||||||||||
| 600 | 4 | ▶ | my ( $self, $new_e, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove_unacceptable_element: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 601 | 4 | my $unacceptable = $self->{unacceptable_e}; | |||||||||||||||||||||||||||
| 602 | 4 | ▶ | if ( ref($new_e) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 603 | 1 | foreach my $a ( @{$new_e} ) { | |||||||||||||||||||||||||||
| 604 | 2 | $self->remove_unacceptable_element( $a, $args ); | |||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||
| 607 | else { | ||||||||||||||||||||||||||||
| 608 | 3 | delete $unacceptable->{$new_e}; | |||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||
| 610 | 4 | return 1; | |||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | =head2 acceptable_attributes | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | Returns a list of the Laundry object's known acceptable attributes, which will | ||||||||||||||||||||||||||||
| 616 | not be stripped during the sanitizing process. | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =cut | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | sub acceptable_attributes { | ||||||||||||||||||||||||||||
| 621 | 3 | ▶ | my ( $self, $listref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
acceptable_attributes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 622 | 3 | ▶ | if ( ref($listref) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 623 | 1 | my @list = @{$listref}; | |||||||||||||||||||||||||||
| 624 | 1 | my %acceptable = map { ( $_, 1 ) } @list; | |||||||||||||||||||||||||||
| 625 | 1 | $self->{acceptable_a} = \%acceptable; | |||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||
| 627 | 3 | return keys %{ $self->{acceptable_a} }; | |||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | =head2 add_acceptable_attribute | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | Adds an attribute (or, if given an array reference, multiple attributes) to the | ||||||||||||||||||||||||||||
| 633 | "acceptable attributes" list maintained by the Laundry object. | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | my $snippet = q{ <p austen:id="3">"My dear Mr. Bennet," said his lady to | ||||||||||||||||||||||||||||
| 636 | him one day, "have you heard that <span austen:footnote="netherfield"> | ||||||||||||||||||||||||||||
| 637 | Netherfield Park</span> is let at last?"</p> | ||||||||||||||||||||||||||||
| 638 | }; | ||||||||||||||||||||||||||||
| 639 | $l->clean( $snippet ); | ||||||||||||||||||||||||||||
| 640 | # returns: | ||||||||||||||||||||||||||||
| 641 | # <p>"My dear Mr. Bennet," said his lady to him one day, | ||||||||||||||||||||||||||||
| 642 | # "have you heard that <span>Netherfield Park</span> is let at | ||||||||||||||||||||||||||||
| 643 | # last?"</p> | ||||||||||||||||||||||||||||
| 644 | $l->add_acceptable_attribute([austen:id, austen:footnote]); | ||||||||||||||||||||||||||||
| 645 | $l->clean( $snippet ); | ||||||||||||||||||||||||||||
| 646 | # returns: | ||||||||||||||||||||||||||||
| 647 | # <p austen:id="3">"My dear Mr. Bennet," said his lady to him | ||||||||||||||||||||||||||||
| 648 | # one day, "have you heard that <span austen:footnote="netherfield"> | ||||||||||||||||||||||||||||
| 649 | # Netherfield Park</span> is let at last?"</span></p> | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | =cut | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | sub add_acceptable_attribute { | ||||||||||||||||||||||||||||
| 654 | 4 | ▶ | my ( $self, $new_a, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_acceptable_attribute: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 655 | 4 | my $acceptable = $self->{acceptable_a}; | |||||||||||||||||||||||||||
| 656 | 4 | ▶ | if ( ref($new_a) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 657 | 1 | foreach my $a ( @{$new_a} ) { | |||||||||||||||||||||||||||
| 658 | 2 | $self->add_acceptable_attribute( $a, $args ); | |||||||||||||||||||||||||||
| 659 | } | ||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||
| 661 | else { | ||||||||||||||||||||||||||||
| 662 | 3 | $acceptable->{$new_a} = 1; | |||||||||||||||||||||||||||
| 663 | } | ||||||||||||||||||||||||||||
| 664 | 4 | return 1; | |||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | =head2 remove_acceptable_attribute | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | Removes an attribute (or, if given an array reference, multiple attributes) | ||||||||||||||||||||||||||||
| 670 | from the "acceptable attributes" list maintained by the Laundry object. | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | $l->clean(q{<p id="plugh">plover</p>}); | ||||||||||||||||||||||||||||
| 673 | # returns '<p id="plugh">plover</p>' | ||||||||||||||||||||||||||||
| 674 | $l->remove_acceptable_element( q{id} ); | ||||||||||||||||||||||||||||
| 675 | $l->clean(q{<p id="plugh">plover</p>}); | ||||||||||||||||||||||||||||
| 676 | # returns '<p>plover</p> | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | =cut | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | sub remove_acceptable_attribute { | ||||||||||||||||||||||||||||
| 681 | 4 | ▶ | my ( $self, $new_a, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove_acceptable_attribute: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 682 | 4 | my $acceptable = $self->{acceptable_a}; | |||||||||||||||||||||||||||
| 683 | 4 | ▶ | if ( ref($new_a) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 684 | 1 | foreach my $a ( @{$new_a} ) { | |||||||||||||||||||||||||||
| 685 | 2 | $self->remove_acceptable_attribute( $a, $args ); | |||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||
| 688 | else { | ||||||||||||||||||||||||||||
| 689 | 3 | delete $acceptable->{$new_a}; | |||||||||||||||||||||||||||
| 690 | } | ||||||||||||||||||||||||||||
| 691 | 4 | return 1; | |||||||||||||||||||||||||||
| 692 | } | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | sub _generate_tidy { | ||||||||||||||||||||||||||||
| 695 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_generate_tidy: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 696 | 8 | my $param = shift; | |||||||||||||||||||||||||||
| 697 | 8 | $self->_generate_html_tidy; | |||||||||||||||||||||||||||
| 698 | 8 | ▶ | if ( !$self->{tidy} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 699 | 8 | $self->_generate_html_tidy_libxml; | |||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||
| 701 | 8 | return; | |||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | sub _generate_html_tidy_libxml { | ||||||||||||||||||||||||||||
| 705 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_generate_html_tidy_libxml: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 706 | { | ||||||||||||||||||||||||||||
| 707 | 8 | local $@; | |||||||||||||||||||||||||||
| 708 | 8 | eval { | |||||||||||||||||||||||||||
| 709 | 8 | require HTML::Tidy::libXML; | |||||||||||||||||||||||||||
| 710 | 0 | $self->{tidy} = HTML::Tidy::libXML->new(); | |||||||||||||||||||||||||||
| 711 | 0 | $self->{tidy_head} = q{<?xml version="1.0" encoding="UTF-8"?> | |||||||||||||||||||||||||||
| 712 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 1.0 Transitional//EN" | ||||||||||||||||||||||||||||
| 713 | "http://www.w3.org/TR/ html1/DTD/ html1-transitional.dtd"> | ||||||||||||||||||||||||||||
| 714 | <html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"><body>}; | ||||||||||||||||||||||||||||
| 715 | 0 | $self->{tidy_foot} = q{</body></html> | |||||||||||||||||||||||||||
| 716 | }; | ||||||||||||||||||||||||||||
| 717 | 0 | $self->{tidy_engine} = q{HTML::Tidy::libXML}; | |||||||||||||||||||||||||||
| 718 | 0 | 1; | |||||||||||||||||||||||||||
| 719 | }; | ||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | sub _generate_html_tidy { | ||||||||||||||||||||||||||||
| 724 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_generate_html_tidy: called CC=3 SCAR=19.4
| |||||||||||||||||||||||||||||
| 725 | { | ||||||||||||||||||||||||||||
| 726 | 8 | local $@; | |||||||||||||||||||||||||||
| 727 | 8 | eval { | |||||||||||||||||||||||||||
| 728 | 8 | require HTML::Tidy; | |||||||||||||||||||||||||||
| 729 | 0 | $self->{tidy_ruleset} = $self->{ruleset}->tidy_ruleset; | |||||||||||||||||||||||||||
| 730 | 0 | ▶ | if ( keys %{ $self->{tidy_added_inline} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 731 | $self->{tidy_ruleset}->{new_inline_tags} | ||||||||||||||||||||||||||||
| 732 | 0 | = join( q{,}, keys %{ $self->{tidy_added_inline} } ); | |||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||
| 734 | 0 | ▶ | if ( keys %{ $self->{tidy_added_empty} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 735 | $self->{tidy_ruleset}->{new_empty_tags} | ||||||||||||||||||||||||||||
| 736 | 0 | = join( q{,}, keys %{ $self->{tidy_added_empty} } ); | |||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||
| 738 | 0 | $self->{tidy} = HTML::Tidy->new( $self->{tidy_ruleset} ); | |||||||||||||||||||||||||||
| 739 | 0 | $self->{tidy_engine} = q{HTML::Tidy}; | |||||||||||||||||||||||||||
| 740 | 0 | 1; | |||||||||||||||||||||||||||
| 741 | }; | ||||||||||||||||||||||||||||
| 742 | } | ||||||||||||||||||||||||||||
| 743 | } | ||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||
| 745 | sub _reset_state { | ||||||||||||||||||||||||||||
| 746 | 462 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_reset_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 747 | 462 | @fragments = (); | |||||||||||||||||||||||||||
| 748 | 462 | $unacceptable_count = 0; | |||||||||||||||||||||||||||
| 749 | 462 | $local_unacceptable_count = 0; | |||||||||||||||||||||||||||
| 750 | 462 | $in_cdata = 0; | |||||||||||||||||||||||||||
| 751 | 462 | $cdata_dirty = 0; | |||||||||||||||||||||||||||
| 752 | 462 | return; | |||||||||||||||||||||||||||
| 753 | } | ||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||
| 755 | sub _tag_start_handler { | ||||||||||||||||||||||||||||
| 756 | 493 | ▶ | my ( $self, $tagname, $attr ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_tag_start_handler: called CC=13 SCAR=25.6
| |||||||||||||||||||||||||||||
| 757 | 493 | ▶ | if ( !$self->_run_callbacks( q{start_tag}, \$tagname, $attr ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 758 | 1 | return; | |||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||
| 760 | 492 | ▶ | if ( !$in_cdata ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 761 | 487 | $cdata_dirty = 0; | |||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||
| 763 | 492 | my @attributes; | |||||||||||||||||||||||||||
| 764 | 492 | foreach my $k ( keys %{$attr} ) { | |||||||||||||||||||||||||||
| 765 | 259 | ▶ | if ( $self->{acceptable_a}->{$k} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 766 | 174 | ▶ | if ( grep {/^$k$/} @{ $self->{uri_list}->{$tagname} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 767 | $self->_uri_handler( $tagname, \$k, \$attr->{$k}, | ||||||||||||||||||||||||||||
| 768 | 58 | $self->{base_uri} ); | |||||||||||||||||||||||||||
| 769 | } | ||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | # Allow uri handler to suppress insertion | ||||||||||||||||||||||||||||
| 772 | 174 | ▶ | if ($k) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 773 | 157 | push @attributes, $k . q{="} . $attr->{$k} . q{"}; | |||||||||||||||||||||||||||
| 774 | } | ||||||||||||||||||||||||||||
| 775 | } | ||||||||||||||||||||||||||||
| 776 | } | ||||||||||||||||||||||||||||
| 777 | 492 | my $attributes = join q{ }, @attributes; | |||||||||||||||||||||||||||
| 778 | 492 | ▶ | if ( $self->{acceptable_e}->{$tagname} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 779 | 376 | ▶ | if ( $self->{empty_e}->{$tagname} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 780 | 58 | ▶ | if ($attributes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 781 | 19 | $attributes = $attributes . q{ }; | |||||||||||||||||||||||||||
| 782 | } | ||||||||||||||||||||||||||||
| 783 | 58 | push @fragments, "<$tagname $attributes/>"; | |||||||||||||||||||||||||||
| 784 | } | ||||||||||||||||||||||||||||
| 785 | else { | ||||||||||||||||||||||||||||
| 786 | 318 | ▶ | if ($attributes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 787 | 122 | $attributes = q{ } . $attributes; | |||||||||||||||||||||||||||
| 788 | } | ||||||||||||||||||||||||||||
| 789 | 318 | push @fragments, "<$tagname$attributes>"; | |||||||||||||||||||||||||||
| 790 | } | ||||||||||||||||||||||||||||
| 791 | } | ||||||||||||||||||||||||||||
| 792 | else { | ||||||||||||||||||||||||||||
| 793 | 116 | ▶ | if ( $self->{unacceptable_e}->{$tagname} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 794 | 24 | ▶ | if ($in_cdata) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 795 | 3 | $local_unacceptable_count += 1; | |||||||||||||||||||||||||||
| 796 | } | ||||||||||||||||||||||||||||
| 797 | else { | ||||||||||||||||||||||||||||
| 798 | 21 | $unacceptable_count += 1; | |||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||
| 800 | } | ||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||
| 802 | 492 | return; | |||||||||||||||||||||||||||
| 803 | } | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | sub _tag_end_handler { | ||||||||||||||||||||||||||||
| 806 | 467 | ▶ | my ( $self, $tagname ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_tag_end_handler: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 807 | 467 | ▶ | if ( !$self->_run_callbacks( q{end_tag}, \$tagname ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 808 | 1 | return; | |||||||||||||||||||||||||||
| 809 | } | ||||||||||||||||||||||||||||
| 810 | 466 | ▶ | if ( !$in_cdata ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 811 | 463 | $cdata_dirty = 0; | |||||||||||||||||||||||||||
| 812 | } | ||||||||||||||||||||||||||||
| 813 | 466 | ▶ | if ( $self->{acceptable_e}->{$tagname} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 814 | 346 | ▶ | if ( !$self->{empty_e}->{$tagname} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 815 | 316 | push @fragments, "</$tagname>"; | |||||||||||||||||||||||||||
| 816 | } | ||||||||||||||||||||||||||||
| 817 | } | ||||||||||||||||||||||||||||
| 818 | else { | ||||||||||||||||||||||||||||
| 819 | 120 | ▶ | if ( $self->{unacceptable_e}->{$tagname} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 820 | 30 | ▶ | if ($in_cdata) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 821 | 1 | $local_unacceptable_count -= 1; | |||||||||||||||||||||||||||
| 822 | 1 | ▶ | $local_unacceptable_count = 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 823 | if ( $local_unacceptable_count < 0 ); | ||||||||||||||||||||||||||||
| 824 | } | ||||||||||||||||||||||||||||
| 825 | else { | ||||||||||||||||||||||||||||
| 826 | 29 | $unacceptable_count -= 1; | |||||||||||||||||||||||||||
| 827 | 29 | ▶ | $unacceptable_count = 0 if ( $unacceptable_count < 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 828 | } | ||||||||||||||||||||||||||||
| 829 | } | ||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||
| 831 | 466 | return; | |||||||||||||||||||||||||||
| 832 | } | ||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||
| 834 | sub _text_handler { | ||||||||||||||||||||||||||||
| 835 | 172 | ▶ | my ( $self, $text, $is_cdata ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_text_handler: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 836 | 172 | ▶ | if ( $in_cdata && $local_unacceptable_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_cdata and $local_unacceptable_count
MC/DC100%2/2
$in_cdata and $local_unacceptable_count
$in_cdata
$local_unacceptable_count
Truth table100%3/3
A and B
A= $in_cdataB= $local_unacceptable_count
| |||||||||||||||||||||||||||||
| 837 | 1 | return; | |||||||||||||||||||||||||||
| 838 | } | ||||||||||||||||||||||||||||
| 839 | 171 | ▶ | if ($unacceptable_count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 840 | 15 | return; | |||||||||||||||||||||||||||
| 841 | } | ||||||||||||||||||||||||||||
| 842 | 156 | ▶ | if ($is_cdata) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 843 | 13 | my $cp = $self->{cdata_parser}; | |||||||||||||||||||||||||||
| 844 | 13 | $in_cdata = 1; | |||||||||||||||||||||||||||
| 845 | 13 | $cp->parse($text); | |||||||||||||||||||||||||||
| 846 | 13 | ▶ | if ( !$local_unacceptable_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 847 | 11 | $cp->eof(); | |||||||||||||||||||||||||||
| 848 | } | ||||||||||||||||||||||||||||
| 849 | 13 | $cdata_dirty = 1; | |||||||||||||||||||||||||||
| 850 | 13 | $in_cdata = 0; | |||||||||||||||||||||||||||
| 851 | 13 | return; | |||||||||||||||||||||||||||
| 852 | } | ||||||||||||||||||||||||||||
| 853 | else { | ||||||||||||||||||||||||||||
| 854 | 143 | ▶ | if ( !$self->_run_callbacks( q{text}, \$text, $is_cdata ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 855 | 1 | return q{}; | |||||||||||||||||||||||||||
| 856 | } | ||||||||||||||||||||||||||||
| 857 | 142 | $text = encode_entities( $text, '<>&"' ); | |||||||||||||||||||||||||||
| 858 | 142 | $cdata_dirty = 0; | |||||||||||||||||||||||||||
| 859 | } | ||||||||||||||||||||||||||||
| 860 | 142 | push @fragments, $text; | |||||||||||||||||||||||||||
| 861 | 142 | return; | |||||||||||||||||||||||||||
| 862 | } | ||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||
| 864 | sub _uri_handler { | ||||||||||||||||||||||||||||
| 865 | 58 | ▶ | my ( $self, $tagname, $attr_ref, $value_ref, $base ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_uri_handler: called CC=11 SCAR=25.9
| |||||||||||||||||||||||||||||
| 866 | 58 | my ( $attr, $value ) = ( ${$attr_ref}, ${$value_ref} ); | |||||||||||||||||||||||||||
| 867 | 58 | $value =~ s/[`\x00-\x1f\x7f]+//g; | |||||||||||||||||||||||||||
| 868 | 58 | $value =~ s/\ufffd//g; | |||||||||||||||||||||||||||
| 869 | 58 | my $uri = URI->new($value); | |||||||||||||||||||||||||||
| 870 | 58 | $uri = $uri->canonical; | |||||||||||||||||||||||||||
| 871 | 58 | ▶ | if ( !$self->_run_callbacks( q{uri}, $tagname, $attr, \$uri ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 872 | 1 | ${$attr_ref} = q{}; | |||||||||||||||||||||||||||
| 873 | 1 | return undef; | |||||||||||||||||||||||||||
| 874 | } | ||||||||||||||||||||||||||||
| 875 | 57 | ▶ | if ( $self->{allowed_schemes} and $uri->scheme ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"allowed_schemes"} and $uri->scheme
MC/DC50%1/2
$self->{"allowed_schemes"} and $uri->scheme
$self->{"allowed_schemes"}
$uri->scheme
Truth table66%2/3
A and B
A= $self->{"allowed_schemes"}B= $uri->scheme
| |||||||||||||||||||||||||||||
| 876 | 42 | ▶ | unless ( $self->{allowed_schemes}->{ $uri->scheme } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 877 | 16 | ${$attr_ref} = q{}; | |||||||||||||||||||||||||||
| 878 | 16 | return undef; | |||||||||||||||||||||||||||
| 879 | } | ||||||||||||||||||||||||||||
| 880 | } | ||||||||||||||||||||||||||||
| 881 | 41 | ▶ | if ( $self->{base_uri} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 882 | 8 | $uri = URI->new_abs( $uri->as_string, $self->{base_uri} ); | |||||||||||||||||||||||||||
| 883 | } | ||||||||||||||||||||||||||||
| 884 | 41 | ▶ | if ( $uri->scheme ) { # Not a local URI | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 885 | 33 | my $host; | |||||||||||||||||||||||||||
| 886 | { | ||||||||||||||||||||||||||||
| 887 | 33 | local $@; | |||||||||||||||||||||||||||
| 888 | 33 | eval { $host = $uri->host; }; | |||||||||||||||||||||||||||
| 889 | } | ||||||||||||||||||||||||||||
| 890 | 33 | ▶ | if ($host) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||
| 892 | # We may need to manually unescape domain names | ||||||||||||||||||||||||||||
| 893 | # to deal with issues like tinyarro.ws | ||||||||||||||||||||||||||||
| 894 | 33 | my $utf8_host = $self->_decode_utf8($host); | |||||||||||||||||||||||||||
| 895 | 33 | utf8::upgrade($utf8_host); | |||||||||||||||||||||||||||
| 896 | 33 | ▶ | if ( $uri->host ne $utf8_host ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||
| 898 | # TODO: Optionally use Punycode in this case | ||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||
| 900 | 0 | ▶ | if ( $uri->port and $uri->port == $uri->default_port ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$uri->port and $uri->port == $uri->default_port
MC/DC0%0/2
$uri->port and $uri->port == $uri->default_port
$uri->port
$uri->port == $uri->default_port
Truth table0%0/3
A and B
A= $uri->portB= $uri->port == $uri->default_port
| |||||||||||||||||||||||||||||
| 901 | 0 | $uri->port(undef); | |||||||||||||||||||||||||||
| 902 | } | ||||||||||||||||||||||||||||
| 903 | 0 | my $escaped_host = $self->_encode_utf8( $uri->host ); | |||||||||||||||||||||||||||
| 904 | 0 | my $uri_str = $uri->canonical->as_string; | |||||||||||||||||||||||||||
| 905 | 0 | $uri_str =~ s/$escaped_host/$utf8_host/; | |||||||||||||||||||||||||||
| 906 | 0 | utf8::upgrade($uri_str); | |||||||||||||||||||||||||||
| 907 | 0 | ${$value_ref} = $uri_str; | |||||||||||||||||||||||||||
| 908 | 0 | return; | |||||||||||||||||||||||||||
| 909 | } | ||||||||||||||||||||||||||||
| 910 | } | ||||||||||||||||||||||||||||
| 911 | } | ||||||||||||||||||||||||||||
| 912 | 41 | ${$value_ref} = $uri->canonical->as_string; | |||||||||||||||||||||||||||
| 913 | 41 | return; | |||||||||||||||||||||||||||
| 914 | } | ||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||
| 916 | sub _decode_utf8 { | ||||||||||||||||||||||||||||
| 917 | 33 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_decode_utf8: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 918 | 33 | my $orig = my $str = shift; | |||||||||||||||||||||||||||
| 919 | 33 | $str =~ s/\%([0-9a-f]{2})/chr(hex($1))/egi; | |||||||||||||||||||||||||||
| 920 | 33 | ▶ | return $str if utf8::decode($str); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 921 | 0 | return $orig; | |||||||||||||||||||||||||||
| 922 | } | ||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||
| 924 | sub _encode_utf8 { | ||||||||||||||||||||||||||||
| 925 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_encode_utf8: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 926 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 927 | 0 | my $highbit = qr/[^\w\$-_.+!*'(),]/; | |||||||||||||||||||||||||||
| 928 | 0 | $str =~ s/($highbit)/ sprintf ("%%%02X", ord($1)) /ge; | |||||||||||||||||||||||||||
| 929 | 0 | utf8::upgrade($str); | |||||||||||||||||||||||||||
| 930 | 0 | return $str; | |||||||||||||||||||||||||||
| 931 | } | ||||||||||||||||||||||||||||
| 932 | |||||||||||||||||||||||||||||
| 933 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||
| 935 | There are a number of tools designed for sanitizing HTML, some of which | ||||||||||||||||||||||||||||
| 936 | may be better suited than HTML::Laundry to particular circumstances. In | ||||||||||||||||||||||||||||
| 937 | addition to L<HTML::Scrubber|HTML::Scrubber>, you may want to consider | ||||||||||||||||||||||||||||
| 938 | L<HTML::StripScripts::Parser|HTML::StripScripts::Parser>, an C<HTML::Parser>-based module designed | ||||||||||||||||||||||||||||
| 939 | solely for the purposes of sanitizing HTML from potential XSS attack vectors; | ||||||||||||||||||||||||||||
| 940 | L<HTML::Defang|HTML::Defang>, a whitelist-based, pure-Perl module; or | ||||||||||||||||||||||||||||
| 941 | L<HTML::Restrict|HTML::Restrict>, an HTML tag whitelist using C<HTML::Parser>. | ||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||
| 943 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||
| 945 | Steve Cook, C<< <scook at sixapart.com> >> | ||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||
| 947 | =head1 BUGS | ||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||
| 949 | Please report any bugs or feature requests on the GitHub page for this project, | ||||||||||||||||||||||||||||
| 950 | http://github.com/snark/html-laundry. | ||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||
| 952 | =head1 ACKNOWLEDGMENTS | ||||||||||||||||||||||||||||
| 953 | |||||||||||||||||||||||||||||
| 954 | Thanks to Dave Cross and Vera Tobin. | ||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||
| 956 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||
| 958 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||
| 960 | perldoc HTML::Laundry | ||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||
| 962 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 963 | |||||||||||||||||||||||||||||
| 964 | Copyright 2009 Six Apart, Ltd., all rights reserved. | ||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||
| 966 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 967 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||
| 969 | =cut | ||||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||
| 971 | 16 | 1; # End of HTML::Laundry | |||||||||||||||||||||||||||