[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
This commit is contained in:
Pierre Paridans
2023-10-23 20:07:30 +00:00
parent 09592c25fd
commit 3295c6e4c7
+6 -9
View File
@@ -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);
};