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 (trac-9521)
14 // Strict HTML recognition (trac-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 ) {
21 // HANDLE: $(""), $(null), $(undefined), $(false)
26 // HANDLE: $(DOMElement)
27 if ( selector.nodeType ) {
32 // HANDLE: $(function)
33 // Shortcut for document ready
34 } else if ( typeof selector === "function" ) {
35 return rootjQuery.ready !== undefined ?
36 rootjQuery.ready( selector ) :
38 // Execute immediately if ready is not present
43 // Handle obvious HTML strings
44 match = selector + "";
45 if ( isObviousHtml( match ) ) {
47 // Assume that strings that start and end with <> are HTML and skip
48 // the regex check. This also handles browser-supported HTML wrappers
50 match = [ null, selector, null ];
52 // Handle HTML strings or selectors
53 } else if ( typeof selector === "string" ) {
54 match = rquickExpr.exec( selector );
56 return jQuery.makeArray( selector, this );
59 // Match html or make sure no context is specified for #id
60 // Note: match[1] may be a string or a TrustedHTML wrapper
61 if ( match && ( match[ 1 ] || !context ) ) {
63 // HANDLE: $(html) -> $(array)
65 context = context instanceof jQuery ? context[ 0 ] : context;
67 // Option to run scripts is true for back-compat
68 // Intentionally let the error be thrown if parseHTML is not present
69 jQuery.merge( this, jQuery.parseHTML(
71 context && context.nodeType ? context.ownerDocument || context : document,
75 // HANDLE: $(html, props)
76 if ( rsingleTag.test( match[ 1 ] ) && jQuery.isPlainObject( context ) ) {
77 for ( match in context ) {
79 // Properties of context are called as methods if possible
80 if ( typeof this[ match ] === "function" ) {
81 this[ match ]( context[ match ] );
83 // ...and otherwise set as attributes
85 this.attr( match, context[ match ] );
94 elem = document.getElementById( match[ 2 ] );
98 // Inject the element directly into the jQuery object
105 // HANDLE: $(expr) & $(expr, $(...))
106 } else if ( !context || context.jquery ) {
107 return ( context || rootjQuery ).find( selector );
109 // HANDLE: $(expr, context)
110 // (which is just equivalent to: $(context).find(expr)
112 return this.constructor( context ).find( selector );
118 // Give the init function the jQuery prototype for later instantiation
119 init.prototype = jQuery.fn;
121 // Initialize central reference
122 rootjQuery = jQuery( document );