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.

22 lines
677 B

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