return insertion.ofNode;
},
deleteText: function(params) {
- var ptr, prev, next, toDetach, middle, text;
+ var ptr, next, toDetach, middle, text;
if(params.from.node.sameNode(params.to.node)) {
ptr = params.from.node;
while(next || ptr.parent()) {
if(next) {
- if(next.sameNode(params.to.node) || (next.nodeType === Node.ELEMENT_NODE && next.containsNode(params.to.node))) {
+ if(next.sameNode(params.to.node)) {
+ return;
+ }
+ else if(next.nodeType === Node.ELEMENT_NODE && next.containsNode(params.to.node)) {
middle = next;
break;
} else {
}
}
- ptr = params.to.node;
-
- if(!this.containsNode(ptr)) {
+ if(!this.containsNode(params.to.node)) {
// The end node was merged during detaching nodes above - there is nothing more left to do.
return;
}
- prev = ptr.prev();
- while(prev || ptr.parent()) {
- if(ptr.sameNode(middle)) {
- break;
- }
- if(prev) {
- toDetach = prev;
- prev = prev.prev();
- toDetach.detach();
+
+ 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;
} else {
- ptr = ptr.parent();
- prev = ptr.prev();
+ ptr = ptr.next();
+ ptr.prev().detach();
}
}
}
expect(contents.length).to.equal(1);
expect(contents[0].getText()).to.equal('Acat');
});
+ it('remove across elements - 4', function() {
+ var doc = getDocumentFromXML('<div>Alice <div>has <span>a</span> cat</div></div>');
+ doc.deleteText({
+ from: {
+ node: getTextNode('Alice ', doc),
+ offset: 1
+ },
+ to: {
+ node: getTextNode(' cat', doc),
+ offset: 1
+ }
+ });
+ var contents = doc.root.contents();
+ expect(contents.length).to.equal(2);
+ expect(contents[0].getText()).to.equal('A');
+ expect(contents[1].getTagName()).to.equal('div');
+ expect(contents[1].contents().length).to.equal(1);
+ expect(contents[1].contents()[0].getText()).to.equal('cat');
+ });
it('removes nodes in between', function() {
var doc = getDocumentFromXML('<div><a>aaa<x>!</x></a>xxx<x></x><b><x>!</x>bbb</b></div>');
doc.deleteText({