a11y: add aria-describedby to file links in summary page (#71092) #298

Merged
fpeters merged 1 commits from wip/71092-file-link-summary-describedby into main 2023-05-12 09:21:13 +02:00
6 changed files with 74 additions and 12 deletions

View File

@ -768,8 +768,8 @@ def test_backoffice_submission_live_condition(pub):
assert 'name="f2"' not in resp.text
resp = resp.form.submit('submit')
resp = resp.follow()
assert '<p class="label">Bar</p>' in resp.text
assert '<p class="label">Foo</p>' not in resp.text
assert 'Bar' in [x.text for x in resp.pyquery('p.label')]
assert 'Foo' not in [x.text for x in resp.pyquery('p.label')]
def test_backoffice_submission_conditional_jump_based_on_bo_field(pub):

View File

@ -7992,8 +7992,8 @@ def test_field_condition(pub):
assert 'name="f2"' not in resp.text
resp = resp.form.submit('submit')
resp = resp.follow()
assert '<p class="label">Bar</p>' in resp.text
assert '<p class="label">Foo</p>' not in resp.text
assert 'Bar' in [x.text for x in resp.pyquery('p.label')]
assert 'Foo' not in [x.text for x in resp.pyquery('p.label')]
def test_field_unicode_condition(pub):

View File

@ -7,6 +7,7 @@ import responses
from webtest import Upload
from wcs import fields
from wcs.blocks import BlockDef
from wcs.categories import Category
from wcs.formdef import FormDef
from wcs.qommon.errors import ConnectionError
@ -486,3 +487,53 @@ def test_form_file_svg_thumbnail(pub):
svg_resp = svg_resp.follow()
assert svg_resp.body == SVG_CONTENT
assert svg_resp.headers['Content-Type'] == 'image/svg+xml'
def test_form_file_field_aria_description(pub):
FormDef.wipe()
formdef = FormDef()
formdef.name = 'test'
formdef.fields = [fields.FileField(id='0', label='field label')]
formdef.store()
formdef.data_class().wipe()
upload = Upload('test.txt', b'foobar', 'text/plain')
resp = get_app(pub).get('/test/')
resp.forms[0]['f0$file'] = upload
resp = resp.forms[0].submit('submit') # -> validation
resp = resp.forms[0].submit('submit') # -> submit
resp = resp.follow()
assert (
resp.pyquery.find('#' + resp.pyquery('[aria-describedby]').attr['aria-describedby']).text()
== 'field label'
)
def test_form_file_field_in_block_aria_description(pub):
BlockDef.wipe()
block = BlockDef()
block.name = 'foobar'
block.fields = [
fields.FileField(id='234', required=True, label='field label', type='file'),
]
block.store()
FormDef.wipe()
formdef = FormDef()
formdef.name = 'test'
formdef.fields = [fields.BlockField(id='1', label='test', type='block:foobar', max_items=3)]
formdef.store()
formdef.data_class().wipe()
upload = Upload('test.txt', b'foobar', 'text/plain')
resp = get_app(pub).get('/test/')
resp.forms[0]['f1$element0$f234$file'] = upload
resp = resp.forms[0].submit('submit') # -> validation
resp = resp.forms[0].submit('submit') # -> submit
resp = resp.follow()
assert (
resp.pyquery.find('#' + resp.pyquery('[aria-describedby]').attr['aria-describedby']).text()
== 'field label'
)

View File

@ -81,8 +81,8 @@ def test_field_live_condition(pub):
assert 'name="f2"' not in resp.text
resp = resp.form.submit('submit')
resp = resp.follow()
assert '<p class="label">Bar</p>' in resp.text
assert '<p class="label">Foo</p>' not in resp.text
assert 'Bar' in [x.text for x in resp.pyquery('p.label')]
assert 'Foo' not in [x.text for x in resp.pyquery('p.label')]
resp = get_app(pub).get('/foo/')
assert 'f1' in resp.form.fields
@ -98,8 +98,8 @@ def test_field_live_condition(pub):
assert 'name="f2"' in resp.text
resp = resp.form.submit('submit')
resp = resp.follow()
assert '<p class="label">Bar</p>' in resp.text
assert '<p class="label">Foo</p>' in resp.text
assert 'Bar' in [x.text for x in resp.pyquery('p.label')]
assert 'Foo' in [x.text for x in resp.pyquery('p.label')]
def test_field_live_condition_on_other_page(pub):

View File

@ -1799,6 +1799,8 @@ class FileField(WidgetField):
attrs = {
'href': '[download]?%s' % download_qs,
}
if kwargs.get('label_id'):
attrs['aria-describedby'] = kwargs.get('label_id')
if max_len:
attrs['title'] = value
t += htmltag('a', **attrs)
@ -4115,13 +4117,14 @@ class BlockField(WidgetField):
sub_value, sub_value_details = field.get_value_info(row_value)
if sub_value is None and not (field.required and include_unset_required_fields):
continue
label_id = f'form-field-label-f{self.id}-r{i}-s{field.id}'
r += htmltext('<div class="%s">' % ' '.join(css_classes))
r += htmltext('<p class="label">%s</p> ') % field.label
r += htmltext('<p id="%s" class="label">%s</p> ') % (label_id, field.label)
if sub_value is None:
r += htmltext('<div class="value"><i>%s</i></div>') % _('Not set')
else:
r += htmltext('<div class="value">')
kwargs = {'parent_field': self, 'parent_field_index': i}
kwargs = {'parent_field': self, 'parent_field_index': i, 'label_id': label_id}
kwargs.update(**sub_value_details)
r += field.get_view_value(sub_value, **kwargs)
r += htmltext('</div>')

View File

@ -595,11 +595,19 @@ class FormStatusPage(Directory, FormTemplateMixin):
if f.extra_css_class:
css_classes.append(f.extra_css_class)
r += htmltext('<div class="%s">' % ' '.join(css_classes))
label_id = f'form-field-label-f{f.id}'
if f.key == 'block' and f.label_display == 'subtitle':
r += htmltext('<div class="subtitle"><h4>%s</h4></div>') % get_publisher().translate(f.label)
r += htmltext('<div id="%s" class="subtitle"><h4>%s</h4></div>') % (
label_id,
get_publisher().translate(f.label),
)
elif not (f.key == 'block' and f.label_display == 'hidden'):
r += htmltext('<p class="label">%s</p> ') % get_publisher().translate(f.label)
r += htmltext('<p id="%s" class="label">%s</p> ') % (
label_id,
get_publisher().translate(f.label),
)
value, value_details = field_value_info['value'], field_value_info['value_details']
value_details['label_id'] = label_id
if value is None:
if not (f.key == 'block' and f.label_display == 'hidden'):
r += htmltext('<div class="value"><i>%s</i></div>') % _('Not set')