From 1dbe94fac7f718912c7d7fd0a896dd38d245a3af Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 26 Nov 2013 14:11:15 +0100 Subject: [PATCH] smartxml: ElementNode.insertAtIndex --- src/smartxml/smartxml.js | 7 +++++++ src/smartxml/smartxml.test.js | 10 ++++++++++ 2 files changed, 17 insertions(+) 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'}), -- 2.20.1