Skip to content

Keep a comment inside raw inline HTML in the same paragraph - #1648

Open
wolfgang-aura wants to merge 2 commits into
Python-Markdown:masterfrom
wolfgang-aura:wolfgang-aura/fix-1643-inline-comment
Open

wolfgang-aura wants to merge 2 commits into
Python-Markdown:masterfrom
wolfgang-aura:wolfgang-aura/fix-1643-inline-comment

Conversation

@wolfgang-aura

Copy link
Copy Markdown

Description

Fixes #1643.

A raw HTML comment on its own line inside an inline element was always treated as a block, so this input:

<span>
<!-- comment -->
</span>

rendered as <p><span></p>\n<!-- comment -->\n<p></span></p>. This change makes it render as <p><span>\n<!-- comment -->\n</span></p>, which matches markdown.pl as described in the issue.

HTMLExtractor now keeps a stack of raw inline tags that are open in the current paragraph. handle_comment treats the comment as inline only when one of those tags is closed later in the same paragraph and the comment has no blank line in it. A blank line in the data clears the stack. Everything else keeps the current block behaviour:

  • comments outside inline HTML;
  • a comment after an inline tag that is already closed;
  • a comment after a tag that is never closed;
  • a comment after a void tag such as <img>;
  • the blank-line case from the issue, which still splits, following the conclusion in the thread.

md_in_html gets the same two hook calls so both parsers behave the same. Nothing special-cases <picture>.

Tests: eight new cases in tests/test_syntax/blocks/test_html_blocks.py. The md_in_html test class inherits them, so they run under both parsers. The two own-line cases fail without the change, and the other six pin behaviour that must stay as it is. The full suite passes (1114 tests, 52 skipped), and flake8 is clean on the changed files. A changelog entry is under [Unreleased].

Not covered: an inline element that contains a block-level element and then a comment, such as <span>\n<div>x</div>\n<!-- c -->\n</span>, still splits as it does today.

AI Assistance Disclosure

  • No AI tools were used in preparing this PR.
  • If AI tools were used, I have disclosed which ones, and fully reviewed and verified their output.

The change and tests were written by Claude Code (claude-sonnet-5-5) and reviewed by a second Claude Code run. The edge cases above were compared with the base commit's output, and the tests were run on Windows with Python 3.14.

Checklist

@waylan

waylan commented Oct 2, 2026

Copy link
Copy Markdown
Member

You have a good set of tests here. However, I would like one more added. There should be no change in behavior for this one but I would like to ensure it stays that way.

<span><!-- comment -->
</span>

@wolfgang-aura

Copy link
Copy Markdown
Author

Added test_comment_in_inline_html_same_line_as_start_tag for <span><!-- comment --> followed by </span> on the next line. It renders the same with and without this change, so it pins the behavior you wanted kept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML comment inside inline raw HTML creates malformed output

2 participants