isNonInteractiveElement.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _ariaQuery = require("aria-query");
  7. var _axobjectQuery = require("axobject-query");
  8. var _arrayIncludes = _interopRequireDefault(require("array-includes"));
  9. var _arrayPrototype = _interopRequireDefault(require("array.prototype.flatmap"));
  10. var _attributesComparator = _interopRequireDefault(require("./attributesComparator"));
  11. function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
  12. function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
  13. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  14. function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
  15. function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
  16. function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
  17. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  18. function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
  19. function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
  20. function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
  21. function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
  22. var roleKeys = _ariaQuery.roles.keys();
  23. var elementRoleEntries = _toConsumableArray(_ariaQuery.elementRoles);
  24. var nonInteractiveRoles = new Set(roleKeys.filter(function (name) {
  25. var role = _ariaQuery.roles.get(name);
  26. return !role["abstract"]
  27. // 'toolbar' does not descend from widget, but it does support
  28. // aria-activedescendant, thus in practice we treat it as a widget.
  29. && name !== 'toolbar'
  30. // This role is meant to have no semantic value.
  31. // @see https://www.w3.org/TR/wai-aria-1.2/#generic
  32. && name !== 'generic' && !role.superClass.some(function (classes) {
  33. return (0, _arrayIncludes["default"])(classes, 'widget');
  34. });
  35. }).concat(
  36. // The `progressbar` is descended from `widget`, but in practice, its
  37. // value is always `readonly`, so we treat it as a non-interactive role.
  38. 'progressbar'));
  39. var interactiveRoles = new Set(roleKeys.filter(function (name) {
  40. var role = _ariaQuery.roles.get(name);
  41. return !role["abstract"]
  42. // The `progressbar` is descended from `widget`, but in practice, its
  43. // value is always `readonly`, so we treat it as a non-interactive role.
  44. && name !== 'progressbar'
  45. // This role is meant to have no semantic value.
  46. // @see https://www.w3.org/TR/wai-aria-1.2/#generic
  47. && name !== 'generic' && role.superClass.some(function (classes) {
  48. return (0, _arrayIncludes["default"])(classes, 'widget');
  49. });
  50. }).concat(
  51. // 'toolbar' does not descend from widget, but it does support
  52. // aria-activedescendant, thus in practice we treat it as a widget.
  53. 'toolbar'));
  54. var interactiveElementRoleSchemas = (0, _arrayPrototype["default"])(elementRoleEntries, function (_ref) {
  55. var _ref2 = _slicedToArray(_ref, 2),
  56. elementSchema = _ref2[0],
  57. rolesArr = _ref2[1];
  58. return rolesArr.some(function (role) {
  59. return interactiveRoles.has(role);
  60. }) ? [elementSchema] : [];
  61. });
  62. var nonInteractiveElementRoleSchemas = (0, _arrayPrototype["default"])(elementRoleEntries, function (_ref3) {
  63. var _ref4 = _slicedToArray(_ref3, 2),
  64. elementSchema = _ref4[0],
  65. rolesArr = _ref4[1];
  66. return rolesArr.every(function (role) {
  67. return nonInteractiveRoles.has(role);
  68. }) ? [elementSchema] : [];
  69. });
  70. var nonInteractiveAXObjects = new Set(_axobjectQuery.AXObjects.keys().filter(function (name) {
  71. return (0, _arrayIncludes["default"])(['window', 'structure'], _axobjectQuery.AXObjects.get(name).type);
  72. }));
  73. var nonInteractiveElementAXObjectSchemas = (0, _arrayPrototype["default"])(_toConsumableArray(_axobjectQuery.elementAXObjects), function (_ref5) {
  74. var _ref6 = _slicedToArray(_ref5, 2),
  75. elementSchema = _ref6[0],
  76. AXObjectsArr = _ref6[1];
  77. return AXObjectsArr.every(function (role) {
  78. return nonInteractiveAXObjects.has(role);
  79. }) ? [elementSchema] : [];
  80. });
  81. function checkIsNonInteractiveElement(tagName, attributes) {
  82. function elementSchemaMatcher(elementSchema) {
  83. return tagName === elementSchema.name && tagName !== 'td' // TODO: investigate why this is needed
  84. && (0, _attributesComparator["default"])(elementSchema.attributes, attributes);
  85. }
  86. // Check in elementRoles for inherent non-interactive role associations for
  87. // this element.
  88. var isInherentNonInteractiveElement = nonInteractiveElementRoleSchemas.some(elementSchemaMatcher);
  89. if (isInherentNonInteractiveElement) {
  90. return true;
  91. }
  92. // Check in elementRoles for inherent interactive role associations for
  93. // this element.
  94. var isInherentInteractiveElement = interactiveElementRoleSchemas.some(elementSchemaMatcher);
  95. if (isInherentInteractiveElement) {
  96. return false;
  97. }
  98. // Check in elementAXObjects for AX Tree associations for this element.
  99. var isNonInteractiveAXElement = nonInteractiveElementAXObjectSchemas.some(elementSchemaMatcher);
  100. if (isNonInteractiveAXElement) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * Returns boolean indicating whether the given element is a non-interactive
  107. * element. If the element has either a non-interactive role assigned or it
  108. * is an element with an inherently non-interactive role, then this utility
  109. * returns true. Elements that lack either an explicitly assigned role or
  110. * an inherent role are not considered. For those, this utility returns false
  111. * because a positive determination of interactiveness cannot be determined.
  112. */
  113. var isNonInteractiveElement = function isNonInteractiveElement(tagName, attributes) {
  114. // Do not test higher level JSX components, as we do not know what
  115. // low-level DOM element this maps to.
  116. if (!_ariaQuery.dom.has(tagName)) {
  117. return false;
  118. }
  119. // <header> elements do not technically have semantics, unless the
  120. // element is a direct descendant of <body>, and this plugin cannot
  121. // reliably test that.
  122. // @see https://www.w3.org/TR/wai-aria-practices/examples/landmarks/banner.html
  123. if (tagName === 'header') {
  124. return false;
  125. }
  126. return checkIsNonInteractiveElement(tagName, attributes);
  127. };
  128. var _default = exports["default"] = isNonInteractiveElement;
  129. module.exports = exports.default;