1 | 'use strict';
|
2 | const he = require('he');
|
3 | const utils = require('handlebars-utils');
|
4 | const common = require('./../lib/common');
|
5 | const SafeString = require('handlebars').SafeString;
|
6 |
|
7 | function 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 |
|
22 | const allowedArguments = [
|
23 | 'useNamedReferences',
|
24 | 'decimal',
|
25 | 'encodeEverything',
|
26 | 'allowUnsafeSymbols'
|
27 | ];
|
28 |
|
29 |
|
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 |
|
40 | module.exports = helper;
|