working on fixing #70

This commit is contained in:
mic 2024-06-25 22:59:28 +02:00
parent 2478774e68
commit d71301f9e1

View file

@ -355,24 +355,32 @@ function removeTagsAndReturnHTML(rootElement, removeTags, preserveTags) {
const elements = rootElement.getElementsByTagName(tag); const elements = rootElement.getElementsByTagName(tag);
while (elements.length > 0) { while (elements.length > 0) {
const element = elements[0]; const element = elements[0];
// Preserve specified tags and their children const fragment = document.createDocumentFragment();
preserveTags.forEach(preserveTag => {
const preservedElements = element.getElementsByTagName(preserveTag); // Preserve specified tags and their children in the correct order
while (preservedElements.length > 0) { let child = element.firstChild;
const preservedElement = preservedElements[0]; while (child) {
element.parentNode.insertBefore(preservedElement, element); console.log(">>>>>>>>>>>> ciclo child: " + child.tagName.toLowerCase());
const nextSibling = child.nextSibling;
if (preserveTags.includes(child.tagName.toLowerCase())) {
console.log(">>>>>>>>>>>> preserve: " + child.tagName.toLowerCase());
fragment.appendChild(child);
} }
}); child = nextSibling;
}
// Append preserved elements back to the parent of the removed element
element.parentNode.insertBefore(fragment, element);
// Remove the element and all its remaining children // Remove the element and all its remaining children
element.parentNode.removeChild(element); element.parentNode.removeChild(element);
} }
}); });
replaceNewlinesWithBr(rootElement); replaceNewlinesWithBr(rootElement);
// Return the updated HTML as a string // Return the updated HTML as a string
return rootElement.innerHTML; return rootElement.innerHTML;
} }
// Replace newline characters with <br> tags // Replace newline characters with <br> tags
function replaceNewlinesWithBr(node) { function replaceNewlinesWithBr(node) {