Skip to content

Commit

Permalink
fix: STRF-12276 Remove compile method from hbs renderer (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc authored Jul 30, 2024
1 parent f8f5929 commit 241747a
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 9 deletions.
4 changes: 3 additions & 1 deletion helpers/3p/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ helpers.after = function(array, n) {
* @api public
*/

helpers.arrayify = function(value) {
helpers.arrayify = function(...args) {
args.pop(); // remove handlebars options object
const value = args[0];
return value ? (Array.isArray(value) ? value : [value]) : [];
};

Expand Down
4 changes: 3 additions & 1 deletion helpers/3p/inflection.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ var helpers = module.exports;
* @api public
*/

helpers.inflect = function(count, singular, plural, include) {
helpers.inflect = function(...args) {
args.pop();
const [count, singular, plural, include] = args;
var word = (count > 1 || count === 0) ? plural : singular;

if (utils.isUndefined(include) || include === false) {
Expand Down
5 changes: 4 additions & 1 deletion helpers/3p/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ var helpers = module.exports;
* @api public
*/

helpers.default = function(value, defaultValue) {
helpers.default = function(...args) {
args.pop();
const value = args.shift();
const defaultValue = args.shift();
return !value
? defaultValue
: value;
Expand Down
1 change: 1 addition & 0 deletions helpers/deprecated/pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const factory = () => {
* @deprecate
*/
return function(...args) {
args.pop();
const target = args.shift();
const toReturn = {};
const paths = args[0];
Expand Down
4 changes: 3 additions & 1 deletion helpers/getImageSrcset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const utils = require('./3p/utils');
const common = require('./lib/common');

const factory = globals => {
return function (image, defaultImageUrl) {
return function (...args) {
args.pop();
let [image, defaultImageUrl] = args;
// Regex to test size string is of the form 123x123 or 100w
const sizeRegex = /(^\d+w$)|(^(\d+?)x(\d+?)$)/;
// Regex to test to that srcset descriptor is of the form 1x 1.5x 2x OR 123w
Expand Down
4 changes: 3 additions & 1 deletion helpers/getObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const { getValue } = require('./lib/common');
* Property paths (`a.b.c`) may be used to get nested properties.
*/
const factory = (globals) => {
return function (path, context) {
return function (...args) {
args.pop();
let [path, context] = args;
// use an empty context if none was given
// (expect 3 args: `path`, `context`, and the `options` object
// Handlebars always passes as the last argument to a helper)
Expand Down
4 changes: 3 additions & 1 deletion helpers/toLowerCase.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const factory = () => {
return function(string) {
return function(...args) {
args.pop();
const string = args[0];
if (typeof string !== 'string') {
return string;
}
Expand Down
4 changes: 3 additions & 1 deletion helpers/truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const substring = require('stringz').substring;
* {{lang (truncate 'blog.post.body.' 40) }}
*/
const factory = globals => {
return function(string, length) {
return function(...args) {
args.pop();
const [string, length] = args;
if (typeof string !== 'string' || string.length === 0) {
return string;
}
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ class HandlebarsRenderer {
context.locale_name = this._translator.getLocale();
}

delete this.handlebars.compile;

// Look up the template
const template = this.handlebars.partials[path];
if (typeof template === 'undefined') {
Expand Down Expand Up @@ -333,11 +335,19 @@ class HandlebarsRenderer {
*/
renderString(template, context) {
return new Promise((resolve, reject) => {
let precompiledTemplate;
context = context || {};

if (typeof template !== 'string') {
return reject(new CompileError('Template must be a string'));
}

// Compile the template
try {
template = this.handlebars.compile(template);
delete this.handlebars.compile;
const precompiled = this.handlebars.precompile(template, handlebarsOptions);
eval(`precompiledTemplate = ${precompiled}`);
template = this.handlebars.template(precompiledTemplate);
} catch(e) {
return reject(new CompileError(e.message));
}
Expand Down
55 changes: 54 additions & 1 deletion spec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ describe('renderString', () => {

it('throws RenderError if given malformed template', done => {
renderer.renderString('{{', context).catch(e => {
expect(e instanceof HandlebarsRenderer.errors.RenderError).to.be.true();
expect(e instanceof HandlebarsRenderer.errors.CompileError).to.be.true();
done();
});
});
Expand Down Expand Up @@ -514,3 +514,56 @@ describe('logging', () => {
});

});

// STRF-12276
describe('object manipulation fix', () => {
let sandbox, logger;
let consoleErrorCopy = console.error;
const templateString = `{{#JSONparse}} '{"a":"b"}'{{/JSONparse}}`;

beforeEach(done => {
sandbox = Sinon.createSandbox();
logger = {
info: Sinon.fake(),
warn: Sinon.fake(),
error: Sinon.fake(),
};
console = {
log: console.log,
error: Sinon.fake(),
};
done();
});

afterEach(done => {
console.error = consoleErrorCopy;
sandbox.restore();
done();
});

it('shouldnt print when use renderString', async () => {
const renderer = new HandlebarsRenderer({}, {}, 'v4', logger);
try {
await renderer.renderString(templateString, {});
expect(logger.error.called()).to.equal(false);
} catch (e) {
expect(e instanceof HandlebarsRenderer.errors.RenderError).to.be.true();
}
});

it('shouldnt print when use render', async () => {
const renderer = new HandlebarsRenderer({}, {}, 'v4', logger);
try {
const templates = {'foo': templateString };

const processor = renderer.getPreProcessor();
renderer.addTemplates(processor(templates));
await renderer.render('foo', {});
expect(logger.error.called()).to.equal(false);
} catch (e) {
expect(e instanceof HandlebarsRenderer.errors.RenderError).to.be.true();
}
});
});


0 comments on commit 241747a

Please sign in to comment.