Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions helpers/inject.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

function helper(paper) {
paper.handlebars.registerHelper('inject', function (key, value) {
paper.handlebars.registerHelper('inject', function (key, value, escape) {
if (typeof value === 'function') {
return;
}
Expand Down Expand Up @@ -31,8 +31,12 @@ function helper(paper) {
});
return filteredObject;
}

if (typeof escape === 'object') {
escape = false;
}

paper.inject[key] = filterValues(value);
paper.inject[key] = escape ? filterValues(value) : value;
});

paper.handlebars.registerHelper('jsContext', function () {
Expand Down
36 changes: 27 additions & 9 deletions test/helpers/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ var Code = require('code'),
it = lab.it;

function c(template, context) {
return new Paper().loadTemplatesSync({template: template}).render('template', context);
return new Paper().loadTemplatesSync({ template: template }).render('template', context);
}

describe('inject helper', function() {
const context = {
value1: "Big",
value2: "Commerce",
badChars: "&<>\"'`",
jsonString: JSON.stringify({"big": "commerce"}),
jsonString: JSON.stringify({ "big": "commerce" }),
nested: {
firstName: "&<>",
lastName: "\"'`",
Expand All @@ -36,12 +36,12 @@ describe('inject helper', function() {
done();
});

it('should escape strings', function(done) {
var template = "{{inject 'filtered' badChars}}{{jsContext}}";
it('should escape strings when escape is set to true', function(done) {
var template = "{{inject 'filtered' badChars true}}{{jsContext}}";

expect(c(template, context))
.to.be.equal('"{\\"filtered\\":\\"&amp;&lt;&gt;&quot;&#x27;&#x60;\\"}"');

done();
});

Expand All @@ -50,16 +50,34 @@ describe('inject helper', function() {

expect(c(template, context))
.to.be.equal('"{\\"filtered\\":\\"{\\\\\\"big\\\\\\":\\\\\\"commerce\\\\\\"}\\"}"');

done();
});

it('should escape strings nested in objects and arrays', function(done) {
var template = "{{inject 'filtered' nested}}{{jsContext}}";
it('should escape strings nested in objects and arrays when escape is set to true', function(done) {
var template = "{{inject 'filtered' nested true}}{{jsContext}}";

expect(c(template, context))
.to.be.equal('"{\\"filtered\\":{\\"firstName\\":\\"&amp;&lt;&gt;\\",\\"lastName\\":\\"&quot;&#x27;&#x60;\\",\\"addresses\\":[{\\"street\\":\\"123 &amp;&lt;&gt;&quot;&#x27;&#x60; St\\"}]}}"');

done()
});

it('should not escape characters by default', function(done) {
var template = "{{inject 'unfiltered' nested.firstName}}{{jsContext}}";

expect(c(template, context))
.to.be.equal('"{\\"unfiltered\\":\\"&<>\\"}"');

done();
})

it('should not escape characters when escape is set to false', function(done) {
var template = "{{inject 'unfiltered' nested.firstName false}}{{jsContext}}";

expect(c(template, context))
.to.be.equal('"{\\"unfiltered\\":\\"&<>\\"}"');

done();
})
});