summaryrefslogtreecommitdiff
path: root/t/filter-methods.t
blob: 9eccaf1655967eb6b0d8e88ebeddbf2d4b0d2864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/perl -w

use Test::More tests => 12;
use strict;

use HTML::Parser;

my $p = HTML::Parser->new(api_version => 3, ignore_tags => [qw(b i em tt)]);
$p->ignore_elements("script");
$p->unbroken_text(1);

$p->handler(default => [], "event, text");
$p->parse(<<"EOT")->eof;
<html><head><title>foo</title><Script language="Perl">
   while (<B>) {
      # ...
   }
</Script><body>
This is an <i>italic</i> and <b>bold</b> text.
</body>
</html>
EOT

my $t = join("||", map join("|", @$_), @{$p->handler("default")});
#diag $t;

is($t, "start_document|||start|<html>||start|<head>||start|<title>||text|foo||end|</title>||start|<body>||text|
This is an italic and bold text.
||end|</body>||text|
||end|</html>||text|
||end_document|", 'ignore_elements');


#------------------------------------------------------

$p = HTML::Parser->new(api_version => 3);
$p->report_tags("a");
$p->handler(start => sub {
		my($tagname, %attr) = @_;
		ok($tagname eq "a" && $attr{href} eq "#a", 'report_tags start');
            }, 'tagname, @attr');
$p->handler(end => sub {
		my $tagname = shift;
		is($tagname, "a", 'report_tags end');
            }, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>

This is <a href="#a">very nice</a> example.

EOT


#------------------------------------------------------

my @tags;
$p = HTML::Parser->new(api_version => 3);
$p->report_tags(qw(a em));
$p->ignore_tags(qw(em));
$p->handler(end => sub {push @tags, @_;}, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>

This is <em>yet another</em> <a href="#a">very nice</a> example.

EOT
is(join('|', @tags), 'a', 'report_tags followed by ignore_tags');


#------------------------------------------------------

@tags = ();
$p = HTML::Parser->new(api_version => 3);
$p->report_tags(qw(h1));
$p->report_tags();
$p->handler(end => sub {push @tags, @_;}, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>
<h2>Next example</h2>

EOT
is(join('|', @tags), 'h1|h2', 'reset report_tags filter');


#------------------------------------------------------

@tags = ();
$p = HTML::Parser->new(api_version => 3);
$p->report_tags(qw(h1 h2));
$p->ignore_tags(qw(h2));
$p->report_tags(qw(h1 h2));
$p->handler(end => sub {push @tags, @_;}, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>
<h2>Next example</h2>

EOT
is(join('|', @tags), 'h1', 'report_tags does not reset ignore_tags');


#------------------------------------------------------

@tags = ();
$p = HTML::Parser->new(api_version => 3);
$p->report_tags(qw(h1 h2));
$p->ignore_tags(qw(h2));
$p->report_tags();
$p->handler(end => sub {push @tags, @_;}, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>
<h2>Next example</h2>

EOT
is(join('|', @tags), 'h1', 'reset report_tags does no reset ignore_tags');


#------------------------------------------------------

@tags = ();
$p = HTML::Parser->new(api_version => 3);
$p->report_tags(qw(h1 h2));
$p->report_tags(qw(h3));
$p->handler(end => sub {push @tags, @_;}, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>
<h2>Next example</h2>
<h3>Next example</h3>

EOT
is(join('|', @tags), 'h3', 'report_tags replaces filter');


#------------------------------------------------------


@tags = ();
$p = HTML::Parser->new(api_version => 3);
$p->ignore_tags(qw(h1 h2));
$p->ignore_tags(qw(h3));
$p->handler(end => sub {push @tags, @_;}, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>
<h2>Next example</h2>
<h3>Next example</h3>

EOT
is(join('|', @tags), 'h1|h2', 'ignore_tags replaces filter');


#------------------------------------------------------

@tags = ();
$p = HTML::Parser->new(api_version => 3);
$p->ignore_tags(qw(h2));
$p->ignore_tags();
$p->handler(end => sub {push @tags, @_;}, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>
<h2>Next example</h2>

EOT
is(join('|', @tags), 'h1|h2', 'reset ignore_tags filter');


#------------------------------------------------------

@tags = ();
$p = HTML::Parser->new(api_version => 3);
$p->ignore_tags(qw(h2));
$p->report_tags(qw(h1 h2));
$p->handler(end => sub {push @tags, @_;}, 'tagname');

$p->parse(<<EOT)->eof;

<h1>Next example</h1>
<h2>Next example</h2>

EOT
is(join('|', @tags), 'h1', 'ignore_tags before report_tags');
#------------------------------------------------------

$p = HTML::Parser->new(api_version => 3);
$p->ignore_elements("script");
my $res="";
$p->handler(default=> sub {$res.=$_[0];}, 'text');
$p->parse(<<'EOT')->eof;
A <script> B </script> C </script> D <script> E </script> F
EOT
is($res,"A  C  D  F\n","ignore </script> without <script> correctly");