[gxml] GomDocument: implemented query_selectors_all()



commit 5785386cfbb78be0840ff16291fd8f9639c5426a
Author: Daniel Espinosa <esodan gmail com>
Date:   Wed Sep 6 10:46:26 2017 -0700

    GomDocument: implemented query_selectors_all()

 gxml/GomDocument.vala         |   12 +++++++-
 gxml/GomElement.vala          |    4 ++-
 gxml/css-selector-parser.vala |    3 --
 test/GomDocumentTest.vala     |   61 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 6 deletions(-)
---
diff --git a/gxml/GomDocument.vala b/gxml/GomDocument.vala
index d148a10..38c4b47 100644
--- a/gxml/GomDocument.vala
+++ b/gxml/GomDocument.vala
@@ -345,8 +345,16 @@ public class GXml.GomDocument : GomNode,
   public int child_element_count { get { return child_nodes.size; } }
 
   public DomNodeList query_selector_all (string selectors) throws GLib.Error  {
-    DomNodeList nulllist = null;
-    return nulllist; // FIXME
+    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);
+      l.add_all ((e as DomElement).query_selector_all (selectors));
+    }
+    return l;
   }
   // DomNonElementParentNode
   public DomElement? get_element_by_id (string element_id) throws GLib.Error {
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index e6a1a34..8884fa4 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -270,8 +270,10 @@ public class GXml.GomElement : GomNode,
     var l = new GomNodeList ();
     foreach (DomNode e in child_nodes) {
       if (!(e is DomElement)) continue;
-      if (cs.match (e as DomElement))
+      if (cs.match (e as DomElement)) {
         l.add (e);
+        l.add_all ((e as DomElement).query_selector_all (selectors));
+      }
     }
     return l;
   }
diff --git a/gxml/css-selector-parser.vala b/gxml/css-selector-parser.vala
index facefd9..b91831c 100644
--- a/gxml/css-selector-parser.vala
+++ b/gxml/css-selector-parser.vala
@@ -239,7 +239,6 @@ public class GXml.CssSelectorParser : GLib.Object {
                int position = 0;
                unichar u = 0;
                while (position < css.length) {
-                       message ("position : %d (%s)\n", position, css.substring (position, 1));
                        u = css.get_char (position);
                        if (u.isspace()) {
                                css.get_next_char (ref position, out u);
@@ -336,9 +335,7 @@ public class GXml.CssSelectorParser : GLib.Object {
                        if (s.selector_type == CssSelectorType.CLASS) {
                                var p = element.get_attribute ("class");
                                if (p == null) return false;
-                               message (p+": "+s.data);
                                var lc = element.class_list;
-                               message (lc.length.to_string ());
                                try {
                                        if (lc.contains (s.data)) return true;
                                } catch (GLib.Error e) {
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index 22ad703..ec5d7c0 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -543,5 +543,66 @@ class GomDocumentTest : GXmlTest {
                    assert_not_reached ();
                  }
                });
+               Test.add_func ("/gxml/gom-document/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 c51 = d.create_element ("child");
+                               c5.append_child (c51);
+                               var n1 = d.query_selector ("child");
+                               assert (n1 != null);
+                               assert (n1.get_attribute ("class") == "error");
+                               var n2 = d.query_selector ("child.warning");
+                               assert (n2 != null);
+                               assert (n2.get_attribute ("class") == "warning");
+                               var n3 = d.query_selector ("child[class]");
+                               assert (n3 != null);
+                               assert (n3.get_attribute ("class") == "error");
+                               var n4 = d.query_selector ("child[class=\"error calc\"]");
+                               assert (n4 != null);
+                               assert (n4.get_attribute ("class") == "error calc");
+                               var l1 = d.query_selector_all ("child");
+                               assert (l1 != null);
+                               message (l1.length.to_string ());
+                               assert (l1.length == 6);
+                               assert (l1.item (4).node_name == "child");
+                               var l2 = d.query_selector_all ("child[class]");
+                               assert (l2 != null);
+                               assert (l2.length == 4);
+                               assert (l2.item (3).node_name == "child");
+                               var l3 = d.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 = d.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]