//转换内容
layui.data.content = function(content){
//支持的html标签
var html = function(end){
return new RegExp('\\n*\\['+ (end||'') +'(code|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');
};
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){ //转义图片
return '<img class="layui-layim-photos" src="' + img.replace(/(^img\[)|(\]$)/g, '') + '">';
})
.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-layim-file" href="'+ href +'" download target="_blank"><i class="layui-icon"></i><cite>'+ (text||href) +'</cite></a>';
})
.replace(/audio\[([^\s]+?)\]/g, function(audio){ //转义音频
return '<div class="layui-unselect layui-layim-audio" layim-event="playAudio" data-src="' + audio.replace(/(^audio\[)|(\]$)/g, '') + '"><i class="layui-icon"></i><p>音频消息</p></div>';
})
.replace(/video\[([^\s]+?)\]/g, function(video){ //转义音频
return '<div class="layui-unselect layui-layim-video" layim-event="playVideo" data-src="' + video.replace(/(^video\[)|(\]$)/g, '') + '"><i class="layui-icon"></i></div>';
})
.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;
};
网友评论