tarinai/js/ui_helpers.js

33 lines
1.1 KiB
JavaScript
Raw Normal View History

2026-06-22 02:03:19 +09:00
"use strict";
(function (global) {
function htmlEscape(value = "") {
return String(value ?? "").replace(/[&<>"']/g, ch => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", "\"": "&quot;", "'": "&#39;" }[ch] || ch));
}
function ensureStyle(id, cssText = "") {
if (!id) return null;
let style = document.getElementById(id);
if (style) return style;
style = document.createElement("style");
style.id = id;
style.textContent = cssText;
document.head.appendChild(style);
return style;
}
function createPanel({ id, className = "", html = "", after = null, parent = document.body } = {}) {
if (!id) return null;
let panel = document.getElementById(id);
if (panel) return panel;
panel = document.createElement("section");
panel.id = id;
if (className) panel.className = className;
panel.innerHTML = html;
if (after?.parentNode) after.parentNode.insertBefore(panel, after.nextSibling);
else parent.appendChild(panel);
return panel;
}
global.TarinaiUIHelpers = { htmlEscape, ensureStyle, createPanel };
})(typeof window !== "undefined" ? window : globalThis);