implicit-role.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import implicitHtmlRoles from '../standards/implicit-html-roles';
  2. import { getNodeFromTree } from '../../core/utils';
  3. import getElementSpec from '../standards/get-element-spec';
  4. import AbstractVirtuaNode from '../../core/base/virtual-node/abstract-virtual-node';
  5. /**
  6. * Get the implicit role for a given node
  7. * @method implicitRole
  8. * @memberof axe.commons.aria
  9. * @instance
  10. * @param {HTMLElement|VirtualNode} node The node to test
  11. * @return {Mixed} Either the role or `null` if there is none
  12. */
  13. function implicitRole(node, { chromium } = {}) {
  14. const vNode =
  15. node instanceof AbstractVirtuaNode ? node : getNodeFromTree(node);
  16. node = vNode.actualNode;
  17. // this error is only thrown if the virtual tree is not a
  18. // complete tree, which only happens in linting and if a
  19. // user used `getFlattenedTree` manually on a subset of the
  20. // DOM tree
  21. if (!vNode) {
  22. throw new ReferenceError(
  23. 'Cannot get implicit role of a node outside the current scope.'
  24. );
  25. }
  26. const nodeName = vNode.props.nodeName;
  27. const role = implicitHtmlRoles[nodeName];
  28. if (!role && chromium) {
  29. const { chromiumRole } = getElementSpec(vNode);
  30. return chromiumRole || null;
  31. }
  32. if (typeof role === 'function') {
  33. return role(vNode);
  34. }
  35. return role || null;
  36. }
  37. export default implicitRole;