Files
spothole/webassets/js/tools/utils.js
Ian Renton 3f117a47d6 JS faff #88
2025-12-31 10:14:52 +00:00

25 lines
617 B
JavaScript

//
// GENERAL UTILITY FUNCTIONS
// String manipulation etc.
//
// Utility function to escape HTML characters from a string.
function escapeHtml(str) {
if (typeof str !== 'string') {
return '';
}
const escapeCharacter = (match) => {
switch (match) {
case '&': return '&';
case '<': return '&lt;';
case '>': return '&gt;';
case '"': return '&quot;';
case '\'': return '&#039;';
case '`': return '&#096;';
default: return match;
}
};
return str.replace(/[&<>"'`]/g, escapeCharacter);
}