smartxml: fix for Document.deleteText
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 30 Jun 2014 12:49:15 +0000 (14:49 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 30 Jun 2014 13:02:11 +0000 (15:02 +0200)
src/smartxml/core.js
src/smartxml/smartxml.test.js

index b40e9c3..f907a20 100644 (file)
@@ -474,7 +474,7 @@ var documentTransformations = {
         return insertion.ofNode;
     },
     deleteText: function(params) {
         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;
 
         if(params.from.node.sameNode(params.to.node)) {
             ptr = params.from.node;
@@ -502,7 +502,11 @@ var documentTransformations = {
                 } else {
                     toDetach = next;
                     next = next.next();
                 } 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();
                 }
             } else {
                 ptr = ptr.parent();
index b50da7f..ce0d98a 100644 (file)
@@ -942,6 +942,28 @@ describe('smartxml', function() {
             expect(contents[1].contents().length).to.equal(1);
             expect(contents[1].contents()[0].getText()).to.equal('b');
         });
             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('<root><div>aaa<span>bbb</span>ccc</div><div>ddd</div></root>');
+            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() {
     });
 
     describe('Splitting text', function() {