52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
/**
|
|
* Hoover — helpers.js
|
|
*
|
|
* Shared utilities: text escaping, attribute escaping, DOM value helpers,
|
|
* zone parsing, form utilities.
|
|
*/
|
|
|
|
/**
|
|
* Escape text for safe HTML output.
|
|
* Appends the string to a temporary div and reads innerHTML,
|
|
* which safely escapes all HTML special characters.
|
|
*/
|
|
export function esc(s) {
|
|
const d = document.createElement('div');
|
|
d.append(String(s ?? ''));
|
|
return d.innerHTML;
|
|
}
|
|
|
|
/**
|
|
* Escape a string for safe use in HTML attributes.
|
|
*/
|
|
export function att_esc(s) {
|
|
return String(s ?? '')
|
|
.replace(/&/g, '&')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>');
|
|
}
|
|
|
|
/**
|
|
* URL-encode a string.
|
|
*/
|
|
export const enc = encodeURIComponent;
|
|
|
|
/**
|
|
* Get the value of a DOM element by ID.
|
|
*/
|
|
export function $val(id) {
|
|
return document.getElementById(id)?.value;
|
|
}
|
|
|
|
/**
|
|
* Parse zone data from various API response shapes into a flat string array.
|
|
*/
|
|
export function parseZones(data) {
|
|
let z = data?.active || data?.zones || [];
|
|
if (typeof z === 'object' && !Array.isArray(z))
|
|
z = Object.values(z).map(i => i?.name || i);
|
|
return Array.isArray(z) ? z : [];
|
|
}
|