Skip to content

Truly independent web browser

2.5 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script src="../../work-test.js"></script>
<script>
    function animationFrame() {
        return new Promise(resolve => requestAnimationFrame(resolve));
    }

    const childDocumentSource = `
        <!DOCTYPE html>
        <style>
            #width { width: 10vw; }
            #height { height: 10vh; }
            #dynamicHeight { height: 10dvh; }
            #inline { width: 10vi; }
            #smaller { width: 10vmin; }
            #calcWidth { width: calc(10px + 10svw); }
            #plain { width: 10px; }
        </style>
        <div id="width"></div>
        <div id="height"></div>
        <div id="dynamicHeight"></div>
        <div id="inline"></div>
        <div id="smaller"></div>
        <div id="calcWidth"></div>
        <div id="plain"></div>
    `;

    promiseTest(async () => {
        const iframe = document.createElement("iframe");
        iframe.style.border = "0";
        iframe.style.width = "300px";
        iframe.style.height = "200px";

        const loaded = new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
        iframe.srcdoc = childDocumentSource;
        document.body.appendChild(iframe);
        await loaded;

        const child = iframe.contentWindow;
        const ids = ["width", "height", "dynamicHeight", "inline", "smaller", "calcWidth", "plain"];
        const resize = async (label, property, value) => {
            child.internals.updateStyle();
            child.document.body.offsetWidth;
            child.internals.beginStyleTrace();
            iframe.style[property] = value;
            document.body.offsetWidth;
            await animationFrame();
            child.document.body.offsetWidth;
            child.internals.updateStyle();
            const restyled = [...new Set(parseStyleTrace(child.internals.takeStyleTrace(), { stylePassesOnly: true }).filter(event => event.event === "restyle" && event.outcome !== "kept").map(event => shortStyleName(event.element)))];
            const sizes = ids.map(id => {
                const property = id.toLowerCase().includes("height") ? "height" : "width";
                return `${id}=${child.getComputedStyle(child.document.getElementById(id))[property]}`;
            });
            println(`${label}: ${sizes.join(" ")}`);
            println(`${label} restyled: [${restyled.join(", ")}]`);
        };

        await resize("width only", "width", "500px");
        await resize("height only", "height", "400px");
    });
</script>