Reviewers: jlabanca, rjrjr,

Description:
c.f. http://code.google.com/p/google-web-toolkit/issues/detail?id=3644

Please review this at http://gwt-code-reviews.appspot.com/34801

Affected files:
   user/src/com/google/gwt/dom/client/Element.java
   user/src/com/google/gwt/dom/client/Node.java
   user/test/com/google/gwt/dom/client/ElementTest.java
   user/test/com/google/gwt/dom/client/NodeTest.java


Index: user/test/com/google/gwt/dom/client/NodeTest.java
===================================================================
--- user/test/com/google/gwt/dom/client/NodeTest.java   (revision 5327)
+++ user/test/com/google/gwt/dom/client/NodeTest.java   (working copy)
@@ -15,6 +15,7 @@
   */
  package com.google.gwt.dom.client;

+import com.google.gwt.core.client.JavaScriptObject;
  import com.google.gwt.junit.client.GWTTestCase;

  /**
@@ -185,4 +186,17 @@
      assertEquals(btn1, txt0.getNextSibling());
      assertEquals(btn1, txt1.getPreviousSibling());
    }
+
+  /**
+   * Tests Element.is() and Element.as().
+   */
+  public void testIsAndAs() {
+    assertTrue(Node.is(Document.get()));
+
+    JavaScriptObject text = Document.get().createTextNode("foo");
+    assertTrue(Node.is(text));
+
+    // Node.is(null) is allowed and should return false.
+    assertFalse(Node.is(null));
+  }
  }
Index: user/test/com/google/gwt/dom/client/ElementTest.java
===================================================================
--- user/test/com/google/gwt/dom/client/ElementTest.java        (revision 5327)
+++ user/test/com/google/gwt/dom/client/ElementTest.java        (working copy)
@@ -377,4 +377,18 @@
        }
      }
    }
+
+  /**
+   * Tests Element.is() and Element.as().
+   */
+  public void testIsAndAs() {
+    assertFalse(Element.is(Document.get()));
+
+    Node div = Document.get().createDivElement();
+    assertTrue(Element.is(div));
+    assertEquals("div", Element.as(div).getTagName().toLowerCase());
+
+    // Element.is(null) is allowed and should return false.
+    assertFalse(Element.is(null));
+  }
  }
Index: user/src/com/google/gwt/dom/client/Element.java
===================================================================
--- user/src/com/google/gwt/dom/client/Element.java     (revision 5327)
+++ user/src/com/google/gwt/dom/client/Element.java     (working copy)
@@ -42,7 +42,8 @@

    /**
     * Determines whether the given {...@link JavaScriptObject} can be cast to  
an
-   * {...@link Element}.
+   * {...@link Element}. A <code>null</code> object will cause this method to
+   * return <code>false</code>.
     */
    public static boolean is(JavaScriptObject o) {
      if (Node.is(o)) {
@@ -53,9 +54,11 @@

    /**
     * Determine whether the given {...@link Node} can be cast to an {...@link  
Element}.
+   * A <code>null</code> node will cause this method to return
+   * <code>false</code>.
     */
    public static boolean is(Node node) {
-    return node.getNodeType() == Node.ELEMENT_NODE;
+    return (node != null) && (node.getNodeType() == Node.ELEMENT_NODE);
    }

    protected Element() {
Index: user/src/com/google/gwt/dom/client/Node.java
===================================================================
--- user/src/com/google/gwt/dom/client/Node.java        (revision 5327)
+++ user/src/com/google/gwt/dom/client/Node.java        (working copy)
@@ -50,10 +50,12 @@
    }

    /**
-   * Determines whether the given {...@link JavaScriptObject} is a DOM node.
+   * Determines whether the given {...@link JavaScriptObject} is a DOM node. A
+   * <code>null</code> object will cause this method to return
+   * <code>false</code>.
     */
    public static native boolean is(JavaScriptObject o) /*-{
-    return !!o.nodeType;
+    return (!!o) && (!!o.nodeType);
    }-*/;

    protected Node() {



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

Reply via email to