Skip to content

Commit

Permalink
fix(FEC-8487): unescaped tokens causes token evalution to fail (#152)
Browse files Browse the repository at this point in the history
tring evaluation is done with a set of JSON parse and stringify on model.
The model data might be unescaped and contain illegal chars for parsing, so need to escape them before.
Add a check if value type is string and if it contains " and if so escape them with \ so they can be parsed.
  • Loading branch information
OrenMe authored Aug 21, 2018
1 parent c5f332f commit 3b86659
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/common/utils/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ function evaluate(template: string, model: Object = {}): string {
let reg,
res = template;
for (let key in model) {
let value = model[key] !== undefined && model[key] !== null ? model[key] : '';
if (typeof value === 'string' && value.indexOf('"') > -1) {
value = value.replace(/"/g, '\\"');
}
reg = new RegExp('{{' + key + '}}', 'g');
res = res.replace(reg, model[key] !== undefined && model[key] !== null ? model[key] : '');
res = res.replace(reg, value);
}
return res;
} catch (e) {
Expand Down
38 changes: 38 additions & 0 deletions test/src/common/utils/evaluate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import evaluate from '../../../../src/common/utils/evaluate';

describe('evaluate', function() {
const dataObject = {
a: {
a1: '{{token1}}',
a2: '{{token2}}'
},
b: {
b1: '{{token3}}',
b2: '{{token4}}'
}
};
const template = JSON.stringify(dataObject);
const model = {token1: 1, token2: 2};
const modelWithDoubleQuotes = {token1: 'my expression with "double quotes"', token2: 2};

it('should evaluate and replace template tokens which have model values', function() {
const evaluatedTemplate = evaluate(template, model);
const evaluatedDataObj = JSON.parse(evaluatedTemplate);
evaluatedDataObj.a.a1.should.be.equal('1');
evaluatedDataObj.a.a2.should.be.equal('2');
});

it("should evaluate and not replace template tokens which don't have model values", function() {
const evaluatedTemplate = evaluate(template, model);
const evaluatedDataObj = JSON.parse(evaluatedTemplate);
evaluatedDataObj.b.b1.should.be.equal('{{token3}}');
evaluatedDataObj.b.b2.should.be.equal('{{token4}}');
});

it('should escape model values which have double quotes', function() {
const evaluatedTemplate = evaluate(template, modelWithDoubleQuotes);
const evaluatedDataObj = JSON.parse(evaluatedTemplate);
evaluatedDataObj.a.a1.should.be.equal('my expression with "double quotes"');
evaluatedDataObj.a.a2.should.be.equal('2');
});
});

0 comments on commit 3b86659

Please sign in to comment.