smartxml: fix - merge newly adjacent text nodes on middle element node moved
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 15 May 2014 10:12:47 +0000 (12:12 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 27 May 2014 14:18:07 +0000 (16:18 +0200)
Related: 25e14ce74de1ecdd95f36cb19e231b5a66898038

Also changing Node.split implementation which relied on this buggy
behavior.

src/smartxml/core.js
src/smartxml/smartxml.test.js

index 419b175..048130e 100644 (file)
@@ -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};
     },
index 2a6c5e0..7fc69a3 100644 (file)
@@ -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('<div><a></a>Alice <span>has</span>a cat</div>'),
+                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('<div><a></a><b></b><c></c></div>'),
                 b = doc.root.contents()[1];