[gxml] Fixes on GDocument's implementation of DomDocument: properties and element collections



commit 57118587d73536a4a777937242c0ba0f8d68bfcf
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Jul 20 17:36:57 2016 -0500

    Fixes on GDocument's implementation of DomDocument: properties and element collections
    
    * Fixed setting values by default for properties
    * Fixed get_elements_by_tag_name(), get_elements_by_tag_name_ns()
      and get_elements_by_class_name()
    * Fixed doctype searching for first Doctype node

 gxml/GXmlDocument.vala     |   34 +++++++++++++++++-------
 test/DomGDocumentTest.vala |   62 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 10 deletions(-)
---
diff --git a/gxml/GXmlDocument.vala b/gxml/GXmlDocument.vala
index 21cd496..282779a 100644
--- a/gxml/GXmlDocument.vala
+++ b/gxml/GXmlDocument.vala
@@ -188,22 +188,27 @@ public class GXml.GDocument : GXml.GNode,
   }
   // DomDocument implementation
   protected Implementation _implementation = new Implementation ();
-  protected string _url = "";
-  protected string _document_uri = "";
+  protected string _url = "about:blank";
   protected string _origin = "";
   protected string _compat_mode = "";
-  protected string _character_set = "";
-  protected string _content_type = "";
+  protected string _character_set = "utf-8";
+  protected string _content_type = "application/xml";
   public DomImplementation implementation { get { return (DomImplementation) _implementation; } }
   public string url { get { return _url; } }
-  public string document_uri { get { return _document_uri; } }
+  public string document_uri { get { return _url; } }
   public string origin { get { return _origin; } }
   public string compat_mode { get { return _compat_mode; } }
   public string character_set { get { return _character_set; } }
   public string content_type { get { return _content_type; } }
 
-  protected DomDocumentType _doctype = null;
-  public DomDocumentType? doctype { owned get { return _doctype; } }
+  public DomDocumentType? doctype {
+    owned get {
+      foreach (DomNode n in child_nodes) {
+        if (n is DomDocumentType) return (DomDocumentType) n;
+      }
+      return null;
+    }
+  }
   public DomElement? document_element { owned get { return (DomElement) root; } }
 
   public DomElement GXml.DomDocument.create_element (string local_name) throws GLib.Error {
@@ -227,13 +232,22 @@ public class GXml.GDocument : GXml.GNode,
   }
 
   public DomHTMLCollection get_elements_by_tag_name (string local_name) {
-      return get_elements_by_name (local_name);
+      var l = new GDomHTMLCollection ();
+      if (document_element == null) return l;
+      l.add_all (document_element.get_elements_by_tag_name (local_name));
+      return l;
   }
   public DomHTMLCollection get_elements_by_tag_name_ns (string? ns, string local_name) {
-      return get_elements_by_name_ns (local_name, ns);
+      var l = new GDomHTMLCollection ();
+      if (document_element == null) return l;
+      l.add_all (document_element.get_elements_by_tag_name_ns (ns, local_name));
+      return l;
   }
   public DomHTMLCollection get_elements_by_class_name(string class_names) {
-      return get_elements_by_property_value ("class", class_names);
+      var l = new GDomHTMLCollection ();
+      if (document_element == null) return l;
+      l.add_all (document_element.get_elements_by_class_name (class_names));
+      return l;
   }
 
   public DomDocumentFragment create_document_fragment() {
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index 956c2f8..18ad1bd 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -52,6 +52,18 @@ static const string HTMLDOC ="
 </html>
 ";
 
+static const string XMLDOC ="<?xml version=\"1.0\"?>
+<root>
+<project xmlns:gxml=\"http://live.gnome.org/GXml\";>
+<code class=\"parent\"/>
+<code class=\"node parent\"/>
+<page class=\"node\"/>
+<page class=\"node parent\"/>
+</project>
+<Author name=\"You\" />
+</root>
+";
+
        public static void add_tests () {
                Test.add_func ("/gxml/dom/document/children", () => {
                        GLib.message ("Doc: "+STRDOC);
@@ -339,5 +351,55 @@ static const string HTMLDOC ="
                                assert_not_reached ();
                        }
                });
+               Test.add_func ("/gxml/dom/document/api", () => {
+                       try {
+                               GLib.message ("Doc: "+XMLDOC);
+                               var doc = new GDocument.from_string (XMLDOC) as DomDocument;
+                               assert (doc.url == "about:blank");
+                               assert (doc.document_uri == "about:blank");
+                               assert (doc.origin == "");
+                               assert (doc.compat_mode == "");
+                               assert (doc.character_set == "utf-8");
+                               assert (doc.content_type == "application/xml");
+                               assert (doc.doctype == null);
+                               assert (doc.document_element != null);
+                               assert (doc.document_element is DomElement);
+                               assert (doc.document_element.node_name == "root");
+                               var le = doc.get_elements_by_tag_name ("code");
+                               assert (le.length == 2);
+                               assert (le.item (0) is DomElement);
+                               assert (le.item (0).node_name == "code");
+                               var n = doc.create_element_ns 
("http://git.gnome.org/browse/gxml","git:MyNode";);
+                               var n2 = doc.document_element.append_child (n) as DomElement;
+                               n2.set_attribute ("class","node");
+                               var lens = doc.get_elements_by_tag_name_ns 
("http://git.gnome.org/browse/gxml","MyNode";);
+                               assert (lens.length == 1);
+                               assert (lens.item (0) is DomElement);
+                               assert (lens.item (0).node_name == "MyNode");
+                               GLib.message ("DOC: "+(doc.document_element as GXml.Node).to_string ());
+                               var lec = doc.get_elements_by_class_name ("node");
+                               GLib.message ("Class node found"+lec.length.to_string ());
+                               assert (lec.length == 4);
+                               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);
+                               assert (lec2.item (0).node_name == "code");
+                               var lec3 = doc.get_elements_by_class_name ("parent code");
+                               assert (lec3.length == 4);
+                               assert (lec3.item (0) is DomElement);
+                               assert (lec3.item (0).node_name == "code");
+                               var lec4 = doc.get_elements_by_class_name ("code parent");
+                               assert (lec4.length == 4);
+                               assert (lec4.item (0) is DomElement);
+                               assert (lec4.item (0).node_name == "code");
+                       } 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]