From fd5607f0a5ffb5f37df8a53a8b50d72ec6a4f8b9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 27 Jan 2014 11:33:00 +0100 Subject: [PATCH] smartxml: another fix for arbitrary text deletion --- src/smartxml/core.js | 30 ++++++++++++++---------------- src/smartxml/smartxml.test.js | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/smartxml/core.js b/src/smartxml/core.js index f940ccc..665f9d5 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -433,7 +433,7 @@ var documentTransformations = { 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; @@ -452,7 +452,10 @@ var documentTransformations = { 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 { @@ -466,24 +469,19 @@ var documentTransformations = { } } - 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(); } } } diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 8ae7721..03befea 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -754,6 +754,25 @@ describe('smartxml', function() { expect(contents.length).to.equal(1); expect(contents[0].getText()).to.equal('Acat'); }); + it('remove across elements - 4', function() { + var doc = getDocumentFromXML('
Alice
has a cat
'); + 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('
aaa!xxx!bbb
'); doc.deleteText({ -- 2.20.1