Merge pull request #862 from mstock/property-fallbacks

Fix and enhance property fallbacks
This commit is contained in:
Yohan Boniface 2021-05-17 09:57:33 +02:00 committed by GitHub
commit 956549e5da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View file

@ -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;

View file

@ -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 () {