wp-polyfill-element-closest.js 895 B

123456789101112131415161718192021222324252627282930313233
  1. // element-closest | CC0-1.0 | github.com/jonathantneal/closest
  2. (function (ElementProto) {
  3. if (typeof ElementProto.matches !== 'function') {
  4. ElementProto.matches = ElementProto.msMatchesSelector || ElementProto.mozMatchesSelector || ElementProto.webkitMatchesSelector || function matches(selector) {
  5. var element = this;
  6. var elements = (element.document || element.ownerDocument).querySelectorAll(selector);
  7. var index = 0;
  8. while (elements[index] && elements[index] !== element) {
  9. ++index;
  10. }
  11. return Boolean(elements[index]);
  12. };
  13. }
  14. if (typeof ElementProto.closest !== 'function') {
  15. ElementProto.closest = function closest(selector) {
  16. var element = this;
  17. while (element && element.nodeType === 1) {
  18. if (element.matches(selector)) {
  19. return element;
  20. }
  21. element = element.parentNode;
  22. }
  23. return null;
  24. };
  25. }
  26. })(window.Element.prototype);