Skip to content
代码片段 群组 项目
提交 c37efd19 编辑于 作者: Jiaan Louw's avatar Jiaan Louw
浏览文件

Merge branch...

Merge branch '393918-using-certain-string-replacement-patterns-like-leads-to-faulty-interpolations' into 'master'

Fix dollar replacements in sprintf

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/113721



Merged-by: default avatarJiaan Louw <3468028-jiaan@users.noreply.gitlab.com>
Approved-by: default avatarJames Rushford <jrushford@gitlab.com>
Approved-by: default avatarJiaan Louw <3468028-jiaan@users.noreply.gitlab.com>
Reviewed-by: default avatarMiguel Rincon $' <mrincon@gitlab.com>
Reviewed-by: default avatarJiaan Louw <3468028-jiaan@users.noreply.gitlab.com>
Reviewed-by: default avatarJames Rushford <jrushford@gitlab.com>
Co-authored-by: default avatarMiguel Rincon <mrincon@gitlab.com>
No related branches found
No related tags found
无相关合并请求
......@@ -22,7 +22,9 @@ export default (input, parameters, escapeParameters = true) => {
mappedParameters.forEach((key, parameterName) => {
const parameterValue = mappedParameters.get(parameterName);
const escapedParameterValue = escapeParameters ? escape(parameterValue) : parameterValue;
output = output.replace(new RegExp(`%{${parameterName}}`, 'g'), escapedParameterValue);
// Pass the param value as a function to ignore special replacement patterns like $` and $'.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#syntax
output = output.replace(new RegExp(`%{${parameterName}}`, 'g'), () => escapedParameterValue);
});
}
......
......@@ -84,5 +84,16 @@ describe('locale', () => {
expect(output).toBe('contains duplicated 15%');
});
});
describe('ignores special replacements in the input', () => {
it.each(['$$', '$&', '$`', `$'`])('replacement "%s" is ignored', (replacement) => {
const input = 'My odd %{replacement} is preserved';
const parameters = { replacement };
const output = sprintf(input, parameters, false);
expect(output).toBe(`My odd ${replacement} is preserved`);
});
});
});
});
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册