[A11y] make text "required" visible in labels (#5042)

This commit is contained in:
Richard Schreiber
2025-05-05 11:40:35 +02:00
committed by GitHub
parent c53d44238c
commit f09e9590a8
2 changed files with 20 additions and 5 deletions

View File

@@ -33,6 +33,7 @@ def render_label(content, label_for=None, label_class=None, label_title='', labe
"""
Render a label with content
"""
tag = 'label'
attrs = attrs or {}
if label_for:
attrs['for'] = label_for
@@ -58,15 +59,16 @@ def render_label(content, label_for=None, label_class=None, label_title='', labe
attrs['class'] += ' label-empty'
# usually checkboxes have overall empty labels and special labels per checkbox
# => remove for-attribute as well as "required"-text appended to label
tag = 'div'
if 'for' in attrs:
del attrs['for']
elif not optional:
opt += '<i class="sr-only label-required">, {}</i>'.format(pgettext('form', 'required'))
opt += '<i class="label-required">{}</i>'.format(pgettext('form', 'required'))
builder = '<{tag}{attrs}>{content}{opt}</{tag}>'
return format_html(
builder,
tag='label',
tag=tag,
attrs=mark_safe(flatatt(attrs)) if attrs else '',
opt=mark_safe(opt),
content=text_value(content),