Skip to content

Commit a0a5c0b

Browse files
committed
Offset: add tests for hidden elements + scroll
- Also add comments to hidden/disconnected tests noting this is to ensure consistency between branches
1 parent 3b1de11 commit a0a5c0b

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/offset.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,20 @@ jQuery.fn.extend({
108108
}
109109

110110
rect = elem.getBoundingClientRect();
111-
win = getWindow( doc );
112111

113-
return {
114-
top: rect.top + ( win.pageYOffset || docElem.scrollTop ) -
115-
( docElem.clientTop || 0 ),
116-
left: rect.left + ( win.pageXOffset || docElem.scrollLeft ) -
117-
( docElem.clientLeft || 0 )
118-
};
112+
if ( rect.width || rect.height || elem.getClientRects().length ) {
113+
win = getWindow( doc );
114+
115+
return {
116+
top: rect.top + ( win.pageYOffset || docElem.scrollTop ) -
117+
( docElem.clientTop || 0 ),
118+
left: rect.left + ( win.pageXOffset || docElem.scrollLeft ) -
119+
( docElem.clientLeft || 0 )
120+
};
121+
}
122+
123+
// Return zeros for hidden elements
124+
return rect;
119125
},
120126

121127
position: function() {

test/data/offset/scroll.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#scroll-1-1 { top: 1px; left: 1px; }
1212
#scroll-1-1-1 { top: 1px; left: 1px; }
1313
#forceScroll { width: 5000px; height: 5000px; }
14+
#hidden { display: none; }
1415
#marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
1516
</style>
1617
<script src="../../jquery.js"></script>
@@ -32,6 +33,7 @@
3233
<div id="scroll-1-1-1" class="scroll"></div>
3334
</div>
3435
</div>
36+
<div id="hidden"></div>
3537
<div id="forceScroll"></div>
3638
<div id="marker"></div>
3739
<p class="instructions">Click the white box to move the marker to it.</p>

test/unit/offset.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ test("disconnected element", function() {
5353

5454
var result = jQuery( document.createElement( "div" ) ).offset();
5555

56+
// These tests are solely for master/compat consistency
57+
// Retrieving offset on disconnected/hidden elements is not officially
58+
// valid input, but will return zeros for back-compat
5659
equal( result.top, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" );
5760
equal( result.left, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" );
5861
});
@@ -65,6 +68,9 @@ test("hidden (display: none) element", function() {
6568

6669
node.remove();
6770

71+
// These tests are solely for master/compat consistency
72+
// Retrieving offset on disconnected/hidden elements is not officially
73+
// valid input, but will return zeros for back-compat
6874
equal( result.top, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
6975
equal( result.left, 0, "Retrieving offset on hidden elements returns zeros (gh-2310)" );
7076
});
@@ -406,7 +412,7 @@ testIframe("offset/table", "table", function( $ ) {
406412
});
407413

408414
testIframe("offset/scroll", "scroll", function( $, win ) {
409-
expect(28);
415+
expect( 30 );
410416

411417
// If we're going to bastardize the tests, let's just DO it
412418
var ie = /msie 8/i.test( navigator.userAgent );
@@ -425,6 +431,12 @@ testIframe("offset/scroll", "scroll", function( $, win ) {
425431
}
426432
equal( $("#scroll-1-1").offset().left, 11, "jQuery('#scroll-1-1').offset().left" );
427433

434+
// These tests are solely for master/compat consistency
435+
// Retrieving offset on disconnected/hidden elements is not officially
436+
// valid input, but will return zeros for back-compat
437+
equal( $("#hidden").offset().top, 0, "Hidden elements do not subtract scroll" );
438+
equal( $("#hidden").offset().left, 0, "Hidden elements do not subtract scroll" );
439+
428440
// scroll offset tests .scrollTop/Left
429441
equal( $("#scroll-1").scrollTop(), 5, "jQuery('#scroll-1').scrollTop()" );
430442
equal( $("#scroll-1").scrollLeft(), 5, "jQuery('#scroll-1').scrollLeft()" );

0 commit comments

Comments
 (0)