Skip to content

Truly independent web browser

2.0 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    promiseTest(async () => {
        const iframe = document.createElement("iframe");
        iframe.style.width = "300px";
        iframe.srcdoc = `
            <!DOCTYPE html>
            <style>
                .item { color: black; }
                /* The custom property means nothing lists whom the rule wins for, so the selector is tested. */
                @media (max-width: 400px) {
                    .item:nth-child(2n) { --even: 1; color: green; }
                }
            </style>
            <div id="list"><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div></div>
            <div id="more"></div>
        `;
        const loaded = new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
        document.body.append(iframe);
        await loaded;

        const contentWindow = iframe.contentWindow;
        const doc = iframe.contentDocument;
        const colors = () => [...doc.querySelectorAll(".item")].map(item => contentWindow.getComputedStyle(item).color).join(", ");
        println(`Before: ${colors()}`);

        // A page reacting to the resize changes much of its tree, so the style update that takes the
        // rule away is too wide to capture the old sibling order the even items were matched in.
        const query = contentWindow.matchMedia("(max-width: 400px)");
        const changed = new Promise(resolve => {
            query.addEventListener(
                "change",
                () => {
                    doc.getElementById("list").prepend(doc.createElement("span"));
                    const more = doc.getElementById("more");
                    for (let i = 0; i < 100; ++i)
                        more.append(doc.createElement("div"));
                    resolve();
                },
                { once: true }
            );
        });
        iframe.style.width = "500px";
        await changed;
        await animationFrame();
        println(`After: ${colors()}`);
    });
</script>