Fixing unwrapping DocumentTextElement
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sat, 13 Jul 2013 14:14:43 +0000 (16:14 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sat, 13 Jul 2013 14:14:43 +0000 (16:14 +0200)
Handling cases when wrapper of a DocumentTextElement has
DocumentTextElement sibling(s)

modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index bd47bf7..08e6e0f 100644 (file)
@@ -502,13 +502,13 @@ describe('Canvas', function() {
 
             describe('unwrapping', function() {
                 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
 
             describe('unwrapping', function() {
                 it('unwraps DocumentTextElement from its parent DocumentNodeElement if it\'s its only child', function() {
-                    var c = canvas.fromXML('<section><div>Alice has a cat</div></section>'),
+                    var c = canvas.fromXML('<section>Alice <span>has a</span> cat</section>'),
                     section = c.doc(),
                     section = c.doc(),
-                    text = section.children()[0].children()[0];
+                    text = section.children()[1].children()[0];
 
                     text.unwrap();
 
 
                     text.unwrap();
 
-                    expect(section.children().length).to.equal(1);
+                    expect(section.children().length).to.equal(1, 'section has one child');
                     expect(section.children()[0].getText()).to.equal('Alice has a cat');
                 })
             });
                     expect(section.children()[0].getText()).to.equal('Alice has a cat');
                 })
             });
index 0e2cb3d..82a85c7 100644 (file)
@@ -199,9 +199,26 @@ $.extend(DocumentTextElement.prototype, {
         }
     },
     unwrap: function() {
         }
     },
     unwrap: function() {
-        if(this.parent().children().length === 1) {
-            var parent = this.parent();
-            parent.after(this);
+        var parent = this.parent();
+        if(parent.children().length === 1) {
+            var grandParent = parent.parent();
+            if(grandParent) {
+                var grandParentChildren = grandParent.children(),
+                    idx = grandParent.childIndex(parent),
+                    prev = idx - 1 > -1 ? grandParentChildren[idx-1] : null,
+                    next = idx + 1 < grandParentChildren.length ? grandParentChildren[idx+1] : null;
+                if(prev && next) {
+                    prev.setText(prev.getText() + this.getText() + next.getText());
+                    next.detach();
+                } else if (prev || next) {
+                    var target = prev ? prev : next;
+                    target.setText(target.getText() + this.getText());
+                } else {
+                    parent.after(this);
+                }
+            } else {
+                parent.after(this);
+            }
             parent.detach();
         }
     },
             parent.detach();
         }
     },