search
HomeWeb Front-endJS TutorialjQuery source code analysis-04 selector-Sizzle-working principle analysis_jquery

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
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
什么是 Microsoft Teams 中的对讲机及其工作原理?什么是 Microsoft Teams 中的对讲机及其工作原理?Apr 14, 2023 pm 12:31 PM

Microsoft Teams 上的对讲机是什么?顾名思义,新的 Walkie Talkie 功能让 Microsoft Teams 上的用户可以通过使用他们的声音与他们的团队成员进行实时交流,从而与他们联系。在频道中连接到 Walkie Talkie 的用户可以一次听一个即按即说格式的对方讲话。这样一来,只有一个人在说话的时候才能引起注意,而不会被其他人打断。微软将这一功能定

听诊器的工作原理是什么听诊器的工作原理是什么Aug 31, 2023 pm 02:37 PM

听诊器的工作原理是通过声学传感器将人体内部的声音转化成电信号,然后通过耳机或扩音器放大和传输这些信号给医生,它的工作原理基于声学原理,能够帮助医生听到内部声音并进行疾病诊断。听诊器的核心部件是声学传感器,通常由一个共振膜和一个接收器组成,共振膜是一个薄膜,通常由金属或塑料制成,它能够感受到人体内部的声音振动,当共振膜受到声波的作用时,它会产生微小的振动。

vue中keep-alive的工作原理及使用方法详解vue中keep-alive的工作原理及使用方法详解Jul 21, 2023 am 11:58 AM

Vue.js是一个流行的前端框架,提供了一些方便的功能来优化性能和提升开发效率。其中一个功能是keep-alive,它可以帮助我们在组件之间保留状态,从而减少不必要的渲染和请求。本文将详细介绍keep-alive的工作原理以及使用方法,并提供一些代码示例。一、keep-alive的工作原理在Vue.js中,每当我们切换组件时,组件都会被重新创建

深入了解Spring框架的架构与工作原理深入了解Spring框架的架构与工作原理Jan 24, 2024 am 09:41 AM

深入剖析Spring框架的架构与工作原理引言:Spring是Java生态系统中最受欢迎的开源框架之一,它不仅提供了一套强大的容器管理和依赖注入功能,还提供了许多其他功能,如事务管理、AOP、数据访问等。本文将深入剖析Spring框架的架构与工作原理,并通过具体的代码示例来解释相关概念。一、Spring框架的核心概念1.1IoC(控制反转)Spring的核心

计算机按工作原理可分为什么计算机按工作原理可分为什么Dec 07, 2020 am 10:24 AM

计算机按工作原理可分为数字计算机和模拟计算机。数字式电子计算机是当今世界电子计算机行业中的主流,其内部处理的是一种称为符号信号或数字信号的电信号,它有着运算速度快、运算精度高、通用性强等特点。模拟计算机是根据相似原理,用一种连续变化的模拟量作为被运算的对象的计算机;模拟计算机以电子线路构成基本运算部件。

了解Spring拦截器的原理和优点了解Spring拦截器的原理和优点Dec 30, 2023 pm 12:25 PM

探究Spring拦截器的工作原理及优势引言:Spring框架是Java开发中最常用的框架之一,它提供了丰富的功能和灵活性,使得开发者能够更加高效地开发应用程序。其中一个重要的组件就是拦截器(Interceptor)。本文将深入探讨Spring拦截器的工作原理和优势,同时给出具体的代码示例。一、Spring拦截器的工作原理Spring拦截器使用了面向切面编程(

交换机的工作原理是什么交换机的工作原理是什么Dec 26, 2023 am 11:56 AM

交换机的工作原理包括:1、数据帧接收和解析;2、转发表的更新;3、数据帧的转发;4、泛洪处理;5、维护连接。详细介绍:1、数据帧接收和解析,当交换机接收到一个数据帧时,它会首先对数据帧进行解析,提取出其中的源MAC地址和目的MAC地址等信息;2、转发表的更新,交换机内部维护着一个转发表,这个表记录了MAC地址与接口的对应关系;3、数据帧的转发等等。

了解PHP中散列查找算法的工作原理及实际应用场景。了解PHP中散列查找算法的工作原理及实际应用场景。Sep 19, 2023 pm 01:00 PM

了解PHP中散列查找算法的工作原理及实际应用场景概述:散列查找算法是一种常用的数据结构和算法,在PHP编程中也有着广泛的应用。它通过将关键字映射为数据结构中的索引位置来实现快速的查找操作。本文将介绍散列查找算法的工作原理和实际应用场景,并给出具体的代码示例。一、散列查找算法的工作原理散列查找算法的基本思想是通过一个散列函数将关键字映射到数据结构中的索引位置,

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment