-
Notifications
You must be signed in to change notification settings - Fork 240
/
template.js
244 lines (237 loc) · 6.84 KB
/
template.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
/*
* Geddy JavaScript Web development framework
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
var utils = require('../../../utils')
, ejs = {};
ejs.Template = function (p) {
var UNDEF;
var params = p || {};
this.mode = null;
this.templateText = params.text ||
// If you don't want to use Fleegix.js,
// override getTemplateTextFromNode to use
// textarea node value for template text
this.getTemplateTextFromNode(params.node);
this.afterLoaded = params.afterLoaded;
this.source = '';
this.markup = UNDEF;
// Try to get from URL
if (typeof this.templateText == 'undefined') {
// If you don't want to use Fleegix.js,
// override getTemplateTextFromUrl to use
// files for template text
this.getTemplateTextFromUrl(params);
}
};
ejs.Template.prototype = new function () {
var _REGEX = /(<%%)|(%%>)|(<%=)|(<%-)|(<%#)|(<%)|(%>\n)|(%>)|(\n)/;
this.modes = {
EVAL: 'eval'
, ESCAPED: 'escaped'
, RAW: 'raw'
, APPEND: 'append'
, COMMENT: 'comment'
, LITERAL: 'literal'
};
this.getTemplateTextFromNode = function (node) {
// Requires the fleegix.xhr module
if (typeof fleegix.string == 'undefined') {
throw('Requires fleegix.string module.'); }
var ret;
if (node) {
ret = node.value;
ret = fleegix.string.unescapeXML(ret);
ret = fleegix.string.trim(ret);
}
return ret;
};
this.getTemplateTextFromUrl = function (params) {
// Requires the fleegix.xhr module
if (typeof fleegix.xhr == 'undefined') {
throw('Requires fleegix.xhr module.'); }
var _this = this;
var url = params.url;
var noCache = params.preventCache || false;
// Found text in cache, and caching is turned on
if (text && !noCache) {
this.templateText = text;
}
// Otherwise go grab the text
else {
// Callback for setting templateText and caching --
// used for both sync and async loading
var callback = function (s) {
_this.templateText = s;
ejs.templateTextCache[url] = s;
// Use afterLoaded hook if set
if (typeof _this.afterLoaded == 'function') {
_this.afterLoaded();
}
};
var opts;
if (params.async) {
opts = {
url: url,
method: 'GET',
preventCache: noCache,
async: true,
handleSuccess: callback
};
// Get templ text asynchronously, wait for
// loading to exec the callback
fleegix.xhr.send(opts);
}
else {
opts = {
url: url,
method: 'GET',
preventCache: noCache,
async: false
};
// Get the templ text inline and pass directly to
// the callback
text = fleegix.xhr.send(opts);
callback(text);
}
}
};
this.process = function (p) {
var params = p || {};
this.data = params.data || {};
var domNode = params.node;
// Cache/reuse the generated template source for speed
this.source = this.source || '';
if (!this.source) { this.generateSource(); }
// Eval the template with the passed data
// Use 'with' to give local scoping to data obj props
// ========================
var _output = ''; // Inner scope var for eval output
with (this.data) {
eval(this.source);
}
this.markup = _output;
if (domNode) {
domNode.innerHTML = this.markup;
}
return this.markup;
};
this.generateSource = function () {
var line = '';
var matches = this.parseTemplateText();
if (matches) {
for (var i = 0; i < matches.length; i++) {
line = matches[i];
if (line) {
this.scanLine(line);
}
}
}
};
this.parseTemplateText = function() {
var str = this.templateText;
var pat = _REGEX;
var result = pat.exec(str);
var arr = [];
while (result) {
var firstPos = result.index;
var lastPos = pat.lastIndex;
if (firstPos !== 0) {
arr.push(str.substring(0, firstPos));
str = str.slice(firstPos);
}
arr.push(result[0]);
str = str.slice(result[0].length);
result = pat.exec(str);
}
if (str !== '') {
arr.push(str);
}
return arr;
};
this.scanLine = function (line) {
var _this = this;
var _addOutput = function () {
// Preserve literal slashes
line = line.replace(/\\/g, '\\\\');
// Convert linebreaks
line = line.replace(/\n/, '\\n');
line = line.replace(/\r/, '\\r');
// Escape double-quotes -- this will be the delimiter
// during execution
line = line.replace(/"/g, '\\"');
_this.source += '_output += "' + line + '";';
};
switch (line) {
case '<%':
this.mode = this.modes.EVAL;
break;
case '<%=':
this.mode = this.modes.ESCAPED;
break;
case '<%-':
this.mode = this.modes.RAW;
break;
case '<%#':
this.mode = this.modes.COMMENT;
break;
case '<%%':
this.mode = this.modes.LITERAL;
this.source += '_output += "' + line + '";';
break;
case '%>':
case '%>\n':
if (this.mode == this.modes.LITERAL) {
_addOutput();
}
this.mode = null;
break;
default:
// In script mode, depends on type of tag
if (this.mode) {
switch (this.mode) {
// Just executing code
case this.modes.EVAL:
this.source += line;
break;
// Exec, esc, and output
case this.modes.ESCAPED:
// Add the exec'd, escaped result to the output
this.source += '_output += utils.string.escapeXML(' +
line.replace(/;\S*/, '') + ');';
break;
// Exec and output
case this.modes.RAW:
// Add the exec'd result to the output
this.source += '_output += ' + line + ';';
break;
case this.modes.COMMENT:
// Do nothing
break;
// Literal <%% mode, append as raw output
case this.modes.LITERAL:
_addOutput();
break;
}
}
// In string mode, just add the output
else {
_addOutput();
}
}
};
};
exports.Template = ejs.Template;