XHTML empty elements are valid but have childNodes

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:

Consider this valid XHTML markup:

<div />
<div></div>

These two divs are semantically equivalent in XML, but behave very differently in most browsers.

It seems that browsers treat the first div, using the short form, as not being closed. So they think it contains everything after the element is actually closed, and javascript evaluating childNodes.length on the first div element will get a nonzero value.

Test page Workaround is included
Reported by: Day Barr.

Explorer Windows, Mozilla, Opera, Safari | Reported on 18 October 2005.

Comments

(Add your own)

Posted by Krijn Hoetmer on 18 October 2005

1

When sending the file as application/xhtml+xml (XHTML 1.1 so it seems logical to do so) the "bug" of course doesn't apply ( http://ktk.xs4all.nl/stuff/xhtml/empty-or-not/ ).

Posted by Sebastian Redl on 18 October 2005

2

Indeed, the mistake is on the reporter's side, not the browser's. Since the markup is sent as text/html, it is treated by the browsers as broken HTML, not valid XHTML. Thus, the DIV is in fact NOT closed.

Posted by Day Barr on 18 October 2005

3

Ah my mistake. Krijn is right. Firefox and Opera behave correctly when rendering his version of the page, which is exactly the same as mine in content but served using the correct MIME type. However, IE 6 cannot view a page that is served with a MIME type of "application/xhtml+xml". Krijn's test page (http://ktk.xs4all.nl/stuff/xhtml/empty-or-not/ ) uses the HTTP response header
"Content-Type: application/xhtml+xml; charset=utf-8" but this causes IE 6 to offer the "File download" dialog instead of rendering the page. Screenshot at http://daybarr.com/playpen/emptyornot/ie6_xhtml_mime.jpg So this MIME type fix cannot be applied without breaking the page for IE users?

Posted by Krijn Hoetmer on 18 October 2005

4

It can be fixed by just using text/html and valid HTML (the same to some) :-) I don't see why you'd want to use (or XHTML 1.1 *cough*) anyway.

Posted by Krijn Hoetmer on 18 October 2005

5

..to use [div /] (or..

Posted by eric scheid on 12 December 2005

6

you might not want to use [div /], but the XSLT tool which you generate your pages with does, so you're screwed.

Posted by Nick Fitzsimons on 25 January 2006

7

@eric scheid: If you are using XSLT to produce HTML, you should use

and you're no longer screwed.

Using, or relying on, the default of is only appropriate if you are, in fact, generating output in an XML vocabulary (such as XHTML).

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