From f240a42ab501fe37b52f92957c144536a9faf09d Mon Sep 17 00:00:00 2001 From: Benoit Socias Date: Tue, 24 Aug 2021 09:25:59 +0000 Subject: [PATCH] [FIX] web: avoid freezing the browser when scrolling to undefined Before this commit the browser did freeze if scrollTo(undefined) was called. This happened because our "closestScrollable"'s jQuery addition entered an infinite recursive exploration of .parent() elements starting from $(undefined). After this commit such wrong calls to scrollTo() will not freeze the browser anymore but crash instead. The closestScrollable function was also made more robust and return an empty element instead of looping. task-2630257 X-original-commit: d1cda0f3ebb942ad58273db36692f28fa8a3b197 Co-authored-by: qsm-odoo --- addons/web/static/src/legacy/js/core/dom.js | 7 +++++-- addons/web/static/src/legacy/js/libs/jquery.js | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/legacy/js/core/dom.js b/addons/web/static/src/legacy/js/core/dom.js index 063ee3742b8..9412fdc1cde 100644 --- a/addons/web/static/src/legacy/js/core/dom.js +++ b/addons/web/static/src/legacy/js/core/dom.js @@ -157,10 +157,10 @@ var dom = { } }, /** - * @return {HTMLElement} + * @return {HTMLElement|null} */ closestScrollable(el) { - return $(el).closestScrollable()[0]; + return $(el).closestScrollable()[0] || null; }, /** * @param {HTMLElement} el @@ -549,6 +549,9 @@ var dom = { * @return {Promise} */ scrollTo(el, options = {}) { + if (!el) { + throw new Error("The scrollTo function was called without any given element"); + } const $el = $(el); const $scrollable = options.$scrollable || $el.parent().closestScrollable(); // If $scrollable and $el are not in the same document, we can safely diff --git a/addons/web/static/src/legacy/js/libs/jquery.js b/addons/web/static/src/legacy/js/libs/jquery.js index c4c627cb306..7fc19bf1d94 100644 --- a/addons/web/static/src/legacy/js/libs/jquery.js +++ b/addons/web/static/src/legacy/js/libs/jquery.js @@ -128,6 +128,10 @@ $.fn.extend({ closestScrollable() { let $el = this; while ($el[0] !== document.scrollingElement) { + // Avoid infinite loop. + if (!$el.length) { + return $(); + } if ($el.isScrollable()) { return $el; }