If I try to put something like this into a question or answer

<!-- <br>s are important here -->

the first > after the "br" is matched to the first < before the "!--", resulting in this being displayed:

s are important here -->

This happens in both previews and actual posts. I'd say it's a bug. HTML comments shouldn't be treated as regular HTML tags; <!-- should only be matched to -->.

  • 1
    Braks it all up? – Josh Darnell Jan 26 '12 at 20:27
  • @jadarnel27: edited – Martin. Jan 26 '12 at 20:31
  • 5
    So what you're saying is that > is ending your HTML comment even though only --> should end your HTML comment, as explained here? In other words, <!-- this is a comment --> and <!-- this is a comment > are being rendered identically (hidden) even though the latter isn't a valid HTML comment? – Chris Frederick Jan 26 '12 at 20:34
  • 2
    @Chris: hmm, I think yes, exactly. Look here, you'll see that html isn't broken by > there – Martin. Jan 26 '12 at 20:36
  • @Chris: no, actually I need to use >--> in order to break it (to terminate the comment). Look at my last edit – Martin. Jan 26 '12 at 20:38
  • @Martin Could you edit that back into your question? It sounds like a lot of folks are getting confused about the difference between HTML comments, Stack Overflow comments, and HTML comments in Stack Overflow comments. You're talking about HTML comments in Stack Overflow questions and answers. – Chris Frederick Jan 26 '12 at 20:39
  • @Chris: Feel free to edit it back, because I'm not sure what should I edit back – Martin. Jan 26 '12 at 20:40
  • @MatthewRead: Glad you got it now :) – Martin. Jan 26 '12 at 20:46
  • @Martin Please confirm that I understood your question correctly. – Chris Frederick Jan 26 '12 at 20:48
  • @Martin: This is not a bug. Please do not add that tag in. – animuson ModStaff Jan 26 '12 at 20:48
  • 3
    @animuson: yes, that's a bug. HTML comment is being malformed by adding > – Martin. Jan 26 '12 at 20:50
  • 2
    @AdamRackis: It doesn't matter. I didn't say that team should fix it. It is a bug, so I reported it. – Martin. Jan 26 '12 at 20:54
  • 1
    Stop adding the bug tag, this isn't one. HTML comments are not among the allowed HTML tags and get removed. Conveniently, this looks like they were treated as comments, but they're not. – balpha Jan 26 '12 at 21:01
  • @balpha: I didn't add one for 14 minutes, because I wanted to wait for you – Martin. Jan 26 '12 at 21:02
  • Can we have a better markdown-ish way to specify language formatting then? <!-- language: lang-foo --> is such a pain to type. How about [lang:foo]? – sarnold Jan 26 '12 at 22:49

It looks like this is :

It should be mentioned that while a > is a valid character in a comment from both the W3C specification and the above Regex, it's not going to work correctly in our Markdown, because the tag sanitizer will eat it:

<!-- love > hate -->

turns into

hate -->

Personally I would not expect the tag sanitizer to take precedence over this, but it's not my call.


The short answer, according to balpha ♦, appears to be that Stack Exchange does not support HTML comments in Markdown: it only supports a very strict subset of HTML. You may be able to put HTML comments in your posts, and they may be hidden in the rendered output, but they're not really guaranteed to work.

  • So the fact that HTML comments produce invisible content in an SO post is merely a by-product of the fact that it's being parsed as an (invalid) HTML tag, and not an HTML comment? – user159834 Jan 26 '12 at 21:12
  • @Madmartigan That's the way I interpret it. – Chris Frederick Jan 26 '12 at 21:13
  • 1
    So then <!-- this --> gets interpreted as a "!-- this --" HTML tag, and it's invalid so it's just not displayed? Just like if you used <hello this is a "comment"> to hide text? Looks like the fact that HTML comments work in the first place is the only "bug" then... – user159834 Jan 26 '12 at 21:14
  • @Mad Except for a very few occasions where they're (ab)used as metadata containers (e.g. specifying the language for the syntax highlighter), yes, it's a byproduct of being considered invalid. – balpha Jan 26 '12 at 21:16
  • 1
    I've always used this <!-- --> to break up a code block into two parts (same language in both blocks), I guess I could have used <anything>. I suppose this is the source of all the confusion then. – user159834 Jan 26 '12 at 21:18

You cannot use HTML comments anywhere here on Stack Exchange. Comments such as <!-- filler --> are sometimes used in answers and questions to bypass character limits, but get removed in the final output. Only some HTML is allowed in posts, the rest all gets filtered out.

If you think this should be allowed, you need to fill out a detailed reason why you think you should be able to do this, file a feature request, and let the community downvote it into oblivion.

As per your example, it is finding the <!-- <br> first because the Markdown system doesn't care about your HTML comment. They're not allowed, so it doesn't search for the closing -->. It just sees that <!-- <br> looks like an HTML element, it's not allowed, and thus it gets removed.

Actual HTML source:

<pre><code>The first one &lt;!-- this is the comment --&gt; the second one &lt;!-- this is the second comment &gt;--&gt;
<p>Result: The first one  the second one --></p>

Notice the lack of actual, physical HTML comments in the source of the document.

  • 3
    Look at the edited post. I actually don't want to show the code for comment, I want to actually make it a HTML comment – Martin. Jan 26 '12 at 20:30
  • @MatthewRead: I know about &lt;, &gt;, but I actually want to hide the comment from the normal reader – Martin. Jan 26 '12 at 20:31
  • 1
    Where do you get the idea that HTML comments aren't allowed in posts (as actual, invisible HTML comments)? They are. I use them quite frequently to break up two blocks of code or to leave a note to would-be editors. – user159834 Jan 26 '12 at 20:46
  • 1
    @Madmartigan: Look at the source. Do you see any actual comments in the HTML? They're in the post when you click edit, but they don't show up in the actual output. – animuson ModStaff Jan 26 '12 at 20:47
  • You said "the Markdown system doesn't care about your HTML comment. They're not allowed" – user159834 Jan 26 '12 at 20:48
  • 1
    @Madmartigan: They're not. They get removed. That seems very not-allowed-ish to me. – animuson ModStaff Jan 26 '12 at 20:49
  • They don't get removed, what do you mean? We're not talking about the actual source code of the page, that's irrelevant. Guys: Are we talking about putting HTML comments in "Comments on a post, like right here" or in the actual post body? Looks very much to me like OP is asking about how this works in an actual SO post. – user159834 Jan 26 '12 at 20:49
  • 4
    @AdamRackis If I understand Martin correctly, he's talking about putting HTML comments in questions and answers. – Chris Frederick Jan 26 '12 at 20:50
  • 2
    @Chris: yeah, that's it – Martin. Jan 26 '12 at 20:52
  • 3
    I have no idea why this gets downvoted into oblivion. This is the correct answer. – balpha Jan 26 '12 at 20:57
  • 1
    @balpha - I'm glad I'm not the only one thinking that. This is a bug only by the most narrow, meaningless criteria. Comments are not displayed. Putting certain things in comments breaks things? Then don't do that. There's no use case for you to ever need to do that. – Adam Rackis Jan 26 '12 at 20:58
  • @Martin - because the inability to enter something—which is already not allowed—in certain formats isn't really a bug. – Adam Rackis Jan 26 '12 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .