From: Aleksander Ɓukasz Date: Thu, 15 May 2014 10:12:47 +0000 (+0200) Subject: smartxml: fix - merge newly adjacent text nodes on middle element node moved X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/4729ca360f43f0b909dadabbce5438d4910382ed?ds=sidebyside smartxml: fix - merge newly adjacent text nodes on middle element node moved Related: 25e14ce74de1ecdd95f36cb19e231b5a66898038 Also changing Node.split implementation which relied on this buggy behavior. --- diff --git a/src/smartxml/core.js b/src/smartxml/core.js index 419b175..048130e 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -16,6 +16,12 @@ var INSERTION = function(implementation) { if(!(this.document.containsNode(this)) || !insertion.isNew) { nodeParent = insertion.ofNode.parent(); } + if(!insertion.insertsNew && insertion.ofNode.isSurroundedByTextNodes()) { + var prev = insertion.ofNode.prev(), + next = insertion.ofNode.next(); + prev.setText(prev.getText()+next.getText()); + next.detach(); + } returned = implementation.call(this, insertion.ofNode); if(!options.silent && returned.sameNode(insertion.ofNode)) { this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent, nodeWasContained); @@ -310,12 +316,12 @@ var textNodeTransformations = { var newElement = this.document.createDocumentNode({tagName: parentElement.getTagName(), attrs: attrs}); parentElement.after(newElement); + succeedingChildren.reverse().forEach(function(child) { + newElement.prepend(child); + }); if(suffix.length > 0) { - newElement.append({text: suffix}); + newElement.prepend({text: suffix}); } - succeedingChildren.forEach(function(child) { - newElement.append(child); - }); return {first: parentElement, second: newElement}; }, diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 2a6c5e0..7fc69a3 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -422,6 +422,18 @@ describe('smartxml', function() { expect(rootContents[0].getText()).to.equal('Alice a cat'); }); + it('merges adjacent text nodes resulting from moving an element node in between', function() { + var doc = getDocumentFromXML('
Alice hasa cat
'), + span = doc.root.contents()[2], + a = doc.root.contents()[0]; + + a.append(span); + + var rootContents = doc.root.contents(); + expect(rootContents).to.have.length(2, 'one child left'); + expect(rootContents[1].getText()).to.equal('Alice a cat'); + }); + it('inserts node at index', function() { var doc = getDocumentFromXML('
'), b = doc.root.contents()[1];