Skip to content

Commit d0df2c6

Browse files
committed
Offset: Fix .offset() to correctly work with ShadowDOM
* Fixes upon comments in pull request * Add test for hidden (display: none) elements
1 parent ebf43fa commit d0df2c6

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/offset.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jQuery.offset = {
3232
elem.style.position = "relative";
3333
}
3434

35-
curOffset = curElem.offset();
35+
curOffset = curElem.offset() || { top: 0, left: 0 };
3636
curCSSTop = jQuery.css( elem, "top" );
3737
curCSSLeft = jQuery.css( elem, "left" );
3838
calculatePosition = ( position === "absolute" || position === "fixed" ) &&
@@ -57,10 +57,10 @@ jQuery.offset = {
5757
}
5858

5959
if ( options.top != null ) {
60-
props.top = ( options.top - ( curOffset ? curOffset.top : 0 ) ) + curTop;
60+
props.top = ( options.top - curOffset.top ) + curTop;
6161
}
6262
if ( options.left != null ) {
63-
props.left = ( options.left - ( curOffset ? curOffset.left : 0 ) ) + curLeft;
63+
props.left = ( options.left - curOffset.left ) + curLeft;
6464
}
6565

6666
if ( "using" in options ) {
@@ -93,18 +93,15 @@ jQuery.fn.extend({
9393
rect = elem.getBoundingClientRect();
9494

9595
// Make sure element is not hidden (display: none) or disconnected
96-
if ( !rect.width && !rect.height && !rect.left &&
97-
!rect.top && !rect.right && !rect.bottom ) {
98-
return;
96+
if ( rect.width || rect.height || elem.getClientRects().length ) {
97+
win = getWindow( doc );
98+
docElem = doc.documentElement;
99+
100+
return {
101+
top: rect.top + win.pageYOffset - docElem.clientTop,
102+
left: rect.left + win.pageXOffset - docElem.clientLeft
103+
};
99104
}
100-
101-
win = getWindow( doc );
102-
docElem = doc.documentElement;
103-
104-
return {
105-
top: rect.top + win.pageYOffset - docElem.clientTop,
106-
left: rect.left + win.pageXOffset - docElem.clientLeft
107-
};
108105
},
109106

110107
position: function() {

test/unit/offset.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,31 @@ test("object without getBoundingClientRect", function() {
5656
equal( result.left, 0, "Check left" );
5757
});
5858

59-
test("disconnected node", function() {
59+
test("disconnected element", function() {
6060
expect(1);
6161

62-
var result = jQuery( document.createElement("div") ).offset();
62+
var result;
6363

64-
equal( typeof result, "undefined", "Check result typeof" );
64+
try {
65+
result = jQuery( document.createElement("div") ).offset();
66+
} catch ( e ) {}
67+
68+
ok( !result, "no position for disconnected element" );
69+
});
70+
71+
test("hidden (display: none) element", function() {
72+
expect(1);
73+
74+
var result,
75+
node = jQuery("<div style='display: none' />").appendTo(document.body);
76+
77+
try {
78+
result = node.offset();
79+
} catch ( e ) {}
80+
81+
node.remove();
82+
83+
ok( !result, "no position for hidden (display: none) element" );
6584
});
6685

6786
testIframe("offset/absolute", "absolute", function($, iframe) {

0 commit comments

Comments
 (0)