Skip to content

Commit

Permalink
PR #780 - will be empty after rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 25, 2024
1 parent a60d232 commit b620467
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
16 changes: 1 addition & 15 deletions lacommunaute/forum/forms.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import re

from django import forms
from django.conf import settings
from django.forms import CharField, CheckboxSelectMultiple, ModelMultipleChoiceField
from taggit.models import Tag

from lacommunaute.forum.models import Forum
from lacommunaute.partner.models import Partner


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

iframe_regex = r"((<div>)?<iframe.*?</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"<div>{match}</div>")

return text
from lacommunaute.utils.html import wrap_iframe_in_div_tag


class ForumForm(forms.ModelForm):
Expand Down
14 changes: 14 additions & 0 deletions lacommunaute/utils/html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import re


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

iframe_regex = r"((<div>)?<iframe[^>]*>.*?<\/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"<div>{match}</div>")

return text
18 changes: 18 additions & 0 deletions lacommunaute/utils/tests/tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from lacommunaute.stats.models import ForumStat
from lacommunaute.users.factories import UserFactory
from lacommunaute.utils.date import get_last_sunday
from lacommunaute.utils.html import wrap_iframe_in_div_tag
from lacommunaute.utils.math import percent
from lacommunaute.utils.matomo import (
collect_forum_stats_from_matomo_api,
Expand Down Expand Up @@ -707,3 +708,20 @@ class TestTheLastSunday:
)
def test_the_last_sunday(self, day, expected_sunday):
assert get_last_sunday(datetime(2024, 5, day)) == expected_sunday


class TestWrapIframeInDiv:
def test_wrap_iframe_in_div_tag(self):
inputs = [
"<iframe src='xxx'></iframe>",
"<div><iframe src='yyy'></iframe></div>",
"<div><iframe src='zzz'></iframe>",
"<iframe src='www'></iframe></div>",
]
outputs = [
"<div><iframe src='xxx'></iframe></div>",
"<div><iframe src='yyy'></iframe></div>",
"<div><iframe src='zzz'></iframe>",
"<iframe src='www'></iframe></div>",
]
assert wrap_iframe_in_div_tag(" ".join(inputs)) == " ".join(outputs)

0 comments on commit b620467

Please sign in to comment.