Skip to content

Truly independent web browser

4.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));
    }

    // The passes of a style trace, each as its kind and the elements it restyled.
    function summarizeStyleTrace(text) {
        const passes = [];
        for (const event of parseStyleTrace(text, { stylePassesOnly: true })) {
            if (event.event === "pass")
                passes.push({ kind: event.kind, restyled: [] });
            else if (event.outcome !== "kept")
                passes.at(-1).restyled.push(shortStyleName(event.element));
        }
        return passes.map(pass => `${pass.kind} [${pass.restyled.join(", ")}]`).join(", ") || "none";
    }

    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 = `
            <!DOCTYPE html>
            <style>
                #target {
                    color: rgb(255, 0, 0);
                }

                #canvas-target {
                    color: rgb(255, 0, 0);
                }

                #nested-parent .nested-target {
                    color: rgb(255, 0, 0);
                }

                @media (min-width: 400px) {
                    #target,
                    #canvas-target {
                        color: rgb(0, 128, 0);
                    }
                }

                #nested-parent {
                    @media (min-width: 400px) {
                        & .nested-target {
                            color: rgb(0, 0, 255);
                        }
                    }
                }

                .bystander {
                    color: rgb(0, 0, 0);
                }
            </style>
            <div id="target">target</div>
            <canvas id="canvas-target"></canvas>
            <div id="nested-parent"><span class="nested-target">nested target</span></div>
            <script>
                for (let i = 0; i < 40; ++i) {
                    const bystander = document.createElement("div");
                    bystander.className = "bystander";
                    bystander.textContent = "bystander " + i;
                    document.body.appendChild(bystander);
                }
            <\/script>
        `;
        document.body.appendChild(iframe);
        await loaded;

        const child = iframe.contentWindow;
        const target = child.document.getElementById("target");
        const canvasTarget = child.document.getElementById("canvas-target");
        const context = canvasTarget.getContext("2d");
        const nestedTarget = child.document.querySelector(".nested-target");

        child.internals.updateStyle();
        println(`initial color: ${child.getComputedStyle(target).color}`);
        context.fillStyle = "currentColor";
        println(`initial canvas fill style: ${context.fillStyle}`);
        println(`initial nested color: ${child.getComputedStyle(nestedTarget).color}`);

        child.internals.beginStyleTrace();
        iframe.style.width = "500px";
        document.body.offsetWidth;
        context.fillStyle = "currentColor";
        println(`targeted canvas fill style: ${context.fillStyle}`);

        await animationFrame();
        child.document.body.offsetWidth;
        child.internals.updateStyle();

        // A media query flip styles the elements its rules apply to.
        const resizeWork = summarizeStyleTrace(child.internals.takeStyleTrace());
        println(`resized color: ${child.getComputedStyle(target).color}`);
        println(`resized nested color: ${child.getComputedStyle(nestedTarget).color}`);
        println(`media query restyle passes: ${resizeWork}`);

        child.internals.beginStyleTrace();
        iframe.style.width = "300px";
        document.body.offsetWidth;
        await animationFrame();
        child.document.body.offsetWidth;
        child.internals.updateStyle();

        const resizeBackWork = summarizeStyleTrace(child.internals.takeStyleTrace());
        println(`resized back color: ${child.getComputedStyle(target).color}`);
        println(`resized back nested color: ${child.getComputedStyle(nestedTarget).color}`);
        println(`media query restyle passes after resize back: ${resizeBackWork}`);
    });
</script>