devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
44 lines (43 loc) • 1.42 kB
JavaScript
let shadowRoot;
if (document.readyState === 'loading')
document.addEventListener('DOMContentLoaded', createShadowRoot);
else
createShadowRoot();
export function getHTMLElementsFromHtml(html) {
const fragment = document.createDocumentFragment();
if (typeof html === "string") {
if (html !== '') {
const doc = new DOMParser().parseFromString(html, 'text/html');
fragment.appendChild(doc.importNode(doc.body, true));
}
}
else {
for (const node of Array.from(html))
fragment.appendChild(node);
}
shadowRoot.textContent = '';
shadowRoot.appendChild(fragment);
return new HTMLResult(shadowRoot);
}
function createShadowRoot() {
const host = document.createElement('div');
host.style.overflow = 'hidden';
host.style.position = 'absolute';
host.style.top = '-9999px';
host.style.left = '-9999px';
shadowRoot = host.attachShadow({ mode: 'open' });
document.body.appendChild(host);
}
class HTMLResult {
constructor(shadowRoot) {
this.shadowRoot = shadowRoot;
}
get elements() {
if (this.shadowRoot.firstChild instanceof HTMLBodyElement)
return Array.from(this.shadowRoot.firstChild.childNodes);
return Array.from(this.shadowRoot.childNodes);
}
dispose() {
this.shadowRoot.textContent = '';
}
}