matches-selector.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * matchesSelector v2.0.2
  3. * matchesSelector( element, '.selector' )
  4. * MIT license
  5. */
  6. /*jshint browser: true, strict: true, undef: true, unused: true */
  7. ( function( window, factory ) {
  8. /*global define: false, module: false */
  9. 'use strict';
  10. // universal module definition
  11. if ( typeof define == 'function' && define.amd ) {
  12. // AMD
  13. define( factory );
  14. } else if ( typeof module == 'object' && module.exports ) {
  15. // CommonJS
  16. module.exports = factory();
  17. } else {
  18. // browser global
  19. window.matchesSelector = factory();
  20. }
  21. }( window, function factory() {
  22. 'use strict';
  23. var matchesMethod = ( function() {
  24. var ElemProto = window.Element.prototype;
  25. // check for the standard method name first
  26. if ( ElemProto.matches ) {
  27. return 'matches';
  28. }
  29. // check un-prefixed
  30. if ( ElemProto.matchesSelector ) {
  31. return 'matchesSelector';
  32. }
  33. // check vendor prefixes
  34. var prefixes = [ 'webkit', 'moz', 'ms', 'o' ];
  35. for ( var i=0; i < prefixes.length; i++ ) {
  36. var prefix = prefixes[i];
  37. var method = prefix + 'MatchesSelector';
  38. if ( ElemProto[ method ] ) {
  39. return method;
  40. }
  41. }
  42. })();
  43. return function matchesSelector( elem, selector ) {
  44. return elem[ matchesMethod ]( selector );
  45. };
  46. }));