Unify the means of detecting a window across the library. Fixes jQuery UI bug #5438 and jQuery bugs #6575 and 6088.

This commit is contained in:
jeresig 2010-09-22 16:41:51 -04:00
parent ab454d9526
commit c8dd49f756
4 changed files with 11 additions and 6 deletions

View file

@ -474,6 +474,11 @@ jQuery.extend({
return jQuery.type(obj) === "array"; return jQuery.type(obj) === "array";
}, },
// A crude way of determining if an object is a window
isWindow: function( obj ) {
return "setInterval" in obj;
},
type: function( obj ) { type: function( obj ) {
return obj == null ? return obj == null ?
String( obj ) : String( obj ) :
@ -484,7 +489,7 @@ jQuery.extend({
// Must be an Object. // Must be an Object.
// Because of IE, we also have to check the presence of the constructor property. // 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 // Make sure that DOM nodes and window objects don't pass through, as well
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || obj.setInterval ) { if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false; return false;
} }
@ -632,7 +637,7 @@ jQuery.extend({
// Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
var type = jQuery.type(array); var type = jQuery.type(array);
if ( array.length == null || type === "string" || type === "function" || type === "regexp" || "setInterval" in array ) { if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
push.call( ret, array ); push.call( ret, array );
} else { } else {
jQuery.merge( ret, array ); jQuery.merge( ret, array );

View file

@ -33,7 +33,7 @@ jQuery.each([ "Height", "Width" ], function( i, name ) {
}); });
} }
return ("scrollTo" in elem && elem.document) ? // does it walk and quack like a window? return jQuery.isWindow( elem ) ?
// Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] || elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
elem.document.body[ "client" + name ] : elem.document.body[ "client" + name ] :

View file

@ -25,7 +25,7 @@ jQuery.event = {
// For whatever reason, IE has trouble passing the window object // For whatever reason, IE has trouble passing the window object
// around, causing it to be cloned in the process // around, causing it to be cloned in the process
if ( elem.setInterval && ( elem !== window && !elem.frameElement ) ) { if ( jQuery.isWindow( elem ) && ( elem !== window && !elem.frameElement ) ) {
elem = window; elem = window;
} }
@ -515,7 +515,7 @@ jQuery.event = {
beforeunload: { beforeunload: {
setup: function( data, namespaces, eventHandle ) { setup: function( data, namespaces, eventHandle ) {
// We only want to do this special case on windows // We only want to do this special case on windows
if ( this.setInterval ) { if ( jQuery.isWindow( this ) ) {
this.onbeforeunload = eventHandle; this.onbeforeunload = eventHandle;
} }
}, },

View file

@ -280,7 +280,7 @@ jQuery.each( ["Left", "Top"], function( i, name ) {
}); });
function getWindow( elem ) { function getWindow( elem ) {
return ("scrollTo" in elem && elem.document) ? return jQuery.isWindow( elem ) ?
elem : elem :
elem.nodeType === 9 ? elem.nodeType === 9 ?
elem.defaultView || elem.parentWindow : elem.defaultView || elem.parentWindow :