24 lines
507 B
JavaScript
24 lines
507 B
JavaScript
// HTML (and XML) text/attribute escaping.
|
|
|
|
const HTML_ESCAPES = {
|
|
"&": "&",
|
|
"<": "<",
|
|
">": ">",
|
|
"\"": """,
|
|
"'": "'"
|
|
};
|
|
const HTML_SPECIALS = /[&<>"']/g;
|
|
|
|
|
|
// value as text safe inside an element or a quoted attribute; null/undefined -> "".
|
|
const escapeHtml = (value) => {
|
|
if (value === null || value === undefined) {
|
|
return "";
|
|
}
|
|
return String(value).replace(HTML_SPECIALS, (ch) => HTML_ESCAPES[ch]);
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
escapeHtml
|
|
};
|