double hyphen in script makes firefox render stran

2020-04-27 08:12发布

问题:

<!-- <script type="text/javascript">/*<![CDATA[*/  c-- ;//]]></script> -->

When I have the above line in the <head> section of a plain html page, Firefox 3.5.5 renders the trailing --> as text. If I change c-- to c- it doesn't. Any ideas what's going on here? I getting an artifact on my pages with this due to a very large script that's been crunched. I can change the statement to c-=1 and avoid the problem for now but.... I'd like to know what bit/byte is biting my a$$.

回答1:

This is due to Firefox implementing SGML (on which HTML was based) comments strictly. This will only occur when the document is loaded in standards mode (i.e. there is a DOCTYPE).

The first <! starts a comment. The first -- enters a section in which > characters are allowed. The second -- (in your script) leaves the section in which > characters are allowed. The > at the end of </script> then ends the comment. The following --> is therefore no longer part of the the comment and gets rendered as text.

See http://www.howtocreate.co.uk/SGMLComments.html for a comprehensive guide to the issue.

Its also worth noting that the HTML 4 Specification says that 'authors should avoid putting two or more adjacent hyphens inside comments' and the HTML 5 Specification says comments must not 'contain two consecutive U+002D HYPHEN-MINUS characters (--)'.

The solution, as you've found, is to not include -- in the middle of a comment.



回答2:

Technically you are not allowed to have double hyphen in a comment in HTML (or XML). So even if browsers "allow" if it is not valid and should fail an HTML validator.

See Comment section of HTML 4 Specification



回答3:

I can't replicate this. Doesn't show up on 3.0.1.