]> source.dussan.org Git - gwtquery.git/commitdiff
fix issue 119 and fix nth-child selector + tests
authorJulien Dramaix <julien.dramaix@gmail.com>
Thu, 19 Jan 2012 23:37:07 +0000 (23:37 +0000)
committerJulien Dramaix <julien.dramaix@gmail.com>
Thu, 19 Jan 2012 23:37:07 +0000 (23:37 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineCssToXPath.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTestGwt.java

index beb89003a9c7b248e16ba2381b6055e3e6cd681c..0424378c331438b278923fdbffead9223472cd28 100644 (file)
@@ -76,22 +76,30 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
   \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
@@ -128,7 +136,7 @@ public class SelectorEngineCssToXPath extends SelectorEngineImpl {
     // :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
index ea54759a6dbc0e991a4169c308d8f6555fc03c11..9d32fece1036d79b31adb0613c7a0ae286925102 100644 (file)
@@ -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);
   }