@uva-glass/component-library
Version:
React components UvA
239 lines (238 loc) • 9.43 kB
JavaScript
var M = {}, m = {}, I;
function F() {
if (I) return m;
I = 1, Object.defineProperty(m, "__esModule", { value: !0 }), m.normalizeSpacerunSpans = m.normalizeSpacing = m.parseHtml = m.MSWordNormalizer = void 0;
const h = [
/<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i,
/xmlns:o="urn:schemas-microsoft-com/i
];
class v {
isActive(r) {
return h.some((n) => n.test(r));
}
normalize(r) {
const { bodyString: n, stylesString: s } = A(r), t = new DOMParser().parseFromString(n, "text/html");
return this.transformListItemLikeLElementsIntoLists(t, s), this.removeMSAttributes(t), t.body.innerHTML;
}
findAllItemLikeElements(r) {
const n = r.querySelectorAll("*"), s = [];
for (const t of n) {
const i = t.tagName.toLowerCase();
if (!["p", "h1", "h2", "h3", "h4", "h5", "h6", "li", "div"].includes(i) || !(t instanceof HTMLElement))
continue;
const c = P(t.getAttribute("style") || "");
if ("mso-list" in c) {
if (!t.parentElement || E(t.parentElement))
continue;
const u = j(c);
s.push(Object.assign(Object.assign({}, u), { element: t }));
}
}
return s;
}
transformListItemLikeLElementsIntoLists(r, n) {
var s, t, i, c, u, S, y;
const g = this.findAllItemLikeElements(r);
if (!g.length)
return;
const d = {}, a = [];
for (const l of g)
if (l.indent != null) {
B(l) || (a.length = 0);
const f = `${l.id}:${l.indent}`, o = Math.min(l.indent - 1, a.length);
if (o < a.length && ((s = a[o]) === null || s === void 0 ? void 0 : s.id) !== l.id && (a.length = o), o < a.length - 1)
a.length = o + 1;
else {
const L = D(l, n);
if (o > a.length - 1 || ((t = a[o]) === null || t === void 0 ? void 0 : t.listElement.tagName.toLowerCase()) != L.type) {
o == 0 && L.type == "ol" && l.id !== void 0 && d[f] && (L.startIndex = (i = d[f]) !== null && i !== void 0 ? i : null);
const x = $(L);
if (a.length == 0) {
const p = l.element.parentNode;
Array.from(p.children).indexOf(l.element) + 1 === p.children.length ? p.appendChild(x) : p.insertBefore(x, l.element);
} else {
const p = (c = a[o - 1]) === null || c === void 0 ? void 0 : c.listItemElements;
p && ((u = p[p.length - 1]) === null || u === void 0 || u.appendChild(x));
}
a[o] = Object.assign(Object.assign({}, l), { listElement: x, listItemElements: [] }), o == 0 && l.id !== void 0 && (d[f] = L.startIndex || 1);
}
}
const b = l.element.tagName.toLowerCase() == "li" ? l.element : document.createElement("li");
(S = a[o]) === null || S === void 0 || S.listElement.appendChild(b), (y = a[o]) === null || y === void 0 || y.listItemElements.push(b), o == 0 && l.id !== void 0 && d[f]++, l.element != b && b.appendChild(l.element), k(l.element);
}
}
removeMSAttributes(r) {
const n = r.querySelectorAll("*"), s = [];
for (const t of n) {
for (const i of t.classList)
/\bmso/gi.exec(i) && t.classList.remove(i);
t instanceof HTMLElement && R(t), (t.tagName === "W:SDT" || t.tagName === "W:SDTPR" && w(t) || t.tagName === "O:P" && w(t)) && s.push(t);
}
for (const t of s)
t.parentNode.removeChild(t);
}
}
m.MSWordNormalizer = v;
function A(e) {
const r = new DOMParser();
e = e.replace(/<!--\[if gte vml 1]>/g, ""), e = e.replace(/<o:SmartTagType(?:\s+[^\s>=]+(?:="[^"]*")?)*\s*\/?>/gi, "");
const n = N(O(e)), s = r.parseFromString(n, "text/html");
T(s);
const t = s.body.innerHTML, i = _(s);
return {
bodyString: t,
stylesString: i
};
}
m.parseHtml = A;
function _(e) {
const r = [], n = [], s = Array.from(e.getElementsByTagName("style"));
for (const t of s)
t.sheet && t.sheet.cssRules && t.sheet.cssRules.length && (r.push(t.sheet), n.push(t.innerHTML));
return n.join(" ");
}
function N(e) {
return C(C(e)).replace(/(<span\s+style=['"]mso-spacerun:yes['"]>[^\S\r\n]*?)[\r\n]+([^\S\r\n]*<\/span>)/g, "$1$2").replace(/<span\s+style=['"]mso-spacerun:yes['"]><\/span>/g, "").replace(/(<span\s+style=['"]letter-spacing:[^'"]+?['"]>)[\r\n]+(<\/span>)/g, "$1 $2").replace(/ <\//g, " </").replace(/ <o:p><\/o:p>/g, " <o:p></o:p>").replace(/<o:p>( |\u00A0)<\/o:p>/g, "").replace(/>([^\S\r\n]*[\r\n]\s*)</g, "><");
}
m.normalizeSpacing = N;
function C(e) {
return e.replace(/<span(?: class="Apple-converted-space"|)>(\s+)<\/span>/g, (r, n) => n.length === 1 ? " " : Array(n.length + 1).join(" ").substring(0, n.length));
}
function T(e) {
e.querySelectorAll("span[style*=spacerun]").forEach((r) => {
const n = r, s = n.innerText.length || 0;
n.innerText = Array(s + 1).join(" ").substring(0, s);
});
}
m.normalizeSpacerunSpans = T;
function O(e) {
const r = "</body>", n = "</html>", s = e.indexOf(r);
if (s < 0)
return e;
const t = e.indexOf(n, s + r.length);
return e.substring(0, s + r.length) + (t >= 0 ? e.substring(t) : "");
}
function P(e) {
const r = {};
return e && e.split(";").map((s) => s.trim()).forEach((s) => {
const [t, i] = s.split(":").map((c) => c.trim());
t && i && (r[t] = i);
}), r;
}
function w(e) {
return !e.textContent && !e.innerHTML.trim();
}
function R(e) {
const t = (e.getAttribute("style") || "").split(";").map((i) => i.trim()).filter((i) => {
var c;
const [u] = i.split(":");
return !/\bmso/gi.exec((c = u == null ? void 0 : u.toLowerCase()) !== null && c !== void 0 ? c : "");
}).join(";");
e.setAttribute("style", t);
}
function j(e) {
var r;
const n = e["mso-list"];
if (n === void 0)
return {};
const s = n.match(/(^|\s{1,100})l(\d+)/i), t = n.match(/\s{0,100}lfo(\d+)/i), i = n.match(/\s{0,100}level(\d+)/i);
return s && t && i ? {
id: s[2],
order: t[1],
indent: parseInt((r = i[1]) !== null && r !== void 0 ? r : "")
} : {
indent: 1
};
}
function D(e, r) {
const n = new RegExp(`@list l${e.id}:level${e.indent}\\s*({[^}]*)`, "gi"), s = /mso-level-number-format:([^;]{0,100});/gi, t = /mso-level-start-at:\s{0,100}([0-9]{0,10})\s{0,100};/gi, i = new RegExp(`@list\\s+l${e.id}:level\\d\\s*{[^{]*mso-level-text:"%\\d\\\\.`, "gi"), c = new RegExp(`@list l${e.id}:level\\d\\s*{[^{]*mso-level-number-format:`, "gi"), u = i.exec(r), S = c.exec(r), y = u && !S, g = n.exec(r);
let d = "decimal", a = "ol", l = null;
if (g && g[1]) {
const f = s.exec(g[1]);
if (f && f[1] && (d = f[1].trim(), a = d !== "bullet" && d !== "image" ? "ol" : "ul"), d === "bullet") {
const o = H(e.element);
o && (d = o);
} else {
const o = t.exec(g[1]);
o && o[1] && (l = parseInt(o[1]));
}
y && (a = "ol");
}
return {
type: a,
startIndex: l,
style: W(d),
isLegalStyleList: y
};
}
function H(e) {
if (e.tagName.toLowerCase() == "li" && e.parentElement.tagName.toLowerCase() == "ul" && e.parentElement.hasAttribute("type"))
return e.parentElement.getAttribute("type");
const r = q(e);
if (!r)
return null;
const n = r.textContent;
return n === "o" ? "circle" : (n ?? "").charCodeAt(0) === 183 ? "disc" : n === "§" ? "square" : null;
}
function q(e) {
var r;
if (((r = Array.from(e.children)[0]) === null || r === void 0 ? void 0 : r.tagName.toLowerCase()) === "#text")
return null;
for (const n of Array.from(e.children))
if (["element", "span"].includes(n.tagName.toLowerCase()))
return n;
return null;
}
function W(e) {
if (e.startsWith("arabic-leading-zero"))
return "decimal-leading-zero";
switch (e) {
case "alpha-upper":
return "upper-alpha";
case "alpha-lower":
return "lower-alpha";
case "roman-upper":
return "upper-roman";
case "roman-lower":
return "lower-roman";
case "circle":
case "disc":
case "square":
return e;
default:
return null;
}
}
function $(e, r) {
const n = document.createElement(e.type);
return e.style && n.style.setProperty("list-style-type", e.style), e.startIndex && e.startIndex > 1 && n.setAttribute("start", e.startIndex.toString()), n;
}
function k(e) {
e.querySelectorAll("span").forEach((n) => {
var s;
const t = n.getAttribute("style");
t && t.includes("mso-list:Ignore") && ((s = n.parentNode) === null || s === void 0 || s.removeChild(n));
});
}
function B(e) {
const r = e.element.previousSibling;
return E(r || e.element.parentElement);
}
const E = (e) => ["ul", "ol"].includes(e.tagName.toLowerCase());
return m;
}
var z;
function U() {
return z || (z = 1, function(h) {
Object.defineProperty(h, "__esModule", { value: !0 }), h.MSWordNormalizer = void 0;
const v = F();
Object.defineProperty(h, "MSWordNormalizer", { enumerable: !0, get: function() {
return v.MSWordNormalizer;
} });
}(M)), M;
}
var J = U();
export {
J as d
};
//# sourceMappingURL=index-B3c5pcHk.js.map