开发者

XHTML empty tags not treated as empty tags by any browser

开发者 https://www.devze.com 2023-01-18 23:22 出处:网络
So I\'ve tried this XHTML 1.1 code (validated at validator.w3.org) in Chrome 6, IE 8, and Firefox 3.5. The <p> following the <a/> gets hyperlinked, and the <p> following the <div/

So I've tried this XHTML 1.1 code (validated at validator.w3.org) in Chrome 6, IE 8, and Firefox 3.5. The <p> following the <a/> gets hyperlinked, and the <p> following the <div/> turns red:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
    "http://www.w3.org/TR/xhtml11开发者_如何学Go/DTD/xhtml11.dtd">
<html 
    xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>test</title>
    </head>
    <body>
        <p><a href="http://www.yahoo.com"/></p>
        <p>
            this should not be hyperlinked
        </p>
        <div style="background:red"/>
        <p>this should not be red</p>
    </body>
</html>

This is really bad news for anyone trying to deal with documents using XML parsers/generators.

I might be able to just convert all </> tags to <></>, but that would mean that things like <br/> become <br></br> -- which is just weird, albeit valid.

Thoughts?


If you serve your document with an XML content-type (such as application/xhtml+xml) then you shouldn't have this problem.

It sounds like you are serving your document as text/html (although this isn't blessed by the text/html specification, which only goes up to XHTML 1.0) in which case you need to follow the HTML compatibility guidelines as you are telling browsers (and other user agents) that it is HTML rather than XHTML.

I might be able to just convert all </> tags to <></>, but that would mean that things like <br/> become <br></br> -- which is just weird, albeit valid.

… and wrong. Some browsers will treat that as <br><br>. Elements defined as EMPTY should use self-closing syntax, everything else should have explicit start and end tags.

Sadly, the simple option of using the correct content-type just introduces a different problem…

XHTML empty tags not treated as empty tags by any browser

… although I believe this will be resolved when IE8 and lower lose significant market share as IE9 introduces support for XHTML.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号