// Tiny UI helpers. `html` is a tagged template that escapes every interpolated value unless it
// was produced by `html` itself (or wrapped in `raw`). Research text comes from the web, so
// escaping by default is the line of defence against markup injection.
const ESC = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
class Safe {
constructor(s) { this.s = s; }
toString() { return this.s; }
}
export const raw = (s) => new Safe(String(s));
export const esc = (v) => String(v ?? '').replace(/[&<>"']/g, (c) => ESC[c]);
function piece(v) {
if (v == null || v === false) return '';
if (v instanceof Safe) return v.s;
if (Array.isArray(v)) return v.map(piece).join('');
return esc(v);
}
export function html(strings, ...values) {
let out = strings[0];
for (let i = 0; i < values.length; i++) out += piece(values[i]) + strings[i + 1];
return new Safe(out);
}
/** Only http(s) links survive; anything else (javascript:, data:) becomes "#". */
export function safeUrl(u) {
try {
const url = new URL(u);
return url.protocol === 'https:' || url.protocol === 'http:' ? url.href : '#';
} catch {
return '#';
}
}
export const extLink = (url, label) =>
html`${label}`;
/** Score bar 0-10 (no inline styles: CSP forbids them, so widths are classes). */
export function bar(v, tone = '') {
if (v == null) return html`–`;
const w = Math.max(0, Math.min(10, Math.round(v)));
return html`${Math.round(v * 10) / 10}`;
}
let toastTimer;
/** Show a short message; `action` = { label, run } adds a button (e.g. Undo). */
export function toast(message, action) {
const el = document.getElementById('toast');
el.replaceChildren();
const span = document.createElement('span');
span.textContent = message;
el.append(span);
if (action) {
const btn = document.createElement('button');
btn.type = 'button';
btn.textContent = action.label;
btn.addEventListener('click', () => { el.classList.remove('show'); action.run(); }, { once: true });
el.append(btn);
}
el.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(() => el.classList.remove('show'), action ? 5000 : 2600);
}
/** Random id that works without a secure context (crypto.randomUUID needs HTTPS). */
export function newId() {
const bytes = crypto.getRandomValues(new Uint8Array(16));
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
/** localStorage that never throws (private mode, blocked storage). */
export const local = {
get(key, fallback) {
try {
const v = localStorage.getItem(`elorasmus.${key}`);
return v == null ? fallback : JSON.parse(v);
} catch {
return fallback;
}
},
set(key, value) {
try {
localStorage.setItem(`elorasmus.${key}`, JSON.stringify(value));
} catch { /* storage unavailable: state just won't survive a reload */ }
},
};
export const ICONS = {
duel: raw(''),
rank: raw(''),
triage: raw(''),
settings: raw(''),
back: raw(''),
undo: raw(''),
skip: raw(''),
};