is-node-or-child.mjs 467 B

1234567891011121314151617181920
  1. /**
  2. * Recursively traverse up the tree to check whether the provided child node
  3. * is the parent or a descendant of it.
  4. *
  5. * @param parent - Element to find
  6. * @param child - Element to test against parent
  7. */
  8. const isNodeOrChild = (parent, child) => {
  9. if (!child) {
  10. return false;
  11. }
  12. else if (parent === child) {
  13. return true;
  14. }
  15. else {
  16. return isNodeOrChild(parent, child.parentElement);
  17. }
  18. };
  19. export { isNodeOrChild };