Skip to content

Truly independent web browser

5.2 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<style>
    .guard .target { color: rgb(0, 128, 0) }
    .guard > .child { outline-color: rgb(0, 0, 255) }
    .outer .inner .deep { border-top-color: rgb(255, 0, 0) }
    .guard .target .nested { border-bottom-color: rgb(128, 0, 128) }
    .guard:nth-child(2) .counted { border-left-color: rgb(0, 128, 128) }
    .guard .slotted { border-right-color: rgb(200, 100, 0) }
</style>
<div id="fixture">
    <div id="spacer"></div>
    <div id="outer">
        <div id="middle" class="inner child">
            <span id="target" class="target deep counted">
                <span id="nested" class="nested"></span>
            </span>
            <span id="sibling" class="target"></span>
        </div>
    </div>
    <div id="host"><span id="light" class="slotted"></span></div>
</div>
<script>
    // Incremental prefix evaluation must stop propagating unchanged selector state. Every
    // skipped node must still settle to the same style as a complete recompute.
    const shadow = document.getElementById("host").attachShadow({ mode: "open" });
    shadow.innerHTML = `<style>.guard ::slotted(.slotted) { color: rgb(9, 9, 9) }</style><slot></slot>`;

    let forced = 0;

    function snapshot() {
        return Array.from(document.querySelectorAll("#fixture *"))
            .map(element => {
                const style = getComputedStyle(element);
                return `${element.id}=${style.color}/${style.outlineColor}/${style.borderTopColor}`
                    + `/${style.borderBottomColor}/${style.borderLeftColor}/${style.borderRightColor}`;
            })
            .join(" ");
    }

    function settlesExactly(name, mutate) {
        internals.updateStyle();
        mutate();
        internals.updateStyle();
        const incremental = snapshot();
        internals.setUserStyle(`/* force complete style ${forced++} */`);
        internals.updateStyle();
        const complete = snapshot();
        internals.setUserStyle("");
        if (incremental === complete) {
            println(`${name}: settles exactly`);
            return;
        }
        println(`${name}: STALE`);
        println(`  incremental: ${incremental}`);
        println(`  complete:    ${complete}`);
    }

    test(() => {
        const fixture = document.getElementById("fixture");
        const spacer = document.getElementById("spacer");
        const outer = document.getElementById("outer");
        const middle = document.getElementById("middle");
        const target = document.getElementById("target");
        const nested = document.getElementById("nested");
        const host = document.getElementById("host");

        settlesExactly("a guard arrives two levels above the targets", () => outer.classList.add("guard"));
        settlesExactly("a nearer guard arrives while the outer one holds", () => middle.classList.add("guard"));
        settlesExactly("the outer guard leaves, the nearer one remains", () => outer.classList.remove("guard"));

        // Two roots in one transaction, one an ancestor of the other: the walk must not let the
        // ancestor's convergence hide the descendant's own change.
        settlesExactly("a guard leaves as a deep target arrives under it", () => {
            middle.classList.remove("guard");
            nested.classList.add("target");
        });
        settlesExactly("and both come back together", () => {
            middle.classList.add("guard");
            nested.classList.remove("target");
        });

        // A change that only the child combinator reads, under a guard that does not move.
        settlesExactly("the child of the guard stops being one", () => middle.classList.remove("child"));
        settlesExactly("and becomes one again", () => middle.classList.add("child"));

        // A positional prefix compound plus a tree change in the same transaction: the plan has to
        // fall back rather than compare states over an ancestry whose ordinals moved.
        settlesExactly("a guard changes while the sequence around it moves", () => {
            outer.classList.add("guard");
            fixture.insertBefore(spacer, outer);
        });
        settlesExactly("and the sequence moves back", () => {
            outer.classList.remove("guard");
            fixture.insertBefore(spacer, fixture.firstChild);
        });

        // A shadow scope's own rule reads a guard in its own tree, which the document-scope
        // convergence pass must not answer for.
        settlesExactly("a guard arrives on the shadow host", () => host.classList.add("guard"));
        settlesExactly("and leaves the host again", () => host.classList.remove("guard"));

        // A whole subtree arriving beside a guard change in one transaction.
        settlesExactly("a subtree arrives as its guard does", () => {
            const arrival = document.createElement("div");
            arrival.innerHTML = `<span class="target"><span class="nested"></span></span>`;
            arrival.id = "arrival";
            outer.appendChild(arrival);
            outer.classList.add("guard");
        });
        settlesExactly("the guard leaves the arrived subtree", () => outer.classList.remove("guard"));
        settlesExactly("a state changes under the guard", () => target.classList.toggle("deep"));
    });
</script>