Skip to content

Truly independent web browser

4.1 KB
Raw Blame
<!DOCTYPE html>
<style>#target { background-color: blue }</style>
<div id="target"></div>
<script src="../include.js"></script>
<script>
    test(() => {
        const red = new CSSStyleSheet();
        red.replaceSync("#target { color: red }");
        const green = new CSSStyleSheet();
        green.replaceSync("#target { color: green }");
        const sheets = () => document.adoptedStyleSheets;
        const color = () => getComputedStyle(target).color;
        const attempt = (name, operation) => {
            try {
                println(`${name}: ${operation()}`);
            } catch (e) {
                println(`${name}: ${e.name}`);
            }
        };

        document.adoptedStyleSheets = [red];
        println(`adopted red: ${color()}`);

        attempt("grow length", () => Reflect.set(sheets(), "length", 3));
        attempt("fractional length", () => Reflect.set(sheets(), "length", 0.5));
        attempt("set past the end", () => Reflect.set(sheets(), 5, green));
        println(`length: ${sheets().length}`);

        sheets()[0] = green;
        println(`overwrote red with green: ${color()}`);

        sheets().push(red);
        attempt("delete first", () => Reflect.deleteProperty(sheets(), 0));
        attempt("delete length", () => Reflect.deleteProperty(sheets(), "length"));
        attempt("delete last", () => Reflect.deleteProperty(sheets(), 1));
        println(`length: ${sheets().length}, color: ${color()}`);

        sheets().length = 0;
        println(`shrank length to 0: ${color()}`);

        document.adoptedStyleSheets = [red, red];
        sheets().length = 1;
        println(`removed one of two reds: ${color()}`);

        document.adoptedStyleSheets = [red, green, red];
        sheets().length = 2;
        println(`removed the last of two reds: ${color()}`);

        const empty = new CSSStyleSheet();
        document.adoptedStyleSheets = [red, empty, green, empty, red];
        sheets().length = 4;
        println(`removed the last red after a duplicated sheet: ${color()}`);

        document.adoptedStyleSheets = [red];
        attempt("set null over red", () => Reflect.set(sheets(), 0, null));
        println(`after a failed set: ${color()}, length: ${sheets().length}`);
        attempt("set a non-constructed sheet over red", () => Reflect.set(sheets(), 0, document.styleSheets[0]));
        println(`after a rejected set: ${color()}, length: ${sheets().length}`);

        document.adoptedStyleSheets = [red];
        attempt("define a non-sheet", () => Reflect.defineProperty(sheets(), 1, { value: 1 }));
        println(`after defining a non-sheet: ${color()}, length: ${sheets().length}`);
        attempt("define a non-constructed sheet", () => Reflect.defineProperty(sheets(), 0, { value: document.styleSheets[0] }));
        println(`style element still applies: ${getComputedStyle(target).backgroundColor}`);
        document.adoptedStyleSheets = [];

        let setterCalls = 0;
        const prototype = Object.create(Array.prototype, { 0: { set() { ++setterCalls; } } });
        Object.setPrototypeOf(sheets(), prototype);
        sheets()[0] = red;
        println(`set with a setter on the prototype: ${color()}, own element: ${Object.hasOwn(sheets(), 0)}, setter calls: ${setterCalls}`);
        document.adoptedStyleSheets = [];
        println(`cleared after that: ${color()}`);
        Object.setPrototypeOf(sheets(), Array.prototype);

        document.adoptedStyleSheets = [red];
        attempt("make an index non-configurable", () => Reflect.defineProperty(sheets(), 0, { configurable: false }));
        attempt("make length non-writable", () => Reflect.defineProperty(sheets(), "length", { writable: false }));
        println(`after rejected descriptors: ${color()}, length: ${sheets().length}`);
        attempt("append after rejected descriptors", () => Reflect.set(sheets(), 1, green));
        attempt("delete after rejected descriptors", () => Reflect.deleteProperty(sheets(), 1));
        attempt("shrink after rejected descriptors", () => Reflect.set(sheets(), "length", 0));
        println(`after shrinking: ${color()}, length: ${sheets().length}`);
    });
</script>