0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Removed spaces and quotes from replacement ids (#15921)

fixes https://github.com/TryGhost/Team/issues/2350
This commit is contained in:
Simon Backx 2022-12-02 10:49:01 +01:00 committed by GitHub
parent 6fbda0730d
commit 1958d0db63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View file

@ -384,6 +384,7 @@ class EmailRenderer {
if (definition) {
replacements.push({
id: replacementStr,
originalId: recipientProperty,
token: new RegExp(escapeRegExp(replacementMatch), 'g'),
getValue: fallback ? (member => definition.getValue(member) || fallback) : definition.getValue
});
@ -391,6 +392,16 @@ class EmailRenderer {
}
}
// Now loop any replacements with possible invalid characters and replace them with a clean id
let counter = 1;
for (const replacement of replacements) {
if (replacement.id.match(/[^a-zA-Z0-9_]/)) {
counter += 1;
replacement.id = replacement.originalId + '_' + counter;
}
delete replacement.originalId;
}
return replacements;
}

View file

@ -56,7 +56,7 @@ describe('Email renderer', function () {
const replacements = emailRenderer.buildReplacementDefinitions({html, newsletter});
assert.equal(replacements.length, 1);
assert.equal(replacements[0].token.toString(), '/%%\\{first_name, "there"\\}%%/g');
assert.equal(replacements[0].id, 'first_name, "there"');
assert.equal(replacements[0].id, 'first_name_2');
assert.equal(replacements[0].getValue(member), 'Test');
// In case of empty name
@ -68,14 +68,14 @@ describe('Email renderer', function () {
const replacements = emailRenderer.buildReplacementDefinitions({html, newsletter});
assert.equal(replacements.length, 3);
assert.equal(replacements[0].token.toString(), '/%%\\{first_name, "there"\\}%%/g');
assert.equal(replacements[0].id, 'first_name, "there"');
assert.equal(replacements[0].id, 'first_name_2');
assert.equal(replacements[0].getValue(member), 'Test');
// In case of empty name
assert.equal(replacements[0].getValue({name: ''}), 'there');
assert.equal(replacements[1].token.toString(), '/%%\\{first_name, "member"\\}%%/g');
assert.equal(replacements[1].id, 'first_name, "member"');
assert.equal(replacements[1].id, 'first_name_3');
assert.equal(replacements[1].getValue(member), 'Test');
// In case of empty name