summaryrefslogtreecommitdiff
path: root/t/handler-eof.t
blob: 39419dc4da3c1beb273b99a7bb56530ff6bb5a4a (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
use Test::More tests => 6;

use strict;
use HTML::Parser ();

my $p = HTML::Parser->new(api_version => 3);

$p->handler(start => sub { my $attr = shift; is($attr->{testno}, 1) },
		     "attr");
$p->handler(end => sub { shift->eof }, "self");
my $text;
$p->handler(text => sub { $text = shift }, "text");

is($p->parse("<foo testno=1>"), $p);

$text = '';
ok(!$p->parse("</foo><foo testno=999>"));
ok(!$text);

$p->handler(end => sub { $p->parse("foo"); }, "");
eval {
    $p->parse("</foo>");
};
like($@,  qr/Parse loop not allowed/);

# We used to get into an infinite loop if the eof triggered
# handler called ->eof

use HTML::Parser;
$p = HTML::Parser->new(api_version => 3);

my $i;
$p->handler("default" =>
	    sub {
		my $p=shift;
	        #++$i; diag "$i @_";
		$p->eof;
	    }, "self, event");
$p->parse("Foo");
$p->eof;

# We used to sometimes trigger events after a handler signaled eof
my $title='';
$p = HTML::Parser->new(api_version => 3,);
$p->handler(start=> \&title_handler, 'tagname, self');
$p->parse("<head><title>foo</title>\n</head>");
is($title, "foo");

sub title_handler {
    return if shift ne 'title';
    my $self = shift; 
    $self->handler(text => sub { $title .= shift}, 'dtext');
    $self->handler(end => sub { shift->eof if shift eq 'title' }, 'tagname, self');
}