valid-scrollable-semantics-evaluate.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { getExplicitRole } from '../../commons/aria';
  2. /**
  3. * A map from HTML tag names to a boolean which reflects whether it is
  4. * appropriate for scrollable elements found in the focus order.
  5. */
  6. const VALID_TAG_NAMES_FOR_SCROLLABLE_REGIONS = {
  7. ARTICLE: true,
  8. ASIDE: true,
  9. NAV: true,
  10. SECTION: true
  11. };
  12. /**
  13. * A map from each landmark role to a boolean which reflects whether it is
  14. * appropriate for scrollable elements found in the focus order.
  15. */
  16. const VALID_ROLES_FOR_SCROLLABLE_REGIONS = {
  17. application: true,
  18. banner: false,
  19. complementary: true,
  20. contentinfo: true,
  21. form: true,
  22. main: true,
  23. navigation: true,
  24. region: true,
  25. search: false
  26. };
  27. /**
  28. * @param {HTMLElement} node
  29. * @return {Boolean} Whether the element has a tag appropriate for a scrollable
  30. * region.
  31. */
  32. function validScrollableTagName(node) {
  33. // Some elements with nonsensical roles will pass this check, but should be
  34. // flagged by other checks.
  35. const nodeName = node.nodeName.toUpperCase();
  36. return VALID_TAG_NAMES_FOR_SCROLLABLE_REGIONS[nodeName] || false;
  37. }
  38. /**
  39. * @param {HTMLElement} node
  40. * @return {Boolean} Whether the node has a role appropriate for a scrollable
  41. * region.
  42. */
  43. function validScrollableRole(node, options) {
  44. var role = getExplicitRole(node);
  45. if (!role) {
  46. return false;
  47. }
  48. return (
  49. VALID_ROLES_FOR_SCROLLABLE_REGIONS[role] ||
  50. options.roles.includes(role) ||
  51. false
  52. );
  53. }
  54. /**
  55. * Check if the element has a valid scrollable role or tag.
  56. *
  57. * @memberof checks
  58. * @param {HTMLElement} node
  59. * @return {Boolean} True if the elements role or tag name is a valid scrollable region. False otherwise.
  60. */
  61. function validScrollableSemanticsEvaluate(node, options) {
  62. return validScrollableRole(node, options) || validScrollableTagName(node);
  63. }
  64. export default validScrollableSemanticsEvaluate;