Skip to content

Commit

Permalink
Merge pull request #194 from bookernath/update-encodehtml
Browse files Browse the repository at this point in the history
Update encodeHtmlEntities helper to match paper-handlebars
  • Loading branch information
lord2800 authored Feb 24, 2020
2 parents 54bd4f3 + 62f83f3 commit 8790a28
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
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();
});
});

0 comments on commit 8790a28

Please sign in to comment.