Skip to content

Truly independent web browser

3.7 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script src="../../work-test.js"></script>
<style id="rules"></style>
<div id="container"></div>
<script>
    test(() => {
        for (let index = 0; index < 64; ++index) {
            const child = container.appendChild(document.createElement("div"));
            child.textContent = "selectable text";
        }
        internals.updateStyle();
        // FIXME: Check that no pass computes the selection styles of elements nothing selects or queries, here
        //        and after the deselected rule change below. The style engine styles ::selection for every element
        //        once the document shows a selection, not only for the selected subtrees.
        rules.sheet.insertRule("*::selection { color: rgb(10, 20, 30); background-color: rgb(40, 50, 60); }");
        internals.updateStyle();
        const target = container.firstElementChild;
        let unselectedColor;
        const query = styleWork(() => unselectedColor = getComputedStyle(target, "::selection").color, internals, { flush: false });
        println(`unselected query: ${unselectedColor}`);
        // A highlight pseudo-element inherits from its ancestors', so a query computes that chain and nothing else.
        println(`query restyled: [${query.restyled().map(shortStyleName).join(", ")}]`);

        getComputedStyle(container.lastElementChild, "::selection").backgroundColor;
        target.getBoundingClientRect();
        const range = document.createRange();
        range.selectNodeContents(target);
        let activation;
        const activationWork = layoutWork(() => {
            activation = styleWork(() => document.getSelection().addRange(range));
            target.getBoundingClientRect();
        });
        println(`activation changed: [${activation.changed().map(shortStyleName).join(", ")}]`);
        println(`activation restyled: [${activation.restyled().map(shortStyleName).join(", ")}]`);
        println(`layout after activation: ${activationWork.passes().join(" | ") || "(no layout)"}`);
        println(`selected query: ${getComputedStyle(target, "::selection").backgroundColor}`);

        const extension = styleWork(() => range.setEnd(target.nextElementSibling.firstChild, 3));
        println(`extension restyled: [${extension.restyled().map(shortStyleName).join(", ")}]`);


        document.getSelection().removeAllRanges();
        rules.sheet.cssRules[0].style.backgroundColor = "rgb(70, 80, 90)";
        internals.updateStyle();
        println(`deselected query: ${getComputedStyle(target, "::selection").backgroundColor}`);

        const reactivation = styleWork(() => document.getSelection().addRange(range));
        println(`reactivation changed: [${reactivation.changed().map(shortStyleName).join(", ")}]`);
        println(`reactivated query: ${getComputedStyle(target, "::selection").backgroundColor}`);
        println(`unselected query while active: ${getComputedStyle(container.lastElementChild, "::selection").backgroundColor}`);

        document.getSelection().removeAllRanges();
        internals.updateStyle();
        rules.sheet.insertRule(".changed { color: rgb(90, 100, 110); }");
        target.className = "changed";
        document.getSelection().addRange(range);
        internals.updateStyle();
        println(`activation with pending rule and class changes: ${getComputedStyle(target).color}`);

        document.getSelection().removeAllRanges();
        internals.updateStyle();
        target.style.color = "rgb(120, 130, 140)";
        document.getSelection().addRange(range);
        internals.updateStyle();
        println(`activation with pending inline style: ${getComputedStyle(target).color}`);
    });
</script>