From: Aleksander Ɓukasz Date: Fri, 25 Oct 2013 07:54:13 +0000 (+0200) Subject: smartxml: Wrapping text spanning multiple sibling TextNodes X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/5625717737822065383ef29c6b5709e9984c3508 smartxml: Wrapping text spanning multiple sibling TextNodes --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index d8b6bf0..56e78af 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -200,6 +200,10 @@ $.extend(ElementNode.prototype, { }; }, + wrapText: function(params) { + return this.document._wrapText(_.extend({inside: this}, params)); + }, + toXML: function() { var wrapper = $('
'); wrapper.append(this._getXMLDOMToDump()); @@ -321,6 +325,10 @@ $.extend(Document.prototype, Backbone.Events, { suffixInside = textNode2.getText().substr(0, params.offsetEnd), suffixOutside = textNode2.getText().substr(params.offsetEnd) ; + + if(!(textNode1.parent().sameNode(textNode2.parent()))) { + throw new Error('Wrapping text in non-sibling text nodes not supported.'); + } var wrapperElement = this.createElementNode({tagName: params._with.tag, attrs: params._with.attrs}); textNode1.after(wrapperElement); diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index a7f3fca..7880017 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -278,6 +278,32 @@ describe('smartxml', function() { expect(node.contents()[2].getText()).to.equal(' a cat!'); }); + describe('Wrapping text', function() { + it('wraps text spanning multiple sibling TextNodes', function() { + var section = elementNodeFromXML('
Alice has a small cat
'), + wrapper = section.wrapText({ + _with: {tag: 'span', attrs: {'attr1': 'value1'}}, + offsetStart: 6, + offsetEnd: 4, + textNodeIdx: [0,2] + }); + + expect(section.contents().length).to.equal(2); + expect(section.contents()[0].nodeType).to.equal(Node.TEXT_NODE); + expect(section.contents()[0].getText()).to.equal('Alice '); + + var wrapper2 = section.contents()[1]; + expect(wrapper2.sameNode(wrapper)).to.be.true; + + var wrapperContents = wrapper.contents(); + expect(wrapperContents.length).to.equal(3); + expect(wrapperContents[0].getText()).to.equal('has a '); + + expect(wrapperContents[1].nodeType).to.equal(Node.ELEMENT_NODE); + expect(wrapperContents[1].contents().length).to.equal(1); + expect(wrapperContents[1].contents()[0].getText()).to.equal('small'); + }); + }); }); describe('Serializing document to WLXML', function() {