[gxml] Fixes on GElement's DomElement implementation for namespaced properties



commit 6b080a266950dd16e77594bc8cd0f815bb86a389
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Jul 20 13:40:28 2016 -0500

    Fixes on GElement's DomElement implementation for namespaced properties
    
    * Fixed GElement.set_ns_attr(),
    * Fixed GHashMapAttr.has_key() infinite loop
    * Fixed GElement.has_attribute_ns()
    * Added Unit Tests

 gxml/DomAttr.vala          |    2 +-
 gxml/GXmlElement.vala      |   14 ++++++++------
 gxml/GXmlHashMapAttr.vala  |    1 +
 test/DomGDocumentTest.vala |   35 +++++++++++++++++++++++++++--------
 4 files changed, 37 insertions(+), 15 deletions(-)
---
diff --git a/gxml/DomAttr.vala b/gxml/DomAttr.vala
index 8f52cac..7aaa860 100644
--- a/gxml/DomAttr.vala
+++ b/gxml/DomAttr.vala
@@ -20,7 +20,7 @@
  *      Daniel Espinosa <esodan gmail com>
  */
 
-public interface GXml.DomAttr {
+public interface GXml.DomAttr : Object {
   public abstract string? namespace_uri { owned get; }
   public abstract string? prefix { owned get; }
   public abstract string local_name { owned get; }
diff --git a/gxml/GXmlElement.vala b/gxml/GXmlElement.vala
index 1dc603d..b969b33 100644
--- a/gxml/GXmlElement.vala
+++ b/gxml/GXmlElement.vala
@@ -70,12 +70,12 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
     if (p == null) return null;
     return new GAttribute (_doc, p);
   }
-  public void set_ns_attr (string ns, string name, string value) {
+  public void set_ns_attr (string ns, string aname, string value) {
     if (_node == null) return;
     string prefix = null;
-    string qname = name;
-    if (":" in name) {
-      string[] s = ns.split(":");
+    string qname = aname;
+    if (":" in aname) {
+      string[] s = aname.split(":");
       if (s.length != 2) return;
       prefix = s[0];
       qname = s[1];
@@ -92,6 +92,7 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
   }
   public GXml.Node? get_ns_attr (string name, string uri) {
     if (_node == null) return null;
+    var ns = _node->doc->search_ns_by_href (_node, uri);
     var a = _node->has_ns_prop (name, uri);
     if (a == null) return null;
     return new GAttribute (_doc, a);
@@ -221,7 +222,7 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
   }
   public void set_attribute (string name, string? value) { set_attr (name, value); }
   public void set_attribute_ns (string? namespace, string name, string? value) {
-    set_ns_attr (namespace, local_name, value);
+    set_ns_attr (namespace, name, value);
   }
   public void remove_attribute (string name) {
     remove_attr (name);
@@ -231,7 +232,8 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
   }
   public bool has_attribute (string name) { return attrs.has_key (name); }
   public bool has_attribute_ns (string? namespace, string local_name) {
-    var attr = _node->has_ns_prop (name, namespace);
+    if (_node == null) return false;
+    var attr = _node->has_ns_prop (local_name, namespace);
     if (attr == null) return false;
     return true;
   }
diff --git a/gxml/GXmlHashMapAttr.vala b/gxml/GXmlHashMapAttr.vala
index 12d247d..0c0642f 100644
--- a/gxml/GXmlHashMapAttr.vala
+++ b/gxml/GXmlHashMapAttr.vala
@@ -93,6 +93,7 @@ public class GXml.GHashMapAttr : Gee.AbstractMap<string,GXml.Node>,
     var p = _node->properties;
     while (p != null) {
       if (p->name == key) return true;
+      p = p->next;
     }
     return false;
   }
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index 4e52075..1c5f96d 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -55,7 +55,7 @@ static const string HTMLDOC ="
        public static void add_tests () {
                Test.add_func ("/gxml/dom/document/children", () => {
                        GLib.message ("Doc: "+STRDOC);
-                       GDocument doc = new GDocument.from_string (STRDOC);
+                       var doc = new GDocument.from_string (STRDOC) as DomDocument;
                        DomElement root = doc.document_element;
                        assert (root != null);
                        assert (root is DomElement);
@@ -68,7 +68,7 @@ static const string HTMLDOC ="
                });
                Test.add_func ("/gxml/dom/document/child_nodes", () => {
                        GLib.message ("Doc: "+STRDOC);
-                       GDocument doc = new GDocument.from_string (STRDOC);
+                       var doc = new GDocument.from_string (STRDOC) as DomDocument;
                        assert (doc is DomDocument);
                        assert (doc.child_nodes != null);
                        assert (doc.child_nodes.size == 2);
@@ -84,7 +84,7 @@ static const string HTMLDOC ="
                });
                Test.add_func ("/gxml/dom/document/element_collections", () => {
                        GLib.message ("Doc: "+STRDOC);
-                       GDocument doc = new GDocument.from_string (HTMLDOC);
+                       var doc = new GDocument.from_string (HTMLDOC) as DomDocument;
                        assert (doc is DomDocument);
                        var le = doc.get_elements_by_tag_name ("p");
                        assert (le.size == 3);
@@ -103,7 +103,7 @@ static const string HTMLDOC ="
                });
                Test.add_func ("/gxml/dom/element/element_collections", () => {
                        GLib.message ("Doc: "+HTMLDOC);
-                       GDocument doc = new GDocument.from_string (HTMLDOC);
+                       var doc = new GDocument.from_string (HTMLDOC) as DomDocument;
                        assert (doc is DomDocument);
                        assert (doc.document_element.children.size == 1);
                        var le = doc.document_element.get_elements_by_tag_name ("p");
@@ -121,7 +121,7 @@ static const string HTMLDOC ="
                Test.add_func ("/gxml/dom/node", () => {
                        try {
                        Test.message ("Doc: "+HTMLDOC);
-                       GDocument doc = new GDocument.from_string (HTMLDOC);
+                       var doc = new GDocument.from_string (HTMLDOC) as DomDocument;
                        assert (doc is DomDocument);
                        assert (doc.document_element.children.size == 1);
                        assert (doc.document_element.children[0] != null);
@@ -253,13 +253,13 @@ static const string HTMLDOC ="
                        }
                });
                Test.add_func ("/gxml/dom/element/api", () => {
+                       try {
                        GLib.message ("Doc: "+HTMLDOC);
-                       GDocument doc = new GDocument.from_string (HTMLDOC);
+                       var doc = new GDocument.from_string (HTMLDOC) as DomDocument;
                        assert (doc is DomDocument);
                        assert (doc.document_element.children.size == 1);
                        var n1 = doc.create_element_ns ("http://live.gnome.org/GXml","gxml:code";);
                        doc.document_element.append_child (n1);
-                       GLib.message ("DOC:"+(doc.document_element as GXml.Node).to_string ());
                        var n = doc.document_element.children[1] as DomElement;
                        assert (n.node_name == "code");
                        n.set_attribute ("id","0y1");
@@ -267,7 +267,6 @@ static const string HTMLDOC ="
                        assert ((n as GXml.Node).namespaces.size == 1);
                        assert ((n as GXml.Node).namespaces[0].uri == "http://live.gnome.org/GXml";);
                        assert ((n as GXml.Node).namespaces[0].prefix == "gxml");
-                       GLib.message ("NODE: "+(n as GXml.Node).to_string ());
                        assert (n.namespace_uri == "http://live.gnome.org/GXml";);
                        assert (n.prefix == "gxml");
                        assert (n.local_name == "code");
@@ -286,6 +285,26 @@ static const string HTMLDOC ="
                        assert ((n as DomNode).lookup_namespace_uri ("gxml") == "http://live.gnome.org/GXml";);
                        assert ((n as DomNode).lookup_prefix ("http://live.gnome.org/GXml";) == "gxml");
                        assert (n.tag_name == "gxml:code");
+                       n.remove_attribute ("id");
+                       assert (n.get_attribute ("id") == null);
+                       assert (!n.has_attribute ("id"));
+                       var n2 = doc.create_element ("p");
+                       doc.document_element.append_child (n2);
+                       assert (doc.document_element.children.length == 3);
+                       GLib.message ("DOC:"+(doc.document_element as GXml.Node).to_string ());
+                       assert (n2.attributes.length == 0);
+                       GLib.message ("Setting nice NS");
+                       n2.set_attribute_ns ("http://devel.org/","dev:nice","good";);
+                       assert (n2.attributes.length == 1);
+                       assert (n2.has_attribute_ns ("http://devel.org/","nice";));
+                       assert (!n2.has_attribute_ns ("http://devel.org/","dev:nice";));
+                       GLib.message ("NODE:"+(n2 as GXml.Node).to_string ());
+                       assert (n2.get_attribute_ns ("http://devel.org/","nice";) == "good");
+                       assert (n2.get_attribute_ns ("http://devel.org/","dev:nice";) == null);
+                       } 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]