Skip to content

Truly independent web browser

2.7 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script src="frame-in-flight.js"></script>
<iframe id="frame" style="width: 200px; height: 100px; border: none" srcdoc="<style>#inner { height: 20px } @media (min-width: 250px) { #inner { height: 40px } }</style><body style='margin: 0'><div id=inner>child</div>"></iframe>
<script>
    // A container resizes its iframe in an animation frame callback. The child's viewport, media queries and box follow
    // the new size in the same rendering update, which delivers the child's resize observation before any task of the
    // child after it. The observation shows whether the update delivered it before the task a held frame lets run
    // beside it, which it does not where the update flew.
    asyncTest(async done => {
        const frameElement = document.getElementById("frame");
        if (!frameElement.contentDocument?.getElementById("inner"))
            await new Promise(resolve => frameElement.addEventListener("load", resolve, { once: true }));
        const child = frameElement.contentDocument;
        const inner = child.getElementById("inner");
        const observed = [];
        const observer = new child.defaultView.ResizeObserver(entries => {
            for (const entry of entries)
                observed.push(`${entry.contentRect.width}x${entry.contentRect.height}`);
        });
        observer.observe(inner);

        const result = await whileRecordingInFlight(() => {
            observed.length = 0;
            frameElement.style.width = "300px";
        }, state => ({ state, observed: observed.join(", ") || "nothing" }));
        println(`state in the next task: ${result.state}`);
        println(`child resize observed before the next task: ${result.observed}`);
        await twoFrames();
        println(`child resize observed: ${observed.join(", ")}`);
        println(`child viewport width: ${child.defaultView.innerWidth}`);
        println(`child media query matches: ${child.defaultView.matchMedia("(min-width: 250px)").matches}`);
        println(`child box: ${inner.offsetWidth}x${inner.offsetHeight}`);

        // Without a hold, a task of the container queued by the callback that resized the iframe may run beside the
        // child's frame, which holds back only the child's tasks, and the child's observation comes once it lands.
        observed.length = 0;
        let timeoutRan = false;
        requestAnimationFrame(() => {
            frameElement.style.width = "220px";
            setTimeout(() => timeoutRan = true, 0);
        });
        await twoFrames();
        println(`observed without a hold: ${observed.join(", ")}, timeout ran: ${timeoutRan}`);
        observer.disconnect();
        done();
    });
</script>