label-content-name-mismatch-matches.js
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { getRole, arialabelText, arialabelledbyText } from '../commons/aria';
import {
getAriaRolesSupportingNameFromContent,
getAriaRolesByType
} from '../commons/standards';
import { sanitize, visibleVirtual } from '../commons/text';
function labelContentNameMismatchMatches(node, virtualNode) {
/**
* Applicability:
* Rule applies to any element that has
* a) a semantic role that is `widget` that supports name from content
* b) has visible text content
* c) has accessible name (eg: `aria-label`)
*/
const role = getRole(node);
if (!role) {
return false;
}
const widgetRoles = getAriaRolesByType('widget');
const isWidgetType = widgetRoles.includes(role);
if (!isWidgetType) {
return false;
}
const rolesWithNameFromContents = getAriaRolesSupportingNameFromContent();
if (!rolesWithNameFromContents.includes(role)) {
return false;
}
/**
* if no `aria-label` or `aria-labelledby` attribute - ignore `node`
*/
if (
!sanitize(arialabelText(virtualNode)) &&
!sanitize(arialabelledbyText(node))
) {
return false;
}
/**
* if no `contentText` - ignore `node`
*/
if (!sanitize(visibleVirtual(virtualNode))) {
return false;
}
return true;
}
export default labelContentNameMismatchMatches;