|
| 1 | +/* |
| 2 | + * Optional limited selector module for custom builds. |
| 3 | + * |
| 4 | + * Note that this DOES NOT SUPPORT many documented jQuery |
| 5 | + * features in exchange for its smaller size: |
| 6 | + * |
| 7 | + * * Attribute not equal selector (!=) |
| 8 | + * * Positional selectors (:first; :eq(n); :odd; etc.) |
| 9 | + * * Type selectors (:input; :checkbox; :button; etc.) |
| 10 | + * * State-based selectors (:animated; :visible; :hidden; etc.) |
| 11 | + * * :has(selector) |
| 12 | + * * :not(complex selector) |
| 13 | + * * custom selectors via jQuery extensions |
| 14 | + * * Leading combinators (e.g., $collection.find("> *")) |
| 15 | + * * Reliable functionality on XML fragments |
| 16 | + * * Requiring all parts of a selector to match elements under context |
| 17 | + * (e.g., $div.find("div > *") now matches children of $div) |
| 18 | + * * Matching against non-elements |
| 19 | + * * Reliable sorting of disconnected nodes |
| 20 | + * * querySelectorAll bug fixes (e.g., unreliable :focus on WebKit) |
| 21 | + * |
| 22 | + * If any of these are unacceptable tradeoffs, either use the full |
| 23 | + * selector engine or customize this stub for the project's specific |
| 24 | + * needs. |
| 25 | + */ |
| 26 | + |
| 27 | +import jQuery from "./core.js"; |
| 28 | +import document from "./var/document.js"; |
| 29 | +import documentElement from "./var/documentElement.js"; |
| 30 | +import whitespace from "./var/whitespace.js"; |
| 31 | + |
| 32 | +// The following utils are attached directly to the jQuery object. |
| 33 | +import "./selector/contains.js"; |
| 34 | +import "./selector/escapeSelector.js"; |
| 35 | +import "./selector/uniqueSort.js"; |
| 36 | + |
| 37 | +// Support: IE 9 - 11+ |
| 38 | +// IE requires a prefix. |
| 39 | +var matches = documentElement.matches || documentElement.msMatchesSelector; |
| 40 | + |
| 41 | +jQuery.extend( { |
| 42 | + find: function( selector, context, results, seed ) { |
| 43 | + var elem, nodeType, |
| 44 | + i = 0; |
| 45 | + |
| 46 | + results = results || []; |
| 47 | + context = context || document; |
| 48 | + |
| 49 | + // Same basic safeguard as in the full selector module |
| 50 | + if ( !selector || typeof selector !== "string" ) { |
| 51 | + return results; |
| 52 | + } |
| 53 | + |
| 54 | + // Early return if context is not an element, document or document fragment |
| 55 | + if ( ( nodeType = context.nodeType ) !== 1 && nodeType !== 9 && nodeType !== 11 ) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + if ( seed ) { |
| 60 | + while ( ( elem = seed[ i++ ] ) ) { |
| 61 | + if ( jQuery.find.matchesSelector( elem, selector ) ) { |
| 62 | + results.push( elem ); |
| 63 | + } |
| 64 | + } |
| 65 | + } else { |
| 66 | + jQuery.merge( results, context.querySelectorAll( selector ) ); |
| 67 | + } |
| 68 | + |
| 69 | + return results; |
| 70 | + }, |
| 71 | + expr: { |
| 72 | + attrHandle: {}, |
| 73 | + match: { |
| 74 | + bool: new RegExp( "^(?:checked|selected|async|autofocus|autoplay|controls|defer" + |
| 75 | + "|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)$", "i" ), |
| 76 | + needsContext: new RegExp( "^" + whitespace + "*[>+~]" ) |
| 77 | + } |
| 78 | + } |
| 79 | +} ); |
| 80 | + |
| 81 | +jQuery.extend( jQuery.find, { |
| 82 | + matches: function( expr, elements ) { |
| 83 | + return jQuery.find( expr, null, null, elements ); |
| 84 | + }, |
| 85 | + matchesSelector: function( elem, expr ) { |
| 86 | + return matches.call( elem, expr ); |
| 87 | + } |
| 88 | +} ); |
0 commit comments