Skip to content

Commit 7b8ceea

Browse files
committed
fix: preserve text nested inside <br> by html.parser
Python's html.parser can wrap text following <br /> (with a space) inside the <br> element as child content when the <br> is preceded by a sibling <br> without a slash. convert_br received the nested text via its `text` parameter but discarded it, silently dropping characters from the output. Append `text` to the returned line-break marker so that any accidentally nested content is always preserved. Closes #244
1 parent add391a commit 7b8ceea

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

‎markdownify/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,12 @@ def _indent_for_blockquote(match):
475475

476476
def convert_br(self, el, text, parent_tags):
477477
if '_inline' in parent_tags:
478-
return ' '
478+
return text + ' ' if text else ' '
479479

480480
if self.options['newline_style'].lower() == BACKSLASH:
481-
return '\\\n'
481+
return '\\\n' + text
482482
else:
483-
return ' \n'
483+
return ' \n' + text
484484

485485
def convert_code(self, el, text, parent_tags):
486486
if '_noformat' in parent_tags:

‎tests/test_conversions.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def test_br():
8181
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'
8282
assert md('<h1>foo<br />bar</h1>', heading_style=ATX) == '\n\n# foo bar\n\n'
8383
assert md('<td>foo<br />bar</td>', heading_style=ATX) == ' foo bar |'
84+
# html.parser may nest text inside <br> when mixing <br> and <br />; text must not be lost
85+
assert md('a<br>b<br />c') == 'a \nb \nc'
86+
assert md('a<br>b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'
8487

8588

8689
def test_code():

0 commit comments

Comments
 (0)