Skip to content

Truly independent web browser

6.8 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<style>
    .guard .target { color: rgb(0, 128, 0) }
    [data-mode] .target { outline-color: rgb(0, 0, 255) }
    [data-mode="dark"] .target { outline-color: rgb(255, 0, 0) }
    .card:has(.error) .button { color: rgb(255, 0, 255) }
    .anchor:has(> .flag) { color: rgb(0, 255, 255) }
    .row .lead + .after { color: rgb(128, 0, 128) }
    span.a.b.c { color: rgb(255, 165, 0) }
    /* The sibling seams a tree mutation moves: adjacency, the general prefix, and a count. */
    .seam .x + .y { color: rgb(0, 100, 100) }
    .seam .x ~ .z { color: rgb(100, 0, 100) }
    .seam > :nth-child(2 of .n) { color: rgb(100, 100, 0) }
    .seam:has(+ .peer) { color: rgb(10, 20, 30) }
    .seam .hole:empty { color: rgb(30, 20, 10) }
</style>
<div id="fixture">
    <div id="outer">
        <div id="inner">
            <span id="target" class="target"></span>
        </div>
    </div>
    <div id="card" class="card">
        <span id="witness"></span>
        <span id="button" class="button"></span>
    </div>
    <div id="anchor" class="anchor"><span id="flag"></span></div>
    <div id="row" class="row"><span id="lead"></span><span id="after" class="after"></span></div>
    <span id="conjunct" class="a b c"></span>
    <div id="seam" class="seam">
        <span id="x" class="x n"></span>
        <span id="y" class="y n"></span>
        <span id="z" class="z n"></span>
        <span id="hole" class="hole"></span>
    </div>
</div>
<script>
    // Routing narrows a reaction batch by proving a candidate's answer cannot have moved. What
    // that has to leave behind is a document whose settled style is the one a full restyle would
    // produce - so every case here mutates, settles incrementally, and then forces every element to
    // be recomputed and compares. A difference is an invalidation the plan should have named.
    let forced = 0;

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

    function settlesExactly(name, mutate) {
        internals.updateStyle();
        mutate();
        internals.updateStyle();
        const incremental = snapshot();
        // A registered property carrying an initial value reaches every element, which is the only
        // way from script to make the engine recompute what it decided it need not.
        CSS.registerProperty({ name: `--force-${forced++}`, syntax: "*", inherits: false, initialValue: "1px" });
        internals.updateStyle();
        const complete = snapshot();
        if (incremental === complete) {
            println(`${name}: settles exactly`);
            return;
        }
        println(`${name}: STALE`);
        println(`  incremental: ${incremental}`);
        println(`  complete:    ${complete}`);
    }

    test(() => {
        const outer = document.getElementById("outer");
        const inner = document.getElementById("inner");
        const witness = document.getElementById("witness");
        const flag = document.getElementById("flag");
        const lead = document.getElementById("lead");
        const conjunct = document.getElementById("conjunct");

        settlesExactly("a guard arrives above the target", () => outer.classList.add("guard"));
        // A nearer guard when one already holds: the answer does not move, and proving that is what
        // the exact candidate check is for. It still must not move.
        settlesExactly("a nearer guard arrives under it", () => inner.classList.add("guard"));
        settlesExactly("the outer guard leaves, the nearer one remains", () => outer.classList.remove("guard"));
        settlesExactly("the last guard leaves", () => inner.classList.remove("guard"));

        settlesExactly("an attribute arrives", () => outer.setAttribute("data-mode", "light"));
        settlesExactly("its value changes", () => outer.setAttribute("data-mode", "dark"));
        settlesExactly("it is rewritten with the same value", () => outer.setAttribute("data-mode", "dark"));
        settlesExactly("it leaves", () => outer.removeAttribute("data-mode"));

        settlesExactly("a relational witness arrives", () => witness.classList.add("error"));
        settlesExactly("a second witness joins it", () => document.getElementById("button").classList.add("error"));
        settlesExactly("the first witness leaves", () => witness.classList.remove("error"));
        settlesExactly("the last witness leaves", () => document.getElementById("button").classList.remove("error"));

        settlesExactly("a child-axis witness arrives", () => flag.classList.add("flag"));
        settlesExactly("it leaves again", () => flag.classList.remove("flag"));

        settlesExactly("a sibling gains what the selector reads", () => lead.classList.add("lead"));
        settlesExactly("and loses it", () => lead.classList.remove("lead"));

        // One transaction moving several conjuncts of one compound at once.
        settlesExactly("two conjuncts leave together", () => (conjunct.className = "a"));
        settlesExactly("they arrive together", () => (conjunct.className = "a b c"));
        settlesExactly("one leaves as another arrives", () => (conjunct.className = "a b d"));

        // Tree mutations, which move the seams a sibling selector reads rather than any fact.
        const seam = document.getElementById("seam");
        const x = document.getElementById("x");
        const y = document.getElementById("y");
        const hole = document.getElementById("hole");
        const spacer = document.createElement("span");

        settlesExactly("an element arrives between two adjacent siblings", () => seam.insertBefore(spacer, y));
        settlesExactly("and leaves again, closing the seam", () => spacer.remove());
        settlesExactly("a counted sibling arrives at the front", () =>
            seam.insertBefore(Object.assign(document.createElement("span"), { className: "n", id: "n0" }), x));
        settlesExactly("the element a general prefix rests on leaves", () => x.remove());
        settlesExactly("and comes back at the front", () => seam.insertBefore(x, seam.firstChild));
        settlesExactly("it moves past what it preceded", () => seam.appendChild(x));
        settlesExactly("a following peer arrives", () =>
            seam.after(Object.assign(document.createElement("div"), { className: "peer", id: "peer" })));
        settlesExactly("that peer leaves", () => document.getElementById("peer").remove());
        settlesExactly("text fills an empty element", () => hole.appendChild(document.createTextNode("t")));
        settlesExactly("and the text goes away again", () => hole.firstChild.remove());
    });
</script>