From c7b78567c732b7799d154b10562b8a975120d09e Mon Sep 17 00:00:00 2001 From: Manfred Stock Date: Sat, 12 Dec 2020 23:09:35 +0100 Subject: [PATCH] Extend regex to support original example from issue #820 and more The original example from issue #820 was using a dash ('-') as fallback, however, the regular expression did not accept those. It also didn't support white space (and many other characters) in the fallback, which are also supported now, so one can even put e.g. links including label in the fallback. --- umap/static/umap/js/umap.core.js | 5 ++++- umap/static/umap/test/Util.js | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index 5042c827..02afe2cd 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -134,8 +134,11 @@ 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. diff --git a/umap/static/umap/test/Util.js b/umap/static/umap/test/Util.js index acdffb5e..04ba27b2 100644 --- a/umap/static/umap/test/Util.js +++ b/umap/static/umap/test/Util.js @@ -185,6 +185,26 @@ describe('L.Util', function () { 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 () {