From b95edd874ce904ef231c295790a003992db002e3 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Tue, 2 Apr 2024 16:45:10 +0200 Subject: [PATCH] [FIX] web: prevent traceback for addButtonLoadingEffect in custo Follow-up of [1] which changed code in stable in such a way it could break when the function was used in a way it was not intended to in some custom code. Note that the function could also be used by giving a string selector to it, that will not work anymore: but instead of crashing, this commit will just make it do nothing (it is only about adding a button loading effect anyway). [1]: https://github.com/odoo/odoo/commit/8bd51d060a48378652ae10c57a1949c3bb7d78de closes odoo/odoo#160253 X-original-commit: d703125766d27ff33659c38fa50c2266f5f3dabb Signed-off-by: Romain Derie (rde) Signed-off-by: Quentin Smetz (qsm) --- .../static/src/legacy/js/core/minimal_dom.js | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/addons/web/static/src/legacy/js/core/minimal_dom.js b/addons/web/static/src/legacy/js/core/minimal_dom.js index 7ceb012e027..50b58a31731 100644 --- a/addons/web/static/src/legacy/js/core/minimal_dom.js +++ b/addons/web/static/src/legacy/js/core/minimal_dom.js @@ -91,7 +91,7 @@ export function makeButtonHandler(fct) { const result = fct.apply(this, arguments); const buttonEl = ev.target && ev.target.closest && ev.target.closest(BUTTON_HANDLER_SELECTOR); - if (!buttonEl) { + if (!(buttonEl instanceof HTMLElement)) { return result; } @@ -99,14 +99,10 @@ export function makeButtonHandler(fct) { // or at least for the duration of the click debounce. This makes // a 'real' debounce creation useless. Also, during the debouncing // part, the button is disabled without any visual effect. - if (buttonEl.classList) { - buttonEl.classList.add('pe-none'); - } + buttonEl.classList.add('pe-none'); Promise.resolve(DEBOUNCE && new Promise(r => setTimeout(r, DEBOUNCE))) .then(function () { - if (buttonEl.classList) { - buttonEl.classList.remove('pe-none'); - } + buttonEl.classList.remove('pe-none'); const restore = addButtonLoadingEffect(buttonEl); return Promise.resolve(result).finally(restore); }); @@ -125,19 +121,18 @@ export function makeButtonHandler(fct) { * initial state */ export function addButtonLoadingEffect(btnEl) { + if (!(btnEl instanceof HTMLElement)) { + return () => {}; + } // Note that pe-none is used alongside "disabled" so that the behavior is // the same on links not using the "btn" class -> pointer-events disabled. - if (btnEl.classList) { - btnEl.classList.add('o_website_btn_loading', 'disabled', 'pe-none'); - } + btnEl.classList.add('o_website_btn_loading', 'disabled', 'pe-none'); btnEl.disabled = true; const loaderEl = document.createElement('span'); loaderEl.classList.add('fa', 'fa-refresh', 'fa-spin', 'me-2'); btnEl.prepend(loaderEl); return () => { - if (btnEl.classList) { - btnEl.classList.remove('o_website_btn_loading', 'disabled', 'pe-none'); - } + btnEl.classList.remove('o_website_btn_loading', 'disabled', 'pe-none'); btnEl.disabled = false; loaderEl.remove(); };