Files
wekan/tests/iconOnlyControlLabels.test.cjs
T
Lauri Ojansivu 8d71c6f925 Accessibility: every icon-only link and button has a name for screen readers.
21 controls showed only an icon and had no title, so a screen reader
announced them as an unnamed link: the swimlane and list drag handles, the
close buttons of the inline forms, the received, start, due and end date
buttons of a card, the add-attachment and edit-custom-field buttons, the
notification drawer's menu and close, the avatar's edit button, the list
open and back carets on a phone, the import's add-member and History's
previous and next. Each now has a translated title, which
client/lib/accessibility.js also makes its aria-label; the existing
translations are used, so no new strings.

tests/iconOnlyControlLabels.test.cjs scans every template for a link or
button whose only content is an icon and fails on one without a name.

This is part of #337, which stays open for the rest.

Thanks to ndarilek and xet7 !
2026-10-14 16:59:57 +03:00

65 lines
2.9 KiB
JavaScript

'use strict';
// #337: a link or button whose only content is an icon says nothing to a
// screen reader unless it has a name. client/lib/accessibility.js copies a
// control's title into aria-label, so every icon-only control in the
// templates needs a title (or an aria-label of its own).
// Run: node tests/iconOnlyControlLabels.test.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const ROOT = path.join(__dirname, '..');
function jadeFiles(dir, out = []) {
for (const entry of fs.readdirSync(path.join(ROOT, dir), { withFileTypes: true })) {
const rel = `${dir}/${entry.name}`;
if (entry.isDirectory()) jadeFiles(rel, out);
else if (entry.name.endsWith('.jade')) out.push(rel);
}
return out;
}
const indent = line => line.length - line.trimStart().length;
const NAMED = /\btitle=|aria-label=/;
// `a` or `button` with only `i.fa...` icon lines (and comments) under it.
function unlabelled(source) {
const lines = source.split('\n');
const found = [];
lines.forEach((line, i) => {
const head = line.trim();
if (!/^(a|button)([.#(][^\s]*?(\(.*\))?)?$/.test(head) || NAMED.test(head)) return;
const children = [];
for (let j = i + 1; j < lines.length && (!lines[j].trim() || indent(lines[j]) > indent(line)); j++) {
if (lines[j].trim()) children.push(lines[j].trim());
}
const iconOnly = children.length > 0 && children.every(c => /^i\.fa/.test(c) || c.startsWith('//'));
if (iconOnly && !children.some(c => NAMED.test(c))) found.push(i + 1);
});
return found;
}
// The scanner itself (positive and negative).
assert.deepEqual(unlabelled('a.js-x\n i.fa.fa-times'), [1]);
assert.deepEqual(unlabelled('a.js-x(class="{{#if y}}z{{/if}}")\n i.fa.fa-times'), [1], 'with attributes');
assert.deepEqual(unlabelled("a.js-x(title=\"{{_ 'close'}}\")\n i.fa.fa-times"), []);
assert.deepEqual(unlabelled("a.js-x\n i.fa.fa-times(aria-label=\"{{_ 'close'}}\")"), []);
assert.deepEqual(unlabelled('a.js-x\n i.fa.fa-times\n | {{_ \'close\'}}'), [], 'visible text is a name');
console.log(' ok - the scanner');
const offenders = [];
for (const file of jadeFiles('client')) {
for (const line of unlabelled(fs.readFileSync(path.join(ROOT, file), 'utf8'))) offenders.push(`${file}:${line}`);
}
assert.deepEqual(offenders, [], 'give each a title="{{_ \'...\'}}"');
console.log(' ok - every icon-only link and button in the templates has a name');
// And the title still becomes the accessible name.
const a11y = fs.readFileSync(path.join(ROOT, 'client/lib/accessibility.js'), 'utf8');
assert.match(a11y, /HTML\.A = \(attributes, \.\.\.others\) => \{\s*return superA\(copyTitleInAriaLabel\(enforceHref\(attributes\)\), \.\.\.others\);/);
assert.match(a11y, /attributes\['aria-label'\] = attributes\.title;/);
console.log(' ok - title is copied into aria-label');
console.log('\niconOnlyControlLabels: all tests passed');