1. Shlomi Fish
  2. perl-XML-LibXML

Commits

ph...@9ae0c189-cd1f-4510-a509-f4891f5cf20d  committed 203a61e

Modified Files:
LibXML.xs perl-libxml-mm.c
[fix] clone node bug with namespaces

t/04node.t
test the cloning of namespaces.

  • Participants
  • Parent commits dc52270
  • Branches default

Comments (0)

Files changed (3)

File LibXML.xs

View file
                 xmlSetTreeDoc(ret, doc);
             }
             
-            # make namespaces available
-            ret->ns = self->ns;
             docfrag = PmmNewFragment( doc );
             xmlAddChild( PmmNODE(docfrag), ret );
             RETVAL = PmmNodeToSv(ret, docfrag);
         }
 
 SV *
-getNamespace( pnode )
-        SV * pnode
+getNamespace( node )
+        xmlNodePtr node
     ALIAS:  
         localNamespace = 1
         localNS        = 2
     PREINIT:
-        xmlNodePtr node;
         xmlNsPtr ns = NULL;
         xmlNsPtr newns = NULL;
         const char * class = "XML::LibXML::Namespace";
-    INIT:
-        node = PmmSvNode(pnode);
-        if ( node == NULL ) {
-            croak( "lost node" );
-        }
     CODE:
         ns = node->ns;
         if ( ns != NULL ) {
+            warn( "node has namespace! %s", ns->prefix );
             newns = xmlCopyNamespace(ns);
             if ( newns != NULL ) {
                 RETVAL = NEWSV(0,0);
                                       );
             }
         }
+        else {
+            warn( "node has no namespace!" );
+            XSRETURN_UNDEF;
+        }
     OUTPUT:
         RETVAL
         

File perl-libxml-mm.c

View file
         case XML_DOCUMENT_NODE:
         case XML_HTML_DOCUMENT_NODE:
         case XML_DOCB_DOCUMENT_NODE:
-            dfProxy->encoding = (int)xmlParseCharEncoding( (const char*)((xmlDocPtr)node)->encoding );
+            if ( ((xmlDocPtr)node)->encoding != NULL ) {
+                dfProxy->encoding = (int)xmlParseCharEncoding( (const char*)((xmlDocPtr)node)->encoding );
+            }
             break;
         default:
             break;
     if ( node != NULL ) {
         switch ( node->type ) {
         case XML_ELEMENT_NODE:
+            retval = xmlCopyNode( node, recursive );
+            xmlReconciliateNs(retval->doc, retval);
+            break;
 		case XML_TEXT_NODE:
 		case XML_CDATA_SECTION_NODE:
 		case XML_ENTITY_REF_NODE:
             break;
 		case XML_ATTRIBUTE_NODE:
             retval = (xmlNodePtr) xmlCopyProp( NULL, (xmlAttrPtr) node );
+            xmlReconciliateNs(retval->doc, retval);
             break;
         case XML_DOCUMENT_NODE:
 		case XML_HTML_DOCUMENT_NODE:
         coder =xmlFindCharEncodingHandler( (const char *)encoding );
     }
     else {
-        xs_warn("no encoding found\n");
+        xs_warn("no encoding found \n");
     }
 
     if ( coder != NULL ) {

File t/04node.t

View file
 
 use Test;
 
-BEGIN { plan tests => 122 };
+BEGIN { plan tests => 125 };
 use XML::LibXML;
 use XML::LibXML::Common qw(:libxml);
 
             ok( scalar(@cn), 1);
             ok( $cn[0]->nodeName, "bar" );
             ok( !$cn[0]->isSameNode( $c1node ) );
+
+            print "# clone namespaced elements\n";
+            my $nsnode = $doc->createElementNS( "foo", "foo:bar" );
+
+            my $cnsnode = $nsnode->cloneNode(0);
+            ok( $cnsnode->nodeName, "bar" );
+            ok( $cnsnode->localNS(), undef );
+
+            print "# clone namespaced elements (recursive)\n";
+            my $c2nsnode = $nsnode->cloneNode(1);
+            ok( $c2nsnode->toString(), $nsnode->toString() );
         }
 
         print "# 1.3 Node Value\n";