From e5004cf57a184e802367f8f5e60b863e7722216f Mon Sep 17 00:00:00 2001 From: jackahl Date: Wed, 25 Sep 2024 19:02:22 +0200 Subject: [PATCH] Add alt_text field to image content type --- news/700.feature | 2 + .../contenttypes/browser/templates/image.pt | 3 +- plone/app/contenttypes/schema/image.xml | 14 +++++++ plone/app/contenttypes/tests/test_image.py | 41 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 news/700.feature diff --git a/news/700.feature b/news/700.feature new file mode 100644 index 00000000..b73a4c8e --- /dev/null +++ b/news/700.feature @@ -0,0 +1,2 @@ +Add alt_text field to image content type. This allows users to manually set the value of alt tag, for usage in automated listings. +[@cekk, @jackahl] diff --git a/plone/app/contenttypes/browser/templates/image.pt b/plone/app/contenttypes/browser/templates/image.pt index cb77820f..2f75f1f3 100644 --- a/plone/app/contenttypes/browser/templates/image.pt +++ b/plone/app/contenttypes/browser/templates/image.pt @@ -27,7 +27,8 @@
Description + + + Briefly describe the meaning of the image for people using assistive technology like screen readers. + This will be used when the image is viewed by itself or in automated contexts like listings. + Do not duplicate the Title or Description fields, since those might also be read by screen readers. + Alt text should describe what a sighted user sees when looking at the image. + This might include text the image contains, or even a description of an abstract pattern. + In case your description already sufficiently describes your image leave this field blank. + + False + Alt Text + + diff --git a/plone/app/contenttypes/tests/test_image.py b/plone/app/contenttypes/tests/test_image.py index b31a514a..e187bc94 100644 --- a/plone/app/contenttypes/tests/test_image.py +++ b/plone/app/contenttypes/tests/test_image.py @@ -72,7 +72,16 @@ def setUp(self): image.title = "My Image" image.description = "This is my image." image.image = dummy_image() + + self.portal.invokeFactory('Image', 'image-with-alt') + image_alt = self.portal['image-with-alt'] + image_alt.title = 'My Image 2' + image_alt.description = 'This is my second image.' + image_alt.alt_text = 'An alt text' + image.image = dummy_image() + self.image = image + self.image_alt = image_alt self.request.set("URL", image.absolute_url()) self.request.set("ACTUAL_URL", image.absolute_url()) alsoProvides(self.request, IPloneFormLayer) @@ -85,6 +94,30 @@ def test_image_view(self): self.assertTrue("My Image" in view()) self.assertTrue("This is my image." in view()) + def test_image_view_alt(self): + view = self.image_alt.restrictedTraverse('@@view') + + self.assertTrue(view()) + self.assertEqual(view.request.response.status, 200) + self.assertTrue('My Image 2' in view()) + self.assertTrue('This is my second image.' in view()) + self.assertTrue('An alt text' in view()) + + def test_image_alt_in_listing_view(self): + self.image_alt.image = dummy_image(u'image.svg') + view = self.portal.restrictedTraverse('@@listing_view') + self.assertTrue('An alt text' in view()) + + def test_image_alt_in_summary_view(self): + self.image_alt.image = dummy_image(u'image.svg') + view = self.portal.restrictedTraverse('@@summary_view') + self.assertTrue('An alt text' in view()) + + def test_image_alt_in_album_view(self): + self.image_alt.image = dummy_image(u'image.svg') + view = self.portal.restrictedTraverse('@@album_view') + self.assertTrue('An alt text' in view()) + # XXX: Not working. See ImageFunctionalTest test_image_view_fullscreen # Problem seems to be that the image is not properly uploaded. # def test_image_view_fullscreen(self): @@ -105,6 +138,14 @@ def test_svg_image(self): r'My Image', # noqa: E501 ) + def test_svg_image_alt(self): + self.image_alt.image = dummy_image(u'image.svg') + scale = self.image_alt.restrictedTraverse('@@images') + self.assertRegex( + scale.scale('image', scale='large').tag(), + r'An alt text' # noqa: E501 + ) + class ImageFunctionalTest(unittest.TestCase): layer = PLONE_APP_CONTENTTYPES_FUNCTIONAL_TESTING