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.

21 lines
789 B

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