Skip to content

Commit 6d10801

Browse files
committed
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()
2 parents 57b268e + 3746bf8 commit 6d10801

File tree

1 file changed

+32
-160
lines changed

1 file changed

+32
-160
lines changed

src/core.js

Lines changed: 32 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ var
4444
// Used for splitting on whitespace
4545
core_rnotwhite = /\S+/g,
4646

47-
// Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE)
48-
rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,
49-
5047
// A simple way to check for HTML strings
5148
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
5249
// Strict HTML recognition (#11290: must start with <)
@@ -55,12 +52,6 @@ var
5552
// Match a standalone tag
5653
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
5754

58-
// JSON RegExp
59-
rvalidchars = /^[\],:{}\s]*$/,
60-
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
61-
rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,
62-
rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g,
63-
6455
// Matches dashed string for camelizing
6556
rmsPrefix = /^-ms-/,
6657
rdashAlpha = /-([\da-z])/gi,
@@ -72,15 +63,8 @@ var
7263

7364
// The ready event handler and self cleanup method
7465
DOMContentLoaded = function() {
75-
if ( document.addEventListener ) {
76-
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
77-
jQuery.ready();
78-
} else if ( document.readyState === "complete" ) {
79-
// we're here because readyState === "complete" in oldIE
80-
// which is good enough for us to call the dom ready!
81-
document.detachEvent( "onreadystatechange", DOMContentLoaded );
82-
jQuery.ready();
83-
}
66+
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
67+
jQuery.ready();
8468
};
8569

