13

Standards mode HTML forbids double hyphens in comments.

<!-- This is a perfectly valid comment - I think. -->
<!-- Invalid comment -- definitely. -->

There are three additional restrictions, exemplified by these invalid examples:

<!--> invalid -->
<!---> invalid -->
<!-- invalid --->

Why bother specifying all these little edge cases? In particular, why forbid double hyphens?

aaaidan
  • 7,093
  • 8
  • 66
  • 102
  • 1
    Because this is a valid comment ` – Cheery Oct 28 '14 at 02:51
  • 3
    This comes from XML, I think. The rational, if I remember correctly, was to allow it to be parsed with exactly 1 character of lookahead past the current one. Edit: I don't think I remember correctly. Not sure where I got that from :-) – Cameron Oct 28 '14 at 02:51
  • 3
    Interestingly `

    This is a test...of the emergency broadcast system.

    ` renders as *This is a test...of the emergency broadcast system.* just fine in the latest versions of Chrome and Firefox.
    – J0e3gan Oct 28 '14 at 03:04
  • 1
    @J0e3gan all kind of invalid markup will render correctly... doesn't make it valid. – MikeSmithDev Oct 28 '14 at 03:05
  • Invalidity is usually discovered in practice. Ironically, it actually works in practice. Regardless, a [simple web search](http://www.bing.com/search?q=double+hyphens+in+html+comment&qs=n&form=QBRE&pq=double+hyphens+in+html+comment) turns up the answer...on SO. – J0e3gan Oct 28 '14 at 03:07

1 Answers1

14

[Definition: Comments may appear anywhere in a document outside other markup; in addition, they may appear within the document type declaration at places allowed by the grammar. They are not part of the document's character data; an XML processor may, but need not, make it possible for an application to retrieve the text of comments. For compatibility, the string " -- " (double-hyphen) must not occur within comments.] Parameter entity references must not be recognized within comments.

The grammar does not allow a comment ending in --->

It seems to be a feature of XML included solely to ensure that XML remains compatible with SGML http://www.w3.org/TR/REC-xml/#sec-comments

Bobz
  • 2,394
  • 1
  • 19
  • 20
  • 2
    This is a good answer. HTML5 needed to try be a common subset of XML comment syntax, SGML comment syntax, and the comment syntax that browsers supported, all 3 of which were different. Of the 3, SGML was probably least important as browsers never supported SGML comment syntax properly (though validators did). But the resulting product should indeed work in an SGML parser. – thomasrutter Jun 29 '16 at 10:21