Manipulation: Support $el.html(selfRemovingScript) (#5378)
[jquery.git] / src / core / init.js
blob9c94547266c48694b3e72f9096295bddc7bd5d19
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)
10 var rootjQuery,
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 ) {
19                 var match, elem;
21                 // HANDLE: $(""), $(null), $(undefined), $(false)
22                 if ( !selector ) {
23                         return this;
24                 }
26                 // HANDLE: $(DOMElement)
27                 if ( selector.nodeType ) {
28                         this[ 0 ] = selector;
29                         this.length = 1;
30                         return this;
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
39                                 selector( jQuery );
41                 } else {
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
49                                 // like TrustedHTML.
50                                 match = [ null, selector, null ];
52                         // Handle HTML strings or selectors
53                         } else if ( typeof selector === "string" ) {
54                                 match = rquickExpr.exec( selector );
55                         } else {
56                                 return jQuery.makeArray( selector, this );
57                         }
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)
64                                 if ( match[ 1 ] ) {
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(
70                                                 match[ 1 ],
71                                                 context && context.nodeType ? context.ownerDocument || context : document,
72                                                 true
73                                         ) );
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
84                                                         } else {
85                                                                 this.attr( match, context[ match ] );
86                                                         }
87                                                 }
88                                         }
90                                         return this;
92                                 // HANDLE: $(#id)
93                                 } else {
94                                         elem = document.getElementById( match[ 2 ] );
96                                         if ( elem ) {
98                                                 // Inject the element directly into the jQuery object
99                                                 this[ 0 ] = elem;
100                                                 this.length = 1;
101                                         }
102                                         return this;
103                                 }
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)
111                         } else {
112                                 return this.constructor( context ).find( selector );
113                         }
114                 }
116         };
118 // Give the init function the jQuery prototype for later instantiation
119 init.prototype = jQuery.fn;
121 // Initialize central reference
122 rootjQuery = jQuery( document );