isNonInteractiveElement.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  8. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  9. var _ariaQuery = require("aria-query");
  10. var _axobjectQuery = require("axobject-query");
  11. var _arrayIncludes = _interopRequireDefault(require("array-includes"));
  12. var _attributesComparator = _interopRequireDefault(require("./attributesComparator"));
  13. var roleKeys = (0, _toConsumableArray2["default"])(_ariaQuery.roles.keys());
  14. var elementRoleEntries = (0, _toConsumableArray2["default"])(_ariaQuery.elementRoles);
  15. var nonInteractiveRoles = new Set(roleKeys.filter(function (name) {
  16. var role = _ariaQuery.roles.get(name);
  17. return !role["abstract"] // 'toolbar' does not descend from widget, but it does support
  18. // aria-activedescendant, thus in practice we treat it as a widget.
  19. && name !== 'toolbar' // This role is meant to have no semantic value.
  20. // @see https://www.w3.org/TR/wai-aria-1.2/#generic
  21. && name !== 'generic' && !role.superClass.some(function (classes) {
  22. return (0, _arrayIncludes["default"])(classes, 'widget');
  23. });
  24. }).concat( // The `progressbar` is descended from `widget`, but in practice, its
  25. // value is always `readonly`, so we treat it as a non-interactive role.
  26. 'progressbar'));
  27. var interactiveRoles = new Set(roleKeys.filter(function (name) {
  28. var role = _ariaQuery.roles.get(name);
  29. return !role["abstract"] // The `progressbar` is descended from `widget`, but in practice, its
  30. // value is always `readonly`, so we treat it as a non-interactive role.
  31. && name !== 'progressbar' // This role is meant to have no semantic value.
  32. // @see https://www.w3.org/TR/wai-aria-1.2/#generic
  33. && name !== 'generic' && role.superClass.some(function (classes) {
  34. return (0, _arrayIncludes["default"])(classes, 'widget');
  35. });
  36. }).concat( // 'toolbar' does not descend from widget, but it does support
  37. // aria-activedescendant, thus in practice we treat it as a widget.
  38. 'toolbar'));
  39. var nonInteractiveElementRoleSchemas = elementRoleEntries.reduce(function (accumulator, _ref) {
  40. var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
  41. elementSchema = _ref2[0],
  42. roleSet = _ref2[1];
  43. if ((0, _toConsumableArray2["default"])(roleSet).every(function (role) {
  44. return nonInteractiveRoles.has(role);
  45. })) {
  46. accumulator.push(elementSchema);
  47. }
  48. return accumulator;
  49. }, []);
  50. var interactiveElementRoleSchemas = elementRoleEntries.reduce(function (accumulator, _ref3) {
  51. var _ref4 = (0, _slicedToArray2["default"])(_ref3, 2),
  52. elementSchema = _ref4[0],
  53. roleSet = _ref4[1];
  54. if ((0, _toConsumableArray2["default"])(roleSet).some(function (role) {
  55. return interactiveRoles.has(role);
  56. })) {
  57. accumulator.push(elementSchema);
  58. }
  59. return accumulator;
  60. }, []);
  61. var nonInteractiveAXObjects = new Set((0, _toConsumableArray2["default"])(_axobjectQuery.AXObjects.keys()).filter(function (name) {
  62. return (0, _arrayIncludes["default"])(['window', 'structure'], _axobjectQuery.AXObjects.get(name).type);
  63. }));
  64. var nonInteractiveElementAXObjectSchemas = (0, _toConsumableArray2["default"])(_axobjectQuery.elementAXObjects).reduce(function (accumulator, _ref5) {
  65. var _ref6 = (0, _slicedToArray2["default"])(_ref5, 2),
  66. elementSchema = _ref6[0],
  67. AXObjectSet = _ref6[1];
  68. if ((0, _toConsumableArray2["default"])(AXObjectSet).every(function (role) {
  69. return nonInteractiveAXObjects.has(role);
  70. })) {
  71. accumulator.push(elementSchema);
  72. }
  73. return accumulator;
  74. }, []);
  75. function checkIsNonInteractiveElement(tagName, attributes) {
  76. function elementSchemaMatcher(elementSchema) {
  77. return tagName === elementSchema.name && (0, _attributesComparator["default"])(elementSchema.attributes, attributes);
  78. } // Check in elementRoles for inherent non-interactive role associations for
  79. // this element.
  80. var isInherentNonInteractiveElement = nonInteractiveElementRoleSchemas.some(elementSchemaMatcher);
  81. if (isInherentNonInteractiveElement) {
  82. return true;
  83. } // Check in elementRoles for inherent interactive role associations for
  84. // this element.
  85. var isInherentInteractiveElement = interactiveElementRoleSchemas.some(elementSchemaMatcher);
  86. if (isInherentInteractiveElement) {
  87. return false;
  88. } // Check in elementAXObjects for AX Tree associations for this element.
  89. var isNonInteractiveAXElement = nonInteractiveElementAXObjectSchemas.some(elementSchemaMatcher);
  90. if (isNonInteractiveAXElement) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Returns boolean indicating whether the given element is a non-interactive
  97. * element. If the element has either a non-interactive role assigned or it
  98. * is an element with an inherently non-interactive role, then this utility
  99. * returns true. Elements that lack either an explicitly assigned role or
  100. * an inherent role are not considered. For those, this utility returns false
  101. * because a positive determination of interactiveness cannot be determined.
  102. */
  103. var isNonInteractiveElement = function isNonInteractiveElement(tagName, attributes) {
  104. // Do not test higher level JSX components, as we do not know what
  105. // low-level DOM element this maps to.
  106. if (!_ariaQuery.dom.has(tagName)) {
  107. return false;
  108. } // <header> elements do not technically have semantics, unless the
  109. // element is a direct descendant of <body>, and this plugin cannot
  110. // reliably test that.
  111. // @see https://www.w3.org/TR/wai-aria-practices/examples/landmarks/banner.html
  112. if (tagName === 'header') {
  113. return false;
  114. }
  115. return checkIsNonInteractiveElement(tagName, attributes);
  116. };
  117. var _default = isNonInteractiveElement;
  118. exports["default"] = _default;