From: Aleksander Ɓukasz Date: Mon, 9 Dec 2013 09:24:42 +0000 (+0100) Subject: smartxml: dividing text node into two with element node X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/86b856fc4fd3a7834c5780b2e5e5700f93a4b0b9?hp=ad1f72d773192f688ed9e55dbf6780756e379d73 smartxml: dividing text node into two with element node --- diff --git a/src/smartxml/core.js b/src/smartxml/core.js index 40fd9b8..10d058f 100644 --- a/src/smartxml/core.js +++ b/src/smartxml/core.js @@ -251,6 +251,27 @@ var textNodeTransformations = { }); 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; } }; diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 5d2bbb7..4cda3bb 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -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('
Alice has a cat
'), + 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('
Alice has a cat
'), + 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('
Alice has a cat
'), + 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() {