Mozilla bug - innerHTML and XHTML pages

Bug confirmed in Mozilla 1.6, Firefox 1.0PR and Safari 1.2.

The innerHTML property behaves weirdly in Mozilla and Safari when used within XHTML pages (ie. pages with an .xhtml extension and MIME type application/xhtml+xml).

Tests

Below you find two tests that work on the innerHTML of the <h2> tag at the top of this page. Reading out the innerHTML works fine, but writing to it causes the text in the header to disappear and Mozilla to throw an error.

Safari probably throws an error, too, but it doesn't hide the original text.

Get the innerHTML of the h2.

Set the innerHTML of the h2.

Discussion

In my opinion this is a bug. Mozilla could have chosen two ways of handling innerHTML in XHTML pages:

  1. Make sure it works just as it always works. (Opera does this)
  2. Decide that innerHTML is non-standard and switch off support.

I prefer option 1, but I could live with option 2. However, the current state of affairs uses neither option, or rather, an uneasy blend of both options, which in my book counts as a bug.

David "liorean" Andersson replied:

They are internally using ranges and the CreateContextualFragment function for emulating the innerHTML setter. Have you tried either using a Range and the [object Range].createContextualFragment function or the global DOMParser function directly to see if they too fail this way? If the former do but the latter don't, then we know where the problem lies.

This is probably true (David generally knows what he's talking about), but sadly I don't understand it, and besides it's not an excuse for this weird behaviour. Either innerHTML works, or it doesn't. A middle way is not possible.