From 3a98c4ab27d6a5c2934a90804a5e2bdcdeea6d91 Mon Sep 17 00:00:00 2001 From: Oleg Gaidarenko Date: Fri, 13 Nov 2015 14:59:25 +0300 Subject: [PATCH] Revert "Offset: Fix .offset() to correctly work with ShadowDOM" This reverts commit 1617479fcf7cbdaf33dc9334ed10a0f30bf14687. --- src/offset.js | 40 ++++++++++++++++------------------------ test/unit/offset.js | 6 +++--- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/offset.js b/src/offset.js index 857b3a5cc..acc41997a 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" ) && @@ -77,41 +77,33 @@ jQuery.fn.extend( { if ( arguments.length ) { return options === undefined ? this : - this.each( function( i ) { + this.each(function( i ) { jQuery.offset.setOffset( this, options, i ); - } ); + }); } - var docElem, win, rect, + var docElem, win, elem = this[ 0 ], + box = { top: 0, left: 0 }, doc = elem && elem.ownerDocument; - if ( !elem ) { + if ( !doc ) { return; } - // Support: IE<=11+ - // Running getBoundingClientRect on a - // disconnected node in IE throws an error - if ( !elem.getClientRects().length ) { - return { top: 0, left: 0 }; - } - - rect = elem.getBoundingClientRect(); - - // Make sure element is not hidden (display: none) or disconnected - if ( rect.width || rect.height || elem.getClientRects().length ) { - win = getWindow( doc ); - docElem = doc.documentElement; + docElem = doc.documentElement; - return { - top: rect.top + win.pageYOffset - docElem.clientTop, - left: rect.left + win.pageXOffset - docElem.clientLeft - }; + // Make sure it's not a disconnected DOM node + if ( !jQuery.contains( docElem, elem ) ) { + return box; } - // Return zeros for disconnected and hidden elements (gh-2310) - return rect; + box = elem.getBoundingClientRect(); + win = getWindow( doc ); + return { + top: box.top + win.pageYOffset - docElem.clientTop, + left: box.left + win.pageXOffset - docElem.clientLeft + }; }, position: function() { diff --git a/test/unit/offset.js b/test/unit/offset.js index a4822e4a9..ea2c26606 100644 --- a/test/unit/offset.js +++ b/test/unit/offset.js @@ -57,7 +57,7 @@ QUnit.test( "object without getBoundingClientRect", function( assert ) { assert.equal( result.left, 0, "Check left" ); }); -QUnit.test( "disconnected element", function( assert ) { +QUnit.test( "disconnected node", function( assert ) { assert.expect( 2 ); var result = jQuery( document.createElement( "div" ) ).offset(); @@ -65,8 +65,8 @@ QUnit.test( "disconnected element", function( assert ) { // These tests are solely for master/compat consistency // Retrieving offset on disconnected/hidden elements is not officially // valid input, but will return zeros for back-compat - assert.equal( result.top, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" ); - assert.equal( result.left, 0, "Retrieving offset on disconnected elements returns zeros (gh-2310)" ); + equal( result.top, 0, "Check top" ); + equal( result.left, 0, "Check left" ); } ); QUnit.test( "hidden (display: none) element", function( assert ) { -- 2.39.5