is-inaccessible.mjs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Partial implementation https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
  3. * which should only be used for elements with a non-presentational role i.e.
  4. * `role="none"` and `role="presentation"` will not be excluded.
  5. *
  6. * Implements aria-hidden semantics (i.e. parent overrides child)
  7. * Ignores "Child Presentational: True" characteristics
  8. *
  9. * @param element
  10. * @param options
  11. * @returns {boolean} true if excluded, otherwise false
  12. */
  13. export function isInaccessible(element) {
  14. var _element$ownerDocumen;
  15. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  16. var _options$getComputedS = options.getComputedStyle,
  17. getComputedStyle = _options$getComputedS === void 0 ? (_element$ownerDocumen = element.ownerDocument.defaultView) === null || _element$ownerDocumen === void 0 ? void 0 : _element$ownerDocumen.getComputedStyle : _options$getComputedS,
  18. _options$isSubtreeIna = options.isSubtreeInaccessible,
  19. isSubtreeInaccessibleImpl = _options$isSubtreeIna === void 0 ? isSubtreeInaccessible : _options$isSubtreeIna;
  20. if (typeof getComputedStyle !== "function") {
  21. throw new TypeError("Owner document of the element needs to have an associated window.");
  22. } // since visibility is inherited we can exit early
  23. if (getComputedStyle(element).visibility === "hidden") {
  24. return true;
  25. }
  26. var currentElement = element;
  27. while (currentElement) {
  28. if (isSubtreeInaccessibleImpl(currentElement, {
  29. getComputedStyle: getComputedStyle
  30. })) {
  31. return true;
  32. }
  33. currentElement = currentElement.parentElement;
  34. }
  35. return false;
  36. }
  37. /**
  38. *
  39. * @param element
  40. * @param options
  41. * @returns {boolean} - `true` if every child of the element is inaccessible
  42. */
  43. export function isSubtreeInaccessible(element) {
  44. var _element$ownerDocumen2;
  45. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  46. var _options$getComputedS2 = options.getComputedStyle,
  47. getComputedStyle = _options$getComputedS2 === void 0 ? (_element$ownerDocumen2 = element.ownerDocument.defaultView) === null || _element$ownerDocumen2 === void 0 ? void 0 : _element$ownerDocumen2.getComputedStyle : _options$getComputedS2;
  48. if (typeof getComputedStyle !== "function") {
  49. throw new TypeError("Owner document of the element needs to have an associated window.");
  50. }
  51. if (element.hidden === true) {
  52. return true;
  53. }
  54. if (element.getAttribute("aria-hidden") === "true") {
  55. return true;
  56. }
  57. if (getComputedStyle(element).display === "none") {
  58. return true;
  59. }
  60. return false;
  61. }
  62. //# sourceMappingURL=is-inaccessible.mjs.map