forked from jser-cc/ccTpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcc-template.js
More file actions
177 lines (144 loc) · 4.13 KB
/
cc-template.js
File metadata and controls
177 lines (144 loc) · 4.13 KB
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
;(function (global, factory) {
if (typeof module !== 'undefined' && module && module.exports) {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define(function () {
return factory();
});
} else if (typeof define === 'function' && define.cmd) {
define(function (require, exports, module) {
module.exports = factory();
});
} else {
global.cc = global.cc || {};
global.cc.Template = factory();
}
})(this, function() {
// List of HTML entities for escaping.
var escapeMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`'
};
var unescapeMap = invert(escapeMap);
function invert(obj) {
return Object.keys(obj)
.reduce(function (memo, key, index) {
var val = obj[key];
memo[val] = key;
return memo;
}, {});
}
function defaults(a, b) {
for (var p in a) {
if (a.hasOwnProperty(p) && b[p] !== void 0) {
a[p] = b[p];
}
}
return a;
}
function Template(options) {
options = options || {};
if (options.escapeMap) {
this.escape = this.createEscaper(options.escapeMap);
}
if (options.unescapeMap) {
this.unescape = this.createEscaper(options.unescapeMap);
}
this.options = defaults(
Template.getDefaultOptions(),
options
);
}
Template.getDefaultOptions = function () {
return {
scope: 'scope',
regexpHead: '<%',
regexpTail: '%>',
};
};
// Functions for escaping and unescaping strings to/from HTML interpolation.
Template.createEscaper = function(map) {
var escaper = function(match) {
return map[match];
};
// Regexes for identifying a key that needs to be escaped.
var source = '(?:' + Object.keys(map).join('|') + ')';
var testRegexp = RegExp(source);
var replaceRegexp = RegExp(source, 'g');
return function(string) {
string = string == null ? '' : '' + string;
return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
};
};
Template.prototype.escape = Template.createEscaper(escapeMap);
Template.prototype.unescape = Template.createEscaper(unescapeMap);
Template.prototype.createEscaper = Template.createEscaper;
Template.prototype._getTemplateRegExp = function () {
return new RegExp(this.options.regexpHead + '([\\s\\S]+?)' + this.options.regexpTail, 'g');
};
Template.prototype.match = function (str) {
var reg = this._getTemplateRegExp();
var match;
var matches = [];
while (match = reg.exec(str)) {
matches.push(match);
}
return matches;
};
Template.prototype.compile = function (str) {
var matches = this.match(str);
var match;
var sign;
var content;
var ret = 'var _ret = "";\n';
var lastIndex = 0;
while (match = matches.shift()) {
sign = match[1][0];
content = match[1].slice(1).trim();
ret += '_ret += "' + str.slice(lastIndex, match.index).replace(/\n/g, '\\n') + '";';
switch (sign) {
case '=':
ret += '_ret += this.escape(' + content + ');\n';
break;
case '-':
ret += '_ret += this.unescape(' + content + ');\n';
break;
case '!':
ret += '_ret += ((' + content + ' || "") + "")' + ';\n';
break;
default:
ret += content + '\n';
break;
}
lastIndex = match.index + match[0].length;
}
ret += '_ret += "' + str.slice(lastIndex).replace(/\n/g, '\\n') + '";\n';
ret += 'return _ret;\n';
var render;
try {
render = new Function(this.options.scope, ret);
} catch (error) {
error.source = ret;
throw error;
}
var self = this;
var compiled = function (scope, ctx) {
if (ctx) {
if (!ctx.escape) {
ctx.escape = self.escape;
}
if (!ctx.unescape) {
ctx.unescape = self.unescape;
}
}
return render.call(ctx || self, scope);
};
compiled.source = 'function(' + this.options.scope + '){\n' + ret + '}';
return compiled;
};
return Template;
});