Escape and ignore % and \, in translator args

This commit is contained in:
Peter Jaszkowiak
2016-12-01 17:06:53 -07:00
parent 1c6cee207f
commit 7c697759e9
2 changed files with 6 additions and 6 deletions

View File

@@ -105,7 +105,7 @@
} else if (text.slice(i, i + 2) === ']]') {
level -= 1;
i += 1;
} else if (level === 0 && text[i] === ',') {
} else if (level === 0 && text[i] === ',' && text[i - 1] !== '\\') {
arr.push(text.slice(brk, i).trim());
i += 1;
brk = i;
@@ -260,7 +260,8 @@
}
var out = translated;
translatedArgs.forEach(function (arg, i) {
out = out.replace(new RegExp('%' + (i + 1), 'g'), arg);
var escaped = arg.replace(/%/g, '%').replace(/\\,/g, ',');
out = out.replace(new RegExp('%' + (i + 1), 'g'), escaped);
});
return out;
});

View File

@@ -127,14 +127,13 @@ describe('new Translator(language)', function () {
});
});
it('should properly escape % and ,', function (done) {
it('should properly escape and ignore % and \\, in arguments', function (done) {
var translator = Translator.create('en-GB');
var title = 'Test 1, 2, 3 % salmon';
title = title.replace(/%/g, '%').replace(/,/g, ',');
var title = 'Test 1\\, 2\\, 3 % salmon';
var key = "[[topic:composer.replying_to, " + title + "]]";
translator.translate(key).then(function (translated) {
assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon');
assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon');
done();
});
});