Skip to content

Commit

Permalink
Convert the anchor kwarg from SafeString to str.
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-schilling authored and vstoykov committed May 3, 2023
1 parent 3113ed8 commit 7dde620
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions imagekit/templatetags/imagekit.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ def render(self, context):
kwargs = {k: v.resolve(context) for k, v in self._generator_kwargs.items()}
kwargs['source'] = self._source.resolve(context)
kwargs.update(parse_dimensions(self._dimensions.resolve(context)))
if kwargs.get('anchor'):
# ImageKit uses pickle at protocol 0, which throws infinite
# recursion errors when anchor is set to a SafeString instance.
# This converts the SafeString into a str instance.
kwargs['anchor'] = kwargs['anchor'][:]
generator = generator_registry.get(generator_id, **kwargs)

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

file = ImageCacheFile(generator)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_thumbnail_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def test_img_tag():
assert attrs[k].strip() != ''


def test_img_tag_anchor():
ttag = r"""{% thumbnail '100x100' img anchor='c' %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag)
expected_attrs = {'src', 'width', 'height'}
assert set(attrs.keys()) == expected_attrs
for k in expected_attrs:
assert attrs[k].strip() != ''


def test_img_tag_attrs():
ttag = r"""{% thumbnail '100x100' img -- alt="Hello" %}"""
clear_imagekit_cache()
Expand Down Expand Up @@ -58,6 +68,13 @@ def test_assignment_tag():
assert html != ''


def test_assignment_tag_anchor():
ttag = r"""{% thumbnail '100x100' img anchor='c' as th %}{{ th.url }}"""
clear_imagekit_cache()
html = render_tag(ttag)
assert html != ''


def test_single_dimension():
ttag = r"""{% thumbnail '100x' img as th %}{{ th.width }}"""
clear_imagekit_cache()
Expand Down

0 comments on commit 7dde620

Please sign in to comment.