From: Aleksander Ɓukasz Date: Fri, 24 Jan 2014 15:44:13 +0000 (+0100) Subject: smartxml - fixes in deleting arbitrary text method X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/1cbef89f1995ae8bb83d01a58d20b005ad8b3a8f?ds=sidebyside smartxml - fixes in deleting arbitrary text method Handling case where start and end text node merged. --- diff --git a/src/smartxml/core.js b/src/smartxml/core.js index 44be8d6..f940ccc 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -442,9 +442,14 @@ var documentTransformations = { return; } + // Both edge text nodes need to be edited before anything else happen in case that + // they get merged when detaching content between them. + params.from.node.setText(params.from.node.getText().substr(0, params.from.offset)); + params.to.node.setText(params.to.node.getText().substr(params.to.offset)); + ptr = params.from.node; - ptr.setText(ptr.getText().substr(0, params.from.offset)); next = ptr.next(); + while(next || ptr.parent()) { if(next) { if(next.sameNode(params.to.node) || (next.nodeType === Node.ELEMENT_NODE && next.containsNode(params.to.node))) { @@ -462,7 +467,11 @@ var documentTransformations = { } ptr = params.to.node; - ptr.setText(ptr.getText().substr(params.to.offset)); + + if(!this.containsNode(ptr)) { + // 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)) { diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 140174e..8ae7721 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -738,6 +738,22 @@ describe('smartxml', function() { expect(bContents[0].contents().length).to.equal(1); expect(bContents[0].contents()[0].getText()).to.equal('cc'); }); + it('remove across elements - 3 (merged text nodes)', function() { + var doc = getDocumentFromXML('
Alice has a cat
'); + doc.deleteText({ + from: { + node: getTextNode('Alice ', doc), + offset: 1 + }, + to: { + node: getTextNode(' a cat', doc), + offset: 3 + } + }); + var contents = doc.root.contents(); + expect(contents.length).to.equal(1); + expect(contents[0].getText()).to.equal('Acat'); + }); it('removes nodes in between', function() { var doc = getDocumentFromXML('
aaa!xxx!bbb
'); doc.deleteText({