Skip to content

Unclosed HTML comments not being escaped with safe_mode='escape' #563

@michaelkedar

Description

@michaelkedar

Describe the bug
Converting a string containing an unclosed <!-- tag with safe_mode='escape' does not replace < with &lt;. This causes the whole html document to be commented out when attempting to render.

There's also something odd about how markdown is being rendered inside of comments

To Reproduce

>>> markdown2.markdown('*foo* <!-- *bar* -->', safe_mode='escape') # with closing tag, OK
'<p><em>foo</em> &lt;!-- *bar* --&gt;</p>\n'
>>> markdown2.markdown('*foo* <!-- *bar*', safe_mode='escape') # without closing tag, bad
'<p><em>foo</em> <!-- <em>bar</em></p>\n'

Expected behavior
<!-- should always be replaced with &lt;!--.
It's also inconsistent whether it puts <em> tags around the 'commented' section. I believe it should be replacing it in both cases:

>>> markdown2.markdown('*foo* <!-- *bar* -->', safe_mode='escape')
'<p><em>foo</em> &lt;!-- <em>bar</em> --&gt;</p>\n'
>>> markdown2.markdown('*foo* <!-- *bar*', safe_mode='escape')
'<p><em>foo</em> &lt;!-- <em>bar</em></p>\n'

Debug info
Version of library being used: 2.4.12

Any extras being used: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions