canvas api: dividing text element at the text boundries
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 5 Aug 2013 10:19:42 +0000 (12:19 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 5 Aug 2013 10:19:42 +0000 (12:19 +0200)
modules/documentCanvas/canvas/canvas.test3.js
modules/documentCanvas/canvas/documentElement.js

index 95698d0..c969ba9 100644 (file)
@@ -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('<section>Alice has a cat</section>'),
+                        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('<section>Alice has a cat</section>'),
+                        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() {
index 129d7b2..b516af1 100644 (file)
@@ -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),