From 1c7e8cbe9b10da5e57ababd06ac17ee2c7da5a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Tamargo?= Date: Mon, 26 Feb 2024 16:27:25 +0100 Subject: [PATCH] MBS-13496: Support attributes with spaces in link phrase interpolation Using the new "a cappella" attribute was breaking link phrase interpolation, because spaces were not accepted as part of the interpolated "variable name". We already had several attributes with spaces in their name though, we just had gotten lucky we were not using any of them in link phrases until now. Just adding spaces to the allowed characters doesn't seem to cause any new issues, so it seems good enough for our needs. --- root/static/scripts/common/i18n/expand2.js | 4 ++-- root/static/scripts/common/i18n/expand2react.js | 2 +- root/static/scripts/tests/i18n/expand2.js | 3 ++- t/lib/t/MusicBrainz/Server/Translation.pm | 6 ++++++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/root/static/scripts/common/i18n/expand2.js b/root/static/scripts/common/i18n/expand2.js index d1c56217bdb..ca8f39ed5c4 100644 --- a/root/static/scripts/common/i18n/expand2.js +++ b/root/static/scripts/common/i18n/expand2.js @@ -206,7 +206,7 @@ export const createTextContentParser = ( return mapValue(text); }; -const varSubst = /^\{([0-9A-z_]+)\}/; +const varSubst = /^\{([0-9A-z_ ]+)\}/; export const createVarSubstParser = ( argFilter: (V) => T, ): Parser => saveMatch( @@ -225,7 +225,7 @@ export const createVarSubstParser = ( export const parseStringVarSubst: Parser = createVarSubstParser(getString); -const condSubstStart = /^\{([0-9A-z_]+):/; +const condSubstStart = /^\{([0-9A-z_ ]+):/; const verticalPipe = /^\|/; export const substEnd: RegExp = /^}/; export const createCondSubstParser = ( diff --git a/root/static/scripts/common/i18n/expand2react.js b/root/static/scripts/common/i18n/expand2react.js index cbaa1f01966..fa541130d9a 100644 --- a/root/static/scripts/common/i18n/expand2react.js +++ b/root/static/scripts/common/i18n/expand2react.js @@ -53,7 +53,7 @@ type Output = Expand2ReactOutput; const textContent = /^[^<>{}]+/; const condSubstThenTextContent = /^[^<>{}|]+/; const percentSign = /(%)/; -const linkSubstStart = /^\{([0-9A-z_]+)\|/; +const linkSubstStart = /^\{([0-9A-z_ ]+)\|/; const htmlTagStart = /^<(?=[a-z])/; const htmlTagName = /^(a|abbr|br|code|em|h1|h2|h3|h4|h5|h6|hr|li|ol|p|span|strong|ul)(?=[\s\/>])/; const htmlTagEnd = /^>/; diff --git a/root/static/scripts/tests/i18n/expand2.js b/root/static/scripts/tests/i18n/expand2.js index 16b6b846cc8..3f689e98d14 100644 --- a/root/static/scripts/tests/i18n/expand2.js +++ b/root/static/scripts/tests/i18n/expand2.js @@ -11,7 +11,7 @@ import expand2text, { } from '../../common/i18n/expand2text.js'; test('expand2', function (t) { - t.plan(69); + t.plan(70); let error = ''; const consoleError = console.error; @@ -46,6 +46,7 @@ test('expand2', function (t) { ); expandText('An {apple_fruit}', null, 'An {apple_fruit}'); expandText('An {apple_fruit}', {apple_fruit: 'apple'}, 'An apple'); + expandText('An {apple fruit}', {'apple fruit': 'apple'}, 'An apple'); expandText('A {number}', {number: 1}, 'A 1'); expandHtml('{null} value', {null: null}, ' value'); t.equal(error, ''); diff --git a/t/lib/t/MusicBrainz/Server/Translation.pm b/t/lib/t/MusicBrainz/Server/Translation.pm index 5950c67970c..ba4ac4830a1 100644 --- a/t/lib/t/MusicBrainz/Server/Translation.pm +++ b/t/lib/t/MusicBrainz/Server/Translation.pm @@ -12,9 +12,15 @@ test 'Check _expand_link' => sub { is(MusicBrainz::Server::Translation->instance->expand('An {apple_fruit}', apple_fruit => 'apple'), 'An apple', 'Simple replacement'); + is(MusicBrainz::Server::Translation->instance->expand('An {apple fruit}', + 'apple fruit' => 'apple'), + 'An apple', 'Simple replacement with spaces in key'); is(MusicBrainz::Server::Translation->instance->expand('An {apple_fruit|Apple}', apple_fruit => 'http://www.apple.com'), 'An Apple', 'Replacement with links'); + is(MusicBrainz::Server::Translation->instance->expand('An {apple fruit|Apple}', + 'apple fruit' => 'http://www.apple.com'), + 'An Apple', 'Replacement with links and spaces in key'); is(MusicBrainz::Server::Translation->instance->expand('A {apple_fruit|apple}', apple_fruit => 'http://www.apple.com', apple => 'pear'), 'A pear', 'Replacement with link description evaluation');