4 ms·
In XHTML (XML) mode, comments are treated as actual comments. In the following working code: <script> alert("Hello <!-- Comment 0 -->world"); <!--
by nayuki 5y ago
In XHTML (XML) mode, comments are treated as actual comments. In the following working code:
<script>
alert("Hello <!-- Comment 0 -->world");
<!-- Comment 1
alert("Bonjour le monde");
</script>
Comment 0 gets removed by the XML parser so it doesn't go into the alert string. Comment 1 is seen by the JavaScript parser and gets removed at that stage.
- chrismorgan 5y agoDemonstration of this, using a data: URL (paste it in the address bar): data:application/xhtml+xml,<script xmlns="http://www.w3.org/1999/xhtml">%0Aalert("Hello <!-- Comment 0 -->world");%0A<!-- Comment 1%0Aalert("Bonjour le monde");%0A</script> (Observe also how what I’ve written here nets you a document that lacks html, head and body tags—HTML syntax fills those in through the magic of optional start and end tags, but XML syntax takes what it’s given and can be used to do things like nesting hyperlinks or putting a heading inside a paragraph. The HTML and XML syntaxes for HTML are actually mutually incompatible.)
- nayuki 5y agoVery nice use of media types and data URIs! Indeed, the root of your document is <script>, which is rather funny. Of course HTML and XML syntaxes are incompatible, but there is a reasonable polyglot subset that is compatible with both. Other common things that HTML syntax won't let you do: <p><p></p></p>, <table><tr><td> without an implicit <tbody>. Why I know this obscure corner of the HTML standard is because I've been running my website on XHTML mode for more than a decade continuously. https://www.nayuki.io/page/practical-guide-to-xhtml https://www.nayuki.io/page/practical-guide-to-xhtml
- chrismorgan 5y agohttps://www.w3.org/TR/html-polyglot/ https://www.w3.org/TR/html-polyglot/ is good reading on this topic too. A fun thing that I learned recently is that `table > tr` is actually valid, the tbody is genuinely optional in the spec, even though the HTML syntax injects it.
- TacticalCoder 5y ago> tbody is genuinely optional in the spec IIRC optional in the HTML spec only. Not in the XHMTL one and not in the polyglot syntax.
- chrismorgan 5y agoThe XHTML spec is obsolete and irrelevant. It’s replaced by the HTML spec using XML syntax. (W3C classes it as a Superseded Recommendation.) But tbody was optional even in XHTML 1.0 Strict; quoting https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd, <!ELEMENT table (caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))> I’m pretty sure that this will be the reason it’s optional in the HTML spec, since there’d be no point in it being optional otherwise, since it’s not attainable in the HTML syntax.
- aidos 5y agoBack in the days when we used to nest tables 70 deep for layouts I don’t recall anyone using tbody - was it added later?
- merb 5y agotbody is old, but was not widely used before html5 or modern spa's that required them.
- chrismorgan 5y agothead/tfoot/tbody were introduced in HTML 4, with tbody mandatory (that is, `table > tr` was now invalid), but implied (that is, its start and end tags were both optional, so that <table><tr>…</tr></table> would be parsed the same as <table><tbody><tr>…</tr></tbody></table>). Source: https://www.w3.org/TR/html4/struct/tables.html#h-11.2 https://www.w3.org/TR/html4/struct/tables.html#h-11.2: <!ELEMENT TABLE - - (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)> <!ELEMENT TBODY O O (TR)+ -- table body -->
- Semaphor 5y agoAh, the times when I was always trying to serve my site in the strictest way possible. I religiously followed everything Anne van Kesteren [0] wrote, and used his code-snippet [1] to serve application/xhtml+xml to browsers that supported it. He was my same-aged teenage hero :) [0]: https://annevankesteren.nl/ https://annevankesteren.nl/ [1]: https://annevankesteren.nl/2003/09/send-applicationxhtmlxml https://annevankesteren.nl/2003/09/send-applicationxhtmlxml
- TacticalCoder 5y agoYup that's not valid polyglot HTML5/XHMTL code. In HTML < (and &) aren't special (except at the end tag). But in XHTML < is treated as a tag. Here's the link to that part of the spec for anyone interested: https://www.w3.org/TR/html-polyglot/#raw-text-elements https://www.w3.org/TR/html-polyglot/#raw-text-elements