Permalink
Browse files

feat(closest polyfill): added closest and matches polyfill

  • Loading branch information...
lara-aigmueller committed Sep 12, 2016
1 parent 11cc603 commit a60f5f9f08de70e41dbd50693f7a6d11f78bebc1
Showing with 30 additions and 0 deletions.
  1. +28 −0 src/js-common-components/closest.js
  2. +2 −0 src/main.js
@@ -0,0 +1,28 @@
// matches polyfill
const matchesPolyfill = function applyMatchesPolyfill(proto) {
const ElementPrototype = proto;
ElementPrototype.matches = ElementPrototype.matches ||
ElementPrototype.matchesSelector ||
ElementPrototype.webkitMatchesSelector ||
ElementPrototype.msMatchesSelector ||
function matches(selector) {
const node = this;
const nodes = (node.parentNode || node.document).querySelectorAll(selector);
let i = -1;
while (nodes[++i] && nodes[i] !== node);
return !!nodes[i];
};
};
matchesPolyfill(Element.prototype);
// closest polyfill
const closestPolyfill = function applyClosestPolyfill(proto) {
const ElementPrototype = proto;
ElementPrototype.closest = ElementPrototype.closest ||
function closest(selector) {
let el = this;
while (el.matches && !el.matches(selector)) el = el.parentNode;
return el.matches ? el : null;
};
};
closestPolyfill(Element.prototype);
View
@@ -1,2 +1,4 @@
require('./js-common-components/toggler');
require('./js-common-components/closest');
require('./expandable/expandable');
require('./table-expandable/table-expandable');

0 comments on commit a60f5f9

Please sign in to comment.