From edb2286544270dc53550180e06668e61c231fb5d Mon Sep 17 00:00:00 2001 From: timmywil Date: Wed, 25 May 2011 19:49:50 -0400 Subject: [PATCH] Return null for outer/inner width/height calls on window/document. Fixes #7557. --- src/css.js | 6 +++++- src/dimensions.js | 10 ++++++---- test/unit/dimensions.js | 30 ++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/css.js b/src/css.js index 46f6bf31..10d36b76 100644 --- a/src/css.js +++ b/src/css.js @@ -170,6 +170,11 @@ jQuery.each(["height", "width"], function( i, name ) { get: function( elem, computed, extra ) { var val; + // Tests for window/document + if ( !elem.style ) { + return null; + } + if ( computed ) { if ( elem.offsetWidth !== 0 ) { val = getWH( elem, name, extra ); @@ -196,7 +201,6 @@ jQuery.each(["height", "width"], function( i, name ) { if ( val < 0 || val == null ) { val = elem.style[ name ]; - // Should return "auto" instead of 0, use 0 for // temporary backwards-compat return val === "" || val === "auto" ? "0px" : val; diff --git a/src/dimensions.js b/src/dimensions.js index e2d411dd..1ab92d1d 100644 --- a/src/dimensions.js +++ b/src/dimensions.js @@ -7,15 +7,17 @@ jQuery.each([ "Height", "Width" ], function( i, name ) { // innerHeight and innerWidth jQuery.fn["inner" + name] = function() { - return this[0] ? - parseFloat( jQuery.css( this[0], type, "padding" ) ) : + var ret; + return this[0] && !isNaN( ret = parseFloat(jQuery.css( this[0], type, "padding" )) ) ? + ret : null; }; // outerHeight and outerWidth jQuery.fn["outer" + name] = function( margin ) { - return this[0] ? - parseFloat( jQuery.css( this[0], type, margin ? "margin" : "border" ) ) : + var ret; + return this[0] && !isNaN( ret = parseFloat(jQuery.css( this[0], type, margin ? "margin" : "border" )) ) ? + ret : null; }; diff --git a/test/unit/dimensions.js b/test/unit/dimensions.js index 641165f4..83cc3908 100644 --- a/test/unit/dimensions.js +++ b/test/unit/dimensions.js @@ -107,7 +107,13 @@ test("height() with function args", function() { }); test("innerWidth()", function() { - expect(4); + expect(8); + + equals(jQuery(window).innerWidth(), null, "Test on window without margin option"); + equals(jQuery(window).innerWidth(true), null, "Test on window with margin option"); + + equals(jQuery(document).innerWidth(), null, "Test on document without margin option"); + equals(jQuery(document).innerWidth(true), null, "Test on document with margin option"); var $div = jQuery("#nothiddendiv"); // set styles @@ -136,7 +142,13 @@ test("innerWidth()", function() { }); test("innerHeight()", function() { - expect(4); + expect(8); + + equals(jQuery(window).innerHeight(), null, "Test on window without margin option"); + equals(jQuery(window).innerHeight(true), null, "Test on window with margin option"); + + equals(jQuery(document).innerHeight(), null, "Test on document without margin option"); + equals(jQuery(document).innerHeight(true), null, "Test on document with margin option"); var $div = jQuery("#nothiddendiv"); // set styles @@ -165,7 +177,12 @@ test("innerHeight()", function() { }); test("outerWidth()", function() { - expect(7); + expect(11); + + equal( jQuery( window ).outerWidth(), null, "Test on window without margin option" ); + equal( jQuery( window ).outerWidth( true ), null, "Test on window with margin option" ); + equal( jQuery( document ).outerWidth(), null, "Test on document without margin option" ); + equal( jQuery( document ).outerWidth( true ), null, "Test on document with margin option" ); var $div = jQuery("#nothiddendiv"); $div.css("width", 30); @@ -195,7 +212,12 @@ test("outerWidth()", function() { }); test("outerHeight()", function() { - expect(7); + expect(11); + + equal( jQuery( window ).outerHeight(), null, "Test on window without margin option" ); + equal( jQuery( window ).outerHeight( true ), null, "Test on window with margin option" ); + equal( jQuery( document ).outerHeight(), null, "Test on document without margin option" ); + equal( jQuery( document ).outerHeight( true ), null, "Test on document with margin option" ); var $div = jQuery("#nothiddendiv"); $div.css("height", 30);