aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java30
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java47
2 files changed, 66 insertions, 11 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
index beb89003..0424378c 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
@@ -76,22 +76,30 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
private static ReplaceCallback rc_nth_child = new ReplaceCallback() {
public String foundMatch(ArrayList<String> s) {
- if (s.get(1) == null || s.get(1).length() == 0) {
- s.set(1, "0");
+ String s1 = s.get(1);
+ String s2 = s.get(2);
+
+ boolean noPrefix = s1 == null || s1.length() == 0;
+
+ if ("n".equals(s2)) {
+ return s1;
}
- if ("n".equals(s.get(2))) {
- return s.get(1);
+ if ("even".equals(s2)) {
+ return "*[position() mod 2=0 and position()>=0]" + (noPrefix ? "" : "/self::" + s1);
}
- if ("even".equals(s.get(2))) {
- return "*[position() mod 2=0 and position()>=0]/self::" + s.get(1);
+ if ("odd".equals(s2)) {
+ String prefix = noPrefix ? "" : s1;
+ return prefix + "[(count(preceding-sibling::*) + 1) mod 2=1]";
}
- if ("odd".equals(s.get(2))) {
- return s.get(1) + "[(count(preceding-sibling::*) + 1) mod 2=1]";
+
+ if (!s2.contains("n")){
+ return "*[position() = "+s2+"]" + (noPrefix ? "" : "/self::" + s1);
}
- String[] t = s.get(2).replaceAll("^([0-9]*)n.*?([0-9]*)?$", "$1+$2").split("\\+");
+
+ String[] t = s2.replaceAll("^([0-9]*)n.*?([0-9]*)?$", "$1+$2").split("\\+");
String t0 = t[0];
String t1 = t.length > 1 ? t[1] : "0";
- return "*[(position()-" + t1 + ") mod " + t0 + "=0 and position()>=" + t1 + "]/self::" + s.get(1);
+ return "*[(position()-" + t1 + ") mod " + t0 + "=0 and position()>=" + t1 + "]" + (noPrefix ? "" : "/self::" + s1);
}
};
@@ -128,7 +136,7 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
// :not
"(.+):not\\(([^\\)]*)\\)", rc_Not,
// :nth-child
- "([a-zA-Z0-9\\_\\-\\*]+):nth-child\\(([^\\)]*)\\)", rc_nth_child,
+ "([a-zA-Z0-9\\_\\-\\*]*):nth-child\\(([^\\)]*)\\)", rc_nth_child,
// :contains(selectors)
":contains\\(([^\\)]*)\\)", "[contains(string(.),'$1')]",
// |= attrib
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
index ea54759a..9d32fece 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java
@@ -376,6 +376,53 @@ public class GQuerySelectorsTestGwt extends GWTTestCase {
assertEquals(n, a.length());
}
+ public void testOddEvenNthChild(){
+
+ $(e).append(
+ "<div id='parent'><div id='first' class='evenClass'>branchA target</div><div id='second' class='oddClass'>branchA target</div><div id='third' class='evenClass'>branchA target</div><div id='fourth' class='oddClass'>branchA target</div></div>");
+
+ GQuery odd = $("#parent > div:odd",e);
+ assertEquals(2, odd.size());
+ for (Element el : odd.elements()){
+ assertEquals("oddClass", el.getClassName());
+ }
+
+ GQuery even = $("#parent > div:even", e);
+ assertEquals(2, even.size());
+ for (Element el : even.elements()){
+ assertEquals("evenClass", el.getClassName());
+ }
+
+ //test filter
+ odd = $("#parent > div",e).filter(":odd");
+ assertEquals(2, odd.size());
+ for (Element el : odd.elements()){
+ assertEquals("oddClass", el.getClassName());
+ }
+
+ even = $("#parent > div", e).filter(":even");
+ assertEquals(2, even.size());
+ for (Element el : even.elements()){
+ assertEquals("evenClass", el.getClassName());
+ }
+
+ //test nth-child
+ GQuery second = $("#parent > div",e).filter(":nth-child(2)");
+ assertEquals(1, second.size());
+ assertEquals("second", second.attr("id"));
+
+ GQuery third =$("#parent > div:nth-child(3)", e);
+ assertEquals(1, third.size());
+ assertEquals("third", third.attr("id"));
+
+ //test nth-child with function
+ GQuery secondAndFourth = $("#parent > div",e).filter(":nth-child(2n)");
+ assertEquals(2, secondAndFourth.size());
+ assertEquals("second", secondAndFourth.eq(0).attr("id"));
+ assertEquals("fourth", secondAndFourth.eq(1).attr("id"));
+
+ }
+
private void assertArrayContains(Object result, Object... array) {
assertArrayContains("", result, array);
}