[gxml] GOM: Improved creating/appending ns elements



commit 5cda12afeac1201bd010d18fa0178c4c4fca756b
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Nov 3 11:52:22 2016 -0600

    GOM: Improved creating/appending ns elements
    
    Namespaced elements don't try to add a namespace
    declaration any more, when it is appended to
    parent and no namespace is found
    
    Added error validations to GomDocument.create_element
    for namespace URI and prefix
    
    Updated Unit tests

 gxml/GomDocument.vala     |   15 +++++++++++----
 gxml/GomNode.vala         |    9 ---------
 test/GomDocumentTest.vala |   16 ++++++++++++++--
 3 files changed, 25 insertions(+), 15 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index d14e088..8f36b00 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -110,7 +110,7 @@ public class GXml.GomDocument : GomNode,
   public DomElement create_element (string local_name) throws GLib.Error {
     return new GomElement (this, local_name);
   }
-  public DomElement create_element_ns (string? ns, string qualified_name) throws GLib.Error
+  public DomElement create_element_ns (string? namespace_uri, string qualified_name) throws GLib.Error
   {
     string n = "";
     string nsp = "";
@@ -123,11 +123,18 @@ public class GXml.GomDocument : GomNode,
       n = s[1];
     } else
       n = qualified_name;
-    if (nsp == "" && ns == null)
+    if (nsp == "" && namespace_uri == null)
       throw new DomError.NAMESPACE_ERROR
         (_("Creating an namespaced element with invalid namespace"));
-      // TODO: check for xmlns https://www.w3.org/TR/dom/#dom-document-createelementns
-    return new GomElement.namespace (this, ns, nsp, n);
+    if ((n == "xmlns" || nsp == "xmlns")
+        && namespace_uri != "http://www.w3.org/2000/xmlns/";)
+      throw new DomError.NAMESPACE_ERROR
+        (_("Invalid namespace URI for xmlns prefix. Use http://www.w3.org/2000/xmlns/";));
+    if ((n != "xmlns" || nsp != "xmlns")
+        && namespace_uri == "http://www.w3.org/2000/xmlns/";)
+      throw new DomError.NAMESPACE_ERROR
+        (_("Only xmlns prefixs can be used with http://www.w3.org/2000/xmlns/";));
+    return new GomElement.namespace (this, namespace_uri, nsp, n);
   }
 
   public DomHTMLCollection get_elements_by_tag_name (string local_name) {
diff --git a/gxml/GomNode.vala b/gxml/GomNode.vala
index 8b8bceb..6739fc2 100644
--- a/gxml/GomNode.vala
+++ b/gxml/GomNode.vala
@@ -210,15 +210,6 @@ public class GXml.GomNode : Object,
       if (e.namespace_uri != null || e.prefix != null) {
         string nsprefix = node.lookup_prefix (e.namespace_uri);
         string nsuri = node.lookup_namespace_uri (e.prefix);
-        if (nsprefix == null && nsuri == null
-            && e.namespace_uri != null) {
-          string name = "xmlns";
-          if (e.prefix != null)
-            name = name + e.prefix;
-          if (e.get_attribute_ns (e.namespace_uri, e.prefix) == null)
-            e.set_attribute_ns ("http://www.w3.org/2000/xmlns/";,
-                              name, e.namespace_uri);
-        }
         if (nsprefix != null && nsprefix != e.prefix) {
           throw new DomError.NAMESPACE_ERROR
             (_("Trying to add a namespaced element to a parent with invalid prefix for namesapce %s ")
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index c456f73..a152b1a 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -345,11 +345,13 @@ class GomDocumentTest : GXmlTest {
                                assert (doc.document_element.child_nodes.size == 1);
                                var c = doc.document_element.child_nodes[0] as DomElement;
                                assert (c is DomElement);
-                               c.set_attribute_ns ("http://www.w3.org/2000/xmlns/","xmlns:gxml2";, 
"http://www.gnome.org/GXml2";);
+                               c.set_attribute_ns ("http://www.w3.org/2000/xmlns/","xmlns:gxml2";,
+                                                                                                             
  "http://www.gnome.org/GXml2";);
                                assert (c.prefix == null);
                                assert (c.namespace_uri == null);
                                c.set_attribute_ns ("http://www.gnome.org/GXml2","gxml2:prop","val";);
-                               var p = (c as DomElement).get_attribute_ns ("http://www.gnome.org/GXml2";, 
"prop");
+                               var p = (c as DomElement)
+                                                                       .get_attribute_ns 
("http://www.gnome.org/GXml2";, "prop");
                                assert (p != null);
                                assert (p == "val");
                                assert (doc.document_element.lookup_namespace_uri (null) != null);
@@ -360,6 +362,16 @@ class GomDocumentTest : GXmlTest {
                                assert (c.lookup_namespace_uri ("gxml2") == "http://www.gnome.org/GXml2";);
                                assert (c.lookup_prefix ("http://www.gnome.org/GXml3";) == null);
                                assert (c.lookup_prefix ("http://www.gnome.org/GXml2";) == "gxml2");
+                               var c2 = doc.create_element_ns ("http://www.gnome.org/GXml/testing";,
+                                                                                                             
                                                  "t:child2");
+                               assert (c2.prefix == "t");
+                               assert (c2.namespace_uri == "http://www.gnome.org/GXml/testing";);
+                               assert (c2.attributes.size == 0);
+                               doc.document_element.append_child (c2);
+                               assert (c2.prefix == "t");
+                               assert (c2.namespace_uri == "http://www.gnome.org/GXml/testing";);
+                               assert (c2.get_attribute_ns ("http://www.w3.org/2000/xmlns/";,
+                                                                                                             
                                   "t") == 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]