aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco <manolo@apache.org>2013-03-15 08:24:31 -0700
committerManuel Carrasco <manolo@apache.org>2013-03-15 08:24:31 -0700
commit7b9441681c9fb74aa88c04b2642dabc2d2b7e06d (patch)
tree52e44935062e809b6c1636cf313215f8f73b90d0
parentd8103d128beae7c038fd94631f286c15ad504241 (diff)
parent7915a9a2d25a38b9ec45529d4bf82bd95bb2658a (diff)
downloadgwtquery-7b9441681c9fb74aa88c04b2642dabc2d2b7e06d.tar.gz
gwtquery-7b9441681c9fb74aa88c04b2642dabc2d2b7e06d.zip
Merge pull request #19 from manolo/props
GQuery prop method
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java61
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java31
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java5
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java83
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java28
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java2
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java18
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTestGwt.java31
-rw-r--r--pom.xml4
9 files changed, 197 insertions, 66 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 47370ef4..d2d0e486 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
@@ -25,9 +25,18 @@ import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayMixed;
import com.google.gwt.core.client.JsArrayString;
-import com.google.gwt.dom.client.*;
+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.InputElement;
+import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.NodeList;
+import com.google.gwt.dom.client.OptionElement;
+import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.HasCssName;
+import com.google.gwt.dom.client.TextAreaElement;
import com.google.gwt.query.client.css.CSS;
import com.google.gwt.query.client.css.HasCssValue;
import com.google.gwt.query.client.css.TakesCssValue;
@@ -265,7 +274,9 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
if (o instanceof IsWidget) {
return $(Arrays.asList(o));
}
- System.err.println("GQuery.$(Object o) could not wrap the type : " + o.getClass());
+ if (!GWT.isProdMode()) {
+ System.err.println("GQuery.$(Object o) could not wrap the type : " + o.getClass());
+ }
}
return $();
}
@@ -3445,34 +3456,49 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
}
/**
- * Accesses a boolean property on the first matched element.
- *
- * @param key the name of the boolean property to be accessed
+ * Get the value of a property for the first element in the set of matched elements.
*
- * @return <code>true</code> if at least one element is matched and the specified boolean property
- * is set to <code>true</code> on the first matched element; <code>false</code> otherwise
+ * @param key the name of the property to be accessed
+ * @return the value of the property, in the case the property is a 'boolean' it
+ * returns a Boolean object, and a Double if is a 'number', so be prepared
+ * if you cast to other numeric objects. In the case of the property is undefined
+ * it returns null.
+ */
+ public <T> T prop(String key) {
+ assert key != null : "Key is null";
+ return isEmpty() ? null : JsUtils.<T>prop(get(0), key);
+ }
+
+ /**
+ * Get the value of a property for the first element in the set of matched elements.
*
+ * @param key the name of the property to be accessed
+ * @param clz the class of the type to return
+ *
+ * @return the value of the property, it safely check the type passed as parameter
+ * and preform the aproproate transformations for numbers and booleans.
+ * In the case of the property is undefined it returns null.
*/
- public boolean prop(String key) {
+ public <T> T prop(String key, Class<? extends T> clz) {
assert key != null : "Key is null";
-
- return !isEmpty() && get(0).getPropertyBoolean(key);
+ return isEmpty() ? null : JsUtils.<T>prop(get(0), key, clz);
}
/**
- * Sets a boolean property to a value on all matched elements.
+ * Sets a property to a value on all matched elements.
*
* @param key the name of the boolean property to be set
- * @param value the value the specified boolean property should be set to
+ * @param value the value specified. In the case the value is a Number, it is set
+ * as a 'number' in the javascript object and the same with Boolean.
*
* @return this <code>GQuery</code> object
*
*/
- public GQuery prop(String key, boolean value) {
+ public GQuery prop(String key, Object value) {
assert key != null : "Key is null";
- for (final Element element : elements) {
- element.setPropertyBoolean(key, value);
+ for (Element e : elements) {
+ JsUtils.prop(e, key, value);
}
return this;
@@ -3499,10 +3525,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
int i = 0;
for (Element e : elements) {
Object value = closure.f(e, i++);
- if (value != null) {
- e.setPropertyBoolean(key, value instanceof Boolean ? (Boolean) value : Boolean
- .valueOf(value.toString()));
- }
+ JsUtils.prop(e, key, value);
}
return this;
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 01492ede..17b04f7a 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
@@ -1745,26 +1745,39 @@ public interface LazyGQuery<T> extends LazyBase<T>{
LazyGQuery<T> prevUntil(GQuery until, String filter);
/**
- * Accesses a boolean property on the first matched element.
+ * Get the value of a property for the first element in the set of matched elements.
*
- * @param key the name of the boolean property to be accessed
- *
- * @return <code>true</code> if at least one element is matched and the specified boolean property
- * is set to <code>true</code> on the first matched element; <code>false</code> otherwise
+ * @param key the name of the property to be accessed
+ * @return the value of the property, in the case the property is a 'boolean' it
+ * returns a Boolean object, and a Double if is a 'number', so be prepared
+ * if you cast to other numeric objects. In the case of the property is undefined
+ * it returns null.
+ */
+ <T> T prop(String key);
+
+ /**
+ * Get the value of a property for the first element in the set of matched elements.
*
+ * @param key the name of the property to be accessed
+ * @param clz the class of the type to return
+ *
+ * @return the value of the property, it safely check the type passed as parameter
+ * and preform the aproproate transformations for numbers and booleans.
+ * In the case of the property is undefined it returns null.
*/
- boolean prop(String key);
+ <T> T prop(String key, Class<? extends T> clz);
/**
- * Sets a boolean property to a value on all matched elements.
+ * Sets a property to a value on all matched elements.
*
* @param key the name of the boolean property to be set
- * @param value the value the specified boolean property should be set to
+ * @param value the value specified. In the case the value is a Number, it is set
+ * as a 'number' in the javascript object and the same with Boolean.
*
* @return this <code>GQuery</code> object
*
*/
- LazyGQuery<T> prop(String key, boolean value);
+ LazyGQuery<T> prop(String key, Object value);
/**
* Sets a boolean property to a computed value on all matched elements.
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java
index 090089f5..7999e10d 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java
@@ -17,6 +17,7 @@ package com.google.gwt.query.client;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayMixed;
+import com.google.gwt.core.shared.GWT;
import com.google.gwt.query.client.js.JsCache;
import com.google.gwt.query.client.js.JsUtils;
@@ -35,7 +36,9 @@ public class Properties extends JavaScriptObject {
try {
return JsUtils.parseJSON(p);
} catch (Exception e) {
- System.err.println("Error creating Properties: \n> " + properties + "\n< " + p + "\n" + e.getMessage());
+ if (!GWT.isProdMode()) {
+ System.err.println("Error creating Properties: \n> " + properties + "\n< " + p + "\n" + e.getMessage());
+ }
}
}
return create();
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java
index f8dca659..5d564cc8 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java
@@ -44,7 +44,7 @@ public class JsCache extends JavaScriptObject {
}
}
- public final native <T> void delete(T name) /*-{
+ public final native void delete(Object name) /*-{
delete this[name];
}-*/;
@@ -56,45 +56,67 @@ public class JsCache extends JavaScriptObject {
}
}-*/;
- public final native <T> boolean exists(T name) /*-{
+ public final native boolean exists(Object name) /*-{
return !!this[name];
}-*/;
- public final native <R, T> R get(T id) /*-{
- var r = this[id], t = typeof r;
- return r && t != 'number' && t != 'boolean' ? r : null;
+ @SuppressWarnings("unchecked")
+ public final <T> T get(Object id, Class<? extends T> clz) {
+ Object o = get(id);
+ if (o instanceof Double) {
+ Double d = (Double)o;
+ if (clz == Float.class) o = d.floatValue();
+ else if (clz == Integer.class) o = d.intValue();
+ else if (clz == Long.class) o = d.longValue();
+ else if (clz == Short.class) o = d.shortValue();
+ else if (clz == Byte.class) o = d.byteValue();
+ } else if (clz == Boolean.class && !(o instanceof Boolean)) {
+ o = Boolean.valueOf(String.valueOf(o));
+ } else if (clz == String.class && !(o instanceof String)) {
+ o = String.valueOf(o);
+ }
+ return (T)o;
+ }
+
+ public final native <T> T get(Object id) /*-{
+ var r = this && this[id], t = typeof r;
+ // box booleans and numbers in a gwt object
+ if (t == 'boolean') return @java.lang.Boolean::valueOf(Z)(r);
+ if (t == 'number') return @java.lang.Double::valueOf(D)(r);
+ return r;
}-*/;
- public final <T> JsCache getCache(int id) {
+ public final JsCache getCache(int id) {
return (JsCache)get(id);
}
- public final native <T> boolean getBoolean(T id) /*-{
- return /true|1/.test(this[id]);
- }-*/;
+ public final boolean getBoolean(Object id) {
+ Boolean r = get(id, Boolean.class);
+ return r == null ? false : r;
+ }
- public final <T> float getFloat(T id) {
- return (float)getDouble(id);
+ public final float getFloat(Object id) {
+ Float r = get(id, Float.class);
+ return r == null ? 0 : r;
}
- public final native <T> double getDouble(T id) /*-{
- // HtmlUnit prints an 'Unknown property name in get valueOf'
- // error here, but it is ok.
- var r = this[id] ? Number(this[id]) : 0;
- return r ? r : 0;
- }-*/;
+ public final double getDouble(Object id) {
+ Double r = get(id, Double.class);
+ return r == null ? 0 : r;
+ }
- public final <T> int getInt(T id) {
- return (int)getDouble(id);
+ public final int getInt(Object id) {
+ Integer r = get(id, Integer.class);
+ return r == null ? 0 : r;
}
- public final native <T> String getString(T id) /*-{
+ public final native String getString(Object id) /*-{
return this[id] == null ? null : String(this[id]);
}-*/;
- public final native <T> JsArrayMixed getArray(T id) /*-{
+ public final native JsArrayMixed getArray(Object id) /*-{
var r = this[id];
- if (r && @com.google.gwt.query.client.js.JsUtils::isArray(*)(r)) {
+ if (Object.prototype.toString.call(r) == '[object Array]') {
return r;
}
return null;
@@ -124,17 +146,28 @@ public class JsCache extends JavaScriptObject {
return this.indexOf(o);
}-*/;
- public final native <T> JsCache putBoolean(T id, boolean b) /*-{
+ public final native JsCache putBoolean(Object id, boolean b) /*-{
this[id] = b;
return this;
}-*/;
- public final native <T> JsCache putNumber(T id, double n) /*-{
+ public final native JsCache putNumber(Object id, double n) /*-{
this[id] = n;
return this;
}-*/;
- public final native <T, O> JsCache put(T id, O obj) /*-{
+ public final JsCache put(Object id, Object obj) {
+ if (obj instanceof Boolean) {
+ putBoolean(id, ((Boolean)obj).booleanValue());
+ } else if (obj instanceof Number) {
+ putNumber(id, ((Number)obj).doubleValue());
+ } else {
+ putObject(id, obj);
+ }
+ return this;
+ }
+
+ public final native JsCache putObject(Object id, Object obj) /*-{
this[id] = obj;
return this;
}-*/;
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
index 974462db..2d9ba9dd 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java
@@ -179,6 +179,29 @@ public class JsUtils {
}
private static JsUtilsImpl utilsImpl = GWT.create(JsUtilsImpl.class);
+
+ /**
+ * Returns a property present in a javascript object.
+ */
+ public static <T> T prop(JavaScriptObject o, Object id, Class<? extends T> type) {
+ return o == null ? null : o.<JsCache>cast().get(id, type);
+ }
+
+ /**
+ * Returns a property present in a javascript object.
+ */
+ public static <T> T prop(JavaScriptObject o, Object id) {
+ return o == null ? null : o.<JsCache>cast().<T>get(id);
+ }
+
+ /**
+ * Set a property to a javascript object
+ */
+ public static void prop(JavaScriptObject o, Object id, Object val) {
+ if (o != null) {
+ o.<JsCache>cast().put(id, val);
+ }
+ }
/**
* Camelize style property names. for instance: font-name -> fontName
@@ -381,8 +404,9 @@ public class JsUtils {
try {
return utilsImpl.parseJSON(json);
} catch (Exception e) {
- System.err.println("Error while parsing json: " + e.getMessage() + ".\n"
- + json);
+ if (!GWT.isProdMode()) {
+ System.err.println("Error while parsing json: " + e.getMessage() + ".\n" + json);
+ }
return Properties.create();
}
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java
index 0a34b5d5..04db71fd 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java
@@ -87,7 +87,7 @@ public class Callbacks {
*/
public Callbacks(String options) {
this();
- opts.load(Properties.create(options.replaceAll("[^\\S]+|$", ":1,")));
+ opts.load(Properties.create(options.replaceAll("[^\\S]+|$", ":true,")));
}
/**
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
index 8f58b519..d5d58793 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java
@@ -18,6 +18,7 @@ package com.google.gwt.query.client;
import static com.google.gwt.query.client.GQuery.$;
import static com.google.gwt.query.client.GQuery.$$;
import static com.google.gwt.query.client.GQuery.document;
+import static com.google.gwt.query.client.GQuery.window;
import java.util.ArrayList;
import java.util.Arrays;
@@ -48,6 +49,7 @@ import com.google.gwt.query.client.js.JsNodeArray;
import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
+import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
@@ -475,7 +477,7 @@ public class GQueryCoreTestGwt extends GWTTestCase {
assertEquals(1, g2.size());
assertEquals(expected, g2.toString());
}
-
+
public void test_issue128() {
GQuery g = $(e).html("<span>a</span><span>b</span><span>c</span>");
assertEquals(g.text(), "abc");
@@ -565,8 +567,8 @@ public class GQueryCoreTestGwt extends GWTTestCase {
public void testPropMethod(){
$(e).html("<input id=\"checkBox1\" type=\"checkbox\" checked=\"checked\" /> <input id=\"checkBox2\" type=\"checkbox\" />");
- assertTrue($("#checkBox1",e).prop("checked"));
- assertFalse($("#checkBox2",e).prop("checked"));
+ assertEquals(true, $("#checkBox1",e).prop("checked"));
+ assertEquals(false, $("#checkBox2",e).prop("checked"));
$("#checkBox1",e).prop("checked", false);
$("#checkBox2",e).prop("checked", new Function() {
@@ -575,10 +577,12 @@ public class GQueryCoreTestGwt extends GWTTestCase {
return Boolean.TRUE;
}
});
-
- assertTrue($("#checkBox2",e).prop("checked"));
- assertFalse($("#checkBox1",e).prop("checked"));
-
+ assertEquals(true, $("#checkBox2",e).prop("checked"));
+ assertEquals(false, $("#checkBox1",e).prop("checked"));
+
+ $(window).prop("foo", 234);
+ assertEquals(234d, $(window).prop("foo"));
+ assertEquals(234l, (long)$(window).prop("foo", Long.class));
}
@DoNotRunWith(Platform.Prod)
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTestGwt.java
index f89c7aaa..74f91d12 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTestGwt.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryJsTestGwt.java
@@ -91,6 +91,37 @@ public class GQueryJsTestGwt extends GWTTestCase {
assertEquals(6, c.length());
assertEquals("N", c.get(-1));
}
+
+ public void testGetMethod() {
+ JsCache j = GQuery.$$("bt: true, bf: false, dz: 0, dp: 1.2, dn: -2.3, st: 'foo', nl: null").cast();
+ Boolean bt = j.get("bt");
+ assertEquals(Boolean.TRUE, bt);
+ Boolean bf = j.get("bf");
+ assertEquals(Boolean.FALSE, bf);
+ Double dz = j.get("dz");
+ assertEquals(0d, dz);
+ Double dp = j.get("dp");
+ assertEquals(1.2, dp);
+ Double dn = j.get("dn");
+ assertEquals(-2.3, dn);
+ String st = j.get("st");
+ assertEquals("foo", st);
+ Object o = j.get("nl");
+ assertNull(o);
+
+ Integer i = j.get("dp", Integer.class);
+ assertEquals(new Integer(1), i);
+ Short s = j.get("dp", Short.class);
+ assertEquals((short)1, (short)s);
+ Long l = j.get("dp", Long.class);
+ assertEquals(1l, (long)l);
+ Byte b = j.get("dp", Byte.class);
+ assertEquals((byte)1, (byte)b);
+
+ j.put("me", j);
+ JsCache r = j.get("me");
+ assertEquals(j, r);
+ }
public void testChrome__gwt_ObjectId() {
JsCache a = JsCache.create();
diff --git a/pom.xml b/pom.xml
index 92847895..54d5584e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -148,8 +148,8 @@
<properties>
<!-- <gwtversion>2.4.0-dollarpatch</gwtversion> -->
- <gwtversion>2.5.0</gwtversion>
- <gwtmaven>2.5.0</gwtmaven>
+ <gwtversion>2.5.1</gwtversion>
+ <gwtmaven>2.5.1-rc1</gwtmaven>
<gqueryclassifier />
<gwt.loglevel>INFO</gwt.loglevel>
<gwt.outputstyle>OBF</gwt.outputstyle>