FWIW, I think that GWT should guarantee a sane behaviour if and only
if we can guarantee that it's possible to offer it without a
significant hit across all browsers. I'd say that for methods with
varying behaviour, principal of least surprise (as noted by fred) is
key.
For these particular bugs:
- 1520: LGTM, the IE solution is nearly as optimal as raw innerHTML
- 314: I'd still rather see an append method on label vs.
guaranteeing roundtripping on innerText/innerHTML. It's certainly not
a pattern we should encourage (it's probably O(N^2) or worse for most
browsers).
- 960: Agreed that IE is the exception - would using createTextNode()
fix this? I think that innerText should set the node's text content
directly, as if you had placed the text escaped in a <span> element.
If you want non-breaking space, use the hex version, \u00a0, in your
strings.
The API consistency across browsers does seem important enough to
warrant this change.
Matt.
Comments inline. Thinking about it some more, I believe that the
guarantees and caveats that GWT should offer are:
- Text set via setInnerText() will be returned exactly as went in
when calling getInnerText() (solves the Label problem).
- HTML set via setInnerHTML() will preserve whitespace as best as
possible going in and will contain the same whitespace coming out.
Browsers may parse HTML differently, so developers should be cautious
to use only simple HTML if they expect results to match across the
board.
- HTML structure returned via getInnerHTML() will mostly resemble the
HTML set via setInnerHTML(), but no guarantees can be made about tag
casing, attribute casing, attribute order or other parsing/formatting
issues. The text content of the HTML returned will be identical to
the text content that was set.
On Feb 7, 3:19 pm, "Sam Gross" <colesb...@gmail.com> wrote:
> Can we use elem.textContent instead of the recursive method inHow does it behave with comments, scripts and style elements? I seem
> Gecko/Safari/Opera? It seems to work in Firefox 2, Safari 3, and
> Opera 9.25at least.
to recall that it differs in some ways. The current DOM
implementation probably includes some of these items in getInnerText()
right now, but we might be able to do something about this.
> setInnerText (issue 960):I think Fred was aiming for innerText to preserve whitespace by using
> Whitespace is not collapsed in IE6 when using the DOMImpl#setInnerText,
> although it doesn't translate spaces into . I think this is because
> IE6 relies on clobbering whitespace before the text nodes are created.
>
> I think adding elem.innerHTML = elem.innerHTML; after the line
> elem.appendChild($doc.createTextNode(text)); in IE6 only may resolve this.
>
> This also makes it more difficult to create a consistent getInnerText.
createTextNode() instead of innerText's setter. Ideally, setInnerText
shouldn't be creating nodes to avoid surprising the developer
with unexpected characters (IMHO, at least). I'm leaning towards the
whitespace-preserving approach myself - I think I'd expect that the
text I call the method with will be the exact value of a text node
underneath the element.
public void onModuleLoad() {
Element div = DOM.createElement("div");
DOM.appendChild(RootPanel.getBodyElement(), div);
DOM.setInnerText(div, "<abc> <def>");
}
<abc> <def>
Window.alert(DOM.getInnerHTML(div));
On Feb 7, 2008 3:19 PM, Sam Gross <cole...@gmail.com> wrote:getInnerText (issue 314):I don't think using the recursive getInnerText in IE6 will achieve consistency. IE6 clobbers the whitespace of statically created HTML elements, as well as whitespace set in the innerHTML property.
For example for a page containing the static HTML:
<span>abc def</span>
The recursive getInnerText on the span returns "abc def" in IE6, but it returns "abc def" in other browsers.
There's not much we can do about the static elements, but we can address the innerHTML assignments (=issue 1520). Ad we AJAX being the name of the game, hopefully dynamic content is a much more common use case than static.
I don't think DOM#getInnerText needs to be consistent, but I think it is important to make Label#getText consistent.
I'd love to see Label#getText be consistent, but I'd also like for us to also maximize the consistency of DOM#getInnerText.
Can we use elem.textContent instead of the recursive method in Gecko/Safari/Opera? It seems to work in Firefox 2, Safari 3, and Opera 9.25 at least.
Good idea. I agree that it seems to work on Safari/Gecko, but in Opera the HTML comments get pulled in as well (which was unexepected). I attached a new patch (issue314-getInnerText-r1805.patch) based on this and a couple of other changes here:
http://code.google.com/p/google-web-toolkit/issues/detail?id=314#c10
setInnerText (issue 960):
Whitespace is not collapsed in IE6 when using the DOMImpl#setInnerText, although it doesn't translate spaces into . I think this is because IE6 relies on clobbering whitespace before the text nodes are created.
Should I assume you mean *does* translate spaces into '....?
I think adding elem.innerHTML = elem.innerHTML; after the line elem.appendChild($doc.createTextNode(text)); in IE6 only may resolve this.This also makes it more difficult to create a consistent getInnerText.
Setting either innerHTML/innerText (or outerHTML/outerText for that matter) clobbers whitespace in IE (with the possible exception of certain uses of PRE elements), so I don't think that's an option. I believe the patch (issue960-setInnerText-r1790.patch) on 960, comment 7 works:
http://code.google.com/p/google-web-toolkit/issues/detail?id=960#c7
Or is it missing something in IE?
Label#getText (issue 314)I'm not sure of the best way to implement Label#getText because of the HTML subclass. I think getText should return the same value that was passed to setText. If the widget's text was set using HTML#setHTML, then getText should probably return a best-effort attempt using DOM#getInnerText.
That seems to make it easier to run through some of these test cases.
I'm okay with best effort in this case.
Since these three issues are really interrelated, I might propose applying all three (i.e. the latest patch on 314,960,1520) at once:
issue314-getInnerText-r1805.patch
issue960-setInnerText-r1790.patch
issue1520-ie-setInnerHTML-null-check-r1790.patch
Thanks--