Added an assertion that style attribute names do not contain a hyphen. Users should use the camelCase form of the attribute name. Patch by: jlabanca Review by: jgw Issue: 2667 git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@3667 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/dom/client/Style.java b/user/src/com/google/gwt/dom/client/Style.java index 08f4d17..56720cd 100644 --- a/user/src/com/google/gwt/dom/client/Style.java +++ b/user/src/com/google/gwt/dom/client/Style.java
@@ -30,23 +30,59 @@ /** * Gets the value of a named property. */ - public final native String getProperty(String name) /*-{ - return this[name]; - }-*/; + public final String getProperty(String name) { + assertCamelCase(name); + return getPropertyImpl(name); + } /** * Sets the value of a named property. */ - public final native void setProperty(String name, String value) /*-{ - this[name] = value; - }-*/; + public final void setProperty(String name, String value) { + assertCamelCase(name); + setPropertyImpl(name, value); + } /** * Sets the value of a named property, in pixels. * * This is shorthand for <code>value + "px"</code>. */ - public final native void setPropertyPx(String name, int value) /*-{ - this[name] = value + "px"; - }-*/; + public final void setPropertyPx(String name, int value) { + assertCamelCase(name); + setPropertyPxImpl(name, value); + } + + /** + * Assert that the specified property does not contain a hyphen. + * + * @param name the property name + */ + private void assertCamelCase(String name) { + assert !name.contains("-") : "The style name '" + name + + "' should be in camelCase format"; + } + + /** + * Gets the value of a named property. + */ + private native String getPropertyImpl(String name) /*-{ + return this[name]; + }-*/; + + /** + * Sets the value of a named property. + */ + private native void setPropertyImpl(String name, String value) /*-{ + this[name] = value; + }-*/; + + /** + * Sets the value of a named property, in pixels. + * + * This is shorthand for <code>value + "px"</code>. + */ + private native void setPropertyPxImpl(String name, int value) /*-{ + this[name] = value + "px"; + }-*/; }
diff --git a/user/test/com/google/gwt/dom/client/ElementTest.java b/user/test/com/google/gwt/dom/client/ElementTest.java index 97398f4..9eb5775 100644 --- a/user/test/com/google/gwt/dom/client/ElementTest.java +++ b/user/test/com/google/gwt/dom/client/ElementTest.java
@@ -268,6 +268,39 @@ } /** + * Test that styles only allow camelCase. + */ + public void testStyleCamelCase() { + DivElement div = Document.get().createDivElement(); + + // Use a camelCase property + div.getStyle().setProperty("backgroundColor", "black"); + assertEquals("black", div.getStyle().getProperty("backgroundColor")); + div.getStyle().setPropertyPx("marginLeft", 10); + assertEquals("10px", div.getStyle().getProperty("marginLeft")); + + // Use a hyphenated style + try { + div.getStyle().setProperty("background-color", "red"); + fail("Expected assertion error: background-color should be in camelCase"); + } catch (AssertionError e) { + // expected + } + try { + div.getStyle().setPropertyPx("margin-left", 20); + fail("Expected assertion error: margin-left should be in camelCase"); + } catch (AssertionError e) { + // expected + } + try { + div.getStyle().getProperty("margin-right"); + fail("Expected assertion error: margin-right should be in camelCase"); + } catch (AssertionError e) { + // expected + } + } + + /** * offset[Left|Top|Width|Height], offsetParent */ public void testOffsets() {