Skip to content

Commit 42b5803

Browse files
committed
Extended HTMLScrubber functionality
1 parent cf102fe commit 42b5803

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/HTML/FormFu/Filter/HTMLScrubber.pm

+18-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use mro 'c3';
77

88
use Clone ();
99

10-
__PACKAGE__->mk_accessors(qw( allow ));
10+
__PACKAGE__->mk_accessors(qw( allow comment default rules script ));
1111

1212
use HTML::Scrubber;
1313

@@ -16,9 +16,13 @@ sub filter {
1616

1717
return if !defined $value;
1818

19-
my $allowed = $self->allow || [];
19+
my %params = ( allow => 0 );
20+
foreach (qw(allow comment default rules script)) {
21+
my $val = $self->$_;
22+
$params{$_} = $val if ( defined($val) );
23+
}
2024

21-
my $scrubber = HTML::Scrubber->new( allow => $allowed );
25+
my $scrubber = HTML::Scrubber->new(%params);
2226

2327
return $scrubber->scrub($value);
2428
}
@@ -46,10 +50,21 @@ HTML::FormFu::Filter::HTMLScrubber - filter removing HTML markup
4650
4751
Remove HTML markup using L<HTML::Scrubber>.
4852
53+
All the functionality of L<HTML::Scrubber> can be accessed using
54+
this module, other than the C<process> directive (which has a name
55+
clash with the L<HTML::FormFu::Filter> framework).
56+
57+
For details of the filtering functionality see
58+
L<HTML::Scrubber/allow>, L<HTML::Scrubber/comment>,
59+
L<HTML::Scrubber/default>, L<HTML::Scrubber/rules> and
60+
L<HTML::Scrubber/script>
61+
4962
=head1 AUTHOR
5063
5164
Carl Franks, C<[email protected]>
5265
66+
Extended by Nigel Metheringham, C<[email protected]>
67+
5368
Based on the original source code of L<HTML::Widget::Filter::HTMLStrip>, by
5469
Lyo Kato, C<[email protected]>
5570

t/filters/htmlscrubber.t

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
use strict;
22
use warnings;
33

4-
use Test::More tests => 4;
4+
use Test::More tests => 6;
55

66
use HTML::FormFu;
77

88
my $form = HTML::FormFu->new;
99

1010
$form->element('Text')->name('foo')->filter('HTMLScrubber');
1111
$form->element('Text')->name('bar')->filter('HTMLScrubber')->allow( ['b'] );
12+
$form->element('Text')->name('fum')->filter('HTMLScrubber')->rules( '*' => 0, p => { '*' => 0 }, a => { href => 1, '*' => 0 } );
1213

1314
my $original_foo = "<p>message</p>";
1415
my $filtered_foo = "message";
1516

1617
my $original_bar = "<p><b>message</b></p>";
1718
my $filtered_bar = "<b>message</b>";
1819

20+
my $original_fum = "<p class=\"y\"><b>message</b><a href=\"#somewhere\" class=\"x\">text</a></p>";
21+
my $filtered_fum = "<p>message<a href=\"#somewhere\">text</a></p>";
22+
1923
$form->process( {
2024
foo => $original_foo,
2125
bar => $original_bar,
26+
fum => $original_fum,
2227
} );
2328

2429
# foo is quoted
@@ -29,3 +34,7 @@ is( $form->params->{foo}, $filtered_foo, 'foo filtered' );
2934
is( $form->param('bar'), $filtered_bar, 'bar filtered' );
3035
is( $form->params->{bar}, $filtered_bar, 'bar filtered' );
3136

37+
# fum is filtered
38+
is( $form->param('fum'), $filtered_fum, 'fum filtered' );
39+
is( $form->params->{fum}, $filtered_fum, 'fum filtered' );
40+

0 commit comments

Comments
 (0)