This article mainly introduces the jQuery selector Source Code explanation (7): elementMatcher function. This article describes the source code, functions, parameters, and return functions, for more information about the Compile execution process of Sizzle, you must first understand the functions and key variables and functions of the involved subprograms, I will explain the Compile code of the jQuery-1.10.2 version one by one, hoping to help you.
ElementMatcher (matchers)
1. source code
The code is as follows:
Function elementMatcher (matchers ){
Return matchers. length> 1? Function (elem, context, xml ){
Var I = matchers. length;
While (I --){
If (! Matchers [I] (elem, context, xml )){
Return false;
}
}
Return true;
}: Matchers [0];
}
2. Functions
This function returns a function to determine whether the incoming elem matches the matching execution function array matchers. if not, false is returned; otherwise, true is returned.
If matchers has only one element, the element itself is returned. Otherwise, a new function is returned, that is, the function (elem, context, xml) function in the code.
The returned function is similar to context. filter (selectors). of course, only true or false is returned, not the jQuery object.
3. parameters
Matchers -- array. each element is a non-pseudo-class matcher that executes the function. For example, during actual execution. map span: lt (10). the matching execution function of p and map will pass in the elementMatcher function as two elements of matchers to filter out whether the parent node of the span node meets the requirements.
4. return functions
4.1 if matchers has more than one element, the following function is returned:
The code is as follows:
Function (elem, context, xml ){
Var I = matchers. length;
While (I --){
If (! Matchers [I] (elem, context, xml )){
Return false;
}
}
Return true;
}
4.1.1 functions
Call from the last matchers element to the first element to match whether the incoming elem node meets the requirements. true is returned if all the incoming elem nodes meet the requirements. otherwise, false is returned.
4.1.2 parameters
Elem-single node element to be checked.
Context -- the context node that executes the entire selector string matching. it is useless in most cases.
Xml: whether the current search object is an HTML or an XML document. if it is an HTML document, the xml parameter is false.
4.2 if matchers has only one element, the element itself is returned.
4.2.1 functions
Check whether the imported elem matches the selector. If yes, true is returned. otherwise, false is returned.
4.2.2 parameters
Same as 4.1.2 parameter description.