Blame view

bower_components/jquery/src/core/init.js 3.32 KB
74249687   Luigi Serra   Cross browser con...
1
  // Initialize a jQuery object
c5169e0e   Renato De Donato   a new hope
2
  define([
74249687   Luigi Serra   Cross browser con...
3
  	"../core",
74249687   Luigi Serra   Cross browser con...
4
5
  	"./var/rsingleTag",
  	"../traversing/findFilter"
c5169e0e   Renato De Donato   a new hope
6
  ], function( jQuery, rsingleTag ) {
74249687   Luigi Serra   Cross browser con...
7
8
9
10
11
12
13
14
15
  
  // 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-]*))$/,
  
c5169e0e   Renato De Donato   a new hope
16
  	init = jQuery.fn.init = function( selector, context ) {
74249687   Luigi Serra   Cross browser con...
17
18
19
20
21
22
23
  		var match, elem;
  
  		// HANDLE: $(""), $(null), $(undefined), $(false)
  		if ( !selector ) {
  			return this;
  		}
  
74249687   Luigi Serra   Cross browser con...
24
25
  		// Handle HTML strings
  		if ( typeof selector === "string" ) {
c5169e0e   Renato De Donato   a new hope
26
  			if ( selector[0] === "<" && selector[ selector.length - 1 ] === ">" && selector.length >= 3 ) {
74249687   Luigi Serra   Cross browser con...
27
28
29
30
31
32
33
34
  				// 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
c5169e0e   Renato De Donato   a new hope
35
  			if ( match && (match[1] || !context) ) {
74249687   Luigi Serra   Cross browser con...
36
37
  
  				// HANDLE: $(html) -> $(array)
c5169e0e   Renato De Donato   a new hope
38
39
  				if ( match[1] ) {
  					context = context instanceof jQuery ? context[0] : context;
74249687   Luigi Serra   Cross browser con...
40
41
42
43
  
  					// 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(
c5169e0e   Renato De Donato   a new hope
44
  						match[1],
74249687   Luigi Serra   Cross browser con...
45
46
47
48
49
  						context && context.nodeType ? context.ownerDocument || context : document,
  						true
  					) );
  
  					// HANDLE: $(html, props)
c5169e0e   Renato De Donato   a new hope
50
  					if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
74249687   Luigi Serra   Cross browser con...
51
  						for ( match in context ) {
74249687   Luigi Serra   Cross browser con...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  							// 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 {
c5169e0e   Renato De Donato   a new hope
67
  					elem = document.getElementById( match[2] );
74249687   Luigi Serra   Cross browser con...
68
69
70
71
  
  					// Support: Blackberry 4.6
  					// gEBID returns nodes no longer in the document (#6963)
  					if ( elem && elem.parentNode ) {
74249687   Luigi Serra   Cross browser con...
72
73
  						// Inject the element directly into the jQuery object
  						this.length = 1;
c5169e0e   Renato De Donato   a new hope
74
  						this[0] = elem;
74249687   Luigi Serra   Cross browser con...
75
76
77
78
79
80
81
82
83
  					}
  
  					this.context = document;
  					this.selector = selector;
  					return this;
  				}
  
  			// HANDLE: $(expr, $(...))
  			} else if ( !context || context.jquery ) {
c5169e0e   Renato De Donato   a new hope
84
  				return ( context || rootjQuery ).find( selector );
74249687   Luigi Serra   Cross browser con...
85
86
87
88
89
90
91
92
93
  
  			// HANDLE: $(expr, context)
  			// (which is just equivalent to: $(context).find(expr)
  			} else {
  				return this.constructor( context ).find( selector );
  			}
  
  		// HANDLE: $(DOMElement)
  		} else if ( selector.nodeType ) {
c5169e0e   Renato De Donato   a new hope
94
  			this.context = this[0] = selector;
74249687   Luigi Serra   Cross browser con...
95
96
97
98
99
100
  			this.length = 1;
  			return this;
  
  		// HANDLE: $(function)
  		// Shortcut for document ready
  		} else if ( jQuery.isFunction( selector ) ) {
c5169e0e   Renato De Donato   a new hope
101
102
  			return typeof rootjQuery.ready !== "undefined" ?
  				rootjQuery.ready( selector ) :
74249687   Luigi Serra   Cross browser con...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  				// 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;
  
c5169e0e   Renato De Donato   a new hope
123
  });