22 lines
457 B
TypeScript
22 lines
457 B
TypeScript
/**
|
|
* @param text Potentially dangerous text
|
|
* @returns Text safe to embed in HTML
|
|
**/
|
|
export function escapeHtml(text: string): string {
|
|
return text.replace(/[&<>"']/g, (char) => {
|
|
switch (char) {
|
|
case "&":
|
|
return "&";
|
|
case "<":
|
|
return "<";
|
|
case ">":
|
|
return ">";
|
|
case '"':
|
|
return """;
|
|
case "'":
|
|
return "'";
|
|
default:
|
|
return char;
|
|
}
|
|
});
|
|
}
|