Recently saw some online analysis of the Sizzle article, on the matching order is often said to use the reverse matching from right to left, but how the specific and not detailed, or as I have several articles before, on the line of code to do a detailed introduction, but the lack of a holistic concept, Here is the jQuery-1.10.2 version of the Sizzle matching logic (precompiled results) to do a whole description, here is not to talk about too much detail.
Sizzle's matching process is based on an improved version of Right-to-left reverse matching, since HTML searches, after all, differ from text matching, and it has its own unique side, so it needs to be optimized for HTML searches. First of all, the following said the relationship selector refers to the combinator selector in the world, because I think the relationship selector is more close to the actual meaning of the name, so use this name.
1, a brief introduction to the sizzle compilation of the implementation of the two main functions:
A) matcherfromtokens--generates execution functions for a block selector, which is called a block selector that does not contain a comma-delimited selector string.
b matcherfromgroupmatchers--the final execution function generated by the different block selectors, which is also responsible for filtering the final results out of duplicate objects.
2. The Matcherfromtokens function produces different execution functions for different types of selectors . If a pseudo class is included, the Setmatcher is returned, otherwise the Elementmatcher is returned, and the code distinguishes Setmatcher and Elementmatcher by identifying whether Matcher contains expando attributes:
(a) for non-pseudo classes and non-relational selectors, the execution functions are generated directly from left to right, and each function acts as a different element of the same matchers array.
(b) for the relationship selector, the previously generated matchers will be pressed into a new matchers array.
C for the Pseudo class selector, an executive function is generated via the Setmatcher function, and the call to Setmatcher is followed by 6 parameters, namely Prefilter, selector, Matcher, Postfilter, Postfinder, Postselector.
Prefilter is the final function that was processed by the Elementmatcher function of the matchers array that was generated before the Setmatcher function was executed, Elementmatcher (matchers) Returns a new function that executes each matchers element function sequentially from the back forward;.
Selector is the corresponding selector string for matchers;
Matcher is a matching function of pseudo class itself;
Postfilter is a pseudo class, the matching function that corresponds to the selector string between the first pseudo class or the relationship character, which is the result of the return of the nested call matcherfromtokens function;
Postfinder is the matching function generated by all selectors after the postfilter corresponding selector, and is also the result of the return of the Matcherfromtokens function by nesting;
Postselector is the corresponding selector string for Postfinder.
D If there is no pseudo class in the selector string, the final matching function generated by Elementmatcher (Matchers) will be returned.
As can be seen from the above introduction, there is a nesting relationship between the generated execution functions, simply speaking, Setmatcher contains matchers, and the relationship selector matching function contains a non pseudo class and a non relational selector matching function.
3, the implementation process of the introduction:
A executes the execution function of the block selector:
For Elementmatcher, from the outside to the back to the first sequence, from the outermost array to the innermost array executed sequentially, in the same array, from the last element to the first element executed sequentially.
For Setmatcher, the matching result is obtained first according to Prefilter and selector, then the Matcher function gets the matching result, then executes the Postfilter function, and finally, Obtain the matching results based on Postfinder and Postselector.
c) After executing the execution function of each block selector sequentially, filter out the duplicate data and return the result.
Looking at the general process, and then looking at the detailed description of each method should be easier to understand, of course, there is no mention of some details, such as the initial result set (seed) generation and the resulting function of the implementation of the logical details of the differences.