Skip to content

Commit

Permalink
Adding tests for Minimal ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
snark committed Oct 20, 2009
1 parent 39397fe commit 6b1623d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -18,5 +18,6 @@ t/09-callbacks.t
t/10-uri.t
t/11-rebase.t
t/12-ruleset.t
t/13-minimal.t
t/pod-coverage.t
t/pod.t
2 changes: 1 addition & 1 deletion lib/HTML/Laundry/Rules/Minimal.pm
Expand Up @@ -22,7 +22,7 @@ a, b, blockquote, code, em, i, li, ol, p, pre, strong, u, and ul
sub acceptable_e {
my $self = shift;
my @acceptable = qw(
a b blockquote code em i li ol p pre strong u ul
a b br blockquote code em i li ol p pre strong u ul
);
my %acceptable = map { ( $_, 1 ) } @acceptable;
return \%acceptable;
Expand Down
3 changes: 1 addition & 2 deletions t/12-ruleset.t
Expand Up @@ -43,5 +43,4 @@ is( $l7->clean( $str ), $str,
my $l8 = HTML::Laundry->new({});
ok( $l8, 'Created Laundry object with no argument' );
is( $l8->clean( $str ), $str,
'Ruleset defaults to HTML::Laundry::Rules::Default' );

'Ruleset defaults to HTML::Laundry::Rules::Default' );
71 changes: 71 additions & 0 deletions t/13-minimal.t
@@ -0,0 +1,71 @@
use strict;
use warnings;

use Test::More tests => 158;

require_ok('HTML::Laundry');
require_ok('HTML::Laundry::Rules::Minimal');

my $l1 = HTML::Laundry->new({ notidy => 1,
rules => 'HTML::Laundry::Rules::Minimal' });

my @ok = qw( a b br blockquote code em i li ol p pre strong u ul );
my %ok = map { $_ => 1 } @ok;

my @e = (
'a', 'abbr', 'acronym', 'address', 'area', 'b', 'bdo', 'big', 'blockquote',
'br', 'button', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'dd',
'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'fieldset', 'font', 'form',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'input', 'ins', 'kbd',
'label', 'legend', 'li', 'map', 'menu', 'ol', 'optgroup', 'option', 'p',
'pre', 'q', 's', 'samp', 'select', 'small', 'span', 'strike', 'strong',
'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead',
'tr', 'tt', 'u', 'ul', 'var', 'wbr'
);

foreach my $e ( @e ) {
if ( $ok{$e} and $e ne 'br' ) {
# The only allowed empty element in this ruleset is <br />
is( $l1->clean("<$e></$e>"), "<$e></$e>", "element $e is not sanitized");
} elsif ( $ok{$e} ) {
is( $l1->clean("<$e></$e>"), "<$e />", "element $e is not sanitized");
} else {
is( $l1->clean("<$e></$e>"), "", "element $e is sanitized");
}
}

my @a = ( 'abbr', 'accept', 'accept-charset', 'accesskey', 'action', 'align', 'alt',
'axis', 'border', 'cellpadding', 'cellspacing', 'char', 'charoff', 'charset',
'checked', 'cite', 'class', 'clear', 'color', 'cols', 'colspan', 'compact',
'coords', 'datetime', 'dir', 'disabled', 'enctype', 'for', 'frame',
'headers', 'height', 'href', 'hreflang', 'hspace', 'id', 'ismap', 'label',
'lang', 'longdesc', 'maxlength', 'media', 'method', 'multiple', 'name',
'nohref', 'noshade', 'nowrap', 'prompt', 'readonly', 'rel', 'rev', 'rows',
'rowspan', 'rules', 'scope', 'selected', 'shape', 'size', 'span', 'src',
'start', 'summary', 'tabindex', 'target', 'title', 'type', 'usemap',
'valign', 'value', 'vspace', 'width', 'xml:lang' );

ok( ! $l1->clean('<script>alert("Jane Austen was here!");</script>'), '<script> is removed in its entirety');
ok( ! $l1->clean('<applet>blah blah</applet>'), '<applet> is removed in its entirety');
is( $l1->clean('<heroine>No one who had ever seen Catherine Morland in her infancy...</heroine>'),
'No one who had ever seen Catherine Morland in her infancy...',
'Unknown tag is stripped, but its contents remain' );
is( $l1->clean('<body>foo</body>'), 'foo', '<body> tag is stripped');
is( $l1->clean('<link />'), '', '<link> tag is stripped');
is( $l1->clean('<meta />'), '', '<meta> tag is stripped');
is( $l1->clean('<html>foo</html>'), 'foo', '<html> tag is stripped');
ok( ! $l1->clean('<?php echo("Foo"); ?>') && ! $l1->clean('<?= $foo ?>'), 'PHP tags are stripped entirely');
is( $l1->clean('<%= "Hello World!" %>'), '&lt;%= &quot;Hello World!&quot; %&gt;', 'ASP tags are transformed into literal text');
is( $l1->clean('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'),
'', 'DOCTYPE declaration is stripped');
is( $l1->clean('<a href="xyzzy" plugh="plover">Her situation in life, the character of her father and mother, her own person and disposition, were all equally against her.</a>'),
'<a href="xyzzy">Her situation in life, the character of her father and mother, her own person and disposition, were all equally against her.</a>',
'Unknown attribute is stripped, but known attribute remains' );

foreach my $a ( @a ) {
if ( $a eq 'href' ) {
is( $l1->clean("<p $a=\"frotz\"></p>"), "<p $a=\"frotz\"></p>", "attribute $a is not sanitized");
} else {
is( $l1->clean("<p $a=\"frotz\"></p>"), "<p></p>", "attribute $a is sanitized");
}
}

0 comments on commit 6b1623d

Please sign in to comment.