Subject: | [PATCH] Add children_trimmed_text method to XML::Twig::Elt |
The XML::Twig::Element objects do not provide a children_trimmed_text method. I'd have expected this method to be similar to children_text but trimming each text it returns. I thought this could be useful because there's a trimming analog to the first_child_text etc methods too.
I provide a patch to make such a method. The patch is for the stable version 3.16 as I've downloaded it from the CPAN. Apply the patch with -p0 (if you are in the parent directory of XML-Twig-3.16). After that, you'll have to remake Twig.pm (from Twig.pm.slow). The patch also includes a new test too.
Code that reproduces this bug:
perl -we 'use XML::Twig; $t = XML::Twig->new; $t->parse("<o><e> hell </e><i> foo </i><e> o, world</e></o>"); print $t->root->children_trimmed_text("e"), $/;'
Error message this produces on the original version:
Can't locate object method "children_trimmed_text" via package "XML::Twig::Elt" at -e line 1.
Correct output it produces after the patch:
hello, world
Details about the environment:
perl, v5.8.5 built for i686-linux
XML::Parser version is 2.34
diff -ur XML-Twig-3.16-org/Twig.pm.slow XML-Twig-3.16/Twig.pm.slow
--- XML-Twig-3.16-org/Twig.pm.slow 2005-02-11 10:56:02.000000000 +0100
+++ XML-Twig-3.16/Twig.pm.slow 2005-05-03 20:50:07.000000000 +0200
@@ -4610,6 +4610,11 @@
return map { $_->text} $elt->children( @_);
}
+sub children_trimmed_text
+ { my $elt= shift;
+ return map { $_->trimmed_text} $elt->children( @_);
+ }
+
sub all_children_are
{ my( $parent, $cond)= @_;
foreach my $child ($parent->children)
diff -ur XML-Twig-3.16-org/t/test_additional.t XML-Twig-3.16/t/test_additional.t
--- XML-Twig-3.16-org/t/test_additional.t 2005-02-02 14:24:49.000000000 +0100
+++ XML-Twig-3.16/t/test_additional.t 2005-05-03 21:07:17.000000000 +0200
@@ -22,7 +22,7 @@
{ $open= eval( 'sub { open( $_[0], $_[1], $_[2]) }'); }
}
-my $TMAX=647;
+my $TMAX=648;
print "1..$TMAX\n";
@@ -202,6 +202,9 @@
$text= join( '-', $root->children_text( qr/elt/));
is( $text, ' text - text text', "children_text"); # test 33
+
+$text= join( '-', $root->children_trimmed_text( qr/elt/));
+is( $text, 'text-text text', "children_trimmed_text"); # test 33.1
}
# test _ancestors _children _descendants