xref: /plugin/combo/resources/snippet/js/webcode.js (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1// Webcode
2// Will set the height of an iframe to his content
3// If the attribute is not set
4window.addEventListener("load", function () {
5
6    // Select the iframe element with the class webCode
7    const webCodeIFrames = document.querySelectorAll("iframe.webcode-combo");
8
9    // Set the height of the iframe to be the height of the internal iframe
10    if (webCodeIFrames != null) {
11        for (let i = 0; i < webCodeIFrames.length; i++) {
12            const webCodeIFrame = webCodeIFrames[i];
13            const height = webCodeIFrame.getAttribute('height');
14            if (height == null) {
15                let htmlIFrameElement = webCodeIFrame.contentWindow.document.querySelector("html");
16                let calculatedHeight = htmlIFrameElement.offsetHeight;
17                let defaultHtmlElementHeight = 150;
18                if (calculatedHeight === defaultHtmlElementHeight) {
19                    // body and not html because html has a default minimal height of 150
20                    calculatedHeight = webCodeIFrame.contentWindow.document.querySelector("body").offsetHeight;
21                    // After setting the height, there is a recalculation and the padding of a descendant phrasing content element
22                    // may ends up in the html element. The below code corrects that
23                    requestAnimationFrame(function() {
24                        if (calculatedHeight !== htmlIFrameElement.offsetHeight) {
25                            webCodeIFrame.height = htmlIFrameElement.offsetHeight;
26                        }
27                    });
28                }
29                webCodeIFrame.height = calculatedHeight;
30            }
31        }
32    }
33
34
35});
36