The jquery selector source code interpretation (ii): Select Method _jquery

Source: Internet
Author: User
Tags new set
The/* Select method is one of the core methods of the Sizzle selector package. It mainly completes the following tasks: * 1, call the Tokenize method to complete the resolution of the selector * 2, for no initial collection (that is, seed is not assigned) and is a single block selector (that is, the selector string does not have commas), *
 Complete the following: * 1 for the first selector is the ID type and the context is document, the direct fetch object overrides the incoming context object * 2) If the selector is a single selector and is an ID, class, tag type, fetch and return the matching DOM element directly  * 3) Get the last ID, class, Tag type selector matching DOM element assignment to the initial collection (i.e. seed variable) * 3, get "precompiled" code by calling the compile method and execute, get and return the matching DOM element * * @param selector Has gone to the tail blank selector String * @param the context to perform the matching initial contexts (that is, the DOM element collection).
 If the context is not assigned a value, the document is taken. * @param results has been matched with a partial final result.
 An empty array is assigned if the results is not assigned a value. * @param seed Initial set/function Select (Selector, context, results, seed) {var I, tokens, token, type, find,//Call Tok

	enize function parsing Selector match = tokenize (selector); If you do not provide an initial set if (!seed) {//Try to minimize operations if there be only one group///If there are only a set of selectors, that is, the selector string has no comma if (MATC H.length = = 1) {//Take a shortcut and set the context if the ' root selector/IS ' an ID/* * The following code is used to process the root selector is Shortcut for ID type * * use slice[0 here to create a new set, * Make sure that the original collection will not be changed by the following code/tokens = Match[0] = match[0].slice (0);  * * If the selector starts with the ID type, and the second is a relationship character (that is, a +~> or a space), * Gets the ID's object as the context continues to complete the subsequent match * * Here's The Conditional judgment: * tokens.length > 2: If tokens has more than two selectors * (token = tokens[0]). Type = = "id": the type of the first selector is ID (that is, start with #), * Support.getbyid: Support Geteleme Ntbyid function * Context.nodetype = = = 9:context object is document * documentishtml: Current processing is HTML code * EXPR.RELATIVE[TOKENS[1] . Type]: The second tokens element is a relationship (that is, a +~> or a space) * If all of the above conditions are met, the statement body within the IF (Tokens.length > 2 && (token = Tokens[0]). Type = = "ID" && support.getbyid && context.nodetype = 9 && documentishtml && Expr.relative[tokens[1].type]) {//To point the current context to the Node object context = (expr.find["id") specified by the first ID selector (token.matches[0 ].replace (RuneScape, Funescape), context) | |
				
				[]) [0];
				If no ID object is specified within the current context, the results if (!context) {return results is returned directly; }//Selector string remove the first id selector selector = selector.slice (Tokens.shift (). Value.lengTH); //Fetch A seed set for right-to-left matching/* The function of the while loop below is to get the initial set based on the last ID, class, Tag type selector * To give a simple Example: If the selector is "div[title= ' 2", the * code obtains all the div nodes under the context according to the DIV and assigns the set to the seed variable, * then calls the compile function, produces the precompiled code, * The precompiled code completes in the above Perform a match of [title= ' 2 '] in the initial collection * First, check the selector string for characters that match the needscontext regular expression * If not, then filter the DOM node from right to left according to selector * Otherwise, you will be the precompiled generation 
			 After code execution (calling the Compile method). * * * * "Needscontext": New RegExp ("^" + whitespace * + "*[>+~]|:( Even|odd|eq|gt|lt|nth|first|last) (?: \ \ ("* + whitespace +" * (?:-\ \d) \\d*) "+ whitespace * +" *\\) |) (? =[^-]|$) "," I ") * Needscontext is used to match the following in the selector string: * 1, >+~ three relationship characters * 2,: Even,: Odd, EQ,: GT,: LT,: Nth,: First,: l AST eight pseudo classes * which, (? =[^-]|$) are used to filter out similar to: First-child and other selectors with the above eight words to begin with, * * i = matchexpr["Needscontext"].test (selector ) ?
			0:tokens.length;

				while (i--) {token = tokens[i]; Abort if we hit a combinator//encounters a relationship break out of the loop if (expr.relative[(type = Token.typE)] {break;
					 } if (find = Expr.find[type]) {//Search, expanding context for leading sibling//combinators/* * rsibling = new RegExp (whitespace + "*[+~]") * rsibling is used to determine whether the token selector is a sibling/if (seed = Find (toke N.matches[0].replace (RuneScape, Funescape), rsibling. Test (tokens[0].type) && context.parent Node | | Context))} {//If seed is empty or no tokens remain, we can//return early//remove the newly used selector tokens.
						Splice (i, 1);
						selector = seed.length && toselector (tokens);
							* * If the selector is empty, stating that the selector is only a single ID, class, tag type, * So directly return the results obtained, otherwise, in the acquisition of seed on the basis of continuing to match * */if (!selector) {
							Push.apply (results, seed);
						return results;
					} break; }}}//Compile and execute a filtering function//provide ' match ' to avoid retokenization if we modifi Ed the//selector above * * First executes compile (selector, match), which returns a "precompiled"function, * and then call the function to obtain the final matching result/compile (selector, match) (seed, context,!documentishtml, results, rsibling.test (selector)
	);
return results; }

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.