Skip to content

Truly independent web browser

3.8 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script src="../work-test.js"></script>
<style>
    .red {
        color: rgb(255, 0, 0);
    }
    .highlight span {
        background-color: rgb(255, 255, 0);
    }
    .detached-red .detached-target {
        color: rgb(255, 0, 0);
    }
    .detached-blue .detached-target {
        color: rgb(0, 0, 255);
    }
    #blue {
        color: rgb(0, 0, 255);
    }
    [data-green] {
        color: rgb(0, 128, 0);
    }
    my-element:defined {
        color: rgb(128, 0, 128);
    }
    div:has(> .flag) {
        outline-color: rgb(1, 2, 3);
    }
</style>
<div id="host"></div>
<script>
    customElements.define("my-element", class extends HTMLElement {});

    test(() => {
        const host = document.getElementById("host");

        // Mutating disconnected elements should not restyle anything.
        const tree = document.createElement("div");
        const custom = document.createElement("my-element");
        const work = styleWork(() => {
            for (let i = 0; i < 10; ++i) {
                const child = document.createElement("span");
                child.className = "red";
                child.setAttribute("data-green", "");
                child.removeAttribute("data-green");
                tree.appendChild(child);
            }
            tree.firstChild.id = "blue";
            tree.firstChild.className = "";
            tree.appendChild(custom);

            // Adding these classes changes no style inputs, because the disconnected tree has no
            // style identities yet.
            tree.className = "highlight";
            tree.children[3].className = "flag";
        });
        println(`style passes after disconnected mutations: ${work.passes().join(" | ") || "(no pass)"}`);
        println(`restyled after disconnected mutations: [${work.restyled().map(shortStyleName).join(", ")}]`);

        // Once inserted, all the mutations made while disconnected must still be reflected in computed style.
        host.appendChild(tree);
        println(`.highlight span background: ${getComputedStyle(tree.children[1]).backgroundColor}`);
        println(`div:has(> .flag) outline color: ${getComputedStyle(tree).outlineColor}`);
        println(`.red child color: ${getComputedStyle(tree.children[1]).color}`);
        println(`#blue child color: ${getComputedStyle(tree.firstChild).color}`);
        println(`custom element :defined color: ${getComputedStyle(custom).color}`);

        // And mutations after insertion must react as usual.
        tree.children[2].className = "";
        println(`un-classed child color: ${getComputedStyle(tree.children[2]).color}`);

        const detachedTree = document.createElement("div");
        detachedTree.className = "detached-red";
        const detachedChild = document.createElement("span");
        detachedChild.className = "detached-target";
        detachedTree.appendChild(detachedChild);
        host.appendChild(detachedTree);
        println(`detached child initial color: ${detachedChild.computedStyleMap().get("color")}`);

        // A disconnected element has no computed style at all: getComputedStyle refuses one, and the
        // computed style map answers the same way rather than reporting a style nothing decided.
        detachedTree.remove();
        detachedChild.style.color = "rgb(0, 128, 0)";
        println(`detached child inline color: ${detachedChild.computedStyleMap().get("color")}`);

        detachedChild.style.color = "";
        detachedTree.className = "detached-blue";
        println(`detached child ancestor class color: ${detachedChild.computedStyleMap().get("color")}`);
        println(`detached child map size: ${detachedChild.computedStyleMap().size}`);
        println(`detached child map has color: ${detachedChild.computedStyleMap().has("color")}`);
    });
</script>