Home Web Front-end JS Tutorial jQuery source code analysis-04 selector-Sizzle-working principle analysis_jquery

jQuery source code analysis-04 selector-Sizzle-working principle analysis_jquery

May 16, 2016 pm 05:59 PM
working principle

Author: nuysoft/Gao Yun QQ: 47214707 EMail: nuysoft@gmail.com
Statement: This article is an original article. If you need to reprint, please indicate the source and retain the original link.
Before analyzing the Sizzle source code, let’s sort out the working principle of the selector.

First clarify some nouns used in the selector so that there will be no ambiguity when reading later:

Selector Expression: "div > p"
Block expression: "div" "p"
Column selector expression: "div, p"
Block splitter: chunker regular in Sizzle, for selection The expression is divided into block expressions from left to right
Finder: Search for block expressions, and the array of DOM elements found is called the candidate set
Filter: Filter block expressions and candidate sets
The relationship filter filters the relationship between block expressions. There are four relationships in total: " " The immediate sibling relationship; ">" the parent-child relationship; "" the ancestor relationship; and all sibling relationships after "~"
Candidate set: the result of the finder, to be filtered by the filter
Map set: a copy of the candidate set, filter and relational filter to filter the map set

Workflow:

1. Use the block splitter to split the selector expression from left to right
If you encounter a parallel selector expression split by a comma ",", only split it to the selection before the first comma Converter expression 1, record the remaining part

2. Search Sizzle.find for the last block expression, put the result into the candidate set, and delete the matching string part in the block expression
The finder Sizzle.find obtains the corresponding regular expression from the regular set Expr.match and matches the block expression. If the match is successful, it obtains the corresponding search function from the search function set Expr.find and executes it
The search order is defined in In Expr.order, the order is: ID CLASS NAME TAG. When searching for CLASS, the browser needs to support getElementsByClassName
Expr.match sets the regular matching expression of ID CLASS NAME ATTR TAG CHILD POS PSEUDO

3. If the last block expression is not empty (string), the filter Sizzle.filter filters the set
The filter Sizzle.filter only works on a single block expression and only on elements in the candidate set set It works, checking that the elements in the candidate set satisfy the remaining block expressions
During the filtering process of the filter Sizzle.filter, those that do not meet the conditions are set to false, and those that meet the conditions are not modified
when filtering Get the corresponding regular expression from the regular set Expr.leftMatch and match the block expression. If the match is successful, get the corresponding filter function from Expr.filter and execute it
Expr.leftMatch defines the same number of regular expressions as Expr.match Formula: ID CLASS NAME ATTR TAG CHILD POS PSEUDO
Filter function set Expr.filter defines the filter function of PSEUDO CHILD ID TAG CLASS ATTR POS
Filter Sizzle.filter will call the pre-filter Expr before filtering .preFilter corrects the parameters required for filtering, but CLASS is an exception
When CLASS is pre-filtered, it is optimized to directly return elements matching class as a candidate set, narrowing the filtering scope and candidate set scope
Copy the candidate set set obtained by the above search and filtering, and put it into the mapping set checkSet. The subsequent filtering operation is performed on the checkSet
The search and filtering of the last block expression ends here, and a candidate set set and mapping set are obtained. checkSet

4. Filter the remaining block expressions from right to left on the mapping set checkSet, and obtain the corresponding function from the relationship filter set Expr.relative based on the relationship with the previous block expression. Execute relational filtering
During the filtering process of the relational filter Expr.relative, those that do not meet the conditions are set to false, and those that meet the conditions are set to the relationship between the parent element, ancestor element, and brother element
There are four types: " " Immediate brotherhood; ">" father-son relationship; "" ancestor relationship; all brotherhoods after "~"
During the filtering process of the relationship filter Expr.relative, if you encounter If the block expression is the tag TAG, then directly compare the tag type nodeName to see if it is equal
If it is not the tag TAG, the filter Sizzle.filter will be called to filter. See step 3 for the filtering process
From right Filter left until all block expressions are filtered

5. According to the filtered mapping set checkSet, select the final result set from the candidate set set. In the mapping set checkSet
if it is null, false, will be filtered
If it is not Element (nodeType===1), it will be filtered
If the context is not a Document but an Element, not a child element of Element, it will be filtered

6. If there is a parallel expression, repeat 1~5, and merge, sort, and deduplicate the final result set
If there is only one selector expression and no parallel selector expression, no sorting is required

The following process does not belong to Sizzle, but belongs to jQuery's extension of Sizzle

7. If there are multiple contexts, repeat 1~6 for each context
Multiple context examples: $(' div').find('div > p'), $('div') may find multiple divs
In fact, step 7 is the entrance to the jQuery selector. From step 7 to call 1~6, when calling Pass in an empty jQuery object as the result set
By default, document is the context: (context || rootjQuery).find( selector )

8. Merge and remove the result sets found from multiple contexts Repeat, return the result set

done!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is SOL coin? How does SOL coin work? What is SOL coin? How does SOL coin work? Mar 16, 2024 am 10:37 AM

