Skip to content

Commit

Permalink
ckeditor: add templatetag which disables iframes if javascript is dis…
Browse files Browse the repository at this point in the history
…abled
  • Loading branch information
goapunk committed Nov 22, 2023
1 parent 8b1956d commit 3039aa8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
7 changes: 7 additions & 0 deletions adhocracy4/ckeditor/templatetags/ckeditor_tags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import time

from django import template
Expand Down Expand Up @@ -45,3 +46,9 @@ def transform_collapsibles(text):
)

return serialize(tree)


@register.filter
def disable_iframes(text):
"""Disable all iframes to prevent them from loading if js is disabled"""
return re.sub(r"(<iframe .*?)src=(.*?>)", r"\1data-src=\2", text)
4 changes: 4 additions & 0 deletions changelog/_8999.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Added

- templatetag which disables iframes to stop them from loading if javascript is
disabled
19 changes: 19 additions & 0 deletions tests/ckeditor/test_ckeditor_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ def test_transform_collapsibles(project_factory):
output = render_template(template, {"project": project})
assert "<span>Title</span>" in output
assert "<div>Body</div>" in output


@pytest.mark.django_db
def test_disable_iframes(project_factory):
project = project_factory(
information="<div><figure class=media><div "
'data-oembed-url="https://www.youtube.com/embed/PkhmcJWSNAU'
'?controls=0"><div><iframe '
'src="https://www.youtube.com/embed/PkhmcJWSNAU?rel=0"></iframe'
"></div></div></figure><p>liqd project info</div>"
)

template = (
"{% load ckeditor_tags %}"
+ "{{ project.information | disable_iframes | safe }}"
)
output = render_template(template, {"project": project})
assert "iframe data-src" in output
assert "iframe src" not in output
19 changes: 17 additions & 2 deletions tests/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,26 @@
],
},
"collapsible-image-editor": {
"tags": ["p", "strong", "em", "u", "ol", "li", "ul", "a", "img", "div"],
"tags": [
"p",
"strong",
"em",
"u",
"ol",
"li",
"ul",
"a",
"img",
"div",
"iframe",
"figure",
],
"attributes": {
"a": ["href", "rel"],
"img": ["src", "alt", "style"],
"div": ["class"],
"div": ["class", "data-oembed-url"],
"iframe": ["src", "alt"],
"figure": ["class", "div", "iframe"],
},
"styles": [
"float",
Expand Down

0 comments on commit 3039aa8

Please sign in to comment.