smartxml: dividing text node into two with element node
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 9 Dec 2013 09:24:42 +0000 (10:24 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:50 +0000 (22:32 +0100)
src/smartxml/core.js
src/smartxml/smartxml.test.js

index 40fd9b8..10d058f 100644 (file)
@@ -251,6 +251,27 @@ var textNodeTransformations = {
         });
 
         return {first: parentElement, second: newElement};
         });
 
         return {first: parentElement, second: newElement};
+    },
+
+    divideWithElementNode: function(node, params) {
+        var insertion = this.getNodeInsertion(node),
+            myText = this.getText();
+
+        if(params.offset === myText.length) {
+            return this.after(node);
+        }
+        if(params.offset === 0) {
+            return this.before(node);
+        }
+
+        var lhsText = myText.substr(0, params.offset),
+            rhsText = myText.substr(params.offset),
+            rhsTextNode = this.document.createDocumentNode({text: rhsText});
+
+        this.setText(lhsText);
+        this.after(insertion.ofNode);
+        insertion.ofNode.after(rhsTextNode);
+        return insertion.ofNode;
     }
 };
 
     }
 };
 
index 5d2bbb7..4cda3bb 100644 (file)
@@ -301,6 +301,52 @@ describe('smartxml', function() {
             });
         });
 
             });
         });
 
+        describe('Dividing text node into two with element node', function() {
+                it('can divide text node with element node, splitting text node into two', function() {
+                    var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
+                        text = doc.root.contents()[0];
+
+                    var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 5}),
+                        contents = doc.root.contents(),
+                        lhsText = contents[0],
+                        rhsText = contents[2];
+
+                    expect(lhsText.getText()).to.equal('Alice');
+                    expect(returned.sameNode(contents[1]));
+                    expect(rhsText.getText()).to.equal(' has a cat');
+                });
+
+                it('treats dividing at the very end as appending after it', function() {
+                    var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
+                        text = doc.root.contents()[0];
+
+
+                    var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 15}),
+                        contents = doc.root.contents(),
+                        textNode = contents[0],
+                        elementNode = contents[1];
+
+                    expect(contents.length).to.equal(2);
+                    expect(textNode.getText()).to.equal('Alice has a cat');
+                    expect(returned.sameNode(elementNode)).to.be.true;
+                    expect(elementNode.getTagName()).to.equal('aside');
+                });
+
+                it('treats dividing at the very beginning as prepending before it', function() {
+                    var doc = getDocumentFromXML('<div>Alice has a cat</div>'),
+                        text = doc.root.contents()[0];
+
+                    var returned = text.divideWithElementNode({tagName: 'aside'}, {offset: 0}),
+                        contents = doc.root.contents(),
+                        textNode = contents[1],
+                        elementNode = contents[0];
+
+                    expect(contents.length).to.equal(2);
+                    expect(textNode.getText()).to.equal('Alice has a cat');
+                    expect(returned.sameNode(elementNode)).to.be.true;
+                    expect(elementNode.getTagName()).to.equal('aside');
+                });
+        });
     });
 
     describe('Manipulations', function() {
     });
 
     describe('Manipulations', function() {