Subject: | XML::Stream::Node copy mishandles cdata |
Date: | Tue, 04 Dec 2007 14:18:52 -0500 |
To: | bug-XML-Stream [...] rt.cpan.org |
From: | Tom Samstag <perl [...] modtwo.com> |
The copy subroutine in XML::Stream::Node handles cdata children
incorrectly. For reference, relevant system information:
XML::Stream v. 1.22
perl, v5.8.8 built for i686-linux
Linux flexo 2.6.22-gentoo-r5 #4 SMP Mon Sep 24 21:23:44 EDT 2007 i686
Intel(R) Pentium(R) 4 CPU 1.60GHz GenuineIntel GNU/Linux
An example program and the one-line fix are as follows:
use strict;
use XML::Stream;
use XML::Stream::Node;
my $a = new XML::Stream::Node;
$a->set_tag("body");
$a->add_cdata("one");
$a->add_child("a","two")->put_attrib(href=>"http://www.google.com");
$a->add_cdata("three");
print $a->GetXML() . "\n";
# correctly prints:
# <body>one<a href='http://www.google.com'>two</a>three</body>
my $b = $a->copy;
print $b->GetXML() . "\n";
# INCORRECTLY prints:
# <body>onethree<a href='http://www.google.com'>two</a>onethree</body>
my $c = $a->newcopy;
print $c->GetXML() . "\n";
# correctly prints:
# <body>one<a href='http://www.google.com'>two</a>three</body>
package XML::Stream::Node;
# a identical to sub copy in XML::Stream::Node except for the one line,
# which was changed as commented
sub newcopy
{
my $self = shift;
my $new_node = new XML::Stream::Node();
$new_node->set_tag($self->get_tag());
$new_node->put_attrib($self->attrib());
foreach my $child ($self->children())
{
if ($child->get_tag() eq
"__xmlstream__:node:cdata")
{
# was: $new_node->add_cdata($self->get_cdata());
$new_node->add_cdata($child->children());
}
else
{
$new_node->add_child($child->copy());
}
}
return $new_node;
}