util.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.getLocalName = getLocalName;
  4. exports.isElement = isElement;
  5. exports.isHTMLTableCaptionElement = isHTMLTableCaptionElement;
  6. exports.isHTMLInputElement = isHTMLInputElement;
  7. exports.isHTMLOptGroupElement = isHTMLOptGroupElement;
  8. exports.isHTMLSelectElement = isHTMLSelectElement;
  9. exports.isHTMLTableElement = isHTMLTableElement;
  10. exports.isHTMLTextAreaElement = isHTMLTextAreaElement;
  11. exports.safeWindow = safeWindow;
  12. exports.isHTMLFieldSetElement = isHTMLFieldSetElement;
  13. exports.isHTMLLegendElement = isHTMLLegendElement;
  14. exports.isHTMLSlotElement = isHTMLSlotElement;
  15. exports.isSVGElement = isSVGElement;
  16. exports.isSVGSVGElement = isSVGSVGElement;
  17. exports.isSVGTitleElement = isSVGTitleElement;
  18. exports.queryIdRefs = queryIdRefs;
  19. exports.hasAnyConcreteRoles = hasAnyConcreteRoles;
  20. var _getRole = _interopRequireDefault(require("./getRole"));
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. /**
  23. * Safe Element.localName for all supported environments
  24. * @param element
  25. */
  26. function getLocalName(element) {
  27. var _element$localName;
  28. return (// eslint-disable-next-line no-restricted-properties -- actual guard for environments without localName
  29. (_element$localName = element.localName) !== null && _element$localName !== void 0 ? _element$localName : // eslint-disable-next-line no-restricted-properties -- required for the fallback
  30. element.tagName.toLowerCase()
  31. );
  32. }
  33. function isElement(node) {
  34. return node !== null && node.nodeType === node.ELEMENT_NODE;
  35. }
  36. function isHTMLTableCaptionElement(node) {
  37. return isElement(node) && getLocalName(node) === "caption";
  38. }
  39. function isHTMLInputElement(node) {
  40. return isElement(node) && getLocalName(node) === "input";
  41. }
  42. function isHTMLOptGroupElement(node) {
  43. return isElement(node) && getLocalName(node) === "optgroup";
  44. }
  45. function isHTMLSelectElement(node) {
  46. return isElement(node) && getLocalName(node) === "select";
  47. }
  48. function isHTMLTableElement(node) {
  49. return isElement(node) && getLocalName(node) === "table";
  50. }
  51. function isHTMLTextAreaElement(node) {
  52. return isElement(node) && getLocalName(node) === "textarea";
  53. }
  54. function safeWindow(node) {
  55. var _ref = node.ownerDocument === null ? node : node.ownerDocument,
  56. defaultView = _ref.defaultView;
  57. if (defaultView === null) {
  58. throw new TypeError("no window available");
  59. }
  60. return defaultView;
  61. }
  62. function isHTMLFieldSetElement(node) {
  63. return isElement(node) && getLocalName(node) === "fieldset";
  64. }
  65. function isHTMLLegendElement(node) {
  66. return isElement(node) && getLocalName(node) === "legend";
  67. }
  68. function isHTMLSlotElement(node) {
  69. return isElement(node) && getLocalName(node) === "slot";
  70. }
  71. function isSVGElement(node) {
  72. return isElement(node) && node.ownerSVGElement !== undefined;
  73. }
  74. function isSVGSVGElement(node) {
  75. return isElement(node) && getLocalName(node) === "svg";
  76. }
  77. function isSVGTitleElement(node) {
  78. return isSVGElement(node) && getLocalName(node) === "title";
  79. }
  80. /**
  81. *
  82. * @param {Node} node -
  83. * @param {string} attributeName -
  84. * @returns {Element[]} -
  85. */
  86. function queryIdRefs(node, attributeName) {
  87. if (isElement(node) && node.hasAttribute(attributeName)) {
  88. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- safe due to hasAttribute check
  89. var ids = node.getAttribute(attributeName).split(" ");
  90. return ids.map(function (id) {
  91. return node.ownerDocument.getElementById(id);
  92. }).filter(function (element) {
  93. return element !== null;
  94. } // TODO: why does this not narrow?
  95. );
  96. }
  97. return [];
  98. }
  99. function hasAnyConcreteRoles(node, roles) {
  100. if (isElement(node)) {
  101. return roles.indexOf((0, _getRole.default)(node)) !== -1;
  102. }
  103. return false;
  104. }
  105. //# sourceMappingURL=util.js.map