Internalise third-party dependencies

(cherry picked from commit 725eb619b4)
This commit is contained in:
Ian Renton
2026-06-18 20:07:42 +01:00
parent 4408203d55
commit 88f055384d
47 changed files with 4771 additions and 49 deletions

33
webassets/js/utils.js Normal file
View File

@@ -0,0 +1,33 @@
//
// GENERAL UTILITY FUNCTIONS
// OBject, 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);
}
// Converts an HTML hex colour to an array of [R, G, B] where each is 0-255.
function hexToRGB(hex) {
return hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i
,(m, r, g, b) => '#' + r + r + g + g + b + b)
.substring(1).match(/.{2}/g)
.map(x => parseInt(x, 16));
}