use strict; use Test::More; use HTML::TagFilter; BEGIN { plan (tests => 4); } my $tf = HTML::TagFilter->new( log_rejects => 0, strip_comments => 1, on_start_document => sub { my ($self, $rawtext) = @_; $self->{_tag_stack} = []; $$rawtext .= " on_start_document"; }, ); my $tf2 = HTML::TagFilter->new( log_rejects => 0, strip_comments => 1, verbose => 1, on_start_document => sub { my ($self, $rawtext) = @_; $self->{_tag_stack} = []; return; }, on_open_tag => sub { my ($self, $tag, $attributes, $sequence) = @_; push @{ $self->{_tag_stack} }, $$tag unless grep {$_ eq $$tag} qw(img br hr meta link); return; }, on_close_tag => sub { my ($self, $tag) = @_; unless (@{ $self->{_tag_stack} } && grep {$_ eq $$tag} @{ $self->{_tag_stack} }) { undef ${ $tag }; return; } my @unclosed; while (my $lasttag = pop @{ $self->{_tag_stack} }) { return join '', map "", @unclosed if $lasttag eq $$tag; push @unclosed, $lasttag; } }, on_finish_document => sub { my ($self, $cleantext) = @_; return join '', map "", reverse @{ $self->{_tag_stack} }; }, ); is( $tf->filter(qq|wake up|), qq|wake up on_start_document|, "basic callbacks working. text modified."); is( $tf2->filter( qq|

Mother, I can feel the soil falling over my head|), qq|

Mother, I can feel the soil falling over my head

|, "callbacks: unclosed tags closed"); is( $tf2->filter( qq|

Mother, I can feel the soil falling over my head|), qq|

Mother, I can feel the soil falling over my head

|, "callbacks: unopened tags omitted"); is( $tf2->filter( qq|

Mother, I can feel the soil falling over my head|), qq|

Mother, I can feel the soil falling over my head

|, "callbacks: bad nesting mended");