Blame view

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