diff options
author | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2022-11-21 23:23:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-21 23:23:39 +0100 |
commit | 4c1171f2ed62584211250df0af8302d34c04621a (patch) | |
tree | ba1f6a5a977f32ca3dd67396458ca6bca602fe0e /src/selector-native.js | |
parent | f62d8e2159baf1eabf3b760b85b2dda56d569a09 (diff) | |
download | jquery-4c1171f2ed62584211250df0af8302d34c04621a.tar.gz jquery-4c1171f2ed62584211250df0af8302d34c04621a.zip |
Selector: Re-introduce selector-native.js
Re-introduce the `selector-native` similar to the one on the `3.x-stable`
branch. One difference is since the `main` branch inlined Sizzle, some
selector utils can be shared between the main `selector` module and
`selector-native`.
The main `selector` module can be disabled in favor of `selector-native`
via:
grunt custom:-selector
Other changes:
* Tests: Fix Safari detection - Chrome Headless has a different user
agent than Safari and a browser check in selector tests didn't take
that into account.
* Tests: Run selector-native tests in `npm test`
* Selector: Fix querying on document fragments
Ref gh-4395
Closes gh-5085
Diffstat (limited to 'src/selector-native.js')
-rw-r--r-- | src/selector-native.js | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/selector-native.js b/src/selector-native.js new file mode 100644 index 000000000..0e2e48d81 --- /dev/null +++ b/src/selector-native.js @@ -0,0 +1,88 @@ +/* + * Optional limited selector module for custom builds. + * + * Note that this DOES NOT SUPPORT many documented jQuery + * features in exchange for its smaller size: + * + * * Attribute not equal selector (!=) + * * Positional selectors (:first; :eq(n); :odd; etc.) + * * Type selectors (:input; :checkbox; :button; etc.) + * * State-based selectors (:animated; :visible; :hidden; etc.) + * * :has(selector) + * * :not(complex selector) + * * custom selectors via jQuery extensions + * * Leading combinators (e.g., $collection.find("> *")) + * * Reliable functionality on XML fragments + * * Requiring all parts of a selector to match elements under context + * (e.g., $div.find("div > *") now matches children of $div) + * * Matching against non-elements + * * Reliable sorting of disconnected nodes + * * querySelectorAll bug fixes (e.g., unreliable :focus on WebKit) + * + * If any of these are unacceptable tradeoffs, either use the full + * selector engine or customize this stub for the project's specific + * needs. + */ + +import jQuery from "./core.js"; +import document from "./var/document.js"; +import documentElement from "./var/documentElement.js"; +import whitespace from "./var/whitespace.js"; + +// The following utils are attached directly to the jQuery object. +import "./selector/contains.js"; +import "./selector/escapeSelector.js"; +import "./selector/uniqueSort.js"; + +// Support: IE 9 - 11+ +// IE requires a prefix. +var matches = documentElement.matches || documentElement.msMatchesSelector; + +jQuery.extend( { + find: function( selector, context, results, seed ) { + var elem, nodeType, + i = 0; + + results = results || []; + context = context || document; + + // Same basic safeguard as in the full selector module + if ( !selector || typeof selector !== "string" ) { + return results; + } + + // Early return if context is not an element, document or document fragment + if ( ( nodeType = context.nodeType ) !== 1 && nodeType !== 9 && nodeType !== 11 ) { + return []; + } + + if ( seed ) { + while ( ( elem = seed[ i++ ] ) ) { + if ( jQuery.find.matchesSelector( elem, selector ) ) { + results.push( elem ); + } + } + } else { + jQuery.merge( results, context.querySelectorAll( selector ) ); + } + + return results; + }, + expr: { + attrHandle: {}, + match: { + bool: new RegExp( "^(?:checked|selected|async|autofocus|autoplay|controls|defer" + + "|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)$", "i" ), + needsContext: new RegExp( "^" + whitespace + "*[>+~]" ) + } + } +} ); + +jQuery.extend( jQuery.find, { + matches: function( expr, elements ) { + return jQuery.find( expr, null, null, elements ); + }, + matchesSelector: function( elem, expr ) { + return matches.call( elem, expr ); + } +} ); |