mirror of
https://github.com/BookStackApp/BookStack.git
synced 2026-01-03 23:42:28 +03:00
Lexical: Split helpers to utils, refactored files
This commit is contained in:
24
resources/js/wysiwyg/utils/dom.ts
Normal file
24
resources/js/wysiwyg/utils/dom.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
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');
|
||||
}
|
||||
Reference in New Issue
Block a user