instanceof Element check fails in FF

4 views
Skip to first unread message

bhomass

unread,
Nov 17, 2009, 12:14:44 AM11/17/09
to Google Web Toolkit
I have this statement
NodeList<Node> childNodes = tdElement.getChildNodes();
for (int i=0; i<childNodes.getLength(); i++){
Node childNode = childNodes.getItem(i);
if (childNode instanceof Element&& !"#text".equals
(childNode.getNodeName())){
dosomething();
}
}

but the #text nodes get thru the if (childNode instanceof Element)
check. I have to add

if (childNode instanceof Element&& !"#text".equals
(childNode.getNodeName())

to fix it. I am sure #text is not an element, and this problem does
not occur in IE, only in FF.

anyone else knows about this anomaly?

Thomas Broyer

unread,
Nov 18, 2009, 11:03:30 AM11/18/09
to Google Web Toolkit
That's not a bug: Element is a JavaScriptObject, and all
JavaScriptObject-derived class are 'instanceof' others.
See http://code.google.com/webtoolkit/doc/1.6/DevGuideCodingBasics.html#DevGuideOverlayTypes
and particularly http://sites.google.com/site/io/surprisingly-rockin-javascript-and-dom-programming-in-gwt
(this is explicitly spelled out at slide 26) and
http://code.google.com/p/google-web-toolkit/wiki/OverlayTypes (search
for "instanceof")

In your case, the fix is easy:

if (Element.is(childNode)) {
dosomething();
}
Reply all
Reply to author
Forward
0 new messages