Skip to content

Truly independent web browser

3.1 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<style id="sheet">
    #target { --a: 1 }
    #target { --b: 1 }
    #target { --c: 1 }
    #target { --d: 1 }
</style>
<div id="target" tabindex="0"></div>
<script>
    const sheet = document.getElementById("sheet").sheet;
    const target = document.getElementById("target");

    function edit(mutate) {
        mutate();
        // Reading a value flushes, so the whole edit is styled and not just the call.
        getComputedStyle(target).getPropertyValue("--a");
    }

    function color() {
        return getComputedStyle(target).color;
    }

    test(() => {
        document.body.offsetWidth;

        // One rule arriving lands where it was asked to. The rules around it keep their order.
        edit(() => sheet.insertRule("#target:focus { color: rgb(1, 1, 1) }", 0));
        edit(() => sheet.insertRule("#target:focus { color: rgb(2, 2, 2) }", 2));
        edit(() => sheet.insertRule("#target:focus { color: rgb(3, 3, 3) }", sheet.cssRules.length));

        // Each landed where it was asked to, so the last one wins on equal specificity.
        target.focus();
        println(`after three inserts: ${color()}`);

        // Deleting the winner lets the rule behind it take over.
        edit(() => sheet.deleteRule(sheet.cssRules.length - 1));
        println(`after deleting the winner: ${color()}`);

        // Retargeting a rule is that one rule changing. Its position is untouched, so it still
        // cascades where it did.
        edit(() => { sheet.cssRules[2].selectorText = "#target:focus, .unrelated"; });
        println(`after retargeting: ${color()}`);

        // A group arriving brings its own rules.
        edit(() => sheet.insertRule("@media all { #target:focus { color: rgb(4, 4, 4) } #target:focus { color: rgb(5, 5, 5) } }", sheet.cssRules.length));
        println(`after the group: ${color()}`);

        // Deleting the group takes its rules with it.
        edit(() => sheet.deleteRule(sheet.cssRules.length - 1));
        println(`after deleting the group: ${color()}`);

        // Every rule can be edited in turn.
        const count = sheet.cssRules.length;
        edit(() => {
            for (let index = 0; index < count; ++index)
                sheet.cssRules[index].selectorText = `#target:focus, .all-${index}`;
        });
        println(`after retargeting all: ${color()}`);

        // Every rule can be deleted in turn.
        edit(() => {
            while (sheet.cssRules.length > 0)
                sheet.deleteRule(sheet.cssRules.length - 1);
        });
        println(`after deleting all: ${color()}`);

        // `replace()` swaps the whole rule list.
        const constructed = new CSSStyleSheet();
        document.adoptedStyleSheets = [constructed];
        edit(() => {
            constructed.replaceSync("#target:focus { color: rgb(6, 6, 6) } .x { color: red } .y { color: red }");
        });
        println(`after replaceSync: ${color()}`);
        edit(() => {
            constructed.replaceSync("#target:focus { color: rgb(7, 7, 7) }");
        });
        println(`after the second replaceSync: ${color()}`);
    });
</script>