prototype-3x5/src/helpers.ts

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 "&amp;";
case "<":
return "&lt;";
case ">":
return "&gt;";
case '"':
return "&quot;";
case "'":
return "&#039;";
default:
return char;
}
});
}