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 encodeHtmlEntities helper to match paper-handlebars #194

Merged
merged 1 commit into from
Feb 24, 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.23 (2020-02-21)
- Support more `he` arguments on encodeHtmlEntities helper

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

Expand Down
24 changes: 23 additions & 1 deletion helpers/encodeHtmlEntities.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,29 @@ function helper(paper) {
throw new TypeError("Non-string passed to encodeHtmlEntities");
}

return new SafeString(he.encode(string));
const options = arguments[arguments.length - 1];

let args = {};

if (utils.isOptions(options)) {
args = options.hash;

// Whitelist of allowed named arguments into `he` function
const allowedArguments = [
'useNamedReferences',
'decimal',
'encodeEverything',
'allowUnsafeSymbols'
];

// Make sure all named arguments from options hash are in the whitelist and have boolean (string) values
if (Object.keys(args).some(key => !allowedArguments.includes(key))
|| !Object.keys(args).map(key => args[key]).every(val => ['true', 'false'].includes(val))) {
throw new TypeError("Invalid named argument passed to encodeHtmlEntities");
}
}

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

Expand Down
2 changes: 1 addition & 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.22",
"version": "2.0.23",
"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 Down
58 changes: 58 additions & 0 deletions test/helpers/encodeHtmlEntities.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe('encodeHtmlEntities helper', function() {
quotes: '"some" \'quotes\'',
};

// Some test cases lifted from https://github.com/mathiasbynens/he

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`);
Expand All @@ -30,4 +32,60 @@ describe('encodeHtmlEntities helper', function() {
.to.be.equal('an ampersand: &');
done();
});

it('should return a string with HTML entities encoded with named references', function(done) {
expect(c('{{encodeHtmlEntities "foo © bar ≠ baz 𝌆 qux" useNamedReferences="true"}}', context))
.to.be.equal(`foo © bar ≠ baz 𝌆 qux`);
done();
});

it('should return a string with HTML entities encoded with decimal option', function(done) {
expect(c('{{encodeHtmlEntities "foo © bar ≠ baz 𝌆 qux" decimal="true"}}', context))
.to.be.equal(`foo © bar ≠ baz 𝌆 qux`);
done();
});

it('should return a string with HTML entities encoded with named references and decimal option', function(done) {
expect(c('{{encodeHtmlEntities "foo © bar ≠ baz 𝌆 qux" useNamedReferences="true" decimal="true"}}', context))
.to.be.equal(`foo © bar ≠ baz 𝌆 qux`);
done();
});

it('should return a string with HTML entities encoded with encodeEverything option', function(done) {
expect(c('{{encodeHtmlEntities "foo © bar ≠ baz 𝌆 qux" encodeEverything="true"}}', context))
.to.be.equal(`foo © bar ≠ baz 𝌆 qux`);
done();
});

it('should return a string with HTML entities encoded with encodeEverything and useNamedReferences option', function(done) {
expect(c('{{encodeHtmlEntities "foo © bar ≠ baz 𝌆 qux" encodeEverything="true" useNamedReferences="true"}}', context))
.to.be.equal(`foo © bar ≠ baz 𝌆 qux`);
done();
});

it('should return a string with HTML entities encoded with allowUnsafeSymbols option', function(done) {
expect(c('{{encodeHtmlEntities "foo © and & ampersand" allowUnsafeSymbols="true"}}', context))
.to.be.equal(`foo © and & ampersand`);
done();
});

it('should throw an exception if an invalid named argument is passed', function (done) {
try {
c('{{encodeHtmlEntities "foo © bar ≠ baz 𝌆 qux" useNamedReferences="blah"}}');
} catch(e) {
}
try {
c('{{encodeHtmlEntities "foo © bar ≠ baz 𝌆 qux" blah="true"}}');
} catch(e) {
}
done();
});

it('should throw an exception if a non-string argument is passed', function (done) {
try {
c('{{encodeHtmlEntities 123}}');
} catch(e) {
}
done();
});
});