Fix for issue 960, ensuring consistent whitespace behavior for setInnerText.
Instead of using IE's native innerText, which translates whitespace to html
entities, we do the same thing on every browser to minimize surprise.
Patch by: fredsa, ajr
Review by: knorton, scottb
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@2874 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/dom/client/DOMImpl.java b/user/src/com/google/gwt/dom/client/DOMImpl.java
index 3369ea2..928f3a3 100644
--- a/user/src/com/google/gwt/dom/client/DOMImpl.java
+++ b/user/src/com/google/gwt/dom/client/DOMImpl.java
@@ -180,9 +180,8 @@
public native void setInnerText(Element elem, String text) /*-{
// Remove all children first.
- while (elem.firstChild) {
- elem.removeChild(elem.firstChild);
- }
+ elem.innerHTML = '';
+
// Add a new text node.
if (text != null) {
elem.appendChild($doc.createTextNode(text));
diff --git a/user/src/com/google/gwt/dom/client/DOMImplIE6.java b/user/src/com/google/gwt/dom/client/DOMImplIE6.java
index fe4e816..6285034 100644
--- a/user/src/com/google/gwt/dom/client/DOMImplIE6.java
+++ b/user/src/com/google/gwt/dom/client/DOMImplIE6.java
@@ -101,9 +101,4 @@
select.add(option);
}
}-*/;
-
- @Override
- public native void setInnerText(Element elem, String text) /*-{
- elem.innerText = text || '';
- }-*/;
}
diff --git a/user/test/com/google/gwt/user/client/ui/DOMTest.java b/user/test/com/google/gwt/user/client/ui/DOMTest.java
index c58977f..40ec2ca 100644
--- a/user/test/com/google/gwt/user/client/ui/DOMTest.java
+++ b/user/test/com/google/gwt/user/client/ui/DOMTest.java
@@ -211,6 +211,31 @@
// should be deleted, including any text nodes, for all supported browsers.
assertTrue(getDenormalizedChildCount(tdElem) == 0);
}
+
+ /**
+ * Tests that {@link DOM#setInnerText(Element, String)} works consistently
+ * across browsers with respect to whitespace. Particularly,
+ * we want to guarantee that whitespace characters won't be replaced with
+ * HTML entities.
+ */
+ public void testSetInnerTextWhitespace() {
+ Element divElem = DOM.createDiv();
+ String spaces = " ";
+ String whitespace = " \n\t ";
+
+ // Block of spaces first. We call trim because it remvoes leading and
+ // trailing whitespace characters -- so, if all is well, all of them.
+ DOM.setInnerText(divElem, spaces);
+ String contents = DOM.getInnerText(divElem);
+ String trimmed = contents.trim();
+ assertEquals(trimmed, "");
+
+ // Now with some assorted whitespace.
+ DOM.setInnerText(divElem, whitespace);
+ contents = DOM.getInnerText(divElem);
+ trimmed = contents.trim();
+ assertEquals(trimmed, "");
+ }
/**
* Tests the correctness of setting the <code>src</code> attribute on