From 20e157dd01368858a20c7535a8fecb7bb9fe0d10 Mon Sep 17 00:00:00 2001 From: "Francois (fge)" Date: Tue, 6 Jul 2021 09:32:19 +0000 Subject: [PATCH] [FIX] web: doesn't apply the invisible data-hotkey The purpose of this commit is to prevent the activation of data-hotkeys associated with hidden elements of the dom. Task-2591069 - BUG 1 --- .../static/src/core/hotkeys/hotkey_service.js | 3 +- .../core/hotkeys/hotkey_service_tests.js | 29 +++++++++++++++++++ .../commands/command_service_tests.js | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/core/hotkeys/hotkey_service.js b/addons/web/static/src/core/hotkeys/hotkey_service.js index 3a8ee6cd363..158c139976d 100644 --- a/addons/web/static/src/core/hotkeys/hotkey_service.js +++ b/addons/web/static/src/core/hotkeys/hotkey_service.js @@ -125,10 +125,9 @@ export const hotkeyService = { reg.callback(); dispatched = true; } - if (!event.repeat && isAlted) { // Click on all elements having a data-hotkey attribute matching the actual hotkey. - const elems = activeElement.querySelectorAll(`[data-hotkey='${hotkey}' i]`); + const elems = ui.getVisibleElements(`[data-hotkey='${hotkey}' i]`); for (const el of elems) { // AAB: not sure it is enough, we might need to trigger all events that occur when you actually click el.focus(); diff --git a/addons/web/static/tests/core/hotkeys/hotkey_service_tests.js b/addons/web/static/tests/core/hotkeys/hotkey_service_tests.js index afb98277ac1..9cf113fbf85 100644 --- a/addons/web/static/tests/core/hotkeys/hotkey_service_tests.js +++ b/addons/web/static/tests/core/hotkeys/hotkey_service_tests.js @@ -78,6 +78,35 @@ QUnit.test("data-hotkey", async (assert) => { comp.destroy(); }); +QUnit.test("invisible data-hotkeys are not enabled. ", async (assert) => { + assert.expect(3); + + class MyComponent extends Component { + onClick() { + assert.step("click"); + } + } + MyComponent.template = xml` +
+
+ `; + + const key = "b"; + const comp = await mount(MyComponent, { env, target }); + + triggerHotkey(key); + await nextTick(); + assert.verifySteps(["click"]) + + comp.el.querySelector(".myButton").disabled = true; + triggerHotkey(key); + await nextTick(); + assert.verifySteps([], "shouldn't trigger the hotkey of an invisible button"); + + comp.destroy(); +}); + QUnit.test("hook", async (assert) => { const key = "q"; class TestComponent extends Component { diff --git a/addons/web/static/tests/webclient/commands/command_service_tests.js b/addons/web/static/tests/webclient/commands/command_service_tests.js index 462173ce69e..420b7ed18ee 100644 --- a/addons/web/static/tests/webclient/commands/command_service_tests.js +++ b/addons/web/static/tests/webclient/commands/command_service_tests.js @@ -141,6 +141,7 @@ QUnit.test("data-hotkey added to command palette", async (assert) => {
`;