landmark-unique-matches.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { findUpVirtual, isVisible } from '../commons/dom';
  2. import { getRole } from '../commons/aria';
  3. import { getAriaRolesByType } from '../commons/standards';
  4. import { accessibleTextVirtual } from '../commons/text';
  5. function landmarkUniqueMatches(node, virtualNode) {
  6. /*
  7. * Since this is a best-practice rule, we are filtering elements as dictated by ARIA 1.1 Practices regardless of treatment by browser/AT combinations.
  8. *
  9. * Info: https://www.w3.org/TR/wai-aria-practices-1.1/#aria_landmark
  10. */
  11. var excludedParentsForHeaderFooterLandmarks = [
  12. 'article',
  13. 'aside',
  14. 'main',
  15. 'nav',
  16. 'section'
  17. ].join(',');
  18. function isHeaderFooterLandmark(headerFooterElement) {
  19. return !findUpVirtual(
  20. headerFooterElement,
  21. excludedParentsForHeaderFooterLandmarks
  22. );
  23. }
  24. function isLandmarkVirtual(virtualNode) {
  25. var { actualNode } = virtualNode;
  26. var landmarkRoles = getAriaRolesByType('landmark');
  27. var role = getRole(actualNode);
  28. if (!role) {
  29. return false;
  30. }
  31. var nodeName = actualNode.nodeName.toUpperCase();
  32. if (nodeName === 'HEADER' || nodeName === 'FOOTER') {
  33. return isHeaderFooterLandmark(virtualNode);
  34. }
  35. if (nodeName === 'SECTION' || nodeName === 'FORM') {
  36. var accessibleText = accessibleTextVirtual(virtualNode);
  37. return !!accessibleText;
  38. }
  39. return landmarkRoles.indexOf(role) >= 0 || role === 'region';
  40. }
  41. return isLandmarkVirtual(virtualNode) && isVisible(node, true);
  42. }
  43. export default landmarkUniqueMatches;