class TestFormat(TestCase): def setUp(self): # test format self.format = Format('test name', 'test label', 'test classnames', 'test filter spec') # test image self.image = MagicMock() self.image.id = 0 def test_editor_attributes(self): result = self.format.editor_attributes(self.image, 'test alt text') self.assertEqual( result, 'data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" ' ) def test_image_to_editor_html(self): result = self.format.image_to_editor_html(self.image, 'test alt text') self.assertRegex( result, '<img data-embedtype="image" data-id="0" data-format="test name" ' 'data-alt="test alt text" class="test classnames" src="[^"]+" width="1" height="1" alt="test alt text">', ) 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, '<img data-embedtype="image" data-id="0" data-format="test name" ' 'data-alt="Arthur "two sheds" Jackson" class="test classnames" src="[^"]+" width="1" height="1" alt="Arthur "two sheds" Jackson">', ) 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, '<img src="[^"]+" width="1" height="1" alt="test alt text">') 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, '<img class="test classnames" src="[^"]+" width="1" height="1" alt="Arthur "two sheds" Jackson">' ) def test_get_image_format(self): register_image_format(self.format) result = get_image_format('test name') self.assertEqual(result, self.format)
def image_embedtype_handler(attrs, images=[]): """ Reimplementation of wagtail.images.rich_text.image_embedtype_handler (v2) Turns an <embed>'s attributes (attrs) into a valid <img> tag. Uses an in-memory list of Images (images) to try and find the image locally before querying the database. """ Image = get_image_model() # noqa: N806 image_id = attrs.get('id', '') alt_text = attrs.get('alt', '') width = attrs.get('width', '') # Handle the case where the embed has a blank or missing 'id' attribute # eg. <embed embedtype="news-image" id=""/><embed> if not str.isdigit(image_id): logger.error('Image embed does not have a valid id: {}'.format(attrs)) return '<img>' # Try to efficiently fetch image from local memory image = None for img in images: if str(img.pk) == image_id: image = img # Default to fetching the image from the database if it is not found in images if not image: try: image = Image.objects.prefetch_related('renditions').get( id=image_id) except Image.DoesNotExist: return '<img>' # Figure out how we will format the image rendition if str.isdigit(width): # Use a custom width rendition if the embed has a width specified filter_spec = 'width-{}'.format(width) image_format = Format( filter_spec=filter_spec, classnames='richtext-image', # Unused required fields name='_', label='_', ) else: # Use the default 'fullwidth' rendition if no width is specified format_name = attrs.get('format', 'fullwidth') image_format = formats.get_image_format(format_name) return image_format.image_to_html(image, alt_text)
def test_image_to_html(self): cif = CaptionedImageFormat( "captioned_center", "Centered captioned", "bodytext-image", "width-800" ) test_file = get_test_image_file() image = Image.objects.create( title="Test image", file=test_file, ) alt_text = "An example image" default_html = Format.image_to_html(cif, image, alt_text) html = cif.image_to_html(image, alt_text) assert ( html == '<figure class="landscape">%s<figcaption>%s</figcaption></figure>' % ( default_html, alt_text, ) )
class TestFormat(TestCase, WagtailTestUtils): def setUp(self): # test format self.format = Format("test name", "test label", "test classnames", "original") # test image self.image = Image.objects.create( title="Test image", file=get_test_image_file(), ) def test_editor_attributes(self): result = self.format.editor_attributes(self.image, "test alt text") self.assertEqual( result, { "data-alt": "test alt text", "data-embedtype": "image", "data-format": "test name", "data-id": self.image.pk, }, ) def test_image_to_editor_html(self): result = self.format.image_to_editor_html(self.image, "test alt text") self.assertTagInHTML( '<img data-embedtype="image" data-id="%d" data-format="test name" ' 'data-alt="test alt text" class="test classnames" ' 'width="640" height="480" alt="test alt text" >' % self.image.pk, 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') expected_html = ( '<img data-embedtype="image" data-id="%d" data-format="test name" ' 'data-alt="Arthur "two sheds" Jackson" class="test classnames" ' 'width="640" height="480" alt="Arthur "two sheds" Jackson" >' % self.image.pk) self.assertTagInHTML(expected_html, 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.assertTagInHTML( '<img width="640" height="480" alt="test alt text">', 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.assertTagInHTML( '<img class="test classnames" width="640" height="480" ' 'alt="Arthur "two sheds" Jackson">', result, allow_extra_attrs=True, ) def test_get_image_format(self): register_image_format(self.format) result = get_image_format("test name") self.assertEqual(result, self.format)
class TestFormat(TestCase): def setUp(self): # test format self.format = Format( 'test name', 'test label', 'test classnames', 'test filter spec' ) # test image self.image = MagicMock() self.image.id = 0 def test_editor_attributes(self): result = self.format.editor_attributes( self.image, 'test alt text' ) self.assertEqual(result, 'data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" ') def test_image_to_editor_html(self): result = self.format.image_to_editor_html( self.image, 'test alt text' ) self.assertRegex( result, '<img data-embedtype="image" data-id="0" data-format="test name" ' 'data-alt="test alt text" class="test classnames" src="[^"]+" width="1" height="1" alt="test alt text">', ) 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, '<img data-embedtype="image" data-id="0" data-format="test name" ' 'data-alt="Arthur "two sheds" Jackson" class="test classnames" src="[^"]+" width="1" height="1" alt="Arthur "two sheds" Jackson">', ) 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, '<img src="[^"]+" width="1" height="1" alt="test alt text">' ) 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, '<img class="test classnames" src="[^"]+" width="1" height="1" alt="Arthur "two sheds" Jackson">' ) def test_get_image_format(self): register_image_format(self.format) result = get_image_format('test name') self.assertEqual(result, self.format)
class TestFormat(TestCase, WagtailTestUtils): def setUp(self): # test format self.format = Format( 'test name', 'test label', 'test classnames', 'original' ) # test image self.image = Image.objects.create( title="Test image", file=get_test_image_file(), ) def test_editor_attributes(self): result = self.format.editor_attributes( self.image, 'test alt text' ) self.assertEqual(result, {'data-alt': 'test alt text', 'data-embedtype': 'image', 'data-format': 'test name', 'data-id': self.image.pk}) def test_image_to_editor_html(self): result = self.format.image_to_editor_html( self.image, 'test alt text' ) self.assertTagInHTML( '<img data-embedtype="image" data-id="%d" data-format="test name" ' 'data-alt="test alt text" class="test classnames" ' 'width="640" height="480" alt="test alt text" >' % self.image.pk, 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' ) expected_html = ( '<img data-embedtype="image" data-id="%d" data-format="test name" ' 'data-alt="Arthur "two sheds" Jackson" class="test classnames" ' 'width="640" height="480" alt="Arthur "two sheds" Jackson" >' % self.image.pk ) self.assertTagInHTML(expected_html, 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.assertTagInHTML( '<img width="640" height="480" alt="test alt text">', 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.assertTagInHTML( '<img class="test classnames" width="640" height="480" ' 'alt="Arthur "two sheds" Jackson">', result, allow_extra_attrs=True) def test_get_image_format(self): register_image_format(self.format) result = get_image_format('test name') self.assertEqual(result, self.format)