From 77d9e1f4e2aa102a961a18f00f316ca42d2e7703 Mon Sep 17 00:00:00 2001 From: Manfred Stock Date: Sat, 12 Dec 2020 17:47:41 +0100 Subject: [PATCH 1/2] Return first defined property instead of last property The changes done in b29adaa5 and ec275d64 introduced a property fallback, however, this always returned the property/string in the rightmost position, ignoring any of the previous values, even if they were defined. --- umap/static/umap/js/umap.core.js | 1 + umap/static/umap/test/Util.js | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index 9b9d5e01..5042c827 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -140,6 +140,7 @@ L.Util.greedyTemplate = function (str, data, ignore) { 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..acdffb5e 100644 --- a/umap/static/umap/test/Util.js +++ b/umap/static/umap/test/Util.js @@ -178,6 +178,13 @@ 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.'); + }); }); describe('#TextColorFromBackgroundColor', function () { From c7b78567c732b7799d154b10562b8a975120d09e Mon Sep 17 00:00:00 2001 From: Manfred Stock Date: Sat, 12 Dec 2020 23:09:35 +0100 Subject: [PATCH 2/2] 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 () {