Skip to content

Truly independent web browser

5.6 KB
Raw Blame
<!doctype html>
<meta charset="UTF-8" />
<script src="../include.js"></script>
<script>
    asyncTest(async done => {
        const httpServer = httpTestServer();
        // A declaration in the head is found before the parser starts. One that comes after the head ended, past the
        // first 1024 bytes, is found by the parser, which changes the encoding on the fly.
        async function runCase(name, declaration, { afterHead = false } = {}) {
            const scriptURL = await httpServer.createEcho("GET", `/parser-streams-late-meta-script-${name}`, {
                status: 200,
                headers: {
                    "Content-Type": "application/javascript",
                },
                body: `window.decodedScriptText = "you’re";`,
            });

            const token = crypto.randomUUID();
            const head = afterHead
                ? `<!DOCTYPE html><body>${" ".repeat(2000)}`
                : `<!DOCTYPE html><head><!--${"a".repeat(2000)}-->`;
            const body = `${head}${declaration}<script src="${scriptURL}"><\/script></head><body><span id="target">©</span>`;
            const url = await httpServer.createEcho("GET", `/parser-streams-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 = `${url}?chunks=1500&chunk_unblock_tokens=${token}`;
            document.body.appendChild(frame);
            if (afterHead) {
                while (!frame.contentDocument.URL.startsWith(url) || !frame.contentDocument.head)
                    await new Promise(resolve => setTimeout(resolve, 0));
            } else {
                while (!(await fetch(httpServer.recordedRequestHeadersURL(url))).ok) {}
            }
            await fetch(new URL(`/unblock/${token}`, url));
            await loaded;

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

        async function runProgressiveCase() {
            const token = crypto.randomUUID();
            const body = `<!DOCTYPE html><body>©<span id="target">ready</span>${" ".repeat(6000)}`;
            const url = await httpServer.createEcho("GET", "/parser-streams-tentative-progressive", {
                status: 200,
                headers: {
                    "Content-Type": "text/html",
                },
                body,
            });

            const frame = document.createElement("iframe");
            let didLoad = false;
            const loadPromise = new Promise(resolve => {
                frame.onload = () => {
                    didLoad = true;
                    resolve();
                };
            });
            frame.src = `${url}?chunks=1500&chunk_unblock_tokens=${token}`;
            document.body.appendChild(frame);

            while (!didLoad && !frame.contentDocument.getElementById("target"))
                await new Promise(resolve => setTimeout(resolve, 10));
            const parsedBeforeLoad = !didLoad && !!frame.contentDocument.getElementById("target");

            await fetch(new URL(`/unblock/${token}`, url));
            await loadPromise;
            println(`charset-less progressive: ${parsedBeforeLoad}`);
            frame.remove();
        }

        async function runPostEOFCase() {
            const token = crypto.randomUUID();
            const scriptURL = await httpServer.createEcho("GET", "/parser-streams-post-eof-script", {
                status: 200,
                headers: {
                    "Content-Type": "application/javascript",
                },
                body: `window.postEOFScriptRan = true;`,
                wait_for_unblock: token,
            });

            const body = `<!DOCTYPE html><head><script type="application/json">"<meta charset=windows-1252>"<\/script><!--${"a".repeat(1200)}--><script src="${scriptURL}"><\/script><meta charset="UTF-8"></head><body>ASCII`;
            const url = await httpServer.createEcho("GET", "/parser-streams-post-eof-meta", {
                status: 200,
                headers: {
                    "Content-Type": "text/html",
                },
                body,
            });

            const frame = document.createElement("iframe");
            const loaded = new Promise(resolve => {
                frame.onload = resolve;
            });
            frame.src = url;
            document.body.appendChild(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}`, url));
            await loaded;

            println(`post-eof charset: ${frame.contentDocument.characterSet}`);
            println(`post-eof script: ${frame.contentWindow.postEOFScriptRan}`);
            frame.remove();
        }

        await runCase("charset", `<meta charset="UTF-8">`);
        await runCase("http-equiv", `<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">`);
        await runCase("charset-after-head", `<meta charset="UTF-8">`, { afterHead: true });
        await runProgressiveCase();
        await runPostEOFCase();
        done();
    });
</script>