Sizzle. matchesselector = function (node, expr ){
Return sizzle (expr, null, null, [node]). length> 0;
};
(Function (){
VaR html = document.doc umentelement,
Matches = html. matchesselector | html. mozmatchesselector | html. webkitmatchesselector | html. msmatchesselector;
If (matches ){
// Check to see if it's possible to do matchesselector
// On a disconnected node (ie 9 fails this)
VaR disconnectedmatch =! Matches. Call (document. createelement ("Div"), "Div "),
Pseudo doworks = false;
Try {
// This shoshould fail with an exception
// Gecko does not error, returns false instead
Matches. Call (document.doc umentelement, "[test! = '']: Sizzle ");
} Catch (pseudo error ){
Pseudo doworks = true;
}
Sizzle. matchesselector = function (node, expr ){
// Make sure that attribute selectors are quoted
Expr = expr. Replace (/\ = \ s * ([^ '"\] *) \ s * \]/g," =' $ 1'] ");
If (! Sizzle. isxml (node )){
Try {
If (pseudo works |! Expr. Match. pseudo. Test (expr )&&! /! =/. Test (expr )){
VaR ret = matches. Call (node, expr );
// Ie 9's matchesselector returns false on disconnected nodes
If (Ret |! Disconnectedmatch |
// As well, disconnected nodes are said to be in a document
// Fragment in IE 9, so check for that
Node.doc ument & node.doc ument. nodetype! = 11 ){
Return ret;
}
}
} Catch (e ){}
}
Return sizzle (expr, null, null, [node]). length> 0;
};
}
})();
On GitHub, we can see that the sample implementation code is
(Function (global, elemproto ){
VaR matchesmethod = (function (){
If (elemproto. matchesselector ){
Return 'matchesselector ';
}
VaR prefixes = ['webkit', 'moz', 'ms', 'O'];
For (VAR I = 0, Len = prefixes. length; I <Len; I ++ ){
VaR prefix = prefixes [I];
VaR method = prefix + 'matchesselector ';
If (elemproto [Method]) {
Return method;
}
}
})();
Console. Log ('abc ');
// ----- Match -----//
Function match (ELEM, selector ){
Return ELEM [matchesmethod] (selector );
}
// ----- Appendtofragment -----//
Function checkparent (ELEM ){
// Not needed if already has parent
If (ELEM. parentnode ){
Return;
}
VaR fragment = Document. createdocumentfragment ();
Fragment. appendchild (ELEM );
}
Function query (ELEM, selector ){
// Append to fragment if no parent
Checkparent (ELEM );
// Match ELEM with all selected elems of parent
VaR elems = ELEM. parentnode. queryselectorall (selector );
For (VAR I = 0, Len = elems. length; I <Len; I ++ ){
// Return true if match
If (elems [I] === ELEM ){
Return true;
}
}
// Otherwise return false
Return false;
}
// ----- Matchchild -----//
Function matchchild (ELEM, selector ){
Checkparent (ELEM );
Return match (ELEM, selector );
}
// ----- Matchesselector -----//
VaR matchesselector;
If (matchesmethod ){
VaR DIV = Document. createelement ('div ');
VaR supportsorphans = match (DIV, 'div ');
Matchesselector = supportsorphans? Match: matchchild;
} Else {
Matchesselector = query;
}
Console. Log (matchesselector );
// Transport
If (typeof define ==== 'function' & define. AMD ){
// Amd
Define ('matches-Selector/matches-Selector ', [], function (){
Return matchesselector;
});
} Else {
// Browser global
Window. matchesselector = matchesselector;
}
}) (This, element. Prototype );
Source Address: https://gist.github.com/3062955