HTML5 是否改变了 HTML 评论的标准?

Does HTML5 change the standard for HTML commenting?

最近我发现HTML5中可能有一种新的评论方式。

认为 我注意到我的 IDE 做了一个常规 <!div >注释掉了。所以我对其进行了测试,令我惊讶的是 Chrome 已经注释掉了该标签。它注释掉了标签而不是div的内容,所以我不得不注释掉更近的<!/div>以避免关闭其他div s.

我测试了另一个,似乎一般在任何标签的开头放置一个感叹号,这个符号<,使该标签被注释掉。

这真的是新的吗?这是不好的做法吗?其实很方便,但是实用吗(如果不是新的)?

额外的细节:

虽然语法错误或对这种特定语法的误解是一个很好的理由,但为什么 Chrome 实际上 将它们呈现为完整评论?

代码写成:

<!div displayed> some text here that is still displayed <!/div>

然后渲染为:

<!--div displayed--> some text here that is still displayed <!--/div-->

我认为这不是一个好习惯,因为 <! 代表像 <!DOCTYPE 这样的标记声明。因此你认为它被注释了(好吧......浏览器会尝试解释它)。

即使没有出现,这似乎也不是注释 HTML 代码的正确语法。

HTML5中没有新的评论标准。唯一有效的注释语法仍然是 <!-- -->。来自 section 8.1.6 of W3C HTML5:

Comments must start with the four character sequence U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS, U+002D HYPHEN-MINUS (<!--).

<! 语法起源于 SGML DTD 标记,它不是 HTML5 的一部分。在 HTML5 中,它保留用于注释、CDATA 部分和 DOCTYPE 声明。因此,这种替代方法是否是不好的做法取决于您是否认为使用(或更糟的是,依赖)过时的标记是不好的做法。

Validator.nu 将您的内容称为“虚假评论”。 — 这意味着即使它不是有效评论,它也会被视为评论。这大概是为了与基于 SGML 的 pre-HTML5 向后兼容,并且具有采用 <!FOO> 形式的标记声明,因此我不会称其为新的。他们被视为 like 评论的原因是因为 SGML 标记声明是特殊声明,不打算呈现,但由于它们在 HTML5 中毫无意义(除了上述例外),就 HTML5 DOM 而言,他们 只不过是评论。

section 8.2.4 中的以下步骤得出了这个结论,Chrome 似乎完全符合以下步骤:

  1. 8.2.4.1 Data state:

    Consume the next input character:

    "<" (U+003C)
    Switch to the tag open state.

  2. 8.2.4.8 Tag open state:

    Consume the next input character:

    "!" (U+0021)
    Switch to the markup declaration open state.

  3. 8.2.4.45 Markup declaration open state:

    If the next two characters are both "-" (U+002D) characters, consume those two characters, create a comment token whose data is the empty string, and switch to the comment start state.

    Otherwise, if the next seven characters are an ASCII case-insensitive match for the word "DOCTYPE", then consume those characters and switch to the DOCTYPE state.

    Otherwise, if there is an adjusted current node and it is not an element in the HTML namespace and the next seven characters are a case-sensitive match for the string "[CDATA[" (the five uppercase letters "CDATA" with a U+005B LEFT SQUARE BRACKET character before and after), then consume those characters and switch to the CDATA section state.

    Otherwise, this is a parse error. Switch to the bogus comment state. The next character that is consumed, if any, is the first character that will be in the comment.

    注意它说只有遇到的字符序列是<!--才切换到评论开始状态,否则就是假评论。这反映了上面第 8.1.6 节中所述的内容。

  4. 8.2.4.44 Bogus comment state:

    Consume every character up to and including the first ">" (U+003E) character or the end of the file (EOF), whichever comes first. Emit a comment token whose data is the concatenation of all the characters starting from and including the character that caused the state machine to switch into the bogus comment state, up to and including the character immediately before the last consumed character (i.e. up to the character just before the U+003E or EOF character), but with any U+0000 NULL characters replaced by U+FFFD REPLACEMENT CHARACTER characters. (If the comment was started by the end of the file (EOF), the token is empty. Similarly, the token is empty if it was generated by the string "<!>".)

    用简单的英语来说,这会将 <!div displayed> 变成 <!--div displayed-->,将 <!/div> 变成 <!--/div-->,正如问题中所描述的那样。

最后一点,您可能期望其他 HTML5 兼容的解析器的行为与 Chrome.

相同