From 97b4fb7216407c6623f0e1307bae4b98b0c64b75 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 6 Aug 2013 12:09:06 +0200 Subject: [PATCH] canvas api: wrapElements - wraps one or more sibling elements with a node element --- modules/documentCanvas/canvas/canvas.js | 36 ++++++++++++++ modules/documentCanvas/canvas/canvas.test3.js | 48 +++++++++++++++++++ .../documentCanvas/canvas/documentElement.js | 1 + 3 files changed, 85 insertions(+) diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index 27f752d..9b57d93 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -319,6 +319,42 @@ $.extend(Canvas.prototype, { 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(), diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index b327bce..ff30843 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -636,6 +636,54 @@ describe('Canvas', function() { expect(wrapperChildren[2].getText()).to.equal(' cat'); }); + + it('wraps multiple sibling Elements', function() { + var c = canvas.fromXML('
Alice
has
a cat
'), + 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('
div>
'), + 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() { diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 9768236..e76ba78 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -83,6 +83,7 @@ $.extend(DocumentElement.prototype, { detach: function() { this.dom().detach(); this.canvas = null; + return this; }, markAsCurrent: function() { -- 2.20.1