UNPKG

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