Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | // Initialize a jQuery object |
| 2 | define( [ |
| 3 | "../core", |
| 4 | "../var/document", |
| 5 | "../var/isFunction", |
| 6 | "./var/rsingleTag", |
| 7 | |
| 8 | "../traversing/findFilter" |
| 9 | ], function( jQuery, document, isFunction, rsingleTag ) { |
| 10 | |
| 11 | "use strict"; |
| 12 | |
| 13 | // A central reference to the root jQuery(document) |
| 14 | var rootjQuery, |
| 15 | |
| 16 | // A simple way to check for HTML strings |
| 17 | // Prioritize #id over <tag> to avoid XSS via location.hash (#9521) |
| 18 | // Strict HTML recognition (#11290: must start with <) |
| 19 | // Shortcut simple #id case for speed |
| 20 | rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/, |
| 21 | |
| 22 | init = jQuery.fn.init = function( selector, context, root ) { |
| 23 | var match, elem; |
| 24 | |
| 25 | // HANDLE: $(""), $(null), $(undefined), $(false) |
| 26 | if ( !selector ) { |
| 27 | return this; |
| 28 | } |
| 29 | |
| 30 | // Method init() accepts an alternate rootjQuery |
| 31 | // so migrate can support jQuery.sub (gh-2101) |
| 32 | root = root || rootjQuery; |
| 33 | |
| 34 | // Handle HTML strings |
| 35 | if ( typeof selector === "string" ) { |
| 36 | if ( selector[ 0 ] === "<" && |
| 37 | selector[ selector.length - 1 ] === ">" && |
| 38 | selector.length >= 3 ) { |
| 39 | |
| 40 | // Assume that strings that start and end with <> are HTML and skip the regex check |
| 41 | match = [ null, selector, null ]; |
| 42 | |
| 43 | } else { |
| 44 | match = rquickExpr.exec( selector ); |
| 45 | } |
| 46 | |
| 47 | // Match html or make sure no context is specified for #id |
| 48 | if ( match && ( match[ 1 ] || !context ) ) { |
| 49 | |
| 50 | // HANDLE: $(html) -> $(array) |
| 51 | if ( match[ 1 ] ) { |
| 52 | context = context instanceof jQuery ? context[ 0 ] : context; |
| 53 | |
| 54 | // Option to run scripts is true for back-compat |
| 55 | // Intentionally let the error be thrown if parseHTML is not present |
| 56 | jQuery.merge( this, jQuery.parseHTML( |
| 57 | match[ 1 ], |
| 58 | context && context.nodeType ? context.ownerDocument || context : document, |
| 59 | true |
| 60 | ) ); |
| 61 | |
| 62 | // HANDLE: $(html, props) |
| 63 | if ( rsingleTag.test( match[ 1 ] ) && jQuery.isPlainObject( context ) ) { |
| 64 | for ( match in context ) { |
| 65 | |
| 66 | // Properties of context are called as methods if possible |
| 67 | if ( isFunction( this[ match ] ) ) { |
| 68 | this[ match ]( context[ match ] ); |
| 69 | |
| 70 | // ...and otherwise set as attributes |
| 71 | } else { |
| 72 | this.attr( match, context[ match ] ); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return this; |
| 78 | |
| 79 | // HANDLE: $(#id) |
| 80 | } else { |
| 81 | elem = document.getElementById( match[ 2 ] ); |
| 82 | |
| 83 | if ( elem ) { |
| 84 | |
| 85 | // Inject the element directly into the jQuery object |
| 86 | this[ 0 ] = elem; |
| 87 | this.length = 1; |
| 88 | } |
| 89 | return this; |
| 90 | } |
| 91 | |
| 92 | // HANDLE: $(expr, $(...)) |
| 93 | } else if ( !context || context.jquery ) { |
| 94 | return ( context || root ).find( selector ); |
| 95 | |
| 96 | // HANDLE: $(expr, context) |
| 97 | // (which is just equivalent to: $(context).find(expr) |
| 98 | } else { |
| 99 | return this.constructor( context ).find( selector ); |
| 100 | } |
| 101 | |
| 102 | // HANDLE: $(DOMElement) |
| 103 | } else if ( selector.nodeType ) { |
| 104 | this[ 0 ] = selector; |
| 105 | this.length = 1; |
| 106 | return this; |
| 107 | |
| 108 | // HANDLE: $(function) |
| 109 | // Shortcut for document ready |
| 110 | } else if ( isFunction( selector ) ) { |
| 111 | return root.ready !== undefined ? |
| 112 | root.ready( selector ) : |
| 113 | |
| 114 | // Execute immediately if ready is not present |
| 115 | selector( jQuery ); |
| 116 | } |
| 117 | |
| 118 | return jQuery.makeArray( selector, this ); |
| 119 | }; |
| 120 | |
| 121 | // Give the init function the jQuery prototype for later instantiation |
| 122 | init.prototype = jQuery.fn; |
| 123 | |
| 124 | // Initialize central reference |
| 125 | rootjQuery = jQuery( document ); |
| 126 | |
| 127 | return init; |
| 128 | |
| 129 | } ); |