Export xsl into seperate file via embedding

This commit is contained in:
Simon Martens
2024-12-28 10:53:17 +01:00
parent 58df7cc1cb
commit 579ce63ee2
8 changed files with 152 additions and 73 deletions

View File

@@ -1,35 +1,50 @@
const p = "[xslt-onload]", T = "xslt-template", i = "xslt-transformed";
function a(c) {
let o = document.querySelectorAll(p);
for (let e of o) {
if (e.getAttribute(i) === "true")
continue;
let r = e.getAttribute(T), t = htmx.find("#" + r);
if (t) {
let l = t.innerHTML ? new DOMParser().parseFromString(t.innerHTML, "application/xml") : t.contentDocument, s = new XSLTProcessor();
s.importStylesheet(l);
let n = new DOMParser().parseFromString(e.innerHTML, "application/xml"), m = s.transformToFragment(n, document), u = new XMLSerializer().serializeToString(m);
e.innerHTML = u, e.setAttribute(i, !0);
} else
throw new Error("Unknown XSLT template: " + r);
}
const u = "[xslt-onload]", f = "xslt-template", a = "xslt-transformed", c = "xslt-remote-template";
function h() {
let t = htmx.findAll(u);
for (let n of t)
d(n);
}
function d() {
a(), htmx.on("htmx:load", a), document.querySelectorAll("template[simple]").forEach((o) => {
let e = o.getAttribute("id"), r = o.content;
function d(t) {
if (t.getAttribute(a) === "true")
return;
let n = t.getAttribute(f), o = htmx.find("#" + n);
if (o) {
let s = o.innerHTML ? new DOMParser().parseFromString(o.innerHTML, "application/xml") : o.contentDocument;
console.log(s);
let l = new XSLTProcessor();
l.importStylesheet(s);
let e = new DOMParser().parseFromString(t.innerHTML, "application/xml"), i = l.transformToFragment(e, document), r = new XMLSerializer().serializeToString(i);
t.innerHTML = r, t.setAttribute(a, !0);
} else if (t.hasAttribute(c)) {
let s = t.getAttribute(c), l = new Request(s, {
headers: { "Content-Type": "application/xslt+xml" },
cache: "default"
});
fetch(l).then((e) => e.text()).then((e) => {
let i = new DOMParser().parseFromString(e, "application/xslt+xml"), r = new XSLTProcessor();
r.importStylesheet(i);
let p = new DOMParser().parseFromString(t.innerHTML, "application/xml"), T = r.transformToFragment(p, document), m = new XMLSerializer().serializeToString(T);
t.innerHTML = m, t.setAttribute(a, !0);
});
} else
throw new Error("Unknown XSLT template: " + n);
}
function S() {
h(), document.querySelectorAll("template[simple]").forEach((n) => {
let o = n.getAttribute("id"), s = n.content;
customElements.define(
e,
o,
class extends HTMLElement {
constructor() {
super(), this.appendChild(r.cloneNode(!0)), this.slots = this.querySelectorAll("slot");
super(), this.appendChild(s.cloneNode(!0)), this.slots = this.querySelectorAll("slot");
}
connectedCallback() {
let t = [];
this.slots.forEach((l) => {
let s = l.getAttribute("name"), n = this.querySelector(`[slot="${s}"]`);
n && (l.replaceWith(n.cloneNode(!0)), t.push(n));
}), t.forEach((l) => {
l.remove();
let l = [];
this.slots.forEach((e) => {
let i = e.getAttribute("name"), r = this.querySelector(`[slot="${i}"]`);
r && (e.replaceWith(r.cloneNode(!0)), l.push(r));
}), l.forEach((e) => {
e.remove();
});
}
}
@@ -37,5 +52,5 @@ function d() {
});
}
export {
d as setup
S as setup
};