Skip to content

Truly independent web browser

1.8 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script src="../../work-test.js"></script>
<ul id="list"></ul>
<div id="host"></div>
<script>
    test(() => {
        for (let i = 0; i < 50; ++i) {
            const bystander = list.appendChild(document.createElement("li"));
            bystander.appendChild(document.createElement("div"));
        }
        const shadow = host.attachShadow({ mode: "open" });
        const item = shadow.appendChild(document.createElement("li"));
        const target = item.appendChild(document.createElement("div")).appendChild(document.createElement("button"));
        internals.updateStyle();

        function restyledForSheet(cssText) {
            const sheet = new CSSStyleSheet();
            sheet.replaceSync(cssText);
            const work = styleWork(() => shadow.adoptedStyleSheets = [sheet]);
            shadow.adoptedStyleSheets = [];
            internals.updateStyle();
            return `[${work.restyled().map(shortStyleName).join(", ")}]`;
        }

        // A sheet adopted by the shadow root restyles only the shadow tree elements its rules can match, so
        // none for :focus while nothing is focused, and none of the 50 list items outside it.
        println(`li > div > :focus: ${restyledForSheet("li > div > :focus { color: red }")}`);
        println(`li > div > *: ${restyledForSheet("li > div > * { color: red }")}`);
        println(`li :focus: ${restyledForSheet("li :focus { color: red }")}`);

        const sheet = new CSSStyleSheet();
        sheet.replaceSync("li > div > :focus { color: rgb(1, 2, 3) }");
        shadow.adoptedStyleSheets = [sheet];
        println(`before focus: ${getComputedStyle(target).color}`);
        target.focus();
        println(`after focus: ${getComputedStyle(target).color}`);
    });
</script>