Skip to content

Truly independent web browser

1.7 KB
Raw Blame
<!doctype html>
<div id="target" class="target"></div>
<script src="../include.js"></script>
<script>
    test(() => {
        const target = document.getElementById("target");
        // Selectors the cache holds on to while others go through it, some of which never parse.
        const workingSet = [".target", "div.target", "#target", "[id]", ".absent", "div:not(.target)", "span"];
        const invalid = ["..", "div,", "a|b|c", ":not("];
        const answers = () => workingSet.map(selector => document.querySelectorAll(selector).length).join(" ");

        function throwsSyntaxError(selector) {
            try {
                document.querySelector(selector);
                return false;
            } catch (error) {
                return error.name === "SyntaxError";
            }
        }

        const first = answers();
        println(first);

        let changed = 0;
        let notThrown = 0;
        for (let round = 0; round < 8; ++round) {
            // More selectors than the cache keeps, each new, between uses of the working set.
            for (let index = 0; index < 300; ++index) {
                document.querySelector(`.round-${round}-${index}`);
                throwsSyntaxError(`..round-${round}-${index}`);
            }
            changed += answers() !== first ? 1 : 0;
            for (const selector of invalid) {
                if (!throwsSyntaxError(selector)) {
                    println(`did not throw: ${selector}`);
                    ++notThrown;
                }
            }
        }
        println(`rounds that answered differently: ${changed}`);
        println(`invalid selectors that did not throw: ${notThrown}`);
    });
</script>