Skip to content

Commit

Permalink
Strip html tags from get_title/get_description in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
marteinn committed Feb 26, 2025
1 parent 92e973a commit e394d82
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Improve developement instructions (@marteinn)
- Make sure title and descriptions are truncated if too long (@marteinn)
- Strip html tags from preview widget (@marteinn)
- Strip html tags from get_title/get_description in settings (@marteinn)

### Fixed
- Fix broken image preview #12 (@marteinn)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_panels_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ def test_title_and_description_trunctation(self):

self.assertEqual(len(google_settings.get_title()), 70)
self.assertEqual(len(google_settings.get_description()), 160)

def test_that_html_tags_are_stripped(self):
google_settings = GoogleSettings(self.google_page)

meta_settings.META_PREVIEW_GOOGLE_TITLE_FIELDS = "seo_title"
meta_settings.META_PREVIEW_GOOGLE_DESCRIPTION_FIELDS = "search_description"

self.google_page.seo_title = "<strong>Hello world</strong> and <em>earth</em>"
self.google_page.search_description = "<p>My description</p>"
self.google_page.save()

self.assertEqual(google_settings.get_title(), "Hello world and earth")
self.assertEqual(google_settings.get_description(), "My description")
5 changes: 5 additions & 0 deletions wagtail_meta_preview/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union, Literal

from django.utils.text import Truncator
from django.utils.html import strip_tags

from . import meta_settings

Expand Down Expand Up @@ -65,6 +66,8 @@ def get_title(self) -> str:
return ""

value = getattr(self.instance, title_field) or ""
value = strip_tags(value)

if self.title_max_chars == -1:
return value

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

value = getattr(self.instance, description_field) or ""
value = strip_tags(value)

if self.description_max_chars == -1:
return value

Expand Down

0 comments on commit e394d82

Please sign in to comment.