diff --git a/src/common/JSONImporter.js b/src/common/JSONImporter.js index 983370a..b4118ee 100644 --- a/src/common/JSONImporter.js +++ b/src/common/JSONImporter.js @@ -819,11 +819,16 @@ define([ let skipNodes = []; if (searchOpts.startHint) { - const match = await this.findNodeWhere(core, searchOpts.startHint, fn); - if (match) { - return match; + let startNode = searchOpts.startHint; + let match = null; + while (startNode) { + match = await this.findNodeWhere(core, startNode, fn, skipNodes); + if (match) { + return match; + } + skipNodes.push(startNode); + startNode = core.getParent(startNode); } - skipNodes.push(searchOpts.startHint); } return await this.findNodeWhere(core, node, fn, skipNodes); diff --git a/test/common/JSONImporter.spec.js b/test/common/JSONImporter.spec.js index 858830e..0f3a14e 100644 --- a/test/common/JSONImporter.spec.js +++ b/test/common/JSONImporter.spec.js @@ -7,6 +7,7 @@ describe('JSONImporter', function () { const _ = testFixture.requirejs('underscore'); const Core = testFixture.requirejs('common/core/coreQ'); const Importer = testFixture.requirejs('webgme-json-importer/JSONImporter'); + const NodeSelections = Importer.NodeSelections; const assert = require('assert'); const gmeConfig = testFixture.getGmeConfig(); const path = testFixture.path; @@ -1065,6 +1066,43 @@ describe('JSONImporter', function () { ); }); + it('should search children for @guid first', async function() { + const fco = await core.loadByPath(root, '/1'); + const parent = core.createNode({base: fco, parent: root}); + const node = core.createNode({base: fco, parent}); + const guid = core.getGuid(node); + + const parentJson = { + children: [ + { + id: `@guid:${guid}` + } + ] + }; + const selectors = new NodeSelections(); + await importer.resolveSelectors(parent, parentJson, selectors); + assert.equal(selectors.cache.length, 1); + }); + + it('should search children of ancestors for @guid next', async function() { + const fco = await core.loadByPath(root, '/1'); + const gparent = core.createNode({base: fco, parent: root}); + const parent = core.createNode({base: fco, parent: gparent}); + const node = core.createNode({base: fco, parent: gparent}); + const guid = core.getGuid(node); + + const parentJson = { + children: [ + { + id: `@guid:${guid}` + } + ] + }; + const selectors = new NodeSelections(); + await importer.resolveSelectors(parent, parentJson, selectors); + assert.equal(selectors.cache.length, 2); + }); + it('should set guid when creating @guid nodes', async function() { const fco = await core.loadByPath(root, '/1'); const node = core.createNode({base: fco, parent: root});