/
html.js
60 lines (58 loc) · 1.75 KB
/
html.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
/**
* ktemplate 模板公用函数
* @author: Knight
* @version: 0.0.1
*/
define(function (require, exports, module) {
var rAmp = /&/g,
rLt = /</g,
rGt = />/g,
rApos = /\'/g,
rQuot = /\"/g,
hChars = /[&<>\"\']/;
var coerceToString = function (val) {
return String((val === null || val === undefined) ? '' : val);
};
/**
* 编码 html , KTemplate中用到
* @param str
* @return {String|XML}
*/
exports.escape = function (str) {
str = coerceToString(str);
return hChars.test(str) ?
str
.replace(rAmp, '&')
.replace(rLt, '<')
.replace(rGt, '>')
.replace(rApos, ''')
.replace(rQuot, '"') :
str;
};
var utils = {};
['forEach', 'some', 'every', 'filter', 'map'].forEach(function (fnName) {
utils[fnName] = function (arr, fn, context) {
if (!arr || typeof arr == 'string') return arr;
context = context || this;
if (arr[fnName]) {
return arr[fnName](fn, context);
} else {
var keys = Object.keys(arr);
return keys[fnName](function (key) {
return fn.call(context, arr[key], key, arr);
}, context);
}
};
});
exports.mixin = function mixin(to, from) {
utils.forEach(from, function (val, key) {
var toString = {}.toString.call(val);
if (toString == '[object Array]' || toString == '[object Object]') {
to[key] = mixin(val, to[key] || {});
} else {
to[key] = val;
}
});
return to;
};
});