Repository navigation
<hr> tags break markup processing in 3.3.0+? #1053
Copy link
Copy link
Closed
Labels
bugBug report.Bug report.confirmedConfirmed bug report or approved feature request.Confirmed bug report or approved feature request.coreRelated to the core parser code.Related to the core parser code.
Description
<hr> elements seem to interrupt markdown text processing:
- Markdown Input:
'*emphasis1*\n<hr>\n*emphasis2*' - HTML Output (actual):
'<p><em>emphasis1</em></p>\n<hr>\n*emphasis2*' - HTML Output (expected):
'<p><em>emphasis1</em>\n<hr>\n<em>emphasis2</em></p>'
*emphasis1*\n<hr/>\n*emphasis2* (closing the hr tag) causes *emphasis2* to render correctly.
Naked <hr> tags should be valid HTML: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hr
Having the md_in_html extension enabled or disabled doesn't seem to make a difference.
Versions
- Markdown==3.3.2 (affects 3.3.0 and above)
- CPython 3.8.6
Steps to reproduce
mderror.py:
import markdown
def test(md_text):
html = markdown.markdown(
md_text,
# Same result with or without md_in_html
# extensions=["markdown.extensions.md_in_html"],
)
okay = "*emphasis" not in html
print(f"{'OK ' if okay else 'ERR'} {md_text!r} → {html!r}")
# ERR
test("*emphasis1*\n<hr>\n*emphasis2*")
# OK
test("*emphasis1*\n<hr/>\n*emphasis2*")
test("*emphasis1*\n<hr></hr>\n*emphasis2*")
test("*emphasis1*\n<img>\n*emphasis2*")Output:
897c854 also fails:
pip install git+https://github.com/Python-Markdown/markdown@897c8541998cdbd0499b04942244f7fdf1191a6c && python mderror.py
ERR '*emphasis1*\n<hr>\n*emphasis2*' → '<p><em>emphasis1</em></p>\n<hr>\n*emphasis2*'
OK '*emphasis1*\n<hr/>\n*emphasis2*' → '<p><em>emphasis1</em>\n<hr/></p>\n<p><em>emphasis2</em></p>'
OK '*emphasis1*\n<hr></hr>\n*emphasis2*' → '<p><em>emphasis1</em></p>\n<hr></hr>\n<p><em>emphasis2</em></p>'
OK '*emphasis1*\n<img>\n*emphasis2*' → '<p><em>emphasis1</em>\n<img>\n<em>emphasis2</em></p>'$ pip install Markdown==3.3.2 && python mderror.py
ERR '*emphasis1*\n<hr>\n*emphasis2*' → '<p><em>emphasis1</em></p>\n<hr>\n*emphasis2*'
OK '*emphasis1*\n<hr/>\n*emphasis2*' → '<p><em>emphasis1</em>\n<hr/></p>\n<p><em>emphasis2</em></p>'
OK '*emphasis1*\n<hr></hr>\n*emphasis2*' → '<p><em>emphasis1</em></p>\n<hr></hr>\n<p><em>emphasis2</em></p>'
OK '*emphasis1*\n<img>\n*emphasis2*' → '<p><em>emphasis1</em>\n<img>\n<em>emphasis2</em></p>'$ pip install Markdown==3.3.1 && python mderror.py
ERR '*emphasis1*\n<hr>\n*emphasis2*' → '<p><em>emphasis1</em></p>\n<hr>\n*emphasis2*'
OK '*emphasis1*\n<hr/>\n*emphasis2*' → '<p><em>emphasis1</em>\n<hr/></p>\n<p><em>emphasis2</em></p>'
OK '*emphasis1*\n<hr></hr>\n*emphasis2*' → '<p><em>emphasis1</em></p>\n<hr></hr>\n<p><em>emphasis2</em></p>'
OK '*emphasis1*\n<img>\n*emphasis2*' → '<p><em>emphasis1</em>\n<img>\n<em>emphasis2</em></p>'3.3.0 starts failing:
→ pip install Markdown==3.3.0 && python mderror.py
ERR '*emphasis1*\n<hr>\n*emphasis2*' → '<p><em>emphasis1</em></p>\n<hr>\n*emphasis2*'
OK '*emphasis1*\n<hr/>\n*emphasis2*' → '<p><em>emphasis1</em>\n<hr/></p>\n<p><em>emphasis2</em></p>'
OK '*emphasis1*\n<hr></hr>\n*emphasis2*' → '<p><em>emphasis1</em></p>\n<hr></hr>\n<p><em>emphasis2</em></p>'
OK '*emphasis1*\n<img>\n*emphasis2*' → '<p><em>emphasis1</em>\n<img>\n<em>emphasis2</em></p>'3.2.2 passes all tests:
→ pip install Markdown==3.2.2 && python mderror.py
OK '*emphasis1*\n<hr>\n*emphasis2*' → '<p><em>emphasis1</em>\n<hr>\n<em>emphasis2</em></p>'
OK '*emphasis1*\n<hr/>\n*emphasis2*' → '<p><em>emphasis1</em>\n<hr/>\n<em>emphasis2</em></p>'
OK '*emphasis1*\n<hr></hr>\n*emphasis2*' → '<p><em>emphasis1</em>\n<hr></hr>\n<em>emphasis2</em></p>'
OK '*emphasis1*\n<img>\n*emphasis2*' → '<p><em>emphasis1</em>\n<img>\n<em>emphasis2</em></p>'Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugBug report.Bug report.confirmedConfirmed bug report or approved feature request.Confirmed bug report or approved feature request.coreRelated to the core parser code.Related to the core parser code.
Activity
[-]<hr> tags rendering differently in 3.3.0+[/-][+]<hr> tags break markup processing in 3.3.0+?[/+]facelessuser commentedon Oct 23, 2020
I'll have to test the tip. We have some unreleased fixes as there were some inconsistencies with certain tags.
facelessuser commentedon Oct 24, 2020
Yeah, it looks like new behavior introduced by the new parser. I would think
<hr>would get handled similar to<br>, but it isn't.On the one hand, as it is a block item, I'm not sure it should render inside a paragraph. But on the other hand, it seems to treat it as a block (which it is) and just keep searching for an end tag. It doesn't find the end, but assumes all the trailing text is inside the
<hr>and since blocks are not parsed for Markdown, the emphasis isn't parsed.Interestingly, if we add
markdown="1"to it, we get data loss:Seems we need to handle unclosed tags a bit better:
@waylan thoughts?
facelessuser commentedon Oct 24, 2020
Here is a potential fix. Interested to hear your opinion on approach @waylan or if you have a different suggestion:
facelessuser commentedon Oct 24, 2020
Should be noted that this doesn't universally fix potential data loss in
md_in_htmlif ending tag is missing.facelessuser commentedon Oct 24, 2020
This will have to be tweaked as well as it isn't perfect and doubles
<hr>tags, but the general direction is what I'm trying to highlight.facelessuser commentedon Oct 24, 2020
This is a bit more sane:
facelessuser commentedon Oct 24, 2020
Still not happy with how the
<hr>is still included in the previous<p>. It's probably due to the fact thathandle_startendtagwas more geared to handling inline tags. I don't have time to investigate that tonight.waylan commentedon Oct 24, 2020
This was an intentional change in behavior. Previously, we strictly enforced the rule which required that an HTML block must start with a blank line. However, in practice, in most cases, even the reference implementation doesn't follow that rule (strangely it does with
<hr>). Therefore, as of version 3.3, Python-Markdown ends a Markdown paragraph (or any other block) upon finding an opening tag (or a self-closing tag) of a block-level element at the start of a line.I should also note that it is invalid HTML to have an
<hr>within a<p>. In fact, browsers will interpretas
Note that
baris not even in a<p>tag as far as the browser is concerned. Therefore, it is nonsensical to output that HTML. The correct way to parse the following Markdownwould be
And that will provide a better rendering in the browser.
Which means that we do have a bug in that the line after the
<hr>is not being wrapped in a<p>. Presumably a blank line needs to be inserted after block-level self-closing tags in the htmlparser.facelessuser commentedon Oct 24, 2020
I have some work here: #1054. We can discuss changes there and change direction if I'm going in bad direction.
This case:
Should render as
Fix issues related to hr tags