66 lines
3.4 KiB
JavaScript
66 lines
3.4 KiB
JavaScript
// 转义聊天内容中的特殊字符
|
|
function replaceContent(content, id, payment, width, height) {
|
|
// 支持的html标签
|
|
var html = function (end) {
|
|
return new RegExp('\\n*\\[' + (end || '') + '(pre|div|span|p|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)([\\s\\S]*?)\\]\\n*', 'g');
|
|
};
|
|
if (isNumber(content)) return content;
|
|
content = (content || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&')
|
|
.replace(/</g, '<').replace(/>/g, '>').replace(/'/g, ''').replace(/"/g, '"') // XSS
|
|
.replace(/@(\S+)(\s+?|$)/g, '@<a href="javascript:;">$1</a>$2') // 转义@
|
|
|
|
.replace(/face\[([^\s\[\]]+?)\]/g, function (face) { // 转义表情
|
|
var alt = face.replace(/^face/g, '');
|
|
return '<img alt="' + alt + '" title="' + alt + '" src="' + faces[alt] + '">';
|
|
})
|
|
.replace(/img\[([^\s]+?)\]/g, function (img) { // 转义图片
|
|
if (payment) {
|
|
return '<img class="layui-mylink-photos" onclick="payShowLayer(this)" id="' + id + '" payment="' + payment + '" src="' + img.replace(/(^img\[)|(\]$)/g, '') + '" width="' + width + '" height="' + height + '">';
|
|
}
|
|
return '<img id="' + id + '" onclick="showBigPic(this)" layer-src="' + img.replace(/(^img\[)|(\]$)/g, '') + '" src="' + img.replace(/(^img\[)|(\]$)/g, '') + '" width="' + width + '" height="' + height + '">';
|
|
|
|
})
|
|
.replace(/file\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) { // 转义文件
|
|
var href = (str.match(/file\(([\s\S]+?)\)\[/) || [])[1];
|
|
var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
|
|
if (!href) return str;
|
|
return '<a class="layui-mylink-file" href="' + href + '" download target="_blank"><i class="layui-icon"></i><cite>' + (text || href) + '</cite></a>';
|
|
})
|
|
.replace(/a\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) { // 转义链接
|
|
var href = (str.match(/a\(([\s\S]+?)\)\[/) || [])[1];
|
|
var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
|
|
if (!href) return str;
|
|
return '<a href="' + href + '" target="_blank">' + (text || href) + '</a>';
|
|
}).replace(html(), '\<$1 $2\>').replace(html('/'), '\</$1\>') // 转移HTML代码
|
|
.replace(/\n/g, '<br>') // 转义换行
|
|
|
|
return content;
|
|
};
|
|
|
|
// 表情替换
|
|
var faces = function () {
|
|
var alt = getFacesIcon(), arr = {};
|
|
layui.each(alt, function (index, item) {
|
|
arr[item] = '/static/service/js/layui/images/face/' + index + '.png';
|
|
});
|
|
return arr;
|
|
}();
|
|
|
|
// 表情对应数组
|
|
function getFacesIcon() {
|
|
return ["[白眼]", "[抱歉]", "[崩溃]", "[啵一个]", "[不理你了]", "[不要]", "[呲牙]", "[打呼]", "[担心]", "[得意]", "[发抖]",
|
|
"[犯错]", "[愤怒]", "[感冒]", "[鬼脸]", "[汗]", "[吼吼]", "[焦虑]", "[惊呆]", "[囧]", "[可爱]", "[可怕]",
|
|
"[冷汗]", "[冷漠]", "[脸红]", "[流鼻涕]", "[流口水]", "[流泪]", "[冒金星]", "[么么哒]", "[难受]", "[气死了]", "[色]", "[傻笑]", "[生气]",
|
|
"[使眼色]", "[是吗]", "[睡]", "[思考]", "[天使]", "[调皮]", "[偷笑]", "[吐舌头]", "[微笑]", "[委屈]", "[无语]", "[嫌弃]", "[笑哭]",
|
|
"[笑死了]"
|
|
]
|
|
}
|
|
|
|
function isNumber(value) {
|
|
var patrn = /^(-)?\d+(\.\d+)?$/;
|
|
if (patrn.exec(value) == null || value == "") {
|
|
return false
|
|
} else {
|
|
return true
|
|
}
|
|
} |