Debian lenny version packages
[pkg-perl] / deb-src / libhtml-parser-perl / libhtml-parser-perl-3.56 / t / filter-methods.t
1 #!/usr/bin/perl -w
2
3 use Test::More tests => 12;
4 use strict;
5
6 use HTML::Parser;
7
8 my $p = HTML::Parser->new(api_version => 3, ignore_tags => [qw(b i em tt)]);
9 $p->ignore_elements("script");
10 $p->unbroken_text(1);
11
12 $p->handler(default => [], "event, text");
13 $p->parse(<<"EOT")->eof;
14 <html><head><title>foo</title><Script language="Perl">
15    while (<B>) {
16       # ...
17    }
18 </Script><body>
19 This is an <i>italic</i> and <b>bold</b> text.
20 </body>
21 </html>
22 EOT
23
24 my $t = join("||", map join("|", @$_), @{$p->handler("default")});
25 #diag $t;
26
27 is($t, "start_document|||start|<html>||start|<head>||start|<title>||text|foo||end|</title>||start|<body>||text|
28 This is an italic and bold text.
29 ||end|</body>||text|
30 ||end|</html>||text|
31 ||end_document|", 'ignore_elements');
32
33
34 #------------------------------------------------------
35
36 $p = HTML::Parser->new(api_version => 3);
37 $p->report_tags("a");
38 $p->handler(start => sub {
39                 my($tagname, %attr) = @_;
40                 ok($tagname eq "a" && $attr{href} eq "#a", 'report_tags start');
41             }, 'tagname, @attr');
42 $p->handler(end => sub {
43                 my $tagname = shift;
44                 is($tagname, "a", 'report_tags end');
45             }, 'tagname');
46
47 $p->parse(<<EOT)->eof;
48
49 <h1>Next example</h1>
50
51 This is <a href="#a">very nice</a> example.
52
53 EOT
54
55
56 #------------------------------------------------------
57
58 my @tags;
59 $p = HTML::Parser->new(api_version => 3);
60 $p->report_tags(qw(a em));
61 $p->ignore_tags(qw(em));
62 $p->handler(end => sub {push @tags, @_;}, 'tagname');
63
64 $p->parse(<<EOT)->eof;
65
66 <h1>Next example</h1>
67
68 This is <em>yet another</em> <a href="#a">very nice</a> example.
69
70 EOT
71 is(join('|', @tags), 'a', 'report_tags followed by ignore_tags');
72
73
74 #------------------------------------------------------
75
76 @tags = ();
77 $p = HTML::Parser->new(api_version => 3);
78 $p->report_tags(qw(h1));
79 $p->report_tags();
80 $p->handler(end => sub {push @tags, @_;}, 'tagname');
81
82 $p->parse(<<EOT)->eof;
83
84 <h1>Next example</h1>
85 <h2>Next example</h2>
86
87 EOT
88 is(join('|', @tags), 'h1|h2', 'reset report_tags filter');
89
90
91 #------------------------------------------------------
92
93 @tags = ();
94 $p = HTML::Parser->new(api_version => 3);
95 $p->report_tags(qw(h1 h2));
96 $p->ignore_tags(qw(h2));
97 $p->report_tags(qw(h1 h2));
98 $p->handler(end => sub {push @tags, @_;}, 'tagname');
99
100 $p->parse(<<EOT)->eof;
101
102 <h1>Next example</h1>
103 <h2>Next example</h2>
104
105 EOT
106 is(join('|', @tags), 'h1', 'report_tags does not reset ignore_tags');
107
108
109 #------------------------------------------------------
110
111 @tags = ();
112 $p = HTML::Parser->new(api_version => 3);
113 $p->report_tags(qw(h1 h2));
114 $p->ignore_tags(qw(h2));
115 $p->report_tags();
116 $p->handler(end => sub {push @tags, @_;}, 'tagname');
117
118 $p->parse(<<EOT)->eof;
119
120 <h1>Next example</h1>
121 <h2>Next example</h2>
122
123 EOT
124 is(join('|', @tags), 'h1', 'reset report_tags does no reset ignore_tags');
125
126
127 #------------------------------------------------------
128
129 @tags = ();
130 $p = HTML::Parser->new(api_version => 3);
131 $p->report_tags(qw(h1 h2));
132 $p->report_tags(qw(h3));
133 $p->handler(end => sub {push @tags, @_;}, 'tagname');
134
135 $p->parse(<<EOT)->eof;
136
137 <h1>Next example</h1>
138 <h2>Next example</h2>
139 <h3>Next example</h3>
140
141 EOT
142 is(join('|', @tags), 'h3', 'report_tags replaces filter');
143
144
145 #------------------------------------------------------
146
147
148 @tags = ();
149 $p = HTML::Parser->new(api_version => 3);
150 $p->ignore_tags(qw(h1 h2));
151 $p->ignore_tags(qw(h3));
152 $p->handler(end => sub {push @tags, @_;}, 'tagname');
153
154 $p->parse(<<EOT)->eof;
155
156 <h1>Next example</h1>
157 <h2>Next example</h2>
158 <h3>Next example</h3>
159
160 EOT
161 is(join('|', @tags), 'h1|h2', 'ignore_tags replaces filter');
162
163
164 #------------------------------------------------------
165
166 @tags = ();
167 $p = HTML::Parser->new(api_version => 3);
168 $p->ignore_tags(qw(h2));
169 $p->ignore_tags();
170 $p->handler(end => sub {push @tags, @_;}, 'tagname');
171
172 $p->parse(<<EOT)->eof;
173
174 <h1>Next example</h1>
175 <h2>Next example</h2>
176
177 EOT
178 is(join('|', @tags), 'h1|h2', 'reset ignore_tags filter');
179
180
181 #------------------------------------------------------
182
183 @tags = ();
184 $p = HTML::Parser->new(api_version => 3);
185 $p->ignore_tags(qw(h2));
186 $p->report_tags(qw(h1 h2));
187 $p->handler(end => sub {push @tags, @_;}, 'tagname');
188
189 $p->parse(<<EOT)->eof;
190
191 <h1>Next example</h1>
192 <h2>Next example</h2>
193
194 EOT
195 is(join('|', @tags), 'h1', 'ignore_tags before report_tags');
196 #------------------------------------------------------
197
198 $p = HTML::Parser->new(api_version => 3);
199 $p->ignore_elements("script");
200 my $res="";
201 $p->handler(default=> sub {$res.=$_[0];}, 'text');
202 $p->parse(<<'EOT')->eof;
203 A <script> B </script> C </script> D <script> E </script> F
204 EOT
205 is($res,"A  C  D  F\n","ignore </script> without <script> correctly");