});
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;
}
};
});
});
+ 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() {