Blame view

utils/html-process.js 866 Bytes
1 2 3 4 5 6
/**
 * html字符处理
 */

'use strict';
lijing authored
7
const _htmlMap = {'<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;'};
郝肖肖 authored
8
const _EscapeMap = {lt: '<', gt: '>', nbsp: ' ', amp: '&', quot: '"'};
9 10 11 12 13

/**
 * [移除html标签]
 */
const removeHtml = (str) => {
14
    return (str || '').replace(/<[^>]+>/g, '');
lijing authored
15
};
16 17 18 19 20

/**
 * [html转为转义]
 */
const htmlToEscape = (html) => {
ccbikai(👎🏻🍜) authored
21
    return (html || '').replace(/[<>&"]/g, (e) => {
22
        return _htmlMap[e];
郝肖肖 authored
23
	                                                                                });
lijing authored
24
};
25 26 27 28 29

/**
 * [转义符转为html]
 */
const escapeToHtml = (str) => {
ccbikai(👎🏻🍜) authored
30
    return (str || '').replace(/&(lt|gt|nbsp|amp|quot);/g, (match, e) => {
31
        return _EscapeMap[e];
郝肖肖 authored
32
	                                                                                });
lijing authored
33
};
34 35

module.exports = {
36 37 38
    removeHtml,
    htmlToEscape,
    escapeToHtml
lijing authored
39
};