1 // Initialize a jQuery object
2 import jQuery from "../core.js";
3 import document from "../var/document.js";
4 import rsingleTag from "./var/rsingleTag.js";
5 import isObviousHtml from "./isObviousHtml.js";
7 import "../traversing/findFilter.js";
9 // A central reference to the root jQuery(document)
12 // A simple way to check for HTML strings
13 // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
14 // Strict HTML recognition (#11290: must start with <)
15 // Shortcut simple #id case for speed
16 rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/,
18 init = jQuery.fn.init = function( selector, context, root ) {
21 // HANDLE: $(""), $(null), $(undefined), $(false)
26 // Method init() accepts an alternate rootjQuery
27 // so migrate can support jQuery.sub (gh-2101)
28 root = root || rootjQuery;
30 // HANDLE: $(DOMElement)
31 if ( selector.nodeType ) {
36 // HANDLE: $(function)
37 // Shortcut for document ready
38 } else if ( typeof selector === "function" ) {
39 return root.ready !== undefined ?
40 root.ready( selector ) :
42 // Execute immediately if ready is not present
47 // Handle obvious HTML strings
48 match = selector + "";
49 if ( isObviousHtml( match ) ) {
51 // Assume that strings that start and end with <> are HTML and skip
52 // the regex check. This also handles browser-supported HTML wrappers
54 match = [ null, selector, null ];
56 // Handle HTML strings or selectors
57 } else if ( typeof selector === "string" ) {
58 match = rquickExpr.exec( selector );
60 return jQuery.makeArray( selector, this );
63 // Match html or make sure no context is specified for #id
64 // Note: match[1] may be a string or a TrustedHTML wrapper
65 if ( match && ( match[ 1 ] || !context ) ) {
67 // HANDLE: $(html) -> $(array)
69 context = context instanceof jQuery ? context[ 0 ] : context;
71 // Option to run scripts is true for back-compat
72 // Intentionally let the error be thrown if parseHTML is not present
73 jQuery.merge( this, jQuery.parseHTML(
75 context && context.nodeType ? context.ownerDocument || context : document,
79 // HANDLE: $(html, props)
80 if ( rsingleTag.test( match[ 1 ] ) && jQuery.isPlainObject( context ) ) {
81 for ( match in context ) {
83 // Properties of context are called as methods if possible
84 if ( typeof this[ match ] === "function" ) {
85 this[ match ]( context[ match ] );
87 // ...and otherwise set as attributes
89 this.attr( match, context[ match ] );
98 elem = document.getElementById( match[ 2 ] );
102 // Inject the element directly into the jQuery object
109 // HANDLE: $(expr) & $(expr, $(...))
110 } else if ( !context || context.jquery ) {
111 return ( context || root ).find( selector );
113 // HANDLE: $(expr, context)
114 // (which is just equivalent to: $(context).find(expr)
116 return this.constructor( context ).find( selector );
122 // Give the init function the jQuery prototype for later instantiation
123 init.prototype = jQuery.fn;
125 // Initialize central reference
126 rootjQuery = jQuery( document );