blob: da837a00462a3e11de6be6c116333f77ac423834 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001define( [
2 "./core",
3 "./var/document",
4 "./var/documentElement",
5 "./var/hasOwn",
6 "./var/indexOf"
7], function( jQuery, document, documentElement, hasOwn, indexOf ) {
8
9"use strict";
10
11/*
12 * Optional (non-Sizzle) selector module for custom builds.
13 *
14 * Note that this DOES NOT SUPPORT many documented jQuery
15 * features in exchange for its smaller size:
16 *
17 * Attribute not equal selector
18 * Positional selectors (:first; :eq(n); :odd; etc.)
19 * Type selectors (:input; :checkbox; :button; etc.)
20 * State-based selectors (:animated; :visible; :hidden; etc.)
21 * :has(selector)
22 * :not(complex selector)
23 * custom selectors via Sizzle extensions
24 * Leading combinators (e.g., $collection.find("> *"))
25 * Reliable functionality on XML fragments
26 * Requiring all parts of a selector to match elements under context
27 * (e.g., $div.find("div > *") now matches children of $div)
28 * Matching against non-elements
29 * Reliable sorting of disconnected nodes
30 * querySelectorAll bug fixes (e.g., unreliable :focus on WebKit)
31 *
32 * If any of these are unacceptable tradeoffs, either use Sizzle or
33 * customize this stub for the project's specific needs.
34 */
35
36var hasDuplicate, sortInput,
37 sortStable = jQuery.expando.split( "" ).sort( sortOrder ).join( "" ) === jQuery.expando,
38 matches = documentElement.matches ||
39 documentElement.webkitMatchesSelector ||
40 documentElement.mozMatchesSelector ||
41 documentElement.oMatchesSelector ||
42 documentElement.msMatchesSelector,
43
44 // CSS string/identifier serialization
45 // https://drafts.csswg.org/cssom/#common-serializing-idioms
46 rcssescape = /([\0-\x1f\x7f]|^-?\d)|^-$|[^\x80-\uFFFF\w-]/g,
47 fcssescape = function( ch, asCodePoint ) {
48 if ( asCodePoint ) {
49
50 // U+0000 NULL becomes U+FFFD REPLACEMENT CHARACTER
51 if ( ch === "\0" ) {
52 return "\uFFFD";
53 }
54
55 // Control characters and (dependent upon position) numbers get escaped as code points
56 return ch.slice( 0, -1 ) + "\\" + ch.charCodeAt( ch.length - 1 ).toString( 16 ) + " ";
57 }
58
59 // Other potentially-special ASCII characters get backslash-escaped
60 return "\\" + ch;
61 };
62
63function sortOrder( a, b ) {
64
65 // Flag for duplicate removal
66 if ( a === b ) {
67 hasDuplicate = true;
68 return 0;
69 }
70
71 // Sort on method existence if only one input has compareDocumentPosition
72 var compare = !a.compareDocumentPosition - !b.compareDocumentPosition;
73 if ( compare ) {
74 return compare;
75 }
76
77 // Calculate position if both inputs belong to the same document
78 compare = ( a.ownerDocument || a ) === ( b.ownerDocument || b ) ?
79 a.compareDocumentPosition( b ) :
80
81 // Otherwise we know they are disconnected
82 1;
83
84 // Disconnected nodes
85 if ( compare & 1 ) {
86
87 // Choose the first element that is related to our preferred document
88 if ( a === document || a.ownerDocument === document &&
89 jQuery.contains( document, a ) ) {
90 return -1;
91 }
92 if ( b === document || b.ownerDocument === document &&
93 jQuery.contains( document, b ) ) {
94 return 1;
95 }
96
97 // Maintain original order
98 return sortInput ?
99 ( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) :
100 0;
101 }
102
103 return compare & 4 ? -1 : 1;
104}
105
106function uniqueSort( results ) {
107 var elem,
108 duplicates = [],
109 j = 0,
110 i = 0;
111
112 hasDuplicate = false;
113 sortInput = !sortStable && results.slice( 0 );
114 results.sort( sortOrder );
115
116 if ( hasDuplicate ) {
117 while ( ( elem = results[ i++ ] ) ) {
118 if ( elem === results[ i ] ) {
119 j = duplicates.push( i );
120 }
121 }
122 while ( j-- ) {
123 results.splice( duplicates[ j ], 1 );
124 }
125 }
126
127 // Clear input after sorting to release objects
128 // See https://github.com/jquery/sizzle/pull/225
129 sortInput = null;
130
131 return results;
132}
133
134function escape( sel ) {
135 return ( sel + "" ).replace( rcssescape, fcssescape );
136}
137
138jQuery.extend( {
139 uniqueSort: uniqueSort,
140 unique: uniqueSort,
141 escapeSelector: escape,
142 find: function( selector, context, results, seed ) {
143 var elem, nodeType,
144 i = 0;
145
146 results = results || [];
147 context = context || document;
148
149 // Same basic safeguard as Sizzle
150 if ( !selector || typeof selector !== "string" ) {
151 return results;
152 }
153
154 // Early return if context is not an element or document
155 if ( ( nodeType = context.nodeType ) !== 1 && nodeType !== 9 ) {
156 return [];
157 }
158
159 if ( seed ) {
160 while ( ( elem = seed[ i++ ] ) ) {
161 if ( jQuery.find.matchesSelector( elem, selector ) ) {
162 results.push( elem );
163 }
164 }
165 } else {
166 jQuery.merge( results, context.querySelectorAll( selector ) );
167 }
168
169 return results;
170 },
171 text: function( elem ) {
172 var node,
173 ret = "",
174 i = 0,
175 nodeType = elem.nodeType;
176
177 if ( !nodeType ) {
178
179 // If no nodeType, this is expected to be an array
180 while ( ( node = elem[ i++ ] ) ) {
181
182 // Do not traverse comment nodes
183 ret += jQuery.text( node );
184 }
185 } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
186
187 // Use textContent for elements
188 return elem.textContent;
189 } else if ( nodeType === 3 || nodeType === 4 ) {
190 return elem.nodeValue;
191 }
192
193 // Do not include comment or processing instruction nodes
194
195 return ret;
196 },
197 contains: function( a, b ) {
198 var adown = a.nodeType === 9 ? a.documentElement : a,
199 bup = b && b.parentNode;
200 return a === bup || !!( bup && bup.nodeType === 1 && adown.contains( bup ) );
201 },
202 isXMLDoc: function( elem ) {
203
204 // documentElement is verified for cases where it doesn't yet exist
205 // (such as loading iframes in IE - #4833)
206 var documentElement = elem && ( elem.ownerDocument || elem ).documentElement;
207 return documentElement ? documentElement.nodeName !== "HTML" : false;
208 },
209 expr: {
210 attrHandle: {},
211 match: {
212 bool: new RegExp( "^(?:checked|selected|async|autofocus|autoplay|controls|defer" +
213 "|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)$", "i" ),
214 needsContext: /^[\x20\t\r\n\f]*[>+~]/
215 }
216 }
217} );
218
219jQuery.extend( jQuery.find, {
220 matches: function( expr, elements ) {
221 return jQuery.find( expr, null, null, elements );
222 },
223 matchesSelector: function( elem, expr ) {
224 return matches.call( elem, expr );
225 },
226 attr: function( elem, name ) {
227 var fn = jQuery.expr.attrHandle[ name.toLowerCase() ],
228
229 // Don't get fooled by Object.prototype properties (jQuery #13807)
230 value = fn && hasOwn.call( jQuery.expr.attrHandle, name.toLowerCase() ) ?
231 fn( elem, name, jQuery.isXMLDoc( elem ) ) :
232 undefined;
233 return value !== undefined ? value : elem.getAttribute( name );
234 }
235} );
236
237} );