aria-required-attr-evaluate.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { requiredAttr, getExplicitRole } from '../../commons/aria';
  2. import { getElementSpec } from '../../commons/standards';
  3. import { uniqueArray } from '../../core/utils';
  4. /**
  5. * Check that the element has all required attributes for its explicit role.
  6. *
  7. * Required ARIA attributes are taken from the `ariaRoles` standards object from the roles `requiredAttrs` property.
  8. *
  9. * ##### Data:
  10. * <table class="props">
  11. * <thead>
  12. * <tr>
  13. * <th>Type</th>
  14. * <th>Description</th>
  15. * </tr>
  16. * </thead>
  17. * <tbody>
  18. * <tr>
  19. * <td><code>String[]</code></td>
  20. * <td>List of all missing require attributes</td>
  21. * </tr>
  22. * </tbody>
  23. * </table>
  24. *
  25. * @memberof checks
  26. * @return {Boolean} True if all required attributes are present. False otherwise.
  27. */
  28. function ariaRequiredAttrEvaluate(node, options = {}, virtualNode) {
  29. const missing = [];
  30. const attrs = virtualNode.attrNames;
  31. if (attrs.length) {
  32. const role = getExplicitRole(virtualNode);
  33. let required = requiredAttr(role);
  34. const elmSpec = getElementSpec(virtualNode);
  35. // @deprecated: required attr options to pass more attrs.
  36. // configure the standards spec instead
  37. if (Array.isArray(options[role])) {
  38. required = uniqueArray(options[role], required);
  39. }
  40. if (role && required) {
  41. for (let i = 0, l = required.length; i < l; i++) {
  42. const attr = required[i];
  43. if (
  44. !virtualNode.attr(attr) &&
  45. !(
  46. elmSpec.implicitAttrs &&
  47. typeof elmSpec.implicitAttrs[attr] !== 'undefined'
  48. )
  49. ) {
  50. missing.push(attr);
  51. }
  52. }
  53. }
  54. }
  55. if (missing.length) {
  56. this.data(missing);
  57. return false;
  58. }
  59. return true;
  60. }
  61. export default ariaRequiredAttrEvaluate;