Skip to content

Truly independent web browser

2.5 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    asyncTest(async done => {
        // The scripts arrive in a later chunk of the body than the start of the page, so the parser runs them as it
        // processes that chunk. A microtask that the first one queues runs before the parser prepares the second, and
        // so does the callback of a mutation observer that keeps the second from running.
        const server = httpTestServer();
        const token = crypto.randomUUID();
        const scriptStartTag = "<scr" + "ipt";
        const scriptEndTag = "</scr" + "ipt>";
        // NB: The parser waits for 1024 bytes before it starts, so the first chunk pads to more. And the response names
        //     its encoding, so that the parser doesn't look past the first chunk for an encoding declaration.
        const firstChunk = `<!DOCTYPE html><!--${"x".repeat(1100)}-->`;
        const secondChunk = `${scriptStartTag}>
                const log = [];
                Promise.resolve().then(() => log.push("microtask queued by the first script"));
                new MutationObserver(records => {
                    for (const record of records) {
                        for (const node of record.addedNodes) {
                            if (node.id === "blocked") {
                                node.type = "text/plain";
                                log.push("observer changed the second script to text/plain");
                            }
                        }
                    }
                }).observe(document.documentElement, { childList: true, subtree: true });
                log.push("first script ran");
            ${scriptEndTag}
            ${scriptStartTag} id="blocked">log.push("second script ran");${scriptEndTag}
            ${scriptStartTag}>addEventListener("load", () => parent.postMessage(log, "*"));${scriptEndTag}`;
        const url = await server.createEcho("GET", `/microtask-checkpoint-between-parser-inserted-scripts-${token}`, {
            status: 200,
            headers: { "Content-Type": "text/html; charset=utf-8" },
            body: firstChunk + secondChunk,
        });

        const frame = document.createElement("iframe");
        window.addEventListener("message", event => {
            for (const entry of event.data)
                println(entry);
            done();
        });
        frame.src = `${url}?chunks=${firstChunk.length}&chunk_delay_ms=100`;
        document.body.appendChild(frame);
    });
</script>