2025-11-01 13:27:53 +00:00
|
|
|
export class HTMLService {
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public clearElement(element: HTMLElement) {
|
2025-11-01 13:27:53 +00:00
|
|
|
element.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public setHTMLContent(container: HTMLElement, htmlString: string) {
|
2025-11-01 13:27:53 +00:00
|
|
|
this.clearElement(container);
|
|
|
|
|
const fragment = this.parseHTMLString(htmlString);
|
|
|
|
|
container.appendChild(fragment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public parseHTMLString(htmlString: string): DocumentFragment {
|
|
|
|
|
const parser = new DOMParser();
|
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
|
const doc = parser.parseFromString(htmlString, "text/html");
|
|
|
|
|
|
|
|
|
|
// Transfer all nodes from the parsed body to the fragment
|
|
|
|
|
while (doc.body.firstChild) {
|
|
|
|
|
fragment.appendChild(doc.body.firstChild);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fragment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a temporary container, parses HTML, and returns the container.
|
|
|
|
|
// Useful for parsing HTML when you need to traverse the resulting DOM structure.
|
|
|
|
|
public parseHTMLToContainer(htmlString: string): HTMLDivElement {
|
|
|
|
|
const container = document.createElement("div");
|
|
|
|
|
const fragment = this.parseHTMLString(htmlString);
|
|
|
|
|
container.appendChild(fragment);
|
|
|
|
|
return container;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|