All files / src/libs safe.js

90.91% Statements 20/22
66.67% Branches 4/6
100% Functions 2/2
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                            1x 1x 1x 1x 1x 1x 1x   1x 1x 1x                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x    
/**
 * @browser safe functions
 * @author heyli
 * @date 2016.07.30
 */
 
/**
 * html实体编码
 * @param  {String} str html文本
 * @return {String}     经html实体编码后的html文本
 */
export function encodeHTML(str) {
    // > 实体标签
    // " Unicode 编码(可以用charCodeAt方法查看某字符对应的unicode编码)
    let s = '';
    Iif (!str || str.length === 0) return '';
    s = str.replace(/&/g, '&');
    s = s.replace(/</g, '&lt;');
    s = s.replace(/>/g, '&gt;');
    s = s.replace(/\'/g, '&#39;');
    s = s.replace(/\"/g, '&quot;');
    // 空格和换行其实可以不转
    s = s.replace(/ /g, '&nbsp;');
    s = s.replace(/\n/g, '<br>');
    return s;
}
 
/**
 * html实体编码转义
 * @param  {String} str html文本
 * @return {String}     经html实体编码转义后的html文本
 */
export function decodeHTML(str) {
    let s = '';
    Iif (str.length === 0) return '';
    s = str.replace(/&#38;/g, '&');
    s = s.replace(/&lt;/g, '<');
    s = s.replace(/&gt;/g, '>');
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, '"');
    s = s.replace(/&nbsp;/g, ' ');
    s = s.replace(/<br>/g, '\n');
    return s;
}