aria-valid-attr-evaluate.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { validateAttr } from '../../commons/aria';
  2. import { getNodeAttributes } from '../../core/utils';
  3. /**
  4. * Check that each `aria-` attribute on an element is a valid ARIA attribute.
  5. *
  6. * Valid ARIA attributes are listed in 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 invalid attributes</td>
  20. * </tr>
  21. * </tbody>
  22. * </table>
  23. *
  24. * @memberof checks
  25. * @return {Boolean} True if all `aria-` attributes are valid. False otherwise.
  26. */
  27. function ariaValidAttrEvaluate(node, options) {
  28. options = Array.isArray(options.value) ? options.value : [];
  29. var invalid = [],
  30. aria = /^aria-/;
  31. var attr,
  32. attrs = getNodeAttributes(node);
  33. for (var i = 0, l = attrs.length; i < l; i++) {
  34. attr = attrs[i].name;
  35. if (
  36. options.indexOf(attr) === -1 &&
  37. aria.test(attr) &&
  38. !validateAttr(attr)
  39. ) {
  40. invalid.push(attr);
  41. }
  42. }
  43. if (invalid.length) {
  44. this.data(invalid);
  45. return false;
  46. }
  47. return true;
  48. }
  49. export default ariaValidAttrEvaluate;