Skip to content

Commit

Permalink
MOD: the module that will be used in template.
Browse files Browse the repository at this point in the history
  • Loading branch information
wulijian committed Feb 2, 2013
1 parent 5cecde1 commit d45f6af
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions lib/html.js
@@ -0,0 +1,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, '&amp;')
.replace(rLt, '&lt;')
.replace(rGt, '&gt;')
.replace(rApos, '&#39;')
.replace(rQuot, '&quot;') :
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;
};
});

0 comments on commit d45f6af

Please sign in to comment.