Home  >  Article  >  Web Front-end  >  jQuery selector source code interpretation (7): elementMatcher function_jquery

jQuery selector source code interpretation (7): elementMatcher function_jquery

WBOY
WBOYOriginal
2016-05-16 16:06:571077browse

To understand the Compile execution process of Sizzle, you first need to understand the functions and key variables and functions of each subroutine involved. I will explain the Compile code of jQuery-1.10.2 version one by one. I hope it can help you. .

elementMatcher(matchers)

1. Source code

Copy 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. Function

This function returns a function to determine whether the incoming elem matches the matching execution function array matchers. If it does not, it returns false, otherwise it returns true.

If matchers has only one element, return the element itself, otherwise return a new function - the function(elem, context, xml) function in the code.

The return function effect is somewhat similar to context.filter(selectors). Of course, the result only returns true or false, not a jQuery object.

3. Parameters
matchers - array, each element is a non-pseudo-class matcher execution function. For example: During the actual execution process, div.map span:lt(10), the matching execution functions of div and map will be passed into the elementMatcher function as the two elements of matchers to filter whether the parent node of the span node meets the requirements.

4. Return function

4.1 If matchers has more than 1 element, the following function will be returned:

Copy code 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 Function
Matchers are called sequentially from the last element to the first element to match whether the incoming elem nodes meet the requirements. True is returned if all are met, otherwise false is returned.

4.1.2 Parameters

elem - the single node element to be checked.

context - the context node that performs the entire selector string matching, most of the time it is useless.

xml——Is the current search object an HTML or XML document? If it is HTML, the xml parameter is false.

4.2 If matchers has only 1 element, return the element itself.
4.2.1 Function
Checks whether the incoming elem matches the selector, returns true if it matches, otherwise returns false.

4.2.2 Parameters
Same as 4.1.2 parameter description.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn