| 1 | package # don't index this | ||
| 2 | HTML::Restricted; | ||
| 3 | use strict; | ||
| 4 | use Moo; | ||
| 5 | use HTML::TreeBuilder 5 '-weak'; # we want weak references | ||
| 6 | |||
| 7 | use vars '$VERSION'; | ||
| 8 | $VERSION = '0.06'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | HTML::Restricted - filter HTML to a set of allowed tags and attributes | ||
| 13 | |||
| 14 | =head1 WHY? | ||
| 15 | |||
| 16 | This is just a band-aid module I needed while without internet connection. | ||
| 17 | There also are L<HTML::Strip> and some other modules that should do the | ||
| 18 | same task and this module will likely vanish again when I review the others | ||
| 19 | and decide on using one. | ||
| 20 | |||
| 21 | L<HTML::StripScripts> seems a likely alternative to this module. | ||
| 22 | |||
| 23 | Currently, attributes are not cleaned up. Also, HTML5 tags are simply | ||
| 24 | stripped as this module doesn't use L<HTML::HTML5::Parser>. | ||
| 25 | |||
| 26 | Don't rely on this module. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use vars qw(%allowed); | ||
| 31 | |||
| 32 | %allowed = ( | ||
| 33 | a => ['href','name'], | ||
| 34 | abbr => 1, | ||
| 35 | address => 1, | ||
| 36 | b => 1, | ||
| 37 | blockquote => 1, | ||
| 38 | body => 1, | ||
| 39 | br => 1, | ||
| 40 | center => 1, | ||
| 41 | code => 1, | ||
| 42 | div => 1, | ||
| 43 | dd => 1, | ||
| 44 | dl => 1, | ||
| 45 | dt => 1, | ||
| 46 | em => 1, | ||
| 47 | font => 'color', | ||
| 48 | form => 1, | ||
| 49 | h1 => 1, | ||
| 50 | h2 => 1, | ||
| 51 | h3 => 1, | ||
| 52 | h4 => 1, | ||
| 53 | html => 1, | ||
| 54 | hr => 1, | ||
| 55 | i => 1, | ||
| 56 | img => ['src'], | ||
| 57 | input => 1, | ||
| 58 | label => 1, | ||
| 59 | li => 1, | ||
| 60 | ol => 1, | ||
| 61 | option => ['value'], | ||
| 62 | p => 1, | ||
| 63 | pre => 1, | ||
| 64 | small => 1, | ||
| 65 | select => 1, | ||
| 66 | span => 1, | ||
| 67 | strong => 1, | ||
| 68 | table => 1, | ||
| 69 | tbody => 1, | ||
| 70 | td => 1, | ||
| 71 | th => 1, | ||
| 72 | tr => 1, | ||
| 73 | tt => 1, | ||
| 74 | ul => 1, | ||
| 75 | ); | ||
| 76 | |||
| 77 | has tree_class => ( | ||
| 78 | is => 'rw', | ||
| 79 | default => 'HTML::TreeBuilder', | ||
| 80 | ); | ||
| 81 | |||
| 82 | #has contents => ( | ||
| 83 | # is => 'ro', | ||
| 84 | # default => sub { +{ %contents } }, | ||
| 85 | #); | ||
| 86 | |||
| 87 | has allowed => ( | ||
| 88 | is => 'ro', | ||
| 89 | default => sub { +{ %allowed } }, | ||
| 90 | ); | ||
| 91 | |||
| 92 | sub filter_element { | ||
| 93 | my( $self, $doc, $elt ) = @_; | ||
| 94 | if( my $attrs = $self->allowed->{ lc $elt->tag } ) { | ||
| 95 | # Strip the attributes except for allowed attributes | ||
| 96 | $attrs = [] if ! ref $attrs; | ||
| 97 | my %aa = map { $_ => 1 } @$attrs; | ||
| 98 | for my $name ($elt->all_external_attr_names) { | ||
| 99 | #warn $name; | ||
| 100 | $elt->attr($name => undef) | ||
| 101 | unless $aa{ lc $name }; | ||
| 102 | }; | ||
| 103 | |||
| 104 | # Recurse into children | ||
| 105 | for my $child ($elt->content_list) { | ||
| 106 | next unless ref $child; | ||
| 107 | $self->filter_element($doc, $child); | ||
| 108 | }; | ||
| 109 | #} elsif( $self->contents->{ lc $elt->tag } ) { | ||
| 110 | # # Replace with its contents# | ||
| 111 | |||
| 112 | # for my $child ($elt->content_list) { | ||
| 113 | # next unless ref $child; | ||
| 114 | # $self->filter_element($doc, $child); | ||
| 115 | # }; | ||
| 116 | # | ||
| 117 | # $elt->replace_with($elt->content_list); | ||
| 118 | } else { | ||
| 119 | print sprintf "%s: removed\n", $elt->tag; | ||
| 120 | $elt->delete; | ||
| 121 | }; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub filter { | ||
| 125 | my( $self, $html, %options ) = @_; | ||
| 126 | |||
| 127 | my $t = $options{treebuilder} || do { | ||
| 128 | # Load the class | ||
| 129 | (my $fn = $self->tree_class) =~ s!::!/!g; | ||
| 130 | require "$fn.pm"; | ||
| 131 | |||
| 132 | $self->tree_class->new; | ||
| 133 | }; | ||
| 134 | |||
| 135 | $t->parse($html || '<html></html>'); | ||
| 136 | $t->eof; | ||
| 137 | |||
| 138 | $t->elementify; | ||
| 139 | |||
| 140 | $self->filter_element( $t, $t->root ); | ||
| 141 | $t | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; |