Fix: Detaching node element surrounded by text elements now merges these text element...
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 7 Aug 2013 10:06:38 +0000 (12:06 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 7 Aug 2013 10:06:38 +0000 (12:06 +0200)
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 10f8348..0fe89cc 100644 (file)
@@ -458,6 +458,22 @@ describe('Canvas', function() {
                 });
             });
 
+            describe('Removing elements', function() {
+                it('merges left and right DocumentTextElement sibling of a detached DocumentNodeElement', function() {
+                    var c = canvas.fromXML('<section>Alice<div>has</div>a cat</section>'),
+                        section = c.doc(),
+                        div = section.children()[1];
+
+                    div.detach();
+
+                    var sectionChildren = section.children(),
+                        textElement = sectionChildren[0];
+
+                    expect(sectionChildren).to.have.length(1);
+                    expect(textElement.getText()).to.equal('Alicea cat');
+                });
+            });
+
             describe('Splitting text', function() {
                 
                 it('splits DocumentTextElement\'s parent into two DocumentNodeElements of the same type', function() {
index 689df0e..4a236df 100644 (file)
@@ -80,12 +80,6 @@ $.extend(DocumentElement.prototype, {
         return wrapper;
     },
 
-    detach: function() {
-        this.dom().detach();
-        this.canvas = null;
-        return this;
-    },
-
     markAsCurrent: function() {
         this.canvas.markAsCurrent(this);
     },
@@ -178,6 +172,24 @@ $.extend(DocumentNodeElement.prototype, {
     _container: function() {
         return this.dom().children('[document-element-content]');
     },
+    detach: function() {
+        var parent = this.parent();
+        if(!parent)
+            return;
+
+        var parentChildren = parent.children(),
+            myIdx = parent.childIndex(this);
+
+        if(myIdx > 0 && myIdx < parentChildren.length) {
+            if((parentChildren[myIdx-1] instanceof DocumentTextElement) && (parentChildren[myIdx+1] instanceof DocumentTextElement)) {
+                parentChildren[myIdx-1].appendText(parentChildren[myIdx+1].getText());
+                parentChildren[myIdx+1].detach();
+            }
+        }
+        this.dom().detach();
+        this.canvas = null;
+        return this;
+    },
     data: function() {
         var dom = this.dom(),
             args = Array.prototype.slice.call(arguments, 0);
@@ -417,9 +429,17 @@ $.extend(DocumentTextElement.prototype, {
         else
             this.$element = $element;
     },
+    detach: function() {
+        this.dom().detach();
+        this.canvas = null;
+        return this;
+    },
     setText: function(text) {
         this.dom().contents()[0].data = text;
     },
+    appendText: function(text) {
+        this.dom().contents()[0].data += text;
+    },
     getText: function() {
         return this.dom().text().replace(utils.unicode.ZWS, '');
     },