import { h as h2 } from "https://esm.town/v/tmcw/h";
export function h(tagName: string, attrsPlus?: any, ...children: any[]) {
if (!attrsPlus) attrsPlus = {};
if (attrsPlus.isEl || typeof attrsPlus === "string")
return h2(tagName, {}, attrsPlus, ...children);
if (tagName.includes("."))
return h2(
tagName.split(".")[0],
{ ...attrsPlus, class: tagName.split(".").slice(1).join(" ") },
...children
);
const camelToKebab = (a: string) =>
a
.split("")
.map((x) => (x === x.toUpperCase() ? "-" + x.toLowerCase() : x))
.join("");
const text2Html = (text: string) =>
text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
let innerHtml = children
.map((x) => (typeof x === "string" ? x : x.html))
.join("");
const attrsToEmit = Object.entries(attrsPlus).reduce((acc: any, [k, v]) => {
if (k.startsWith("on") && typeof v === "function") return acc;
if (v === null || v === undefined) return acc;
if (typeof v === "function") v = v();
if (k === "innerHTML") innerHtml = String(v);
else if (k === "innerText") innerHtml = text2Html(String(v));
else if (k[0] === "." && v)
acc.class = ((acc.class || "") + " " + k.slice(1)).trim();
else if (k[0] === "$" && v !== null && v !== undefined)
acc.style =
`${camelToKebab(k.slice(1))}: ${v}` +
(acc.style ? "; " + acc.style : "");
else acc[k] = v;
return acc;
}, {});
const needsEnding =
!"base br col hr img input link meta param command keygen source"
.split(" ")
.includes(tagName);
const attrsHtml = Object.entries(attrsToEmit)
.map(([k, v]) => `${k}=${JSON.stringify(v)}`)
.join(" ");
return {
isEl: true,
html: [
"<",
tagName,
attrsHtml ? " " + attrsHtml : "",
">",
innerHtml,
needsEnding ? `</${tagName}>` : "",
].join(""),
};
}