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.
This commit is contained in:
parent
77d9e1f4e2
commit
c7b78567c7
2 changed files with 24 additions and 1 deletions
|
@ -134,8 +134,11 @@ L.Util.greedyTemplate = function (str, data, ignore) {
|
||||||
return value;
|
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;
|
var vars = key.split('|'), value, path;
|
||||||
|
if (staticFallback !== undefined) {
|
||||||
|
vars.push(staticFallback);
|
||||||
|
}
|
||||||
for (var i = 0; i < vars.length; i++) {
|
for (var i = 0; i < vars.length; i++) {
|
||||||
path = vars[i];
|
path = vars[i];
|
||||||
if (path.startsWith('"') && path.endsWith('"')) value = path.substring(1, path.length -1); // static default value.
|
if (path.startsWith('"') && path.endsWith('"')) value = path.substring(1, path.length -1); // static default value.
|
||||||
|
|
|
@ -185,6 +185,26 @@ describe('L.Util', function () {
|
||||||
it('should use the first defined value', 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.');
|
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 () {
|
describe('#TextColorFromBackgroundColor', function () {
|
||||||
|
|
Loading…
Reference in a new issue