From 6c36a6ef2221026713b9d6146bd4fb50afc6b663 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Thu, 7 Feb 2013 15:59:18 +0100 Subject: [PATCH] Remove attribute after the property is set to null. Fixes issue 165 --- .../google/gwt/query/client/impl/AttributeImpl.java | 2 +- .../com/google/gwt/query/client/js/JsUtils.java | 13 +++++++++++-- .../google/gwt/query/client/GQueryCoreTestGwt.java | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/AttributeImpl.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/AttributeImpl.java index 76fd335c..fcdba699 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/AttributeImpl.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/AttributeImpl.java @@ -170,7 +170,6 @@ public class AttributeImpl { if (e.getNodeType() != 1) { continue; } - e.removeAttribute(key); if (JsUtils.hasProperty(e, key)) { if (BOOLEAN_ATTR_REGEX.test(key)) { e.setPropertyBoolean(key, false); @@ -178,6 +177,7 @@ public class AttributeImpl { e.setPropertyObject(key, null); } } + e.removeAttribute(key); } } 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 962eccf1..c54ab588 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 @@ -245,13 +245,22 @@ public class JsUtils { } /** - * Check if an object have already a property with name name + * Check if an object has already a property with name name * defined. */ public static native boolean hasProperty(JavaScriptObject o, String name)/*-{ return o && name in o; }-*/; - + + /** + * Check whether an element has an attribute, this is here since GWT Element.getAttribute + * implementation returns an empty string instead of null when the attribute is not + * present + */ + public static native boolean hasAttribute(JavaScriptObject o, String name)/*-{ + return !!(o && o.getAttribute(name)); + }-*/; + /** * Hyphenize style property names. for instance: fontName -> font-name */ 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 34fafb99..f4081011 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 @@ -1148,6 +1148,18 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertEquals("disabled", ie.getAttribute("disabled")); } + + public void testAttr_Issue165() { + $(e).html("anchor"); + Element a = $("a", e).get(0); + + assertEquals("a title", a.getAttribute("title")); + assertTrue(JsUtils.hasAttribute(a, "title")); + + $(a).removeAttr("title"); + assertEquals("", a.getAttribute("title")); + assertFalse(JsUtils.hasAttribute(a, "title")); + } @DoNotRunWith({Platform.Prod}) // FIXME: the hidden part does not work in FF nor Chrome -- 2.39.5