File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed
Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -164,18 +164,22 @@ export function escapeRegExp(str: string): string {
164164 return str . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
165165}
166166
167- export function escapeHTML ( str : string ) : string {
167+ export function escapeHTML < T extends string | null | undefined > ( str : T ) : T {
168168 if ( str === null || str === undefined ) {
169169 return str ;
170170 }
171- str = str
172- . replace ( / & / g, "&" )
173- . replace ( / < / g, "<" )
174- . replace ( / > / g, ">" )
175- . replace ( / " / g, """ )
176- . replace ( / ' / g, "'" ) ;
177-
178- return str ;
171+
172+ const escapeMap : Record < string , string > = {
173+ "&" : "&" ,
174+ "<" : "<" ,
175+ ">" : ">" ,
176+ '"' : """ ,
177+ "'" : "'" ,
178+ "/" : "/" ,
179+ "`" : "`" ,
180+ } ;
181+
182+ return str . replace ( / [ & < > " ' / ` ] / g, ( char ) => escapeMap [ char ] as string ) as T ;
179183}
180184
181185export function isUsernameValid ( name : string ) : boolean {
You can’t perform that action at this time.
0 commit comments