Skip to content
Merged
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
126 changes: 126 additions & 0 deletions hydra-gates/scripts/lib/check_button_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: EUPL-1.2
r"""Gate-39 button-name — every button must have an accessible name
(WCAG 2.2 AA SC 4.1.2 Name, Role, Value; axe-core `button-name`).

An icon-only button with no name is announced as just "button". The finding
is real and worth keeping.

WHY THIS WAS REWRITTEN
----------------------
The accepted-attribute regex was:

r'(^|\s)(:?aria-label|aria-labelledby|v-bind:aria-label|title)\s*='

`:?` binds to the FIRST alternative only, so `:aria-label` was accepted while
`:title`, `v-bind:title` and `:aria-labelledby` were not. Vue templates bind
almost every user-visible string, because it has to go through `t()`:

<button type="button"
class="settings-page-editor__remove"
:title="t('openbuild', 'Remove tab')"
@click="removeTab(index)">

That button HAS a name. All 22 of openbuild's findings were this exact shape
— a correctly translated bound title read as a missing one. The only way to
close them was to add a second, redundant name (#222 family; see
ConductionNL/.github#225 for the sibling scope defect).

An accessible name that is BOUND is still an accessible name. What matters is
whether the attribute reaches the DOM, and `:title` reaches it exactly as
`title` does. Note the gate already accepted static `title=`; accepting the
bound form is a consistency fix, not a new claim about how strong a name
`title` is.

Also accepted, on the same principle — the name arrives, whatever the syntax:
* `aria-label`, `aria-labelledby`, `title`, each bound or literal
* `ariaLabel` / `aria-label` as NcButton's published prop (camelCase form)
* a default-slot `<template #default>` carrying text

WHAT STILL FAILS, AND MUST
--------------------------
A button whose body is nothing but an icon component and which declares no
name at all:

<NcButton type="tertiary" @click="$emit('close')"><CloseIcon /></NcButton>

Usage:
check_button_name.py <file.vue>... # findings on stdout
"""
from __future__ import annotations

import re
import sys

COMMENT = re.compile(r'<!--.*?-->', re.DOTALL)
BLOCK = re.compile(r'<(script|style)\b[^>]*>.*?</\1\s*>', re.DOTALL | re.IGNORECASE)

# THE FIX. The optional bound prefix applies to EVERY name attribute, not just
# the first alternative. `(?:^|\s)` still anchors so `data-title=` never counts.
NAME_ATTR = re.compile(
r'(?:^|\s)(?::|v-bind:)?(?:aria-label|arialabel|aria-labelledby|'
r'arialabelledby|title|name)\s*=',
re.IGNORECASE)

# A button element and its body. Quote-aware attribute run so `:title="a > b"`
# does not end the tag early.
BUTTONS = (
re.compile(r'<(NcButton)\b((?:"[^"]*"|\'[^\']*\'|[^>"\'])*?)>(.*?)</NcButton\s*>',
re.DOTALL),
re.compile(r'<(button)\b((?:"[^"]*"|\'[^\']*\'|[^>"\'])*?)>(.*?)</button\s*>',
re.DOTALL | re.IGNORECASE),
)
TAG = re.compile(r'<[^>]*>', re.DOTALL)
DEFAULT_SLOT = re.compile(r'<template\s+(?:#default|v-slot:default)\b', re.IGNORECASE)


def _named(attrs: str, body: str) -> bool:
if NAME_ATTR.search(attrs):
return True
# A Vue interpolation is dynamic visible text — the button's own label.
if '{{' in body and '}}' in body:
return True
# An explicit default slot renders into the button's label.
if DEFAULT_SLOT.search(body):
return True
# Literal text content. One character is not a name (an icon ligature or a
# stray bullet); the previous threshold of >= 2 is kept deliberately.
return len(re.sub(r'\s+', '', TAG.sub('', body))) >= 2


def scan_source(fname: str, src: str) -> list[str]:
body_src = BLOCK.sub(' ', COMMENT.sub(' ', src))
findings: list[str] = []
for pat in BUTTONS:
for m in pat.finditer(body_src):
tagname, attrs, body = m.group(1), m.group(2) or '', m.group(3) or ''
if _named(attrs, body):
continue
opening = re.sub(r'\s+', ' ', f'<{tagname}{attrs}>').strip()
if len(opening) > 200:
opening = opening[:197] + '...'
findings.append(
f'{fname}: {opening} rule=icon-only-button-without-accessible-name')
return findings


def scan_files(files: list[str]) -> list[str]:
out: list[str] = []
for fname in files:
try:
with open(fname, encoding='utf-8', errors='replace') as f:
src = f.read()
except OSError:
continue
out.extend(scan_source(fname, src))
return out


def main(argv: list[str]) -> int:
for line in scan_files(argv[1:]):
print(line)
return 0


if __name__ == '__main__':
sys.exit(main(sys.argv))
168 changes: 168 additions & 0 deletions hydra-gates/scripts/lib/test_check_button_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: EUPL-1.2
"""Tests for check_button_name (gate-39). Run with:

python3 scripts/lib/test_check_button_name.py

BOTH ARMS, EVERY TIME
---------------------
gate-39's accepted-attribute regex was

r'(^|\\s)(:?aria-label|aria-labelledby|v-bind:aria-label|title)\\s*='

where `:?` binds to the FIRST alternative only. `:aria-label` was accepted;
`:title`, `v-bind:title` and `:aria-labelledby` were not. Vue binds nearly
every user-visible string because it has to pass through `t()`, so ALL 22 of
openbuild's findings were a correctly translated `:title` read as missing.

Accepting a bound attribute widens the gate, and widening is how a gate goes
quiet. So each accepted shape below sits next to the genuinely-unnamed button
it must not swallow.
"""
from __future__ import annotations

