diff --git a/lacommunaute/utils/html.py b/lacommunaute/utils/html.py index 3d994973..2d3644e1 100644 --- a/lacommunaute/utils/html.py +++ b/lacommunaute/utils/html.py @@ -2,13 +2,15 @@ def wrap_iframe_in_div_tag(text): - # iframe tags must be wrapped in a div tag to be displayed correctly - # add div tag if not present + """ + given a markdown text, wrap all iframe tags in a div tag + this is required for iframes to be displayed correctly + """ - iframe_regex = r"((
)?]*>.*?<\/iframe>(<\/div>)?)" + iframe_regex = r"((
)?(]*><\/iframe>)(<\/div>)?)" - for match, starts_with, ends_with in re.findall(iframe_regex, text, re.DOTALL): - if not starts_with and not ends_with: - text = text.replace(match, f"
{match}
") + for match, starts_with, iframe, ends_with in re.findall(iframe_regex, text): + if not (starts_with and ends_with): + text = text.replace(match, f"
{iframe}
") return text diff --git a/lacommunaute/utils/tests/tests_utils.py b/lacommunaute/utils/tests/tests_utils.py index 2ff4d8ba..ca87fd8f 100644 --- a/lacommunaute/utils/tests/tests_utils.py +++ b/lacommunaute/utils/tests/tests_utils.py @@ -711,17 +711,26 @@ def test_the_last_sunday(self, day, expected_sunday): class TestWrapIframeInDiv: - def test_wrap_iframe_in_div_tag(self): - inputs = [ - "", - "
", - "
", - "
", - ] - outputs = [ - "
", - "
", - "
", - "
", - ] - assert wrap_iframe_in_div_tag(" ".join(inputs)) == " ".join(outputs) + @pytest.mark.parametrize( + "input,output", + [ + ("", "
"), + ( + "markdown text markdown text", + "markdown text
markdown text", + ), + ("
", "
"), + ("
text", "
text"), + ("
", "
"), + ( + "", + "
", + ), + ( + "
", + "
", + ), + ], + ) + def test_wrap_iframe_in_div_tag(self, input, output): + assert wrap_iframe_in_div_tag(input) == output