Q:

text in HTML becomes split / malformed in the DOM when elements are inserted around text and then removed (preserving the inner text, though)

So, here is normal text: just your standard paragraph.

I have javascript that will insert a span around selected text (for highlighting purposes).
The problem is that when I remove the span, the nice text block becomes chunky, and malformed:

How do I restore the block of text to its original state?

A:

I think you must to keep the original node, and replace it cloning the innerHTML with your span-wraper. Then just replace the nodes.

