innerHTML and text normalization

Atom RSS

This site heavily relies on bug reports created by its readers. Anyone can report a bug and be published.

Main navigation:




Search reports by browser:

IE applies HTML normalization to the data that is assigned to the innerHTML property. This causes incorrect display of whitespace in elements that ought to preserve formatting, such as <pre> and <textarea>

Test page. Workaround is not included.
Reported by Sebastian Redl.

Explorer 5-6 Windows, Explorer 7 beta 2 | Reported on 22 November 2004.

Comments

(Add your own)

Posted by Greg Reimer on 24 November 2004

1

Applies to list elements also. Specifically, IE removes closing LI tags and insists on adding a line break. Especially troublesome since the line break triggers IE's whitespace-in-CSS-lists bug.

Posted by HyperListBuilder on 27 November 2004

2

I know why I keep coming back. Thanks!

Posted by Phil Endecott on 26 March 2005

3

It also does this when you set a text node's .data property. The only way I've found to change the content of a PRE element and have line breaks in the right place is to use .innerHTML and put tags for newlines. Any other solutiions?

Posted by victor on 7 June 2005

4

I've found out that setting outerHTML doesn't trigger normalization. So I've decided to test for .outerHTML property and if not false (i.e., IE), prepend and append the opening and closing pre tags to the content I want to set and assign that to outerHTML.

Posted by Ates Goral on 7 September 2005

5

victor: Thanks a lot for the workaround! Saved my day (night actually).

Code excerpt:

// Workaround for IE <PRE> innerHTML normalization quirk
if (elem.tagName == "PRE" && "outerHTML" in elem)
{
    elem.outerHTML = "<PRE>" + str + "</PRE>";
}
else
{
    elem.innerHTML = str;
}

(Works with IE & FF)

Post a comment

Commenting guidelines:

  1. When quoting specs, articles or your own research, please include a URL of the document.
  2. Test your stuff. When reporting browser behaviour, make sure that your report is correct.

Yes