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