Joel - Please do a code review for this Style enhancement.
Description: ======== When setting style properties programatically in GWT, it would be nice to assert that the styles do not contain hyphens so people don't accidently use names like "margin-left" instead of "marginLeft". Fix: === This patch adds an assertion that the property name for all Style methods is in camelCase format. I verified that when assertions are disabled, the code will be completely compiled out. I also verified that mozilla extensions follow the camelCase rule (eg. "-moz-opacity" should be "MozOpacity"). Testing: ====== I verified this in hosted and web mode on FF and added a unit test. Thanks, John LaBanca [EMAIL PROTECTED] --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---
Index: user/test/com/google/gwt/dom/client/ElementTest.java =================================================================== --- user/test/com/google/gwt/dom/client/ElementTest.java (revision 3661) +++ user/test/com/google/gwt/dom/client/ElementTest.java (working copy) @@ -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() { Index: user/src/com/google/gwt/dom/client/Style.java =================================================================== --- user/src/com/google/gwt/dom/client/Style.java (revision 3661) +++ user/src/com/google/gwt/dom/client/Style.java (working copy) @@ -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"; + }-*/; }