@kosatyi/ejs 0.0.107 → 0.0.108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -7
- package/bin/bundler.js +21 -9
- package/dist/cjs/browser.js +532 -651
- package/dist/cjs/bundler.js +105 -66
- package/dist/cjs/element.js +28 -63
- package/dist/cjs/index.js +540 -668
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/worker.js +553 -976
- package/dist/esm/browser.js +511 -540
- package/dist/esm/bundler.js +104 -66
- package/dist/esm/element.js +13 -47
- package/dist/esm/index.js +514 -555
- package/dist/esm/worker.js +519 -565
- package/dist/kosatyi-ejs-0.0.107.tgz +0 -0
- package/dist/umd/browser.js +533 -652
- package/dist/umd/browser.js.map +7 -0
- package/dist/umd/browser.min.js +1 -1
- package/dist/umd/browser.min.js.map +7 -0
- package/dist/umd/element.js +28 -63
- package/dist/umd/element.min.js +1 -1
- package/dist/umd/index.js +541 -669
- package/dist/umd/index.min.js +1 -1
- package/dist/umd/worker.js +819 -1242
- package/dist/umd/worker.min.js +1 -3
- package/package.json +80 -71
- package/types/browser.d.ts +10 -0
- package/types/bundler.d.ts +11 -12
- package/types/context.d.ts +96 -0
- package/types/ejs.d.ts +111 -73
- package/types/element.d.ts +9 -0
- package/types/error.d.ts +14 -0
- package/types/global.d.ts +6 -6
- package/types/index.d.ts +16 -0
- package/types/worker.d.ts +31 -0
package/dist/umd/browser.js
CHANGED
|
@@ -1,76 +1,111 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
3
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ejsInstance = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
/**
|
|
8
|
+
* @type {EjsConfig}
|
|
9
|
+
*/
|
|
10
|
+
const ejsDefaults = {
|
|
11
|
+
precompiled: 'ejsPrecompiled',
|
|
12
|
+
cache: true,
|
|
13
|
+
path: 'views',
|
|
14
|
+
extension: 'ejs',
|
|
15
|
+
rmWhitespace: true,
|
|
16
|
+
strict: true,
|
|
17
|
+
resolver: (path, template) => {
|
|
18
|
+
return Promise.resolve(['resolver is not defined', path, template].join(' '));
|
|
19
|
+
},
|
|
20
|
+
globals: [],
|
|
21
|
+
vars: {
|
|
22
|
+
SCOPE: 'ejs',
|
|
23
|
+
COMPONENT: 'ui',
|
|
24
|
+
ELEMENT: 'el',
|
|
25
|
+
EXTEND: '$$e',
|
|
26
|
+
BUFFER: '$$a',
|
|
27
|
+
LAYOUT: '$$l',
|
|
28
|
+
BLOCKS: '$$b',
|
|
29
|
+
MACRO: '$$m',
|
|
30
|
+
SAFE: '$$v'
|
|
31
|
+
},
|
|
32
|
+
token: {
|
|
33
|
+
start: '<%',
|
|
34
|
+
end: '%>',
|
|
35
|
+
regex: '([\\s\\S]+?)'
|
|
36
|
+
}
|
|
20
37
|
};
|
|
21
|
-
|
|
22
|
-
|
|
38
|
+
|
|
39
|
+
const jsVariableName = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
|
|
40
|
+
const typeProp = function () {
|
|
41
|
+
const args = [].slice.call(arguments);
|
|
42
|
+
const callback = args.shift();
|
|
43
|
+
return args.filter(callback).pop();
|
|
23
44
|
};
|
|
24
|
-
|
|
25
|
-
|
|
45
|
+
const isArray = value => Array.isArray(value);
|
|
46
|
+
const isFunction = value => typeof value === 'function';
|
|
47
|
+
const isString = value => typeof value === 'string';
|
|
48
|
+
const isBoolean = value => typeof value === 'boolean';
|
|
49
|
+
const isArrayOfVariables = value => {
|
|
50
|
+
if (!isArray(value)) return false;
|
|
51
|
+
return value.filter(name => {
|
|
52
|
+
const valid = jsVariableName.test(name);
|
|
53
|
+
if (valid === false) console.log(`ejsConfig.globals: expected '${name}' to be valid variable name --> skipped`);
|
|
54
|
+
return valid;
|
|
55
|
+
});
|
|
26
56
|
};
|
|
27
57
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
58
|
+
const configSchema = (config, options) => {
|
|
59
|
+
return Object.assign(config, {
|
|
60
|
+
path: typeProp(isString, ejsDefaults.path, config.path, options.path),
|
|
61
|
+
precompiled: typeProp(isString, ejsDefaults.precompiled, config.export, options.export),
|
|
62
|
+
resolver: typeProp(isFunction, ejsDefaults.resolver, config.resolver, options.resolver),
|
|
63
|
+
extension: typeProp(isString, ejsDefaults.extension, config.extension, options.extension),
|
|
64
|
+
strict: typeProp(isBoolean, ejsDefaults.strict, config.strict, options.strict),
|
|
65
|
+
rmWhitespace: typeProp(isBoolean, ejsDefaults.rmWhitespace, config.rmWhitespace, options.rmWhitespace),
|
|
66
|
+
cache: typeProp(isBoolean, ejsDefaults.cache, config.cache, options.cache),
|
|
67
|
+
globals: typeProp(isArray, ejsDefaults.globals, config.globals, isArrayOfVariables(options.globals)),
|
|
68
|
+
token: Object.assign({}, ejsDefaults.token, config.token, options.token),
|
|
69
|
+
vars: Object.assign({}, ejsDefaults.vars, config.vars, options.vars)
|
|
70
|
+
});
|
|
31
71
|
};
|
|
32
|
-
|
|
72
|
+
|
|
73
|
+
const symbolEntities = {
|
|
33
74
|
"'": "'",
|
|
34
75
|
'\\': '\\',
|
|
35
76
|
'\r': 'r',
|
|
36
77
|
'\n': 'n',
|
|
37
78
|
'\t': 't',
|
|
38
|
-
|
|
39
|
-
|
|
79
|
+
'\u2028': 'u2028',
|
|
80
|
+
'\u2029': 'u2029'
|
|
40
81
|
};
|
|
41
|
-
|
|
82
|
+
const htmlEntities = {
|
|
42
83
|
'&': '&',
|
|
43
84
|
'<': '<',
|
|
44
85
|
'>': '>',
|
|
45
86
|
'"': '"',
|
|
46
87
|
"'": '''
|
|
47
88
|
};
|
|
48
|
-
|
|
49
|
-
|
|
89
|
+
const regexKeys = obj => new RegExp(['[', Object.keys(obj).join(''), ']'].join(''), 'g');
|
|
90
|
+
const htmlEntitiesMatch = regexKeys(htmlEntities);
|
|
91
|
+
const symbolEntitiesMatch = regexKeys(symbolEntities);
|
|
92
|
+
const entities = function () {
|
|
93
|
+
let string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
94
|
+
return ('' + string).replace(htmlEntitiesMatch, match => htmlEntities[match]);
|
|
50
95
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
var entities = function entities() {
|
|
54
|
-
var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
55
|
-
return ('' + string).replace(htmlEntitiesMatch, function (match) {
|
|
56
|
-
return htmlEntities[match];
|
|
57
|
-
});
|
|
58
|
-
};
|
|
59
|
-
var symbols = function symbols(string) {
|
|
60
|
-
return ('' + string).replace(symbolEntitiesMatch, function (match) {
|
|
61
|
-
return '\\' + symbolEntities[match];
|
|
62
|
-
});
|
|
96
|
+
const symbols = string => {
|
|
97
|
+
return ('' + string).replace(symbolEntitiesMatch, match => '\\' + symbolEntities[match]);
|
|
63
98
|
};
|
|
64
|
-
|
|
65
|
-
|
|
99
|
+
const safeValue = (value, escape) => {
|
|
100
|
+
const check = value;
|
|
66
101
|
return check == null ? '' : Boolean(escape) === true ? entities(check) : check;
|
|
67
102
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
for (
|
|
73
|
-
|
|
103
|
+
const getPath = (context, name, strict) => {
|
|
104
|
+
let data = context;
|
|
105
|
+
let chunks = String(name).split('.');
|
|
106
|
+
let prop = chunks.pop();
|
|
107
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
108
|
+
const part = chunks[i];
|
|
74
109
|
if (isFunction(data['toJSON'])) {
|
|
75
110
|
data = data.toJSON();
|
|
76
111
|
}
|
|
@@ -85,344 +120,242 @@
|
|
|
85
120
|
}
|
|
86
121
|
return [data, prop];
|
|
87
122
|
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (ext !== defaults) {
|
|
91
|
-
path = [path, defaults].join('.');
|
|
92
|
-
}
|
|
93
|
-
return path;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @type {<T extends {}, U, V, W,Y>(T,U,V,W,Y)=> T & U & V & W & Y}
|
|
98
|
-
*/
|
|
99
|
-
var extend = function extend(target) {
|
|
100
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
101
|
-
args[_key - 1] = arguments[_key];
|
|
102
|
-
}
|
|
103
|
-
return args.filter(function (source) {
|
|
104
|
-
return source;
|
|
105
|
-
}).reduce(function (target, source) {
|
|
106
|
-
return Object.assign(target, source);
|
|
107
|
-
}, target);
|
|
108
|
-
};
|
|
109
|
-
var noop = function noop() {};
|
|
110
|
-
var each = function each(object, callback) {
|
|
111
|
-
var prop;
|
|
123
|
+
const each = (object, callback) => {
|
|
124
|
+
let prop;
|
|
112
125
|
for (prop in object) {
|
|
113
126
|
if (hasProp(object, prop)) {
|
|
114
127
|
callback(object[prop], prop, object);
|
|
115
128
|
}
|
|
116
129
|
}
|
|
117
130
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
});
|
|
126
|
-
return result;
|
|
127
|
-
};
|
|
128
|
-
var filter = function filter(object, callback, context) {
|
|
129
|
-
var isArray = object instanceof Array;
|
|
130
|
-
var result = isArray ? [] : {};
|
|
131
|
-
each(object, function (value, key, object) {
|
|
132
|
-
var item = callback(value, key, object);
|
|
133
|
-
if (isUndefined(item) === false) {
|
|
134
|
-
if (isArray) {
|
|
135
|
-
result.push(item);
|
|
136
|
-
} else {
|
|
137
|
-
result[key] = item;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
});
|
|
131
|
+
const omit = (object, list) => {
|
|
132
|
+
const result = {
|
|
133
|
+
...object
|
|
134
|
+
};
|
|
135
|
+
for (const key of list) {
|
|
136
|
+
delete result[key];
|
|
137
|
+
}
|
|
141
138
|
return result;
|
|
142
139
|
};
|
|
143
|
-
|
|
144
|
-
return
|
|
145
|
-
if (list.indexOf(key) === -1) {
|
|
146
|
-
return value;
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
*
|
|
153
|
-
* @param object
|
|
154
|
-
* @param prop
|
|
155
|
-
* @return {boolean}
|
|
156
|
-
*/
|
|
157
|
-
var hasProp = function hasProp(object, prop) {
|
|
158
|
-
return object && object.hasOwnProperty(prop);
|
|
140
|
+
const hasProp = (object, prop) => {
|
|
141
|
+
return object && Object.hasOwn(object, prop);
|
|
159
142
|
};
|
|
160
|
-
|
|
143
|
+
const joinPath = (path, template) => {
|
|
161
144
|
template = [path, template].join('/');
|
|
162
145
|
template = template.replace(/\/\//g, '/');
|
|
163
146
|
return template;
|
|
164
147
|
};
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return match;
|
|
174
|
-
});
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
var defaults = {};
|
|
178
|
-
defaults["export"] = 'ejsPrecompiled';
|
|
179
|
-
defaults.cache = true;
|
|
180
|
-
defaults.chokidar = null;
|
|
181
|
-
defaults.path = 'views';
|
|
182
|
-
defaults.resolver = function (path, template) {
|
|
183
|
-
return Promise.resolve(['resolver is not defined', path, template].join(' '));
|
|
184
|
-
};
|
|
185
|
-
defaults.extension = 'ejs';
|
|
186
|
-
defaults.rmWhitespace = true;
|
|
187
|
-
defaults.withObject = true;
|
|
188
|
-
defaults.globalHelpers = [];
|
|
189
|
-
defaults.vars = {
|
|
190
|
-
SCOPE: 'ejs',
|
|
191
|
-
COMPONENT: 'ui',
|
|
192
|
-
ELEMENT: 'el',
|
|
193
|
-
EXTEND: '$$e',
|
|
194
|
-
BUFFER: '$$a',
|
|
195
|
-
LAYOUT: '$$l',
|
|
196
|
-
BLOCKS: '$$b',
|
|
197
|
-
MACRO: '$$m',
|
|
198
|
-
SAFE: '$$v'
|
|
199
|
-
};
|
|
200
|
-
defaults.token = {
|
|
201
|
-
start: '<%',
|
|
202
|
-
end: '%>',
|
|
203
|
-
regex: '([\\s\\S]+?)'
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
var configSchema = function configSchema(config, options) {
|
|
207
|
-
return extend(config, {
|
|
208
|
-
path: typeProp(isString, defaults.path, config.path, options.path),
|
|
209
|
-
"export": typeProp(isString, defaults["export"], config["export"], options["export"]),
|
|
210
|
-
resolver: typeProp(isFunction, defaults.resolver, config.resolver, options.resolver),
|
|
211
|
-
extension: typeProp(isString, defaults.extension, config.extension, options.extension),
|
|
212
|
-
withObject: typeProp(isBoolean, defaults.withObject, config.withObject, options.withObject),
|
|
213
|
-
rmWhitespace: typeProp(isBoolean, defaults.rmWhitespace, config.rmWhitespace, options.rmWhitespace),
|
|
214
|
-
cache: typeProp(isBoolean, defaults.cache, config.cache, options.cache),
|
|
215
|
-
token: extend({}, defaults.token, config.token, options.token),
|
|
216
|
-
vars: extend({}, defaults.vars, config.vars, options.vars),
|
|
217
|
-
globalHelpers: typeProp(isArray, defaults.globalHelpers, config.globalHelpers, options.globalHelpers)
|
|
218
|
-
});
|
|
148
|
+
const bindContext = function (object) {
|
|
149
|
+
let methods = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
150
|
+
for (let i = 0, len = methods.length; i < len; i++) {
|
|
151
|
+
const name = methods[i];
|
|
152
|
+
if (name in object) {
|
|
153
|
+
object[name] = object[name].bind(object);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
219
156
|
};
|
|
220
157
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
return compiler.compile(content, template);
|
|
158
|
+
class Template {
|
|
159
|
+
#path;
|
|
160
|
+
#resolver;
|
|
161
|
+
#cache;
|
|
162
|
+
#compiler;
|
|
163
|
+
static exports = ['configure', 'get', 'compile'];
|
|
164
|
+
constructor(options, cache, compiler) {
|
|
165
|
+
bindContext(this, this.constructor.exports);
|
|
166
|
+
this.#cache = cache;
|
|
167
|
+
this.#compiler = compiler;
|
|
168
|
+
this.configure(options ?? {});
|
|
169
|
+
}
|
|
170
|
+
configure(options) {
|
|
171
|
+
this.#path = options.path;
|
|
172
|
+
if (isFunction(options.resolver)) {
|
|
173
|
+
this.#resolver = options.resolver;
|
|
238
174
|
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
175
|
+
}
|
|
176
|
+
#resolve(template) {
|
|
177
|
+
const cached = this.#cache.get(template);
|
|
178
|
+
if (cached instanceof Promise) return cached;
|
|
179
|
+
const result = Promise.resolve(this.#resolver(this.#path, template));
|
|
180
|
+
this.#cache.set(template, result);
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
compile(content, template) {
|
|
184
|
+
const cached = this.#cache.get(template);
|
|
185
|
+
if (typeof cached === 'function') return cached;
|
|
186
|
+
if (typeof content === 'string') {
|
|
187
|
+
content = this.#compiler.compile(content, template);
|
|
243
188
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
};
|
|
248
|
-
var configure = function configure(options) {
|
|
249
|
-
config.path = options.path;
|
|
250
|
-
if (isFunction(options.resolver)) {
|
|
251
|
-
config.resolver = options.resolver;
|
|
189
|
+
if (typeof content === 'function') {
|
|
190
|
+
this.#cache.set(template, content);
|
|
191
|
+
return content;
|
|
252
192
|
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
compile: compile
|
|
259
|
-
};
|
|
260
|
-
};
|
|
193
|
+
}
|
|
194
|
+
get(template) {
|
|
195
|
+
return this.#resolve(template).then(content => this.compile(content, template));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
261
198
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
config.
|
|
287
|
-
config.
|
|
288
|
-
config.
|
|
289
|
-
config.vars = options.vars;
|
|
290
|
-
config.globalHelpers = options.globalHelpers;
|
|
291
|
-
config.matches = [];
|
|
292
|
-
config.formats = [];
|
|
293
|
-
config.slurp = {
|
|
199
|
+
const tokenList = [['-', (v, b, s) => `')\n${b}(${s}(${v},1))\n${b}('`], ['=', (v, b, s) => `')\n${b}(${s}(${v}))\n${b}('`], ['#', (v, b) => `')\n/**${v}**/\n${b}('`], ['', (v, b) => `')\n${v}\n${b}('`]];
|
|
200
|
+
const tokensMatch = (regex, content, callback) => {
|
|
201
|
+
let index = 0;
|
|
202
|
+
content.replace(regex, function () {
|
|
203
|
+
const params = [].slice.call(arguments, 0, -1);
|
|
204
|
+
const offset = params.pop();
|
|
205
|
+
const match = params.shift();
|
|
206
|
+
callback(params, index, offset);
|
|
207
|
+
index = offset + match.length;
|
|
208
|
+
return match;
|
|
209
|
+
});
|
|
210
|
+
};
|
|
211
|
+
class Compiler {
|
|
212
|
+
#config = {};
|
|
213
|
+
static exports = ['compile'];
|
|
214
|
+
constructor(options) {
|
|
215
|
+
bindContext(this, this.constructor.exports);
|
|
216
|
+
this.configure(options);
|
|
217
|
+
}
|
|
218
|
+
configure(options) {
|
|
219
|
+
this.#config.strict = options.strict;
|
|
220
|
+
this.#config.rmWhitespace = options.rmWhitespace;
|
|
221
|
+
this.#config.token = options.token;
|
|
222
|
+
this.#config.vars = options.vars;
|
|
223
|
+
this.#config.globals = options.globals;
|
|
224
|
+
this.#config.legacy = options.legacy ?? true;
|
|
225
|
+
this.#config.slurp = {
|
|
294
226
|
match: '[s\t\n]*',
|
|
295
|
-
start: [config.token.start, '_'],
|
|
296
|
-
end: ['_', config.token.end]
|
|
227
|
+
start: [this.#config.token.start, '_'],
|
|
228
|
+
end: ['_', this.#config.token.end]
|
|
297
229
|
};
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
config.
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
230
|
+
this.#config.matches = [];
|
|
231
|
+
this.#config.formats = [];
|
|
232
|
+
for (const [symbol, format] of tokenList) {
|
|
233
|
+
this.#config.matches.push(this.#config.token.start.concat(symbol).concat(this.#config.token.regex).concat(this.#config.token.end));
|
|
234
|
+
this.#config.formats.push(format);
|
|
235
|
+
}
|
|
236
|
+
this.#config.regex = new RegExp(this.#config.matches.join('|').concat('|$'), 'g');
|
|
237
|
+
this.#config.slurpStart = new RegExp([this.#config.slurp.match, this.#config.slurp.start.join('')].join(''), 'gm');
|
|
238
|
+
this.#config.slurpEnd = new RegExp([this.#config.slurp.end.join(''), this.#config.slurp.match].join(''), 'gm');
|
|
239
|
+
if (this.#config.globals.length) {
|
|
240
|
+
if (this.#config.legacy) {
|
|
241
|
+
this.#config.globalVariables = `const ${this.#config.globals.map(name => `${name}=${this.#config.vars.SCOPE}.${name}`).join(',')};`;
|
|
242
|
+
} else {
|
|
243
|
+
this.#config.globalVariables = `const {${this.#config.globals.join(',')}} = ${this.#config.vars.SCOPE};`;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
compile(content, path) {
|
|
248
|
+
const GLOBALS = this.#config.globalVariables;
|
|
249
|
+
const {
|
|
250
|
+
SCOPE,
|
|
251
|
+
SAFE,
|
|
252
|
+
BUFFER,
|
|
253
|
+
COMPONENT,
|
|
254
|
+
ELEMENT
|
|
255
|
+
} = this.#config.vars;
|
|
256
|
+
if (this.#config.rmWhitespace) {
|
|
315
257
|
content = String(content).replace(/[\r\n]+/g, '\n').replace(/^\s+|\s+$/gm, '');
|
|
316
258
|
}
|
|
317
|
-
content = String(content).replace(config.slurpStart, config.token.start).replace(config.slurpEnd, config.token.end);
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
params.forEach(
|
|
259
|
+
content = String(content).replace(this.#config.slurpStart, this.#config.token.start).replace(this.#config.slurpEnd, this.#config.token.end);
|
|
260
|
+
let OUTPUT = `${BUFFER}('`;
|
|
261
|
+
tokensMatch(this.#config.regex, content, (params, index, offset) => {
|
|
262
|
+
OUTPUT += symbols(content.slice(index, offset));
|
|
263
|
+
params.forEach((value, index) => {
|
|
322
264
|
if (value) {
|
|
323
|
-
|
|
265
|
+
OUTPUT += this.#config.formats[index](value.trim(), BUFFER, SAFE);
|
|
324
266
|
}
|
|
325
267
|
});
|
|
326
268
|
});
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (config.
|
|
330
|
-
|
|
269
|
+
OUTPUT += `');`;
|
|
270
|
+
OUTPUT = `try{${OUTPUT}}catch(e){return ${BUFFER}.error(e,'${path}')}`;
|
|
271
|
+
if (this.#config.strict === false) {
|
|
272
|
+
OUTPUT = `with(${SCOPE}){${OUTPUT}}`;
|
|
273
|
+
}
|
|
274
|
+
OUTPUT = `${BUFFER}.start();${OUTPUT}return ${BUFFER}.end();`;
|
|
275
|
+
OUTPUT += `\n//# sourceURL=${path}`;
|
|
276
|
+
if (GLOBALS) {
|
|
277
|
+
OUTPUT = `${GLOBALS}\n${OUTPUT}`;
|
|
331
278
|
}
|
|
332
|
-
source = "".concat(BUFFER, ".start();").concat(source, "return ").concat(BUFFER, ".end();");
|
|
333
|
-
source += "\n//# sourceURL=".concat(path);
|
|
334
|
-
var result = null;
|
|
335
|
-
var params = [SCOPE, COMPONENT, ELEMENT, BUFFER, SAFE].concat(GLOBALS);
|
|
336
279
|
try {
|
|
337
|
-
|
|
338
|
-
result
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
280
|
+
const params = [SCOPE, COMPONENT, ELEMENT, BUFFER, SAFE];
|
|
281
|
+
const result = Function.apply(null, params.concat(OUTPUT));
|
|
282
|
+
result.source = `(function(${params.join(',')}){\n${OUTPUT}\n});`;
|
|
283
|
+
return result;
|
|
284
|
+
} catch (error) {
|
|
285
|
+
error.filename = path;
|
|
286
|
+
error.source = OUTPUT;
|
|
287
|
+
throw error;
|
|
343
288
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
configure(options);
|
|
347
|
-
return {
|
|
348
|
-
configure: configure,
|
|
349
|
-
compile: compile
|
|
350
|
-
};
|
|
351
|
-
};
|
|
289
|
+
}
|
|
290
|
+
}
|
|
352
291
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
292
|
+
class Cache {
|
|
293
|
+
static exports = ['load', 'set', 'get', 'exist', 'clear', 'remove', 'resolve'];
|
|
294
|
+
#cache = true;
|
|
295
|
+
#precompiled;
|
|
296
|
+
#list = {};
|
|
297
|
+
constructor(options) {
|
|
298
|
+
bindContext(this, this.constructor.exports);
|
|
299
|
+
this.configure(options);
|
|
300
|
+
}
|
|
301
|
+
get(key) {
|
|
302
|
+
if (this.#cache) {
|
|
303
|
+
return this.#list[key];
|
|
361
304
|
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
if (
|
|
365
|
-
|
|
305
|
+
}
|
|
306
|
+
set(key, value) {
|
|
307
|
+
if (this.#cache) {
|
|
308
|
+
this.#list[key] = value;
|
|
366
309
|
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
if (
|
|
370
|
-
list
|
|
310
|
+
}
|
|
311
|
+
exist(key) {
|
|
312
|
+
if (this.#cache) {
|
|
313
|
+
return this.#list.hasOwnProperty(key);
|
|
371
314
|
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
315
|
+
}
|
|
316
|
+
clear() {
|
|
317
|
+
Object.keys(this.#list).forEach(this.remove);
|
|
318
|
+
}
|
|
319
|
+
remove(key) {
|
|
320
|
+
delete this.#list[key];
|
|
321
|
+
}
|
|
322
|
+
resolve(key) {
|
|
323
|
+
return Promise.resolve(this.get(key));
|
|
324
|
+
}
|
|
325
|
+
load(data) {
|
|
326
|
+
if (this.#cache) {
|
|
327
|
+
Object.assign(this.#list, data || {});
|
|
376
328
|
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
};
|
|
384
|
-
var resolve = function resolve(key) {
|
|
385
|
-
return Promise.resolve(get(key));
|
|
386
|
-
};
|
|
387
|
-
var configure = function configure() {
|
|
388
|
-
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
389
|
-
config.enabled = options.cache;
|
|
390
|
-
config["export"] = options["export"];
|
|
391
|
-
if (isNode() === false) {
|
|
392
|
-
load(global[config["export"]]);
|
|
329
|
+
}
|
|
330
|
+
configure(options) {
|
|
331
|
+
this.#cache = options.cache;
|
|
332
|
+
this.#precompiled = options.precompiled;
|
|
333
|
+
if (typeof window === 'object') {
|
|
334
|
+
this.load(window[this.#precompiled]);
|
|
393
335
|
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
return {
|
|
397
|
-
configure: configure,
|
|
398
|
-
load: load,
|
|
399
|
-
set: set,
|
|
400
|
-
get: get,
|
|
401
|
-
exist: exist,
|
|
402
|
-
clear: clear,
|
|
403
|
-
remove: remove,
|
|
404
|
-
resolve: resolve
|
|
405
|
-
};
|
|
406
|
-
};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
407
338
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
339
|
+
const selfClosed = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
|
|
340
|
+
const space = ' ';
|
|
341
|
+
const quote = '"';
|
|
342
|
+
const equal = '=';
|
|
343
|
+
const slash = '/';
|
|
344
|
+
const lt = '<';
|
|
345
|
+
const gt = '>';
|
|
346
|
+
const eachAttribute = _ref => {
|
|
347
|
+
let [key, value] = _ref;
|
|
348
|
+
if (value !== null && value !== undefined) {
|
|
349
|
+
return [entities(key), [quote, entities(value), quote].join('')].join(equal);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
const element = (tag, attrs, content) => {
|
|
353
|
+
const result = [];
|
|
354
|
+
const hasClosedTag = selfClosed.indexOf(tag) === -1;
|
|
355
|
+
const attributes = Object.entries(attrs ?? {}).map(eachAttribute).filter(e => e).join(space);
|
|
423
356
|
result.push([lt, tag, space, attributes, gt].join(''));
|
|
424
357
|
if (content && hasClosedTag) {
|
|
425
|
-
result.push(content
|
|
358
|
+
result.push(Array.isArray(content) ? content.join('') : content);
|
|
426
359
|
}
|
|
427
360
|
if (hasClosedTag) {
|
|
428
361
|
result.push([lt, slash, tag, gt].join(''));
|
|
@@ -430,237 +363,204 @@
|
|
|
430
363
|
return result.join('');
|
|
431
364
|
};
|
|
432
365
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
code: 0,
|
|
443
|
-
getCode: function getCode() {
|
|
444
|
-
return this.code;
|
|
445
|
-
},
|
|
446
|
-
getMessage: function getMessage() {
|
|
447
|
-
return this.message;
|
|
448
|
-
},
|
|
449
|
-
toString: function toString() {
|
|
450
|
-
return this.getMessage();
|
|
366
|
+
class TemplateError extends Error {
|
|
367
|
+
name = 'TemplateError';
|
|
368
|
+
constructor(error) {
|
|
369
|
+
super(error);
|
|
370
|
+
if (error instanceof Error) {
|
|
371
|
+
this.stack = error.stack;
|
|
372
|
+
this.filename = error.filename;
|
|
373
|
+
this.lineno = error.lineno;
|
|
374
|
+
}
|
|
451
375
|
}
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
*
|
|
456
|
-
* @constructor
|
|
457
|
-
*/
|
|
458
|
-
function TemplateNotFound() {
|
|
459
|
-
TemplateError.call(this);
|
|
460
376
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
code
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
/**
|
|
467
|
-
*
|
|
468
|
-
* @constructor
|
|
469
|
-
*/
|
|
470
|
-
function TemplateSyntaxError() {
|
|
471
|
-
TemplateError.call(this);
|
|
472
|
-
}
|
|
473
|
-
Object.setPrototypeOf(TemplateSyntaxError.prototype, TemplateError.prototype);
|
|
474
|
-
Object.assign(TemplateSyntaxError.prototype, {
|
|
475
|
-
code: 500
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
function resolve(list) {
|
|
479
|
-
return Promise.all(list || []).then(function (list) {
|
|
480
|
-
return list.join('');
|
|
481
|
-
})["catch"](function (e) {
|
|
482
|
-
return e;
|
|
483
|
-
});
|
|
377
|
+
class TemplateNotFound extends TemplateError {
|
|
378
|
+
name = 'TemplateNotFound';
|
|
379
|
+
code = 404;
|
|
484
380
|
}
|
|
485
|
-
|
|
486
|
-
|
|
381
|
+
class TemplateSyntaxError extends TemplateError {
|
|
382
|
+
name = 'TemplateSyntaxError';
|
|
383
|
+
code = 500;
|
|
487
384
|
}
|
|
488
385
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
386
|
+
const resolve = list => {
|
|
387
|
+
return Promise.all(list || []).then(list => list.join('')).catch(e => e);
|
|
388
|
+
};
|
|
389
|
+
const reject = error => {
|
|
390
|
+
return Promise.reject(new TemplateSyntaxError(error));
|
|
391
|
+
};
|
|
392
|
+
const EjsBuffer = () => {
|
|
393
|
+
let store = [];
|
|
394
|
+
let array = [];
|
|
395
|
+
/**
|
|
396
|
+
*
|
|
397
|
+
* @param value
|
|
398
|
+
* @constructor
|
|
399
|
+
*/
|
|
400
|
+
const EjsBuffer = value => {
|
|
497
401
|
array.push(value);
|
|
498
402
|
};
|
|
499
|
-
|
|
403
|
+
EjsBuffer.start = () => {
|
|
500
404
|
array = [];
|
|
501
405
|
};
|
|
502
|
-
|
|
406
|
+
EjsBuffer.backup = () => {
|
|
503
407
|
store.push(array.concat());
|
|
504
408
|
array = [];
|
|
505
409
|
};
|
|
506
|
-
|
|
507
|
-
|
|
410
|
+
EjsBuffer.restore = () => {
|
|
411
|
+
const result = array.concat();
|
|
508
412
|
array = store.pop();
|
|
509
413
|
return resolve(result);
|
|
510
414
|
};
|
|
511
|
-
|
|
415
|
+
EjsBuffer.error = (e, filename) => {
|
|
512
416
|
return reject(e);
|
|
513
417
|
};
|
|
514
|
-
|
|
418
|
+
EjsBuffer.end = () => {
|
|
515
419
|
return resolve(array);
|
|
516
420
|
};
|
|
517
|
-
return
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
var PARENT = Symbol('ContextScope.parentTemplate');
|
|
521
|
-
var createContextScope = function createContextScope(config, methods) {
|
|
522
|
-
var _config$vars = config.vars,
|
|
523
|
-
BLOCKS = _config$vars.BLOCKS,
|
|
524
|
-
MACRO = _config$vars.MACRO,
|
|
525
|
-
EXTEND = _config$vars.EXTEND,
|
|
526
|
-
LAYOUT = _config$vars.LAYOUT,
|
|
527
|
-
BUFFER = _config$vars.BUFFER,
|
|
528
|
-
SAFE = _config$vars.SAFE,
|
|
529
|
-
SCOPE = _config$vars.SCOPE,
|
|
530
|
-
COMPONENT = _config$vars.COMPONENT,
|
|
531
|
-
ELEMENT = _config$vars.ELEMENT;
|
|
532
|
-
/**
|
|
533
|
-
*
|
|
534
|
-
* @type {symbol}
|
|
535
|
-
*/
|
|
421
|
+
return EjsBuffer;
|
|
422
|
+
};
|
|
536
423
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
424
|
+
const PARENT = Symbol('EjsContext.parentTemplate');
|
|
425
|
+
const createContext$1 = (config, methods) => {
|
|
426
|
+
const {
|
|
427
|
+
BLOCKS,
|
|
428
|
+
MACRO,
|
|
429
|
+
EXTEND,
|
|
430
|
+
LAYOUT,
|
|
431
|
+
BUFFER,
|
|
432
|
+
SAFE,
|
|
433
|
+
SCOPE,
|
|
434
|
+
COMPONENT,
|
|
435
|
+
ELEMENT
|
|
436
|
+
} = config.vars;
|
|
437
|
+
const globals = config.globals || [];
|
|
438
|
+
function EjsContext(data) {
|
|
543
439
|
this[PARENT] = null;
|
|
544
440
|
this[BLOCKS] = {};
|
|
545
441
|
this[MACRO] = {};
|
|
546
442
|
Object.assign(this, omit(data, [SCOPE, BUFFER, SAFE, COMPONENT, ELEMENT]));
|
|
547
443
|
}
|
|
548
|
-
Object.
|
|
549
|
-
|
|
550
|
-
value
|
|
444
|
+
Object.entries(methods).forEach(_ref => {
|
|
445
|
+
let [name, value] = _ref;
|
|
446
|
+
if (isFunction(value) && globals.includes(name)) {
|
|
447
|
+
value = value.bind(EjsContext.prototype);
|
|
448
|
+
}
|
|
449
|
+
EjsContext.prototype[name] = value;
|
|
450
|
+
});
|
|
451
|
+
Object.defineProperty(EjsContext.prototype, BUFFER, {
|
|
452
|
+
value: EjsBuffer()
|
|
551
453
|
});
|
|
552
|
-
Object.defineProperty(
|
|
454
|
+
Object.defineProperty(EjsContext.prototype, BLOCKS, {
|
|
553
455
|
value: {},
|
|
554
456
|
writable: true
|
|
555
457
|
});
|
|
556
|
-
Object.defineProperty(
|
|
458
|
+
Object.defineProperty(EjsContext.prototype, MACRO, {
|
|
557
459
|
value: {},
|
|
558
460
|
writable: true
|
|
559
461
|
});
|
|
560
|
-
Object.defineProperty(
|
|
462
|
+
Object.defineProperty(EjsContext.prototype, LAYOUT, {
|
|
561
463
|
value: false,
|
|
562
464
|
writable: true
|
|
563
465
|
});
|
|
564
|
-
Object.defineProperty(
|
|
466
|
+
Object.defineProperty(EjsContext.prototype, EXTEND, {
|
|
565
467
|
value: false,
|
|
566
468
|
writable: true
|
|
567
469
|
});
|
|
568
|
-
Object.defineProperty(
|
|
470
|
+
Object.defineProperty(EjsContext.prototype, PARENT, {
|
|
569
471
|
value: null,
|
|
570
472
|
writable: true
|
|
571
473
|
});
|
|
572
|
-
Object.defineProperties(
|
|
474
|
+
Object.defineProperties(EjsContext.prototype, {
|
|
573
475
|
/** @type {function} */
|
|
574
476
|
setParentTemplate: {
|
|
575
|
-
value
|
|
576
|
-
this[PARENT] =
|
|
477
|
+
value(value) {
|
|
478
|
+
this[PARENT] = value;
|
|
577
479
|
return this;
|
|
578
480
|
}
|
|
579
481
|
},
|
|
580
482
|
/** @type {function} */
|
|
581
483
|
getParentTemplate: {
|
|
582
|
-
value
|
|
484
|
+
value() {
|
|
583
485
|
return this[PARENT];
|
|
584
486
|
}
|
|
585
487
|
},
|
|
586
488
|
/** @type {function} */
|
|
587
489
|
useSafeValue: {
|
|
588
|
-
get:
|
|
589
|
-
return safeValue;
|
|
590
|
-
}
|
|
490
|
+
get: () => safeValue
|
|
591
491
|
},
|
|
592
492
|
/** @type {function} */
|
|
593
493
|
useComponent: {
|
|
594
|
-
get
|
|
494
|
+
get() {
|
|
595
495
|
if (isFunction(this[COMPONENT])) {
|
|
596
496
|
return this[COMPONENT].bind(this);
|
|
597
497
|
} else {
|
|
598
|
-
return
|
|
599
|
-
throw new Error(
|
|
498
|
+
return () => {
|
|
499
|
+
throw new Error(`${COMPONENT} must be a function`);
|
|
600
500
|
};
|
|
601
501
|
}
|
|
602
502
|
}
|
|
603
503
|
},
|
|
604
504
|
/** @type {function} */
|
|
605
505
|
useElement: {
|
|
606
|
-
get
|
|
506
|
+
get() {
|
|
607
507
|
if (isFunction(this[ELEMENT])) {
|
|
608
508
|
return this[ELEMENT].bind(this);
|
|
609
509
|
} else {
|
|
610
|
-
return
|
|
611
|
-
throw new Error(
|
|
510
|
+
return () => {
|
|
511
|
+
throw new Error(`${ELEMENT} must be a function`);
|
|
612
512
|
};
|
|
613
513
|
}
|
|
614
514
|
}
|
|
615
515
|
},
|
|
616
|
-
/** @type {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
return this[
|
|
516
|
+
/** @type {function} */
|
|
517
|
+
useBuffer: {
|
|
518
|
+
get() {
|
|
519
|
+
return this[BUFFER];
|
|
620
520
|
}
|
|
621
521
|
},
|
|
622
522
|
/** @type {function} */
|
|
623
|
-
|
|
624
|
-
value
|
|
625
|
-
return this[
|
|
523
|
+
getMacro: {
|
|
524
|
+
value() {
|
|
525
|
+
return this[MACRO];
|
|
626
526
|
}
|
|
627
527
|
},
|
|
628
528
|
/** @type {function} */
|
|
629
529
|
getBlocks: {
|
|
630
|
-
value
|
|
530
|
+
value() {
|
|
631
531
|
return this[BLOCKS];
|
|
632
532
|
}
|
|
633
533
|
},
|
|
634
534
|
/** @type {function} */
|
|
635
535
|
setExtend: {
|
|
636
|
-
value
|
|
637
|
-
this[EXTEND] =
|
|
536
|
+
value(value) {
|
|
537
|
+
this[EXTEND] = value;
|
|
638
538
|
return this;
|
|
639
539
|
}
|
|
640
540
|
},
|
|
641
541
|
/** @type {function} */
|
|
642
542
|
getExtend: {
|
|
643
|
-
value
|
|
543
|
+
value() {
|
|
644
544
|
return this[EXTEND];
|
|
645
545
|
}
|
|
646
546
|
},
|
|
647
547
|
/** @type {function} */
|
|
648
548
|
setLayout: {
|
|
649
|
-
value
|
|
549
|
+
value(layout) {
|
|
650
550
|
this[LAYOUT] = layout;
|
|
651
551
|
return this;
|
|
652
552
|
}
|
|
653
553
|
},
|
|
654
554
|
/** @type {function} */
|
|
655
555
|
getLayout: {
|
|
656
|
-
value
|
|
556
|
+
value() {
|
|
657
557
|
return this[LAYOUT];
|
|
658
558
|
}
|
|
659
559
|
},
|
|
660
560
|
/** @type {function} */
|
|
661
561
|
clone: {
|
|
662
|
-
value
|
|
663
|
-
|
|
562
|
+
value(exclude_blocks) {
|
|
563
|
+
const filter = [LAYOUT, EXTEND, BUFFER];
|
|
664
564
|
if (exclude_blocks === true) {
|
|
665
565
|
filter.push(BLOCKS);
|
|
666
566
|
}
|
|
@@ -669,39 +569,35 @@
|
|
|
669
569
|
},
|
|
670
570
|
/** @type {function} */
|
|
671
571
|
extend: {
|
|
672
|
-
value
|
|
572
|
+
value(layout) {
|
|
673
573
|
this.setExtend(true);
|
|
674
574
|
this.setLayout(layout);
|
|
675
575
|
}
|
|
676
576
|
},
|
|
677
577
|
/** @type {function} */
|
|
678
578
|
echo: {
|
|
679
|
-
value
|
|
680
|
-
|
|
681
|
-
var params = [].slice.call(arguments);
|
|
682
|
-
params.forEach(buffer);
|
|
579
|
+
value() {
|
|
580
|
+
return [].slice.call(arguments).forEach(this.useBuffer);
|
|
683
581
|
}
|
|
684
582
|
},
|
|
685
583
|
/** @type {function} */
|
|
686
584
|
fn: {
|
|
687
|
-
value
|
|
688
|
-
|
|
689
|
-
var context = this;
|
|
690
|
-
return function () {
|
|
585
|
+
value(callback) {
|
|
586
|
+
return () => {
|
|
691
587
|
if (isFunction(callback)) {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
return
|
|
588
|
+
this.useBuffer.backup();
|
|
589
|
+
this.useBuffer(callback.apply(this, arguments));
|
|
590
|
+
return this.useBuffer.restore();
|
|
695
591
|
}
|
|
696
592
|
};
|
|
697
593
|
}
|
|
698
594
|
},
|
|
699
595
|
/** @type {function} */
|
|
700
596
|
macro: {
|
|
701
|
-
value
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
597
|
+
value(name, callback) {
|
|
598
|
+
const list = this.getMacro();
|
|
599
|
+
const macro = this.fn(callback);
|
|
600
|
+
const context = this;
|
|
705
601
|
list[name] = function () {
|
|
706
602
|
return context.echo(macro.apply(undefined, arguments));
|
|
707
603
|
};
|
|
@@ -709,10 +605,10 @@
|
|
|
709
605
|
},
|
|
710
606
|
/** @type {function} */
|
|
711
607
|
call: {
|
|
712
|
-
value
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
608
|
+
value(name) {
|
|
609
|
+
const list = this.getMacro();
|
|
610
|
+
const macro = list[name];
|
|
611
|
+
const params = [].slice.call(arguments, 1);
|
|
716
612
|
if (isFunction(macro)) {
|
|
717
613
|
return macro.apply(macro, params);
|
|
718
614
|
}
|
|
@@ -720,51 +616,47 @@
|
|
|
720
616
|
},
|
|
721
617
|
/** @type {function} */
|
|
722
618
|
block: {
|
|
723
|
-
value
|
|
724
|
-
|
|
725
|
-
var blocks = this.getBlocks();
|
|
619
|
+
value(name, callback) {
|
|
620
|
+
const blocks = this.getBlocks();
|
|
726
621
|
blocks[name] = blocks[name] || [];
|
|
727
622
|
blocks[name].push(this.fn(callback));
|
|
728
623
|
if (this.getExtend()) return;
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
var _next = function next() {
|
|
734
|
-
var parent = current();
|
|
624
|
+
const list = Object.assign([], blocks[name]);
|
|
625
|
+
const shift = () => list.shift();
|
|
626
|
+
const next = () => {
|
|
627
|
+
const parent = shift();
|
|
735
628
|
if (parent) {
|
|
736
|
-
return
|
|
737
|
-
|
|
629
|
+
return () => {
|
|
630
|
+
this.echo(parent(next()));
|
|
738
631
|
};
|
|
739
632
|
} else {
|
|
740
|
-
return
|
|
633
|
+
return () => {};
|
|
741
634
|
}
|
|
742
635
|
};
|
|
743
|
-
this.echo(
|
|
636
|
+
this.echo(shift()(next()));
|
|
744
637
|
}
|
|
745
638
|
},
|
|
746
639
|
/** @type {function} */
|
|
747
640
|
hasBlock: {
|
|
748
|
-
value
|
|
641
|
+
value(name) {
|
|
749
642
|
return this.getBlocks().hasOwnProperty(name);
|
|
750
643
|
}
|
|
751
644
|
},
|
|
752
645
|
/** @type {function} */
|
|
753
646
|
include: {
|
|
754
|
-
value
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
647
|
+
value(path, data, cx) {
|
|
648
|
+
const context = cx === false ? {} : this.clone(true);
|
|
649
|
+
const params = Object.assign(context, data || {});
|
|
650
|
+
const promise = this.render(path, params);
|
|
758
651
|
this.echo(promise);
|
|
759
652
|
}
|
|
760
653
|
},
|
|
761
654
|
/** @type {function} */
|
|
762
655
|
use: {
|
|
763
|
-
value
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
each(exports, function (macro, name) {
|
|
656
|
+
value(path, namespace) {
|
|
657
|
+
this.echo(Promise.resolve(this.require(path)).then(exports$1 => {
|
|
658
|
+
const list = this.getMacro();
|
|
659
|
+
each(exports$1, (macro, name) => {
|
|
768
660
|
list[[namespace, name].join('.')] = macro;
|
|
769
661
|
});
|
|
770
662
|
}));
|
|
@@ -772,34 +664,34 @@
|
|
|
772
664
|
},
|
|
773
665
|
/** @type {function} */
|
|
774
666
|
async: {
|
|
775
|
-
value
|
|
667
|
+
value(promise, callback) {
|
|
776
668
|
this.echo(Promise.resolve(promise).then(callback));
|
|
777
669
|
}
|
|
778
670
|
},
|
|
779
671
|
/** @type {function} */
|
|
780
672
|
get: {
|
|
781
|
-
value
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
673
|
+
value(name, defaults) {
|
|
674
|
+
const path = getPath(this, name, true);
|
|
675
|
+
const result = path.shift();
|
|
676
|
+
const prop = path.pop();
|
|
785
677
|
return hasProp(result, prop) ? result[prop] : defaults;
|
|
786
678
|
}
|
|
787
679
|
},
|
|
788
680
|
/** @type {function} */
|
|
789
681
|
set: {
|
|
790
|
-
value
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
682
|
+
value(name, value) {
|
|
683
|
+
const path = getPath(this, name, false);
|
|
684
|
+
const result = path.shift();
|
|
685
|
+
const prop = path.pop();
|
|
794
686
|
if (this.getParentTemplate() && hasProp(result, prop)) {
|
|
795
687
|
return result[prop];
|
|
796
688
|
}
|
|
797
|
-
return result[prop] =
|
|
689
|
+
return result[prop] = value;
|
|
798
690
|
}
|
|
799
691
|
},
|
|
800
692
|
/** @type {function} */
|
|
801
693
|
each: {
|
|
802
|
-
value
|
|
694
|
+
value(object, callback) {
|
|
803
695
|
if (isString(object)) {
|
|
804
696
|
object = this.get(object, []);
|
|
805
697
|
}
|
|
@@ -809,150 +701,139 @@
|
|
|
809
701
|
},
|
|
810
702
|
/** @type {function} */
|
|
811
703
|
el: {
|
|
812
|
-
value
|
|
704
|
+
value(tag, attr, content) {
|
|
813
705
|
content = isFunction(content) ? this.fn(content)() : content;
|
|
814
|
-
this.echo(Promise.resolve(content).then(
|
|
815
|
-
return element(tag, attr, content);
|
|
816
|
-
}));
|
|
706
|
+
this.echo(Promise.resolve(content).then(content => element(tag, attr, content)));
|
|
817
707
|
},
|
|
818
708
|
writable: true
|
|
819
709
|
},
|
|
820
710
|
/** @type {function} */
|
|
821
711
|
ui: {
|
|
822
|
-
value
|
|
712
|
+
value(layout) {},
|
|
823
713
|
writable: true
|
|
824
714
|
}
|
|
825
715
|
});
|
|
826
|
-
return
|
|
827
|
-
};
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
return new
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
return {
|
|
846
|
-
configure: configure,
|
|
847
|
-
create: create,
|
|
848
|
-
helpers: helpers
|
|
849
|
-
};
|
|
850
|
-
};
|
|
716
|
+
return EjsContext;
|
|
717
|
+
};
|
|
718
|
+
class Context {
|
|
719
|
+
#context;
|
|
720
|
+
static exports = ['create', 'globals', 'helpers'];
|
|
721
|
+
constructor(options, methods) {
|
|
722
|
+
bindContext(this, this.constructor.exports);
|
|
723
|
+
this.configure(options, methods);
|
|
724
|
+
}
|
|
725
|
+
create(data) {
|
|
726
|
+
return new this.#context(data);
|
|
727
|
+
}
|
|
728
|
+
helpers(methods) {
|
|
729
|
+
Object.assign(this.#context.prototype, methods);
|
|
730
|
+
}
|
|
731
|
+
configure(options, methods) {
|
|
732
|
+
this.#context = createContext$1(options, methods);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
851
735
|
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
var require = function require(name) {
|
|
873
|
-
var scope = createContext({});
|
|
874
|
-
return output(filePath(name), scope).then(function () {
|
|
875
|
-
return scope.getMacro();
|
|
736
|
+
class EjsInstance {
|
|
737
|
+
#cache;
|
|
738
|
+
#context;
|
|
739
|
+
#compiler;
|
|
740
|
+
#template;
|
|
741
|
+
#config = {};
|
|
742
|
+
#methods = {};
|
|
743
|
+
static exports = ['configure', 'create', 'createContext', 'render', 'require', 'preload', 'compile', 'helpers'];
|
|
744
|
+
constructor() {
|
|
745
|
+
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
746
|
+
bindContext(this, this.constructor.exports);
|
|
747
|
+
this.#methods = {};
|
|
748
|
+
this.#config = configSchema({}, options);
|
|
749
|
+
this.#context = new Context(this.#config, this.#methods);
|
|
750
|
+
this.#compiler = new Compiler(this.#config);
|
|
751
|
+
this.#cache = new Cache(this.#config);
|
|
752
|
+
this.#template = new Template(this.#config, this.#cache, this.#compiler);
|
|
753
|
+
this.helpers({
|
|
754
|
+
render: this.render,
|
|
755
|
+
require: this.require
|
|
876
756
|
});
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
757
|
+
}
|
|
758
|
+
create(options) {
|
|
759
|
+
return new this.constructor(options);
|
|
760
|
+
}
|
|
761
|
+
configure(options) {
|
|
762
|
+
if (options) {
|
|
763
|
+
configSchema(this.#config, options);
|
|
764
|
+
this.#context.configure(this.#config, this.#methods);
|
|
765
|
+
this.#compiler.configure(this.#config);
|
|
766
|
+
this.#cache.configure(this.#config);
|
|
767
|
+
this.#template.configure(this.#config);
|
|
768
|
+
}
|
|
769
|
+
return this.#config;
|
|
770
|
+
}
|
|
771
|
+
createContext(data) {
|
|
772
|
+
return this.#context.create(data);
|
|
773
|
+
}
|
|
774
|
+
preload(list) {
|
|
775
|
+
return this.#cache.load(list || {});
|
|
776
|
+
}
|
|
777
|
+
compile(content, path) {
|
|
778
|
+
return this.#compiler.compile(content, path);
|
|
779
|
+
}
|
|
780
|
+
helpers(methods) {
|
|
781
|
+
this.#context.helpers(Object.assign(this.#methods, methods));
|
|
782
|
+
}
|
|
783
|
+
async render(name, params) {
|
|
784
|
+
const data = this.createContext(params);
|
|
785
|
+
return this.#output(this.#path(name), data).then(this.#outputContent(name, data));
|
|
786
|
+
}
|
|
787
|
+
async require(name) {
|
|
788
|
+
const data = this.createContext({});
|
|
789
|
+
return this.#output(this.#path(name), data).then(() => data.getMacro());
|
|
790
|
+
}
|
|
791
|
+
#path(name) {
|
|
792
|
+
const ext = name.split('.').pop();
|
|
793
|
+
if (ext !== this.#config.extension) {
|
|
794
|
+
name = [name, this.#config.extension].join('.');
|
|
795
|
+
}
|
|
796
|
+
return name;
|
|
797
|
+
}
|
|
798
|
+
#output(path, data) {
|
|
799
|
+
return this.#template.get(path).then(callback => callback.apply(data, [data, data.useComponent, data.useElement, data.useBuffer, data.useSafeValue]));
|
|
800
|
+
}
|
|
801
|
+
#renderLayout(name, params, parentTemplate) {
|
|
802
|
+
const data = this.createContext(params);
|
|
803
|
+
if (parentTemplate) data.setParentTemplate(parentTemplate);
|
|
804
|
+
return this.#output(this.#path(name), data).then(this.#outputContent(name, data));
|
|
805
|
+
}
|
|
806
|
+
#outputContent(name, data) {
|
|
807
|
+
return content => {
|
|
808
|
+
if (data.getExtend()) {
|
|
809
|
+
data.setExtend(false);
|
|
810
|
+
return this.#renderLayout(data.getLayout(), data, name);
|
|
887
811
|
}
|
|
888
812
|
return content;
|
|
889
813
|
};
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
var scope = createContext(data);
|
|
893
|
-
if (parent) scope.setParentTemplate(parent);
|
|
894
|
-
return output(filePath(name), scope).then(outputContent(name, scope));
|
|
895
|
-
};
|
|
896
|
-
var helpers = function helpers(extendMethods) {
|
|
897
|
-
context.helpers(extend(methods, extendMethods));
|
|
898
|
-
};
|
|
899
|
-
var createContext = function createContext(data) {
|
|
900
|
-
return context.create(data);
|
|
901
|
-
};
|
|
902
|
-
var compile = function compile(content, path) {
|
|
903
|
-
return compiler.compile(content, path);
|
|
904
|
-
};
|
|
905
|
-
var preload = function preload(list) {
|
|
906
|
-
return cache.load(list || {});
|
|
907
|
-
};
|
|
908
|
-
var create = function create(config) {
|
|
909
|
-
return _EJS$1(config);
|
|
910
|
-
};
|
|
911
|
-
var output = function output(path, scope) {
|
|
912
|
-
var params = [scope, scope.useComponent, scope.useElement, scope.getBuffer(), scope.useSafeValue];
|
|
913
|
-
var globals = config.globalHelpers.filter(function (name) {
|
|
914
|
-
return isFunction(scope[name]);
|
|
915
|
-
}).map(function (name) {
|
|
916
|
-
return scope[name].bind(scope);
|
|
917
|
-
});
|
|
918
|
-
return template.get(path).then(function (callback) {
|
|
919
|
-
return callback.apply(scope, params.concat(globals));
|
|
920
|
-
});
|
|
921
|
-
};
|
|
922
|
-
helpers({
|
|
923
|
-
render: render,
|
|
924
|
-
require: require
|
|
925
|
-
});
|
|
926
|
-
return {
|
|
927
|
-
configure: configure,
|
|
928
|
-
create: create,
|
|
929
|
-
createContext: createContext,
|
|
930
|
-
render: render,
|
|
931
|
-
require: require,
|
|
932
|
-
preload: preload,
|
|
933
|
-
compile: compile,
|
|
934
|
-
helpers: helpers
|
|
935
|
-
};
|
|
936
|
-
};
|
|
814
|
+
}
|
|
815
|
+
}
|
|
937
816
|
|
|
938
|
-
|
|
939
|
-
return fetch(joinPath(path, template)).then(
|
|
940
|
-
return response.text();
|
|
941
|
-
|
|
942
|
-
|
|
817
|
+
const httpRequest = (path, template) => {
|
|
818
|
+
return fetch(joinPath(path, template)).then(response => {
|
|
819
|
+
if (response.ok) return response.text();
|
|
820
|
+
throw new TemplateNotFound(`template ${path} / ${template} not found`);
|
|
821
|
+
}, reason => {
|
|
822
|
+
return new TemplateError(reason);
|
|
943
823
|
});
|
|
944
824
|
};
|
|
945
825
|
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
826
|
+
const {
|
|
827
|
+
render,
|
|
828
|
+
createContext,
|
|
829
|
+
compile,
|
|
830
|
+
helpers,
|
|
831
|
+
preload,
|
|
832
|
+
configure,
|
|
833
|
+
create
|
|
834
|
+
} = new EjsInstance({
|
|
835
|
+
resolver: httpRequest
|
|
836
|
+
});
|
|
956
837
|
|
|
957
838
|
exports.TemplateError = TemplateError;
|
|
958
839
|
exports.TemplateNotFound = TemplateNotFound;
|