From 98fcea52140038aef85333060e10a5022d5c5f35 Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Mon, 28 Mar 2011 12:25:55 +0000 Subject: [PATCH] eq() and get() method must accept negative index (count from the end of the matched set) --- .../com/google/gwt/query/client/GQuery.java | 36 ++++++++++++++-- .../google/gwt/query/client/LazyGQuery.java | 30 +++++++++++-- .../gwt/query/client/GQueryCoreTest.java | 42 +++++++++++++++++++ 3 files changed, 102 insertions(+), 6 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index d6adeb09..bfa35e91 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -1086,7 +1086,20 @@ public class GQuery implements Lazy { } /** - * 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: + * + *
+   *  $("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
+   *  ...
+   * 
*/ public GQuery eq(int pos) { return $(get(pos)); @@ -1225,11 +1238,28 @@ public class GQuery implements Lazy { } /** - * 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: + *
+   *  $("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
+   *  ...
+   * 
*/ public Element get(int i) { int l = elements.getLength(); - return i >= 0 && i < l ? elements.getItem(i) : null; + if (i >= 0 && i < l) { + return elements.getItem(i); + } + if (i < 0 && l + i >= 0) { + return elements.getItem(l + i); + } + return null; } /** diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java index 4036ab44..98d3d035 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java @@ -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 extends LazyBase{ LazyGQuery 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: + * + *
+   *  $("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
+   *  ...
+   * 
*/ LazyGQuery eq(int pos); @@ -506,7 +519,18 @@ public interface LazyGQuery extends LazyBase{ NodeList 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: + *
+   *  $("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
+   *  ...
+   * 
*/ Element get(int i); diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java index 75addc88..21365a01 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java @@ -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 = "
blop1
blop2
blop3
blop4
"; + $(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(); -- 2.39.5