Author: [EMAIL PROTECTED]
Date: Thu Sep 18 17:16:14 2008
New Revision: 3667

Modified:
    trunk/user/src/com/google/gwt/dom/client/Style.java
    trunk/user/test/com/google/gwt/dom/client/ElementTest.java

Log:
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

Modified: trunk/user/src/com/google/gwt/dom/client/Style.java
==============================================================================
--- trunk/user/src/com/google/gwt/dom/client/Style.java (original)
+++ trunk/user/src/com/google/gwt/dom/client/Style.java Thu Sep 18 17:16:14  
2008
@@ -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";
+   }-*/;
  }

Modified: trunk/user/test/com/google/gwt/dom/client/ElementTest.java
==============================================================================
--- trunk/user/test/com/google/gwt/dom/client/ElementTest.java  (original)
+++ trunk/user/test/com/google/gwt/dom/client/ElementTest.java  Thu Sep 18  
17:16:14 2008
@@ -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() {

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to