From: Aleksander Ɓukasz Date: Mon, 19 Aug 2013 11:52:30 +0000 (+0200) Subject: canvas api: fixing unwrapContents X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/dad0c998b0e446b9489e258f2a49ca49f0d63a00?ds=inline canvas api: fixing unwrapContents --- diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 1908574..2dc24eb 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -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'); }); + + it('merges text elements on the boundries - single child case', function() { + var c = canvas.fromXML('
Alice has a cat
'), + 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'); + }); }); }); diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index f8806ae..1a10c1a 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -228,18 +228,23 @@ $.extend(DocumentNodeElement.prototype, { 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; + 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;