]> source.dussan.org Git - jquery.git/commitdiff
Offset: Fix .offset() to correctly work with ShadowDOM
authorArthur Stolyar <nekr.fabula@gmail.com>
Tue, 5 May 2015 15:16:29 +0000 (08:16 -0700)
committerTimmy Willison <timmywillisn@gmail.com>
Tue, 5 May 2015 15:16:29 +0000 (08:16 -0700)
Fixes gh-1784
Close gh-2043

src/offset.js
test/unit/offset.js

index a0d3ab5fb5b27128873a5481cff8891cd6bb41a0..49ff26ac9dcde45b6f1e3f3084af5a328107c7eb 100644 (file)
@@ -32,7 +32,7 @@ jQuery.offset = {
                        elem.style.position = "relative";
                }
 
-               curOffset = curElem.offset();
+               curOffset = curElem.offset() || { top: 0, left: 0 };
                curCSSTop = jQuery.css( elem, "top" );
                curCSSLeft = jQuery.css( elem, "left" );
                calculatePosition = ( position === "absolute" || position === "fixed" ) &&
@@ -82,28 +82,26 @@ jQuery.fn.extend({
                                });
                }
 
-               var docElem, win,
+               var docElem, win, rect,
                        elem = this[ 0 ],
-                       box = { top: 0, left: 0 },
                        doc = elem && elem.ownerDocument;
 
                if ( !doc ) {
                        return;
                }
 
-               docElem = doc.documentElement;
+               rect = elem.getBoundingClientRect();
 
-               // Make sure it's not a disconnected DOM node
-               if ( !jQuery.contains( docElem, elem ) ) {
-                       return box;
-               }
+               // Make sure element is not hidden (display: none) or disconnected
+               if ( rect.width || rect.height || elem.getClientRects().length ) {
+                       win = getWindow( doc );
+                       docElem = doc.documentElement;
 
-               box = elem.getBoundingClientRect();
-               win = getWindow( doc );
-               return {
-                       top: box.top + win.pageYOffset - docElem.clientTop,
-                       left: box.left + win.pageXOffset - docElem.clientLeft
-               };
+                       return {
+                               top: rect.top + win.pageYOffset - docElem.clientTop,
+                               left: rect.left + win.pageXOffset - docElem.clientLeft
+                       };
+               }
        },
 
        position: function() {
index 785b2396147a4186568a0b9b176aaa17b3f2833d..057aa6df7badc3baa041a73c7f79cdc866792fb4 100644 (file)
@@ -56,13 +56,31 @@ test("object without getBoundingClientRect", function() {
        equal( result.left, 0, "Check left" );
 });
 
-test("disconnected node", function() {
-       expect(2);
+test("disconnected element", function() {
+       expect(1);
 
-       var result = jQuery( document.createElement("div") ).offset();
+       var result;
 
-       equal( result.top, 0, "Check top" );
-       equal( result.left, 0, "Check left" );
+       try {
+               result = jQuery( document.createElement("div") ).offset();
+       } catch ( e ) {}
+
+       ok( !result, "no position for disconnected element" );
+});
+
+test("hidden (display: none) element", function() {
+       expect(1);
+
+       var result,
+               node = jQuery("<div style='display: none' />").appendTo("#qunit-fixture");
+
+       try {
+               result = node.offset();
+       } catch ( e ) {}
+
+       node.remove();
+
+       ok( !result, "no position for hidden (display: none) element" );
 });
 
 testIframe("offset/absolute", "absolute", function($, iframe) {