diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2013-05-08 22:21:52 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2013-05-08 22:21:52 +0000 |
commit | 2f253cfc85ffd55a8acb988e91f0bc5ab348124c (patch) | |
tree | 4734ccd522c71dd455879162006742002f8c1565 /t/handler-eof.t | |
download | HTML-Parser-tarball-master.tar.gz |
HTML-Parser-3.71HEADHTML-Parser-3.71master
Diffstat (limited to 't/handler-eof.t')
-rw-r--r-- | t/handler-eof.t | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/t/handler-eof.t b/t/handler-eof.t new file mode 100644 index 0000000..39419dc --- /dev/null +++ b/t/handler-eof.t @@ -0,0 +1,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'); +} |