mirror of
https://github.com/jquery/jquery.git
synced 2026-04-20 03:01:22 -04:00
Merge branch '2.0-core' of https://github.com/rwldrn/jquery
* '2.0-core' of https://github.com/rwldrn/jquery: Explanations for each step of isPlainObject obj === Object(obj) comparison is an unnecessary artifact from refactoring Ensure that null/undefined args don't choke on native indexOf Further reduction, thanks @dcherman Straightforward support note Straightforward support note Remove setTimeout for body existance 2.0: Reduce globalEval 2.0: Reduced parseXML 2.0: Reduce isPlainObject 2.0: Remove isArray shim 2.0: Remove now shim 2.0: Remove inArray shim 2.0: Remove JSON.parse shim 2.0: Remove trim shim 2.0: Removes attachEvent paths for jQuery.ready()
This commit is contained in:
192
src/core.js
192
src/core.js
@@ -44,9 +44,6 @@ var
|
||||
// Used for splitting on whitespace
|
||||
core_rnotwhite = /\S+/g,
|
||||
|
||||
// Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE)
|
||||
rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/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 <)
|
||||
@@ -55,12 +52,6 @@ var
|
||||
// Match a standalone tag
|
||||
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
|
||||
|
||||
// JSON RegExp
|
||||
rvalidchars = /^[\],:{}\s]*$/,
|
||||
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
|
||||
rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,
|
||||
rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g,
|
||||
|
||||
// Matches dashed string for camelizing
|
||||
rmsPrefix = /^-ms-/,
|
||||
rdashAlpha = /-([\da-z])/gi,
|
||||
@@ -72,15 +63,8 @@ var
|
||||
|
||||
// The ready event handler and self cleanup method
|
||||
DOMContentLoaded = function() {
|
||||
if ( document.addEventListener ) {
|
||||
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
|
||||
jQuery.ready();
|
||||
} else if ( document.readyState === "complete" ) {
|
||||
// we're here because readyState === "complete" in oldIE
|
||||
// which is good enough for us to call the dom ready!
|
||||
document.detachEvent( "onreadystatechange", DOMContentLoaded );
|
||||
jQuery.ready();
|
||||
}
|
||||
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
|
||||
jQuery.ready();
|
||||
};
|
||||
|
||||
jQuery.fn = jQuery.prototype = {
|
||||
@@ -384,11 +368,6 @@ jQuery.extend({
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
|
||||
if ( !document.body ) {
|
||||
return setTimeout( jQuery.ready );
|
||||
}
|
||||
|
||||
// Remember that the DOM is ready
|
||||
jQuery.isReady = true;
|
||||
|
||||
@@ -413,9 +392,7 @@ jQuery.extend({
|
||||
return jQuery.type(obj) === "function";
|
||||
},
|
||||
|
||||
isArray: Array.isArray || function( obj ) {
|
||||
return jQuery.type(obj) === "array";
|
||||
},
|
||||
isArray: Array.isArray,
|
||||
|
||||
isWindow: function( obj ) {
|
||||
return obj != null && obj == obj.window;
|
||||
@@ -435,32 +412,29 @@ jQuery.extend({
|
||||
},
|
||||
|
||||
isPlainObject: function( obj ) {
|
||||
// Must be an Object.
|
||||
// Because of IE, we also have to check the presence of the constructor property.
|
||||
// Make sure that DOM nodes and window objects don't pass through, as well
|
||||
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
|
||||
// Not plain objects:
|
||||
// - Any object or value whose internal [[Class]] property is not "[object Object]"
|
||||
// - DOM nodes
|
||||
// - window
|
||||
if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Support: Firefox >16
|
||||
// The try/catch supresses exceptions thrown when attempting to access
|
||||
// the "constructor" property of certain host objects, ie. |window.location|
|
||||
try {
|
||||
// Not own constructor property must be Object
|
||||
if ( obj.constructor &&
|
||||
!core_hasOwn.call(obj, "constructor") &&
|
||||
!core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
|
||||
!core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
|
||||
return false;
|
||||
}
|
||||
} catch ( e ) {
|
||||
// IE8,9 Will throw exceptions on certain host objects #9897
|
||||
return false;
|
||||
}
|
||||
|
||||
// Own properties are enumerated firstly, so to speed up,
|
||||
// if last one is own, then all properties are own.
|
||||
|
||||
var key;
|
||||
for ( key in obj ) {}
|
||||
|
||||
return key === undefined || core_hasOwn.call( obj, key );
|
||||
// If the function hasn't returned already, we're confident that
|
||||
// |obj| is a plain object, created by {} or constructed with new Object
|
||||
return true;
|
||||
},
|
||||
|
||||
isEmptyObject: function( obj ) {
|
||||
@@ -504,35 +478,7 @@ jQuery.extend({
|
||||
return jQuery.merge( [], parsed.childNodes );
|
||||
},
|
||||
|
||||
parseJSON: function( data ) {
|
||||
// Attempt to parse using the native JSON parser first
|
||||
if ( window.JSON && window.JSON.parse ) {
|
||||
return window.JSON.parse( data );
|
||||
}
|
||||
|
||||
if ( data === null ) {
|
||||
return data;
|
||||
}
|
||||
|
||||
if ( typeof data === "string" ) {
|
||||
|
||||
// Make sure leading/trailing whitespace is removed (IE can't handle it)
|
||||
data = jQuery.trim( data );
|
||||
|
||||
if ( data ) {
|
||||
// Make sure the incoming data is actual JSON
|
||||
// Logic borrowed from http://json.org/json2.js
|
||||
if ( rvalidchars.test( data.replace( rvalidescape, "@" )
|
||||
.replace( rvalidtokens, "]" )
|
||||
.replace( rvalidbraces, "")) ) {
|
||||
|
||||
return ( new Function( "return " + data ) )();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
jQuery.error( "Invalid JSON: " + data );
|
||||
},
|
||||
parseJSON: JSON.parse,
|
||||
|
||||
// Cross-browser xml parsing
|
||||
parseXML: function( data ) {
|
||||
@@ -540,19 +486,16 @@ jQuery.extend({
|
||||
if ( !data || typeof data !== "string" ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Support: IE9
|
||||
try {
|
||||
if ( window.DOMParser ) { // Standard
|
||||
tmp = new DOMParser();
|
||||
xml = tmp.parseFromString( data , "text/xml" );
|
||||
} else { // IE
|
||||
xml = new ActiveXObject( "Microsoft.XMLDOM" );
|
||||
xml.async = "false";
|
||||
xml.loadXML( data );
|
||||
}
|
||||
} catch( e ) {
|
||||
tmp = new DOMParser();
|
||||
xml = tmp.parseFromString( data , "text/xml" );
|
||||
} catch ( e ) {
|
||||
xml = undefined;
|
||||
}
|
||||
if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
|
||||
|
||||
if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) {
|
||||
jQuery.error( "Invalid XML: " + data );
|
||||
}
|
||||
return xml;
|
||||
@@ -561,16 +504,10 @@ jQuery.extend({
|
||||
noop: function() {},
|
||||
|
||||
// Evaluates a script in a global context
|
||||
// Workarounds based on findings by Jim Driscoll
|
||||
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
|
||||
globalEval: function( data ) {
|
||||
if ( data && jQuery.trim( data ) ) {
|
||||
// We use execScript on Internet Explorer
|
||||
// We use an anonymous function so that context is window
|
||||
// rather than jQuery in Firefox
|
||||
( window.execScript || function( data ) {
|
||||
window[ "eval" ].call( window, data );
|
||||
} )( data );
|
||||
var indirect = eval;
|
||||
if ( jQuery.trim( data ) ) {
|
||||
indirect( data + ";" );
|
||||
}
|
||||
},
|
||||
|
||||
@@ -634,20 +571,9 @@ jQuery.extend({
|
||||
return obj;
|
||||
},
|
||||
|
||||
// Use native String.trim function wherever possible
|
||||
trim: core_trim && !core_trim.call("\uFEFF\xA0") ?
|
||||
function( text ) {
|
||||
return text == null ?
|
||||
"" :
|
||||
core_trim.call( text );
|
||||
} :
|
||||
|
||||
// Otherwise use our own trimming functionality
|
||||
function( text ) {
|
||||
return text == null ?
|
||||
"" :
|
||||
( text + "" ).replace( rtrim, "" );
|
||||
},
|
||||
trim: function( text ) {
|
||||
return text == null ? "" : core_trim.call( text );
|
||||
},
|
||||
|
||||
// results is for internal usage only
|
||||
makeArray: function( arr, results ) {
|
||||
@@ -668,25 +594,7 @@ jQuery.extend({
|
||||
},
|
||||
|
||||
inArray: function( elem, arr, i ) {
|
||||
var len;
|
||||
|
||||
if ( arr ) {
|
||||
if ( core_indexOf ) {
|
||||
return core_indexOf.call( arr, elem, i );
|
||||
}
|
||||
|
||||
len = arr.length;
|
||||
i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;
|
||||
|
||||
for ( ; i < len; i++ ) {
|
||||
// Skip accessing in sparse arrays
|
||||
if ( i in arr && arr[ i ] === elem ) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
return arr == null ? -1 : core_indexOf.call( arr, elem, i );
|
||||
},
|
||||
|
||||
merge: function( first, second ) {
|
||||
@@ -846,9 +754,7 @@ jQuery.extend({
|
||||
length ? fn( elems[0], key ) : emptyGet;
|
||||
},
|
||||
|
||||
now: function() {
|
||||
return ( new Date() ).getTime();
|
||||
}
|
||||
now: Date.now
|
||||
});
|
||||
|
||||
jQuery.ready.promise = function( obj ) {
|
||||
@@ -864,46 +770,12 @@ jQuery.ready.promise = function( obj ) {
|
||||
setTimeout( jQuery.ready );
|
||||
|
||||
// Standards-based browsers support DOMContentLoaded
|
||||
} else if ( document.addEventListener ) {
|
||||
} else {
|
||||
// Use the handy event callback
|
||||
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
|
||||
|
||||
// A fallback to window.onload, that will always work
|
||||
window.addEventListener( "load", jQuery.ready, false );
|
||||
|
||||
// If IE event model is used
|
||||
} else {
|
||||
// Ensure firing before onload, maybe late but safe also for iframes
|
||||
document.attachEvent( "onreadystatechange", DOMContentLoaded );
|
||||
|
||||
// A fallback to window.onload, that will always work
|
||||
window.attachEvent( "onload", jQuery.ready );
|
||||
|
||||
// If IE and not a frame
|
||||
// continually check to see if the document is ready
|
||||
var top = false;
|
||||
|
||||
try {
|
||||
top = window.frameElement == null && document.documentElement;
|
||||
} catch(e) {}
|
||||
|
||||
if ( top && top.doScroll ) {
|
||||
(function doScrollCheck() {
|
||||
if ( !jQuery.isReady ) {
|
||||
|
||||
try {
|
||||
// Use the trick by Diego Perini
|
||||
// http://javascript.nwbox.com/IEContentLoaded/
|
||||
top.doScroll("left");
|
||||
} catch(e) {
|
||||
return setTimeout( doScrollCheck, 50 );
|
||||
}
|
||||
|
||||
// and execute any waiting functions
|
||||
jQuery.ready();
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
}
|
||||
return readyList.promise( obj );
|
||||
|
||||
Reference in New Issue
Block a user