aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManolo Carrasco <manolo@apache.org>2011-08-26 11:24:25 +0000
committerManolo Carrasco <manolo@apache.org>2011-08-26 11:24:25 +0000
commit625fee96a194ab6e69dd7cb1d874c1e8f67d3d9a (patch)
tree0a54df32fe7bf736b7befafabc3c7e304a1bf141
parent5252c707d950bccca07bc7320f70503b675361a7 (diff)
downloadgwtquery-625fee96a194ab6e69dd7cb1d874c1e8f67d3d9a.tar.gz
gwtquery-625fee96a194ab6e69dd7cb1d874c1e8f67d3d9a.zip
Fix attr and removeAttr with checked, Fixes issue97. Thanks to @arny.ok for the code
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java31
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java6
2 files changed, 30 insertions, 7 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 d6b394cc..8926f5cf 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
@@ -469,10 +469,19 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
return array;
}-*/;
+ private static native void setElementAttribute(Element e, String key, String value) /*-{
+ if (value == null)
+ e.removeAttribute(key);
+ else
+ e.setAttribute(key, value);
+ e[key] = value;
+ }-*/;
+
private static native void setElementValue(Element e, String value) /*-{
e.value = value;
}-*/;
+
private static native void scrollIntoViewImpl(Node n) /*-{
if (n)
n.scrollIntoView()
@@ -891,18 +900,29 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
for (Element e : elements) {
Object val = closure.f(e.<com.google.gwt.dom.client.Element>cast(), i++);
if (val != null) {
- e.setAttribute(key, String.valueOf(val));
+ setElementAttribute(e, key, String.valueOf(val));
}
}
return this;
}
-
+
+ /**
+ * Set a single property to a value, on all matched elements.
+ */
+ public GQuery attr(String key, boolean value) {
+ String val = value ? "true" : null;
+ for (Element e : elements) {
+ setElementAttribute(e, key, val);
+ }
+ return this;
+ }
+
/**
* Set a single property to a value, on all matched elements.
*/
public GQuery attr(String key, String value) {
for (Element e : elements) {
- e.setAttribute(key, value);
+ setElementAttribute(e, key, value);
}
return this;
}
@@ -3032,10 +3052,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* Remove the named attribute from every element in the matched set.
*/
public GQuery removeAttr(String key) {
- for (Element e : elements) {
- e.removeAttribute(key);
- }
- return this;
+ return attr(key, (String)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 163dc03d..ec2d913a 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
@@ -985,6 +985,12 @@ public class GQueryCoreTest extends GWTTestCase {
assertEquals("1", $("#cb:checked", e).val());
$("#cb", e).removeAttr("checked");
assertNull($("#cb:checked", e).val());
+ $("#cb", e).attr("checked", true);
+ assertEquals("1", $("#cb:checked", e).val());
+ $("#cb", e).attr("checked", false);
+ assertNull($("#cb:checked", e).val());
+ $("#cb", e).attr("checked", "");
+ assertNull($("#cb:checked", e).val());
}
public void testWidthHeight() {