mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-02-04 09:14:30 +00:00
JS faff #88
This commit is contained in:
25
webassets/js/tools/utils.js
Normal file
25
webassets/js/tools/utils.js
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// 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 '<';
|
||||
case '>': return '>';
|
||||
case '"': return '"';
|
||||
case '\'': return ''';
|
||||
case '`': return '`';
|
||||
default: return match;
|
||||
}
|
||||
};
|
||||
|
||||
return str.replace(/[&<>"'`]/g, escapeCharacter);
|
||||
}
|
||||
Reference in New Issue
Block a user