Skip to content

Commit fcc1eee

Browse files
authored
Fix #208 -- Avoid illegal i10n integer width values in the srcset-attribute (#209)
Django's template internationalization (i10n) with the `USE_THOUSAND_SEPARATOR` set true will inject illegal characters into width values `srcset` attribute, causing client/browser errors.
1 parent 0358053 commit fcc1eee

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<picture{% include 'pictures/attrs.html' with attrs=picture_attrs %}>{% for type, srcset in sources.items %}
22
<source type="image/{{ type|lower }}"
3-
srcset="{% for width, pic in srcset.items %}{% if use_placeholders %}{% url 'pictures:placeholder' alt|urlencode:'' ratio width type %}{% else %}{{ pic.url }}{% endif %} {{ width }}w{% if not forloop.last %}, {% endif %}{% endfor %}"
3+
srcset="{% for width, pic in srcset.items %}{% if use_placeholders %}{% url 'pictures:placeholder' alt|urlencode:'' ratio width type %}{% else %}{{ pic.url }}{% endif %} {{ width|stringformat:'i' }}w{% if not forloop.last %}, {% endif %}{% endfor %}"
44
sizes="{{ media }}">{% endfor %}
55
<img{% include 'pictures/attrs.html' with attrs=img_attrs %}>
66
</picture>

tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ def tiny_image_upload_file():
2626
return SimpleUploadedFile("image.png", output.getvalue())
2727

2828

29+
@pytest.fixture
30+
def large_image_upload_file():
31+
img = Image.new("RGBA", (1000, 1000), (255, 55, 255, 1))
32+
33+
with io.BytesIO() as output:
34+
img.save(output, format="PNG")
35+
return SimpleUploadedFile("image.png", output.getvalue())
36+
37+
2938
@pytest.fixture(autouse=True, scope="function")
3039
def media_root(settings, tmpdir_factory):
3140
settings.MEDIA_ROOT = tmpdir_factory.mktemp("media", numbered=True)

tests/test_templatetags.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
</picture>
1313
"""
1414

15+
picture_html_large = b"""
16+
<picture>
17+
<source type="image/webp"
18+
srcset="/media/testapp/profile/image/800w.webp 800w, /media/testapp/profile/image/100w.webp 100w, /media/testapp/profile/image/900w.webp 900w, /media/testapp/profile/image/200w.webp 200w, /media/testapp/profile/image/1000w.webp 1000w, /media/testapp/profile/image/300w.webp 300w, /media/testapp/profile/image/400w.webp 400w, /media/testapp/profile/image/500w.webp 500w, /media/testapp/profile/image/600w.webp 600w, /media/testapp/profile/image/700w.webp 700w"
19+
sizes="(min-width: 0px) and (max-width: 991px) 100vw, (min-width: 992px) and (max-width: 1199px) 33vw, 600px">
20+
<img src="/media/testapp/profile/image.png" alt="Spiderman" width="1000" height="1000">
21+
</picture>
22+
"""
23+
1524
picture_with_placeholders_html = b"""
1625
<picture>
1726
<source type="image/webp"
@@ -31,6 +40,17 @@ def test_picture(client, image_upload_file, settings):
3140
assert picture_html in response.content
3241

3342

43+
@pytest.mark.django_db
44+
def test_picture__large(client, large_image_upload_file, settings):
45+
settings.PICTURES["USE_PLACEHOLDERS"] = False
46+
# ensure that USE_THOUSAND_SEPARATOR doesn't break srcset with widths greater than 1000px
47+
settings.USE_THOUSAND_SEPARATOR = True
48+
profile = Profile.objects.create(name="Spiderman", picture=large_image_upload_file)
49+
response = client.get(profile.get_absolute_url())
50+
assert response.status_code == 200
51+
assert picture_html_large in response.content
52+
53+
3454
@pytest.mark.django_db
3555
def test_picture__placeholder(client, image_upload_file, settings):
3656
settings.PICTURES["USE_PLACEHOLDERS"] = True

0 commit comments

Comments
 (0)