From 1c1f1435be2e6498031da96269b136945801be23 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Thu, 11 Sep 2025 11:45:35 +0100 Subject: [PATCH] Check HTML-encoded quotes when handling translations for embedded pages (such as welcome.html) (#30743) * Check HTML encoding on embedded page * Add tests * lint --- src/components/structures/EmbeddedPage.tsx | 6 ++- .../views/context_menus/EmbeddedPage-test.tsx | 4 +- .../__snapshots__/EmbeddedPage-test.tsx.snap | 48 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/components/structures/EmbeddedPage.tsx b/src/components/structures/EmbeddedPage.tsx index f1c7b75b96..b1702c87c7 100644 --- a/src/components/structures/EmbeddedPage.tsx +++ b/src/components/structures/EmbeddedPage.tsx @@ -72,7 +72,11 @@ export default class EmbeddedPage extends React.PureComponent { return; } - let body = (await res.text()).replace(/_t\(['"]([\s\S]*?)['"]\)/gm, (match, g1) => this.translate(g1)); + // Replace '," and HTML encoded variants + let body = (await res.text()).replace( + /_t\((?:['"]|(?:&#(?:34|27);))([\s\S]*?)(?:['"]|(?:&#(?:34|27);))\)/gm, + (match, g1) => this.translate(g1), + ); if (this.props.replaceMap) { Object.keys(this.props.replaceMap).forEach((key) => { diff --git a/test/unit-tests/components/views/context_menus/EmbeddedPage-test.tsx b/test/unit-tests/components/views/context_menus/EmbeddedPage-test.tsx index 1740963583..b1d3b063dc 100644 --- a/test/unit-tests/components/views/context_menus/EmbeddedPage-test.tsx +++ b/test/unit-tests/components/views/context_menus/EmbeddedPage-test.tsx @@ -19,10 +19,10 @@ jest.mock("../../../../../src/languageHandler", () => ({ })); describe("", () => { - it("should translate _t strings", async () => { + it.each([`"`, `'`, `'`, `4`])("should translate _t strings", async (character) => { mocked(_t).mockReturnValue("Przeglądaj pokoje"); fetchMock.get("https://home.page", { - body: '

_t("Explore rooms")

', + body: `

_t(${character}Explore rooms${character})

`, }); const { asFragment } = render(); diff --git a/test/unit-tests/components/views/context_menus/__snapshots__/EmbeddedPage-test.tsx.snap b/test/unit-tests/components/views/context_menus/__snapshots__/EmbeddedPage-test.tsx.snap index fd88987845..f10040e325 100644 --- a/test/unit-tests/components/views/context_menus/__snapshots__/EmbeddedPage-test.tsx.snap +++ b/test/unit-tests/components/views/context_menus/__snapshots__/EmbeddedPage-test.tsx.snap @@ -41,3 +41,51 @@ exports[` should translate _t strings 1`] = ` `; + +exports[` should translate _t strings 2`] = ` + +
+
+

+ Przeglądaj pokoje +

+
+
+
+`; + +exports[` should translate _t strings 3`] = ` + +
+
+

+ Przeglądaj pokoje +

+
+
+
+`; + +exports[` should translate _t strings 4`] = ` + +
+
+

+ Przeglądaj pokoje +

+
+
+
+`;