edgerender
Version:
Render at the edge
22 lines (19 loc) • 710 B
text/typescript
// TODO could use the approach in https://github.com/component/escape-html/blob/master/index.js
// but not sure it would be faster
const html_tags: Record<string, string> = {'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}
const replace_html = (letter: string): string => html_tags[letter] || letter
export default class HtmlEscape {
static content(s: string | undefined): string {
if (typeof s == 'string') {
return s.replace(/[&<>]/g, replace_html)
} else {
return ''
}
}
static attr_double(s: string): string {
return s.replace(/[&<>"]/g, replace_html)
}
static attr_single(s: string): string {
return s.replace(/[&<>']/g, replace_html)
}
}