Continuous operation of node

Hello, I’m sorry but I have encountered a problem and don’t know how to solve it. I need to seek help. Here is a button that needs to be clicked to clear empty lines or spaces in the document or text. However, it will display an error message “RangeError: Position 191 out of range”, as shown in the screenshot. Thank you

beaebf90-295f-11ef-bf94-5b7bc10d5e0d beaf5bd0-295f-11ef-bf94-5b7bc10d5e0d

Previous deletions change the positions of the nodes, causing this code to produce invalid changes when there’s more than one change to be made. You’ll want to either map node positions through tr.mapping before using them, or store some description of the changes in an array and apply them back-to-front.

Thank you very much for your reply. After using tr.mapping, the above issue was resolved. Thank you very much