diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst
index 47f027d52..84e70d970 100644
--- a/CONTRIBUTORS.rst
+++ b/CONTRIBUTORS.rst
@@ -272,6 +272,7 @@ Contributors
* J Rob Gant
* Mary Kate Fain
* Dário Marcelino
+* Dave Bell
Translators
===========
diff --git a/docs/releases/2.1.rst b/docs/releases/2.1.rst
index 0cee1720d..98c769b7c 100644
--- a/docs/releases/2.1.rst
+++ b/docs/releases/2.1.rst
@@ -23,3 +23,8 @@ Bug fixes
Upgrade considerations
======================
+
+Image format ``image_to_html`` method has been updated
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+The internal API for rich text image format objects (see :ref:`rich_text_image_formats`) has been updated; the ``Format.image_to_html`` method now receives the ``extra_attributes`` keyword argument as a dictionary of attributes, rather than a string. If you have defined any custom format objects that override this method, these will need to be updated.
diff --git a/wagtail/images/formats.py b/wagtail/images/formats.py
index 774284f04..75e1b18c2 100644
--- a/wagtail/images/formats.py
+++ b/wagtail/images/formats.py
@@ -15,30 +15,31 @@ class Format:
def editor_attributes(self, image, alt_text):
"""
- Return string of additional attributes to go on the HTML element
+ Return additional attributes to go on the HTML element
when outputting this image within a rich text editor field
"""
- return 'data-embedtype="image" data-id="%d" data-format="%s" data-alt="%s" ' % (
- image.id, self.name, escape(alt_text)
- )
+ return {
+ 'data-embedtype': "image",
+ 'data-id': image.id,
+ 'data-format': self.name,
+ 'data-alt': escape(alt_text),
+ }
def image_to_editor_html(self, image, alt_text):
return self.image_to_html(
image, alt_text, self.editor_attributes(image, alt_text)
)
- def image_to_html(self, image, alt_text, extra_attributes=''):
+ def image_to_html(self, image, alt_text, extra_attributes=None):
+ if extra_attributes is None:
+ extra_attributes = {}
rendition = get_rendition_or_not_found(image, self.filter_spec)
+ extra_attributes['alt'] = escape(alt_text)
if self.classnames:
- class_attr = 'class="%s" ' % escape(self.classnames)
- else:
- class_attr = ''
+ extra_attributes['class'] = "%s" % escape(self.classnames)
- return '
' % (
- extra_attributes, class_attr,
- escape(rendition.url), rendition.width, rendition.height, escape(alt_text)
- )
+ return rendition.img_tag(extra_attributes)
FORMATS = []
diff --git a/wagtail/images/tests/test_rich_text.py b/wagtail/images/tests/test_rich_text.py
index 0d827d340..fc4cafcfd 100644
--- a/wagtail/images/tests/test_rich_text.py
+++ b/wagtail/images/tests/test_rich_text.py
@@ -2,11 +2,12 @@ from bs4 import BeautifulSoup
from django.test import TestCase
from wagtail.images.rich_text import ImageEmbedHandler, image_embedtype_handler
+from wagtail.tests.utils import WagtailTestUtils
from .utils import Image, get_test_image_file
-class TestImageEmbedHandler(TestCase):
+class TestImageEmbedHandler(TestCase, WagtailTestUtils):
def test_get_db_attributes(self):
soup = BeautifulSoup(
'foo',
@@ -30,7 +31,7 @@ class TestImageEmbedHandler(TestCase):
'alt': 'test-alt',
'format': 'left'}
)
- self.assertIn('
', result, allow_extra_attrs=True)
def test_expand_db_attributes_escapes_alt_text(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
@@ -47,8 +48,7 @@ class TestImageEmbedHandler(TestCase):
{'id': 1,
'format': 'left'},
)
- self.assertIn('
', result, allow_extra_attrs=True)
def test_expand_db_attributes_for_editor(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
@@ -57,10 +57,9 @@ class TestImageEmbedHandler(TestCase):
'alt': 'test-alt',
'format': 'left'},
)
- self.assertIn(
+ self.assertTagInHTML(
'
', result, allow_extra_attrs=True)
def test_expand_db_attributes_for_editor_escapes_alt_text(self):
Image.objects.create(id=1, title='Test', file=get_test_image_file())
@@ -69,10 +68,11 @@ class TestImageEmbedHandler(TestCase):
'alt': 'Arthur "two sheds" Jackson',
'format': 'left'},
)
- self.assertIn(
+ self.assertTagInHTML(
'
',
+ result, allow_extra_attrs=True)
+
self.assertIn('alt="Arthur "two sheds" Jackson"', result)
def test_expand_db_attributes_for_editor_with_missing_alt(self):
@@ -81,7 +81,6 @@ class TestImageEmbedHandler(TestCase):
{'id': 1,
'format': 'left'},
)
- self.assertIn(
- '
', result, allow_extra_attrs=True)
diff --git a/wagtail/images/tests/tests.py b/wagtail/images/tests/tests.py
index c35d67084..b335738c7 100644
--- a/wagtail/images/tests/tests.py
+++ b/wagtail/images/tests/tests.py
@@ -6,7 +6,6 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings
from django.urls import reverse
-from mock import MagicMock
from taggit.forms import TagField, TagWidget
from wagtail.images import get_image_model, get_image_model_string
@@ -176,18 +175,20 @@ class TestMissingImage(TestCase):
)
-class TestFormat(TestCase):
+class TestFormat(TestCase, WagtailTestUtils):
def setUp(self):
# test format
self.format = Format(
'test name',
'test label',
'test classnames',
- 'test filter spec'
+ 'original'
)
# test image
- self.image = MagicMock()
- self.image.id = 0
+ self.image = Image.objects.create(
+ title="Test image",
+ file=get_test_image_file(),
+ )
def test_editor_attributes(self):
result = self.format.editor_attributes(
@@ -195,45 +196,43 @@ class TestFormat(TestCase):
'test alt text'
)
self.assertEqual(result,
- 'data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" ')
+ {'data-alt': 'test alt text', 'data-embedtype': 'image',
+ 'data-format': 'test name', 'data-id': 1})
def test_image_to_editor_html(self):
result = self.format.image_to_editor_html(
self.image,
'test alt text'
)
- self.assertRegex(
- result,
- '
',
- )
+ self.assertTagInHTML(
+ '
', result, allow_extra_attrs=True)
def test_image_to_editor_html_with_quoting(self):
result = self.format.image_to_editor_html(
self.image,
'Arthur "two sheds" Jackson'
)
- self.assertRegex(
- result,
- '
',
- )
+ self.assertTagInHTML(
+ '
',
+ result, allow_extra_attrs=True)
+
def test_image_to_html_no_classnames(self):
self.format.classnames = None
result = self.format.image_to_html(self.image, 'test alt text')
- self.assertRegex(
- result,
- '
'
- )
+ self.assertTagInHTML(
+ '
', result, allow_extra_attrs=True)
self.format.classnames = 'test classnames'
def test_image_to_html_with_quoting(self):
result = self.format.image_to_html(self.image, 'Arthur "two sheds" Jackson')
- self.assertRegex(
- result,
- '
'
- )
+ self.assertTagInHTML(
+ '
', result, allow_extra_attrs=True)
def test_get_image_format(self):
register_image_format(self.format)