Could you see if this patch solves your issue?
(The code you proposed, by deleting and then re-inserting the content of the text, would work poorly with collaborative editing and other things that involve mapping positions. But fortunately there was existing code that almost did what you needed here, and used ReplaceAroundStep to avoid this problem.)