From 1cbef89f1995ae8bb83d01a58d20b005ad8b3a8f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Fri, 24 Jan 2014 16:44:13 +0100 Subject: [PATCH] smartxml - fixes in deleting arbitrary text method Handling case where start and end text node merged. --- src/smartxml/core.js | 13 +++++++++++-- src/smartxml/smartxml.test.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) 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({ -- 2.20.1