-
-
Notifications
You must be signed in to change notification settings - Fork 992
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
base: main
Are you sure you want to change the base?
Conversation
6cdcbef
to
161b8fd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable but I don’t think I’ll have time to actually test this / review in more depth, sorry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the final ReST test that includes a newline (see my other comment), I'm able to get the tests to pass with this small change:
diff --git a/blog/models.py b/blog/models.py
index 0083dd22..9ca310b0 100644
--- a/blog/models.py
+++ b/blog/models.py
@@ -1,3 +1,4 @@
+import html
from urllib.parse import urlparse
from django.conf import settings
@@ -73,7 +74,7 @@ class ContentFormat(models.TextChoices):
CF = type(self)
return {
CF.REST: f".. image:: {url}\n :alt: {alt_text}",
- CF.HTML: f'<img src="{url}" alt="{alt_text}">',
+ CF.HTML: f'<img src="{url}" alt="{html.escape(alt_text)}">',
CF.MARKDOWN: f"",
}[self]
I'm wondering if _md_escape
is not needed since markdown
escapes alt text automatically:
>>> import markdown
>>> markdown.markdown('')
'<p><img alt="Al"t t"ext" src="/path/to/img.jpg" /></p>'
If the intent behind _md_escape
is something beyond fixing alt text quoting, could we put it in a different PR?
161b8fd
to
cef9de9
Compare
I got carried away not realizing that escaping was not really needed in markdown, I agree that keeping it simple is better here. I went for the Let me know what you think (I've kept the changes in a separate commit for ease of review but the two should get squashed when merging). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Looks great! No worries on the initial complexity. Thanks for considering my suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This should fix #2035 and also address escaping issues in the other formats (markdown and restructured text).