Codebase list libhtml-restrict-perl / d93248b
Renames strip_comments to allow_comments. Adds allow_declaration(). Olaf Alders 12 years ago
3 changed file(s) with 55 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
77 use HTML::Parser;
88 use MooseX::Params::Validate;
99 use Perl6::Junction qw( any );
10
11 has 'allow_comments' => (
12 is => 'rw',
13 isa => 'Bool',
14 default => 0,
15 );
16
17 has 'allow_declaration' => (
18 is => 'rw',
19 isa => 'Bool',
20 default => 0,
21 );
1022
1123 has 'debug' => (
1224 is => 'rw',
2840 is => 'ro',
2941 lazy => 1,
3042 builder => '_build_parser',
31 );
32
33 has 'strip_comments' => (
34 is => 'rw',
35 isa => 'Bool',
36 default => 1,
3743 );
3844
3945 has 'trim' => (
110116 sub {
111117 my ( $p, $text ) = @_;
112118 print "comment: $text\n" if $self->debug;
113 if ( !$self->strip_comments ) {
119 if ( $self->allow_comments ) {
114120 $self->_processed( ( $self->_processed || q{} ) . $text );
115121 }
116122 },
117123 "self,text"
118124 ],
125
126 declaration_h => [
127 sub {
128 my ( $p, $text ) = @_;
129 print "declaration: $text\n" if $self->debug;
130 if ( $self->allow_declaration ) {
131 $self->_processed( ( $self->_processed || q{} ) . $text );
132 }
133 },
134 "self,text"
135 ],
136
119137
120138 );
121139
329347 By default all leading and trailing spaces will be removed when text is
330348 processed. Set this value to 0 in order to disable this behaviour.
331349
332 For example, to preserve leading and trailing whitespace:
350 For example, to allow leading and trailing whitespace:
333351
334352 $hr->trim( 0 );
335353 my $trimmed = $hr->process(' <b>i am bold</b> ');
1010 my $text = '<!-- comment here -->stuff';
1111 $hr->debug( 1 );
1212
13 is $hr->process( $text ), 'stuff', 'comments stripped';
14 $hr->strip_comments( 0 );
15 is $hr->process( $text ), $text, 'comments preserved';
13 is $hr->process( $text ), 'stuff', 'comments allowed';
14 $hr->allow_comments( 1 );
15 is $hr->process( $text ), $text, 'comments allowd';
1616
1717 $text = 'before<!-- This is a comment -- -- So is this -->after';
18 $hr->strip_comments( 1 );
18 $hr->allow_comments( 0 );
1919
20 is $hr->process( $text ), 'beforeafter', 'comment stripped';
20 is $hr->process( $text ), 'beforeafter', 'comment allowed';
2121
22 $hr->strip_comments( 0 );
23 is $hr->process( $text ), $text, 'comments preserved';
22 $hr->allow_comments( 1 );
23 is $hr->process( $text ), $text, 'comments allowd';
2424
25 $hr->strip_comments( 1 );
25 $hr->allow_comments( 0 );
2626 $text = '<!-- <script> <h1> -->';
2727 is $hr->process( $text ), undef, 'tags nested in comments removed';
2828
0 #!/usr/bin/env perl
1
2 use warnings;
3 use strict;
4
5 use HTML::Restrict;
6 use Test::More;
7
8 my $hr = HTML::Restrict->new;
9
10 my $text = '<!DOCTYPE HTML> ';
11 $hr->debug( 1 );
12
13 is $hr->process( $text ), '', 'declaration not preserved';
14 $hr->allow_declaration( 1 );
15 is $hr->process( $text ), '<!DOCTYPE HTML>', 'declaration is preserved';
16
17 $text = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
18 is $hr->process( $text ), $text, 'declaration preserved';
19
20 done_testing();