aria-allowed-role-evaluate.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { isVisible } from '../../commons/dom';
  2. import { getElementUnallowedRoles } from '../../commons/aria';
  3. /**
  4. * Check if an element is allowed to have its explicit role value.
  5. *
  6. * Allowed ARIA roles are taken from the `htmlElms` standards object from the elements `allowedRoles` property.
  7. *
  8. * @memberof checks
  9. * @param {Boolean} [options.allowImplicit=true] Allow the explicit role to match the implicit role of the element.
  10. * @param {String[]} [options.ignoredTags=[]] Do not check for allowed roles in the provided HTML elements list.
  11. * @data {String[]} List of all unallowed roles.
  12. * @return {Boolean} True if the role is allowed on the element. False otherwise.
  13. */
  14. function ariaAllowedRoledEvaluate(node, options = {}) {
  15. /**
  16. * Implements allowed roles defined at:
  17. * https://www.w3.org/TR/html-aria/#docconformance
  18. * https://www.w3.org/TR/SVG2/struct.html#implicit-aria-semantics
  19. */
  20. const { allowImplicit = true, ignoredTags = [] } = options;
  21. const tagName = node.nodeName.toUpperCase();
  22. // check if the element should be ignored, by an user setting
  23. if (ignoredTags.map(t => t.toUpperCase()).includes(tagName)) {
  24. return true;
  25. }
  26. const unallowedRoles = getElementUnallowedRoles(node, allowImplicit);
  27. if (unallowedRoles.length) {
  28. this.data(unallowedRoles);
  29. if (!isVisible(node, true)) {
  30. // flag hidden elements for review
  31. return undefined;
  32. }
  33. return false;
  34. }
  35. return true;
  36. }
  37. export default ariaAllowedRoledEvaluate;