Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def strip1_pre(text):
def strip_pre(text):
"""Strip all leading and trailing newlines from a <pre> string."""
text = re_pre_lstrip.sub('', text)
text = re_pre_rstrip.sub('', text)
text = text.rstrip(' \n')
return text


Expand Down
27 changes: 27 additions & 0 deletions tests/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Test whitelisting/blacklisting of specific tags.

"""
import time

import pytest

from markdownify import markdownify, LSTRIP, RSTRIP, STRIP, STRIP_ONE
from .utils import md

Expand Down Expand Up @@ -45,3 +49,26 @@ def bs4_options():
assert markdownify("<p>Hello</p>", bs4_options="html.parser") == "Hello"
assert markdownify("<p>Hello</p>", bs4_options=["html.parser"]) == "Hello"
assert markdownify("<p>Hello</p>", bs4_options={"features": "html.parser"}) == "Hello"


@pytest.mark.parametrize('text, expected', [
(' indented', ' indented'), ('x ', 'x'), ('x \n ', 'x'),
('', ''), (' \n ', ''), ('\tx\t', '\tx\t'),
('x\r', 'x\r'), ('x\v', 'x\v'), ('x\u00a0', 'x\u00a0'),
(' \n x \n ', ' x'),
])
def test_strip_pre_whitespace_semantics(text, expected):
expected_markdown = '```\n' + expected + '\n```' if text else ''
assert markdownify('<pre>' + text + '</pre>') == expected_markdown


def test_strip_pre_long_indentation():
# A space run followed by text must not restart a trailing-whitespace
# search at every position. The linear implementation takes milliseconds;
# this generous limit catches the quadratic regression.
text = ' ' * 65536 + 'x'
start = time.monotonic()
result = markdownify('<pre>' + text + '</pre>')
elapsed = time.monotonic() - start
assert result == '```\n' + text + '\n```'
assert elapsed < 5