diff --git a/news/76.feature b/news/76.feature new file mode 100644 index 00000000..a1a41d10 --- /dev/null +++ b/news/76.feature @@ -0,0 +1,2 @@ +Change tag method logic: don't return a useless 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__)