xmltwig icon indicating copy to clipboard operation
xmltwig copied to clipboard

#TEXT handler not called if handler is defined after twig creation

Open chrispitude opened this issue 5 years ago • 0 comments

If a handler is defined for #TEXT elements, the handler works if it's defined during twig creation using XML::Twig->new(twig_handlers => ...), but not if applied to the twig afterward using $twig->setTwigHandlers().

In the following testcase, note that TEXT and SPAN elements are created by the handlers in "Good", but only SPAN elements are created in "Bad":

Good:
<ul>
  <li>This is <SPAN><ph><TEXT>item</TEXT></ph></SPAN><TEXT> 1.</TEXT></li>
  <li>This is <SPAN><ph><TEXT>item</TEXT></ph></SPAN><TEXT> 2.</TEXT></li>
</ul>

Bad:
<ul>
  <li>This is <SPAN><ph>item</ph></SPAN> 1.</li>
  <li>This is <SPAN><ph>item</ph></SPAN> 2.</li>
</ul>

Testcase script:

#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;

my $xml = <<EOF;
<ul>
 <li>This is <ph>item</ph> 1.</li>
 <li>This is <ph>item</ph> 2.</li>
</ul>
EOF

# good
{
my $twig = XML::Twig->new(
 twig_handlers => {
  '#TEXT' => sub { $_->wrap_in('TEXT'); return 1; },
  'ph' => sub { $_->wrap_in('SPAN'); return 1; },
 },
 )->parse($xml);

print "Good:\n".$twig->sprint(pretty_print => 'indented')."\n";
}

# bad
{
my $twig = XML::Twig->new();
$twig->setTwigHandlers({
 '#TEXT' => sub { $_->wrap_in('TEXT'); return 1; },
 'ph' => sub { $_->wrap_in('SPAN'); return 1; },
 });
$twig->parse($xml);

print "Bad:\n".$twig->sprint(pretty_print => 'indented')."\n";
}

chrispitude avatar Jun 16 '19 13:06 chrispitude