Implementation of matchesselector matching selector expression sizzle

Source: Internet
Author: User

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.