Subject: | Comments always dropped after a twig object set 'comments' to 'drop' |
Hi,
I've noticed that if you create an XML::Twig object with 'comments' set to 'drop', then all subsequent XML::Twig objects also drop comments, even if they have set 'comments' to 'keep'. It looks like this is because the code deletes the twig handler.
Below is a small test script to demonstrate the problem. I've attached a diff against Twig.pm.slow that ships with version 3.10 that fixes this problem.
Thanks for this great module.
Simon Flack
#!/usr/local/bin/perl -w
use strict;
use XML::Twig;
use Test::Simple tests => 3;
my $xml = <<XML_TEST;
<xml_root>
<!-- some comment -->
<key>value</key>
</xml_root>
XML_TEST
{
my $twig1 = XML::Twig->new(comments => 'keep', keep_spaces => 1);
$twig1->parse($xml);
ok ($twig1->sprint() =~ /<!--.*-->/s, 'keep comments');
#print $twig1->sprint, "\n", '-'x80, "\n"; # keeps comments ok
$twig1->dispose;
}
{
my $twig2 = XML::Twig->new(comments => 'drop', keep_spaces => 1);
$twig2->parse($xml);
ok ($twig2->sprint() !~ /<!--.*-->/s, 'drop comments');
#print $twig2->sprint, "\n", '-'x80, "\n"; # drops comments ok
$twig2->dispose;
}
{
my $twig3 = XML::Twig->new(comments => 'keep', keep_spaces => 1);
$twig3->parse($xml);
ok ($twig3->sprint() =~ /<!--.*-->/s, 'keep comments');
#print $twig3->sprint, "\n", '-'x80, "\n"; # drops comments!!
$twig3->dispose;
}
--- Twig.pm.slow-3.10 Sun Jun 8 23:01:16 2003
+++ Twig.pm.slow Wed Sep 3 18:26:00 2003
@@ -464,5 +464,5 @@
$args{Comments}||= $COMMENTS_DEFAULT;
if( $args{Comments} eq 'drop')
- { delete $twig_handlers{Comment}; }
+ { $self->{twig_keep_comments}= 0; }
elsif( $args{Comments} eq 'keep')
{ $self->{twig_keep_comments}= 1; }
@@ -1731,4 +1731,5 @@
my( $p, $data)= @_;
my $t=$p->{twig};
+ return unless $t->{twig_keep_comments};
$data= $t->{twig_input_filter}->( $data) if( $t->{twig_input_filter});