diff options
author | Timmy Willison <timmywillisn@gmail.com> | 2013-08-15 14:15:49 -0400 |
---|---|---|
committer | Timmy Willison <timmywillisn@gmail.com> | 2013-08-15 14:15:49 -0400 |
commit | 6318ae6ab90d4b450dfadf32ab95fe52ed6331cb (patch) | |
tree | 50b247fed8569e909e380b281e9145bd1458a39e /src/core.js | |
parent | 7627b8b6d9ef6e57dbd20a55b946bd1991c1223e (diff) | |
download | jquery-6318ae6ab90d4b450dfadf32ab95fe52ed6331cb.tar.gz jquery-6318ae6ab90d4b450dfadf32ab95fe52ed6331cb.zip |
AMD-ify jQuery sourcegit s! Woo! Fixes #14113, #14163.
Diffstat (limited to 'src/core.js')
-rw-r--r-- | src/core.js | 198 |
1 files changed, 46 insertions, 152 deletions
diff --git a/src/core.js b/src/core.js index 55a0c25d3..6d5c747f4 100644 --- a/src/core.js +++ b/src/core.js @@ -1,18 +1,24 @@ +define([ + "./var/strundefined", + "./var/arr", + "./var/slice", + "./var/concat", + "./var/push", + "./var/indexOf", + // [[Class]] -> type pairs + "./var/class2type", + "./var/toString", + "./var/hasOwn", + "./var/trim" +], function( strundefined, arr, slice, concat, push, indexOf, + class2type, toString, hasOwn, trim ) { + var // A central reference to the root jQuery(document) rootjQuery, - // The deferred used on DOM ready - readyList, - - // Support: IE9 - // For `typeof xmlNode.method` instead of `xmlNode.method !== undefined` - core_strundefined = typeof undefined, - // Use the correct document accordingly with window argument (sandbox) - location = window.location, document = window.document, - docElem = document.documentElement, // Map over jQuery in case of overwrite _jQuery = window.jQuery, @@ -20,22 +26,7 @@ var // Map over the $ in case of overwrite _$ = window.$, - // [[Class]] -> type pairs - class2type = {}, - - // List of deleted data cache ids, so we can reuse them - core_deletedIds = [], - - core_version = "@VERSION", - - // Save a reference to some core methods - core_concat = core_deletedIds.concat, - core_push = core_deletedIds.push, - core_slice = core_deletedIds.slice, - core_indexOf = core_deletedIds.indexOf, - core_toString = class2type.toString, - core_hasOwn = class2type.hasOwnProperty, - core_trim = core_version.trim, + version = "@VERSION", // Define a local copy of jQuery jQuery = function( selector, context ) { @@ -43,12 +34,6 @@ var return new jQuery.fn.init( selector, context, rootjQuery ); }, - // Used for matching numbers - core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source, - - // Used for splitting on whitespace - core_rnotwhite = /\S+/g, - // 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 <) @@ -64,20 +49,14 @@ var // Used by jQuery.camelCase as callback to replace() fcamelCase = function( all, letter ) { return letter.toUpperCase(); - }, - - // The ready event handler and self cleanup method - completed = function() { - document.removeEventListener( "DOMContentLoaded", completed, false ); - window.removeEventListener( "load", completed, false ); - jQuery.ready(); }; jQuery.fn = jQuery.prototype = { // The current version of jQuery being used - jquery: core_version, + jquery: version, constructor: jQuery, + init: function( selector, context, rootjQuery ) { var match, elem; @@ -162,7 +141,10 @@ jQuery.fn = jQuery.prototype = { // HANDLE: $(function) // Shortcut for document ready } else if ( jQuery.isFunction( selector ) ) { - return rootjQuery.ready( selector ); + return typeof rootjQuery.ready !== "undefined" ? + rootjQuery.ready( selector ) : + // Execute immediately if ready is not present + selector( jQuery ); } if ( selector.selector !== undefined ) { @@ -180,7 +162,7 @@ jQuery.fn = jQuery.prototype = { length: 0, toArray: function() { - return core_slice.call( this ); + return slice.call( this ); }, // Get the Nth element in the matched element set OR @@ -217,15 +199,8 @@ jQuery.fn = jQuery.prototype = { return jQuery.each( this, callback, args ); }, - ready: function( fn ) { - // Add the callback - jQuery.ready.promise().done( fn ); - - return this; - }, - slice: function() { - return this.pushStack( core_slice.apply( this, arguments ) ); + return this.pushStack( slice.apply( this, arguments ) ); }, first: function() { @@ -254,9 +229,9 @@ jQuery.fn = jQuery.prototype = { // For internal use only. // Behaves like an Array's method, not like a jQuery method. - push: core_push, - sort: [].sort, - splice: [].splice + push: push, + sort: arr.sort, + splice: arr.splice }; // Give the init function the jQuery prototype for later instantiation @@ -328,7 +303,10 @@ jQuery.extend = jQuery.fn.extend = function() { jQuery.extend({ // Unique for each copy of jQuery on the page - expando: "jQuery" + ( core_version + Math.random() ).replace( /\D/g, "" ), + expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ), + + // Assume jQuery is ready without the ready module + isReady: true, noConflict: function( deep ) { if ( window.$ === jQuery ) { @@ -342,47 +320,6 @@ jQuery.extend({ return jQuery; }, - // Is the DOM ready to be used? Set to true once it occurs. - isReady: false, - - // A counter to track how many items to wait for before - // the ready event fires. See #6781 - readyWait: 1, - - // Hold (or release) the ready event - holdReady: function( hold ) { - if ( hold ) { - jQuery.readyWait++; - } else { - jQuery.ready( true ); - } - }, - - // Handle when the DOM is ready - ready: function( wait ) { - - // Abort if there are pending holds or we're already ready - if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) { - return; - } - - // Remember that the DOM is ready - jQuery.isReady = true; - - // If a normal DOM Ready event fired, decrement, and wait if need be - if ( wait !== true && --jQuery.readyWait > 0 ) { - return; - } - - // If there are functions bound, to execute - readyList.resolveWith( document, [ jQuery ] ); - - // Trigger any bound ready events - if ( jQuery.fn.trigger ) { - jQuery( document ).trigger("ready").off("ready"); - } - }, - // See test/unit/core.js for details concerning isFunction. // Since version 1.3, DOM methods and functions like alert // aren't supported. They return false on IE (#2968). @@ -406,7 +343,7 @@ jQuery.extend({ } // Support: Safari <= 5.1 (functionish RegExp) return typeof obj === "object" || typeof obj === "function" ? - class2type[ core_toString.call(obj) ] || "object" : + class2type[ toString.call(obj) ] || "object" : typeof obj; }, @@ -425,7 +362,7 @@ jQuery.extend({ // https://bugzilla.mozilla.org/show_bug.cgi?id=814622 try { if ( obj.constructor && - !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) { + !hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) { return false; } } catch ( e ) { @@ -452,6 +389,7 @@ jQuery.extend({ // data: string of html // context (optional): If specified, the fragment will be created in this context, defaults to document // keepScripts (optional): If true, will include scripts passed in the html string + // TODO: Circular reference core -> manipulation -> core parseHTML: function( data, context, keepScripts ) { if ( !data || typeof data !== "string" ) { return null; @@ -507,7 +445,7 @@ jQuery.extend({ // Evaluates a script in a global context globalEval: function( code ) { var script, - indirect = eval; + indirect = eval; code = jQuery.trim( code ); @@ -588,7 +526,7 @@ jQuery.extend({ }, trim: function( text ) { - return text == null ? "" : core_trim.call( text ); + return text == null ? "" : trim.call( text ); }, // results is for internal usage only @@ -602,7 +540,7 @@ jQuery.extend({ [ arr ] : arr ); } else { - core_push.call( ret, arr ); + push.call( ret, arr ); } } @@ -610,7 +548,7 @@ jQuery.extend({ }, inArray: function( elem, arr, i ) { - return arr == null ? -1 : core_indexOf.call( arr, elem, i ); + return arr == null ? -1 : indexOf.call( arr, elem, i ); }, merge: function( first, second ) { @@ -682,7 +620,7 @@ jQuery.extend({ } // Flatten any nested arrays - return core_concat.apply( [], ret ); + return concat.apply( [], ret ); }, // A global GUID counter for objects @@ -706,9 +644,9 @@ jQuery.extend({ } // Simulated bind - args = core_slice.call( arguments, 2 ); + args = slice.call( arguments, 2 ); proxy = function() { - return fn.apply( context || this, args.concat( core_slice.call( arguments ) ) ); + return fn.apply( context || this, args.concat( slice.call( arguments ) ) ); }; // Set the guid of unique handler to the same of original handler, so it can be removed @@ -770,56 +708,9 @@ jQuery.extend({ length ? fn( elems[0], key ) : emptyGet; }, - now: Date.now, - - // A method for quickly swapping in/out CSS properties to get correct calculations. - // Note: this method belongs to the css module but it's needed here for the support module. - // If support gets modularized, this method should be moved back to the css module. - swap: function( elem, options, callback, args ) { - var ret, name, - old = {}; - - // Remember the old values, and insert the new ones - for ( name in options ) { - old[ name ] = elem.style[ name ]; - elem.style[ name ] = options[ name ]; - } - - ret = callback.apply( elem, args || [] ); - - // Revert the old values - for ( name in options ) { - elem.style[ name ] = old[ name ]; - } - - return ret; - } + now: Date.now }); -jQuery.ready.promise = function( obj ) { - if ( !readyList ) { - - readyList = jQuery.Deferred(); - - // Catch cases where $(document).ready() is called after the browser event has already occurred. - // we once tried to use readyState "interactive" here, but it caused issues like the one - // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15 - if ( document.readyState === "complete" ) { - // Handle it asynchronously to allow scripts the opportunity to delay ready - setTimeout( jQuery.ready ); - - } else { - - // Use the handy event callback - document.addEventListener( "DOMContentLoaded", completed, false ); - - // A fallback to window.onload, that will always work - window.addEventListener( "load", completed, false ); - } - } - return readyList.promise( obj ); -}; - // Populate the class2type map jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) { class2type[ "[object " + name + "]" ] = name.toLowerCase(); @@ -843,4 +734,7 @@ function isArraylike( obj ) { } // All jQuery objects should point back to these -rootjQuery = jQuery(document); +rootjQuery = jQuery( document ); + +return jQuery; +}); |