[gxml] Fixed GElement implementation of DomElement for namespace_uri and prefix



commit d27729259355a5193ca1e34debdcd3edc2b6e6b9
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Jul 20 00:17:22 2016 -0500

    Fixed GElement implementation of DomElement for namespace_uri and prefix
    
    * Added Unit tests namespace_uri, prefix, id, node_name, local_name,
      id, class_name, class_list and attributes list

 gxml/DomElement.vala       |    6 ++++++
 gxml/GXmlElement.vala      |   21 +++++++++++++++++----
 test/DomGDocumentTest.vala |   31 +++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 4 deletions(-)
---
diff --git a/gxml/DomElement.vala b/gxml/DomElement.vala
index fd74379..991dbf8 100644
--- a/gxml/DomElement.vala
+++ b/gxml/DomElement.vala
@@ -26,7 +26,13 @@ public interface GXml.DomElement : GLib.Object,
                   GXml.DomNonDocumentTypeChildNode,
                   GXml.DomChildNode
 {
+  /**
+   * Returns default namespace's uri defined in node or first found.
+   */
   public abstract string? namespace_uri { owned get; }
+  /**
+   * Returns default namespace's prefix defined in node or first found.
+   */
   public abstract string? prefix { owned get; }
   public abstract string local_name { owned get; }
   public abstract string tag_name { owned get; }
diff --git a/gxml/GXmlElement.vala b/gxml/GXmlElement.vala
index 1adc25a..fd71fcc 100644
--- a/gxml/GXmlElement.vala
+++ b/gxml/GXmlElement.vala
@@ -135,16 +135,29 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
   public string? namespace_uri {
     owned get {
       if (_node == null) return null;
-      if (_node->ns != null)
-          return _node->ns->href.dup ();
+      var ns = _node->ns_def;
+      var dns = ns;
+      while (ns != null) {
+        if (ns->prefix == null) dns = ns;
+        ns = ns->next;
+      }
+      if (dns != null)
+        return dns->href.dup ();
       return null;
     }
   }
   public string? prefix {
     owned get {
       if (_node == null) return null;
-      if (_node->ns != null)
-          return _node->ns->prefix.dup ();
+      var ns = _node->ns_def;
+      var dns = ns;
+      while (ns != null) {
+        if (ns->prefix == null) dns = ns;
+        ns = ns->next;
+      }
+      if (dns != null)
+        if (dns->prefix != null)
+          return dns->prefix.dup ();
       return null;
     }
   }
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index 80952ce..d816371 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -246,5 +246,36 @@ static const string HTMLDOC ="
                                assert_not_reached ();
                        }
                });
+               Test.add_func ("/gxml/dom/element/api", () => {
+                       GLib.message ("Doc: "+HTMLDOC);
+                       GDocument doc = new GDocument.from_string (HTMLDOC);
+                       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");
+                       n.set_attribute ("class","login black");
+                       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");
+                       assert (n.node_name == "code");
+                       assert (n.id == "0y1");
+                       assert (n.class_list != null);
+                       assert (n.class_list.length == 2);
+                       assert (n.class_list.item (0) == "login");
+                       assert (n.class_list.item (1) == "black");
+                       assert (n.attributes != null);
+                       assert (n.attributes.length == 2);
+                       assert (n.attributes.get_named_item ("id") is DomNode);
+                       assert (n.attributes.get_named_item ("id").node_name == "id");
+                       assert (n.attributes.get_named_item ("id").node_value == "0y1");
+               });
        }
 }


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