Introduction to the

Sizzle uses tools such as checking whether a CSS identifier is compliant. The following is a little bit more about character sets in my other blog post. After I read the notes I wrote you should be able to understand.

funescape

var whitespace = "[\\x20\\t\\r\\n\\f]",
runescape = new RegExp("\ \ \ \ [\ \ da - fA - F] {1, 6}" + whitespace + "? |\\\\([^\\r\\n\\f])"."g")
// Analyze the regular match \ number + letter 1 to 6 matches blank 0 to more, or does not start with blank
/ / -- -- -- -- > / \ \ [\ da - fA - F] {1, 6} [\ x20 \ r \ n \ \ t F]? |\\([^\r\n\f])/g
Copy the code

Runescape is a regular expression that matches an escaped hexadecimal number. Parsing “\\” a bit, suppose we now match a hexadecimal string “\\ 26EE “(base 10 23356), the result is [\26EE], The unicode equivalent for U+26EE is “⛮”.

  • In CSS 2.1, the backslash (\) character can represent one of three types of character escapes.
    • It removes the meaning of special CSS characters. Any character (except hexadecimal digits, newlines, carriage returns, or page feeds) can be escaped with a backslash to remove its special meaning

    For example, “\” is a string enclosed by double quotes. The stylesheet preprocessor must not remove these backslashes from the stylesheet because it would change the meaning of the stylesheet.

    • In CSS comments, the backslash represents itself
    • The backslash escape allows authors to reference characters that they cannot easily fit into a document.
  • Note: the backslash escape is always considered to be part of an identifier or string (i.e., “\ 7B” is not a punctuation mark, even if “{” is allowed, and” \ 32 “is allowed at the beginning of a class name, even though” 2 “is not).

The Ecmascript standard dictates that the JavaScript language is developed based on the Unicode standard. The JavaScript kernel is written entirely in the UCS character set, so every character in JavaScript code is represented by two bytes. Think this involves the knowledge of character set encoding, do not understand the students recommended to learn. I also wrote a little bit about character sets in my other HTTP summary blog, and this API can be learned from MDN

/** * [funescape description] * @param {[String]} escape [hexadecimal String] Pass a hexadecimal String that needs to be escaped * @param {[Boolean]} nonHex Return {[String]} [utF-16 code unit sequence created by the String] returns the corresponding Unicode character */ funescape = function (escape, NonHex) {// If you can't read it here, you can read my other article character set knowledge. var high = "0x" + escape.slice(1) - 0x10000; return nonHex ? nonHex : high < 0 ? String. FromCharCode (high + 0x10000) : // If it is greater than 0, it is another secondary plane double byte. // The high bit is moved 10 bits to the right, and the remaining high bit is left. Or 0xD800 equals +, and then we have the high bit. // The low bit is high& (0x3FF,1111 1111 11),0x3FF is 0. Then add 0 + xdc00 went out the low byte String. FromCharCode (high > > 10 | 0 xd800, high & 0 x3ff | 0 xdc00); },Copy the code

fcssescape

/ / the control character is between 0 and 1 f/f / 1-7 f between ASCII characters / / 7 f - followed by the digital control characters or other characters / / uFFFF biggest yards In a plane rcssescape = / ([\ 0 \ x1f \ x7f] | ^ -? \ d) | ^ - $| 0 [^ \ \ x1f \ x7f - -] \ uFFFF \ w/g, [\ 0 \ x1f \ x7f] capture or control characters match - the beginning of the digital a match or "-" start "-" the end of the characters [^ \ 0 \ x1f \ x7f - \ uFFFF \ w -] analyzed separately: Not \ 0 \ x1f, \ x7f - \ uFFFF, \ w - any one character https://condor.depaul.edu/sjost/lsp121/documents/ascii-npr.htm / * * * [fcssescape Description] * @param {[String]} ch rcssescape * @param {[Boolean]} asCodePoint * @return {[String]} = function (ch) {return - */ fcssescape = function (ch, AsCodePoint) {if (asCodePoint) {// unitcde = unitcde // U+0000 NULL becomes U+FFFD REPLACEMENT CHARACTER if (ch === "\0") { return "\uFFFD"; } Control characters and numbers (depending on position) as code points to escape return ch.slice(0, -1) + "\\" + ch.charcodeat (ch.length-1).toString(16) + ""; } // Other potentially-special ASCII characters get backslash-escaped return "\\" + ch; }Copy the code

createCache

/** * [createCache description] Creates a queue cache *@return {[type]} [description]
 */
function createCache() {
        var keys = [];
        // A closure is created to persist data in memory
        function cache(key, value) {
                // If the value stored is larger than the length of the variable to cache, then the data in the cache is queued out
                // Use (key + " ") to avoid collision with native prototype properties (see Issue #157)
                if (keys.push(key + "") > Expr.cacheLength) {

                        // Only keep the most recent entries
                        delete cache[keys.shift()];
                }
                return (cache[key + ""] = value);
        }
        return cache;
}

Copy the code

markFunction

var expando = "sizzle" + 1 * new Date(), // The unique identifier of the sizzle instance
/** * [markFunction description] specifies a unique identifier * for the function passed in@param  {Function} fn 
 * @return {[Function]}    Return this method */

function markFunction(fn) {
	fn[expando] = true;
	return fn;
}

Copy the code

assert

/** * [assert Description] tests whether the element used is supported *@param  {Function} Fn passes the create element and returns a Boolean *@return {[bolean]}     * /
 
function assert(fn) {
        var el = document.createElement("fieldset");

        try {
                return!!!!! fn(el);/ /!!!!! Cast to Boolean type
        } catch (e) {
                return false;
        } finally {

                // Remove from its parent by default
                if (el.parentNode) {
                        el.parentNode.removeChild(el);
                }

                The advantage of this is that you can release the location of the variable in the browser's memory
                el = null; }}Copy the code

Sizzle.error

/** * [Error description] Description indicates an error *@param  {[string]} MSG Error message *@throw  {[Error]}   Throw an error cause */
Sizzle.error = function(msg) {
        throw new Error("Syntax error, unrecognized expression: " + msg);
};

Copy the code

isXML


/** * [isXML] detects the XML node *@param  {[type]}  Elem XML element *@return {Boolean}     * /
Sizzle.isXML = function(elem) {
	// Get the URI of the element's namespace
	var namespace = elem.namespaceURI,
	/ / to get the elements of the parent document | | or current
		docElem = (elem.ownerDocument || elem).documentElement;
	// Support: IE <=8
	// Assume HTML when documentElement doesn't yet exist, such as inside loading iframes
	// Assume that the HTML document element has not yet been loaded, such as when the internal iframe is loaded
	// https://bugs.jquery.com/ticket/4833
	// rhtml = /HTML$/i,
	/ / if there is a namespace or use docElem && docElem. NodeName or | | HTML
	// If it is HTML, return rhtml.test true
	return! rhtml.test(namespace || docElem && docElem.nodeName ||"HTML");
};
Copy the code

sortOrder

/** * [sortOrder comparison function] *@param  {any} a 
 * @param  {any} b 
 * @return {number}   0 */ / array.sort (sortOrder) if returns 0 relative position unchanged */
sortOrder = function (a, b) {
	if (a === b) {
		// Whether to repeat
			hasDuplicate = true
	}
	return 0
}

Copy the code

indexOf

/** * [indexOf description] finds the position of the element *@param  {[array]} List The object to be searched *@param  {[any]} Elem finds the object *@return {[number]}     The position of the element */
indexOf = function (list, elem) {
	var i = 0, len = list.length
	for(; i < len; i++) {if (list[i]=== item) {
			return i
		}
	}
	return -1
}
Copy the code

andHandle

/** * [addHandle description] splits the incoming string into an array for each attribute, Expr. AttrHandle adds method * to each attribute@param {[string]} Attrs String *@param {[function]} handler 
 */
function addHandle( attrs, handler ) {
	var arr = attrs.split( "|" ),
		i = arr.length;

	while( i-- ) { Expr.attrHandle[ arr[ i ] ] = handler; }}Copy the code

If you want to reprint, please indicate the source. Thank you

Sizzle source code analysis (I) Basic concepts

Sizzle source code analysis (2) tool approach

Sizzle source code analysis (iii) compatibility processing

Sizzle source code analysis (5) How to select the corresponding HTML node according to the CSS selector