\r
private static ReplaceCallback rc_nth_child = new ReplaceCallback() {\r
public String foundMatch(ArrayList<String> s) {\r
- if (s.get(1) == null || s.get(1).length() == 0) {\r
- s.set(1, "0");\r
+ String s1 = s.get(1);\r
+ String s2 = s.get(2);\r
+ \r
+ boolean noPrefix = s1 == null || s1.length() == 0;\r
+\r
+ if ("n".equals(s2)) {\r
+ return s1;\r
}\r
- if ("n".equals(s.get(2))) {\r
- return s.get(1);\r
+ if ("even".equals(s2)) {\r
+ return "*[position() mod 2=0 and position()>=0]" + (noPrefix ? "" : "/self::" + s1);\r
}\r
- if ("even".equals(s.get(2))) {\r
- return "*[position() mod 2=0 and position()>=0]/self::" + s.get(1);\r
+ if ("odd".equals(s2)) {\r
+ String prefix = noPrefix ? "" : s1;\r
+ return prefix + "[(count(preceding-sibling::*) + 1) mod 2=1]";\r
}\r
- if ("odd".equals(s.get(2))) {\r
- return s.get(1) + "[(count(preceding-sibling::*) + 1) mod 2=1]";\r
+ \r
+ if (!s2.contains("n")){\r
+ return "*[position() = "+s2+"]" + (noPrefix ? "" : "/self::" + s1);\r
}\r
- String[] t = s.get(2).replaceAll("^([0-9]*)n.*?([0-9]*)?$", "$1+$2").split("\\+");\r
+ \r
+ String[] t = s2.replaceAll("^([0-9]*)n.*?([0-9]*)?$", "$1+$2").split("\\+");\r
String t0 = t[0];\r
String t1 = t.length > 1 ? t[1] : "0";\r
- return "*[(position()-" + t1 + ") mod " + t0 + "=0 and position()>=" + t1 + "]/self::" + s.get(1);\r
+ return "*[(position()-" + t1 + ") mod " + t0 + "=0 and position()>=" + t1 + "]" + (noPrefix ? "" : "/self::" + s1);\r
}\r
};\r
\r
// :not\r
"(.+):not\\(([^\\)]*)\\)", rc_Not,\r
// :nth-child\r
- "([a-zA-Z0-9\\_\\-\\*]+):nth-child\\(([^\\)]*)\\)", rc_nth_child,\r
+ "([a-zA-Z0-9\\_\\-\\*]*):nth-child\\(([^\\)]*)\\)", rc_nth_child,\r
// :contains(selectors)\r
":contains\\(([^\\)]*)\\)", "[contains(string(.),'$1')]",\r
// |= attrib\r
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);
}