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

Commit

Permalink
Escape placeholder before injecting it into the style (#11607)
Browse files Browse the repository at this point in the history
* Escape placeholder before injecting it into the style

In particular this adds escaping for backslashes which was previously missing.

* Update snapshots

* Add tests
  • Loading branch information
Johennes authored Sep 19, 2023
1 parent e9c9377 commit 3fbf38f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/components/views/rooms/BasicMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
};

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ exports[`RoomView for a local room in state NEW should match the snapshot 1`] =
data-testid="basicmessagecomposer"
dir="auto"
role="textbox"
style="--placeholder: 'Send a message…';"
style="--placeholder: 'Send\\ a\\ message…';"
tabindex="0"
translate="no"
>
Expand Down Expand Up @@ -687,7 +687,7 @@ exports[`RoomView for a local room in state NEW that is encrypted should match t
data-testid="basicmessagecomposer"
dir="auto"
role="textbox"
style="--placeholder: 'Send a message…';"
style="--placeholder: 'Send\\ a\\ message…';"
tabindex="0"
translate="no"
>
Expand Down
16 changes: 16 additions & 0 deletions test/components/views/rooms/BasicMessageComposer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ describe("BasicMessageComposer", () => {
const transformedText = model.parts.map((part) => part.text).join("");
expect(transformedText).toBe("/plain foobar\n");
});

it("should escape single quote in placeholder", async () => {
const model = new EditorModel([], pc, renderer);
const composer = render(<BasicMessageComposer placeholder={"Don't"} model={model} room={room} />);
const input = composer.queryAllByRole("textbox");
const placeholder = input[0].style.getPropertyValue("--placeholder");
expect(placeholder).toMatch("'Don\\'t'");
});

it("should escape backslash in placeholder", async () => {
const model = new EditorModel([], pc, renderer);
const composer = render(<BasicMessageComposer placeholder={"w\\e"} model={model} room={room} />);
const input = composer.queryAllByRole("textbox");
const placeholder = input[0].style.getPropertyValue("--placeholder");
expect(placeholder).toMatch("'w\\\\e'");
});
});

function generateMockDataTransferForString(string: string): DataTransfer {
Expand Down

0 comments on commit 3fbf38f

Please sign in to comment.