-
-
Notifications
You must be signed in to change notification settings - Fork 449
/
tangular.js
341 lines (279 loc) · 9.7 KB
/
tangular.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
(function(W) {
if (W.Tangular)
return;
var Tangular = {};
var Thelpers = Tangular.helpers = {};
Tangular.version = 'v4.0.0';
Tangular.cache = {};
Tangular.debug = false;
W.Tangular = Tangular;
W.Thelpers = Thelpers;
var SKIP = { 'null': true, 'undefined': true, 'true': true, 'false': true, 'Object': 1, 'String': 1, 'Number': 1, 'Boolean': 1, 'Date': 1, 'Array': 1, 'window': 1, 'global': 1, 'arguments': 1, 'eval': 1, 'Function': 1, 'function': 1, 'var': 1, 'let': 1, 'const': 1, 'delete': 1 };
var REG_CMDFIND = /\{\{.*?\}\}/g;
var REG_CMDCLEAN = /\{\{|\}\}/g;
var REG_ENCODE = /[<>&"]/g;
var REG_TRIM = /\n$/g;
function parseInlineVariables(line, blacklist) {
var tmp = '';
var variables = [];
var skip = 0;
for (var i = 0; i < line.length; i++) {
var c = line.charCodeAt(i);
if (!skip) {
if ((tmp && c > 47 && c < 58) || (c > 64 && c < 91) || (c > 96 && c < 123) || (c === 95 || c === 36)) {
tmp += line.charAt(i);
continue;
}
if (tmp) {
if (!SKIP[tmp] && variables.indexOf(tmp) === -1 && (!blacklist || blacklist.indexOf(tmp) === -1))
variables.push(tmp);
tmp = '';
}
}
if (c === 46 || c === 124) { // "." or "|"
skip = c;
} else if ((skip === 46 || skip === 124) && c === 40) { // ("." or "|") and "("
skip = 0;
} else if (c === 96 || c === 34 || c === 39) { // "`" or "'" or "\""
if (c === skip)
skip = 0;
else
skip = c;
}
}
if (tmp && !SKIP[tmp] && variables.indexOf(tmp) === -1 && (!blacklist || blacklist.indexOf(tmp) === -1))
variables.push(tmp);
return variables;
}
Tangular.toArray = function(obj) {
var keys = Object.keys(obj);
var arr = [];
for (var i = 0, length = keys.length; i < length; i++)
arr.push({ key: keys[i], value: obj[keys[i]] });
return arr;
};
function Template() {
this.commands;
this.variables;
this.builder;
this.split = '\0';
}
Template.prototype.compile = function(template) {
var self = this;
var ifcount = 0;
var loopcount = 0;
var tmp;
var loops = [];
self.variables = {};
self.commands = [];
self.builder = template.replace(REG_CMDFIND, function(text) {
var cmd = text.replace(REG_CMDCLEAN, '').trim();
var variable = null;
var helpers = null;
var index;
var isif = false;
var isloop = false;
var iscode = true;
if (cmd === 'fi') {
ifcount--;
// end of condition
} else if (cmd === 'end') {
loopcount--;
// end of loop
loops.pop();
} else if (cmd.substring(0, 3) === 'if ') {
// condition
ifcount++;
variable = parseInlineVariables(cmd.substring(3), loops);
if (variable.length) {
for (var i = 0; i < variable.length; i++) {
var name = variable[i];
if (self.variables[name])
self.variables[name]++;
else
self.variables[name] = 1;
}
} else
variable = null;
isif = true;
iscode = true;
} else if (cmd.substring(0, 8) === 'foreach ') {
loopcount++;
// loop
tmp = cmd.substring(8).split(' ');
loops.push(tmp[0].trim());
index = tmp[2].indexOf('.');
if (index !== -1)
tmp[2] = tmp[2].substring(0, index);
variable = tmp[2].trim();
if (loops.indexOf(variable) === -1) {
if (self.variables[variable])
self.variables[variable]++;
else
self.variables[variable] = 1;
variable = [variable];
}
else
variable = null;
isloop = true;
} else if (cmd.substring(0, 8) === 'else if ') {
// else if
variable = parseInlineVariables(cmd.substring(8), loops);
if (variable.length) {
for (var i = 0; i < variable.length; i++) {
var name = variable[i];
if (self.variables[name])
self.variables[name]++;
else
self.variables[name] = 1;
}
} else
variable = null;
isif = true;
} else if (cmd !== 'continue' && cmd !== 'break' && cmd !== 'else') {
variable = parseInlineVariables(cmd);
for (var i = 0; i < variable.length; i++) {
var v = variable[i];
if (self.variables[v])
self.variables[v]++;
else
self.variables[v] = 1;
}
if (!variable.length)
variable = null;
if (cmd.indexOf('|') === -1)
cmd += ' | encode';
helpers = cmd.split('|');
cmd = helpers[0];
helpers = helpers.slice(1);
if (helpers.length) {
for (var i = 0; i < helpers.length; i++) {
var helper = helpers[i].trim();
index = helper.indexOf('(');
if (index === -1) {
helper = 'Thelpers.$execute(model,\'' + helper + '\',\7)';
} else
helper = 'Thelpers.$execute(model,\'' + helper.substring(0, index) + '\',\7,' + helper.substring(index + 1);
helpers[i] = helper;
}
} else
helpers = null;
cmd = self.safe(cmd.trim() || 'model');
iscode = false;
}
self.commands.push({ index: self.commands.length, cmd: cmd, ifcount: ifcount, loopcount: loopcount, variable: variable, helpers: helpers, isloop: isloop, isif: isif, iscode: iscode });
return self.split;
}).split(self.split);
for (var i = 0; i < self.builder.length; i++) {
var m = self.builder[i];
self.builder[i] = m ? m.replace(REG_TRIM, '') : m;
}
return self.make();
};
Template.prototype.safe = function(cmd) {
var arr = cmd.split('.');
var output = [];
for (var i = 1; i < arr.length; i++) {
var k = arr.slice(0, i).join('.');
output.push(k + '==null?\'\':');
}
return output.join('') + arr.join('.');
};
Template.prototype.make = function() {
var self = this;
var builder = ['var $output=$text[0];var $tmp;var $index=0;'];
for (var i = 0, length = self.commands.length; i < length; i++) {
var cmd = self.commands[i];
var tmp;
i && builder.push('$output+=$text[' + i + '];');
if (cmd.iscode) {
if (cmd.isloop) {
var name = '$i' + Math.random().toString(16).substring(3, 6);
var namea = name + 'a';
tmp = cmd.cmd.substring(cmd.cmd.lastIndexOf(' in ') + 4).trim();
tmp = namea + '=' + self.safe(tmp) + ';if(!(' + namea + ' instanceof Array)){if(' + namea + '&&typeof(' + namea + ')===\'object\')' + namea + '=Tangular.toArray(' + namea + ')}if(' + namea + ' instanceof Array&&' + namea + '.length){for(var ' + name + '=0,' + name + 'l=' + namea + '.length;' + name + '<' + name + 'l;' + name + '++){$index=' + name + ';var ' + cmd.cmd.split(' ')[1] + '=' + namea + '[' + name + '];';
builder.push(tmp);
} else if (cmd.isif) {
if (cmd.cmd.substring(0, 8) === 'else if ')
builder.push('}' + cmd.cmd.substring(0, 8).trim() + '(' + cmd.cmd.substring(8).trim() + '){');
else
builder.push(cmd.cmd.substring(0, 3).trim() + '(' + cmd.cmd.substring(3).trim() + '){');
} else {
switch (cmd.cmd) {
case 'else':
builder.push('}else{');
break;
case 'end':
builder.push('}}');
break;
case 'fi':
builder.push('}');
break;
case 'break':
builder.push('break;');
break;
case 'continue':
builder.push('continue;');
break;
}
}
} else {
if (cmd.helpers) {
var str = '';
for (var j = 0; j < cmd.helpers.length; j++) {
var helper = cmd.helpers[j];
if (j === 0)
str = helper.replace('\7', cmd.cmd.trim()).trim();
else
str = helper.replace('\7', str.trim());
}
builder.push('$tmp=' + str + ';if($tmp!=null)$output+=$tmp;');
} else
builder.push('if(' + cmd.cmd + '!=null)$output+=' + cmd.cmd + ';');
}
}
builder.push((length ? ('$output+=$text[' + length + '];') : '') + 'return $output.charAt(0) === \'\\n\'?$output.substring(1):$output;');
delete self.variables.$;
var variables = Object.keys(self.variables);
var names = ['$ || {}', 'model'];
for (var i = 0; i < variables.length; i++)
names.push('model.' + variables[i]);
var code = 'var tangular=function($,model' + (variables.length ? (',' + variables.join(',')) : '') + '){' + builder.join('') + '};return function(model,$){return tangular(' + names.join(',') + ');}';
return (new Function('$text', code))(self.builder);
};
Thelpers.$execute = function(model, name, a, b, c, d, e, f, g, h) {
if (Thelpers[name] == null) {
console && console.warn('Tangular: missing helper', '"' + name + '"');
return a;
}
return Thelpers[name].call(model, a, b, c, d, e, f, g, h);
};
Thelpers.encode = function(value) {
return value == null ? '' : (value + '').replace(REG_ENCODE, function(c) {
switch (c) {
case '&': return '&';
case '<': return '<';
case '>': return '>';
case '"': return '"';
}
return c;
});
};
Thelpers.raw = function(value) {
return value;
};
Tangular.render = function(template, model, repository) {
return new Template().compile(template)(model == null ? {} : model, repository);
};
Tangular.compile = function(template) {
return new Template().compile(template);
};
Tangular.register = function(name, fn) {
Thelpers[name] = fn;
return Tangular;
};
Thelpers.pluralize = function(r,e,t,a,n){ return r||(r=0),'number'!=typeof r&&(r=parseFloat(r.toString().replace(/\s/g,'').replace(',','.'))),r.pluralize(e,t,a,n); };
Thelpers.format=function(r,e,t,a){var n=typeof r;if(r==0||r==null)return'';if('number'===n||r instanceof Date)return r.format(e==null?null:e,t,a);'string'!==n&&(r=r.toString()),r=r.trim();for(var i=!1,o=0,f=0,u=0,l=r.length;l>u;u++){var g=r.charCodeAt(u);if(58===g||32===g||84===g){i=!0;break;}if(45===g){if(o++,1===o)continue;i=!0;break;}if(46===g){if(f++,1===f)continue;i=!0;break;}}return i?r.parseDate().format(e||'dd.MM.yyyy'):r.parseFloat().format(e,t,a);};
Thelpers.def=function(e,n){return e?Thelpers.encode(e):n||'---';};
Thelpers.currency=function(e,t){switch(typeof e){case'number':return e.currency(t);case'string':return e.parseFloat().currency(t);default:return'';}};
})(global);