Commits

Shlomi Fish  committed e15b4eb

Fix XML::LibXML::Element == and eq comparison.

  • Participants
  • Parent commits 92496b3

Comments (0)

Files changed (3)

 Revision history for Perl extension XML::LibXML
 
+1.93
+    - Fix XML::LibXML::Element comparision with == and eq.
+        - Fixes https://rt.cpan.org/Ticket/Display.html?id=75257 ,
+        https://rt.cpan.org/Ticket/Display.html?id=75293 ,
+        https://rt.cpan.org/Ticket/Display.html?id=75259 .
+        - Thanks to Toby Inkster for a preliminary patch (that was modified by
+        me) and to the various people who reported the problem.
+
 1.92            Tue Feb 21 19:00:48 IST 2012
     - Fix for test failure on perls < 5.10.
         - Fixes https://rt.cpan.org/Public/Bug/Display.html?id=75195
 use XML::LibXML::AttributeHash;
 use Carp;
 
+use Scalar::Util qw(blessed);
+
 use overload
     '%{}'  => 'getAttributeHash',
     'bool' => sub { 1 },
+    'eq' => '_isSameNodeLax', '==' => '_isSameNodeLax',
     ;
 
+sub _isSameNodeLax { 
+    my ($self, $other) = @_;
+
+    if (blessed($other) and $other->isa('XML::LibXML::Element')) 
+    { 
+        return ($self->isSameNode($other) ? 1 : ''); 
+    }
+    else
+    {
+        return '';
+    }
+}
+
 {
     # Note that we could generate a new hashref each time this
     # is called. However, that breaks "each %$element" and

File t/71overloads.t

 use strict;
 use warnings;
-use Test::More tests => 16;
+use Test::More tests => 22;
 use XML::LibXML;
 
 my $root = XML::LibXML->load_xml( IO => \*DATA )->documentElement;
     '!!! toStringEC14N',
     );
 
+# TEST
+is_deeply(
+    [($root == $root)],
+    [1],
+    '== comparison',
+);
+
+# TEST
+is_deeply(
+    [($root eq $root)],
+    [1],
+    'eq comparison',
+);
+
+# TEST
+is_deeply(
+    [($root == 'not-root')],
+    [''],
+    '== negative comparison',
+);
+
+# TEST
+is_deeply(
+    [($root eq 'not-root')],
+    [''],
+    'eq negative comparison',
+);
+
+{
+    my $doc = XML::LibXML->load_xml( string => <<'EOT' )->documentElement;
+<foo>
+    <bar />
+    <baz />
+</foo>
+EOT
+
+    my ($bar_elem) = $doc->findnodes('//bar');
+    my ($baz_elem) = $doc->findnodes('//baz');
+
+    # TEST
+    is_deeply([$bar_elem == $baz_elem], [''],
+        '== comparison between two differenet nodes'
+    );
+
+    # TEST
+    is_deeply([$bar_elem eq $baz_elem], [''],
+        'eq comparison between two differenet nodes'
+    );
+}
 __DATA__
 <root attr1="foo" xmlns:x="http://localhost/" x:attr2="bar" />