UNPKG

1.39 kBJavaScriptView Raw
1'use strict';
2const he = require('he');
3const utils = require('handlebars-utils');
4const common = require('./../lib/common');
5const SafeString = require('handlebars').SafeString;
6
7function helper(paper) {
8 paper.handlebars.registerHelper('encodeHtmlEntities', function(string) {
9 string = common.unwrapIfSafeString(string);
10 if (!utils.isString(string)){
11 throw new TypeError("Non-string passed to encodeHtmlEntities");
12 }
13
14 const options = arguments[arguments.length - 1];
15
16 let args = {};
17
18 if (utils.isOptions(options)) {
19 args = options.hash;
20
21 // Whitelist of allowed named arguments into `he` function
22 const allowedArguments = [
23 'useNamedReferences',
24 'decimal',
25 'encodeEverything',
26 'allowUnsafeSymbols'
27 ];
28
29 // Make sure all named arguments from options hash are in the whitelist and have boolean (string) values
30 if (Object.keys(args).some(key => !allowedArguments.includes(key))
31 || !Object.keys(args).map(key => args[key]).every(val => ['true', 'false'].includes(val))) {
32 throw new TypeError("Invalid named argument passed to encodeHtmlEntities");
33 }
34 }
35
36 return new SafeString(he.encode(string, args));
37 });
38}
39
40module.exports = helper;