From cf3d6a4e66b646bf666b73fa0b16f9f2f5418c5b Mon Sep 17 00:00:00 2001 From: Jeffrey Hearn Date: Fri, 6 Jun 2014 10:58:14 -0400 Subject: [PATCH 1/4] Addition of attrs property to image template tag --- wagtail/wagtailimages/models.py | 6 ++++++ wagtail/wagtailimages/tests.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py index 24fd1f0bf..921f65921 100644 --- a/wagtail/wagtailimages/models.py +++ b/wagtail/wagtailimages/models.py @@ -225,6 +225,12 @@ class AbstractRendition(models.Model): def url(self): return self.file.url + @property + def attrs(self): + return mark_safe( + 'src="%s" width="%d" height="%d" alt="%s"' % (escape(self.url), self.width, self.height, escape(self.image.title)) + ) + def img_tag(self): return mark_safe( '%s' % (escape(self.url), self.width, self.height, escape(self.image.title)) diff --git a/wagtail/wagtailimages/tests.py b/wagtail/wagtailimages/tests.py index 00d1245e6..f36751650 100644 --- a/wagtail/wagtailimages/tests.py +++ b/wagtail/wagtailimages/tests.py @@ -183,6 +183,18 @@ class TestImageTag(TestCase): self.assertTrue('height="300"' in result) self.assertTrue('alt="Test image"' in result) + def render_image_tag_as(self, image, filter_spec): + temp = template.Template('{% load image_tags %}{% image image_obj ' + filter_spec + ' as test_img %}') + context = template.Context({'image_obj': image}) + return temp.render(context) + + def test_image_tag_attrs(self): + result = self.render_image_tag_as(self.image, 'width-400') + + # Check that all the required HTML attributes are set + self.assertTrue('width="400"' in result) + self.assertTrue('height="300"' in result) + self.assertTrue('alt="Test image"' in result) ## ===== ADMIN VIEWS ===== From 4e8e1f97ca250920f958ffdd732edbf4de816402 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 19 Jun 2014 17:28:34 +0100 Subject: [PATCH 2/4] use self.attrs in AbstractRendition.img_tag to avoid duplication --- wagtail/wagtailimages/models.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py index 17e1ece86..8535199e7 100644 --- a/wagtail/wagtailimages/models.py +++ b/wagtail/wagtailimages/models.py @@ -243,9 +243,7 @@ class AbstractRendition(models.Model): ) def img_tag(self): - return mark_safe( - '%s' % (escape(self.url), self.width, self.height, escape(self.image.title)) - ) + return mark_safe('' % self.attrs) class Meta: abstract = True From f6ae1834fa350692db2632be6f6714b69726c4e6 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 19 Jun 2014 17:34:59 +0100 Subject: [PATCH 3/4] Document the 'attrs' property of image renditions --- docs/building_your_site/frontenddevelopers.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/building_your_site/frontenddevelopers.rst b/docs/building_your_site/frontenddevelopers.rst index f0054eaab..a8cf6a25e 100644 --- a/docs/building_your_site/frontenddevelopers.rst +++ b/docs/building_your_site/frontenddevelopers.rst @@ -197,6 +197,11 @@ In some cases greater control over the ``img`` tag is required, for example to a {{ tmp_photo.alt }} +You can also use the ``attrs`` property as a shorthand to output the ``src``, ``width``, ``height`` and ``alt`` attributes in one go: + +.. code-block:: django + + .. _rich-text-filter: From aad0e24a48812a683ad295870a700b07eee31eac Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 19 Jun 2014 17:36:57 +0100 Subject: [PATCH 4/4] Changelog entry for #299 --- CHANGELOG.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 6686cf973..c577040c6 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -12,6 +12,7 @@ Changelog * Added a new datetime picker widget * Added styleguide (mainly for wagtail developers) * Aesthetic improvements to preview experience + * Added an 'attrs' property to image rendition objects to output src, width, height and alt attributes all in one go * Fix: Animated GIFs are now coalesced before resizing * Fix: Wand backend clones images before modifying them * Fix: Admin breadcrumb now positioned correctly on mobile