From: Timmy Willison Date: Mon, 15 Jun 2015 15:02:08 +0000 (-0400) Subject: Offset: return zeros for disconnected/hidden elements X-Git-Tag: 3.0.0-alpha1~21 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=40dcc767640c41a4387a343f1ef53ac57ed631c5;p=jquery.git Offset: return zeros for disconnected/hidden elements Fixes gh-2310 Close gh-2396 --- diff --git a/src/offset.js b/src/offset.js index a6aa2f1d5..fcb7b14de 100644 --- a/src/offset.js +++ b/src/offset.js @@ -32,7 +32,7 @@ jQuery.offset = { elem.style.position = "relative"; } - curOffset = curElem.offset() || { top: 0, left: 0 }; + curOffset = curElem.offset(); curCSSTop = jQuery.css( elem, "top" ); curCSSLeft = jQuery.css( elem, "left" ); calculatePosition = ( position === "absolute" || position === "fixed" ) && @@ -103,6 +103,9 @@ jQuery.fn.extend({ left: rect.left + win.pageXOffset - docElem.clientLeft }; } + + // Return zeros for disconnected and hidden elements (gh-2310) + return rect; }, position: function() { diff --git a/test/unit/offset.js b/test/unit/offset.js index 7b2ecc905..bffe87dd1 100644 --- a/test/unit/offset.js +++ b/test/unit/offset.js @@ -48,30 +48,24 @@ test("empty set", function() { }); test("disconnected element", function() { - expect(1); - - var result; + expect( 2 ); - try { - result = jQuery( document.createElement("div") ).offset(); - } catch ( e ) {} + var result = jQuery( document.createElement( "div" ) ).offset(); - ok( !result, "no position for disconnected element" ); + equal( result.top, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" ); + equal( result.left, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" ); }); test("hidden (display: none) element", function() { - expect(1); - - var result, - node = jQuery("
").appendTo("#qunit-fixture"); + expect( 2 ); - try { + var node = jQuery("
").appendTo("#qunit-fixture"), result = node.offset(); - } catch ( e ) {} node.remove(); - ok( !result, "no position for hidden (display: none) element" ); + equal( result.top, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" ); + equal( result.left, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" ); }); testIframe("offset/absolute", "absolute", function($, iframe) {