[gxml] Implemented CssSelectorParser E:first-child pseudo selector
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] Implemented CssSelectorParser E:first-child pseudo selector
- Date: Fri, 8 Sep 2017 17:04:25 +0000 (UTC)
commit bdb294a3e956331ba4866e1cd36a5362a31e37e4
Author: Daniel Espinosa <esodan gmail com>
Date: Fri Sep 8 09:20:16 2017 -0700
Implemented CssSelectorParser E:first-child pseudo selector
gxml/CssSelectorParser.vala | 9 +++++
test/CssSelectorTest.vala | 69 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 78 insertions(+), 0 deletions(-)
---
diff --git a/gxml/CssSelectorParser.vala b/gxml/CssSelectorParser.vala
index fbcffea..7610d76 100644
--- a/gxml/CssSelectorParser.vala
+++ b/gxml/CssSelectorParser.vala
@@ -381,6 +381,15 @@ public class GXml.CssSelectorParser : GLib.Object {
if (s.data.down () == "empty") {
if (!element.has_child_nodes ()) return true;
}
+ if (s.data.down () == "fist-child") {
+ if (element.parent_node == null) return false;
+ if (!(element.parent_node is DomElement)) return false;
+ if (element is GomElement)
+ if (element == (element.parent_node as
DomParentNode).first_element_child) return true;
+ if (element is GElement)
+ if ((element as GNode).get_internal_node ()
+ == ((element.parent_node as
DomParentNode).first_element_child as GNode).get_internal_node ()) return true;
+ }
}
if (s.selector_type == CssSelectorType.CLASS) {
var p = element.get_attribute ("class");
diff --git a/test/CssSelectorTest.vala b/test/CssSelectorTest.vala
index 0303331..0ac6159 100644
--- a/test/CssSelectorTest.vala
+++ b/test/CssSelectorTest.vala
@@ -527,5 +527,74 @@ class CssSelectorTest : GXmlTest {
warning ("ERROR: "+e.message);
}
});
+ Test.add_func ("/gxml/css-selector/pseudo/first-child", () => {
+ try {
+ var cp = new CssSelectorParser ();
+ cp.parse ("second:first-child");
+ foreach (CssSelectorData sel in cp.selectors) {
+ message ("Type: "+sel.selector_type.to_string ()+" : "+sel.data+" :
"+sel.value);
+ }
+ assert (cp.selectors.size == 3);
+ var s = cp.selectors[0];
+ assert (s != null);
+ assert (s.selector_type == CssSelectorType.ELEMENT);
+ var si = cp.selectors[1];
+ assert (si != null);
+ assert (si.selector_type == CssSelectorType.INSIDE);
+ var sa = cp.selectors[2];
+ assert (sa != null);
+ assert (sa.selector_type == CssSelectorType.PSEUDO);
+ var d = new GomDocument ();
+ var r = d.create_element ("toplevel");
+ d.append_child (r);
+ var c1 = d.create_element ("child");
+ r.append_child (c1);
+ var c2 = d.create_element ("child");
+ c2.set_attribute ("prop", "subval");
+ r.append_child (c2);
+ var c3 = d.create_element ("child");
+ c3.set_attribute ("prop", "techval");
+ r.append_child (c3);
+ var c4 = d.create_element ("child");
+ c4.set_attribute ("prop", "secondaryvalue");
+ r.append_child (c4);
+ var c5 = d.create_element ("second");
+ c3.append_child (c5);
+ var c6 = d.create_element ("second");
+ c4.append_child (c6);
+ assert (!cp.match (r));
+ assert (!cp.match (c1));
+ assert (!cp.match (c2));
+ assert (!cp.match (c3));
+ assert (!cp.match (c4));
+ assert (cp.match (c5));
+ assert (cp.match (c6));
+ var d2 = new GDocument () as DomDocument;
+ var r2 = d2.create_element ("toplevel");
+ d2.append_child (r2);
+ var c1g = d2.create_element ("child");
+ r2.append_child (c1g);
+ var c2g = d2.create_element ("child");
+ c2g.set_attribute ("prop", "subval");
+ r2.append_child (c2g);
+ var c3g = d2.create_element ("child");
+ c3g.set_attribute ("prop", "techval");
+ r2.append_child (c3g);
+ var c4g = d2.create_element ("child");
+ c4g.set_attribute ("prop", "secondaryvalue");
+ r2.append_child (c4g);
+ var c5g = d.create_element ("second");
+ c3g.append_child (c5g);
+ var c6g = d.create_element ("second");
+ c4g.append_child (c6g);
+ assert (cp.match (r));
+ assert (!cp.match (c1));
+ assert (!cp.match (c2));
+ assert (!cp.match (c3));
+ assert (!cp.match (c4));
+ } catch (GLib.Error e){
+ warning ("ERROR: "+e.message);
+ }
+ });
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]