| 1 |
from ..errors import CompileWarning |
| 2 |
from ..language import ExtAccessibility, Property |
| 3 |
from .utils import LinterRule |
| 4 |
|
| 5 |
|
| 6 |
class RequireA11yLabel(LinterRule): |
| 7 |
id = "missing_descriptive_text" |
| 8 |
severity = "suggestion" |
| 9 |
category = "a11y" |
| 10 |
|
| 11 |
def check(self, type, child, stack): |
| 12 |
# rule suggestion/require-a11y-label |
| 13 |
properties = child.content.children[Property] |
| 14 |
if type == "Gtk.Button": |
| 15 |
label = None |
| 16 |
tooltip_text = None |
| 17 |
accessibility_label = False |
| 18 |
|
| 19 |
# FIXME: Check what ATs actually do |
| 20 |
|
| 21 |
for property in properties: |
| 22 |
if property.name == "label": |
| 23 |
label = property.value |
| 24 |
elif property.name == "tooltip-text": |
| 25 |
tooltip_text = property.value |
| 26 |
|
| 27 |
accessibility_child = child.content.children[ExtAccessibility] |
| 28 |
if len(accessibility_child) > 0: |
| 29 |
accessibility_properties = child.content.children[ExtAccessibility][ |
| 30 |
0 |
| 31 |
].properties |
| 32 |
for accessibility_property in accessibility_properties: |
| 33 |
if accessibility_property.name in ("label", "labelled-by"): |
| 34 |
accessibility_label = True |
| 35 |
|
| 36 |
if label is None and tooltip_text is None and accessibility_label is False: |
| 37 |
self.problems.append( |
| 38 |
CompileWarning( |
| 39 |
f"{type} is missing an accessibility label", |
| 40 |
child.signature_range, |
| 41 |
id=self.id, |
| 42 |
) |
| 43 |
) |
| 44 |
|
| 45 |
# rule suggestion/require-a11y-label |
| 46 |
elif type == "Gtk.Image" or type == "Gtk.Picture": |
| 47 |
for property in properties: |
| 48 |
if ( |
| 49 |
property.name == "accessible-role" |
| 50 |
and property.value.range.text == "presentation" |
| 51 |
): |
| 52 |
return |
| 53 |
|
| 54 |
accessibility_child = child.content.children[ExtAccessibility] |
| 55 |
if len(accessibility_child) > 0: |
| 56 |
accessibility_properties = child.content.children[ExtAccessibility][ |
| 57 |
0 |
| 58 |
].properties |
| 59 |
for accessibility_property in accessibility_properties: |
| 60 |
if accessibility_property.name in ("label", "labelled-by"): |
| 61 |
return |
| 62 |
|
| 63 |
self.problems.append( |
| 64 |
CompileWarning( |
| 65 |
f"{type} is missing an accessibility label", |
| 66 |
child.signature_range, |
| 67 |
id=self.id, |
| 68 |
) |
| 69 |
) |