Skip to content

Truly independent web browser

3.4 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script src="../../work-test.js"></script>
<style>
    #descendant-anchor.active #descendant-target { color: red }
    #sibling-anchor.active ~ #sibling-target { color: red }
    #positional-target:nth-child(odd) { color: red }
    #relational-anchor:has(.witness) { color: red }
</style>
<div id="descendant-anchor"><div id="descendant-target"></div></div>
<div id="sibling-root"><div id="sibling-anchor"></div><div id="sibling-target"></div></div>
<div id="positional-root"><div id="positional-target"></div></div>
<div id="relational-anchor"></div>
<div id="filler-root"></div>
<script>
    test(() => {
        function measure(action, undo) {
            action();
            internals.updateStyle();
            undo();
            internals.updateStyle();

            const work = styleWork(action);
            undo();
            internals.updateStyle();
            return work;
        }

        const descendantAnchor = document.getElementById("descendant-anchor");
        const siblingAnchor = document.getElementById("sibling-anchor");
        const positionalRoot = document.getElementById("positional-root");
        const positionalTarget = document.getElementById("positional-target");
        const positionalShift = document.createElement("div");
        const relationalAnchor = document.getElementById("relational-anchor");
        const witness = document.createElement("span");
        witness.className = "witness";
        const fillerRoot = document.getElementById("filler-root");

        internals.updateStyle();
        const measurements = [];
        for (const size of [256, 1024, 4096]) {
            const fragment = document.createDocumentFragment();
            for (let index = fillerRoot.childElementCount; index < size; ++index)
                fragment.appendChild(document.createElement("div"));
            fillerRoot.appendChild(fragment);
            internals.updateStyle();

            measurements.push({
                descendant: measure(
                    () => descendantAnchor.classList.add("active"),
                    () => descendantAnchor.classList.remove("active")),
                sibling: measure(
                    () => siblingAnchor.classList.add("active"),
                    () => siblingAnchor.classList.remove("active")),
                positional: measure(
                    () => positionalRoot.insertBefore(positionalShift, positionalTarget),
                    () => positionalShift.remove()),
                relational: measure(
                    () => relationalAnchor.appendChild(witness),
                    () => witness.remove()),
            });
        }

        for (const operator of ["descendant", "sibling", "positional", "relational"]) {
            const works = measurements.map(measurement => measurement[operator]);
            const changed = works[0].changed();
            const unchanged = works[0].restyled().filter(element => !changed.includes(element));
            println(`${operator}: passes ${works[0].passes().join(" | ") || "(no pass)"}, changed [${changed.map(shortStyleName).join(", ")}], restyled but unchanged [${unchanged.map(shortStyleName).join(", ")}]`);
            println(`${operator} style work is the same at every document size: ${works.every(work => work.text === works[0].text)}`);
        }
        println(`largest unrelated population: ${fillerRoot.childElementCount}`);
    });
</script>