Skip to content
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

Convert the anchor kwarg from SafeString to str. #562

Merged
merged 1 commit into from
May 10, 2023
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
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