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 | 1
1
4
1
3
1
2
1
124
95
95
95
95
775
775
771
1
1
770
4
4
766
4
4
762
4
775
95
1
12
1
11
11
11
11
8
3
2
3
1
3
1
1
1
1
224
130
130
1
129
223
223
223
223
223
223
223
223
1
222
10
220
220
64
71
220
16
220
220
223
223
223
223
89
89
76
76
13
223
71
71
71
71
33
38
36
2
71
2
2
2
1
1
1
2
43
43
43
10
6
4
10
33
2
2
2
31
2
29
1
28
2
2
7
2
26
4
22
22
22
20
22
22
22
2
22
22
2
2
2
2
2
2
20
| module.exports = function(Velocity, utils) {
'use strict';
function getSize(obj) {
if (utils.isArray(obj)) {
return obj.length;
} else if (utils.isObject(obj)) {
return utils.keys(obj).length;
}
return undefined;
}
/**
* unicode转码
*/
function convert(str) {
if (typeof str !== 'string') return str;
var result = ""
var escape = false
var i, c, cstr;
for (i = 0 ; i < str.length ; i++) {
c = str.charAt(i);
if ((' ' <= c && c <= '~') || (c === '\r') || (c === '\n')) {
if (c === '&') {
cstr = "&"
escape = true
} else if (c === '<') {
cstr = "<"
escape = true
} else if (c === '>') {
cstr = ">"
escape = true
} else {
cstr = c.toString()
}
} else {
cstr = "&#" + c.charCodeAt().toString() + ";"
}
result = result + cstr
}
return escape ? result : str
}
function getter(base, property) {
// get(1)
if (typeof property === 'number') {
return base[property];
}
var letter = property.charCodeAt(0);
var isUpper = letter < 91;
var ret = base[property];
if (ret !== undefined) {
return ret;
}
if (isUpper) {
// Address => address
property = String.fromCharCode(letter).toLowerCase() + property.slice(1);
}
if (!isUpper) {
// address => Address
property = String.fromCharCode(letter).toUpperCase() + property.slice(1);
}
return base[property];
}
utils.mixin(Velocity.prototype, {
// 增加某些函数,不需要执行html转义
addIgnoreEscpape: function(key) {
Eif (!utils.isArray(key)) key = [key]
utils.forEach(key, function(key) {
this.config.unescape[key] = true
}, this)
},
/**
* 引用求值
* @param {object} ast 结构来自velocity.yy
* @param {bool} isVal 取值还是获取字符串,两者的区别在于,求值返回结果,求
* 字符串,如果没有返回变量自身,比如$foo
*/
getReferences: function(ast, isVal) {
if (ast.prue) {
var define = this.defines[ast.id];
if (utils.isArray(define)) {
return this._render(define);
}
if (ast.id in this.config.unescape) ast.prue = false;
}
var escape = this.config.escape;
var isSilent = this.silence || ast.leader === "$!";
var isfn = ast.args !== undefined;
var context = this.context;
var ret = context[ast.id];
var local = this.getLocal(ast);
var text = Velocity.Helper.getRefText(ast);
if (text in context) {
return (ast.prue && escape) ? convert(context[text]) : context[text];
}
if (ret !== undefined && isfn) {
ret = this.getPropMethod(ast, context, ast);
}
if (local.isLocaled) ret = local['value'];
if (ast.path && ret !== undefined) {
utils.some(ast.path, function(property) {
// 第三个参数,返回后面的参数ast
ret = this.getAttributes(property, ret, ast);
}, this);
}
if (isVal && ret === undefined) {
ret = isSilent ? '' : Velocity.Helper.getRefText(ast);
}
ret = (ast.prue && escape) ? convert(ret) : ret;
return ret;
},
/**
* 获取局部变量,在macro和foreach循环中使用
*/
getLocal: function(ast) {
var id = ast.id;
var local = this.local;
var ret = false;
var isLocaled = utils.some(this.conditions, function(contextId) {
var _local = local[contextId];
if (id in _local) {
ret = _local[id];
return true;
}
return false;
}, this);
return {
value: ret,
isLocaled: isLocaled
};
},
/**
* $foo.bar 属性求值,最后面两个参数在用户传递的函数中用到
* @param {object} property 属性描述,一个对象,主要包括id,type等定义
* @param {object} baseRef 当前执行链结果,比如$a.b.c,第一次baseRef是$a,
* 第二次是$a.b返回值
* @private
*/
getAttributes: function(property, baseRef, ast) {
/**
* type对应着velocity.yy中的attribute,三种类型: method, index, property
*/
var type = property.type;
var ret;
var id = property.id;
if (type === 'method') {
ret = this.getPropMethod(property, baseRef, ast);
} else if (type === 'property') {
ret = baseRef[id];
} else {
ret = this.getPropIndex(property, baseRef);
}
return ret;
},
/**
* $foo.bar[1] index求值
* @private
*/
getPropIndex: function(property, baseRef) {
var ast = property.id;
var key;
if (ast.type === 'references') {
key = this.getReferences(ast);
} else Eif (ast.type === 'integer') {
key = ast.value;
} else {
key = ast.value;
}
return baseRef[key];
},
/**
* $foo.bar()求值
*/
getPropMethod: function(property, baseRef, ast) {
var id = property.id;
var ret = '';
// getter 处理
if (id.indexOf('get') === 0 && !(id in baseRef)) {
if (id.length === 3) {
// get('address')
ret = getter(baseRef, this.getLiteral(property.args[0]));
} else {
// getAddress()
ret = getter(baseRef, id.slice(3));
}
return ret;
// setter 处理
} else if (id.indexOf('set') === 0 && !baseRef[id]) {
baseRef[id.slice(3)] = this.getLiteral(property.args[0]);
// $page.setName(123)
baseRef.toString = function() { return ''; };
return baseRef;
} else if (id.indexOf('is') === 0 && !(id in baseRef)) {
return getter(baseRef, id.slice(2));
} else if (id === 'keySet') {
return utils.keys(baseRef);
} else if (id === 'entrySet') {
ret = [];
utils.forEach(baseRef, function(value, key) {
ret.push({key: key, value: value});
});
return ret;
} else if (id === 'size') {
return getSize(baseRef);
} else {
ret = baseRef[id];
var args = [];
utils.forEach(property.args, function(exp) {
args.push(this.getLiteral(exp));
}, this);
Eif (ret && ret.call) {
var that = this;
baseRef.eval = function() {
return that.eval.apply(that, arguments);
};
try {
ret = ret.apply(baseRef, args);
} catch (e) {
var pos = ast.pos;
var text = Velocity.Helper.getRefText(ast);
var err = ' on ' + text + ' at L/N ' +
pos.first_line + ':' + pos.first_column;
e.name = '';
e.message += err;
throw new Error(e);
}
} else {
ret = undefined;
}
}
return ret;
}
})
}
|