[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 <qsm@odoo.com>
This commit is contained in:
co-authored by
qsm-odoo
parent
1285726ff9
commit
f240a42ab5
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user