sc-site-sync/lib/common/htmlEscape.js
2026-10-19 01:25:16 -05:00

24 lines
507 B
JavaScript

// HTML (and XML) text/attribute escaping.
const HTML_ESCAPES = {
"&": "&",
"<": "&lt;",
">": "&gt;",
"\"": "&quot;",
"'": "&#39;"
};
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
};