Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set alt attribute in tag method by default #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/76.feature
Original file line number Diff line number Diff line change
@@ -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]
10 changes: 5 additions & 5 deletions plone/namedfile/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -110,7 +109,7 @@ def tag(self, height=_marker, width=_marker, alt=_marker,

parts = ['<img']
for k, v in values:
if v is None:
if v in [None, _marker]:
continue
if isinstance(v, int):
v = str(v)
Expand Down Expand Up @@ -334,7 +333,8 @@ def traverse(self, name, furtherPath):
return ImmutableTraverser(self.scale(name, furtherPath[-1]))

if image is not None:
return image.tag()
alt_text = getattr(self.context, 'alt_text', '')
return image.tag(alt=alt_text, title=None)
raise TraversalError(self, name)

_sizes = {}
Expand Down
71 changes: 62 additions & 9 deletions plone/namedfile/tests/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def testGetScaleByName(self):
base = self.item.absolute_url()
expected = \
r'<img src="{0}/@@images/([-0-9a-f]{{36}}).(jpeg|gif|png)" ' \
r'alt="foo" title="foo" height="(\d+)" width="(\d+)" />'.format(
r'alt="" height="(\d+)" width="(\d+)" />'.format(
base,
)
groups = re.match(expected, tag).groups()
Expand All @@ -145,7 +145,7 @@ def testGetHighPixelDensityScaleByName(self):
r'<img src="{0}'.format(base) +
r'/@@images/([-0-9a-f]{36})'
r'.(jpeg|gif|png)" '
r'alt="foo" title="foo" height="(\d+)" width="(\d+)" '
r'alt="" height="(\d+)" width="(\d+)" '
r'srcset="http://nohost/item/@@images/([-0-9a-f]{36})'
r'.(jpeg|gif|png)'
r' 2x" />')
Expand All @@ -167,7 +167,7 @@ def testGetRetinaScaleByWidthAndHeight(self):
r'<img src="{0}'.format(base) +
r'/@@images/([-0-9a-f]{36})'
r'.(jpeg|gif|png)" '
r'alt="foo" title="foo" height="(\d+)" width="(\d+)" '
r'alt="" height="(\d+)" width="(\d+)" '
r'srcset="http://nohost/item/@@images/([-0-9a-f]{36})'
r'.(jpeg|gif|png)'
r' 2x" />')
Expand All @@ -189,7 +189,7 @@ def testGetRetinaScaleByWidthOnly(self):
r'<img src="{0}'.format(base) +
r'/@@images/([-0-9a-f]{36})'
r'.(jpeg|gif|png)" '
r'alt="foo" title="foo" height="(\d+)" width="(\d+)" '
r'alt="" height="(\d+)" width="(\d+)" '
r'srcset="http://nohost/item/@@images/([-0-9a-f]{36})'
r'.(jpeg|gif|png)'
r' 2x" />')
Expand All @@ -211,7 +211,7 @@ def testGetRetinaScaleByHeightOnly(self):
r'<img src="{0}'.format(base) +
r'/@@images/([-0-9a-f]{36})'
r'.(jpeg|gif|png)" '
r'alt="foo" title="foo" height="(\d+)" width="(\d+)" '
r'alt="" height="(\d+)" width="(\d+)" '
r'srcset="http://nohost/item/@@images/([-0-9a-f]{36})'
r'.(jpeg|gif|png)'
r' 2x" />')
Expand Down Expand Up @@ -282,7 +282,7 @@ def testGetOriginalScaleTag(self):
base = self.item.absolute_url()
expected = \
r'<img src="{0}/@@images/([-0-9a-f]{{36}}).(jpeg|gif|png)" ' \
r'alt="foo" title="foo" height="(\d+)" width="(\d+)" />'.format(
r'alt="" height="(\d+)" width="(\d+)" />'.format(
base,
)
self.assertTrue(re.match(expected, tag).groups())
Expand All @@ -293,7 +293,7 @@ def testScaleOnItemWithNonASCIITitle(self):
base = self.item.absolute_url()
expected = \
r'<img src="{0}/@@images/([-0-9a-f]{{36}}).(jpeg|gif|png)" ' \
r'alt="\xfc" title="\xfc" height="(\d+)" width="(\d+)" />'.format(
r'alt="" height="(\d+)" width="(\d+)" />'.format(
base,
)
self.assertTrue(re.match(expected, tag).groups())
Expand All @@ -304,7 +304,7 @@ def testScaleOnItemWithUnicodeTitle(self):
base = self.item.absolute_url()
expected = \
r'<img src="{0}/@@images/([-0-9a-f]{{36}}).(jpeg|gif|png)" ' \
r'alt="\xfc" title="\xfc" height="(\d+)" width="(\d+)" />'.format(
r'alt="" height="(\d+)" width="(\d+)" />'.format(
base,
)
self.assertTrue(re.match(expected, tag).groups())
Expand Down Expand Up @@ -369,7 +369,7 @@ def traverse(self, path=''):
base = self.item.absolute_url()
expected = \
r'<img src="{0}/@@images/([-0-9a-f]{{36}}).(jpeg|gif|png)" ' \
r'alt="foo" title="foo" height="(\d+)" width="(\d+)" />'.format(
r'alt="" height="(\d+)" width="(\d+)" />'.format(
base,
)
groups = re.match(expected, tag).groups()
Expand Down Expand Up @@ -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'<img src="{0}/@@images/([-0-9a-f]{{36}}).(jpeg|gif|png)" ' \
r'alt="example alt" height="(\d+)" width="(\d+)" />'.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'<img src="{0}/@@images/([-0-9a-f]{{36}}).(jpeg|gif|png)" ' \
r'alt="custom alt" height="(\d+)" width="(\d+)" />'.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'<img src="{0}/@@images/([-0-9a-f]{{36}}).(jpeg|gif|png)" ' \
r'alt="example alt" title="custom title" height="(\d+)" width="(\d+)" />'.format(
base,
)
groups = re.match(expected, tag).groups()
self.assertTrue(groups, tag)


def test_suite():
from unittest import defaultTestLoader
return defaultTestLoader.loadTestsFromName(__name__)