


jQuery selector source code interpretation (5): parsing process of tokenize_jquery
The following analysis is based on jQuery-1.10.2.js version.
The following will take $("div:not(.class:contain('span')):eq(3)") as an example to explain how the tokenize and preFilter codes are coordinated to complete the parsing. If you want to know the detailed explanation of each line of code of the tokenize method and preFilter class, please refer to the following two articles:
http://www.jb51.net/article/63155.htm
http://www.jb51.net/article/63163.htm
The following is the source code of the tokenize method. For simplicity, I have removed all the codes related to caching, comma matching and relational character matching, leaving only the core code related to the current example. The code that was removed is very simple. If necessary, you can read the above article.
In addition, the code is written above the description text.
function tokenize(selector, parseOnly) {
var matched, match, tokens, type, soFar, groups, preFilters;
soFar = selector;
groups = [];
preFilters = Expr.preFilter;
while (soFar) {
if (!matched) {
groups.push(tokens = []);
}
matched = false;
for (type in Expr.filter) {
If ((match = matchExpr[type].exec(soFar))
&& (!preFilters[type] || (match = preFilters[type]
(match)))) {
Matched = match.shift();
tokens.push({
Value: matched,
Type : type,
matches: match
});
SoFar = soFar.slice(matched.length);
}
}
if (!matched) {
Break;
}
}
return parseOnly ? soFar.length : soFar ? Sizzle.error(selector) :
tokenCache(selector, groups).slice(0);
}
First, tokenize is called for the first time by the select method during jQuery execution, and "div:not(.class:contain('span')):eq(3)" is passed into the method as the selector parameter.
soFar = selector;
soFar = "div:not(.class:contain('span')):eq(3)"
When entering the while loop for the first time, since matched has not been assigned a value, the following statement body in the if is executed. This statement will initialize the tokens variable and push tokens into the groups array.
groups.push(tokens = []);
After that, enter the for statement.
The first for loop: take the first element "TAG" from Expr.filter and assign it to the type variable, and execute the loop body code.
If ((match = matchExpr[type].exec(soFar))
&& (!preFilters[type] || (match = preFilters[type]
(match)))) {
The execution result of match = matchExpr[type].exec(soFar) is as follows:
match =["div", "div"]
The first selector in the example is div, which matches the regular expression of matchExpr["TAG"], and preFilters["TAG"] does not exist, so the statement body within the if is executed.
matched = match.shift();
Remove the first element div in the match and assign the element to the matched variable. At this time, matched="div", match = ["div"]
tokens.push({
Value: matched,
Type : type,
matches: match
}
Create a new object { value: "div", type: "TAG", matches: ["div"] } and push the object into the tokens array.
SoFar = soFar.slice(matched.length);
The soFar variable deletes the div. At this time, soFar=":not(.class:contain('span')):eq(3)"
The second for loop: Take the second element "CLASS" from Expr.filter and assign it to the type variable, and execute the loop body code.
If ((match = matchExpr[type].exec(soFar))
&& (!preFilters[type] || (match = preFilters[type]
(match)))) {
Since the current soFar=":not(.class:contain('span')):eq(3)" does not match the regular expression of CLASS type, this loop ends.
The third for loop: Take the third element "ATTR" from Expr.filter and assign it to the type variable, and execute the loop body code.
Similarly, since the current remaining selectors are not attribute selectors, this cycle ends.
The fourth for loop: Take the fourth element "CHILD" from Expr.filter and assign it to the type variable, and execute the loop body code.
Similarly, since the current remaining selector is not a CHILD selector, this cycle ends.
The fifth for loop: Take the fifth element "PSEUDO" from Expr.filter and assign it to the type variable, and execute the loop body code.
If ((match = matchExpr[type].exec(soFar))
&& (!preFilters[type] || (match = preFilters[type]
(match)))) {
The execution result of match = matchExpr[type].exec(soFar) is as follows:
[":not(.class:contain('span')):eq(3)", "not", ".class:contain('span')):eq(3", undefined, undefined, undefined, undefined , undefined, undefined, undefined, undefined]
Since preFilters["PSEUDO"] exists, the following code is executed:
match = preFilters[type](match)
preFilters["PSEUDO"] code is as follows:
"PSEUDO" : function(match) {
var excess, unquoted = !match[5] && match[2];
if (matchExpr["CHILD"].test(match[0])) {
return null;
}
if (match[3] && match[4] !== undefined) {
match[2] = match[4];
} else if (unquoted
&& rpseudo.test(unquoted)
&& (excess = tokenize(unquoted, true))
&& (excess = unquoted.indexOf(")", unquoted.length
- excess)
- unquoted.length)) {
match[0] = match[0].slice(0, excess);
match[2] = unquoted.slice(0, excess);
}
return match.slice(0, 3);
}
The match parameter passed in is equal to:
[":not(.class:contain('span')):eq(3)", "not", ".class:contain('span')):eq(3", undefined, undefined, undefined, undefined , undefined
unquoted = !match[5] && match[2]
unquoted = ".class:contain('span')):eq(3"
if (matchExpr["CHILD"].test(match[0])) {
Return null;
}
match[0] = ":not(.class:contain('span')):eq(3)", does not match the matchExpr["CHILD"] regular expression, and does not execute the return null statement.
if (match[3] && match[4] !== undefined) {
Match[2] = match[4];
}
Since match[3] and match[4] are both equal to undefined, the else statement body is executed.
else if (unquoted
&& rpseudo.test(unquoted)
&& (excess = tokenize(unquoted, true))
&& (excess = unquoted.indexOf(")", unquoted.length - excess) - unquoted.length)
At this time, unquoted = ".class:contain('span')):eq(3" is true, and because unquoted contains:contain('span'), it matches the regular expression rpseudo, so rpseudo. test(unquoted) is true, and then call tokenize again to parse unquoted again, as follows:
excess = tokenize(unquoted, true)
When calling the tokenize function this time, the incoming selector parameter is equal to ".class:contain('span')):eq(3", and parseOnly is equal to true. The execution process in the function body is as follows:
soFar = selector;
soFar = ".class:contain('span')):eq(3"
When entering the while loop for the first time, since matched has not been assigned a value, the following statement body in the if is executed. This statement will initialize the tokens variable and push tokens into the groups array.
groups.push(tokens = []);
, enter the for statement.
The first for loop: take the first element "TAG" from Expr.filter and assign it to the type variable, and execute the loop body code.
if ((match = matchExpr[type].exec(soFar))
&& (!preFilters[type] || (match = preFilters[type]
(match)))) {
Since the current remaining selector is not a TAG selector, this cycle ends.
The second for loop: Take the second element "CLASS" from Expr.filter and assign it to the type variable, and execute the loop body code.
The execution result of match = matchExpr[type].exec(soFar) is as follows:
match = ["class" , "class"]
Since preFilters["CLASS"] does not exist, the statement body within the if is executed.
matched = match.shift();
Remove the first element class in match and assign the element to the matched variable. At this time, matched="class", match = ["class"]
tokens.push({
value : matched,
Type : type,
matches : match
}
Create a new object { value: "class", type: "CLASS", matches: ["class"] } and push the object into the tokens array.
soFar = soFar.slice(matched.length);
The soFar variable deletes the class. At this time, soFar = ":contain('span')):eq(3"
The third for loop: Take the third element "ATTR" from Expr.filter and assign it to the type variable, and execute the loop body code.
Similarly, since the current remaining selectors are not attribute selectors, this cycle ends.
The fourth for loop: Take the fourth element "CHILD" from Expr.filter and assign it to the type variable, and execute the loop body code.
Similarly, since the current remaining selector is not a CHILD selector, this cycle ends.
The fifth for loop: Take the fifth element "PSEUDO" from Expr.filter and assign it to the type variable, and execute the loop body code.
if ((match = matchExpr[type].exec(soFar))
&& (!preFilters[type] || (match = preFilters[type]
(match)))) {
The execution result of match = matchExpr[type].exec(soFar) is as follows:
[":contain('span')", "contain", "'span'", "'", "span", undefined, undefined, undefined, undefined, undefined, undefined]
Since preFilters["PSEUDO"] exists, the following code is executed:
match = preFilters[type](match)
The preFilters["PSEUDO"] code is shown above and will not be listed here.
"PSEUDO" : function(match) {
var excess, unquoted = !match[5] && match[2];
If (matchExpr["CHILD"].test(match[0])) {
return null; }
If (match[3] && match[4] !== undefined) {
match[2] = match[4];
} else if (unquoted
&& (excess = tokenize(unquoted, true)) && (excess = unquoted.indexOf(")", unquoted.length
- excess)
- unquoted.length)) {
match[0] = match[0].slice(0, excess);
match[2] = unquoted.slice(0, excess);
}
Return match.slice(0, 3);
}
The incoming match parameter is equal to:
unquoted = "span"
Because ":contain('span')" does not match the matchExpr["CHILD"] regular expression, the internal statement body is not executed.
if (match[3] && match[4] !== undefined) {
match[2] = match[4];
}
Since match[3] = "'" and match[4] ="span", the internal if statement body is executed and "span" is assigned to match[2]
return match.slice(0, 3);
Returns a copy of the first three elements of match
At this time, return to the for loop of the tokenize method to continue execution. At this time, the values of each variable are as follows:
match = [":contain('span')", "contain", "span"]
soFar = ":contain('span')):eq(3"
matched = match.shift();
Remove ":contain('span')" from the match array and assign it to the matched variable
tokens.push({
value : matched,
Type : type,
matches : match
}
Create a new object { value:
":contain('span')", type:"PSEUDO", matches: ["contain", "span"] }, and push the object into the tokens array.
soFar = soFar.slice(matched.length);
The soFar variable deletes ":contain('span')". At this time, soFar="):eq(3)", after that, until the for loop ends and the while loop is executed again, there is no valid selector. So exit the while loop.
return parseOnly ? soFar.length : soFar ? Sizzle.error(selector) :
tokenCache(selector, groups).slice(0);
Since parseOnly = true at this time, the length of soFar at this time is returned, 6, and the code of preFilters["PSEUDO"] continues to be executed
else if (unquoted
&& rpseudo.test(unquoted)
&& (excess = tokenize(unquoted, true))
&& (excess = unquoted.indexOf(")", unquoted.length - excess) - unquoted.length)
Assign 6 to the excess variable, and then the code
excess = unquoted.indexOf(")", unquoted.length - excess) - unquoted.length
Calculate: not selector end position (i.e. right bracket position) 22
match[0] = match[0].slice(0, excess);
match[2] = unquoted.slice(0, excess);
Calculate the complete :not selector string (match[0]) and the string in its brackets (match[2]) respectively, which are equal to:
match[0] = ":not(.class:contain('span'))"
match[2] = ".class:contain('span')"
return match.slice(0, 3);
Returns a copy of the first three elements in match.
Return to the tokenize function, now match = [":not(.class:contain('span'))", "not", ".class:contain('span')"]
matched = match.shift();
Remove the first element ":not(.class:contain('span'))" in match and assign the element to the matched variable. At this time, matched="":not(.class:contain( 'span'))"",
match = ["not", ".class:contain('span')"]
tokens.push({
value : matched,
Type : type,
matches : match
}
Create a new object { value: ":not(.class:contain('span'))"", type: "PSEUDO", matches: ["not", ".class:contain('span') "] }, and push the object into the tokens array. At this time, tokens have two elements, namely div and not selector.
soFar = soFar.slice(matched.length);
SoFar variable deletes ":not(.class:contain('span'))". At this time, soFar=":eq(3)", after ending this for loop, return to the while loop again, the same way , to obtain the eq selector of the third element of tokens, the process is consistent with not, and I will not go into details here. The results of the final groups are as follows:
group[0][0] = {value: "div", type: "TAG", matches: ["div"] }
group[0][1] = {value: ":not(.class:contain('span'))", type: "PSEUDO", matches: ["not", ".class:contain(' span')"] }
group[0][2] = {value: ":eq(3)", type: "PSEUDO", matches: ["eq", "3"] }
return parseOnly ? soFar.length : soFar ? Sizzle.error(selector) :
tokenCache(selector, groups).slice(0);
Since parseOnly = undefined, tokenCache(selector, groups).slice(0) is executed. This statement pushes groups into the cache and returns its copy.
From this, all the parsing is completed. Some people may ask, the second element here is not parsed out. Yes, this needs to be parsed again in actual operation. Of course, if you can save the result of the valid selector in the cache when you just parsed "class:contain('span')):eq(3", you can avoid parsing again and improve the execution speed. But this It only improves the current running speed because during execution, when ".class:contain('span')" is submitted for analysis again, it will be stored in the cache.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software