From 2c5467b38cfac17aa87ff4ac09370e61d8bf4053 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Wed, 18 Sep 2019 21:33:13 -0400 Subject: [PATCH] Fixes error when getting offset of element not in document This fixes an error which is called out in jQuery Migrate but probably never happens in real life. This is because we call `jQuery.fn.offset` without checking if the element is in the document. Based on testing done here and within the MediaWiki team, I'm pretty sure jQuery never actually implemented explicit checks and this jQuery Migrate warning is just to cover the case where a browser might start returning inconsistnet results. And we could at least reproduce the inconsistency, so that's something. We now default the offset to 0/0 if the parent element happens to not be in the document. This appears to be what jQuery used to do in the past, and generally appears to be what people expect in these cases. This fixes #5584. --- src/js/select2/dropdown/attachBody.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/js/select2/dropdown/attachBody.js b/src/js/select2/dropdown/attachBody.js index 7380d9c6..1ef2373c 100644 --- a/src/js/select2/dropdown/attachBody.js +++ b/src/js/select2/dropdown/attachBody.js @@ -190,7 +190,14 @@ define([ $offsetParent = $offsetParent.offsetParent(); } - var parentOffset = $offsetParent.offset(); + var parentOffset = { + top: 0, + left: 0 + }; + + if ($.contains(document.body, $offsetParent[0])) { + parentOffset = $offsetParent.offset(); + } css.top -= parentOffset.top; css.left -= parentOffset.left;