named-from-contents.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import getRole from './get-role';
  2. import standards from '../../standards';
  3. import { getNodeFromTree } from '../../core/utils';
  4. import AbstractVirtuaNode from '../../core/base/virtual-node/abstract-virtual-node';
  5. /**
  6. * Check if an element is named from contents
  7. *
  8. * @param {Node|VirtualNode} element
  9. * @param {Object} options
  10. * @property {Bool} strict Whether or not to follow the spects strictly
  11. * @return {Bool}
  12. */
  13. function namedFromContents(vNode, { strict } = {}) {
  14. vNode = vNode instanceof AbstractVirtuaNode ? vNode : getNodeFromTree(vNode);
  15. if (vNode.props.nodeType !== 1) {
  16. return false;
  17. }
  18. const role = getRole(vNode);
  19. const roleDef = standards.ariaRoles[role];
  20. if (roleDef && roleDef.nameFromContent) {
  21. return true;
  22. }
  23. /**
  24. * Note: Strictly speaking if the role is null, presentation, or none, the element
  25. * isn't named from contents. Axe-core often needs to know if an element
  26. * has content anyway, so we're allowing it here.
  27. * Use { strict: true } to disable this behavior.
  28. */
  29. if (strict) {
  30. return false;
  31. }
  32. return !roleDef || ['presentation', 'none'].includes(role);
  33. }
  34. export default namedFromContents;