[gxml] GDocument: implemented query_selectors_all()
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] GDocument: implemented query_selectors_all()
- Date: Wed, 6 Sep 2017 17:55:57 +0000 (UTC)
commit 878791d38b9ea81f577c5e4cf6e27868e099f0cc
Author: Daniel Espinosa <esodan gmail com>
Date: Wed Sep 6 10:53:07 2017 -0700
GDocument: implemented query_selectors_all()
gxml/GXmlDocument.vala | 12 +++++++-
gxml/GXmlElement.vala | 1 +
gxml/GomElement.vala | 5 +--
test/GDocumentTest.vala | 61 +++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 74 insertions(+), 5 deletions(-)
---
diff --git a/gxml/GXmlDocument.vala b/gxml/GXmlDocument.vala
index e97bb1c..4078878 100644
--- a/gxml/GXmlDocument.vala
+++ b/gxml/GXmlDocument.vala
@@ -347,8 +347,16 @@ public class GXml.GDocument : GXml.GNode,
public int child_element_count { get { return children_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 (GXml.Node e in children_nodes) {
+ if (!(e is DomElement)) continue;
+ if (cs.match (e as DomElement))
+ l.add (e as DomNode);
+ l.add_all ((e as DomElement).query_selector_all (selectors));
+ }
+ return l as DomNodeList;
}
// DomNonElementParentNode
public DomElement? get_element_by_id (string element_id) throws GLib.Error {
diff --git a/gxml/GXmlElement.vala b/gxml/GXmlElement.vala
index cc90781..b6ce1ab 100644
--- a/gxml/GXmlElement.vala
+++ b/gxml/GXmlElement.vala
@@ -321,6 +321,7 @@ public class GXml.GElement : GXml.GNonDocumentChildNode,
if (!(e is DomElement)) continue;
if (cs.match (e as DomElement))
l.add (e as DomNode);
+ l.add_all ((e as DomElement).query_selector_all (selectors));
}
return l as DomNodeList;
}
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index 8884fa4..f1c6675 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -270,10 +270,9 @@ 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));
- }
+ l.add_all ((e as DomElement).query_selector_all (selectors));
}
return l;
}
diff --git a/test/GDocumentTest.vala b/test/GDocumentTest.vala
index 2cc81f3..f49cc25 100644
--- a/test/GDocumentTest.vala
+++ b/test/GDocumentTest.vala
@@ -345,5 +345,66 @@ class GDocumentTest : GXmlTest {
var doc = new GDocument ();
assert (doc.parent == null);
});
+ Test.add_func ("/gxml/gdocument/css-selector", () => {
+ try {
+ var d = new GDocument () as DomDocument;
+ 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]