Skip to content

Fixed escaping of alt text in ContentFormat.img() #2036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils import timezone
from django.utils.cache import _generate_cache_header_key
from django.utils.formats import date_format
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from django_hosts.resolvers import get_host, reverse, reverse_host
from docutils.core import publish_parts
Expand Down Expand Up @@ -73,7 +74,7 @@ def img(self, url, alt_text):
CF = type(self)
return {
CF.REST: f".. image:: {url}\n :alt: {alt_text}",
CF.HTML: f'<img src="{url}" alt="{alt_text}">',
CF.HTML: format_html('<img src="{}" alt="{}">', url, alt_text),
CF.MARKDOWN: f"![{alt_text}]({url})",
}[self]

Expand Down
40 changes: 40 additions & 0 deletions blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,43 @@ def test_full_url(self):
r"http://www\.djangoproject\.localhost:8000"
r"/m/blog/images/2005/07/test(_\w+)?\.png",
)

def test_alt_text_html_escape(self):
testdata = [
(ContentFormat.HTML, 'te"st', '<img src="." alt="te&quot;st">'),
(ContentFormat.HTML, "te<st>", '<img src="." alt="te&lt;st&gt;">'),
(ContentFormat.MARKDOWN, 'te"st', '<img src="." alt="te&quot;st">'),
(ContentFormat.MARKDOWN, "te[st]", '<img src="." alt="te[st]">'),
(ContentFormat.MARKDOWN, "te{st}", '<img src="." alt="te{st}">'),
(ContentFormat.MARKDOWN, "te<st>", '<img src="." alt="te&lt;st&gt;">'),
(ContentFormat.MARKDOWN, "test*", '<img src="." alt="test*">'),
(ContentFormat.MARKDOWN, "test_", '<img src="." alt="test_">'),
(ContentFormat.MARKDOWN, "test`", '<img src="." alt="test`">'),
(ContentFormat.MARKDOWN, "test+", '<img src="." alt="test+">'),
(ContentFormat.MARKDOWN, "test-", '<img src="." alt="test-">'),
(ContentFormat.MARKDOWN, "test.", '<img src="." alt="test.">'),
(ContentFormat.MARKDOWN, "test!", '<img src="." alt="test!">'),
(ContentFormat.MARKDOWN, "te\nst", '<img src="." alt="te\nst">'),
(ContentFormat.REST, 'te"st', '<img src="." alt="te&quot;st">'),
(ContentFormat.REST, "te[st]", '<img src="." alt="te[st]">'),
(ContentFormat.REST, "te{st}", '<img src="." alt="te{st}">'),
(ContentFormat.REST, "te<st>", '<img src="." alt="te&lt;st&gt;">'),
(ContentFormat.REST, "te:st", '<img src="." alt="te:st">'),
(ContentFormat.REST, "test*", '<img src="." alt="test*">'),
(ContentFormat.REST, "test_", '<img src="." alt="test_">'),
(ContentFormat.REST, "test`", '<img src="." alt="test`">'),
(ContentFormat.REST, "test+", '<img src="." alt="test+">'),
(ContentFormat.REST, "test-", '<img src="." alt="test-">'),
(ContentFormat.REST, "test.", '<img src="." alt="test.">'),
(ContentFormat.REST, "test!", '<img src="." alt="test!">'),
]
for cf, alt_text, expected in testdata:
# RST doesn't like an empty src, so we use . instead
img_tag = cf.img(url=".", alt_text=alt_text)
if cf is ContentFormat.MARKDOWN:
expected = f"<p>{expected}</p>"
with self.subTest(cf=cf, alt_text=alt_text):
self.assertHTMLEqual(
ContentFormat.to_html(cf, img_tag),
expected,
)