UNPKG

2.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Template = void 0;
4const tslib_1 = require("tslib");
5const templateScript_1 = tslib_1.__importDefault(require("./templateScript"));
6class Template {
7 constructor(html, placeholders = {}, options = {}, info = {}) {
8 this.html = html;
9 this.placeholders = placeholders;
10 this.options = options;
11 this.info = info;
12 }
13 render() {
14 this.replacePlaceholders();
15 this.injectJSVar('FORCE_OPTIONS', this.options);
16 this.injectJSVar('FORCE_INFO', this.info);
17 this.injectJSVar('FORCE_PLACEHOLDERS', this.placeholders);
18 this.injectHTML(templateScript_1.default);
19 this.wrapForm();
20 return this.rendered;
21 }
22 static htmlEntities(str) {
23 return String(str)
24 .replace(/&/g, '&')
25 .replace(/</g, '&lt;')
26 .replace(/>/g, '&gt;')
27 .replace(/"/g, '&quot;')
28 .replace(/'/g, '&#39;')
29 .replace(/`/g, '&#96;');
30 }
31 replacePlaceholders() {
32 this.rendered = this.html.replace(/\$\{\s?(\w+)\s?\|?\s?(\w*)\s?\}/g, (all, name, option) => {
33 if (name in this.placeholders) {
34 let value = this.placeholders[name];
35 if (Array.isArray(value) || value === Object(value)) {
36 value = JSON.stringify(value);
37 }
38 switch (option) {
39 case 'raw':
40 case 'html':
41 return value;
42 default:
43 return Template.htmlEntities(value);
44 }
45 }
46 return '';
47 });
48 }
49 injectHTML(html, prepend = false) {
50 if (prepend) {
51 this.rendered = html + this.rendered;
52 }
53 else {
54 this.rendered += html;
55 }
56 }
57 injectJSVar(name, value) {
58 const html = `<script>window.${name} = ${JSON.stringify(value)};</script>`;
59 this.injectHTML(html, true);
60 }
61 injectJSFile(url, prepend = false) {
62 const html = `<script src="${url}"></script>`;
63 this.injectHTML(html, prepend);
64 }
65 injectCSSFile(url, prepend = false) {
66 const html = `<link rel="stylesheet" type="text/css" href="${url}">`;
67 this.injectHTML(html, prepend);
68 }
69 wrapForm() {
70 this.rendered = `<meta charset="UTF-8" /><style>html {overflow-y: auto !important;}</style><form id="FORCE_FORM">${this.rendered}</form>`;
71 }
72}
73exports.Template = Template;
74//# sourceMappingURL=template.js.map
\No newline at end of file