Skip to content

Truly independent web browser

1.5 KB
Raw Blame
<!DOCTYPE html>
<style id="rules">
    @position-try --a { top: 0px; left: 200px; }
</style>
<style>
    #container { position: relative; width: 300px; height: 300px; }
    #box { position: absolute; top: 500px; left: 0; width: 50px; height: 50px; position-try-fallbacks: --a; }
</style>
<div id="container"><div id="box"></div></div>
<script src="../include.js"></script>
<script>
    // A box whose last successful position option came from a @position-try rule goes back to its base styles once that
    // rule is replaced, even where the replacing rule's declarations land on the address of the old ones, as they will
    // often enough over many edits with a style flush but no layout between.
    test(() => {
        const rules = document.getElementById("rules");
        const box = document.getElementById("box");
        let kept = 0;
        for (let i = 0; i < 200; ++i) {
            box.style.top = "500px";
            box.offsetWidth;
            // The base styles fit now, which the box only finds if it starts from them.
            box.style.top = "10px";
            rules.textContent = `@position-try --a { top: 0px; left: ${200 + i % 2}px; }`;
            getComputedStyle(box).color;
            rules.textContent = `@position-try --a { top: 0px; left: ${202 + i % 2}px; }`;
            getComputedStyle(box).color;
            if (box.getBoundingClientRect().left - container.getBoundingClientRect().left !== 0)
                ++kept;
        }
        println(`${kept} of 200 kept an option of a replaced rule`);
    });
</script>