The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/lib/escapeHTML.ts

27 lines
706 B

const characterToHtmlEntityCode = {
'¢': 'cent',
'£': 'pound',
'¥': 'yen',
'€': 'euro',
'©': 'copy',
'®': 'reg',
'<': 'lt',
'>': 'gt',
'"': 'quot',
'&': 'amp',
'\'': '#39',
} as const;
const regex = new RegExp(`[${ Object.keys(characterToHtmlEntityCode).join('') }]`, 'g');
const toString = (object: unknown): string =>
(object ? `${ object }` : '');
const isEscapable = (char: string): char is keyof typeof characterToHtmlEntityCode =>
char in characterToHtmlEntityCode;
const escapeChar = (char: string): string =>
(isEscapable(char) ? `&${ characterToHtmlEntityCode[char] };` : '');
export const escapeHTML = (str: string): string =>
toString(str).replace(regex, escapeChar);