fix(core): improve island props serialization (#3071)

This commit is contained in:
Marvin Hagemeister
2025-06-30 16:31:58 +02:00
committed by GitHub
parent 8d3b8c53c6
commit 8416078b6a
5 changed files with 135 additions and 7 deletions
+3 -2
View File
@@ -1,7 +1,7 @@
import { useEffect } from "preact/hooks";
import { useSignal } from "@preact/signals";
export function EscapeIsland(_props: { str: string }) {
export function EscapeIsland(props: { str: string }) {
const active = useSignal(false);
useEffect(() => {
@@ -10,7 +10,8 @@ export function EscapeIsland(_props: { str: string }) {
return (
<div class={active.value ? "ready" : ""}>
it works
<p>it works</p>
<div>{props.str}</div>
</div>
);
}
+70 -1
View File
@@ -31,6 +31,7 @@ import { getBuildCache } from "../src/app.ts";
import type { FreshConfig } from "../src/config.ts";
import { FreshAttrs } from "./fixtures_islands/FreshAttrs.tsx";
import { FakeServer } from "../src/test_utils.ts";
import { PARTIAL_SEARCH_PARAM } from "../src/constants.ts";
await buildProd(allIslandApp);
@@ -577,6 +578,20 @@ Deno.test({
<EscapeIsland str={`"foo"asdf`} />
</Doc>,
);
})
.get("/foo", (ctx) => {
return ctx.render(
<Doc>
<EscapeIsland str={`<script>alert('hey')</script>`} />
</Doc>,
);
})
.get("/bar", (ctx) => {
return ctx.render(
<Doc>
<EscapeIsland str={`<!--<script>alert('hey')</script>`} />
</Doc>,
);
});
await withBrowserApp(app, async (page, address) => {
@@ -585,9 +600,63 @@ Deno.test({
// Page would error here
const text = await page
.locator<HTMLDivElement>(".ready")
.locator<HTMLDivElement>(".ready p")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
// Page would error here
const text2 = await page
.locator<HTMLDivElement>(".ready div")
.evaluate((el) => el.textContent!);
expect(text2).toEqual(`"foo"asdf`);
});
// Check escaping of `</`
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/foo`, { waitUntil: "load" });
await page.locator(".ready").wait();
const text = await page
.locator<HTMLDivElement>(".ready p")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
const text2 = await page
.locator<HTMLDivElement>(".ready div")
.evaluate((el) => el.textContent!);
expect(text2).toEqual(`<script>alert('hey')</script>`);
});
// Check escaping of `<!--`
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/bar`, { waitUntil: "load" });
await page.locator(".ready").wait();
const text = await page
.locator<HTMLDivElement>(".ready p")
.evaluate((el) => el.textContent!);
expect(text).toEqual("it works");
const text2 = await page
.locator<HTMLDivElement>(".ready div")
.evaluate((el) => el.textContent!);
expect(text2).toEqual(`<!--<script>alert('hey')</script>`);
});
// Partials (they use a different code path)
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/foo?${PARTIAL_SEARCH_PARAM}`, {
waitUntil: "load",
});
const text = await page
.locator<HTMLScriptElement>("script[type='application/json']")
.evaluate((el) => el.textContent!);
const json = JSON.parse(text);
expect(json.props).toContain("<script>alert('hey')</script>");
});
},
});