diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index 9b9d5e01..02afe2cd 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -134,12 +134,16 @@ L.Util.greedyTemplate = function (str, data, ignore) { return value; } - return str.replace(/\{ *([\w_\:\."\|]+) *\}/g, function (str, key) { + return str.replace(/\{ *([\w_\:\.\|]+)(?:\|("[^"]*"))? *\}/g, function (str, key, staticFallback) { var vars = key.split('|'), value, path; + if (staticFallback !== undefined) { + vars.push(staticFallback); + } for (var i = 0; i < vars.length; i++) { path = vars[i]; if (path.startsWith('"') && path.endsWith('"')) value = path.substring(1, path.length -1); // static default value. else value = getValue(data, path.split('.')); + if (value !== undefined) break; } if (value === undefined) { if (ignore) value = str; diff --git a/umap/static/umap/test/Util.js b/umap/static/umap/test/Util.js index d990028e..04ba27b2 100644 --- a/umap/static/umap/test/Util.js +++ b/umap/static/umap/test/Util.js @@ -178,6 +178,33 @@ describe('L.Util', function () { assert.equal(L.Util.greedyTemplate('A phrase with a {fr.var.bar|try.again|"default"}.', {}), 'A phrase with a default.'); }); + it('should use the first defined value', function () { + assert.equal(L.Util.greedyTemplate('A phrase with a {fr.var.bar|try.again|"default"}.', {try: { again: 'please'}}), 'A phrase with a please.'); + }); + + it('should use the first defined value', function () { + assert.equal(L.Util.greedyTemplate('A phrase with a {fr.var.bar|try.again|"default"}.', {try: { again: 'again'}, fr: {var: {bar: 'value'}}}), 'A phrase with a value.'); + }); + + it('should support the first example from #820 when translated to final syntax', function () { + assert.equal(L.Util.greedyTemplate('# {name} ({ele|"-"} m ü. M.)', {name: 'Portalet'}), '# Portalet (- m ü. M.)'); + }); + + it('should support the first example from #820 when translated to final syntax when no fallback required', function () { + assert.equal(L.Util.greedyTemplate('# {name} ({ele|"-"} m ü. M.)', {name: 'Portalet', ele: 3344}), '# Portalet (3344 m ü. M.)'); + }); + + it('should support white space in fallback', function () { + assert.equal(L.Util.greedyTemplate('A phrase with {var|"white space in the fallback."}', {}), 'A phrase with white space in the fallback.'); + }); + + it('should support empty string as fallback', function () { + assert.equal(L.Util.greedyTemplate('A phrase with empty string ("{var|""}") in the fallback.', {}), 'A phrase with empty string ("") in the fallback.'); + }); + + it('should support e.g. links as fallback', function () { + assert.equal(L.Util.greedyTemplate('A phrase with {var|"[[https://osm.org|link]]"} as fallback.', {}), 'A phrase with [[https://osm.org|link]] as fallback.'); + }); }); describe('#TextColorFromBackgroundColor', function () {