Skip to content

Truly independent web browser

2.6 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script src="../../work-test.js"></script>
<style id="registrations">
    .declarer { --registered: 12px; }
    .reference { width: var(--registered); }
</style>
<div id="unrelated"></div>
<div class="unrelated"></div>
<div class="unrelated"></div>
<div class="unrelated"></div>
<div class="declarer"></div>
<div class="reference"></div>
<script>
    test(() => {
        const sheet = document.getElementById("registrations").sheet;
        // The first CSSOM edit recompiles shared contents into a private stylesheet. Insert and remove
        // a dummy rule before measuring, so the counters cover only subsequent incremental edits.
        sheet.insertRule(":not(*) {}", 0);
        sheet.deleteRule(0);

        const registration = styleWork(() => {
            document.getElementById("registrations").sheet.insertRule(
                "@property --registered { syntax: '<length>'; inherits: false; initial-value: 0px; }"
            );
            document.getElementById("registrations").sheet.insertRule(":not(*) { color: red; }");
        });

        // The registration restyles only the declarer and the reference.
        println(`@property restyled: [${registration.restyled().map(shortStyleName).join(", ")}], passes: ${registration.passes().join(" | ") || "(no pass)"}`);
        println(`unrelated registered initial value: ${getComputedStyle(unrelated).getPropertyValue("--registered")}`);

        document.querySelector(".reference").remove();
        const apiDeclarer = document.createElement("div");
        apiDeclarer.style.setProperty("--api-registered", "14px");
        document.body.appendChild(apiDeclarer);
        const apiReference = document.createElement("div");
        apiReference.style.width = "var(--api-registered)";
        document.body.appendChild(apiReference);
        internals.updateStyle();

        const apiRegistration = styleWork(() => {
            CSS.registerProperty({
                name: "--api-registered",
                syntax: "<length>",
                inherits: false,
                initialValue: "0px",
            });
            document.getElementById("registrations").sheet.insertRule(":not(*) { color: green; }");
        });

        // This registration restyles only the API declarer and reference.
        println(`CSS.registerProperty() restyled: [${apiRegistration.restyled().map(shortStyleName).join(", ")}], passes: ${apiRegistration.passes().join(" | ") || "(no pass)"}`);

        println(`unrelated API initial value: ${getComputedStyle(unrelated).getPropertyValue("--api-registered")}`);
    });
</script>