From: Aleksander Ɓukasz Date: Tue, 26 Nov 2013 13:11:15 +0000 (+0100) Subject: smartxml: ElementNode.insertAtIndex X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/1dbe94fac7f718912c7d7fd0a896dd38d245a3af?hp=f1f9dfddb9e3958dcbdc47aea789c5e1448fb68c smartxml: ElementNode.insertAtIndex --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 9f060e8..cfe94f0 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -272,6 +272,13 @@ $.extend(ElementNode.prototype, { this._$.prepend(nativeNode); }), + insertAtIndex: function(nativeNode, index) { + var contents = this.contents(); + if(contents[index]) { + return contents[index].before(nativeNode); + } + }, + unwrapContent: function() { var parent = this.parent(); if(!parent) { diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index a4ceaa4..98d7f88 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -336,6 +336,16 @@ describe('smartxml', function() { expect(rootContents[0].getText()).to.equal('Alice a cat'); }); + it('inserts node at index', function() { + var doc = getDocumentFromXML('
'), + b = doc.root.contents()[1]; + + var inserted = doc.root.insertAtIndex({tagName: 'test'}, 1); + + expect(doc.root.contents()[1].sameNode(inserted)).to.equal(true, 'inserted node returned'); + expect(b.getIndex()).to.equal(2, 'b node shifted right'); + }); + it('appends element node to another element node', function() { var node1 = elementNodeFromParams({tag: 'div'}), node2 = elementNodeFromParams({tag: 'a'}),