Skip to content

Commit 2a147f5

Browse files
authored
fix: paste html (#1855)
1 parent 2cb38ad commit 2a147f5

File tree

1 file changed

+16
-5
lines changed
  • frontend/src/components/chat/MessageComposer

1 file changed

+16
-5
lines changed

frontend/src/components/chat/MessageComposer/Input.tsx

+16-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ export interface InputMethods {
3434
reset: () => void;
3535
}
3636

37+
const escapeHtml = (unsafe: string) => {
38+
return unsafe
39+
.replace(/&/g, '&')
40+
.replace(/</g, '&lt;')
41+
.replace(/>/g, '&gt;')
42+
.replace(/"/g, '&quot;')
43+
.replace(/'/g, '&#039;');
44+
};
45+
3746
const Input = forwardRef<InputMethods, Props>(
3847
(
3948
{
@@ -220,18 +229,20 @@ const Input = forwardRef<InputMethods, Props>(
220229
const _onPaste = (event: ClipboardEvent) => {
221230
event.preventDefault();
222231

223-
const text = event.clipboardData
224-
?.getData('text/plain')
225-
.replace(/\n/g, '<br>');
226-
if (text) {
232+
const textData = event.clipboardData?.getData('text/plain');
233+
234+
if (textData) {
235+
const escapedText = escapeHtml(textData);
236+
const textWithNewLines = escapedText.replace(/\n/g, '<br>');
237+
227238
const selection = window.getSelection();
228239
if (selection?.rangeCount) {
229240
const range = selection.getRangeAt(0);
230241
range.deleteContents();
231242

232243
// Insert the HTML content
233244
const tempDiv = document.createElement('div');
234-
tempDiv.innerHTML = text;
245+
tempDiv.innerHTML = textWithNewLines;
235246
const fragment = document.createDocumentFragment();
236247
while (tempDiv.firstChild) {
237248
fragment.appendChild(tempDiv.firstChild);

0 commit comments

Comments
 (0)