44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
export function escapeHtml(value: string): string {
|
|
if (!value) return ''
|
|
return String(value).replace(/[&<>"']/g, c => {
|
|
switch (c) {
|
|
case '&': return '&'
|
|
case '<': return '<'
|
|
case '>': return '>'
|
|
case '"': return '"'
|
|
case "'": return '''
|
|
default: return c
|
|
}
|
|
})
|
|
}
|
|
|
|
export function escapeAttribute(value: string): string {
|
|
if (!value) return ''
|
|
return String(value).replace(/[&<>"'`]/g, c => {
|
|
switch (c) {
|
|
case '&': return '&'
|
|
case '<': return '<'
|
|
case '>': return '>'
|
|
case '"': return '"'
|
|
case "'": return '''
|
|
case '`': return '`'
|
|
default: return c
|
|
}
|
|
})
|
|
}
|
|
|
|
export function renderMultilineText(value: string): string {
|
|
return escapeHtml(value).replace(/\r?\n/g, '<br />')
|
|
}
|
|
|
|
export function decodeHtmlEntities(value: string): string {
|
|
if (!value) return ''
|
|
return String(value)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, "'")
|
|
.replace(/`/g, '`')
|
|
}
|