util.mjs 2.9 KB

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