[gxml] GomElement: Added tests for query_selector()



commit a9a013161e232d55d5d1638d43761e05b65569c5
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Sep 6 09:58:27 2017 -0700

    GomElement: Added tests for query_selector()

 gxml/DomCollections.vala      |    4 ++-
 gxml/GXmlDocument.vala        |    3 --
 gxml/GXmlElement.vala         |    4 ---
 gxml/GomDocument.vala         |    3 --
 gxml/GomElement.vala          |   15 ++++++----
 gxml/css-selector-parser.vala |    9 +++---
 test/GomElementTest.vala      |   59 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 75 insertions(+), 22 deletions(-)
---
diff --git a/gxml/DomCollections.vala b/gxml/DomCollections.vala
index ab16d06..d6425e2 100644
--- a/gxml/DomCollections.vala
+++ b/gxml/DomCollections.vala
@@ -31,7 +31,9 @@ public interface GXml.DomParentNode : GLib.Object {
   public abstract DomElement? last_element_child { owned get; }
   public abstract int child_element_count { get; }
 
-  public abstract DomElement? query_selector (string selectors) throws GLib.Error;
+  public virtual DomElement? query_selector (string selectors) throws GLib.Error {
+    return query_selector_all (selectors).item (0) as DomElement;
+  }
   public abstract DomNodeList query_selector_all (string selectors) throws GLib.Error;
   /**
    * Search all child {@link GXml.Element} with a given property's name and with
diff --git a/gxml/GXmlDocument.vala b/gxml/GXmlDocument.vala
index 534e53c..e97bb1c 100644
--- a/gxml/GXmlDocument.vala
+++ b/gxml/GXmlDocument.vala
@@ -346,9 +346,6 @@ public class GXml.GDocument : GXml.GNode,
   }
   public int child_element_count { get { return children_nodes.size; } }
 
-  public DomElement? query_selector (string selectors) throws GLib.Error {
-    return null; // FIXME
-  }
   public DomNodeList query_selector_all (string selectors) throws GLib.Error  {
     DomNodeList nulllist = null;
     return nulllist; // FIXME
diff --git a/gxml/GXmlElement.vala b/gxml/GXmlElement.vala
index 0d1931b..3cf8fb1 100644
--- a/gxml/GXmlElement.vala
+++ b/gxml/GXmlElement.vala
@@ -313,10 +313,6 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
   public DomElement? last_element_child { owned get { return (DomElement) children.last (); } }
   public int child_element_count { get { return children.size; } }
 
-  public DomElement? query_selector (string selectors) throws GLib.Error {
-  // FIXME:
-    throw new DomError.SYNTAX_ERROR (_("DomElement query_selector is not implemented"));
-  }
   public DomNodeList query_selector_all (string selectors) throws GLib.Error {
   // FIXME:
     throw new DomError.SYNTAX_ERROR (_("DomElement query_selector_all is not implemented"));
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index 007d3d6..d148a10 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -344,9 +344,6 @@ public class GXml.GomDocument : GomNode,
   }
   public int child_element_count { get { return child_nodes.size; } }
 
-  public DomElement? query_selector (string selectors) throws GLib.Error {
-    return null; // FIXME
-  }
   public DomNodeList query_selector_all (string selectors) throws GLib.Error  {
     DomNodeList nulllist = null;
     return nulllist; // FIXME
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index ef2436d..e6a1a34 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -264,13 +264,16 @@ public class GXml.GomElement : GomNode,
   public DomElement? last_element_child { owned get { return (DomElement) children.last (); } }
   public int child_element_count { get { return children.size; } }
 
-  public DomElement? query_selector (string selectors) throws GLib.Error {
-  // FIXME:
-    throw new DomError.SYNTAX_ERROR (_("DomElement query_selector is not implemented"));
-  }
   public DomNodeList query_selector_all (string selectors) throws GLib.Error {
-  // FIXME:
-    throw new DomError.SYNTAX_ERROR (_("DomElement query_selector_all is not implemented"));
+    var cs = new CssSelectorParser ();
+    cs.parse (selectors);
+    var l = new GomNodeList ();
+    foreach (DomNode e in child_nodes) {
+      if (!(e is DomElement)) continue;
+      if (cs.match (e as DomElement))
+        l.add (e);
+    }
+    return l;
   }
   // GXml.DomElement
   /**
diff --git a/gxml/css-selector-parser.vala b/gxml/css-selector-parser.vala
index 7445a9e..facefd9 100644
--- a/gxml/css-selector-parser.vala
+++ b/gxml/css-selector-parser.vala
@@ -339,12 +339,11 @@ public class GXml.CssSelectorParser : GLib.Object {
                                message (p+": "+s.data);
                                var lc = element.class_list;
                                message (lc.length.to_string ());
-                               for (int k = 0; k < lc.length; k++) {
-                                       var cl = lc.item (k);
-                                       if (cl == null) continue;
-                                       if (cl.down () == s.data.down ()) return true;
+                               try {
+                                       if (lc.contains (s.data)) return true;
+                               } catch (GLib.Error e) {
+                                       warning ("Error: "+e.message);
                                }
-                               if (lc.contains (s.data)) return true;
                                if (p=="warning") warning ("Not found");
                        }
                }
diff --git a/test/GomElementTest.vala b/test/GomElementTest.vala
index 76dbc30..cf2280f 100644
--- a/test/GomElementTest.vala
+++ b/test/GomElementTest.vala
@@ -317,5 +317,64 @@ class GomElementTest : GXmlTest  {
                                        assert_not_reached ();
                                }
                });
+               Test.add_func ("/gxml/gom-element/css-selector", () => {
+                       try {
+                               var d = new GomDocument ();
+                               var r = d.create_element ("root");
+                               d.append_child (r);
+                               var c1 = d.create_element ("child");
+                               c1.set_attribute ("class", "error");
+                               r.append_child (c1);
+                               var c2 = d.create_element ("child");
+                               c2.set_attribute ("class", "warning");
+                               r.append_child (c2);
+                               var c3 = d.create_element ("child");
+                               c3.set_attribute ("class", "error warning");
+                               r.append_child (c3);
+                               var c4 = d.create_element ("child");
+                               c4.set_attribute ("class", "error calc");
+                               r.append_child (c4);
+                               var c5 = d.create_element ("child");
+                               r.append_child (c5);
+                               var n1 = r.query_selector ("child");
+                               assert (n1 != null);
+                               assert (n1.get_attribute ("class") == "error");
+                               var n2 = r.query_selector ("child.warning");
+                               assert (n2 != null);
+                               assert (n2.get_attribute ("class") == "warning");
+                               var n3 = r.query_selector ("child[class]");
+                               assert (n3 != null);
+                               assert (n3.get_attribute ("class") == "error");
+                               var n4 = r.query_selector ("child[class=\"error calc\"]");
+                               assert (n4 != null);
+                               assert (n4.get_attribute ("class") == "error calc");
+                               var l1 = r.query_selector_all ("child");
+                               assert (l1 != null);
+                               message (l1.length.to_string ());
+                               assert (l1.length == 5);
+                               assert (l1.item (4).node_name == "child");
+                               var l2 = r.query_selector_all ("child[class]");
+                               assert (l2 != null);
+                               assert (l2.length == 4);
+                               assert (l2.item (3).node_name == "child");
+                               var l3 = r.query_selector_all ("child[class=\"error\"]");
+                               assert (l3 != null);
+                               assert (l3.length == 1);
+                               assert (l3.item (0).node_name == "child");
+                               var c6 = d.create_element ("child");
+                               c6.set_attribute ("prop", "val1");
+                               r.append_child (c6);
+                               var c7 = d.create_element ("child");
+                               c7.set_attribute ("prop", "val1");
+                               r.append_child (c7);
+                               var l4 = r.query_selector_all ("child[prop=\"val1\"]");
+                               assert (l4 != null);
+                               assert (l4.length == 2);
+                               assert (l4.item (0).node_name == "child");
+                       } 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]