Skip to content

Commit 241747a

Browse files
authored
fix: STRF-12276 Remove compile method from hbs renderer (#320)
1 parent f8f5929 commit 241747a

10 files changed

Lines changed: 88 additions & 9 deletions

File tree

helpers/3p/array.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ helpers.after = function(array, n) {
4242
* @api public
4343
*/
4444

45-
helpers.arrayify = function(value) {
45+
helpers.arrayify = function(...args) {
46+
args.pop(); // remove handlebars options object
47+
const value = args[0];
4648
return value ? (Array.isArray(value) ? value : [value]) : [];
4749
};
4850

helpers/3p/inflection.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ var helpers = module.exports;
1818
* @api public
1919
*/
2020

21-
helpers.inflect = function(count, singular, plural, include) {
21+
helpers.inflect = function(...args) {
22+
args.pop();
23+
const [count, singular, plural, include] = args;
2224
var word = (count > 1 || count === 0) ? plural : singular;
2325

2426
if (utils.isUndefined(include) || include === false) {

helpers/3p/misc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ var helpers = module.exports;
1616
* @api public
1717
*/
1818

19-
helpers.default = function(value, defaultValue) {
19+
helpers.default = function(...args) {
20+
args.pop();
21+
const value = args.shift();
22+
const defaultValue = args.shift();
2023
return !value
2124
? defaultValue
2225
: value;

helpers/deprecated/pick.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const factory = () => {
66
* @deprecate
77
*/
88
return function(...args) {
9+
args.pop();
910
const target = args.shift();
1011
const toReturn = {};
1112
const paths = args[0];

helpers/getImageSrcset.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const utils = require('./3p/utils');
33
const common = require('./lib/common');
44

55
const factory = globals => {
6-
return function (image, defaultImageUrl) {
6+
return function (...args) {
7+
args.pop();
8+
let [image, defaultImageUrl] = args;
79
// Regex to test size string is of the form 123x123 or 100w
810
const sizeRegex = /(^\d+w$)|(^(\d+?)x(\d+?)$)/;
911
// Regex to test to that srcset descriptor is of the form 1x 1.5x 2x OR 123w

helpers/getObject.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const { getValue } = require('./lib/common');
1010
* Property paths (`a.b.c`) may be used to get nested properties.
1111
*/
1212
const factory = (globals) => {
13-
return function (path, context) {
13+
return function (...args) {
14+
args.pop();
15+
let [path, context] = args;
1416
// use an empty context if none was given
1517
// (expect 3 args: `path`, `context`, and the `options` object
1618
// Handlebars always passes as the last argument to a helper)

helpers/toLowerCase.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

33
const factory = () => {
4-
return function(string) {
4+
return function(...args) {
5+
args.pop();
6+
const string = args[0];
57
if (typeof string !== 'string') {
68
return string;
79
}

helpers/truncate.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ const substring = require('stringz').substring;
2424
* {{lang (truncate 'blog.post.body.' 40) }}
2525
*/
2626
const factory = globals => {
27-
return function(string, length) {
27+
return function(...args) {
28+
args.pop();
29+
const [string, length] = args;
2830
if (typeof string !== 'string' || string.length === 0) {
2931
return string;
3032
}

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ class HandlebarsRenderer {
296296
context.locale_name = this._translator.getLocale();
297297
}
298298

299+
delete this.handlebars.compile;
300+
299301
// Look up the template
300302
const template = this.handlebars.partials[path];
301303
if (typeof template === 'undefined') {
@@ -333,11 +335,19 @@ class HandlebarsRenderer {
333335
*/
334336
renderString(template, context) {
335337
return new Promise((resolve, reject) => {
338+
let precompiledTemplate;
336339
context = context || {};
337340

341+
if (typeof template !== 'string') {
342+
return reject(new CompileError('Template must be a string'));
343+
}
344+
338345
// Compile the template
339346
try {
340-
template = this.handlebars.compile(template);
347+
delete this.handlebars.compile;
348+
const precompiled = this.handlebars.precompile(template, handlebarsOptions);
349+
eval(`precompiledTemplate = ${precompiled}`);
350+
template = this.handlebars.template(precompiledTemplate);
341351
} catch(e) {
342352
return reject(new CompileError(e.message));
343353
}

spec/index.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ describe('renderString', () => {
374374

375375
it('throws RenderError if given malformed template', done => {
376376
renderer.renderString('{{', context).catch(e => {
377-
expect(e instanceof HandlebarsRenderer.errors.RenderError).to.be.true();
377+
expect(e instanceof HandlebarsRenderer.errors.CompileError).to.be.true();
378378
done();
379379
});
380380
});
@@ -514,3 +514,56 @@ describe('logging', () => {
514514
});
515515

516516
});
517+
518+
// STRF-12276
519+
describe('object manipulation fix', () => {
520+
let sandbox, logger;
521+
let consoleErrorCopy = console.error;
522+
const templateString = `{{#JSONparse}} '{"a":"b"}'{{/JSONparse}}`;
523+
524+
beforeEach(done => {
525+
sandbox = Sinon.createSandbox();
526+
logger = {
527+
info: Sinon.fake(),
528+
warn: Sinon.fake(),
529+
error: Sinon.fake(),
530+
};
531+
console = {
532+
log: console.log,
533+
error: Sinon.fake(),
534+
};
535+
done();
536+
});
537+
538+
afterEach(done => {
539+
console.error = consoleErrorCopy;
540+
sandbox.restore();
541+
done();
542+
});
543+
544+
it('shouldnt print when use renderString', async () => {
545+
const renderer = new HandlebarsRenderer({}, {}, 'v4', logger);
546+
try {
547+
await renderer.renderString(templateString, {});
548+
expect(logger.error.called()).to.equal(false);
549+
} catch (e) {
550+
expect(e instanceof HandlebarsRenderer.errors.RenderError).to.be.true();
551+
}
552+
});
553+
554+
it('shouldnt print when use render', async () => {
555+
const renderer = new HandlebarsRenderer({}, {}, 'v4', logger);
556+
try {
557+
const templates = {'foo': templateString };
558+
559+
const processor = renderer.getPreProcessor();
560+
renderer.addTemplates(processor(templates));
561+
await renderer.render('foo', {});
562+
expect(logger.error.called()).to.equal(false);
563+
} catch (e) {
564+
expect(e instanceof HandlebarsRenderer.errors.RenderError).to.be.true();
565+
}
566+
});
567+
});
568+
569+

0 commit comments

Comments
 (0)