From: Aleksander Ɓukasz Date: Thu, 17 Jul 2014 12:57:33 +0000 (+0200) Subject: smartxml: fix in Document.getNodeByPath X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/83edceffb18f5802bb6a9b20746437a3a46f2450?ds=inline smartxml: fix in Document.getNodeByPath --- diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 1e3ae44..a5a1abc 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -665,8 +665,11 @@ $.extend(Document.prototype, Backbone.Events, fragments, { getNodeByPath: function(path) { var toret = this.root; - path.forEach(function(idx) { + path.some(function(idx) { toret = toret.contents()[idx]; + if(!toret) { + return true; + } }); return toret; }, diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index ce0d98a..524c976 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -69,6 +69,24 @@ describe('smartxml', function() { expect(node.contents()[0].getText()).to.equal('Alice'); expect(node.contents()[1].getTagName()).to.equal('b'); }); + + describe('Retrieving node by path', function() { + it('passes smoke tests', function() { + var doc = getDocumentFromXML('c'); + expect(doc.getNodeByPath([0]).sameNode(doc.root.contents()[0])).to.be.true; + expect(doc.getNodeByPath([0,0]).sameNode(doc.root.contents()[0].contents()[0])).to.be.true; + }); + it('treats empty path as a root path', function() { + var doc = getDocumentFromXML(''); + expect(doc.getNodeByPath([]).sameNode(doc.root)).to.be.true; + }); + it('returns undefined for non existing paths', function() { + var doc = getDocumentFromXML(''); + expect(doc.getNodeByPath([1])).to.be.undefined; + expect(doc.getNodeByPath([0,1])).to.be.undefined; + expect(doc.getNodeByPath([10,1])).to.be.undefined; + }); + }); }); describe('DocumentNode', function() {