canvas api: fixing unwrapContents
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 19 Aug 2013 11:52:30 +0000 (13:52 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 19 Aug 2013 11:52:30 +0000 (13:52 +0200)
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 1908574..2dc24eb 100644 (file)
@@ -846,6 +846,18 @@ describe('Canvas', function() {
                     expect(range.element1.sameNode(sectionChildren[0])).to.equal(true, 'range start ok');
                     expect(range.element2.sameNode(sectionChildren[2])).to.equal(true, 'range end ok');
                 });
                     expect(range.element1.sameNode(sectionChildren[0])).to.equal(true, 'range start ok');
                     expect(range.element2.sameNode(sectionChildren[2])).to.equal(true, 'range end ok');
                 });
+
+                it('merges text elements on the boundries - single child case', function() {
+                    var c = canvas.fromXML('<section>Alice <span>has</span> a cat</section>'),
+                        section = c.doc(),
+                        span = section.children()[1];
+
+                    var range = span.unwrapContents(),
+                        sectionChildren = section.children();
+
+                    expect(sectionChildren).to.have.length(1);
+                    expect(sectionChildren[0].getText()).to.equal('Alice has a cat');
+                });
             });
             
         });
             });
             
         });
index f8806ae..1a10c1a 100644 (file)
@@ -228,18 +228,23 @@ $.extend(DocumentNodeElement.prototype, {
         if(myChildren.length === 0)
             return this.detach();
 
         if(myChildren.length === 0)
             return this.detach();
 
-        var moveLeftRange, moveRightRange;
+        var moveLeftRange, moveRightRange, leftMerged;
 
         if(myIdx > 0 && (parentChildren[myIdx-1] instanceof DocumentTextElement) && (myChildren[0] instanceof DocumentTextElement)) {
             parentChildren[myIdx-1].appendText(myChildren[0].getText());
             myChildren[0].detach();
             moveLeftRange = true;
 
         if(myIdx > 0 && (parentChildren[myIdx-1] instanceof DocumentTextElement) && (myChildren[0] instanceof DocumentTextElement)) {
             parentChildren[myIdx-1].appendText(myChildren[0].getText());
             myChildren[0].detach();
             moveLeftRange = true;
+            leftMerged = true;
+        } else {
+            leftMerged = false;
         }
 
         }
 
-        if(myIdx < parentChildren.length - 1 && (parentChildren[parentChildren.length-1] instanceof DocumentTextElement) && (myChildren[myChildren.length-1] instanceof DocumentTextElement)) {
-            parentChildren[parentChildren.length-1].prependText(myChildren[myChildren.length-1].getText());
-            myChildren[myChildren.length-1].detach();
-            moveRightRange = true;
+        if(!(leftMerged && myChildren.length === 1)) {
+            if(myIdx < parentChildren.length - 1 && (parentChildren[myIdx+1] instanceof DocumentTextElement) && (myChildren[myChildren.length-1] instanceof DocumentTextElement)) {
+                parentChildren[myIdx+1].prependText(myChildren[myChildren.length-1].getText());
+                myChildren[myChildren.length-1].detach();
+                moveRightRange = true;
+            }
         }
 
         var childrenLength = this.children().length;
         }
 
         var childrenLength = this.children().length;