Skip to content

Commit e394d82

Browse files
committed
Strip html tags from get_title/get_description in settings
1 parent 92e973a commit e394d82

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Improve developement instructions (@marteinn)
99
- Make sure title and descriptions are truncated if too long (@marteinn)
1010
- Strip html tags from preview widget (@marteinn)
11+
- Strip html tags from get_title/get_description in settings (@marteinn)
1112

1213
### Fixed
1314
- Fix broken image preview #12 (@marteinn)

tests/test_panels_google.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,16 @@ def test_title_and_description_trunctation(self):
109109

110110
self.assertEqual(len(google_settings.get_title()), 70)
111111
self.assertEqual(len(google_settings.get_description()), 160)
112+
113+
def test_that_html_tags_are_stripped(self):
114+
google_settings = GoogleSettings(self.google_page)
115+
116+
meta_settings.META_PREVIEW_GOOGLE_TITLE_FIELDS = "seo_title"
117+
meta_settings.META_PREVIEW_GOOGLE_DESCRIPTION_FIELDS = "search_description"
118+
119+
self.google_page.seo_title = "<strong>Hello world</strong> and <em>earth</em>"
120+
self.google_page.search_description = "<p>My description</p>"
121+
self.google_page.save()
122+
123+
self.assertEqual(google_settings.get_title(), "Hello world and earth")
124+
self.assertEqual(google_settings.get_description(), "My description")

wagtail_meta_preview/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Union, Literal
22

33
from django.utils.text import Truncator
4+
from django.utils.html import strip_tags
45

56
from . import meta_settings
67

@@ -65,6 +66,8 @@ def get_title(self) -> str:
6566
return ""
6667

6768
value = getattr(self.instance, title_field) or ""
69+
value = strip_tags(value)
70+
6871
if self.title_max_chars == -1:
6972
return value
7073

@@ -90,6 +93,8 @@ def get_description(self):
9093
return ""
9194

9295
value = getattr(self.instance, description_field) or ""
96+
value = strip_tags(value)
97+
9398
if self.description_max_chars == -1:
9499
return value
95100

0 commit comments

Comments
 (0)