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

标签 html comments

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

而不是典型的 <!-- -->多行评论 我读过,我 我注意到我的 IDE 做了一个常规的 <!div >注释掉了。所以我对其进行了测试,令我惊讶的是 Chrome 已经注释掉了该标签。它注释掉了标签而不是div 的内容,所以我不得不注释掉越近的<!/div>。以避免关闭其他 div。

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

这真的是新的吗?这是不好的做法吗?它实际上非常方便,但是否实用(如果不是新的)?

额外的细节:

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

代码写成:

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

然后呈现为:

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

最佳答案

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 的 HTML5 之前的版本向后兼容,并且具有采用 <!FOO> 形式的标记声明。 ,所以我不会称之为新的。它们被视为 注释的原因是因为 SGML 标记声明是不打算呈现的特殊声明,但由于它们在 HTML5 中毫无意义(除了上述异常(exception)),就 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.

    注意它说只有当遇到的字符序列是<!--时才切换到注释开始状态。 ,否则就是虚假评论。这反射(reflect)了上面第 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 相同。

关于html - HTML5 是否改变了 HTML 评论的标准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29405858/

相关文章:

html - 语义 ui react ,是否可以在两个部分上拆分选项卡?

c - 评论总是在预处理器之前处理吗?

python - 如何在 Python 代码中包装和对齐注释?

javascript - JQuery 对象 promise 的目的是什么?

c++ - 在 Visual Studio Enterprise 2017 中禁用自动生成的评论

html - 搜索引擎会读取 &lt;!-- --> 并且对搜索结果有害吗

javascript - Rails 评论不使用 ajax 呈现

html - 如何将 CSS 应用于 HTML 文本输入?

javascript - jQuery:我的下拉菜单的 getPos

jquery - 第二节元素移动到第二行