Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 3fbf38f

Browse files
authored
Escape placeholder before injecting it into the style (#11607)
* Escape placeholder before injecting it into the style In particular this adds escaping for backslashes which was previously missing. * Update snapshots * Add tests
1 parent e9c9377 commit 3fbf38f

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/components/views/rooms/BasicMessageComposer.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,7 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
282282
};
283283

284284
private showPlaceholder(): void {
285-
// escape single quotes
286-
const placeholder = this.props.placeholder?.replace(/'/g, "\\'");
287-
this.editorRef.current?.style.setProperty("--placeholder", `'${placeholder}'`);
285+
this.editorRef.current?.style.setProperty("--placeholder", `'${CSS.escape(this.props.placeholder ?? "")}'`);
288286
this.editorRef.current?.classList.add("mx_BasicMessageComposer_inputEmpty");
289287
}
290288

test/components/structures/__snapshots__/RoomView-test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ exports[`RoomView for a local room in state NEW should match the snapshot 1`] =
446446
data-testid="basicmessagecomposer"
447447
dir="auto"
448448
role="textbox"
449-
style="--placeholder: 'Send a message…';"
449+
style="--placeholder: 'Send\\ a\\ message…';"
450450
tabindex="0"
451451
translate="no"
452452
>
@@ -687,7 +687,7 @@ exports[`RoomView for a local room in state NEW that is encrypted should match t
687687
data-testid="basicmessagecomposer"
688688
dir="auto"
689689
role="textbox"
690-
style="--placeholder: 'Send a message…';"
690+
style="--placeholder: 'Send\\ a\\ message…';"
691691
tabindex="0"
692692
translate="no"
693693
>

test/components/views/rooms/BasicMessageComposer-test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ describe("BasicMessageComposer", () => {
9494
const transformedText = model.parts.map((part) => part.text).join("");
9595
expect(transformedText).toBe("/plain foobar\n");
9696
});
97+
98+
it("should escape single quote in placeholder", async () => {
99+
const model = new EditorModel([], pc, renderer);
100+
const composer = render(<BasicMessageComposer placeholder={"Don't"} model={model} room={room} />);
101+
const input = composer.queryAllByRole("textbox");
102+
const placeholder = input[0].style.getPropertyValue("--placeholder");
103+
expect(placeholder).toMatch("'Don\\'t'");
104+
});
105+
106+
it("should escape backslash in placeholder", async () => {
107+
const model = new EditorModel([], pc, renderer);
108+
const composer = render(<BasicMessageComposer placeholder={"w\\e"} model={model} room={room} />);
109+
const input = composer.queryAllByRole("textbox");
110+
const placeholder = input[0].style.getPropertyValue("--placeholder");
111+
expect(placeholder).toMatch("'w\\\\e'");
112+
});
97113
});
98114

99115
function generateMockDataTransferForString(string: string): DataTransfer {

0 commit comments

Comments
 (0)