Skip to content

Commit b1ff281

Browse files
authored
Merge pull request #562 from matthewwithanm/anchor-thumbnail
Convert the anchor kwarg from SafeString to str.
2 parents db22c84 + 7dde620 commit b1ff281

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

imagekit/templatetags/imagekit.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ def render(self, context):
9292
kwargs = {k: v.resolve(context) for k, v in self._generator_kwargs.items()}
9393
kwargs['source'] = self._source.resolve(context)
9494
kwargs.update(parse_dimensions(self._dimensions.resolve(context)))
95+
if kwargs.get('anchor'):
96+
# ImageKit uses pickle at protocol 0, which throws infinite
97+
# recursion errors when anchor is set to a SafeString instance.
98+
# This converts the SafeString into a str instance.
99+
kwargs['anchor'] = kwargs['anchor'][:]
95100
generator = generator_registry.get(generator_id, **kwargs)
96101

97102
context[variable_name] = ImageCacheFile(generator)
@@ -114,6 +119,11 @@ def render(self, context):
114119
kwargs = {k: v.resolve(context) for k, v in self._generator_kwargs.items()}
115120
kwargs['source'] = self._source.resolve(context)
116121
kwargs.update(dimensions)
122+
if kwargs.get('anchor'):
123+
# ImageKit uses pickle at protocol 0, which throws infinite
124+
# recursion errors when anchor is set to a SafeString instance.
125+
# This converts the SafeString into a str instance.
126+
kwargs['anchor'] = kwargs['anchor'][:]
117127
generator = generator_registry.get(generator_id, **kwargs)
118128

119129
file = ImageCacheFile(generator)

tests/test_thumbnail_tag.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ def test_img_tag():
1515
assert attrs[k].strip() != ''
1616

1717

18+
def test_img_tag_anchor():
19+
ttag = r"""{% thumbnail '100x100' img anchor='c' %}"""
20+
clear_imagekit_cache()
21+
attrs = get_html_attrs(ttag)
22+
expected_attrs = {'src', 'width', 'height'}
23+
assert set(attrs.keys()) == expected_attrs
24+
for k in expected_attrs:
25+
assert attrs[k].strip() != ''
26+
27+
1828
def test_img_tag_attrs():
1929
ttag = r"""{% thumbnail '100x100' img -- alt="Hello" %}"""
2030
clear_imagekit_cache()
@@ -58,6 +68,13 @@ def test_assignment_tag():
5868
assert html != ''
5969

6070

71+
def test_assignment_tag_anchor():
72+
ttag = r"""{% thumbnail '100x100' img anchor='c' as th %}{{ th.url }}"""
73+
clear_imagekit_cache()
74+
html = render_tag(ttag)
75+
assert html != ''
76+
77+
6178
def test_single_dimension():
6279
ttag = r"""{% thumbnail '100x' img as th %}{{ th.width }}"""
6380
clear_imagekit_cache()

0 commit comments

Comments
 (0)