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.

25 lines
697 B

2 months ago
  1. // @flow
  2. import { isShadowRoot } from './instanceOf';
  3. export default function contains(parent: Element, child: Element) {
  4. const rootNode = child.getRootNode && child.getRootNode();
  5. // First, attempt with faster native method
  6. if (parent.contains(child)) {
  7. return true;
  8. }
  9. // then fallback to custom implementation with Shadow DOM support
  10. else if (rootNode && isShadowRoot(rootNode)) {
  11. let next = child;
  12. do {
  13. if (next && parent.isSameNode(next)) {
  14. return true;
  15. }
  16. // $FlowFixMe[prop-missing]: need a better way to handle this...
  17. next = next.parentNode || next.host;
  18. } while (next);
  19. }
  20. // Give up, the result is false
  21. return false;
  22. }