Difference in display between HTML and XHTML

2020-04-12 00:04发布

I thought XHTML documents were supposed to be displayed with exactly the same standards compliance mode as "strict" HTML documents.

However, there is a difference in how they display pre elements: in HTML documents, if the <pre> start tag is followed by a LF (or CRLF), this is ignored. Not so in XHTML.

Example: a HTML file and a XHTML file that have the same content, but are rendered differently.
(Or if you think it's cheating to give a HTML file and a XHTML file exactly the same content, including DOCTYPE, here are two files that follow more classic rules: HTML 4.01 strict and XHTML 1.0 strict.)

So, does that mean that statements like this W3C page's

all line breaks and spaces are rendered exactly as they appear in the HTML. The exception is a newline immediately after the start tag <pre> and immediately before the end tag </pre>, which are discarded.

are only valid for HTML, not for XHTML? I haven't found any indication that XHTML can display things differently if it likes. But all the browsers I've tested do.

1条回答
家丑人穷心不美
2楼-- · 2020-04-12 00:44

The removal of the starting newline is nothing to do with the rendering in HTML, and Dave Raggett's page is just a little imprecise and getting a little out of date. The removal of the starting newline is a property of the HTML parser.

In the in body tree construction phase the HTML5 spec for the text/html mime type says:

A start tag whose tag name is one of: "pre", "listing"

If the stack of open elements has a p element in button scope, then close a p element.

Insert an HTML element for the token.

If the next token is a "LF" (U+000A) character token, then ignore that token and move on to the next one. (Newlines at the start of pre blocks are ignored as an authoring convenience.)

Set the frameset-ok flag to "not ok".

An XML parser is simply not allowed to do this by the XML parsing rules, so XHTML does not drop that new line.

查看更多
登录 后发表回答