diff --git a/core/src/utilities/helpers/generic-helpers.js b/core/src/utilities/helpers/generic-helpers.js index 45f2211b60..7587891a45 100644 --- a/core/src/utilities/helpers/generic-helpers.js +++ b/core/src/utilities/helpers/generic-helpers.js @@ -1,5 +1,6 @@ // Standalone or partly-standalone methods that are used widely through the whole app and are synchronous. import { LuigiElements, LuigiConfig } from '../../core-api'; +import { replace, get } from 'lodash'; class GenericHelpersClass { /** @@ -195,12 +196,22 @@ class GenericHelpersClass { replaceVars(inputString, params, prefix, parenthesis = true) { let processedString = inputString; if (params) { - Object.entries(params).forEach(entry => { - processedString = processedString.replace( - new RegExp(this.escapeRegExp((parenthesis ? '{' : '') + prefix + entry[0] + (parenthesis ? '}' : '')), 'g'), - encodeURIComponent(entry[1]) - ); - }); + if (parenthesis) { + processedString = replace(processedString, /{([\s\S]+?)}/g, val => { + let repl = val.slice(1, -1).trim(); + if (repl.indexOf(prefix) === 0) { + repl = repl.substring(prefix.length); + } + return get(params, repl, val); + }); + } else { + Object.entries(params).forEach(entry => { + processedString = processedString.replace( + new RegExp(this.escapeRegExp(prefix + entry[0]), 'g'), + encodeURIComponent(entry[1]) + ); + }); + } } if (parenthesis) { processedString = processedString.replace(new RegExp('\\{' + this.escapeRegExp(prefix) + '[^\\}]+\\}', 'g'), ''); diff --git a/core/test/utilities/helpers/generic-helpers.spec.js b/core/test/utilities/helpers/generic-helpers.spec.js index dc75edce02..b7308296f7 100644 --- a/core/test/utilities/helpers/generic-helpers.spec.js +++ b/core/test/utilities/helpers/generic-helpers.spec.js @@ -86,6 +86,30 @@ describe('Generic-helpers', () => { assert.deepEqual(GenericHelpers.removeProperties(input, keys), expected); }); + it('replaceVars', () => { + const context = { + a: 'a_val', + b: { + c: 'c_val' + } + }; + assert.equal( + GenericHelpers.replaceVars('index.html#/{context.a}', context, 'context.'), + 'index.html#/a_val', + 'first level context vars being interpolated' + ); + assert.equal( + GenericHelpers.replaceVars('index.html#/{context.b.c}', context, 'context.'), + 'index.html#/c_val', + 'nested context vars being interpolated' + ); + assert.equal( + GenericHelpers.replaceVars('index.html#/:a/{context.b.c}', context, ':', false), + 'index.html#/a_val/{context.b.c}', + 'colon prefix, no paranthesis' + ); + }); + describe('semverCompare', () => { it('standard versions', () => { const input = ['1.1.1', '0.6.4', '0.7.7', '0.7.1', '1.0.0']; diff --git a/core/test/utilities/helpers/routing-helpers.spec.js b/core/test/utilities/helpers/routing-helpers.spec.js index 66ded348f3..5e8015b4f5 100644 --- a/core/test/utilities/helpers/routing-helpers.spec.js +++ b/core/test/utilities/helpers/routing-helpers.spec.js @@ -110,6 +110,19 @@ describe('Routing-helpers', () => { expect(RoutingHelpers.substituteViewUrl(viewUrl, {})).to.equal(expected); }); + + it('substituteViewUrl - substitutes nested context variable', () => { + const viewUrl = '/{i18n.currentLocale}/microfrontend.html#/:var1/{context.nested.value}/{nodeParams.param1}'; + const expected = '/en/microfrontend.html#/var1_value/context_nested_value/nodeparam_value'; + + expect( + RoutingHelpers.substituteViewUrl(viewUrl, { + pathParams: { var1: 'var1_value' }, + context: { nested: { value: 'context_nested_value' } }, + nodeParams: { param1: 'nodeparam_value' } + }) + ).to.equal(expected); + }); }); describe('substitute search query params', () => { afterEach(() => {