JQuery selector code (2) -- select method, jquery Selector

Source: Internet
Author: User

JQuery selector code (2) -- select method, jquery Selector

Original article. Please indicate the source for reprinting. Thank you!

/** 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 parse the selector * 2. For a single block selector without an initial set (that is, the seed has no value assigned) (that is, there is no comma in the selector string ), * Do the following: * 1) if the first selector is ID and the context is document, directly obtain the object to replace the passed-in context object * 2) if the selector is a single selector, if it is of the id, class, or tag type, the corresponding DOM element is directly obtained and returned. * 3) obtain the matching DOM element of the last id, class, and tag type selector and assign it to the initial set (that is, the seed variable) * 4). Call the compile method to obtain and execute the pre-compiled code, get and return the matched DOM element ** @ param selector removes the leading and trailing blank selector string * @ param context to execute the initial context of the matching (that is, DOM Element Set ). If the context value is not assigned, the document is used. * @ Param results: the final result of the matched part. If results is not assigned a value, an empty array is assigned. * @ Param seed initial set */function select (selector, context, results, seed) {var I, tokens, token, type, find, // call the tokenize function to parse selectormatch = tokenize (selector); // if the initial set if (! Seed) {// Try to minimize operations if there is only one group // if there is only one group of selectors, that is, the selector string has no comma if (match. length = 1) {// Take a shortcut cut and set the context if the root selector // is an ID/** the following code is used to process the root selector as an ID shortcut ** use slice here [0] Create a new set, * Make sure that the original set will not be changed after the Code */tokens = match [0] = match [0]. slice (0);/** if the selector is of the id type and the second is a relational character (I .e., Or space). * The object to which the id belongs is used as the context to continue matching. ** the condition judgment here is: * tokens. length> 2: If tokens has more than two selectors * (token = tokens [0]). type = "ID": the type of the first selector is ID (starting with #), * support. getById: supports the getElementById function * context. nodeType = 9: the context object is document * documentIsHTML: the current processing is the HTML code * Expr. relative [tokens [1]. type]: The second tokens element is a link (I .e., + ~> Or space) * if all the preceding conditions are met, execute the statement body in if */if (tokens. length> 2 & (token = tokens [0]). type = "ID" & support. getById & context. nodeType === 9 & documentIsHTML & Expr. relative [tokens [1]. type]) {// point the current context to the Node object specified by the first ID selector context = (Expr. find ["ID"] (token. matches [0]. replace (runescape, funescape), context) | []) [0]; // if no ID object is specified in the current context, resultsif (! Context) {return results;} // remove the first ID selector = selector from the selector string. slice (tokens. shift (). value. length );} // Fetch a seed set for right-to-left matching/** the while LOOP below is used to obtain the initial set based on the last id, class, and tag type selector * for Example simple Example: if the selector is "div [title = '2']", * the code obtains all div nodes under the context based on the div and assigns this set to the seed variable, * Then, call the compile function to generate the pre-compiled code. * The pre-compiled code completes the matching of [title = '2'] in the above initial set. ** first, check whether the selector string contains any character that matches the needsContext Regular Expression * if no character exists, the selector filters D from right to left. OM node * Otherwise, run the program into pre-compiled code (call 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 whether the selector string contains the following content: * 1.> + ~ Three Relational operators * 2,: even,: odd,: eq,: gt,: lt,: nth,: first,: last, eight pseudo Classes ,(? = [^-] | $) Is used to filter out objects similar: other selector with a first-child or a middle bar that starts with the preceding eight words */I = matchExpr ["needsContext"]. test (selector )? 0: tokens. length; while (I --) {token = tokens [I]; // Abort if we hit a combinator // 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 character */if (seed = find (token. matches [0]. replace (runescape, funescape), rsibling. test (tokens [0]. type) & context. parentNode | context) {// If seed is empty or no tokens remain, we can // return early // remove the recently used selector tokens. splice (I, 1); selector = seed. length & toSelector (tokens);/** if selector is empty, the selector is only of the single id, class, and tag type. * The obtained result is returned directly. Otherwise, continue matching */if (! Selector) {push. apply (results, seed); return results;} break ;}}}}} // Compile and execute a filtering function // Provide 'match' to avoid retokenization if we modified the // selector above/** first execute compile (selector, match ), it returns a pre-compiled function, * and then calls this function to obtain the final matching result */compile (selector, match) (seed, context ,! DocumentIsHTML, results, rsibling. test (selector); return results ;}

Dear friends, if you think it's a good job, help me with it and give me some motivation. Thank you!


JQuery selector code (1) -- Sizzle Method

JQuery selector code (2) -- select Method

JQuery selector code (3) -- tokenize Method

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.