[gxml] Fixed GDocument's implementation of DomDocument.import_node()



commit 3712dfe4496880a8ed111758130e1184be4c6688
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Jul 20 18:49:51 2016 -0500

    Fixed GDocument's implementation of DomDocument.import_node()
    
    * Fixed GXml.Node.copy() to honors deep paramenters, just copying
      attributes if false and all childs if true
    * Added unit tests

 gxml/GXmlDocument.vala     |    6 +++++-
 gxml/Node.vala             |    7 +------
 test/DomGDocumentTest.vala |   28 +++++++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 8 deletions(-)
---
diff --git a/gxml/GXmlDocument.vala b/gxml/GXmlDocument.vala
index 282779a..6eac371 100644
--- a/gxml/GXmlDocument.vala
+++ b/gxml/GXmlDocument.vala
@@ -267,7 +267,11 @@ public class GXml.GDocument : GXml.GNode,
       if (node is DomDocument)
         throw new GXml.DomError.NOT_SUPPORTED_ERROR (_("Can't import a Document"));
       var dst = this.create_element (node.node_name);
-      GXml.Node.copy (this, dst, (GXml.Node) node, true);
+      if (document_element == null)
+        this.append_child (dst as DomNode);
+      else
+        document_element.append_child (dst as DomNode);
+      GXml.Node.copy (this, dst, (GXml.Node) node, deep);
       return (DomNode) dst;
   }
   public DomNode adopt_node (DomNode node) throws GLib.Error {
diff --git a/gxml/Node.vala b/gxml/Node.vala
index d8b7ddc..442fa8b 100644
--- a/gxml/Node.vala
+++ b/gxml/Node.vala
@@ -181,6 +181,7 @@ public interface GXml.Node : Object
       foreach (GXml.Node p in source.attrs.values) {
         ((GXml.Element) node).set_attr (p.name, p.value); // TODO: Namespace
       }
+      if (!deep) return true;
 #if DEBUG
       GLib.message ("Copying source's child nodes to destiny node");
 #endif
@@ -190,12 +191,6 @@ public interface GXml.Node : Object
 #if DEBUG
             GLib.message (@"Copying child Element node: $(c.name)");
 #endif
-          if (!deep){
-#if DEBUG
-            GLib.message (@"No deep copy was requested, skeeping node $(c.name)");
-#endif
-            continue;
-          }
           try {
             var e = doc.create_element (c.name); // TODO: Namespace
             node.children_nodes.add (e);
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index 18ad1bd..26e8ad7 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -383,7 +383,6 @@ static const string XMLDOC ="<?xml version=\"1.0\"?>
                                assert (lec.item (0) is DomElement);
                                assert (lec.item (0).node_name == "code");
                                n.set_attribute ("class","node parent");
-                               GLib.message ("DOC: "+(doc.document_element as GXml.Node).to_string ());
                                var lec2 = doc.get_elements_by_class_name ("parent");
                                assert (lec2.length == 4);
                                assert (lec2.item (0) is DomElement);
@@ -396,6 +395,33 @@ static const string XMLDOC ="<?xml version=\"1.0\"?>
                                assert (lec4.length == 4);
                                assert (lec4.item (0) is DomElement);
                                assert (lec4.item (0).node_name == "code");
+                               var t = doc.create_text_node ("TEXT");
+                               n.append_child (t);
+                               assert (n.child_nodes[0] is DomText);
+                               assert (n.child_nodes[0].node_value == "TEXT");
+                               var comment = doc.create_comment ("COMMENT");
+                               doc.document_element.append_child (comment);
+                               assert (doc.document_element.last_child is DomComment);
+                               assert (doc.document_element.last_child.node_value == "COMMENT");
+                               var pi = doc.create_processing_instruction ("git","commit");
+                               doc.document_element.append_child (pi);
+                               GLib.message ("DOC: "+(doc.document_element as GXml.Node).to_string ());
+                               assert (doc.document_element.last_child is DomProcessingInstruction);
+                       } catch (GLib.Error e) {
+                               GLib.message ("Error: "+ e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/dom/document/import", () => {
+                       try {
+                               GLib.message ("Doc: "+XMLDOC);
+                               var doc = new GDocument.from_string (XMLDOC) as DomDocument;
+                               var doc2 = new GDocument.from_string (STRDOC) as DomDocument;
+                               doc.import_node (doc2.document_element, false);
+                               GLib.message ("DOC: "+(doc.document_element as GXml.Node).to_string ());
+                               assert (doc.document_element.last_child is DomElement);
+                               assert (doc.document_element.last_child.node_name == "Sentences");
+                               assert (doc.document_element.last_child.child_nodes.length == 0);
                        } catch (GLib.Error e) {
                                GLib.message ("Error: "+ e.message);
                                assert_not_reached ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]