diff options
9 files changed, 191 insertions, 64 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..0fad3d5c 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; @@ -3445,34 +3454,49 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { } /** - * 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. + */ + 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 +3523,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..87840ec7 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 @@ -35,7 +35,8 @@ 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()); + String msg = e.getMessage(); + System.err.println("Error creating Properties: \n> " + properties + "\n< " + p + "\n" + msg); } } 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..645ccd15 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 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..7b0bbca6 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,8 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertEquals(1, g2.size()); assertEquals(expected, g2.toString()); } - + + // FIXME: it started failing after updating to 2.5.1 public void test_issue128() { GQuery g = $(e).html("<span>a</span><span>b</span><span>c</span>"); assertEquals(g.text(), "abc"); @@ -565,8 +568,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 +578,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(); @@ -67,7 +67,7 @@ <!-- <module>gwtquery-core-2.1.0</module> --> <!-- <module>gwtquery-core-2.0.1</module> --> <!-- <module>samples</module> --> - <!-- <module>devtest</module> --> + <module>devtest</module> </modules> <pluginRepositories> @@ -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> |