Are nested HTML comments possible?

前端 未结 10 1393
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 20:04

as per the title; is it possible to have nested comments in valid HTML? see the example below...

some text

Truth: I am too lazy to tell you how many comments are represented by the above tag pollution, but it's at least 10.

I got less lazy: This so-called "comment" actually consists of 10 comments, three words outside any comment (i.e., just bad SGML), and the beginning of a comment that is not terminated. It's a real mess:

--1----2----3----4----5--
HEADER BEGINS HERE
--6----7----8----9----10-- -->

Of course, it's not quite that simple, due to differences in how each browser chooses to interpret the spec.

Here's an excellent article that explains it:

    http://weblog.200ok.com.au/2008/01/dashing-into-trouble-why-html-comments.html

Long answer: Why we get it wrong

Most of us who grew up with HTML (without delving into the SGML that underlies it)) have come to believe that the string ends a comment.

Actually, and > delimit an SGML declaration within your HTML document, such as the DOCTYPE declaration we've all seen at the top of our pages. Within an SGML declaration, comments are delimited by double-dashes. Thus, the HTML comment

which most of us would believe is parsed like this is actually parsed like this:
-- this is a comment -->. It is an SGML declaration that is empty except for a comment.

Because HTML is a form of SGML, this "comment-within-a-declaration" functions as an HTML comment.

Out of interest, here's a chunk of pure SGML that shows comments functioning as they were intended in SGML: this attribute list definition contains a comment on each line:

提交回复
热议问题