From 3295c6e4c75f3ac15895b87de84fa84e101207e6 Mon Sep 17 00:00:00 2001 From: Pierre Paridans Date: Mon, 23 Oct 2023 10:48:49 +0200 Subject: [PATCH] [REF] web: simplify iframe retrieval in position hook This commit reworks and simplifies the `getIFrame()` method in the following way : instead of trying to find/guess the targetted element ancestor's document and then looking in this parent for the iframe containing the targetted element, it now uses the popperEl as parent for the iframes who may contain the targetted element. In practice, the only caveats is that it won't support multiple nested iframes (which we shouldn't use anyway in our codebase). Part-of: odoo/odoo#138847 --- addons/web/static/src/core/position_hook.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/addons/web/static/src/core/position_hook.js b/addons/web/static/src/core/position_hook.js index f08abfd850b..9c716157be3 100644 --- a/addons/web/static/src/core/position_hook.js +++ b/addons/web/static/src/core/position_hook.js @@ -86,16 +86,13 @@ const DEFAULTS = { }; /** - * @param {HTMLElement} el + * @param {HTMLElement} popperEl + * @param {HTMLElement} targetEl * @returns {HTMLIFrameElement?} */ -function getIFrame(el) { - const parentDocument = el.ownerDocument.defaultView.parent?.document; - if (!parentDocument || parentDocument === el.ownerDocument) { - return; - } - return [...parentDocument.getElementsByTagName("iframe")].find((iframe) => - iframe.contentDocument.contains(el) +function getIFrame(popperEl, targetEl) { + return [...popperEl.ownerDocument.getElementsByTagName("iframe")].find((iframe) => + iframe.contentDocument?.contains(targetEl) ); } @@ -319,7 +316,7 @@ export function usePosition(refName, getTarget, options = {}) { } // Prepare - const iframe = getIFrame(targetEl); + const iframe = getIFrame(ref.el, targetEl); reposition(ref.el, targetEl, { ...DEFAULTS, ...options }, iframe); };