- ptr = params.to.node;
- ptr.setText(ptr.getText().substr(params.to.offset));
- prev = ptr.prev();
- while(prev || ptr.parent()) {
- if(ptr.sameNode(middle)) {
- break;
- }
- if(prev) {
- toDetach = prev;
- prev = prev.prev();
- toDetach.detach();
+ if(!this.containsNode(params.to.node)) {
+ // The end node was merged during detaching nodes above - there is nothing more left to do.
+ return;
+ }
+
+ ptr = middle.contents()[0];
+ while(ptr && !ptr.sameNode(params.to.node)) {
+ if(ptr.nodeType === Node.ELEMENT_NODE && ptr.containsNode(params.to.node)) {
+ ptr = ptr.contents()[0];
+ continue;