/** * Escapes characters for use in HTML. * * The backtick is escaped along with the quotes. It is not special in HTML * itself, but it ends a template literal, and escaped text is often dropped * into one on the way to the page. Older IE also accepted it as an attribute * delimiter, which is the form the OWASP guidance is written against. * * This escapes text for an element body or a quoted attribute. It is not * enough on its own inside a `') * //=> '<script>alert("xss")</script>' */ export default function escapeHTML(str: string): string { const map: Record = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '`': '`', }; return str.replace(/[&<>"'`]/g, (m) => map[m] as string); }