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

Escape placeholder before injecting it into the style #11607

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ?? "")}'`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.mozilla.org/en-US/docs/Web/API/CSS/escape_static says its intended for use for escaping selectors rather than values, are we sure its the right method to use here?

Copy link
Contributor Author

@Johennes Johennes Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's its primary use. The docs you linked also point at the possible use for escaping general strings:

The escape() method can also be used for escaping strings, although it escapes characters that don't strictly need to be escaped

The over-escaping didn't seem to hurt in my testing and I couldn't find anything better / easier.

I think the alternative would be to extend the custom escaping to also cover backslashes? If you think that's better, I could also try that. Just seemed more complicated and error prone at first sight.

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
Loading