Firefox Comment Rendering, It's Not A Bug! But We'll Fix It

  

Ran into an issue recently with how Firefox renders HTML comments.  Everyone here knows what comments look like right?


<!-- This is a comment. -->
<!--
        They can also span
        multiple lines.
-->

Pretty straight forward. And you would think it's easy to identify when rendering. Once you see the "<!--" then ignore everything until you see "-->".  Except Firefox doesn't do that exactly.  It turns out if you have 2 dashes or hyphens contained in the comment, Firefox will prematurely end the comment.  And probably break your page.  Example:


<!-- This is a fine looking comment--Until you get here. Now you're screwed! -->

The above comment Will break in all versions of Firefox up to the current stable version of 3.5.  Take a look (You are using Firefox right?)  This doesn't happen in any other modern browser including Internet Explorer.  But okay, so there's a rendering bug in Firefox.  It happens right?  No big deal.  I'll just fulfill my responsibility and go on over to the bug list and make sure it's there.  Sure enough, I found the bug, and that's when things got interesting.

Apparently people have been arguing about this bug for 6 years!  And it still isn't fixed!  Because, like all good stories about programming minutia, some really smart guys don't agree that it's a bug.  HTML is a tag-based language originally based on SGML, and apparently the spec for HTML relies on SGML to specify how comments should be handled.  Here's the choice excerpt from the spec.

White space is not permitted between the markup declaration open delimiter("<!") and the comment open delimiter ("--"), but is permitted between the comment close delimiter ("--") and the markup declaration close delimiter (">"). A common error is to include a string of hyphens ("---") within a comment. Authors should avoid putting two or more adjacent hyphens inside comments.

You see the issue here is that the end delimiter of a comment in SGML isn't "-->", it's just "--". After the double hyphen your text starts to mean something again. And you can include as much text as you want after that. But according to HTML you're still inside a tag, because it hasn't seen a closing ">" yet. Hence the weirdness. In fact it's even more complicated than that. Check out this more in-depth breakdown. Which explains why one set of double hyphens breaks comments while a second set will fix them. <!-- So something -- like this -- would work just fine. -->. Remember, if you're going to include double hyphens inside your HTML comments, make sure they come in multiples of 2.

Oh... well... fine then.  That's kind of stupid, but okay.  Bug closed as invalid.  And it was.  Until someone else realized it was stupid.  In fact, several people did.  Which is why it was successfully removed as a requirement for passing the Acid 2 compliance test.  And finally it was fixed where it should've originally been fixed, in the spec.  HTML5 addresses this issue.   Hooray!   Now the official end delimiter for comments is "-->".   Except you're still not supposed to use double hyphens... whatever.

So now the smart guys at Mozilla are listening to reason and plan to fix the bug in the next version of FF.  But only after the HTML spec supports it.  That's the most interesting thing to come out of all of this in my opinion.  It raises a few questions.  Is it good to hang on doggedly to a spec written years ago, when clearly it does more harm than good in modern practice?  And more importantly, when is it time to stop being pedantic and fix the damn bug already?  Apparently you can give it at least 6 years.

Update: Apparently the HTML Tidy validator plugin will warn you of this. Don't know how long this has been around. But it's handy at least.

13 Responses to this Article

  • Added by ATtectTrourge on

    What Word Press template do you use?

  • Added by John on

    Just ran into this on a pretty big site this morning, and thought I was losing it. Thanks for writing this up!

  • Added by Bryant Stevens on

    I didn't understand the concluding part of your article, could you please explain it more?

  • Added by Marco Rogers on

    What is it you don't understand?

  • Added by genxbong on

    How you find ideas for articles, I am always lack of new ideas for articles. Some tips would be great

  • Added by Enronaaccisse on

    Hi...... post good :)

  • Added by Vitaly Stockman on

    So silly. Of course it should be "-->" that closes the comment. The code could easily have double dash inside, which needs to be commented just like anything else. It's so easy for users who fill the content to enter "--" (decoration, weird link etc.), but nearly impossible to enter come up with the "-->".

    Not very practical these "smart people" who are arguing it should be otherwise.

  • Added by Chris martin on

    It's a very useful article but how can you fix the comments issue for 250 files just by replacing something in the code? Dont have time to fix it one by one, any ideas?? Thnks

  • Added by jordan on

    Just spent 2 days scratching my head wondering why one particular page was causing me float problems in firefox whilst the several other pages i have using the same external style sheet all displayed fine. The reason was this feckin comment bug/not technically a bug thing. Ah well, lesson learned!

  • Added by custom baseball jerseys on

    i do agree with you!

  • Added by watch Rio online on

    Hey,

    Thanks for sharing this link - but unfortunately it seems to be down? Does anybody here at marcorogers.com have a mirror or another source?


    Cheers,
    Charlie

  • Added by Keyloggers download on

    It can be discussed

    infinitely..

  • Added by Todays top news on

    Hi there just wanted to give you a quick heads up. The words in your article seem to be running off the screen in Internet explorer. I'm not sure if this is a format issue or something to do with browser compatibility but I thought I'd post to let you know. The layout look great though! Hope you get the issue resolved soon. Many thanks

Add Your Comment