8670
jQuery.fn = jQuery.prototype = {
@@ -384,11 +368,6 @@ jQuery.extend({
384368
return;
385369
}
386370

387-
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
388-
if ( !document.body ) {
389-
return setTimeout( jQuery.ready );
390-
}
391-
392371
// Remember that the DOM is ready
393372
jQuery.isReady = true;
394373

@@ -413,9 +392,7 @@ jQuery.extend({
413392
return jQuery.type(obj) === "function";
414393
},
415394

416-
isArray: Array.isArray || function( obj ) {
417-
return jQuery.type(obj) === "array";
418-
},
395+
isArray: Array.isArray,
419396

420397
isWindow: function( obj ) {
421398
return obj != null && obj == obj.window;
@@ -435,32 +412,29 @@ jQuery.extend({
435412
},
436413

437414
isPlainObject: function( obj ) {
438-
// Must be an Object.
439-
// Because of IE, we also have to check the presence of the constructor property.
440-
// Make sure that DOM nodes and window objects don't pass through, as well
441-
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
415+
// Not plain objects:
416+
// - Any object or value whose internal [[Class]] property is not "[object Object]"
417+
// - DOM nodes
418+
// - window
419+
if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
442420
return false;
443421
}
444422

423+
// Support: Firefox >16
424+
// The try/catch supresses exceptions thrown when attempting to access
425+
// the "constructor" property of certain host objects, ie. |window.location|
445426
try {
446-
// Not own constructor property must be Object
447427
if ( obj.constructor &&
448-
!core_hasOwn.call(obj, "constructor") &&
449-
!core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
428+
!core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
450429
return false;
451430
}
452431
} catch ( e ) {
453-
// IE8,9 Will throw exceptions on certain host objects #9897
454432
return false;
455433
}
456434

457-
// Own properties are enumerated firstly, so to speed up,
458-
// if last one is own, then all properties are own.
459-
460-
var key;
461-
for ( key in obj ) {}
462-
463-
return key === undefined || core_hasOwn.call( obj, key );
435+
// If the function hasn't returned already, we're confident that
436+
// |obj| is a plain object, created by {} or constructed with new Object
437+
return true;
464438
},
465439

466440
isEmptyObject: function( obj ) {
@@ -504,55 +478,24 @@ jQuery.extend({
504478
return jQuery.merge( [], parsed.childNodes );
505479
},
506480

507-
parseJSON: function( data ) {
508-
// Attempt to parse using the native JSON parser first
509-
if ( window.JSON && window.JSON.parse ) {
510-
return window.JSON.parse( data );
511-
}
512-
513-
if ( data === null ) {
514-
return data;
515-
}
516-
517-
if ( typeof data === "string" ) {
518-
519-
// Make sure leading/trailing whitespace is removed (IE can't handle it)
520-
data = jQuery.trim( data );
521-
522-
if ( data ) {
523-
// Make sure the incoming data is actual JSON
524-
// Logic borrowed from http://json.org/json2.js
525-
if ( rvalidchars.test( data.replace( rvalidescape, "@" )
526-
.replace( rvalidtokens, "]" )
527-
.replace( rvalidbraces, "")) ) {
528-
529-
return ( new Function( "return " + data ) )();
530-
}
531-
}
532-
}
533-
534-
jQuery.error( "Invalid JSON: " + data );
535-
},
481+
parseJSON: JSON.parse,
536482

537483
// Cross-browser xml parsing
538484
parseXML: function( data ) {
539485
var xml, tmp;
540486
if ( !data || typeof data !== "string" ) {
541487
return null;
542488
}
489+
490+
// Support: IE9
543491
try {
544-
if ( window.DOMParser ) { // Standard
545-
tmp = new DOMParser();
546-
xml = tmp.parseFromString( data , "text/xml" );
547-
} else { // IE
548-
xml = new ActiveXObject( "Microsoft.XMLDOM" );
549-
xml.async = "false";
550-
xml.loadXML( data );
551-
}
552-
} catch( e ) {
492+
tmp = new DOMParser();
493+
xml = tmp.parseFromString( data , "text/xml" );
494+
} catch ( e ) {
553495
xml = undefined;
554496
}
555-
if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
497+
498+
if ( !xml || xml.getElementsByTagName( "parsererror" ).length ) {
556499
jQuery.error( "Invalid XML: " + data );
557500
}
558501
return xml;
@@ -561,16 +504,10 @@ jQuery.extend({
561504
noop: function() {},
562505

563506
// Evaluates a script in a global context
564-
// Workarounds based on findings by Jim Driscoll
565-
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
566507
globalEval: function( data ) {
567-
if ( data && jQuery.trim( data ) ) {
568-
// We use execScript on Internet Explorer
569-
// We use an anonymous function so that context is window
570-
// rather than jQuery in Firefox
571-
( window.execScript || function( data ) {
572-
window[ "eval" ].call( window, data );
573-
} )( data );
508+
var indirect = eval;
509+
if ( jQuery.trim( data ) ) {
510+
indirect( data + ";" );
574511
}
575512
},
576513

@@ -634,20 +571,9 @@ jQuery.extend({
634571
return obj;
635572
},
636573

637-
// Use native String.trim function wherever possible
638-
trim: core_trim && !core_trim.call("\uFEFF\xA0") ?
639-
function( text ) {
640-
return text == null ?
641-
"" :
642-
core_trim.call( text );
643-
} :
644-
645-
// Otherwise use our own trimming functionality
646-
function( text ) {
647-
return text == null ?
648-
"" :
649-
( text + "" ).replace( rtrim, "" );
650-
},
574+
trim: function( text ) {
575+
return text == null ? "" : core_trim.call( text );
576+
},
651577

652578
// results is for internal usage only
653579
makeArray: function( arr, results ) {
@@ -668,25 +594,7 @@ jQuery.extend({
668594
},
669595

670596
inArray: function( elem, arr, i ) {
671-
var len;
672-
673-
if ( arr ) {
674-
if ( core_indexOf ) {
675-
return core_indexOf.call( arr, elem, i );
676-
}
677-
678-
len = arr.length;
679-
i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;
680-
681-
for ( ; i < len; i++ ) {
682-
// Skip accessing in sparse arrays
683-
if ( i in arr && arr[ i ] === elem ) {
684-
return i;
685-
}
686-
}
687-
}
688-
689-
return -1;
597+
return arr == null ? -1 : core_indexOf.call( arr, elem, i );
690598
},
691599

692600
merge: function( first, second ) {
@@ -846,9 +754,7 @@ jQuery.extend({
846754
length ? fn( elems[0], key ) : emptyGet;
847755
},
848756

849-
now: function() {
850-
return ( new Date() ).getTime();
851-
}
757+
now: Date.now
852758
});
853759

854760
jQuery.ready.promise = function( obj ) {
@@ -864,46 +770,12 @@ jQuery.ready.promise = function( obj ) {
864770
setTimeout( jQuery.ready );
865771

866772
// Standards-based browsers support DOMContentLoaded
867-
} else if ( document.addEventListener ) {
773+
} else {
868774
// Use the handy event callback
869775
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
870776

871777
// A fallback to window.onload, that will always work
872778
window.addEventListener( "load", jQuery.ready, false );
873-
874-
// If IE event model is used
875-
} else {
876-
// Ensure firing before onload, maybe late but safe also for iframes
877-
document.attachEvent( "onreadystatechange", DOMContentLoaded );
878-
879-
// A fallback to window.onload, that will always work
880-
window.attachEvent( "onload", jQuery.ready );
881-
882-
// If IE and not a frame
883-
// continually check to see if the document is ready
884-
var top = false;
885-
886-
try {
887-
top = window.frameElement == null && document.documentElement;
888-
} catch(e) {}
889-
890-
if ( top && top.doScroll ) {
891-
(function doScrollCheck() {
892-
if ( !jQuery.isReady ) {
893-
894-
try {
895-
// Use the trick by Diego Perini
896-
// http://javascript.nwbox.com/IEContentLoaded/
897-
top.doScroll("left");
898-
} catch(e) {
899-
return setTimeout( doScrollCheck, 50 );
900-
}
901-
902-
// and execute any waiting functions
903-
jQuery.ready();
904-
}
905-
})();
906-
}
907779
}
908780
}
909781
return readyList.promise( obj );

0 commit comments

Comments
 (0)