ranui
Version:
A framework-agnostic Web Components UI library built on native custom elements, with TypeScript types, light/dark theming, SSR and PWA support.
88 lines (87 loc) • 2.33 kB
JavaScript
import { i as y, o as $ } from "./ssr-DSGwPtEd.js";
function S(t) {
const o = {}, n = /([\w-]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g;
let e;
for (; (e = n.exec(t)) !== null; )
e[1] && (o[e[1]] = e[2] ?? e[3] ?? e[4] ?? "");
return o;
}
function h(t, o, n) {
const e = `<${o}`, r = `</${o}>`;
let c = 1, s = n;
for (; c > 0 && s < t.length; ) {
const i = t.indexOf(e, s), a = t.indexOf(r, s);
if (a === -1) return -1;
if (i !== -1 && i < a) {
const f = t[i + e.length];
if (f === " " || f === ">" || f === "/" || f === `
` || f === " ") {
c++, s = i + e.length;
continue;
}
}
if (c--, c === 0) return a;
s = a + r.length;
}
return -1;
}
function w(t) {
const o = [], n = /<([a-z][a-z0-9]*(?:-[a-z0-9]+)+)/gi;
let e = 0, r;
for (n.lastIndex = 0; (r = n.exec(t)) !== null; ) {
const c = r.index, s = r[1].toLowerCase(), i = c + r[0].length, a = t[i];
if (a !== ">" && a !== " " && a !== `
` && a !== " " && a !== "\r" && a !== "\f") {
n.lastIndex = i;
continue;
}
const f = t.indexOf(">", i);
if (f === -1) break;
const x = t.slice(i, f), l = f + 1, u = h(t, s, l);
if (u === -1) continue;
const T = t.slice(l, u), g = u + `</${s}>`.length;
c > e && o.push({
type: "static",
content: t.slice(e, c)
}), o.push({
type: "custom-element",
tagName: s,
attrs: S(x),
innerHTML: T
}), e = g, n.lastIndex = g;
}
return e < t.length && o.push({
type: "static",
content: t.slice(e)
}), o;
}
async function d(t) {
const o = [];
for await (const n of t) o.push(n);
return o.join("");
}
async function* p(t) {
const o = w(t);
for (const n of o) {
if (n.type === "static") {
yield n.content;
continue;
}
const e = $(n.tagName);
if (!e) {
const c = Object.entries(n.attrs).map(([s, i]) => i === "" ? ` ${s}` : ` ${s}="${i}"`).join("");
yield `<${n.tagName}${c}>${n.innerHTML}</${n.tagName}>`;
continue;
}
const r = new e();
for (const [c, s] of Object.entries(n.attrs)) r.setAttribute(c, s);
n.innerHTML.trim() && (r.innerHTML = await d(p(n.innerHTML))), yield y(r);
}
}
async function C(t) {
return d(p(t));
}
export {
C as renderHTMLToString,
p as renderToStream
};