Created
November 15, 2016 06:18
-
-
Save pentaphobe/a810c4fbce63fc715aa4e8e1195552ef to your computer and use it in GitHub Desktop.
Filter function for Angular's jqlite element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adds a filter() method to Angular's jqlite | |
*/ | |
(function (angular) { | |
/** | |
* Polyfill from MDN (mostly to cover IE9 and edge browsers) | |
* https://developer.mozilla.org/en/docs/Web/API/Element/matches | |
*/ | |
if (!Element.prototype.matches) { | |
Element.prototype.matches = | |
Element.prototype.matchesSelector || | |
Element.prototype.mozMatchesSelector || | |
Element.prototype.msMatchesSelector || | |
Element.prototype.oMatchesSelector || | |
Element.prototype.webkitMatchesSelector || | |
function(s) { | |
var matches = (this.document || this.ownerDocument).querySelectorAll(s), | |
i = matches.length; | |
while (--i >= 0 && matches.item(i) !== this) {} | |
return i > -1; | |
}; | |
} | |
/** | |
* END OF MDN Polyfill | |
*/ | |
/** | |
* Does the actual work of providing a basic selector filter | |
*/ | |
angular.element.prototype.filter = function (selector) { | |
return angular.element( | |
Array.prototype.filter.call(this, function (el) { | |
return el && (el instanceof Element) && | |
el.nodeType === 1 && | |
el.matches(selector); | |
}) | |
); | |
}; | |
}(angular)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment