Skip to content

Truly independent web browser

2.9 KB
Raw Blame
<!DOCTYPE html>
<meta charset="utf-8">
<script src="../../include.js"></script>
<script src="../../work-test.js"></script>
<div id="parent" style="--declared: 10px">
    <div id="inheritor"></div>
    <div id="reader" style="color: var(--registered-later, rgb(1, 2, 3))"></div>
    <div id="caller" style="width: --double(5px)"></div>
</div>
<script>
    function addBystanders(parent, count) {
        for (let i = 0; i < count; ++i) {
            const bystander = document.createElement("div");
            bystander.textContent = `bystander ${i}`;
            parent.appendChild(bystander);
        }
    }

    function addSheet(text) {
        const style = document.createElement("style");
        style.textContent = text;
        document.head.appendChild(style);
    }

    function dumpWork(label, mutate) {
        const work = styleWork(mutate);
        const fullPass = work.passes().includes("FULL") ? " (full pass)" : "";
        println(`${label}: restyled [${work.restyled().map(shortStyleName).join(", ")}]${fullPass}`);
    }

    test(() => {
        addBystanders(document.body, 25);
        const parent = document.getElementById("parent");
        const inheritor = document.getElementById("inheritor");
        const reader = document.getElementById("reader");
        const caller = document.getElementById("caller");

        // A registration nobody declares or reads restyles nothing, not even the arriving style element.
        // FIXME: Every registration also restyles #caller, which calls a custom function.
        dumpWork("@property nobody uses", () => addSheet(`@property --unused { syntax: "<length>"; inherits: false; initial-value: 0px; }`));

        // A registration restyles the elements holding its name, whose children follow.
        dumpWork("@property a parent declares", () => addSheet(`@property --declared { syntax: "<length>"; inherits: false; initial-value: 3px; }`));
        println(`parent: ${getComputedStyle(parent).getPropertyValue("--declared")}`);
        println(`inheritor: ${getComputedStyle(inheritor).getPropertyValue("--declared")}`);

        // A registration through script restyles the elements reading its name (and #caller, as above).
        dumpWork("CSS.registerProperty() one element reads", () => {
            CSS.registerProperty({ name: "--registered-later", syntax: "<color>", inherits: false, initialValue: "rgb(0, 128, 0)" });
        });
        println(`reader: ${getComputedStyle(reader).color}`);

        // A counter style restyles only the elements naming its scope's registry.
        dumpWork("@counter-style nobody uses", () => addSheet(`@counter-style unused-style { system: cyclic; symbols: "*"; }`));

        // A custom function restyles the elements calling one.
        dumpWork("@function one element calls", () => addSheet(`@function --double(--value) { result: calc(var(--value) * 2); }`));
        println(`caller: ${getComputedStyle(caller).width}`);
    });
</script>