有没有HTML评论的新标准? [英] Is there a new standard for HTML commenting?

查看:88
本文介绍了有没有HTML评论的新标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发现,可能有一种新的HTML5注释方式。



而不是典型的<! - - > 多行注释我已阅读,我认为我注意到我的IDE制作了一个普通的<!div> 注释掉了。所以我测试了一下,令我惊讶的是Chrome已经注释掉了这个标签。它仅 注释掉了标签,而不是div的内容,所以我必须注释更接近<!/ div> 以避免关闭其他div。



我测试了另一个div,通常会在任何标签的开头前放一个感叹号,这个符号< ,使该标记注释掉。



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

编辑额外的详细信息:
尽管语法错误或对这种特定语法的错误解释是一个很好的理由, >实际上
将它们呈现为完整评论?



代码写成

 <!div显示>这里仍然显示一些文字<!/ div> 

然后呈现为

 <! -  div显示 - >这里仍然显示一些文字<! -  / div  - > 


解决方案

HTML5中没有新的评论标准。唯一有效的注释语法仍然是<! - - > 。从 W3C HTML5第8.1.6节


注释必须以四个字符序列U + 003C LESS-SIGN 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> 形式的标记声明,所以我不会调用这个新的。他们被视为类似注释的原因是因为SGML标记声明是特殊的声明,并不是要呈现,但由于它们在HTML5中是没有意义的(除了上述例外),就HTML5 DOM而言关注他们 不过是评论。



第8.2.4节得出这个结论,Chrome看起来跟在这封信上:


  1. 8.2.4.1数据状态


    消费下一个输入字符:

    < (U + 003C)

    切换到标签打开状态。 a href =http://www.w3.org/TR/html5/syntax.html#tag-open-state> 8.2.4.8标记打开状态:


    消费下一个输入字符:

    ! (U + 0021)

    切换到标记声明打开状态。



  2. 8.2.4.45标记声明打开状态


    如果接下来的两个字符都是 - (U + 002D)个字符,则使用这两个字符,创建一个注释标记,其数据为空字符串,并切换到注释开始状态。



    否则,如果接下来的七个字符是单词DOCTYPE的ASCII不区分大小写匹配,然后消耗这些字符并切换到DOCTYPE状态。



    否则,如果存在调整的当前节点,并且它不是HTML命名空间中的元素,并且接下来的七个字符是字符串[CDATA [(前后有U + 005B LEFT SQUARE BRACKET字符的五个大写字母CDATA)的区分大小写的匹配),然后消耗这些字符并切换到CDATA部分的状态。

    否则,这是一个解析错误。切换到虚假注释状态。消费的下一个字符(如果有的话)是注释中的第一个字符。


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

  3. 8.2.4.44虚假评论状态


    消费每个角色,包括第一个>(U + 003E)字符或文件结尾(EOF),以先到者为准。发出一个注释标记,其数据是所有字符的连接,包括导致状态机切换到假注释状态的字符,包括紧接在最后一个消耗字符之前的字符(即直到字符就在U + 003E或EOF字符之前),但是用U + FFFD REPLACEMENT CHARACTER字符替换任何U + 0000 NULL字符。 (如果注释是由文件末尾开始的(EOF),则标记为空。同样,如果标记是由字符串<!>

    用简单的英文表示<!div显示> 转换为<! - div显示 - > <!/ div> <! - / div - > ,完全如问题中所述
    $ b

    最后一点,您可能会期望其他符合HTML5的解析器的行为与Chrome相同。


    Recently I found that there is, possibly, a new way of commenting in HTML5.

    Instead of the typical <!-- --> multi-line commenting I've read about, I thought I noticed that my IDE made a regular <!div > commented out. So I tested it out, and to my surprise Chrome had commented out that tag. It only commented out the tag and not the contents of the div, so I had to comment out the closer <!/div> to avoid closing other divs.

    I tested another and it appears that generally putting an exclamation marker in front of the opening of any tag, this symbol <, makes that tag commented out.

    Is this actually new? Is it bad practice? It is actually very convenient, but is it practical yet(if not new)?

    Edit extra details: Although a syntax error or misinterpretations of this particular syntax is a good reason, how come Chrome actually renders them as full comments?

    The code is written as:

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

    And then it is rendered as:

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

    解决方案

    There is no new standard for comments in HTML5. The only valid comment syntax is still <!-- -->. From 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 (<!--).

    The <! syntax originates in SGML DTD markup, which is not part of HTML5. In HTML5, it is reserved for comments, CDATA sections, and the DOCTYPE declaration. Therefore whether this alternative is bad practice depends on whether you consider the use of (or worse, the dependence on) obsolete markup to be bad practice.

    Validator.nu calls what you have a "Bogus comment." — which means that it's treated like a comment even though it's not a valid comment. This is presumably for backward compatibility with pre-HTML5, which was SGML-based, and had markup declarations that took the form <!FOO>, so I wouldn't call this new. The reason they're treated like comments is because SGML markup declarations were special declarations not meant to be rendered, but since they are meaningless in HTML5 (with the above exceptions), as far as the HTML5 DOM is concerned they are nothing more than comments.

    The following steps within section 8.2.4 lead to this conclusion, which Chrome appears to be following to the letter:

    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.

      Notice that it says to switch to the comment start state only if the sequence of characters encountered is <!--, otherwise it's a bogus comment. This reflects what is stated in section 8.1.6 above.

    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 "<!>".)

      In plain English, this turns <!div displayed> into <!--div displayed--> and <!/div> into <!--/div-->, exactly as described in the question.

    On a final note, you can probably expect other HTML5-compliant parsers to behave the same as Chrome.

    这篇关于有没有HTML评论的新标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