Core:Manipulation: Add basic TrustedHTML support
[jquery.git] / src / core / init.js
blob8fc24d8dd6b54aae34e626dfe72d5a7ebdffe6fc
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 (#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 ) {
19                 var match, elem;
21                 // HANDLE: $(""), $(null), $(undefined), $(false)
22                 if ( !selector ) {
23                         return this;
24                 }
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 ) {
32                         this[ 0 ] = selector;
33                         this.length = 1;
34                         return this;
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
43                                 selector( jQuery );
45                 } else {
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
53                                 // like TrustedHTML.
54                                 match = [ null, selector, null ];
56                         // Handle HTML strings or selectors
57                         } else if ( typeof selector === "string" ) {
58                                 match = rquickExpr.exec( selector );
59                         } else {
60                                 return jQuery.makeArray( selector, this );
61                         }
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)
68                                 if ( match[ 1 ] ) {
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(
74                                                 match[ 1 ],
75                                                 context && context.nodeType ? context.ownerDocument || context : document,
76                                                 true
77                                         ) );
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
88                                                         } else {
89                                                                 this.attr( match, context[ match ] );
90                                                         }
91                                                 }
92                                         }
94                                         return this;
96                                 // HANDLE: $(#id)
97                                 } else {
98                                         elem = document.getElementById( match[ 2 ] );
100                                         if ( elem ) {
102                                                 // Inject the element directly into the jQuery object
103                                                 this[ 0 ] = elem;
104                                                 this.length = 1;
105                                         }
106                                         return this;
107                                 }
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)
115                         } else {
116                                 return this.constructor( context ).find( selector );
117                         }
118                 }
120         };
122 // Give the init function the jQuery prototype for later instantiation
123 init.prototype = jQuery.fn;
125 // Initialize central reference
126 rootjQuery = jQuery( document );