Skip to content

Commit

Permalink
Use native Element#matchesSelector when available.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Dean committed Aug 7, 2012
1 parent 16d4d67 commit 91b2778
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions index.js
@@ -1,6 +1,9 @@
;(function() {
'use strict';

var slice = Array.prototype.slice;
var styleSheetElement;

function camelize(string) {
return string.replace(/-+(.)?/g, function(match, chr) {
return chr ? chr.toUpperCase() : '';
Expand All @@ -19,8 +22,20 @@
return void(0) === value;
}

var slice = Array.prototype.slice;
var styleSheetElement;
var proto = (typeof Element != 'undefined') ? Element.prototype : {} ;

// Check for native `matchesSelector` method:
var matchesSelector = proto.matchesSelector || proto.webkitMatchesSelector
|| proto.mozMatchesSelector || proto.msMatchesSelector || proto.oMatchesSelector;

if (!matchesSelector) {
// No native `matchesSelector` method; create userland alternative:
matchesSelector = function(selector) {
var matches = this.parentNode.querySelectorAll(selector);
return slice.call(matches).indexOf(this) > -1;
};
}

var Extensions = {

/**
Expand Down Expand Up @@ -217,8 +232,7 @@
if (selector) {
var current = event.target;
while(current !== scope && match == null) {
var matches = current.parentNode.findAll(selector);
if (slice.call(matches).indexOf(current) > -1) {
if (matchesSelector.call(current, selector)) {
match = current;
} else {
current = current.parentNode;
Expand Down

0 comments on commit 91b2778

Please sign in to comment.