]> source.dussan.org Git - gwtquery.git/commitdiff
eq() and get() method must accept negative index (count from the end of the matched...
authorJulien Dramaix <julien.dramaix@gmail.com>
Mon, 28 Mar 2011 12:25:55 +0000 (12:25 +0000)
committerJulien Dramaix <julien.dramaix@gmail.com>
Mon, 28 Mar 2011 12:25:55 +0000 (12:25 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java

index d6adeb09ae4154a030cff1fb7531cee884e68108..bfa35e91b503d0c6792d8fa366e3c868931623ef 100644 (file)
@@ -1086,7 +1086,20 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Reduce GQuery to element in the specified position.\r
+   * Reduce GQuery to element in the specified position. This method accept\r
+   * negative index. A negative index is counted from the end of the matched\r
+   * set:\r
+   * \r
+   * Example:\r
+   * \r
+   * <pre>\r
+   *  $("div").eq(0) will reduce the matched set to the first matched div\r
+   *  $("div").eq(1) will reduce the matched set to the second matched div\r
+   *  \r
+   *  $("div").eq(-1) will reduce the matched set to the last matched div\r
+   *  $("div").eq(-2) will reduce the matched set to the second-to-last matched div\r
+   *  ...\r
+   * </pre>\r
    */\r
   public GQuery eq(int pos) {\r
     return $(get(pos));\r
@@ -1225,11 +1238,28 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Return the ith element matched.\r
+   * Return the ith element matched. This method accept negative index. A\r
+   * negative index is counted from the end of the matched set.\r
+   * \r
+   * Example:\r
+   * <pre>\r
+   *  $("div").get(0) will return the first matched div\r
+   *  $("div").get(1) will return the second matched div\r
+   *  \r
+   *  $("div").get(-1) will return the last matched div\r
+   *  $("div").get(-2) will return the secont-to-last matched div\r
+   *  ...\r
+   * </pre>\r
    */\r
   public Element get(int i) {\r
     int l = elements.getLength();\r
-    return i >= 0 && i < l ? elements.getItem(i) : null;\r
+    if (i >= 0 && i < l) {\r
+      return elements.getItem(i);\r
+    }\r
+    if (i < 0 && l + i >= 0) {\r
+      return elements.getItem(l + i);\r
+    }\r
+    return null;\r
   }\r
 \r
   /**\r
index 4036ab449d159d48eba85710ab32f589cdeef062..98d3d0358e5ec5a44af3d58ab059fa45730703c4 100644 (file)
@@ -24,7 +24,6 @@ import com.google.gwt.dom.client.BodyElement;
 import com.google.gwt.dom.client.ButtonElement;
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
-import com.google.gwt.dom.client.IFrameElement;
 import com.google.gwt.dom.client.InputElement;
 import com.google.gwt.dom.client.Node;
 import com.google.gwt.dom.client.NodeList;
@@ -49,6 +48,7 @@ import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.EventListener;
 import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.GqUi;
 import com.google.gwt.user.client.ui.Widget;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -430,7 +430,20 @@ public interface LazyGQuery<T> extends LazyBase<T>{
   LazyGQuery<T> end();
 
   /**
-   * Reduce GQuery to element in the specified position.
+   * Reduce GQuery to element in the specified position. This method accept
+   * negative index. A negative index is counted from the end of the matched
+   * set:
+   * 
+   * Example:
+   * 
+   * <pre>
+   *  $("div").eq(0) will reduce the matched set to the first matched div
+   *  $("div").eq(1) will reduce the matched set to the second matched div
+   *  
+   *  $("div").eq(-1) will reduce the matched set to the last matched div
+   *  $("div").eq(-2) will reduce the matched set to the second-to-last matched div
+   *  ...
+   * </pre>
    */
   LazyGQuery<T> eq(int pos);
 
@@ -506,7 +519,18 @@ public interface LazyGQuery<T> extends LazyBase<T>{
   NodeList<Element> get();
 
   /**
-   * Return the ith element matched.
+   * Return the ith element matched. This method accept negative index. A
+   * negative index is counted from the end of the matched set.
+   * 
+   * Example:
+   * <pre>
+   *  $("div").get(0) will return the first matched div
+   *  $("div").get(1) will return the second matched div
+   *  
+   *  $("div").get(-1) will return the last matched div
+   *  $("div").get(-2) will return the secont-to-last matched div
+   *  ...
+   * </pre>
    */
   Element get(int i);
 
index 75addc888e236c20355b1b1a9e9dd468598cd63a..21365a0172d09e7c5855702991475e6869ca6e07 100644 (file)
@@ -717,6 +717,48 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals(2, $("p", e).slice(0, -1).size());
     assertEquals(0, $("p", e).slice(3, 2).size());
   }
+  
+  public void testGetEqLastFirstMethods(){
+    String content = "<div id='1'>blop1</div><div id='2'>blop2</div><div id='3'>blop3</div><div id='4'>blop4</div>";
+    $(e).html(content);
+    
+    GQuery divs =$("div",e);
+    assertEquals(4, divs.size());
+    assertEquals("1", divs.get(0).getId());
+    assertEquals("2", divs.get(1).getId());
+    assertEquals("3", divs.get(2).getId());
+    assertEquals("4", divs.get(3).getId());
+    assertEquals("1", divs.get(-4).getId());
+    assertEquals("2", divs.get(-3).getId());
+    assertEquals("3", divs.get(-2).getId());
+    assertEquals("4", divs.get(-1).getId());
+    
+    assertEquals(1, divs.first().size());
+    assertEquals("1", divs.first().get(0).getId());
+    
+    assertEquals(1, divs.last().size());
+    assertEquals("4", divs.last().get(0).getId());
+    
+    assertEquals(1, divs.eq(0).size());
+    assertEquals("1", divs.eq(0).get(0).getId());
+    assertEquals(1, divs.eq(1).size());
+    assertEquals("2", divs.eq(1).get(0).getId());
+    assertEquals(1, divs.eq(2).size());
+    assertEquals("3", divs.eq(2).get(0).getId());
+    assertEquals(1, divs.eq(3).size());
+    assertEquals("4", divs.eq(3).get(0).getId());
+    
+    assertEquals(1, divs.eq(-4).size());
+    assertEquals("1", divs.eq(-4).get(0).getId());
+    assertEquals(1, divs.eq(-3).size());
+    assertEquals("2", divs.eq(-3).get(0).getId());
+    assertEquals(1, divs.eq(-2).size());
+    assertEquals("3", divs.eq(-2).get(0).getId());
+    assertEquals(1, divs.eq(-1).size());
+    assertEquals("4", divs.eq(-1).get(0).getId());
+    
+    
+  }
 
   public void testUnique() {
     SelectorEngineImpl selSizz = new SelectorEngineSizzle();