prototype-3x5/src/helpers.ts

23 lines
457 B
TypeScript
Raw Normal View History

/**
2023-08-05 05:09:33 +00:00
* @param text Potentially dangerous text
* @returns Text safe to embed in HTML
**/
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 "&amp;";
case "<":
return "&lt;";
case ">":
return "&gt;";
case '"':
return "&quot;";
case "'":
return "&#039;";
default:
return char;
}
});
}