containsNode.js 923 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. *
  9. */
  10. var isTextNode = require('./isTextNode');
  11. /*eslint-disable no-bitwise */
  12. /**
  13. * Checks if a given DOM node contains or is another DOM node.
  14. */
  15. function containsNode(outerNode, innerNode) {
  16. if (!outerNode || !innerNode) {
  17. return false;
  18. } else if (outerNode === innerNode) {
  19. return true;
  20. } else if (isTextNode(outerNode)) {
  21. return false;
  22. } else if (isTextNode(innerNode)) {
  23. return containsNode(outerNode, innerNode.parentNode);
  24. } else if ('contains' in outerNode) {
  25. return outerNode.contains(innerNode);
  26. } else if (outerNode.compareDocumentPosition) {
  27. return !!(outerNode.compareDocumentPosition(innerNode) & 16);
  28. } else {
  29. return false;
  30. }
  31. }
  32. module.exports = containsNode;