[1]: http://sgmljs.net
> The standard "tag soup" HTML parser has numerous
> surprising behaviors on invalid or questionable HTML code.
There is nothing surprising here. And some elements are allowed to omit end tags, or even start and end tags altogether.In fact, if you dig deeper there are a lot more surprises in XHTML documents (assuming they are served with the proper MIME type so they get treated like XHTML).
<input />, <br />, and <hr />
And it is valid XML. They are referred to as self closing tags.It fails if you try this with non-void elements: E.g. XHTML <div /> might turn into <div> in HTML5, which opens a div element, which is not the same. (I'm not sure if this behaviour is specified or not)
https://html.spec.whatwg.org/multipage/syntax.html#start-tag...
> 6. Then, if the element is one of the void elements [...] then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements [...]
https://html.spec.whatwg.org/multipage/syntax.html#void-elem...
> area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr
https://www.w3.org/wiki/Doctypes_and_markup_styles
For example...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
... is the XHTML 1.0 equivalent of HTML 4.01 except it won't validate loose HTML style markup, it all needs to be well formed XML.That said, for HTML5 ...
https://stackoverflow.com/questions/1570418/if-i-use-html-5s...
Ten years ago they thought that should work a while. Looks like it.
> you should take a look at SGML
I am aware that SGML is the ancestor of HTML and XML. I'm not sure there are any popular software today that handle SGML syntax. Furthermore XML has fewer rules to remember than SGML. So I will largely ignore SGML, except for explaining why some behaviors went into HTML.
> The hard-coded tag soup parser is by no means "standard".
I believe HTML5 standardized the syntax/error handling for HTML (not XHTML) documents. It should mean that for any given document, every HTML5-compliant web browser - Chrome, Firefox, Edge, etc. - will parse it into the same DOM tree.
> Does every popular browser support [XHTML] in the same way?
Yes, XHTML mode is supported by IE 9+, all Firefox, all Chrome, all Safari, all Opera, and all mobile browsers on Android and iOS. https://caniuse.com/#feat=xhtml
> I would expect some subtle differences, because this mode is not very popular and probably not as tested as standard HTML.
Indeed XHTML is very unpopular. There was one slight bug I encountered in Firefox about a year ago, where invalid XHTML pages would show a yellow error screen (correct) but with no error message text (this was a regression). That said, I'm pretty sure that the XML-parsing engine, XSLT, and related technologies are here to stay, so XHTML isn't at risk of losing support.
> On the contrary, part of the point of XHTML is you can reference the document type definition (DTD) so everyone would be on the same page.
That was the original intent in XHTML 1.0, 1.1, and 2.0. But anyway, nowadays the DTD is optional and probably unhelpful: https://stackoverflow.com/questions/38427011/does-xhtml5-req...
>> The standard "tag soup" HTML parser has numerous surprising behaviors
> There is nothing surprising here.
So are you an expert at diagnosing HTML parsing and invalid syntax? I prefer not to be one, because I'd like the XML parser to yell at me loudly when I'm wrong, rather than helpfully try to fix my code the wrong way.
> if you dig deeper there are a lot more surprises in XHTML documents
Like what? I'll try to name a few: Namespaces (colons), character entities, CDATA sections, strict comments (rules about the substring "--").
> assuming they are served with the proper MIME type
Yes, I forgot to mention this. My website https://www.nayuki.io/ has been serving application/xhtml+xml for about a decade, and seems to work well with the browsers that the audience uses.
The standard "tag soup" HTML parser has numerous surprising behaviors on invalid or questionable HTML code. Examples:
The <p> element cannot nest, so starting a new one will end the previous one:
Furthermore, trailing junk triggers weirdness: Some elements are defined in the HTML DTD to have implicit start tags: Whereas in XHTML mode, either your code is a parse error, or what you see is what you get. You can nest <p> elements in XHTML, and you can make a <table> contain <td> directly (without the <tbody> and <tr> containers).