}\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
}\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
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;
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;
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);
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);
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();