You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HTML comment inside inline raw HTML creates malformed output #1643
Originally mis-filed against Zensical as zensical/zensical#981. @squidfunkgraciously deduced this project as the root cause. The repro repo illustrates via Zensical, but let me know if I should provide a more compact version that doesn't rely on that as the vehicle.
A picture is inherently an inline element, so Python Markdown treats it that way. Python Markdown also treats HTML comments as their own block. So the comment breaks your content into two paragraphs.
A picture is inherently an inline element, so Python Markdown treats it that way. Python Markdown also treats HTML comments as their own block. So the comment breaks your content into two paragraphs.
Sorry if I'm confused. Does that mean my example is invalid HTML? Apologies if that's the case.
It's not necessarily invalid, but this happens if you try to do this with any inline element in Python Markdown.
If <picture> was treated as a block, the Python Markdown parser would handle it differently, but it is treated as inline. This would happen if you did this with any inline element in Python Markdown.
It should be noted that browsers treat <picture> as inline by default, so it being inline is not a bug.
this happens if you try to do this with any inline element in Python Markdown.
The key is the above quote. This is not unique to <picture> but all inline elements. Therefore, a fix which only addresses <picture> is not the correct fix (and that is why I closed #1645). If we were to change anything it would be to recognize when a raw HTML comment is within a raw inline HTML element and not treat that comment as block-level. Of course, in all other contexts, comments should continue to be treated as block-level. Whether we should make that change however, could depend on the rules and how the reference implementation behaves. I will need to do some investigating.
Python-Markdown's behavior is currently as follows:
Input
<span><!-- comment --></span>
Output
<p><span><!-- comment --></span></p>
This first example behaves as expected. But then when a comment is not on a line by itself, Markdown has always treated it differently. This matches the reference implementation and does not need to change.
Input
<span>
<!-- comment -->
</span>
Output
<p><span></p><!-- comment --><p></span></p>
This is the issue in its most basic form. Interestingly, the reference implementation (markdown.pl) gets this correct. We should be outputting the same:
Expected Output
<p><span><!-- comment --></span></p>
This is confirmed as a bug which needs to be fixed.
For completeness, I also checked this input:
Input
<span>
<!-- comment -->
</span>
and the reference implementation outputs the following:
Output of markdown.pl
<p><span></p><!-- comment --><p></span></p>
I would be okay with calling that a bug in the reference implementation. We should match the behavior of the previous example (presumably while preserving whitespace).
changed the title [-]HTML comment inside `<picture>...</picture>` in Markdown creates malformed HTML[/-][+]HTML comment inside inline raw HTML creates malformed output[/+]on Sep 29, 2026
Thinking further about the example with blank lines, I think we need to follow the refernce implementation. raw inline HTML does not change how blocks are parsed. Specifically, consider this:
input
<span>
</span>
That is (correctly) rendered as
<p><span></p><p></span></p>
Therefore, we shouldn't special case it when there is a comment inserted in the middle.
Conversely, this:
<span>
</span>
renders as
<p><span></span></p>
So inserting a comment in the middle should not change that.
Okay, this sounds like it requires a change in our HTML parser, then. It should take context as to whether a comment is its own block or not, I assume. I haven't looked at the code yet. Just thinking out loud.
Activity
facelessuser commentedon Sep 28, 2026
A picture is inherently an inline element, so Python Markdown treats it that way. Python Markdown also treats HTML comments as their own block. So the comment breaks your content into two paragraphs.
posita commentedon Sep 29, 2026
Sorry if I'm confused. Does that mean my example is invalid HTML? Apologies if that's the case.
facelessuser commentedon Sep 29, 2026
It's not necessarily invalid, but this happens if you try to do this with any inline element in Python Markdown.
If
<picture>was treated as a block, the Python Markdown parser would handle it differently, but it is treated as inline. This would happen if you did this with any inline element in Python Markdown.It should be noted that browsers treat
<picture>as inline by default, so it being inline is not a bug.waylan commentedon Sep 29, 2026
The key is the above quote. This is not unique to
<picture>but all inline elements. Therefore, a fix which only addresses<picture>is not the correct fix (and that is why I closed #1645). If we were to change anything it would be to recognize when a raw HTML comment is within a raw inline HTML element and not treat that comment as block-level. Of course, in all other contexts, comments should continue to be treated as block-level. Whether we should make that change however, could depend on the rules and how the reference implementation behaves. I will need to do some investigating.waylan commentedon Sep 29, 2026
Python-Markdown's behavior is currently as follows:
Input
Output
This first example behaves as expected. But then when a comment is not on a line by itself, Markdown has always treated it differently. This matches the reference implementation and does not need to change.
Input
Output
This is the issue in its most basic form. Interestingly, the reference implementation (
markdown.pl) gets this correct. We should be outputting the same:Expected Output
This is confirmed as a bug which needs to be fixed.
For completeness, I also checked this input:
Input
and the reference implementation outputs the following:
Output of markdown.pl
I would be okay with calling that a bug in the reference implementation. We should match the behavior of the previous example (presumably while preserving whitespace).Expected Output[-]HTML comment inside `<picture>...</picture>` in Markdown creates malformed HTML[/-][+]HTML comment inside inline raw HTML creates malformed output[/+]facelessuser commentedon Sep 29, 2026
Based on how block handling is done throughout Python Markdown, I'm not sure if we can reasonably handle it...well, not without some effort, at least.
I think this is certainly possible:
What about things like below? How does the reference implementation handle this?
waylan commentedon Sep 29, 2026
Thinking further about the example with blank lines, I think we need to follow the refernce implementation. raw inline HTML does not change how blocks are parsed. Specifically, consider this:
input
That is (correctly) rendered as
Therefore, we shouldn't special case it when there is a comment inserted in the middle.
Conversely, this:
renders as
So inserting a comment in the middle should not change that.
A few more examples.
Input
Output (correct)
Input
Output (incorrect)
Expected Output (what
markdown.ploutputs)facelessuser commentedon Sep 29, 2026
Okay, this sounds like it requires a change in our HTML parser, then. It should take context as to whether a comment is its own block or not, I assume. I haven't looked at the code yet. Just thinking out loud.
Keep a comment inside raw inline HTML in the same paragraph