16 lines
356 B
JavaScript
16 lines
356 B
JavaScript
export function escapeHTML(str) {
|
|
if (typeof str !== 'string') return '';
|
|
return str.replace(/[&<>"']/g, function(c) {
|
|
return {
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
'"': '"',
|
|
'\'': '''
|
|
} [c]
|
|
})
|
|
}
|
|
export function textToHTML(text) {
|
|
if (typeof text !== 'string') return '';
|
|
return escapeHTML(text).replace(/\n/g, '<br>')
|
|
} |