[gxml] Fixed GDocument's DomDocument.import_node() and adopt_node()



commit debbe939b60c8d3d79c716e230636c84c664e53f
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Jul 20 23:34:19 2016 -0500

    Fixed GDocument's DomDocument.import_node() and adopt_node()

 gxml/GXmlDocument.vala     |   32 ++++++++++++++++++++++++++------
 gxml/Node.vala             |    4 ++--
 test/DomGDocumentTest.vala |   18 ++++++++++++++++++
 3 files changed, 46 insertions(+), 8 deletions(-)
---
diff --git a/gxml/GXmlDocument.vala b/gxml/GXmlDocument.vala
index 6eac371..1630721 100644
--- a/gxml/GXmlDocument.vala
+++ b/gxml/GXmlDocument.vala
@@ -266,21 +266,41 @@ public class GXml.GDocument : GXml.GNode,
   public DomNode import_node (DomNode node, bool deep = false) throws GLib.Error {
       if (node is DomDocument)
         throw new GXml.DomError.NOT_SUPPORTED_ERROR (_("Can't import a Document"));
-      var dst = this.create_element (node.node_name);
-      if (document_element == null)
-        this.append_child (dst as DomNode);
-      else
+      if (!(node is DomElement) && this.document_element == null)
+        throw new GXml.DomError.HIERARCHY_REQUEST_ERROR (_("Can't import a non Element type node to a 
Document"));
+      GXml.DomNode dst = null;
+      if (node is DomElement) {
+        dst = (this as DomDocument).create_element (node.node_name);
+        GXml.Node.copy (this, (GXml.Node) dst, (GXml.Node) node, deep);
+        if (document_element == null) {
+          this.append_child (dst);
+          return dst;
+        }
+      }
+      if (node is DomText)
+        dst = this.create_text_node ((node as DomText).data);
+      if (node is DomComment)
+        dst = (this as DomDocument).create_comment ((node as DomComment).data);
+      if (node is DomProcessingInstruction)
+        dst = this.create_processing_instruction ((node as DomProcessingInstruction).target, (node as 
DomProcessingInstruction).data);
+      if (dst != null) {
         document_element.append_child (dst as DomNode);
-      GXml.Node.copy (this, dst, (GXml.Node) node, deep);
-      return (DomNode) dst;
+        return dst;
+      }
+      return node;
   }
   public DomNode adopt_node (DomNode node) throws GLib.Error {
       if (node is DomDocument)
         throw new GXml.DomError.NOT_SUPPORTED_ERROR (_("Can't adopt a Document"));
+      if (this == node.owner_document) return node;
       var dst = this.create_element (node.node_name);
       GXml.Node.copy (this, dst, (GXml.Node) node, true);
       if (node.parent_node != null)
         node.parent_node.child_nodes.remove_at (node.parent_node.child_nodes.index_of (node));
+      if (this.document_element == null)
+        this.append_child (dst as DomNode);
+      else
+        document_element.append_child (dst as DomNode);
       return (DomNode) dst;
   }
 
diff --git a/gxml/Node.vala b/gxml/Node.vala
index 442fa8b..b8db74f 100644
--- a/gxml/Node.vala
+++ b/gxml/Node.vala
@@ -155,9 +155,9 @@ public interface GXml.Node : Object
    */
   public virtual string ns_uri () { return namespaces.first ().uri; }
   /**
-   * Copy a {@link GXml.Node} relaing on {@link GXml.Document} to other {@link GXml.Node}.
+   * Copy a {@link GXml.Node} relaying on {@link GXml.Document} to other {@link GXml.Node}.
    *
-   * node could belongs from different {@link GXml.Document}, while source is a node
+   * @node could belongs from different {@link GXml.Document}, while source is a node
    * belonging to given document.
    *
    * Just {@link GXml.Element} objects are supported. For attributes, use
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index 26e8ad7..4deb2f1 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -422,6 +422,24 @@ static const string XMLDOC ="<?xml version=\"1.0\"?>
                                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);
+                               //TODO: import text, comment and pi
+                       } catch (GLib.Error e) {
+                               GLib.message ("Error: "+ e.message);
+                               assert_not_reached ();
+                       }
+               });
+               Test.add_func ("/gxml/dom/document/adopt", () => {
+                       try {
+                               GLib.message ("Doc: "+XMLDOC);
+                               var doc = new GDocument.from_string (XMLDOC) as DomDocument;
+                               var doc2 = new GDocument.from_string (STRDOC) as DomDocument;
+                               doc2.adopt_node (doc.document_element.children.last ());
+                               GLib.message ("DOC: "+(doc.document_element as GXml.Node).to_string ());
+                               GLib.message ("DOC: "+(doc2.document_element as GXml.Node).to_string ());
+                               assert (doc.document_element.children.last ().node_name == "project");
+                               assert (doc2.document_element.last_child is DomElement);
+                               assert (doc2.document_element.last_child.node_name == "Author");
+                               //TODO: adopt text, comment and pi
                        } 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]