aria-allowed-attr-evaluate.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { uniqueArray } from '../../core/utils';
  2. import { getRole, allowedAttr, validateAttr } from '../../commons/aria';
  3. /**
  4. * Check if each ARIA attribute on an element is allowed for its semantic role.
  5. *
  6. * Allowed ARIA attributes are taken from the `ariaRoles` standards object combining the roles `requiredAttrs` and `allowedAttrs` properties, as well as any global ARIA attributes from the `ariaAttrs` standards object.
  7. *
  8. * ##### Data:
  9. * <table class="props">
  10. * <thead>
  11. * <tr>
  12. * <th>Type</th>
  13. * <th>Description</th>
  14. * </tr>
  15. * </thead>
  16. * <tbody>
  17. * <tr>
  18. * <td><code>String[]</code></td>
  19. * <td>List of all unallowed aria attributes and their value</td>
  20. * </tr>
  21. * </tbody>
  22. * </table>
  23. *
  24. * @memberof checks
  25. * @return {Boolean} True if each aria attribute is allowed. False otherwise.
  26. */
  27. function ariaAllowedAttrEvaluate(node, options, virtualNode) {
  28. const invalid = [];
  29. const role = getRole(virtualNode);
  30. const attrs = virtualNode.attrNames;
  31. let allowed = allowedAttr(role);
  32. // @deprecated: allowed attr options to pass more attrs.
  33. // configure the standards spec instead
  34. if (Array.isArray(options[role])) {
  35. allowed = uniqueArray(options[role].concat(allowed));
  36. }
  37. if (role && allowed) {
  38. for (let i = 0; i < attrs.length; i++) {
  39. const attrName = attrs[i];
  40. if (validateAttr(attrName) && !allowed.includes(attrName)) {
  41. invalid.push(attrName + '="' + virtualNode.attr(attrName) + '"');
  42. }
  43. }
  44. }
  45. if (invalid.length) {
  46. this.data(invalid);
  47. return false;
  48. }
  49. return true;
  50. }
  51. export default ariaAllowedAttrEvaluate;