[gxml] GomDocument: Fixed get_element_by_id()



commit 662ae595f095755b384391744e02a02f9976d36f
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Oct 26 15:22:55 2017 -0500

    GomDocument: Fixed get_element_by_id()

 gxml/DomCollections.vala  |   19 ++++++++-----------
 gxml/GomDocument.vala     |   22 +++++++++++++++-------
 gxml/GomElement.vala      |    4 ++--
 test/GomDocumentTest.vala |   11 +++++++++++
 4 files changed, 36 insertions(+), 20 deletions(-)
---
diff --git a/gxml/DomCollections.vala b/gxml/DomCollections.vala
index c103d99..3d35f63 100644
--- a/gxml/DomCollections.vala
+++ b/gxml/DomCollections.vala
@@ -46,17 +46,14 @@ public interface GXml.DomParentNode : GLib.Object {
    get_elements_by_property_value (string property, string value)
   {
     var list = new GXml.DomElementList ();
-    foreach (var child in children) {
-      if (child is GXml.Element) {
-        list.add_all (child.get_elements_by_property_value (property, value));
-        if (child.attributes == null) continue;
-        var cls = child.attributes.get (property);
-        if (cls == null) {
-          continue;
-        }
-        if (value in cls.node_value)
-            list.add ((GXml.DomElement) child);
-      }
+    foreach (DomElement child in children) {
+      if (child == null) continue;
+      list.add_all (child.get_elements_by_property_value (property, value));
+      if (child.attributes == null) continue;
+      var cls = child.get_attribute (property);
+      if (cls == null) continue;
+      if (value == cls)
+          list.add ((GXml.DomElement) child);
     }
     return list;
   }
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index f2bfc34..bcee1af 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -335,7 +335,15 @@ public class GXml.GomDocument : GomNode,
       return new GDomTreeWalker (root, what_to_show, filter);
   }
   // DomParentNode
-  public DomHTMLCollection children { owned get { return (DomHTMLCollection) child_nodes; } }
+  public DomHTMLCollection children {
+    owned get {
+      var l = new GDomHTMLCollection ();
+      foreach (GXml.DomNode n in child_nodes) {
+        if (n is DomElement) l.add ((DomElement) n);
+      }
+      return l;
+    }
+  }
   public DomElement? first_element_child {
     owned get { return (DomElement) child_nodes.first (); }
   }
@@ -358,9 +366,9 @@ public class GXml.GomDocument : GomNode,
   }
   // DomNonElementParentNode
   public DomElement? get_element_by_id (string element_id) throws GLib.Error {
-    var l = this.get_elements_by_property_value ("id", element_id);
-    if (l.size > 0) return (DomElement) l[0];
-    return null;
+    var l = get_elements_by_property_value ("id", element_id);
+    if (l.size == 0) return null;
+    return l.get_element (0) as DomElement;
   }
 }
 
@@ -449,7 +457,7 @@ public class GXml.GomDocumentFragment : GXml.GomNode,
   // DomParentNode
   public new DomHTMLCollection children {
     owned get {
-      var l = new DomElementList ();
+      var l = new GDomHTMLCollection ();
       foreach (GXml.DomNode n in child_nodes) {
         if (n is DomElement) l.add ((DomElement) n);
       }
@@ -472,8 +480,8 @@ public class GXml.GomDocumentFragment : GXml.GomNode,
   // DomNonElementParentNode
   public DomElement? get_element_by_id (string element_id) throws GLib.Error {
     var l = get_elements_by_property_value ("id", element_id);
-    if (l.size > 0) return (DomElement) l[0];
-    return null;
+    if (l.size == 0) return null;
+    return (DomElement) l.get_element (0) as DomElement;
   }
 }
 
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index e2bc8d6..f80dced 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -245,9 +245,9 @@ public class GXml.GomElement : GomNode,
   // DomParentNode
   public new DomHTMLCollection children {
     owned get {
-      var l = new DomElementList ();
+      var l = new GDomHTMLCollection ();
       foreach (GXml.DomNode n in child_nodes) {
-        if (n is DomElement) l.add ((DomElement) n);
+        if (n is DomElement) l.add (n as DomElement);
       }
       return l;
     }
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index 4048177..ddf6372 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -711,5 +711,16 @@ class GomDocumentTest : GXmlTest {
                    assert_not_reached ();
                  }
                });
+               Test.add_func ("/gxml/gom-document/element-id", () => {
+                       try {
+                               var d = new GomDocument.from_string ("""<root><child id="id1"/><child 
id="id2"/></root>""") as DomDocument;
+                               message ((d as GomDocument).write_string ());
+                               var e = d.get_element_by_id ("id1");
+                               assert (e != null);
+                       } catch (GLib.Error e) {
+                   GLib.message ("Error: "+e.message);
+                   assert_not_reached ();
+                 } //<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd";>
+               });
        }
 }


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