Commit 6d108011 authored by Rick Waldron's avatar Rick Waldron

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()
parents 57b268e6 3746bf8f
...@@ -44,9 +44,6 @@ var ...@@ -44,9 +44,6 @@ var
// Used for splitting on whitespace // Used for splitting on whitespace
core_rnotwhite = /\S+/g, 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 // A simple way to check for HTML strings
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521) // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
// Strict HTML recognition (#11290: must start with <) // Strict HTML recognition (#11290: must start with <)
...@@ -55,12 +52,6 @@ var ...@@ -55,12 +52,6 @@ var
// Match a standalone tag // Match a standalone tag
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/, 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 // Matches dashed string for camelizing
rmsPrefix = /^-ms-/, rmsPrefix = /^-ms-/,
rdashAlpha = /-([\da-z])/gi, rdashAlpha = /-([\da-z])/gi,
...@@ -72,15 +63,8 @@ var ...@@ -72,15 +63,8 @@ var
// The ready event handler and self cleanup method // The ready event handler and self cleanup method
DOMContentLoaded = function() { DOMContentLoaded = function() {
if ( document.addEventListener ) { document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); jQuery.ready();
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();
}
}; };
jQuery.fn = jQuery.prototype = { jQuery.fn = jQuery.prototype = {
...@@ -384,11 +368,6 @@ jQuery.extend({ ...@@ -384,11 +368,6 @@ jQuery.extend({
return; 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 // Remember that the DOM is ready
jQuery.isReady = true; jQuery.isReady = true;
...@@ -413,9 +392,7 @@ jQuery.extend({ ...@@ -413,9 +392,7 @@ jQuery.extend({
return jQuery.type(obj) === "function"; return jQuery.type(obj) === "function";
}, },
isArray: Array.isArray || function( obj ) { isArray: Array.isArray,
return jQuery.type(obj) === "array";
},
isWindow: function( obj ) { isWindow: function( obj ) {
return obj != null && obj == obj.window; return obj != null && obj == obj.window;
...@@ -435,32 +412,29 @@ jQuery.extend({ ...@@ -435,32 +412,29 @@ jQuery.extend({
}, },
isPlainObject: function( obj ) { isPlainObject: function( obj ) {
// Must be an Object. // Not plain objects:
// Because of IE, we also have to check the presence of the constructor property. // - Any object or value whose internal [[Class]] property is not "[object Object]"
// Make sure that DOM nodes and window objects don't pass through, as well // - DOM nodes
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) { // - window
if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false; 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 { try {
// Not own constructor property must be Object
if ( obj.constructor && if ( obj.constructor &&
!core_hasOwn.call(obj, "constructor") && !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
!core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
return false; return false;
} }
} catch ( e ) { } catch ( e ) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false; return false;
} }
// Own properties are enumerated firstly, so to speed up, // If the function hasn't returned already, we're confident that
// if last one is own, then all properties are own. // |obj| is a plain object, created by {} or constructed with new Object
return true;
var key;
for ( key in obj ) {}
return key === undefined || core_hasOwn.call( obj, key );
}, },
isEmptyObject: function( obj ) { isEmptyObject: function( obj ) {
...@@ -504,35 +478,7 @@ jQuery.extend({ ...@@ -504,35 +478,7 @@ jQuery.extend({
return jQuery.merge( [], parsed.childNodes ); return jQuery.merge( [], parsed.childNodes );
}, },
parseJSON: function( data ) { parseJSON: JSON.parse,
// 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 );
},
// Cross-browser xml parsing // Cross-browser xml parsing
parseXML: function( data ) { parseXML: function( data ) {
...@@ -540,19 +486,16 @@ jQuery.extend({ ...@@ -540,19 +486,16 @@ jQuery.extend({
if ( !data || typeof data !== "string" ) { if ( !data || typeof data !== "string" ) {
return null; return null;
} }
// Support: IE9
try { try {
if ( window.DOMParser ) { // Standard tmp = new DOMParser();
tmp = new DOMParser(); xml = tmp.parseFromString( data , "text/xml" );
xml = tmp.parseFromString( data , "text/xml" ); } catch ( e ) {
} else { // IE
xml = new ActiveXObject( "Microsoft.XMLDOM" );
xml.async = "false";
xml.loadXML( data );
}
} catch( e ) {
xml = undefined; xml = undefined;
} }
if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) {
jQuery.error( "Invalid XML: " + data ); jQuery.error( "Invalid XML: " + data );
} }
return xml; return xml;
...@@ -561,16 +504,10 @@ jQuery.extend({ ...@@ -561,16 +504,10 @@ jQuery.extend({
noop: function() {}, noop: function() {},
// Evaluates a script in a global context // 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 ) { globalEval: function( data ) {
if ( data && jQuery.trim( data ) ) { var indirect = eval;
// We use execScript on Internet Explorer if ( jQuery.trim( data ) ) {
// We use an anonymous function so that context is window indirect( data + ";" );
// rather than jQuery in Firefox
( window.execScript || function( data ) {
window[ "eval" ].call( window, data );
} )( data );
} }
}, },
...@@ -634,20 +571,9 @@ jQuery.extend({ ...@@ -634,20 +571,9 @@ jQuery.extend({
return obj; return obj;
}, },
// Use native String.trim function wherever possible trim: function( text ) {
trim: core_trim && !core_trim.call("\uFEFF\xA0") ? return text == null ? "" : core_trim.call( text );
function( text ) { },
return text == null ?
"" :
core_trim.call( text );
} :
// Otherwise use our own trimming functionality
function( text ) {
return text == null ?
"" :
( text + "" ).replace( rtrim, "" );
},
// results is for internal usage only // results is for internal usage only
makeArray: function( arr, results ) { makeArray: function( arr, results ) {
...@@ -668,25 +594,7 @@ jQuery.extend({ ...@@ -668,25 +594,7 @@ jQuery.extend({
}, },
inArray: function( elem, arr, i ) { inArray: function( elem, arr, i ) {
var len; return arr == null ? -1 : core_indexOf.call( arr, elem, i );
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;
}, },
merge: function( first, second ) { merge: function( first, second ) {
...@@ -846,9 +754,7 @@ jQuery.extend({ ...@@ -846,9 +754,7 @@ jQuery.extend({
length ? fn( elems[0], key ) : emptyGet; length ? fn( elems[0], key ) : emptyGet;
}, },
now: function() { now: Date.now
return ( new Date() ).getTime();
}
}); });
jQuery.ready.promise = function( obj ) { jQuery.ready.promise = function( obj ) {
...@@ -864,46 +770,12 @@ jQuery.ready.promise = function( obj ) { ...@@ -864,46 +770,12 @@ jQuery.ready.promise = function( obj ) {
setTimeout( jQuery.ready ); setTimeout( jQuery.ready );
// Standards-based browsers support DOMContentLoaded // Standards-based browsers support DOMContentLoaded
} else if ( document.addEventListener ) { } else {
// Use the handy event callback // Use the handy event callback
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// A fallback to window.onload, that will always work // A fallback to window.onload, that will always work
window.addEventListener( "load", jQuery.ready, false ); 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 ); return readyList.promise( obj );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment