From a0ea5d8b3f8045f428a032df8cacde0ed54172ab Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 30 Jun 2014 14:49:15 +0200 Subject: [PATCH] smartxml: fix for Document.deleteText --- src/smartxml/core.js | 8 ++++++-- src/smartxml/smartxml.test.js | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/smartxml/core.js b/src/smartxml/core.js index b40e9c3..f907a20 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -474,7 +474,7 @@ var documentTransformations = { return insertion.ofNode; }, deleteText: function(params) { - var ptr, next, toDetach, middle, text; + var ptr, next, nextNext, toDetach, middle, text; if(params.from.node.sameNode(params.to.node)) { ptr = params.from.node; @@ -502,7 +502,11 @@ var documentTransformations = { } else { toDetach = next; next = next.next(); - toDetach.detach(); + nextNext = next ? next.next() : null; + toDetach.detach({normalizeStrategy: (next && next.sameNode(params.to.node)) ? 'merge' : 'detach-right'}); + if(next && !next.isInDocument()) { + next = nextNext; + } } } else { ptr = ptr.parent(); diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index b50da7f..ce0d98a 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -942,6 +942,28 @@ describe('smartxml', function() { expect(contents[1].contents().length).to.equal(1); expect(contents[1].contents()[0].getText()).to.equal('b'); }); + it('removes across elements - 6', function() { + var doc = getDocumentFromXML('
aaabbbccc
ddd
'); + doc.deleteText({ + from: { + node: getTextNode('aaa', doc), + offset: 1 + }, + to: { + node: getTextNode('ddd', doc), + offset: 1 + } + }, { + error: function(e) {throw e;} + }); + + var contents = doc.root.contents(); + expect(contents.length).to.equal(2); + expect(contents[0].contents().length).to.equal(1); + expect(contents[0].contents()[0].getText()).to.equal('a'); + expect(contents[1].contents().length).to.equal(1); + expect(contents[1].contents()[0].getText()).to.equal('dd'); + }); }); describe('Splitting text', function() { -- 2.20.1