Solana Blockchain and SOL Token Solana is a blockchain platform focused on providing high performance, security and scalability for decentralized applications (dApps). As the native asset of the Solana blockchain, SOL tokens are mainly used to pay transaction fees, pledge and participate in governance decisions. Solana’s unique features are its fast transaction confirmation times and high throughput, making it a favored choice among developers and users. Through SOL tokens, users can participate in various activities of the Solana ecosystem and jointly promote the development and progress of the platform. How Solana works Solana uses an innovative consensus mechanism called Proof of History (PoH) that is capable of efficiently processing thousands of transactions.

What is the architecture and working principle of Spring Data JPA? What is the architecture and working principle of Spring Data JPA? Apr 17, 2024 pm 02:48 PM

SpringDataJPA is based on the JPA architecture and interacts with the database through mapping, ORM and transaction management. Its repository provides CRUD operations, and derived queries simplify database access. Additionally, it uses lazy loading to only retrieve data when necessary, thus improving performance.

What is VET coin? How does VET coin work? What is VET coin? How does VET coin work? Mar 16, 2024 am 11:40 AM

VET Coin: Blockchain-based IoT ecosystem VeChainThor (VET) is a platform based on blockchain technology that aims to enhance the Internet of Things (IoT) field by ensuring the credibility of data and enabling safe transfer of value. supply chain management and business processes. VET coin is the native token of the VeChainThor blockchain and has the following functions: Pay transaction fees: VET coins are used to pay transaction fees on the VeChainThor network, including data storage, smart contract execution and identity verification. Governance: VET token holders can participate in the governance of VeChainThor, including voting on platform upgrades and proposals. Incentives: VET coins are used to incentivize validators in the network to ensure the

What is SHIB coin? How does SHIB coin work? What is SHIB coin? How does SHIB coin work? Mar 17, 2024 am 08:49 AM

ShibaInu Coin: Dog-Inspired Cryptocurrency ShibaInu Coin (SHIB) is a decentralized cryptocurrency inspired by the iconic Shiba Inu emoji. The cryptocurrency was launched in August 2020 and aims to be an alternative to Dogecoin on the Ethereum network. Working Principle SHIB coin is a digital currency built on the Ethereum blockchain and complies with the ERC-20 token standard. It utilizes a decentralized consensus mechanism, Proof of Stake (PoS), which allows holders to stake their SHIB tokens to verify transactions and earn rewards for doing so. Key Features Huge supply: The initial supply of SHIB coins is 1,000 trillion coins, making it one of the largest cryptocurrencies in circulation. Low price: S

What is Polygon coin? How does Polygon coin work? What is Polygon coin? How does Polygon coin work? Mar 16, 2024 am 09:22 AM

Polygon: A multifunctional blockchain that builds the Ethereum ecosystem Polygon is a multifunctional blockchain platform built on Ethereum, formerly known as MaticNetwork. Its goal is to solve the scalability, high fees, and complexity issues in the Ethereum network. Polygon provides developers and users with a faster, cheaper, and simpler blockchain experience by providing scalability solutions. Here’s how Polygon works: Sidechain Network: Polygon creates a network of multiple sidechains. These sidechains run in parallel with the main Ethereum chain and can handle large volumes of transactions, thereby increasing overall network throughput. Plasma framework: Polygon utilizes the Plasma framework, which

What is Algorand coin? How does Algorand coin work? What is Algorand coin? How does Algorand coin work? Mar 17, 2024 am 08:30 AM

Algorand: A blockchain platform based on pure Byzantine consensus protocol Algorand is a blockchain platform built on pure Byzantine consensus protocol and aims to provide efficient, secure and scalable blockchain solutions. The platform was founded in 2017 by MIT professor Silvio Micali. Working Principle The core of Algorand lies in its unique pure Byzantine consensus protocol, the Algorand consensus. This protocol allows nodes to achieve consensus in a trustless environment, even if there are malicious nodes in the network. Algorand consensus achieves this goal through a series of steps. Key generation: Each node generates a pair of public and private keys. Proposal phase: A randomly selected node proposes a new zone

What is AR coin? How does AR coin work? What is AR coin? How does AR coin work? Mar 15, 2024 pm 07:25 PM

AR Coin: Digital currency based on augmented reality technology AR Coin is a digital currency that uses augmented reality technology to provide users with the experience of interacting with digital content, allowing them to create immersive experiences in the real world. How it works AR Coin works based on the following key concepts: Augmented Reality (AR): AR technology overlays digital information on the real world, allowing users to interact with virtual objects. Blockchain: Blockchain is a distributed ledger technology used to record and verify transactions. It provides security and transparency to AR coins. Smart Contracts: Smart contracts are codes stored on the blockchain that are used to automate specific operations. They play a vital role in the creation and management of AR coins. The workflow of AR coins is as follows: Create AR body

What is Beam Coin? How does Beam Coin work? What is Beam Coin? How does Beam Coin work? Mar 15, 2024 pm 09:50 PM

Beam Coin: Privacy-Focused Cryptocurrency Beam Coin is a privacy-focused cryptocurrency designed to provide secure and anonymous transactions. It uses the MimbleWimble protocol, a blockchain technology that enhances user privacy by merging transactions and hiding the addresses of senders and receivers. The design concept of Beam Coin is to provide users with a digital currency option that ensures the confidentiality of transaction information. By adopting this protocol, users can conduct transactions with greater confidence without worrying about their personal privacy information being leaked. This privacy-preserving feature makes Beam Coin work. MimbleWimble protocol enhances privacy by: Transaction merging: It combines multiple transactions into

See all articles