label-content-name-mismatch-matches.js 1.3 KB

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