@@ -34,6 +34,15 @@ export interface InputMethods {
34
34
reset : ( ) => void ;
35
35
}
36
36
37
+ const escapeHtml = ( unsafe : string ) => {
38
+ return unsafe
39
+ . replace ( / & / g, '&' )
40
+ . replace ( / < / g, '<' )
41
+ . replace ( / > / g, '>' )
42
+ . replace ( / " / g, '"' )
43
+ . replace ( / ' / g, ''' ) ;
44
+ } ;
45
+
37
46
const Input = forwardRef < InputMethods , Props > (
38
47
(
39
48
{
@@ -220,18 +229,20 @@ const Input = forwardRef<InputMethods, Props>(
220
229
const _onPaste = ( event : ClipboardEvent ) => {
221
230
event . preventDefault ( ) ;
222
231
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
+
227
238
const selection = window . getSelection ( ) ;
228
239
if ( selection ?. rangeCount ) {
229
240
const range = selection . getRangeAt ( 0 ) ;
230
241
range . deleteContents ( ) ;
231
242
232
243
// Insert the HTML content
233
244
const tempDiv = document . createElement ( 'div' ) ;
234
- tempDiv . innerHTML = text ;
245
+ tempDiv . innerHTML = textWithNewLines ;
235
246
const fragment = document . createDocumentFragment ( ) ;
236
247
while ( tempDiv . firstChild ) {
237
248
fragment . appendChild ( tempDiv . firstChild ) ;
0 commit comments