private convertThymeleafToHtml(thymeleafTemplate: string): string {
const parser = new DOMParser();
const doc = parser.parseFromString(thymeleafTemplate, 'text/html');
const walker = document.createTreeWalker(
doc,
NodeFilter.SHOW_ELEMENT,
null,
false
);
const thymeleafAttributes = ['th:text', 'th:href', 'th:each', 'th:if', 'th:unless', 'th:case', 'th:choose', 'th:fragment', 'th:object', 'th:attr'];
let currentNode: Element | null = walker.nextNode() as Element;
while (currentNode) {
thymeleafAttributes.forEach(attr => {
if (currentNode.hasAttribute(attr)) {
const attrValue = currentNode.getAttribute(attr)!;
currentNode.setAttribute(`data-${attr}`, attrValue);
currentNode.removeAttribute(attr);
// Handle th:text specifically to add placeholder span
if (attr === 'th:text') {
const placeholder = `${${attrValue.replace(/${(.*?)}/, '$1')}}`;
const formattedPlaceholder = this.nonEditablePlaceholderSpan(placeholder);
currentNode.innerHTML = ''; // Clear existing innerHTML
currentNode.appendChild(formattedPlaceholder);
}
}
});
currentNode = walker.nextNode() as Element;
}
return doc.body.innerHTML; // Return the inner HTML of the body
}
I want to convert it back to html, need most efficient traversal code. and alternative method to that can be sued tml to thymleaf and thymleaf to html conversion ..