import os
import sys
import unittest

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import check_button_name as cbn # noqa: E402


def rules(markup: str) -> list[str]:
src = "<template>\n" + markup + "\n</template>\n"
return [line.rsplit("rule=", 1)[1]
for line in cbn.scan_source("Component.vue", src)]


FLAG = ["icon-only-button-without-accessible-name"]


class TheUnnamedButton(unittest.TestCase):
"""THE POSITIVE CONTROL. If these stop firing the gate is decoration."""

def test_tp_an_icon_only_ncbutton(self):
# procest TaskCreateDialog.vue, verbatim shape.
self.assertEqual(rules(
'<NcButton type="tertiary" @click="$emit(\'close\')">'
'<CloseIcon :size="20" /></NcButton>'), FLAG)

def test_tp_an_icon_only_native_button(self):
self.assertEqual(rules(
'<button type="button" class="close" @click="close">'
'<CloseIcon /></button>'), FLAG)

def test_tp_an_empty_button(self):
self.assertEqual(rules('<button @click="go"></button>'), FLAG)

def test_tp_a_single_character_body_is_not_a_name(self):
# An icon ligature or a stray bullet. The >= 2 threshold is kept.
self.assertEqual(rules('<button @click="go">×</button>'), FLAG)

def test_tp_a_lookalike_attribute_does_not_name_it(self):
# `data-title` is not `title`; the `(?:^|\s)` anchor is load-bearing.
self.assertEqual(rules(
'<button data-title="Remove" @click="go"><Icon /></button>'), FLAG)


class BoundNamesAreNames(unittest.TestCase):
"""THE FIX. Each of these was reported as unnamed."""

def test_fp_bound_title(self):
# openbuild SettingsPageEditor.vue, verbatim. All 22 of openbuild's
# findings were this shape.
self.assertEqual(rules("""
<button type="button"
class="settings-page-editor__remove"
:title="t('openbuild', 'Remove tab')"
@click="removeTab(index)">
<CloseIcon :size="16" />
</button>
"""), [])

def test_fp_v_bind_title(self):
self.assertEqual(rules(
'<button v-bind:title="label" @click="go"><Icon /></button>'), [])

def test_fp_bound_aria_labelledby(self):
self.assertEqual(rules(
'<button :aria-labelledby="headingId" @click="go"><Icon /></button>'), [])

def test_fp_bound_aria_label_still_works(self):
# Accepted before; must not regress.
self.assertEqual(rules(
'<button :aria-label="t(\'app\', \'Close\')" @click="go"><Icon /></button>'), [])

def test_fp_static_title_still_works(self):
self.assertEqual(rules('<button title="Close" @click="go"><Icon /></button>'), [])

def test_fp_the_camelcase_prop_form(self):
# NcButton's published prop name.
self.assertEqual(rules(
'<NcButton :ariaLabel="label" @click="go"><Icon /></NcButton>'), [])

# ---- the paired true positive -----------------------------------------
def test_tp_the_same_button_with_the_binding_REMOVED_is_still_flagged(self):
# One attribute is the whole difference from test_fp_bound_title.
self.assertEqual(rules("""
<button type="button"
class="settings-page-editor__remove"
@click="removeTab(index)">
<CloseIcon :size="16" />
</button>
"""), FLAG)


class NamedByContent(unittest.TestCase):
def test_fp_literal_text(self):
self.assertEqual(rules('<NcButton @click="go">Save changes</NcButton>'), [])

def test_fp_an_interpolation(self):
self.assertEqual(rules(
'<NcButton @click="go">{{ t(\'app\', \'Save\') }}</NcButton>'), [])

def test_fp_an_explicit_default_slot(self):
# `<template #icon>` + `<template #default>` is the NcButton idiom;
# the default slot renders the visible label.
self.assertEqual(rules("""
<NcButton @click="go">
<template #icon><PlusIcon :size="20" /></template>
<template #default>Add a field</template>
</NcButton>
"""), [])

def test_tp_an_icon_slot_ALONE_is_not_a_name(self):
# The negative control for the slot rule: an icon slot names nothing.
self.assertEqual(rules("""
<NcButton @click="go">
<template #icon><PlusIcon :size="20" /></template>
</NcButton>
"""), FLAG)


class Parsing(unittest.TestCase):
def test_a_greater_than_inside_an_attribute_does_not_end_the_tag(self):
# The old `[^>]*` run ended the tag at the expression's `>`, so the
# `:title` that followed was never seen.
self.assertEqual(rules(
'<button :class="a > b ? \'x\' : \'y\'" :title="label" @click="go">'
'<Icon /></button>'), [])

def test_commented_out_markup_ships_nothing(self):
self.assertEqual(rules('<!-- <button @click="go"><Icon /></button> -->'), [])

def test_a_button_in_a_script_string_is_not_markup(self):
src = ('<template><p>hi</p></template>\n<script>\n'
"const s = '<button><i></i></button>'\n</script>\n")
self.assertEqual(cbn.scan_source("C.vue", src), [])

def test_each_unnamed_button_is_reported(self):
self.assertEqual(rules("""
<button @click="a"><Icon /></button>
<button :title="t('app', 'B')" @click="b"><Icon /></button>
<button @click="c"><Icon /></button>
"""), FLAG * 2)


if __name__ == '__main__':
unittest.main(verbosity=2)
Loading
Loading