Skip to content

Commit

Permalink
Offset: return zeros for disconnected/hidden elements
Browse files Browse the repository at this point in the history
Fixes gh-2310
Close gh-2396
  • Loading branch information
timmywil committed Jun 16, 2015
1 parent ee69e9c commit 63f19a9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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" ) &&
Expand Down Expand Up @@ -110,6 +110,9 @@ jQuery.fn.extend({
( docElem.clientLeft || 0 )
};
}

// Return zeros for disconnected and hidden elements (gh-2310)
return rect;
},

position: function() {
Expand Down
22 changes: 8 additions & 14 deletions test/unit/offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,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("<div style='display: none' />").appendTo("#qunit-fixture");
expect( 2 );

try {
var node = jQuery("<div style='display: none' />").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) {
Expand Down

0 comments on commit 63f19a9

Please sign in to comment.