canvas api: wrapElements
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 6 Aug 2013 10:09:06 +0000 (12:09 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 6 Aug 2013 10:09:06 +0000 (12:09 +0200)
- wraps one or more sibling elements with a node element

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

index 27f752d..9b57d93 100644 (file)
@@ -319,6 +319,42 @@ $.extend(Canvas.prototype, {
             wrapperElement.after({text: suffixOutside});
         return wrapperElement;
     },
             wrapperElement.after({text: suffixOutside});
         return wrapperElement;
     },
+
+    wrapElements: function(params) {
+        if(!(params.element1.parent().sameNode(params.element2.parent())))
+            return;
+
+        var parent = params.element1.parent(),
+            parentChildren = parent.children(),
+            wrapper = documentElement.DocumentNodeElement.create({
+                tag: params._with.tag,
+                klass: params._with.klass}),
+            idx1 = parent.childIndex(params.element1),
+            idx2 = parent.childIndex(params.element2);
+
+        if(idx1 > idx2) {
+            var tmp = idx1;
+            idx1 = idx2;
+            idx2 = tmp;
+        }
+
+        var insertingMethod, insertingTarget;
+        if(idx1 === 0) {
+            insertingMethod = 'prepend';
+            insertingTarget = parent;
+        } else {
+            insertingMethod = 'after';
+            insertingTarget = parentChildren[idx1-1];
+        }
+
+        for(var i = idx1; i <= idx2; i++) {
+            wrapper.append(parentChildren[i].detach());
+        }
+
+        insertingTarget[insertingMethod](wrapper);
+        return wrapper;
+    },
+
     getSiblingParents: function(params) {
         var parents1 = [params.element1].concat(params.element1.parents()).reverse(),
             parents2 = [params.element2].concat(params.element2.parents()).reverse(),
     getSiblingParents: function(params) {
         var parents1 = [params.element1].concat(params.element1.parents()).reverse(),
             parents2 = [params.element2].concat(params.element2.parents()).reverse(),
index b327bce..ff30843 100644 (file)
@@ -636,6 +636,54 @@ describe('Canvas', function() {
 
                     expect(wrapperChildren[2].getText()).to.equal(' cat');
                 });
 
                     expect(wrapperChildren[2].getText()).to.equal(' cat');
                 });
+
+                it('wraps multiple sibling Elements', function() {
+                    var c = canvas.fromXML('<section>Alice<div>has</div><div>a cat</div></section>'),
+                        section = c.doc(),
+                        aliceText = section.children()[0],
+                        firstDiv = section.children()[1],
+                        lastDiv = section.children()[section.children().length -1];
+
+                    var returned = c.wrapElements({
+                            element1: aliceText,
+                            element2: lastDiv,
+                            _with: {tag: 'header'}
+                        });
+
+                    var sectionChildren = section.children(),
+                        header = sectionChildren[0],
+                        headerChildren = header.children();
+
+                    expect(sectionChildren).to.have.length(1);
+                    expect(header.sameNode(returned)).to.equal(true, 'wrapper returned');
+                    expect(headerChildren).to.have.length(3);
+                    expect(headerChildren[0].sameNode(aliceText)).to.equal(true, 'first node wrapped');
+                    expect(headerChildren[1].sameNode(firstDiv)).to.equal(true, 'second node wrapped');
+                    expect(headerChildren[2].sameNode(lastDiv)).to.equal(true, 'third node wrapped');
+                });
+                it('wraps multiple sibling Elements - middle case', function() {
+                    var c = canvas.fromXML('<section><div></div>div></div><div></div><div></div></section>'),
+                        section = c.doc(),
+                        div1 = section.children()[0],
+                        div2 = section.children()[1],
+                        div3 = section.children()[2],
+                        div4 = section.children()[3];
+
+                    var returned = c.wrapElements({
+                            element1: div2,
+                            element2: div3,
+                            _with: {tag: 'header'}
+                        });
+
+                    var sectionChildren = section.children(),
+                        header = sectionChildren[1],
+                        headerChildren = header.children();
+
+                    expect(sectionChildren).to.have.length(3);
+                    expect(headerChildren).to.have.length(2);
+                    expect(headerChildren[0].sameNode(div2)).to.equal(true, 'first node wrapped');
+                    expect(headerChildren[1].sameNode(div3)).to.equal(true, 'second node wrapped');
+                });
             });
 
             describe('unwrapping', function() {
             });
 
             describe('unwrapping', function() {
index 9768236..e76ba78 100644 (file)
@@ -83,6 +83,7 @@ $.extend(DocumentElement.prototype, {
     detach: function() {
         this.dom().detach();
         this.canvas = null;
     detach: function() {
         this.dom().detach();
         this.canvas = null;
+        return this;
     },
 
     markAsCurrent: function() {
     },
 
     markAsCurrent: function() {