From: Aleksander Ɓukasz Date: Mon, 5 Aug 2013 10:19:42 +0000 (+0200) Subject: canvas api: dividing text element at the text boundries X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/eda3b8f3b50cc6f2a7d770c8cdd0e35010062931 canvas api: dividing text element at the text boundries --- diff --git a/modules/documentCanvas/canvas/canvas.test3.js b/modules/documentCanvas/canvas/canvas.test3.js index 95698d0..c969ba9 100644 --- a/modules/documentCanvas/canvas/canvas.test3.js +++ b/modules/documentCanvas/canvas/canvas.test3.js @@ -403,6 +403,38 @@ describe('Canvas', function() { expect(returned.sameNode(sectionChildren[1])); expect(rhsText.getText()).to.equal(' has a cat'); }); + + it('treats dividing DocumentTextElement at the very end as appending after it', function() { + var c = canvas.fromXML('
Alice has a cat
'), + section = c.doc(), + text = section.children()[0]; + + var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 15}), + sectionChildren = section.children(), + textElement = sectionChildren[0], + nodeElement = sectionChildren[1]; + + expect(sectionChildren.length).to.equal(2); + expect(textElement.getText()).to.equal('Alice has a cat'); + expect(returned.sameNode(nodeElement)).to.be.true; + expect(nodeElement.getWlxmlTag()).to.equal('aside'); + }); + + it('treats dividing DocumentTextElement at the very beginning as appending before it', function() { + var c = canvas.fromXML('
Alice has a cat
'), + section = c.doc(), + text = section.children()[0]; + + var returned = text.divide({tag: 'aside', klass: 'footnote', offset: 0}), + sectionChildren = section.children(), + nodeElement = sectionChildren[0], + textElement = sectionChildren[1]; + + expect(sectionChildren.length).to.equal(2); + expect(textElement.getText()).to.equal('Alice has a cat'); + expect(returned.sameNode(nodeElement)).to.be.true; + expect(nodeElement.getWlxmlTag()).to.equal('aside'); + }); }); describe('Splitting text', function() { diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 129d7b2..b516af1 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -466,9 +466,11 @@ $.extend(DocumentTextElement.prototype, { }, divide: function(params) { var myText = this.getText(); - - if(params.offset <= 0 || params.offset >= myText.length) - return; + + if(params.offset === myText.length) + return this.after(params); + if(params.offset === 0) + return this.before(params); var lhsText = myText.substr(0, params.offset), rhsText = myText.substr(params.offset),