You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
759 B

2 months ago
  1. import getNodeName from "./getNodeName.js";
  2. import getDocumentElement from "./getDocumentElement.js";
  3. import { isShadowRoot } from "./instanceOf.js";
  4. export default function getParentNode(element) {
  5. if (getNodeName(element) === 'html') {
  6. return element;
  7. }
  8. return (// this is a quicker (but less type safe) way to save quite some bytes from the bundle
  9. // $FlowFixMe[incompatible-return]
  10. // $FlowFixMe[prop-missing]
  11. element.assignedSlot || // step into the shadow DOM of the parent of a slotted node
  12. element.parentNode || ( // DOM Element detected
  13. isShadowRoot(element) ? element.host : null) || // ShadowRoot detected
  14. // $FlowFixMe[incompatible-call]: HTMLElement is a Node
  15. getDocumentElement(element) // fallback
  16. );
  17. }