Self-hosted AI workspace.
Python
perf(ui): hoist esc() lookup table and build option lists once (#160)
chrisdvz.ioad38492894be
diff --git a/static/js/cookbook-hwfit.js b/static/js/cookbook-hwfit.jsindex 3d438aa19..6817d15cb 100644--- a/static/js/cookbook-hwfit.js+++ b/static/js/cookbook-hwfit.js@@ -1094,12 +1094,13 @@ export function _hwfitInit() {for (const sel of selectors) {if (!sel) continue;const currentVal = sel.value;- sel.innerHTML = `<option value="local">Local</option>`;+ let html = `<option value="local">Local</option>`;_envState.servers.forEach((s, i) => {if (!s.host) return;const label = s.name || s.host || `Server ${i + 1}`;- sel.innerHTML += `<option value="${i}">${uiModule.esc(label)}</option>`;+ html += `<option value="${i}">${uiModule.esc(label)}</option>`;});+ sel.innerHTML = html;sel.value = currentVal;}}diff --git a/static/js/group.js b/static/js/group.jsindex 4914d76e1..ed98872d3 100644--- a/static/js/group.js+++ b/static/js/group.js@@ -487,10 +487,11 @@ export async function showModelPicker() {`;const sel = document.createElement('select');sel.style.cssText = 'font-size:11px;padding:3px 6px;border-radius:4px;border:1px solid var(--border);background:var(--bg);color:var(--fg);max-width:140px;';- sel.innerHTML = '<option value="">No character</option>';+ let optsHtml = '<option value="">No character</option>';characters.forEach(c => {- sel.innerHTML += `<option value="${c.id}">${uiModule.esc(c.name)}</option>`;+ optsHtml += `<option value="${c.id}">${uiModule.esc(c.name)}</option>`;});+ sel.innerHTML = optsHtml;sel.addEventListener('change', () => {if (sel.value) {const ch = characters.find(c => c.id === sel.value);diff --git a/static/js/ui.js b/static/js/ui.jsindex 3142790e2..5af1d2c2d 100644--- a/static/js/ui.js+++ b/static/js/ui.js@@ -516,14 +516,15 @@ export function styledPrompt(message, {});}+// Lookup table for esc(); hoisted out of the replace callback so it is+// allocated once rather than per matched character.+const _ESC_MAP = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };/*** HTML-escape a string to prevent XSS.* Canonical implementation — other modules should use uiModule.esc() instead of local copies.*/export function esc(s) {- return (s || '').replace(/[&<>"']/g, function(m) {- return {'&':'&','<':'<','>':'>','"':'"',"'":'''}[m];- });+ return (s || '').replace(/[&<>"']/g, (m) => _ESC_MAP[m]);}// ── Mobile: suppress synthetic click/mousedown on backdrop ──