Skip to content

Commit

Permalink
Unify the means of detecting a window across the library. Fixes jQuer…
Browse files Browse the repository at this point in the history
…y UI bug #5438 and jQuery bugs #6575 and 6088.
  • Loading branch information
jeresig committed Sep 22, 2010
1 parent ab454d9 commit c8dd49f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/core.js
Expand Up @@ -474,6 +474,11 @@ jQuery.extend({
return jQuery.type(obj) === "array";
},

// A crude way of determining if an object is a window
isWindow: function( obj ) {
return "setInterval" in obj;

This comment has been minimized.

Copy link
@cpojer

cpojer Sep 23, 2010

return this == window;

This comment has been minimized.

Copy link
@mitsuhiko

mitsuhiko Sep 23, 2010

You are assuming there is only one window, while in fact that condition must still be true if a popup is in use.

This comment has been minimized.

Copy link
@cpojer

cpojer Sep 23, 2010

shudder

This comment has been minimized.

Copy link
@jdalton

jdalton Sep 23, 2010

Member

K so I do smth like this in fusejs...

// some browsers give window an internal [[Class]] of Window or DOMWindow
var isWindow = function(object) {
  return toString.call(object).indexOf('Window') > -1;
};

// weak fallback
if ( !isWindow(window) ) {
  isWindow = function(object) {
    return object != null && object.window == object;
  };
}

This comment has been minimized.

Copy link
@jdalton

jdalton Sep 23, 2010

Member

return "setInterval" in obj;

Will error if obj is null or undefined.

This comment has been minimized.

Copy link
@jeresig

jeresig Sep 23, 2010

Author Member

@jdalton: You missed the follow-up commit with test cases and tweaks.

This comment has been minimized.

Copy link
@jdalton

jdalton Sep 23, 2010

Member

Ah cool. Your current (revised) way is good for a fallback solution.

This comment has been minimized.

Copy link
@jeresig

jeresig Sep 23, 2010

Author Member

For other people hitting this thread those changes were made in d7a6e75.

},

type: function( obj ) {
return obj == null ?
String( obj ) :
Expand All @@ -484,7 +489,7 @@ jQuery.extend({
// 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 || obj.setInterval ) {
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}

Expand Down Expand Up @@ -632,7 +637,7 @@ jQuery.extend({
// Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
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 );
} else {
jQuery.merge( ret, array );
Expand Down
2 changes: 1 addition & 1 deletion src/dimensions.js
Expand Up @@ -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
elem.document.compatMode === "CSS1Compat" && elem.document.documentElement[ "client" + name ] ||
elem.document.body[ "client" + name ] :
Expand Down
4 changes: 2 additions & 2 deletions src/event.js
Expand Up @@ -25,7 +25,7 @@ jQuery.event = {

// For whatever reason, IE has trouble passing the window object
// 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;
}

Expand Down Expand Up @@ -515,7 +515,7 @@ jQuery.event = {
beforeunload: {
setup: function( data, namespaces, eventHandle ) {
// We only want to do this special case on windows
if ( this.setInterval ) {
if ( jQuery.isWindow( this ) ) {
this.onbeforeunload = eventHandle;
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/offset.js
Expand Up @@ -280,7 +280,7 @@ jQuery.each( ["Left", "Top"], function( i, name ) {
});

function getWindow( elem ) {
return ("scrollTo" in elem && elem.document) ?
return jQuery.isWindow( elem ) ?
elem :
elem.nodeType === 9 ?
elem.defaultView || elem.parentWindow :
Expand Down

0 comments on commit c8dd49f

Please sign in to comment.