Skip to content

Truly independent web browser

4.2 KB
Raw Blame
<!doctype html>
<meta charset="UTF-8" />
<script src="../include.js"></script>
<script>
    asyncTest(async done => {
        const httpServer = httpTestServer();

        async function createBlockingScript(name, token) {
            return httpServer.createEcho("GET", `/parser-streams-blocked-late-meta-script-${name}`, {
                status: 200,
                headers: {
                    "Content-Type": "application/javascript",
                },
                body: `window.blockingScriptRan = true; parent.postMessage("script ran", "*");`,
                wait_for_unblock: token,
            });
        }

        async function runCase(name, body, query, unblock) {
            const url = await httpServer.createEcho("GET", `/parser-streams-blocked-late-meta-${name}`, {
                status: 200,
                headers: {
                    "Content-Type": "text/html",
                },
                body,
            });

            const frame = document.createElement("iframe");
            const loaded = new Promise(resolve => {
                frame.onload = resolve;
                frame.src = query ? `${url}?${query}` : url;
                document.body.appendChild(frame);
            });
            await unblock(frame, url);
            await loaded;

            println(`${name} charset: ${frame.contentDocument.characterSet}`);
            println(`${name} script: ${frame.contentWindow.blockingScriptRan}`);
            println(`${name} body: ${frame.contentDocument.getElementById("target").textContent}`);
            frame.remove();
        }

        // The comment keeps the declaration past the first 1024 bytes. It also exceeds the response sniffing threshold,
        // so the first chunk can be parsed before EOF.
        const padding = `<!--${"a".repeat(2000)}-->`;

        // The declaration trails a parser-blocking script, so the parser is still paused at that script when the rest
        // of the response arrives.
        async function runPausedCase() {
            const token = crypto.randomUUID();
            const scriptURL = await createBlockingScript("paused", token);
            const body = `<!DOCTYPE html><head><script src="${scriptURL}"><\/script>${padding}<meta charset="UTF-8"></head><body><span id="target">©</span>`;
            await runCase("paused", body, undefined, async frame => {
                while (!(await fetch(httpServer.recordedRequestHeadersURL(scriptURL))).ok) {}
                while (!internals.htmlParserBodyIsExhausted(frame.contentDocument))
                    await new Promise(resolve => setTimeout(resolve, 0));
                await fetch(new URL(`/unblock/${token}`, scriptURL));
            });
        }

        // The blocking script resolves during the gap between chunks, so the parser has drained the first chunk before
        // the declaration arrives in the second. The declaration comes after the head ended, so the parser can start
        // before it arrives.
        async function runResumedCase() {
            const token = crypto.randomUUID();
            const scriptURL = await createBlockingScript("resumed");
            const head = `<!DOCTYPE html><body><script src="${scriptURL}"><\/script>${padding}`;
            const body = `${head}<meta charset="UTF-8"></head><body><span id="target">©</span>`;
            await runCase(
                "resumed",
                body,
                `chunks=${head.length}&chunk_unblock_tokens=${token}`,
                async (frame, url) => {
                    if (!frame.contentWindow.blockingScriptRan) {
                        await new Promise(resolve => {
                            window.addEventListener("message", function receive(event) {
                                if (event.source !== frame.contentWindow || event.data !== "script ran") return;
                                window.removeEventListener("message", receive);
                                resolve();
                            });
                        });
                    }
                    await fetch(new URL(`/unblock/${token}`, url));
                }
            );
        }

        await runPausedCase();
        await runResumedCase();
        done();
    });
</script>