mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-12 13:33:46 +08:00
24 lines
719 B
TypeScript
24 lines
719 B
TypeScript
export function el(tag: string, attrs: Record<string, string | null> = {}, children: (string | HTMLElement)[] = []): HTMLElement {
|
|
const el = document.createElement(tag);
|
|
const attrKeys = Object.keys(attrs);
|
|
for (const attr of attrKeys) {
|
|
if (attrs[attr] !== null) {
|
|
el.setAttribute(attr, attrs[attr] as string);
|
|
}
|
|
}
|
|
|
|
for (const child of children) {
|
|
if (typeof child === 'string') {
|
|
el.append(document.createTextNode(child));
|
|
} else {
|
|
el.append(child);
|
|
}
|
|
}
|
|
|
|
return el;
|
|
}
|
|
|
|
export function htmlToDom(html: string): Document {
|
|
const parser = new DOMParser();
|
|
return parser.parseFromString(html, 'text/html');
|
|
} |