Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update paper 2.x branch with encodeHtmlEntities helper and release #192

Merged
merged 1 commit into from
Jan 29, 2020
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unreleased

## 2.0.22 (2020-01-28)
- Add encodeHtmlEntities helper

## 2.0.21 (2020-01-17)
- Backport more permissive behavior of if helper in subexpressions[#190](https://github.com/bigcommerce/paper/pull/190)

Expand Down
18 changes: 18 additions & 0 deletions helpers/encodeHtmlEntities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const he = require('he');
const utils = require('handlebars-utils');
const common = require('./../lib/common');
const SafeString = require('handlebars').SafeString;

function helper(paper) {
paper.handlebars.registerHelper('encodeHtmlEntities', function(string) {
string = common.unwrapIfSafeString(string);
if (!utils.isString(string)){
throw new TypeError("Non-string passed to encodeHtmlEntities");
}

return new SafeString(he.encode(string));
});
}

module.exports = helper;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bigcommerce/stencil-paper",
"version": "2.0.21",
"version": "2.0.22",
"description": "A stencil plugin to register partials and helpers from handlebars and returns the compiled version for the stencil platform.",
"main": "index.js",
"author": "Bigcommerce",
Expand All @@ -26,6 +26,7 @@
"handlebars": "3.0.7",
"handlebars-helpers": "^0.8.0",
"handlebars-utils": "^1.0.6",
"he": "^1.2.0",
"lodash": "^3.6.0",
"messageformat": "^0.2.2",
"stringz": "^0.1.1",
Expand Down
33 changes: 33 additions & 0 deletions test/helpers/encodeHtmlEntities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var Code = require('code'),
Lab = require('lab'),
Paper = require('../../index'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
expect = Code.expect,
it = lab.it;

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

describe('encodeHtmlEntities helper', function() {
const context = {
string: 'foo © bar ≠ baz 𝌆 qux',
quotes: '"some" \'quotes\'',
};

it('should return a string with HTML entities encoded', function(done) {
expect(c('{{encodeHtmlEntities "foo © bar ≠ baz 𝌆 qux"}}', context))
.to.be.equal(`foo © bar ≠ baz 𝌆 qux`);
expect(c('{{encodeHtmlEntities string}}', context))
.to.be.equal(`foo © bar ≠ baz 𝌆 qux`);
expect(c('{{encodeHtmlEntities "string"}}', context))
.to.be.equal(`string`);
expect(c('{{encodeHtmlEntities quotes}}', context))
.to.be.equal('"some" 'quotes'');
expect(c('{{encodeHtmlEntities "an ampersand: &"}}', context))
.to.be.equal('an ampersand: &');
done();
});
});