I use DOMPurify library (https://github.com/cure53/DOMPurify) to clean up html code copied from google docs.
I would like to remove the span tags from the copied text but keep the text inside the tags as well as any strong tags included in the deleted span tags.
I manage to remove the span tags while keeping the text inside the tags but any strong tags are also removed.
Example
DOMPurify.sanitize("<p>
<span style="background-color:transparent;color:#000000;"><strong>Some strong text</strong></span>
</p>", {
ALLOWED_TAGS: ['p','strong']
})
Output
<p>Some strong text</p>
Expected output
<p><strong>Some strong text</strong></p>
I also tried with this kind of hook
DOMPurify.addHook("afterSanitizeAttributes", function (node, data, config) {
if (node.nodeName === "SPAN") {
node.replaceWith(node.textContent ?? "");
}
});
But the output is the same, <strong>
tags inside <span>
are also deleted.
Can you please help me to keep (sub) <strong>
tags after “sanitize”?
Many thanks