2023-07-27 02:14:16 +00:00
|
|
|
/**
|
2023-08-05 05:09:33 +00:00
|
|
|
* @param text Potentially dangerous text
|
|
|
|
* @returns Text safe to embed in HTML
|
2023-07-27 02:14:16 +00:00
|
|
|
**/
|
2023-08-05 05:09:33 +00:00
|
|
|
export function escapeHtml(text: string): string {
|
2023-08-06 06:38:42 +00:00
|
|
|
return text.replace(/[&<>"']/g, (char) => {
|
|
|
|
switch (char) {
|
|
|
|
case "&":
|
|
|
|
return "&";
|
|
|
|
case "<":
|
|
|
|
return "<";
|
|
|
|
case ">":
|
|
|
|
return ">";
|
|
|
|
case '"':
|
|
|
|
return """;
|
|
|
|
case "'":
|
|
|
|
return "'";
|
|
|
|
default:
|
|
|
|
return char;
|
|
|
|
}
|
|
|
|
});
|
2023-07-27 02:14:16 +00:00
|
|
|
}
|