Files
hyperframes/packages/cli/src/utils/staticProjectServer.charset.browser.test.ts
T
Miguel Ángel 6d357ff0d8 fix(cli): check, snapshot and Studio read project text as UTF-8 (#5843)
* fix(cli): check, snapshot and Studio read project text as UTF-8

The preview servers sent HTML, CSS, JS and JSON with no charset, so Chrome
read a page without <meta charset> as windows-1252 and stills showed garbled
non-Latin text while the render, whose file server already sends utf-8,
was correct. The shared MIME table now carries charset=utf-8 for text types,
and the two hard-coded headers use it.

* fix(cli): snapshot shows accented text when the page's meta charset sits outside its head

A <meta charset> past Chrome's first 1024 bytes and outside <head> is ignored, so with no
charset in the header the page decoded as windows-1252. The browser test serves such a page
through the static server snapshot uses. play compares the media type without its charset,
so JSON assets keep their content-hash revalidation.
2026-10-20 13:52:43 +00:00

40 lines
1.6 KiB
TypeScript

import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it } from "vitest";
import { openSettledCompositionPage } from "../capture/captureCompositionFrame.js";
import { serveStaticProjectHtml } from "./staticProjectServer.js";
// Past Chrome's 1024-byte prescan and outside <head>, a <meta charset> is ignored; the page must still be UTF-8.
const HTML = `<!doctype html><html><head><style>p{margin:0}/*${"x".repeat(2048)}*/</style></head><body>
<meta charset="UTF-8" />
<div data-composition-id="main" data-width="320" data-height="180" data-duration="1">
<p id="raw">Café è ù ✓</p></div>
<script>window.__renderReady = true;</script></body></html>`;
it("shows accented text as written when the page's meta charset is not in its head", async () => {
const projectDir = mkdtempSync(join(tmpdir(), "hf-charset-"));
writeFileSync(join(projectDir, "index.html"), HTML);
const server = await serveStaticProjectHtml(projectDir, HTML);
try {
const { browser, page } = await openSettledCompositionPage(HTML, server.url, {
renderReadyTimeoutMs: 5_000,
renderReadyWarningSuffix: "test",
browserGpuMode: "software",
});
try {
expect(
await page.evaluate(() => ({
charset: document.characterSet,
raw: document.getElementById("raw")?.textContent,
})),
).toEqual({ charset: "UTF-8", raw: "Café è ù ✓" });
} finally {
await browser.close();
}
} finally {
await server.close();
rmSync(projectDir, { recursive: true, force: true });
}
}, 90_000);