Skip to content

Commit 1c1f143

Browse files
authored
Check HTML-encoded quotes when handling translations for embedded pages (such as welcome.html) (#30743)
* Check HTML encoding on embedded page * Add tests * lint
1 parent a69ce3f commit 1c1f143

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/components/structures/EmbeddedPage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ export default class EmbeddedPage extends React.PureComponent<IProps, IState> {
7272
return;
7373
}
7474

75-
let body = (await res.text()).replace(/_t\(['"]([\s\S]*?)['"]\)/gm, (match, g1) => this.translate(g1));
75+
// Replace '," and HTML encoded variants
76+
let body = (await res.text()).replace(
77+
/_t\((?:['"]|(?:&#(?:34|27);))([\s\S]*?)(?:['"]|(?:&#(?:34|27);))\)/gm,
78+
(match, g1) => this.translate(g1),
79+
);
7680

7781
if (this.props.replaceMap) {
7882
Object.keys(this.props.replaceMap).forEach((key) => {

test/unit-tests/components/views/context_menus/EmbeddedPage-test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ jest.mock("../../../../../src/languageHandler", () => ({
1919
}));
2020

2121
describe("<EmbeddedPage />", () => {
22-
it("should translate _t strings", async () => {
22+
it.each([`"`, `'`, `&#x27;`, `&#x34;`])("should translate _t strings", async (character) => {
2323
mocked(_t).mockReturnValue("Przeglądaj pokoje");
2424
fetchMock.get("https://home.page", {
25-
body: '<h1>_t("Explore rooms")</h1>',
25+
body: `<h1>_t(${character}Explore rooms${character})</h1>`,
2626
});
2727

2828
const { asFragment } = render(<EmbeddedPage url="https://home.page" />);

test/unit-tests/components/views/context_menus/__snapshots__/EmbeddedPage-test.tsx.snap

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,51 @@ exports[`<EmbeddedPage /> should translate _t strings 1`] = `
4141
</div>
4242
</DocumentFragment>
4343
`;
44+
45+
exports[`<EmbeddedPage /> should translate _t strings 2`] = `
46+
<DocumentFragment>
47+
<div
48+
class="undefined_guest"
49+
>
50+
<div
51+
class="undefined_body"
52+
>
53+
<h1>
54+
Przeglądaj pokoje
55+
</h1>
56+
</div>
57+
</div>
58+
</DocumentFragment>
59+
`;
60+
61+
exports[`<EmbeddedPage /> should translate _t strings 3`] = `
62+
<DocumentFragment>
63+
<div
64+
class="undefined_guest"
65+
>
66+
<div
67+
class="undefined_body"
68+
>
69+
<h1>
70+
Przeglądaj pokoje
71+
</h1>
72+
</div>
73+
</div>
74+
</DocumentFragment>
75+
`;
76+
77+
exports[`<EmbeddedPage /> should translate _t strings 4`] = `
78+
<DocumentFragment>
79+
<div
80+
class="undefined_guest"
81+
>
82+
<div
83+
class="undefined_body"
84+
>
85+
<h1>
86+
Przeglądaj pokoje
87+
</h1>
88+
</div>
89+
</div>
90+
</DocumentFragment>
91+
`;

0 commit comments

Comments
 (0)