aboutsummaryrefslogtreecommitdiffstats
path: root/gwtquery-core
diff options
context:
space:
mode:
authorManolo Carrasco <manolo@apache.org>2011-02-24 11:05:45 +0000
committerManolo Carrasco <manolo@apache.org>2011-02-24 11:05:45 +0000
commitfafbe1f7e71a7861718ccfcf3ca994379f98947e (patch)
tree62e64f8d450c3a789296720ade57d5b1cfde0e80 /gwtquery-core
parent742e3c11e31bf4db429a13810a390a3c6e2c99b4 (diff)
downloadgwtquery-fafbe1f7e71a7861718ccfcf3ca994379f98947e.tar.gz
gwtquery-fafbe1f7e71a7861718ccfcf3ca994379f98947e.zip
$() must handle nulls
Diffstat (limited to 'gwtquery-core')
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java42
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java12
2 files changed, 37 insertions, 17 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 dc8fd780..5db86e3a 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
@@ -214,21 +214,21 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* Wrap a GQuery around an existing element.
*/
public static GQuery $(Element element) {
- return new GQuery(JSArray.create(element));
+ return new GQuery(element);
}
/**
* Wrap a GQuery around an event's target element.
*/
public static GQuery $(Event event) {
- return $((Element) event.getCurrentEventTarget().cast());
+ return event == null ? $() : $((Element) event.getCurrentEventTarget().cast());
}
/**
* Wrap a GQuery around an existing node.
*/
public static GQuery $(Node n) {
- return new GQuery(JSArray.create(n));
+ return n == null ? $() : new GQuery(JSArray.create(n));
}
/**
@@ -244,11 +244,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
*/
public static GQuery $(@SuppressWarnings("rawtypes") ArrayList a) {
JSArray elements = JSArray.create();
- for (Object o : a ) {
- if (o instanceof Node) {
- elements.addNode((Node)o);
- } else if (o instanceof Widget) {
- elements.addNode(((Widget)o).getElement());
+ if (a != null) {
+ for (Object o : a ) {
+ if (o instanceof Node) {
+ elements.addNode((Node)o);
+ } else if (o instanceof Widget) {
+ elements.addNode(((Widget)o).getElement());
+ }
}
}
return new GQuery(elements);
@@ -347,7 +349,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* Wrap a GQuery around an existing widget.
*/
public static GQuery $(Widget w){
- return $(w.getElement());
+ return w == null ? $() : $(w.getElement());
}
/**
@@ -544,30 +546,35 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
return $wnd;
}-*/;
- protected NodeList<Element> elements = null;
+ protected NodeList<Element> elements = JavaScriptObject.createArray().cast();
private String currentSelector;
private GQuery previousObject;
public GQuery() {
- elements = JavaScriptObject.createArray().cast();
}
public GQuery(Element element) {
- elements = JSArray.create(element);
+ if (element != null) {
+ elements = JSArray.create(element);
+ }
}
public GQuery(GQuery gq) {
- this(gq.get());
+ this(gq == null ? null : gq.get());
}
public GQuery(JSArray elements) {
- this.elements = elements;
+ if (elements != null) {
+ this.elements = elements;
+ }
}
public GQuery(NodeList<Element> list) {
- elements = list;
+ if (list != null) {
+ elements = list;
+ }
}
/**
@@ -1190,7 +1197,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* Reduce GQuery to element in the specified position.
*/
public GQuery eq(int pos) {
- return $(elements.getItem(pos));
+ return $(get(pos));
}
/**
@@ -1316,7 +1323,8 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* Return the ith element matched.
*/
public Element get(int i) {
- return elements.getItem(i);
+ int l = elements.getLength();
+ return i >= 0 && i < l? elements.getItem(i) : null;
}
/**
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 9611b2a3..9d0d74aa 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
@@ -21,6 +21,8 @@ import static com.google.gwt.query.client.GQuery.document;
import java.util.ArrayList;
+import junit.framework.Assert;
+
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
@@ -32,6 +34,7 @@ import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.dom.client.Node;
/**
* Test class for testing gwtquery-core api.
@@ -833,5 +836,14 @@ public class GQueryCoreTest extends GWTTestCase {
assertEquals(3, $(a).size());
}
+
+ public void testNulls() {
+ Assert.assertEquals(0, $((Node)null).size());
+ Assert.assertEquals(0, $((Element)null).size());
+ Assert.assertEquals(0, $((String)null).size());
+ Assert.assertNull($((String)null).get(0));
+ Assert.assertNull($((String)null).get(-1));
+ Assert.assertEquals(0, $((String)null).eq(0).size());
+ }
}