From 514a3772b4207cc3585afe5fbc26657c08de5056 Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Thu, 18 Apr 2019 09:08:37 +0200 Subject: [PATCH 1/2] don't pass title by default in scale method, and pass alt text value --- news/76.feature | 2 + plone/namedfile/scaling.py | 10 ++-- plone/namedfile/tests/test_scaling.py | 71 +++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 news/76.feature diff --git a/news/76.feature b/news/76.feature new file mode 100644 index 00000000..f38b4bee --- /dev/null +++ b/news/76.feature @@ -0,0 +1,2 @@ +Change scale method logic: don't return a redundant title attr for images, but set `alt` attribute from a new alt_text field. +[cekk] diff --git a/plone/namedfile/scaling.py b/plone/namedfile/scaling.py index 62b4a8c6..3c233890 100644 --- a/plone/namedfile/scaling.py +++ b/plone/namedfile/scaling.py @@ -83,15 +83,14 @@ def tag(self, height=_marker, width=_marker, alt=_marker, css_class=None, title=_marker, **kwargs): """Create a tag including scale """ + if height is _marker: height = getattr(self, 'height', self.data._height) if width is _marker: width = getattr(self, 'width', self.data._width) if alt is _marker: - alt = self.context.Title() - if title is _marker: - title = self.context.Title() + alt = getattr(self.context, 'alt_text', '') values = [ ('src', self.url), @@ -110,7 +109,7 @@ def tag(self, height=_marker, width=_marker, alt=_marker, parts = [''.format( + r'alt="" height="(\d+)" width="(\d+)" />'.format( base, ) groups = re.match(expected, tag).groups() @@ -145,7 +145,7 @@ def testGetHighPixelDensityScaleByName(self): r'') @@ -167,7 +167,7 @@ def testGetRetinaScaleByWidthAndHeight(self): r'') @@ -189,7 +189,7 @@ def testGetRetinaScaleByWidthOnly(self): r'') @@ -211,7 +211,7 @@ def testGetRetinaScaleByHeightOnly(self): r'') @@ -282,7 +282,7 @@ def testGetOriginalScaleTag(self): base = self.item.absolute_url() expected = \ r''.format( + r'alt="" height="(\d+)" width="(\d+)" />'.format( base, ) self.assertTrue(re.match(expected, tag).groups()) @@ -293,7 +293,7 @@ def testScaleOnItemWithNonASCIITitle(self): base = self.item.absolute_url() expected = \ r''.format( + r'alt="" height="(\d+)" width="(\d+)" />'.format( base, ) self.assertTrue(re.match(expected, tag).groups()) @@ -304,7 +304,7 @@ def testScaleOnItemWithUnicodeTitle(self): base = self.item.absolute_url() expected = \ r''.format( + r'alt="" height="(\d+)" width="(\d+)" />'.format( base, ) self.assertTrue(re.match(expected, tag).groups()) @@ -369,7 +369,7 @@ def traverse(self, path=''): base = self.item.absolute_url() expected = \ r''.format( + r'alt="" height="(\d+)" width="(\d+)" />'.format( base, ) groups = re.match(expected, tag).groups() @@ -428,6 +428,59 @@ def testGuardedAccess(self): self.item.__allow_access_to_unprotected_subobjects__ = 1 +class ImageScalingAltTextTests(unittest.TestCase): + + layer = PLONE_NAMEDFILE_INTEGRATION_TESTING + + def setUp(self): + data = getFile('image.png') + item = DummyContent() + item.image = NamedImage(data, 'image/png', u'image.png') + item.alt_text = 'example alt' + self.layer['app']._setOb('item', item) + self.item = self.layer['app'].item + self.scaling = ImageScaling(self.item, None) + + def testShowAltInScale(self): + self.scaling.available_sizes = {'foo': (60, 60)} + foo = self.scaling.scale('image', scale='foo') + tag = foo.tag() + base = self.item.absolute_url() + expected = \ + r''.format( + base, + ) + groups = re.match(expected, tag).groups() + self.assertTrue(groups, tag) + + def testShowCustomAltInScale(self): + self.scaling.available_sizes = {'foo': (60, 60)} + foo = self.scaling.scale('image', scale='foo') + tag = foo.tag(alt='custom alt') + base = self.item.absolute_url() + expected = \ + r''.format( + base, + ) + groups = re.match(expected, tag).groups() + self.assertTrue(groups, tag) + + def testShowTitleIfPassed(self): + self.scaling.available_sizes = {'foo': (60, 60)} + foo = self.scaling.scale('image', scale='foo') + tag = foo.tag(title='custom title') + base = self.item.absolute_url() + expected = \ + r''.format( + base, + ) + groups = re.match(expected, tag).groups() + self.assertTrue(groups, tag) + + def test_suite(): from unittest import defaultTestLoader return defaultTestLoader.loadTestsFromName(__name__) From f78aba5012491985fb0bc274e67fec0847f6e40c Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Thu, 18 Apr 2019 09:15:33 +0200 Subject: [PATCH 2/2] fix changelog entry --- news/76.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/76.feature b/news/76.feature index f38b4bee..a1a41d10 100644 --- a/news/76.feature +++ b/news/76.feature @@ -1,2 +1,2 @@ -Change scale method logic: don't return a redundant title attr for images, but set `alt` attribute from a new alt_text field. +Change tag method logic: don't return a useless title attr for images, but set `alt` attribute from a new alt_text field. [cekk]